sysfile.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2005 by Free Pascal development team
  4. Low level file functions
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. { Enable this for file handling debug }
  12. {DEFINE ASYS_FPC_FILEDEBUG}
  13. {*****************************************************************************
  14. File-handling Support Functions
  15. *****************************************************************************}
  16. type
  17. { AmigaOS does not automatically close opened files on exit back to }
  18. { the operating system, therefore as a precuation we close all files }
  19. { manually on exit. }
  20. PFileList = ^TFileList;
  21. TFileList = record { no packed, must be correctly aligned }
  22. handle : THandle; { Handle to file }
  23. next : PFileList; { Next file in list }
  24. buffered : boolean; { used buffered I/O? }
  25. end;
  26. var
  27. ASYS_fileList: PFileList; public name 'ASYS_FILELIST'; { List pointer to opened files }
  28. { Function to be called at program shutdown, to close all opened files }
  29. procedure CloseList(l: PFileList);
  30. var
  31. tmpNext : PFileList;
  32. tmpHandle : THandle;
  33. begin
  34. if l=nil then exit;
  35. ObtainSemaphore(ASYS_fileSemaphore);
  36. { First, close all tracked files }
  37. tmpNext:=l^.next;
  38. while tmpNext<>nil do begin
  39. tmpHandle:=tmpNext^.handle;
  40. if (tmpHandle<>StdInputHandle) and (tmpHandle<>StdOutputHandle)
  41. and (tmpHandle<>StdErrorHandle) then begin
  42. dosClose(tmpHandle);
  43. end;
  44. tmpNext:=tmpNext^.next;
  45. end;
  46. { Next, erase the linked list }
  47. while l<>nil do begin
  48. tmpNext:=l;
  49. l:=l^.next;
  50. dispose(tmpNext);
  51. end;
  52. ReleaseSemaphore(ASYS_fileSemaphore);
  53. end;
  54. { Function to be called to add a file to the opened file list }
  55. procedure AddToList(var l: PFileList; h: THandle); alias: 'ADDTOLIST'; [public];
  56. var
  57. p : PFileList;
  58. inList: Boolean;
  59. begin
  60. inList:=False;
  61. ObtainSemaphore(ASYS_fileSemaphore);
  62. if l<>nil then begin
  63. { if there is a valid filelist, search for the value }
  64. { in the list to avoid double additions }
  65. p:=l;
  66. while (p^.next<>nil) and (not inList) do
  67. if p^.next^.handle=h then inList:=True
  68. else p:=p^.next;
  69. p:=nil;
  70. end else begin
  71. { if the list is not yet allocated, allocate it. }
  72. New(l);
  73. l^.next:=nil;
  74. end;
  75. if not inList then begin
  76. New(p);
  77. p^.handle:=h;
  78. p^.buffered:=False;
  79. p^.next:=l^.next;
  80. l^.next:=p;
  81. end
  82. {$IFDEF ASYS_FPC_FILEDEBUG}
  83. else
  84. RawDoFmt('FPC_FILE_DEBUG: Error! Trying add filehandle a filehandle twice: $%lx !'+#10,@h,pointer(1),nil);
  85. {$ENDIF}
  86. ;
  87. ReleaseSemaphore(ASYS_fileSemaphore);
  88. end;
  89. { Function to be called to remove a file from the list }
  90. function RemoveFromList(var l: PFileList; h: THandle): boolean; alias: 'REMOVEFROMLIST'; [public];
  91. var
  92. p : PFileList;
  93. inList : Boolean;
  94. tmpList: PFileList;
  95. begin
  96. inList:=False;
  97. if l=nil then begin
  98. RemoveFromList:=inList;
  99. exit;
  100. end;
  101. ObtainSemaphore(ASYS_fileSemaphore);
  102. p:=l;
  103. while (p^.next<>nil) and (not inList) do
  104. if p^.next^.handle=h then inList:=True
  105. else p:=p^.next;
  106. if inList then begin
  107. tmpList:=p^.next^.next;
  108. dispose(p^.next);
  109. p^.next:=tmpList;
  110. end
  111. {$IFDEF ASYS_FPC_FILEDEBUG}
  112. else
  113. RawDoFmt('FPC_FILE_DEBUG: Error! Trying to remove not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
  114. {$ENDIF}
  115. ;
  116. ReleaseSemaphore(ASYS_fileSemaphore);
  117. RemoveFromList:=inList;
  118. end;
  119. { Function to check if file is in the list }
  120. function CheckInList(var l: PFileList; h: THandle): pointer; alias: 'CHECKINLIST'; [public];
  121. var
  122. p : PFileList;
  123. inList : Pointer;
  124. begin
  125. inList:=nil;
  126. if l=nil then begin
  127. CheckInList:=inList;
  128. exit;
  129. end;
  130. ObtainSemaphore(ASYS_fileSemaphore);
  131. p:=l;
  132. while (p^.next<>nil) and (inList=nil) do
  133. if p^.next^.handle=h then inList:=p^.next
  134. else p:=p^.next;
  135. {$IFDEF ASYS_FPC_FILEDEBUG}
  136. if inList=nil then
  137. RawDoFmt('FPC_FILE_DEBUG: Warning! Check for not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
  138. {$ENDIF}
  139. ReleaseSemaphore(ASYS_fileSemaphore);
  140. CheckInList:=inList;
  141. end;
  142. {****************************************************************************
  143. Low level File Routines
  144. All these functions can set InOutRes on errors
  145. ****************************************************************************}
  146. { close a file from the handle value }
  147. procedure do_close(handle : THandle);
  148. begin
  149. if RemoveFromList(ASYS_fileList,handle) then begin
  150. { Do _NOT_ check CTRL_C on Close, because it will conflict
  151. with System_Exit! }
  152. if not dosClose(handle) then
  153. dosError2InOut(IoErr);
  154. end;
  155. end;
  156. procedure do_erase(p : pchar; pchangeable: boolean);
  157. var
  158. tmpStr: array[0..255] of Char;
  159. begin
  160. tmpStr:=PathConv(strpas(p))+#0;
  161. checkCTRLC;
  162. if not dosDeleteFile(@tmpStr) then
  163. dosError2InOut(IoErr);
  164. end;
  165. procedure do_rename(p1,p2 : pchar; p1changeable, p2changeable: boolean);
  166. { quite stack-effective code, huh? :) damn path conversions... (KB) }
  167. var
  168. tmpStr1: array[0..255] of Char;
  169. tmpStr2: array[0..255] of Char;
  170. begin
  171. tmpStr1:=PathConv(strpas(p1))+#0;
  172. tmpStr2:=PathConv(strpas(p2))+#0;
  173. checkCTRLC;
  174. if not (dosRename(@tmpStr1,@tmpStr2) <> 0) then
  175. dosError2InOut(IoErr);
  176. end;
  177. function do_write(h: THandle; addr: pointer; len: longint) : longint;
  178. var dosResult: LongInt;
  179. begin
  180. checkCTRLC;
  181. do_write:=0;
  182. if (len<=0) or (h=0) or (h=-1) then exit;
  183. {$IFDEF ASYS_FPC_FILEDEBUG}
  184. if not ((h=StdOutputHandle) or (h=StdInputHandle) or
  185. (h=StdErrorHandle)) then CheckInList(ASYS_fileList,h);
  186. {$ENDIF}
  187. dosResult:=dosWrite(h,addr,len);
  188. if dosResult<0 then begin
  189. dosError2InOut(IoErr);
  190. end else begin
  191. do_write:=dosResult;
  192. end;
  193. end;
  194. function do_read(h: THandle; addr: pointer; len: longint) : longint;
  195. var dosResult: LongInt;
  196. begin
  197. checkCTRLC;
  198. do_read:=0;
  199. if (len<=0) or (h=0) or (h=-1) then exit;
  200. {$IFDEF ASYS_FPC_FILEDEBUG}
  201. if not ((h=StdOutputHandle) or (h=StdInputHandle) or
  202. (h=StdErrorHandle)) then CheckInList(ASYS_fileList,h);
  203. {$ENDIF}
  204. dosResult:=dosRead(h,addr,len);
  205. if dosResult<0 then begin
  206. dosError2InOut(IoErr);
  207. end else begin
  208. do_read:=dosResult;
  209. end
  210. end;
  211. function do_filepos(handle: THandle) : longint;
  212. var dosResult: LongInt;
  213. begin
  214. checkCTRLC;
  215. do_filepos:=-1;
  216. if CheckInList(ASYS_fileList,handle)<>nil then begin
  217. { Seeking zero from OFFSET_CURRENT to find out where we are }
  218. dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
  219. if dosResult<0 then begin
  220. dosError2InOut(IoErr);
  221. end else begin
  222. do_filepos:=dosResult;
  223. end;
  224. end;
  225. end;
  226. procedure do_seek(handle: THandle; pos: longint);
  227. begin
  228. checkCTRLC;
  229. if CheckInList(ASYS_fileList,handle)<>nil then begin
  230. { Seeking from OFFSET_BEGINNING }
  231. if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
  232. dosError2InOut(IoErr);
  233. end;
  234. end;
  235. function do_seekend(handle: THandle):longint;
  236. var dosResult: LongInt;
  237. begin
  238. checkCTRLC;
  239. do_seekend:=-1;
  240. if CheckInList(ASYS_fileList,handle)<>nil then begin
  241. { Seeking to OFFSET_END }
  242. dosResult:=dosSeek(handle,0,OFFSET_END);
  243. if dosResult<0 then begin
  244. dosError2InOut(IoErr);
  245. end else begin
  246. do_seekend:=dosSeek(handle,0,OFFSET_CURRENT);
  247. end;
  248. end;
  249. end;
  250. {$IFNDEF AMIGA_LEGACY}
  251. {$DEFINE ASYS_FILESIZE_USE_EXAMINEFH}
  252. {$ENDIF}
  253. { I changed the double-Seek filesize method which we
  254. were using for 10+ years to the new ExamineFH() method.
  255. It should be available AmigaOS 2.0+, and much faster.
  256. (I actually measured several magnitudes of improvement,
  257. especially on large files.)
  258. It should be safe since there are several libc implementations
  259. using the same method on all Amiga flavors, but if anyone has
  260. a problem with it, disable this define to revert to the old
  261. method and report the issue. (KB)
  262. Actually, as some file systems (FTPMount, probably more) do
  263. not support the ExamineFH action, lets fall back to double
  264. seek silently in that case (KB) }
  265. function do_filesize(handle : THandle) : longint;
  266. var
  267. {$IFDEF ASYS_FILESIZE_USE_EXAMINEFH}
  268. fib: PFileInfoBlock;
  269. {$ENDIF}
  270. currfilepos: longint;
  271. begin
  272. checkCTRLC;
  273. do_filesize:=-1;
  274. if CheckInList(ASYS_fileList,handle)<>nil then begin
  275. {$IFDEF ASYS_FILESIZE_USE_EXAMINEFH}
  276. fib:=AllocDosObject(DOS_FIB,nil);
  277. if fib <> nil then begin
  278. if ExamineFH(BPTR(handle), fib) then
  279. do_filesize:=fib^.fib_Size;
  280. FreeDosObject(DOS_FIB,fib);
  281. end;
  282. {$ENDIF}
  283. if do_filesize = -1 then
  284. begin
  285. currfilepos:=do_filepos(handle);
  286. do_filesize:=do_seekend(handle);
  287. do_seek(handle,currfilepos);
  288. end;
  289. end;
  290. end;
  291. { truncate at a given position }
  292. procedure do_truncate(handle: THandle; pos: longint);
  293. begin
  294. checkCTRLC;
  295. {$IFNDEF AMIGA_LEGACY}
  296. if CheckInList(ASYS_fileList,handle)<>nil then begin
  297. { Seeking from OFFSET_BEGINNING }
  298. if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  299. dosError2InOut(IoErr);
  300. end;
  301. {$ELSE}
  302. dosError2InOut(ERROR_NOT_IMPLEMENTED);
  303. {$ENDIF}
  304. end;
  305. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  306. {
  307. filerec and textrec have both handle and mode as the first items so
  308. they could use the same routine for opening/creating.
  309. when (flags and $10) the file will be append
  310. when (flags and $100) the file will be truncate/rewritten
  311. when (flags and $1000) there is no check for close (needed for textfiles)
  312. }
  313. var
  314. handle : THandle;
  315. openflags: LongInt;
  316. tmpStr : array[0..255] of Char;
  317. begin
  318. tmpStr:=PathConv(strpas(p))+#0;
  319. { close first if opened }
  320. if ((flags and $10000)=0) then begin
  321. case filerec(f).mode of
  322. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  323. fmclosed : ;
  324. else begin
  325. inoutres:=102; {not assigned}
  326. exit;
  327. end;
  328. end;
  329. end;
  330. { reset file handle }
  331. filerec(f).handle:=UnusedHandle;
  332. { convert filemode to filerec modes }
  333. { READ/WRITE on existing file }
  334. { RESET/APPEND }
  335. openflags:=MODE_OLDFILE;
  336. case (flags and 3) of
  337. 0 : filerec(f).mode:=fminput;
  338. 1 : filerec(f).mode:=fmoutput;
  339. 2 : filerec(f).mode:=fminout;
  340. end;
  341. { rewrite (create a new file) }
  342. if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
  343. { empty name is special }
  344. if p[0]=#0 then begin
  345. case filerec(f).mode of
  346. fminput :
  347. filerec(f).handle:=StdInputHandle;
  348. fmappend,
  349. fmoutput : begin
  350. filerec(f).handle:=StdOutputHandle;
  351. filerec(f).mode:=fmoutput; {fool fmappend}
  352. end;
  353. end;
  354. exit;
  355. end;
  356. handle:=Open(@tmpStr,openflags);
  357. if handle=0 then begin
  358. begin
  359. dosError2InOut(IoErr);
  360. FileRec(f).mode:=fmclosed;
  361. end
  362. end else begin
  363. AddToList(ASYS_fileList,handle);
  364. filerec(f).handle:=handle;
  365. end;
  366. { append mode }
  367. if ((Flags and $100)<>0) and
  368. (FileRec(F).Handle<>UnusedHandle) then begin
  369. do_seekend(filerec(f).handle);
  370. filerec(f).mode:=fmoutput; {fool fmappend}
  371. end;
  372. end;
  373. function do_isdevice(handle: THandle): boolean;
  374. begin
  375. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  376. (handle=StdErrorHandle) then
  377. do_isdevice:=True
  378. else
  379. do_isdevice:=False;
  380. end;