sysfile.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. {$DEFINE ASYS_FILESIZE_USE_EXAMINEFH}
  251. { I changed the double-Seek filesize method which we
  252. were using for 10+ years to the new ExamineFH() method.
  253. It should be available AmigaOS 2.0+, and much faster.
  254. (I actually measured several magnitudes of improvement,
  255. especially on large files.)
  256. It should be safe since there are several libc implementations
  257. using the same method on all Amiga flavors, but if anyone has
  258. a problem with it, disable this define to revert to the old
  259. method and report the issue. (KB)
  260. Actually, as some file systems (FTPMount, probably more) do
  261. not support the ExamineFH action, lets fall back to double
  262. seek silently in that case (KB) }
  263. function do_filesize(handle : THandle) : longint;
  264. var
  265. {$IFDEF ASYS_FILESIZE_USE_EXAMINEFH}
  266. fib: PFileInfoBlock;
  267. {$ENDIF}
  268. currfilepos: longint;
  269. begin
  270. checkCTRLC;
  271. do_filesize:=-1;
  272. if CheckInList(ASYS_fileList,handle)<>nil then begin
  273. {$IFDEF ASYS_FILESIZE_USE_EXAMINEFH}
  274. fib:=AllocDosObject(DOS_FIB,nil);
  275. if fib <> nil then begin
  276. if ExamineFH(BPTR(handle), fib) then
  277. do_filesize:=fib^.fib_Size;
  278. FreeDosObject(DOS_FIB,fib);
  279. end;
  280. {$ENDIF}
  281. if do_filesize = -1 then
  282. begin
  283. currfilepos:=do_filepos(handle);
  284. do_filesize:=do_seekend(handle);
  285. do_seek(handle,currfilepos);
  286. end;
  287. end;
  288. end;
  289. { truncate at a given position }
  290. procedure do_truncate(handle: THandle; pos: longint);
  291. begin
  292. checkCTRLC;
  293. if CheckInList(ASYS_fileList,handle)<>nil then begin
  294. { Seeking from OFFSET_BEGINNING }
  295. if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  296. dosError2InOut(IoErr);
  297. end;
  298. end;
  299. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  300. {
  301. filerec and textrec have both handle and mode as the first items so
  302. they could use the same routine for opening/creating.
  303. when (flags and $10) the file will be append
  304. when (flags and $100) the file will be truncate/rewritten
  305. when (flags and $1000) there is no check for close (needed for textfiles)
  306. }
  307. var
  308. handle : THandle;
  309. openflags: LongInt;
  310. tmpStr : array[0..255] of Char;
  311. begin
  312. tmpStr:=PathConv(strpas(p))+#0;
  313. { close first if opened }
  314. if ((flags and $10000)=0) then begin
  315. case filerec(f).mode of
  316. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  317. fmclosed : ;
  318. else begin
  319. inoutres:=102; {not assigned}
  320. exit;
  321. end;
  322. end;
  323. end;
  324. { reset file handle }
  325. filerec(f).handle:=UnusedHandle;
  326. { convert filemode to filerec modes }
  327. { READ/WRITE on existing file }
  328. { RESET/APPEND }
  329. openflags:=MODE_OLDFILE;
  330. case (flags and 3) of
  331. 0 : filerec(f).mode:=fminput;
  332. 1 : filerec(f).mode:=fmoutput;
  333. 2 : filerec(f).mode:=fminout;
  334. end;
  335. { rewrite (create a new file) }
  336. if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
  337. { empty name is special }
  338. if p[0]=#0 then begin
  339. case filerec(f).mode of
  340. fminput :
  341. filerec(f).handle:=StdInputHandle;
  342. fmappend,
  343. fmoutput : begin
  344. filerec(f).handle:=StdOutputHandle;
  345. filerec(f).mode:=fmoutput; {fool fmappend}
  346. end;
  347. end;
  348. exit;
  349. end;
  350. handle:=Open(@tmpStr,openflags);
  351. if handle=0 then begin
  352. begin
  353. dosError2InOut(IoErr);
  354. FileRec(f).mode:=fmclosed;
  355. end
  356. end else begin
  357. AddToList(ASYS_fileList,handle);
  358. filerec(f).handle:=handle;
  359. end;
  360. { append mode }
  361. if ((Flags and $100)<>0) and
  362. (FileRec(F).Handle<>UnusedHandle) then begin
  363. do_seekend(filerec(f).handle);
  364. filerec(f).mode:=fmoutput; {fool fmappend}
  365. end;
  366. end;
  367. function do_isdevice(handle: THandle): boolean;
  368. begin
  369. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  370. (handle=StdErrorHandle) then
  371. do_isdevice:=True
  372. else
  373. do_isdevice:=False;
  374. end;