sysutils.pp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Sysutils unit for linux
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. {$MODESWITCH OUT}
  16. { force ansistrings }
  17. {$H+}
  18. {$modeswitch typehelpers}
  19. {$modeswitch advancedrecords}
  20. {$if (defined(BSD) or defined(SUNOS)) and defined(FPC_USE_LIBC)}
  21. {$define USE_VFORK}
  22. {$endif}
  23. {$DEFINE OS_FILESETDATEBYNAME}
  24. {$DEFINE HAS_SLEEP}
  25. {$DEFINE HAS_OSERROR}
  26. {$DEFINE HAS_OSCONFIG}
  27. {$DEFINE HAS_TEMPDIR}
  28. {$DEFINE HASUNIX}
  29. {$DEFINE HASCREATEGUID}
  30. {$DEFINE HAS_OSUSERDIR}
  31. {$DEFINE HAS_LOCALTIMEZONEOFFSET}
  32. {$DEFINE HAS_GETTICKCOUNT64}
  33. // this target has an fileflush implementation, don't include dummy
  34. {$DEFINE SYSUTILS_HAS_FILEFLUSH_IMPL}
  35. { used OS file system APIs use ansistring }
  36. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  37. { OS has an ansistring/single byte environment variable API }
  38. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  39. uses
  40. {$IFDEF LINUX}linux,{$ENDIF}
  41. {$IFDEF FreeBSD}freebsd,{$ENDIF}
  42. Unix,errors,sysconst,Unixtype;
  43. {$IF defined(LINUX) or defined(FreeBSD)}
  44. {$DEFINE HAVECLOCKGETTIME}
  45. {$ENDIF}
  46. {$if defined(LINUX)}
  47. {$if sizeof(clong)<8}
  48. {$DEFINE USE_STATX}
  49. {$DEFINE USE_UTIMENSAT}
  50. {$endif sizeof(clong)<=4}
  51. {$DEFINE USE_FUTIMES}
  52. {$endif}
  53. { Include platform independent interface part }
  54. {$i sysutilh.inc}
  55. Function AddDisk(const path:string) : Byte;
  56. { the following is Kylix compatibility stuff, it should be moved to a
  57. special compatibilty unit (FK) }
  58. const
  59. RTL_SIGINT = 0;
  60. RTL_SIGFPE = 1;
  61. RTL_SIGSEGV = 2;
  62. RTL_SIGILL = 3;
  63. RTL_SIGBUS = 4;
  64. RTL_SIGQUIT = 5;
  65. RTL_SIGLAST = RTL_SIGQUIT;
  66. RTL_SIGDEFAULT = -1;
  67. type
  68. TSignalState = (ssNotHooked, ssHooked, ssOverridden);
  69. function InquireSignal(RtlSigNum: Integer): TSignalState;
  70. procedure AbandonSignalHandler(RtlSigNum: Integer);
  71. procedure HookSignal(RtlSigNum: Integer);
  72. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  73. implementation
  74. Uses
  75. {$ifdef android}
  76. dl,
  77. {$endif android}
  78. {$ifdef FPC_USE_LIBC}initc{$ELSE}Syscall{$ENDIF}, Baseunix, unixutil;
  79. type
  80. tsiginfo = record
  81. oldsiginfo: sigactionrec;
  82. hooked: boolean;
  83. end;
  84. const
  85. rtlsig2ossig: array[RTL_SIGINT..RTL_SIGLAST] of byte =
  86. (SIGINT,SIGFPE,SIGSEGV,SIGILL,SIGBUS,SIGQUIT);
  87. { to avoid linking in all this stuff in every program,
  88. as it's unlikely to be used by anything but libraries
  89. }
  90. signalinfoinited: boolean = false;
  91. var
  92. siginfo: array[RTL_SIGINT..RTL_SIGLAST] of tsiginfo;
  93. oldsigfpe: SigActionRec; external name '_FPC_OLDSIGFPE';
  94. oldsigsegv: SigActionRec; external name '_FPC_OLDSIGSEGV';
  95. oldsigbus: SigActionRec; external name '_FPC_OLDSIGBUS';
  96. oldsigill: SigActionRec; external name '_FPC_OLDSIGILL';
  97. procedure defaultsighandler; external name '_FPC_DEFAULTSIGHANDLER';
  98. procedure installdefaultsignalhandler(signum: Integer; out oldact: SigActionRec); external name '_FPC_INSTALLDEFAULTSIGHANDLER';
  99. function InternalInquireSignal(RtlSigNum: Integer; out act: SigActionRec; frominit: boolean): TSignalState;
  100. begin
  101. result:=ssNotHooked;
  102. if (RtlSigNum<>RTL_SIGDEFAULT) and
  103. (RtlSigNum<RTL_SIGLAST) then
  104. begin
  105. if (frominit or
  106. siginfo[RtlSigNum].hooked) and
  107. (fpsigaction(rtlsig2ossig[RtlSigNum],nil,@act)=0) then
  108. begin
  109. if not frominit then
  110. begin
  111. { check whether the installed signal handler is still ours }
  112. {$if not defined(aix) and (not defined(linux) or not defined(cpupowerpc64) or (defined(_call_elf) and (_call_elf = 2)))}
  113. if (pointer(act.sa_handler)=pointer(@defaultsighandler)) then
  114. {$else}
  115. { on aix and linux/ppc64 (ELFv1), procedure addresses are
  116. actually descriptors -> check whether the code addresses
  117. inside the descriptors match, rather than the descriptors
  118. themselves }
  119. if (ppointer(act.sa_handler)^=ppointer(@defaultsighandler)^) then
  120. {$endif}
  121. result:=ssHooked
  122. else
  123. result:=ssOverridden;
  124. end
  125. else if IsLibrary then
  126. begin
  127. { library -> signals have not been hooked by system init code }
  128. exit
  129. end
  130. else
  131. begin
  132. { program -> signals have been hooked by system init code }
  133. if (byte(RtlSigNum) in [RTL_SIGFPE,RTL_SIGSEGV,RTL_SIGILL,RTL_SIGBUS]) then
  134. begin
  135. {$if not defined(aix) and (not defined(linux) or not defined(cpupowerpc64) or (defined(_call_elf) and (_call_elf = 2)))}
  136. if (pointer(act.sa_handler)=pointer(@defaultsighandler)) then
  137. {$else}
  138. if (ppointer(act.sa_handler)^=ppointer(@defaultsighandler)^) then
  139. {$endif}
  140. result:=ssHooked
  141. else
  142. result:=ssOverridden;
  143. { return the original handlers as saved by the system unit
  144. (the current call to sigaction simply returned our
  145. system unit's installed handlers)
  146. }
  147. case RtlSigNum of
  148. RTL_SIGFPE:
  149. act:=oldsigfpe;
  150. RTL_SIGSEGV:
  151. act:=oldsigsegv;
  152. RTL_SIGILL:
  153. act:=oldsigill;
  154. RTL_SIGBUS:
  155. act:=oldsigbus;
  156. end;
  157. end
  158. else
  159. begin
  160. { these are not hooked in the startup code }
  161. result:=ssNotHooked;
  162. end
  163. end
  164. end
  165. end;
  166. end;
  167. procedure initsignalinfo;
  168. var
  169. i: Integer;
  170. begin
  171. for i:=RTL_SIGINT to RTL_SIGLAST do
  172. siginfo[i].hooked:=(InternalInquireSignal(i,siginfo[i].oldsiginfo,true)=ssHooked);
  173. signalinfoinited:=true;
  174. end;
  175. function InquireSignal(RtlSigNum: Integer): TSignalState;
  176. var
  177. act: SigActionRec;
  178. begin
  179. if not signalinfoinited then
  180. initsignalinfo;
  181. result:=InternalInquireSignal(RtlSigNum,act,false);
  182. end;
  183. procedure AbandonSignalHandler(RtlSigNum: Integer);
  184. begin
  185. if not signalinfoinited then
  186. initsignalinfo;
  187. if (RtlSigNum<>RTL_SIGDEFAULT) and
  188. (RtlSigNum<RTL_SIGLAST) then
  189. siginfo[RtlSigNum].hooked:=false;
  190. end;
  191. procedure HookSignal(RtlSigNum: Integer);
  192. var
  193. lowsig, highsig, i: Integer;
  194. begin
  195. if not signalinfoinited then
  196. initsignalinfo;
  197. if (RtlSigNum<>RTL_SIGDEFAULT) then
  198. begin
  199. lowsig:=RtlSigNum;
  200. highsig:=RtlSigNum;
  201. end
  202. else
  203. begin
  204. { we don't hook SIGINT and SIGQUIT by default }
  205. lowsig:=RTL_SIGFPE;
  206. highsig:=RTL_SIGBUS;
  207. end;
  208. { install the default rtl signal handler for the selected signal(s) }
  209. for i:=lowsig to highsig do
  210. begin
  211. installdefaultsignalhandler(rtlsig2ossig[i],siginfo[i].oldsiginfo);
  212. siginfo[i].hooked:=true;
  213. end;
  214. end;
  215. procedure UnhookSignal(RtlSigNum: Integer; OnlyIfHooked: Boolean = True);
  216. var
  217. act: SigActionRec;
  218. lowsig, highsig, i: Integer;
  219. begin
  220. if not signalinfoinited then
  221. initsignalinfo;
  222. if (RtlSigNum<>RTL_SIGDEFAULT) then
  223. begin
  224. lowsig:=RtlSigNum;
  225. highsig:=RtlSigNum;
  226. end
  227. else
  228. begin
  229. { we don't hook SIGINT and SIGQUIT by default }
  230. lowsig:=RTL_SIGFPE;
  231. highsig:=RTL_SIGBUS;
  232. end;
  233. for i:=lowsig to highsig do
  234. begin
  235. if not OnlyIfHooked or
  236. (InquireSignal(i)=ssHooked) then
  237. begin
  238. { restore the handler that was present when we hooked the signal,
  239. if we hooked it at one time or another. If the user doesn't
  240. want this, they have to call AbandonSignalHandler() first
  241. }
  242. if siginfo[i].hooked then
  243. act:=siginfo[i].oldsiginfo
  244. else
  245. begin
  246. fillchar(act,sizeof(act),0);
  247. pointer(act.sa_handler):=pointer(SIG_DFL);
  248. end;
  249. if (fpsigaction(rtlsig2ossig[i],@act,nil)=0) then
  250. siginfo[i].hooked:=false;
  251. end;
  252. end;
  253. end;
  254. {$Define OS_FILEISREADONLY} // Specific implementation for Unix.
  255. {$DEFINE FPC_FEXPAND_TILDE} { Tilde is expanded to home }
  256. {$DEFINE FPC_FEXPAND_GETENVPCHAR} { GetEnv result is a PChar }
  257. { Include platform independent implementation part }
  258. {$define executeprocuni}
  259. {$i sysutils.inc}
  260. { Include SysCreateGUID function }
  261. {$i suuid.inc}
  262. function GetTickCount64: QWord;
  263. var
  264. tp: TTimeVal;
  265. {$IFDEF HAVECLOCKGETTIME}
  266. ts: TTimeSpec;
  267. {$ENDIF}
  268. begin
  269. {$IFDEF HAVECLOCKGETTIME}
  270. if clock_gettime(CLOCK_MONOTONIC, @ts)=0 then
  271. begin
  272. Result := (Int64(ts.tv_sec) * 1000) + (ts.tv_nsec div 1000000);
  273. exit;
  274. end;
  275. {$ENDIF}
  276. fpgettimeofday(@tp, nil);
  277. Result := (Int64(tp.tv_sec) * 1000) + (tp.tv_usec div 1000);
  278. end;
  279. {****************************************************************************
  280. File Functions
  281. ****************************************************************************}
  282. Function DoFileLocking(Handle: Longint; Mode: Integer) : Longint;
  283. var
  284. lockop: cint;
  285. lockres: cint;
  286. closeres: cint;
  287. lockerr: cint;
  288. begin
  289. DoFileLocking:=Handle;
  290. {$ifdef beos}
  291. {$else}
  292. if (Handle>=0) then
  293. begin
  294. {$if defined(solaris) or defined(aix)}
  295. { Solaris' & AIX' flock is based on top of fcntl, which does not allow
  296. exclusive locks for files only opened for reading nor shared locks
  297. for files opened only for writing.
  298. If no locking is specified, we normally need an exclusive lock.
  299. So create an exclusive lock for fmOpenWrite and fmOpenReadWrite,
  300. but only a shared lock for fmOpenRead (since an exclusive lock
  301. is not possible in that case)
  302. }
  303. if ((mode and (fmShareCompat or fmShareExclusive or fmShareDenyWrite or fmShareDenyRead or fmShareDenyNone)) = 0) then
  304. begin
  305. if ((mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) = fmOpenRead) then
  306. mode := mode or fmShareDenyWrite
  307. else
  308. mode := mode or fmShareExclusive;
  309. end;
  310. {$endif solaris}
  311. case (mode and (fmShareCompat or fmShareExclusive or fmShareDenyWrite or fmShareDenyRead or fmShareDenyNone)) of
  312. fmShareCompat,
  313. fmShareExclusive:
  314. lockop:=LOCK_EX or LOCK_NB;
  315. fmShareDenyWrite,
  316. fmShareDenyNone:
  317. lockop:=LOCK_SH or LOCK_NB;
  318. else
  319. begin
  320. { fmShareDenyRead does not exit under *nix, only shared access
  321. (similar to fmShareDenyWrite) and exclusive access (same as
  322. fmShareExclusive)
  323. }
  324. repeat
  325. closeres:=FpClose(Handle);
  326. until (closeres<>-1) or (fpgeterrno<>ESysEINTR);
  327. DoFileLocking:=-1;
  328. exit;
  329. end;
  330. end;
  331. repeat
  332. lockres:=fpflock(Handle,lockop);
  333. until (lockres=0) or
  334. (fpgeterrno<>ESysEIntr);
  335. lockerr:=fpgeterrno;
  336. { Only return an error if locks are working and the file was already
  337. locked. Not if locks are simply unsupported (e.g., on Angstrom Linux
  338. you always get ESysNOLCK in the default configuration) }
  339. if (lockres<>0) and
  340. ((lockerr=ESysEAGAIN) or
  341. (lockerr=EsysEDEADLK)) then
  342. begin
  343. repeat
  344. closeres:=FpClose(Handle);
  345. until (closeres<>-1) or (fpgeterrno<>ESysEINTR);
  346. DoFileLocking:=-1;
  347. exit;
  348. end;
  349. end;
  350. {$endif not beos}
  351. end;
  352. Function FileOpenNoLocking (Const FileName : RawbyteString; Mode : Integer) : Longint;
  353. Function IsHandleDirectory(Handle : Longint) : boolean;
  354. Var Info : Stat;
  355. begin
  356. Result := (fpFStat(Handle, Info)<0) or fpS_ISDIR(info.st_mode);
  357. end;
  358. Var
  359. SystemFileName: RawByteString;
  360. fd,LinuxFlags : longint;
  361. begin
  362. LinuxFlags:=0;
  363. case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
  364. fmOpenRead : LinuxFlags:=LinuxFlags or O_RdOnly;
  365. fmOpenWrite : LinuxFlags:=LinuxFlags or O_WrOnly;
  366. fmOpenReadWrite : LinuxFlags:=LinuxFlags or O_RdWr;
  367. end;
  368. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  369. repeat
  370. fd:=fpOpen (pointer(SystemFileName),LinuxFlags);
  371. until (fd<>-1) or (fpgeterrno<>ESysEINTR);
  372. { Do not allow to open directories with FileOpen.
  373. This would cause weird behavior of TFileStream.Size,
  374. TMemoryStream.LoadFromFile etc. }
  375. if (fd<>-1) and IsHandleDirectory(fd) then
  376. begin
  377. fpClose(fd);
  378. fd:=feInvalidHandle;
  379. end;
  380. FileOpenNoLocking:=fd;
  381. end;
  382. Function FileOpen (Const FileName : RawbyteString; Mode : Integer) : Longint;
  383. begin
  384. FileOpen:=FileOpenNoLocking(FileName, Mode);
  385. FileOpen:=DoFileLocking(FileOpen, Mode);
  386. end;
  387. function FileFlush(Handle: THandle): Boolean;
  388. begin
  389. Result:= fpfsync(handle)=0;
  390. end;
  391. Function FileCreate (Const FileName : RawByteString) : Longint;
  392. Var
  393. SystemFileName: RawByteString;
  394. begin
  395. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  396. repeat
  397. FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc);
  398. until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
  399. end;
  400. Function FileCreate (Const FileName : RawByteString;Rights : Longint) : Longint;
  401. Var
  402. SystemFileName: RawByteString;
  403. begin
  404. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  405. repeat
  406. FileCreate:=fpOpen(pointer(SystemFileName),O_RdWr or O_Creat or O_Trunc,Rights);
  407. until (FileCreate<>-1) or (fpgeterrno<>ESysEINTR);
  408. end;
  409. Function FileCreate (Const FileName : RawByteString; ShareMode : Longint; Rights:LongInt ) : Longint;
  410. Var
  411. fd: Longint;
  412. begin
  413. { if the file already exists and we can't open it using the requested
  414. ShareMode (e.g. exclusive sharing), exit immediately so that we don't
  415. first empty the file and then check whether we can lock this new file
  416. (which we can by definition) }
  417. fd:=FileOpenNoLocking(FileName,ShareMode);
  418. { the file exists, check whether our locking request is compatible }
  419. if fd>=0 then
  420. begin
  421. Result:=DoFileLocking(fd,ShareMode);
  422. FileClose(fd);
  423. { Can't lock -> abort }
  424. if Result<0 then
  425. exit;
  426. end;
  427. { now create the file }
  428. Result:=FileCreate(FileName,Rights);
  429. Result:=DoFileLocking(Result,ShareMode);
  430. end;
  431. Function FileRead (Handle : Longint; out Buffer; Count : longint) : Longint;
  432. begin
  433. repeat
  434. FileRead:=fpRead (Handle,Buffer,Count);
  435. until (FileRead<>-1) or (fpgeterrno<>ESysEINTR);
  436. end;
  437. Function FileWrite (Handle : Longint; const Buffer; Count : Longint) : Longint;
  438. begin
  439. repeat
  440. FileWrite:=fpWrite (Handle,Buffer,Count);
  441. until (FileWrite<>-1) or (fpgeterrno<>ESysEINTR);
  442. end;
  443. Function FileSeek (Handle,FOffset,Origin : Longint) : Longint;
  444. begin
  445. result:=longint(FileSeek(Handle,int64(FOffset),Origin));
  446. end;
  447. Function FileSeek (Handle : Longint; FOffset : Int64; Origin : Longint) : Int64;
  448. begin
  449. FileSeek:=fplSeek (Handle,FOffset,Origin);
  450. end;
  451. Procedure FileClose (Handle : Longint);
  452. var
  453. res: cint;
  454. begin
  455. repeat
  456. res:=fpclose(Handle);
  457. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  458. end;
  459. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  460. var
  461. res: cint;
  462. begin
  463. if (SizeOf (TOff) < 8) (* fpFTruncate only supporting signed 32-bit size *)
  464. and (Size > high (longint)) then
  465. FileTruncate := false
  466. else
  467. begin
  468. repeat
  469. res:=fpftruncate(Handle,Size);
  470. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  471. FileTruncate:=res>=0;
  472. end;
  473. end;
  474. Function FileAge (Const FileName : RawByteString): Int64;
  475. Var
  476. Info : Stat;
  477. SystemFileName: RawByteString;
  478. {$ifdef USE_STATX}
  479. Infox : TStatx;
  480. {$endif USE_STATX}
  481. begin
  482. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  483. {$ifdef USE_STATX}
  484. { first try statx }
  485. if (statx(0,pchar(SystemFileName),0,STATX_MTIME or STATX_MODE,Infox)>=0) and not(fpS_ISDIR(Infox.stx_mode)) then
  486. begin
  487. Result:=Infox.stx_mtime.tv_sec;
  488. exit;
  489. end;
  490. {$endif USE_STATX}
  491. If (fpstat(pchar(SystemFileName),Info)<0) or fpS_ISDIR(info.st_mode) then
  492. exit(-1)
  493. else
  494. Result:=info.st_mtime;
  495. end;
  496. Function LinuxToWinAttr (const FN : RawByteString; Const Info : Stat) : Longint;
  497. Var
  498. LinkInfo : Stat;
  499. nm : RawByteString;
  500. begin
  501. Result:=faArchive;
  502. If fpS_ISDIR(Info.st_mode) then
  503. Result:=Result or faDirectory;
  504. nm:=ExtractFileName(FN);
  505. If (Length(nm)>=2) and
  506. (nm[1]='.') and
  507. (nm[2]<>'.') then
  508. Result:=Result or faHidden;
  509. If (Info.st_Mode and S_IWUSR)=0 Then
  510. Result:=Result or faReadOnly;
  511. If fpS_ISSOCK(Info.st_mode) or fpS_ISBLK(Info.st_mode) or fpS_ISCHR(Info.st_mode) or fpS_ISFIFO(Info.st_mode) Then
  512. Result:=Result or faSysFile;
  513. If fpS_ISLNK(Info.st_mode) Then
  514. begin
  515. Result:=Result or faSymLink;
  516. // Windows reports if the link points to a directory.
  517. if (fpstat(pchar(FN),LinkInfo)>=0) and fpS_ISDIR(LinkInfo.st_mode) then
  518. Result := Result or faDirectory;
  519. end;
  520. end;
  521. {$ifdef USE_STATX}
  522. Function LinuxToWinAttr (const FN : RawByteString; Const Info : TStatx) : Longint;
  523. Var
  524. LinkInfo : Stat;
  525. nm : RawByteString;
  526. begin
  527. Result:=faArchive;
  528. If fpS_ISDIR(Info.stx_mode) then
  529. Result:=Result or faDirectory;
  530. nm:=ExtractFileName(FN);
  531. If (Length(nm)>=2) and
  532. (nm[1]='.') and
  533. (nm[2]<>'.') then
  534. Result:=Result or faHidden;
  535. If (Info.stx_Mode and S_IWUSR)=0 Then
  536. Result:=Result or faReadOnly;
  537. If fpS_ISSOCK(Info.stx_mode) or fpS_ISBLK(Info.stx_mode) or fpS_ISCHR(Info.stx_mode) or fpS_ISFIFO(Info.stx_mode) Then
  538. Result:=Result or faSysFile;
  539. If fpS_ISLNK(Info.stx_mode) Then
  540. begin
  541. Result:=Result or faSymLink;
  542. // Windows reports if the link points to a directory.
  543. { as we are only interested in the st_mode field here, we do not need to use statx }
  544. if (fpstat(pchar(FN),LinkInfo)>=0) and fpS_ISDIR(LinkInfo.st_mode) then
  545. Result := Result or faDirectory;
  546. end;
  547. end;
  548. {$endif USE_STATX}
  549. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  550. var
  551. Info : Stat;
  552. SystemFileName: RawByteString;
  553. begin
  554. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  555. if (fplstat(SystemFileName,Info)>=0) and fpS_ISLNK(Info.st_mode) then begin
  556. FillByte(SymLinkRec, SizeOf(SymLinkRec), 0);
  557. SymLinkRec.TargetName:=fpreadlink(SystemFileName);
  558. if fpstat(pointer(SystemFileName), Info) < 0 then
  559. raise EDirectoryNotFoundException.Create(SysErrorMessage(GetLastOSError));
  560. SymLinkRec.Attr := LinuxToWinAttr(SystemFileName, Info);
  561. SymLinkRec.Size := Info.st_size;
  562. SymLinkRec.Mode := Info.st_mode;
  563. Result:=True;
  564. end else
  565. Result:=False;
  566. end;
  567. Function FileExists (Const FileName : RawByteString; FollowLink : Boolean) : Boolean;
  568. var
  569. Info : Stat;
  570. SystemFileName: RawByteString;
  571. isdir: Boolean;
  572. begin
  573. // Do not call fpAccess with an empty name. (Valgrind will complain)
  574. if Filename='' then
  575. Exit(False);
  576. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  577. // Don't use stat. It fails on files >2 GB.
  578. // Access obeys the same access rules, so the result should be the same.
  579. FileExists:=fpAccess(pointer(SystemFileName),F_OK)=0;
  580. { we need to ensure however that we aren't dealing with a directory }
  581. isdir:=False;
  582. if FileExists then begin
  583. if (fpstat(pointer(SystemFileName),Info)>=0) and fpS_ISDIR(Info.st_mode) then begin
  584. FileExists:=False;
  585. isdir:=True;
  586. end;
  587. end;
  588. { if we shall not follow the link we only need to check for a symlink if the
  589. target file itself should not exist }
  590. if not FileExists and not isdir and not FollowLink then
  591. FileExists:=(fplstat(pointer(SystemFileName),Info)>=0) and fpS_ISLNK(Info.st_mode);
  592. end;
  593. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  594. Var
  595. Info : Stat;
  596. SystemFileName: RawByteString;
  597. exists: Boolean;
  598. begin
  599. SystemFileName:=ToSingleByteFileSystemEncodedFileName(Directory);
  600. exists:=fpstat(pointer(SystemFileName),Info)>=0;
  601. DirectoryExists:=exists and fpS_ISDIR(Info.st_mode);
  602. { if we shall not follow the link we only need to check for a symlink if the
  603. target directory itself should not exist }
  604. if not exists and not FollowLink then
  605. DirectoryExists:=(fplstat(pointer(SystemFileName),Info)>=0) and fpS_ISLNK(Info.st_mode);
  606. end;
  607. { assumes that pattern and name have the same code page }
  608. Function FNMatch(const Pattern,Name:string):Boolean;
  609. Var
  610. LenPat,LenName : longint;
  611. function NameUtf8CodePointLen(index: longint): longint;
  612. var
  613. MaxLookAhead: longint;
  614. begin
  615. MaxLookAhead:=LenName-Index+1;
  616. { abs so that in case of an invalid sequence, we count this as one
  617. codepoint }
  618. NameUtf8CodePointLen:=abs(Utf8CodePointLen(pansichar(@Name[index]),MaxLookAhead,true));
  619. { if the sequence was incomplete, use the incomplete sequence as
  620. codepoint }
  621. if NameUtf8CodePointLen=0 then
  622. NameUtf8CodePointLen:=MaxLookAhead;
  623. end;
  624. procedure GoToLastByteOfUtf8CodePoint(var j: longint);
  625. begin
  626. inc(j,NameUtf8CodePointLen(j)-1);
  627. end;
  628. { input:
  629. i: current position in pattern (start of utf-8 code point)
  630. j: current position in name (start of utf-8 code point)
  631. update_i_j: should i and j be changed by the routine or not
  632. output:
  633. i: if update_i_j, then position of last matching part of code point in
  634. pattern, or first non-matching code point in pattern. Otherwise the
  635. same value as on input.
  636. j: if update_i_j, then position of last matching part of code point in
  637. name, or first non-matching code point in name. Otherwise the
  638. same value as on input.
  639. result: true if match, false if no match
  640. }
  641. function CompareUtf8CodePoint(var i,j: longint; update_i_j: boolean): Boolean;
  642. var
  643. bytes,
  644. new_i,
  645. new_j: longint;
  646. begin
  647. bytes:=NameUtf8CodePointLen(j);
  648. new_i:=i;
  649. new_j:=j;
  650. { ensure that a part of an UTF-8 codepoint isn't interpreted
  651. as '*' or '?' }
  652. repeat
  653. dec(bytes);
  654. Result:=
  655. (new_j<=LenName) and
  656. (new_i<=LenPat) and
  657. (Pattern[new_i]=Name[new_j]);
  658. inc(new_i);
  659. inc(new_j);
  660. until not(Result) or
  661. (bytes=0);
  662. if update_i_j then
  663. begin
  664. i:=new_i;
  665. j:=new_j;
  666. end;
  667. end;
  668. Function DoFNMatch(i,j:longint):Boolean;
  669. Var
  670. UTF8, Found : boolean;
  671. Begin
  672. Found:=true;
  673. { ensure that we don't skip partial characters in UTF-8-encoded strings }
  674. UTF8:=StringCodePage(Name)=CP_UTF8;
  675. While Found and (i<=LenPat) Do
  676. Begin
  677. Case Pattern[i] of
  678. '?' :
  679. begin
  680. Found:=(j<=LenName);
  681. if UTF8 then
  682. GoToLastByteOfUtf8CodePoint(j);
  683. end;
  684. '*' : Begin
  685. {find the next character in pattern, different of ? and *}
  686. while Found do
  687. begin
  688. inc(i);
  689. if i>LenPat then
  690. Break;
  691. case Pattern[i] of
  692. '*' : ;
  693. '?' : begin
  694. if j>LenName then
  695. begin
  696. DoFNMatch:=false;
  697. Exit;
  698. end;
  699. if UTF8 then
  700. GoToLastByteOfUtf8CodePoint(j);
  701. inc(j);
  702. end;
  703. else
  704. Found:=false;
  705. end;
  706. end;
  707. Assert((i>LenPat) or ( (Pattern[i]<>'*') and (Pattern[i]<>'?') ));
  708. { Now, find in name the character which i points to, if the * or
  709. ? wasn't the last character in the pattern, else, use up all
  710. the chars in name }
  711. Found:=false;
  712. if (i<=LenPat) then
  713. begin
  714. repeat
  715. {find a letter (not only first !) which maches pattern[i]}
  716. if UTF8 then
  717. begin
  718. while (j<=LenName) and
  719. ((name[j]<>pattern[i]) or
  720. not CompareUtf8CodePoint(i,j,false)) do
  721. begin
  722. GoToLastByteOfUtf8CodePoint(j);
  723. inc(j);
  724. end;
  725. end
  726. else
  727. begin
  728. while (j<=LenName) and (name[j]<>pattern[i]) do
  729. inc (j);
  730. end;
  731. if (j<LenName) then
  732. begin
  733. { while positions i/j have already been checked, in
  734. case of UTF-8 we have to ensure that we don't split
  735. a code point. Otherwise we can skip over comparing
  736. the same characters twice }
  737. if DoFnMatch(i+ord(not UTF8),j+ord(not UTF8)) then
  738. begin
  739. i:=LenPat;
  740. j:=LenName;{we can stop}
  741. Found:=true;
  742. Break;
  743. end
  744. { We didn't find one, need to look further }
  745. else
  746. begin
  747. if UTF8 then
  748. GoToLastByteOfUtf8CodePoint(j);
  749. inc(j);
  750. end;
  751. end
  752. else if j=LenName then
  753. begin
  754. Found:=true;
  755. Break;
  756. end;
  757. { This 'until' condition must be j>LenName, not j>=LenName.
  758. That's because when we 'need to look further' and
  759. j = LenName then loop must not terminate. }
  760. until (j>LenName);
  761. end
  762. else
  763. begin
  764. j:=LenName;{we can stop}
  765. Found:=true;
  766. end;
  767. end;
  768. #128..#255:
  769. begin
  770. Found:=(j<=LenName) and (pattern[i]=name[j]);
  771. if Found and UTF8 then
  772. begin
  773. { ensure that a part of an UTF-8 codepoint isn't matched with
  774. '*' or '?' }
  775. Found:=CompareUtf8CodePoint(i,j,true);
  776. { at this point, either Found is false (and we'll stop), or
  777. both pattern[i] and name[j] are the end of the current code
  778. point and equal }
  779. end
  780. end
  781. else {not a wildcard character in pattern}
  782. Found:=(j<=LenName) and (pattern[i]=name[j]);
  783. end;
  784. inc(i);
  785. inc(j);
  786. end;
  787. DoFnMatch:=Found and (j>LenName);
  788. end;
  789. Begin {start FNMatch}
  790. LenPat:=Length(Pattern);
  791. LenName:=Length(Name);
  792. FNMatch:=DoFNMatch(1,1);
  793. End;
  794. Type
  795. TUnixFindData = Record
  796. NamePos : LongInt; {to track which search this is}
  797. DirPtr : Pointer; {directory pointer for reading directory}
  798. SearchSpec : RawbyteString;
  799. SearchType : Byte; {0=normal, 1=open will close, 2=only 1 file}
  800. SearchAttr : Longint; {attribute we are searching for}
  801. End;
  802. PUnixFindData = ^TUnixFindData;
  803. Procedure InternalFindClose(var Handle: Pointer);
  804. var
  805. D: PUnixFindData absolute Handle;
  806. begin
  807. If D=Nil then
  808. Exit;
  809. if D^.SearchType=0 then
  810. begin
  811. if D^.dirptr<>nil then
  812. fpclosedir(pdir(D^.dirptr)^);
  813. end;
  814. Dispose(D);
  815. D:=nil;
  816. end;
  817. Function FindGetFileInfo(const s: RawByteString; var f: TAbstractSearchRec; var Name: RawByteString):boolean;
  818. Var
  819. {$ifdef USE_STATX}
  820. stx : linux.tstatx;
  821. {$endif USE_STATX}
  822. st : baseunix.stat;
  823. WinAttr : longint;
  824. begin
  825. {$ifdef USE_STATX}
  826. if Assigned(f.FindHandle) and ( (PUnixFindData(F.FindHandle)^.searchattr and faSymlink) > 0) then
  827. FindGetFileInfo:=statx(AT_FDCWD,pointer(s),AT_SYMLINK_NOFOLLOW,STATX_ALL,stx)=0
  828. else
  829. FindGetFileInfo:=statx(AT_FDCWD,pointer(s),0,STATX_ALL,stx)=0;
  830. if FindGetFileInfo then
  831. begin
  832. WinAttr:=LinuxToWinAttr(s,stx);
  833. FindGetFileInfo:=(WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=0;
  834. if FindGetFileInfo then
  835. begin
  836. Name:=ExtractFileName(s);
  837. f.Attr:=WinAttr;
  838. f.Size:=stx.stx_Size;
  839. f.Mode:=stx.stx_mode;
  840. f.Time:=stx.stx_mtime.tv_sec;
  841. FindGetFileInfo:=true;
  842. end;
  843. end
  844. { no statx? try stat }
  845. else if fpgeterrno=ESysENOSYS then
  846. {$endif USE_STATX}
  847. begin
  848. if Assigned(f.FindHandle) and ( (PUnixFindData(F.FindHandle)^.searchattr and faSymlink) > 0) then
  849. FindGetFileInfo:=(fplstat(pointer(s),st)=0)
  850. else
  851. FindGetFileInfo:=(fpstat(pointer(s),st)=0);
  852. if not FindGetFileInfo then
  853. exit;
  854. WinAttr:=LinuxToWinAttr(s,st);
  855. FindGetFileInfo:=(WinAttr and Not(PUnixFindData(f.FindHandle)^.searchattr))=0;
  856. if FindGetFileInfo then
  857. begin
  858. Name:=ExtractFileName(s);
  859. f.Attr:=WinAttr;
  860. f.Size:=st.st_Size;
  861. f.Mode:=st.st_mode;
  862. f.Time:=st.st_mtime;
  863. FindGetFileInfo:=true;
  864. end;
  865. end;
  866. end;
  867. // Returns the FOUND filename. Error code <> 0 if no file found
  868. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  869. Var
  870. DirName : RawByteString;
  871. FName,
  872. SName : RawBytestring;
  873. Found,
  874. Finished : boolean;
  875. p : pdirent;
  876. UnixFindData : PUnixFindData;
  877. Begin
  878. Result:=-1;
  879. UnixFindData:=PUnixFindData(Rslt.FindHandle);
  880. { SearchSpec='' means that there were no wild cards, so only one file to
  881. find.
  882. }
  883. If (UnixFindData=Nil) or (UnixFindData^.SearchSpec='') then
  884. exit;
  885. if (UnixFindData^.SearchType=0) and
  886. (UnixFindData^.Dirptr=nil) then
  887. begin
  888. If UnixFindData^.NamePos = 0 Then
  889. DirName:='./'
  890. Else
  891. DirName:=Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos);
  892. UnixFindData^.DirPtr := fpopendir(Pchar(DirName));
  893. end;
  894. SName:=Copy(UnixFindData^.SearchSpec,UnixFindData^.NamePos+1,Length(UnixFindData^.SearchSpec));
  895. Found:=False;
  896. Finished:=(UnixFindData^.dirptr=nil);
  897. While Not Finished Do
  898. Begin
  899. p:=fpreaddir(pdir(UnixFindData^.dirptr)^);
  900. if p=nil then
  901. FName:=''
  902. else
  903. FName:=p^.d_name;
  904. If FName='' Then
  905. Finished:=True
  906. Else
  907. Begin
  908. SetCodePage(FName,DefaultFileSystemCodePage,false);
  909. If FNMatch(SName,FName) Then
  910. Begin
  911. Found:=FindGetFileInfo(Copy(UnixFindData^.SearchSpec,1,UnixFindData^.NamePos)+FName,Rslt,Name);
  912. if Found then
  913. begin
  914. Result:=0;
  915. exit;
  916. end;
  917. End;
  918. End;
  919. End;
  920. End;
  921. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  922. {
  923. opens dir and calls FindNext if needed.
  924. }
  925. var
  926. UnixFindData : PUnixFindData;
  927. Begin
  928. Result:=-1;
  929. { this is safe even though Rslt actually contains a refcounted field, because
  930. it is declared as "out" and hence has already been initialised }
  931. fillchar(Rslt,sizeof(Rslt),0);
  932. if Path='' then
  933. exit;
  934. { Allocate UnixFindData (we always need it, for the search attributes) }
  935. New(UnixFindData);
  936. FillChar(UnixFindData^,sizeof(UnixFindData^),0);
  937. Rslt.FindHandle:=UnixFindData;
  938. {We always also search for readonly and archive, regardless of Attr:}
  939. UnixFindData^.SearchAttr := Attr or faarchive or fareadonly;
  940. {Wildcards?}
  941. if (Pos('?',Path)=0) and (Pos('*',Path)=0) then
  942. begin
  943. if FindGetFileInfo(ToSingleByteFileSystemEncodedFileName(Path),Rslt,Name) then
  944. Result:=0;
  945. end
  946. else
  947. begin
  948. {Create Info}
  949. UnixFindData^.SearchSpec := ToSingleByteFileSystemEncodedFileName(Path);
  950. UnixFindData^.NamePos := Length(UnixFindData^.SearchSpec);
  951. while (UnixFindData^.NamePos>0) and (UnixFindData^.SearchSpec[UnixFindData^.NamePos]<>'/') do
  952. dec(UnixFindData^.NamePos);
  953. Result:=InternalFindNext(Rslt,Name);
  954. end;
  955. If (Result<>0) then
  956. InternalFindClose(Rslt.FindHandle);
  957. End;
  958. Function FileGetDate (Handle : Longint) : Int64;
  959. Var
  960. Info : Stat;
  961. {$ifdef USE_STATX}
  962. Infox : TStatx;
  963. {$endif USE_STATX}
  964. Char0 : char;
  965. begin
  966. Result:=-1;
  967. {$ifdef USE_STATX}
  968. Char0:=#0;
  969. if statx(Handle,@Char0,AT_EMPTY_PATH,STATX_MTIME,Infox)=0 then
  970. Result:=Infox.stx_Mtime.tv_sec
  971. else if fpgeterrno=ESysENOSYS then
  972. {$endif USE_STATX}
  973. begin
  974. If fpFStat(Handle,Info)=0 then
  975. Result:=Info.st_Mtime;
  976. end;
  977. end;
  978. Function FileSetDate (Handle : Longint;Age : Int64) : Longint;
  979. {$ifdef USE_FUTIMES}
  980. var
  981. times : tkernel_timespecs;
  982. {$endif USE_FUTIMES}
  983. begin
  984. Result:=0;
  985. {$ifdef USE_FUTIMES}
  986. times[0].tv_sec:=Age;
  987. times[0].tv_nsec:=0;
  988. times[1].tv_sec:=Age;
  989. times[1].tv_nsec:=0;
  990. if futimens(Handle,times) = -1 then
  991. Result:=fpgeterrno;
  992. {$else USE_FUTIMES}
  993. FileSetDate:=-1;
  994. {$endif USE_FUTIMES}
  995. end;
  996. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  997. Var
  998. SystemFileName: RawByteString;
  999. Info : Stat;
  1000. res : Integer;
  1001. begin
  1002. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1003. res:=FpLStat(pointer(SystemFileName),Info);
  1004. if res<0 then
  1005. res:=FpStat(pointer(SystemFileName),Info);
  1006. if res<0 then
  1007. Result:=-1
  1008. Else
  1009. Result:=LinuxToWinAttr(SystemFileName,Info);
  1010. end;
  1011. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  1012. begin
  1013. Result:=-1;
  1014. end;
  1015. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  1016. var
  1017. SystemFileName: RawByteString;
  1018. begin
  1019. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1020. Result:=fpUnLink (pchar(SystemFileName))>=0;
  1021. end;
  1022. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  1023. var
  1024. SystemOldName, SystemNewName: RawByteString;
  1025. begin
  1026. SystemOldName:=ToSingleByteFileSystemEncodedFileName(OldName);
  1027. SystemNewName:=ToSingleByteFileSystemEncodedFileName(NewName);
  1028. RenameFile:=BaseUnix.FpRename(pointer(SystemOldName),pointer(SystemNewName))>=0;
  1029. end;
  1030. Function FileIsReadOnly(const FileName: RawByteString): Boolean;
  1031. var
  1032. SystemFileName: RawByteString;
  1033. begin
  1034. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1035. Result:=fpAccess(PChar(SystemFileName),W_OK)<>0;
  1036. end;
  1037. Function FileSetDate (Const FileName : RawByteString; Age : Int64) : Longint;
  1038. var
  1039. SystemFileName: RawByteString;
  1040. {$ifdef USE_UTIMENSAT}
  1041. times : tkernel_timespecs;
  1042. {$endif USE_UTIMENSAT}
  1043. t: TUTimBuf;
  1044. begin
  1045. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  1046. Result:=0;
  1047. {$ifdef USE_UTIMENSAT}
  1048. times[0].tv_sec:=Age;
  1049. times[0].tv_nsec:=0;
  1050. times[1].tv_sec:=Age;
  1051. times[1].tv_nsec:=0;
  1052. if utimensat(AT_FDCWD,PChar(SystemFileName),times,0) = -1 then
  1053. Result:=fpgeterrno;
  1054. if fpgeterrno=ESysENOSYS then
  1055. {$endif USE_UTIMENSAT}
  1056. begin
  1057. Result:=0;
  1058. t.actime:= Age;
  1059. t.modtime:=Age;
  1060. if fputime(PChar(SystemFileName), @t) = -1 then
  1061. Result:=fpgeterrno;
  1062. end
  1063. end;
  1064. {****************************************************************************
  1065. Disk Functions
  1066. ****************************************************************************}
  1067. {
  1068. The Diskfree and Disksize functions need a file on the specified drive, since this
  1069. is required for the fpstatfs system call.
  1070. These filenames are set in drivestr[0..26], and have been preset to :
  1071. 0 - '.' (default drive - hence current dir is ok.)
  1072. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  1073. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  1074. 3 - '/' (C: equivalent of dos is the root partition)
  1075. 4..26 (can be set by you're own applications)
  1076. ! Use AddDisk() to Add new drives !
  1077. They both return -1 when a failure occurs.
  1078. }
  1079. Const
  1080. FixDriveStr : array[0..3] of pchar=(
  1081. '.',
  1082. '/fd0/.',
  1083. '/fd1/.',
  1084. '/.'
  1085. );
  1086. var
  1087. Drives : byte = 4;
  1088. DriveStr : array[4..26] of pchar;
  1089. Function AddDisk(const path:string) : Byte;
  1090. begin
  1091. if not (DriveStr[Drives]=nil) then
  1092. FreeMem(DriveStr[Drives]);
  1093. GetMem(DriveStr[Drives],length(Path)+1);
  1094. StrPCopy(DriveStr[Drives],path);
  1095. Result:=Drives;
  1096. inc(Drives);
  1097. if Drives>26 then
  1098. Drives:=4;
  1099. end;
  1100. Function DiskFree(Drive: Byte): int64;
  1101. var
  1102. fs : tstatfs;
  1103. Begin
  1104. if ((Drive in [Low(FixDriveStr)..High(FixDriveStr)]) and (not (fixdrivestr[Drive]=nil)) and (fpstatfs(StrPas(fixdrivestr[drive]),@fs)<>-1)) or
  1105. ((Drive in [Low(DriveStr)..High(DriveStr)]) and (not (drivestr[Drive]=nil)) and (fpstatfs(StrPas(drivestr[drive]),@fs)<>-1)) then
  1106. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  1107. else
  1108. Diskfree:=-1;
  1109. End;
  1110. Function DiskSize(Drive: Byte): int64;
  1111. var
  1112. fs : tstatfs;
  1113. Begin
  1114. if ((Drive in [Low(FixDriveStr)..High(FixDriveStr)]) and (not (fixdrivestr[Drive]=nil)) and (fpstatfs(StrPas(fixdrivestr[drive]),@fs)<>-1)) or
  1115. ((Drive in [Low(DriveStr)..High(DriveStr)]) and (not (drivestr[Drive]=nil)) and (fpstatfs(StrPas(drivestr[drive]),@fs)<>-1)) then
  1116. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  1117. else
  1118. DiskSize:=-1;
  1119. End;
  1120. Procedure FreeDriveStr;
  1121. var
  1122. i: longint;
  1123. begin
  1124. for i:=low(drivestr) to high(drivestr) do
  1125. if assigned(drivestr[i]) then
  1126. begin
  1127. freemem(drivestr[i]);
  1128. drivestr[i]:=nil;
  1129. end;
  1130. end;
  1131. {****************************************************************************
  1132. Misc Functions
  1133. ****************************************************************************}
  1134. {****************************************************************************
  1135. Locale Functions
  1136. ****************************************************************************}
  1137. Function GetEpochTime: cint;
  1138. {
  1139. Get the number of seconds since 00:00, January 1 1970, GMT
  1140. the time NOT corrected any way
  1141. }
  1142. begin
  1143. GetEpochTime:=fptime;
  1144. end;
  1145. Procedure DoGetUniversalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1146. var
  1147. tz:timeval;
  1148. begin
  1149. fpgettimeofday(@tz,nil);
  1150. EpochToUniversal(tz.tv_sec,year,month,day,hour,min,sec);
  1151. msec:=tz.tv_usec div 1000;
  1152. usec:=tz.tv_usec mod 1000;
  1153. end;
  1154. // Now, adjusted to local time.
  1155. Procedure DoGetLocalDateTime(var year, month, day, hour, min, sec, msec, usec : word);
  1156. var
  1157. tz:timeval;
  1158. begin
  1159. fpgettimeofday(@tz,nil);
  1160. EpochToLocal(tz.tv_sec,year,month,day,hour,min,sec);
  1161. msec:=tz.tv_usec div 1000;
  1162. usec:=tz.tv_usec mod 1000;
  1163. end;
  1164. procedure GetTime(var hour,min,sec,msec,usec:word);
  1165. Var
  1166. year,day,month:Word;
  1167. begin
  1168. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1169. end;
  1170. procedure GetTime(var hour,min,sec,sec100:word);
  1171. {
  1172. Gets the current time, adjusted to local time
  1173. }
  1174. var
  1175. year,day,month,usec : word;
  1176. begin
  1177. DoGetLocalDateTime(year,month,day,hour,min,sec,sec100,usec);
  1178. sec100:=sec100 div 10;
  1179. end;
  1180. Procedure GetTime(Var Hour,Min,Sec:Word);
  1181. {
  1182. Gets the current time, adjusted to local time
  1183. }
  1184. var
  1185. year,day,month,msec,usec : Word;
  1186. Begin
  1187. DoGetLocalDateTime(year,month,day,hour,min,sec,msec,usec);
  1188. End;
  1189. Procedure GetDate(Var Year,Month,Day:Word);
  1190. {
  1191. Gets the current date, adjusted to local time
  1192. }
  1193. var
  1194. hour,minute,second,msec,usec : word;
  1195. Begin
  1196. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1197. End;
  1198. Procedure GetDateTime(Var Year,Month,Day,hour,minute,second:Word);
  1199. {
  1200. Gets the current date, adjusted to local time
  1201. }
  1202. Var
  1203. usec,msec : word;
  1204. Begin
  1205. DoGetLocalDateTime(year,month,day,hour,minute,second,msec,usec);
  1206. End;
  1207. {$ifndef FPUNONE}
  1208. Procedure GetLocalTime(var SystemTime: TSystemTime);
  1209. var
  1210. usecs : Word;
  1211. begin
  1212. DoGetLocalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1213. end ;
  1214. {$endif}
  1215. Procedure InitAnsi;
  1216. Var
  1217. i : longint;
  1218. begin
  1219. { Fill table entries 0 to 127 }
  1220. for i := 0 to 96 do
  1221. UpperCaseTable[i] := chr(i);
  1222. for i := 97 to 122 do
  1223. UpperCaseTable[i] := chr(i - 32);
  1224. for i := 123 to 191 do
  1225. UpperCaseTable[i] := chr(i);
  1226. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  1227. for i := 0 to 64 do
  1228. LowerCaseTable[i] := chr(i);
  1229. for i := 65 to 90 do
  1230. LowerCaseTable[i] := chr(i + 32);
  1231. for i := 91 to 191 do
  1232. LowerCaseTable[i] := chr(i);
  1233. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  1234. end;
  1235. Procedure InitInternational;
  1236. begin
  1237. InitInternationalGeneric;
  1238. InitAnsi;
  1239. end;
  1240. function SysErrorMessage(ErrorCode: Integer): String;
  1241. begin
  1242. Result:=StrError(ErrorCode);
  1243. end;
  1244. {****************************************************************************
  1245. OS utility functions
  1246. ****************************************************************************}
  1247. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  1248. begin
  1249. { no need to adjust the code page of EnvVar to DefaultSystemCodePage, as only
  1250. ASCII identifiers are supported }
  1251. Result:=BaseUnix.FPGetenv(PChar(pointer(EnvVar)));
  1252. end;
  1253. Function GetEnvironmentVariableCount : Integer;
  1254. begin
  1255. Result:=FPCCountEnvVar(EnvP);
  1256. end;
  1257. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  1258. begin
  1259. Result:=FPCGetEnvStrFromP(Envp,Index);
  1260. end;
  1261. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  1262. var
  1263. pid : longint;
  1264. e : EOSError;
  1265. CommandLine: RawByteString;
  1266. LPath : RawByteString;
  1267. cmdline2 : ppchar;
  1268. Begin
  1269. { always surround the name of the application by quotes
  1270. so that long filenames will always be accepted. But don't
  1271. do it if there are already double quotes!
  1272. }
  1273. // Only place we still parse
  1274. cmdline2:=nil;
  1275. LPath:=Path;
  1276. UniqueString(LPath);
  1277. SetCodePage(LPath,DefaultFileSystemCodePage,true);
  1278. if Comline<>'' Then
  1279. begin
  1280. CommandLine:=ComLine;
  1281. { Make an unique copy because stringtoppchar modifies the
  1282. string, and force conversion to intended fscp }
  1283. UniqueString(CommandLine);
  1284. SetCodePage(CommandLine,DefaultFileSystemCodePage,true);
  1285. cmdline2:=StringtoPPChar(CommandLine,1);
  1286. cmdline2^:=pchar(pointer(LPath));
  1287. end
  1288. else
  1289. begin
  1290. getmem(cmdline2,2*sizeof(pchar));
  1291. cmdline2^:=pchar(LPath);
  1292. cmdline2[1]:=nil;
  1293. end;
  1294. {$ifdef USE_VFORK}
  1295. pid:=fpvFork;
  1296. {$else USE_VFORK}
  1297. pid:=fpFork;
  1298. {$endif USE_VFORK}
  1299. if pid=0 then
  1300. begin
  1301. {The child does the actual exec, and then exits}
  1302. fpexecve(pchar(pointer(LPath)),Cmdline2,envp);
  1303. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1304. fpExit(127);
  1305. end
  1306. else
  1307. if pid=-1 then {Fork failed}
  1308. begin
  1309. e:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,-1]);
  1310. e.ErrorCode:=-1;
  1311. raise e;
  1312. end;
  1313. { We're in the parent, let's wait. }
  1314. result:=WaitProcess(pid); // WaitPid and result-convert
  1315. if Comline<>'' Then
  1316. freemem(cmdline2);
  1317. if (result<0) or (result=127) then
  1318. begin
  1319. E:=EOSError.CreateFmt(SExecuteProcessFailed,[LPath,result]);
  1320. E.ErrorCode:=result;
  1321. Raise E;
  1322. end;
  1323. End;
  1324. function ExecuteProcess(Const Path: RawByteString; Const ComLine: Array Of RawByteString;Flags:TExecuteFlags=[]):integer;
  1325. var
  1326. pid : longint;
  1327. e : EOSError;
  1328. Begin
  1329. pid:=fpFork;
  1330. if pid=0 then
  1331. begin
  1332. {The child does the actual exec, and then exits}
  1333. fpexecl(Path,Comline);
  1334. { If the execve fails, we return an exitvalue of 127, to let it be known}
  1335. fpExit(127);
  1336. end
  1337. else
  1338. if pid=-1 then {Fork failed}
  1339. begin
  1340. e:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,-1]);
  1341. e.ErrorCode:=-1;
  1342. raise e;
  1343. end;
  1344. { We're in the parent, let's wait. }
  1345. result:=WaitProcess(pid); // WaitPid and result-convert
  1346. if (result<0) or (result=127) then
  1347. begin
  1348. E:=EOSError.CreateFmt(SExecuteProcessFailed,[Path,result]);
  1349. E.ErrorCode:=result;
  1350. raise E;
  1351. end;
  1352. End;
  1353. procedure Sleep(milliseconds: Cardinal);
  1354. Var
  1355. timeout,timeoutresult : TTimespec;
  1356. res: cint;
  1357. begin
  1358. timeout.tv_sec:=milliseconds div 1000;
  1359. timeout.tv_nsec:=1000*1000*(milliseconds mod 1000);
  1360. repeat
  1361. res:=fpnanosleep(@timeout,@timeoutresult);
  1362. timeout:=timeoutresult;
  1363. until (res<>-1) or (fpgeterrno<>ESysEINTR);
  1364. end;
  1365. Function GetLastOSError : Integer;
  1366. begin
  1367. Result:=fpgetErrNo;
  1368. end;
  1369. { ---------------------------------------------------------------------
  1370. Application config files
  1371. ---------------------------------------------------------------------}
  1372. {$ifdef android}
  1373. var
  1374. _HomeDir: string;
  1375. _HasPackageDataDir: boolean;
  1376. Function GetHomeDir : String;
  1377. var
  1378. h: longint;
  1379. i: longint;
  1380. begin
  1381. Result:=_HomeDir;
  1382. if Result <> '' then
  1383. exit;
  1384. if IsLibrary then
  1385. begin
  1386. // For shared library get the package name of a host Java application
  1387. h:=FileOpen('/proc/self/cmdline', fmOpenRead or fmShareDenyNone);
  1388. if h >= 0 then
  1389. begin
  1390. SetLength(Result, MAX_PATH);
  1391. SetLength(Result, FileRead(h, Result[1], Length(Result)));
  1392. SetLength(Result, strlen(PChar(Result)));
  1393. FileClose(h);
  1394. Result:='/data/data/' + Result;
  1395. _HasPackageDataDir:=DirectoryExists(Result);
  1396. if _HasPackageDataDir then
  1397. begin
  1398. Result:=Result + '/files/';
  1399. ForceDirectories(Result);
  1400. end
  1401. else
  1402. Result:=''; // No package
  1403. end;
  1404. end;
  1405. if Result = '' then
  1406. Result:='/data/local/tmp/';
  1407. _HomeDir:=Result;
  1408. end;
  1409. Function XdgConfigHome : String;
  1410. begin
  1411. Result:=GetHomeDir;
  1412. end;
  1413. {$else}
  1414. Function GetHomeDir : String;
  1415. begin
  1416. Result:=GetEnvironmentVariable('HOME');
  1417. If (Result<>'') then
  1418. Result:=IncludeTrailingPathDelimiter(Result);
  1419. end;
  1420. { Follows base-dir spec,
  1421. see [http://freedesktop.org/Standards/basedir-spec].
  1422. Always ends with PathDelim. }
  1423. Function XdgConfigHome : String;
  1424. begin
  1425. Result:=GetEnvironmentVariable('XDG_CONFIG_HOME');
  1426. if (Result='') then
  1427. Result:=GetHomeDir + '.config/'
  1428. else
  1429. Result:=IncludeTrailingPathDelimiter(Result);
  1430. end;
  1431. {$endif android}
  1432. Function GetAppConfigDir(Global : Boolean) : String;
  1433. begin
  1434. If Global then
  1435. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1436. else
  1437. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1438. {$ifdef android}
  1439. if _HasPackageDataDir then
  1440. exit;
  1441. {$endif android}
  1442. if VendorName<>'' then
  1443. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1444. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1445. end;
  1446. Function GetAppConfigFile(Global : Boolean; SubDir : Boolean) : String;
  1447. begin
  1448. If Global then
  1449. Result:=IncludeTrailingPathDelimiter(SysConfigDir)
  1450. else
  1451. Result:=IncludeTrailingPathDelimiter(XdgConfigHome);
  1452. {$ifdef android}
  1453. if _HasPackageDataDir then
  1454. begin
  1455. Result:=Result+'config'+ConfigExtension;
  1456. exit;
  1457. end;
  1458. {$endif android}
  1459. if SubDir then
  1460. begin
  1461. if VendorName<>'' then
  1462. Result:=IncludeTrailingPathDelimiter(Result+VendorName);
  1463. Result:=IncludeTrailingPathDelimiter(Result+ApplicationName);
  1464. end;
  1465. Result:=Result+ApplicationName+ConfigExtension;
  1466. end;
  1467. {****************************************************************************
  1468. GetTempDir
  1469. ****************************************************************************}
  1470. Function GetTempDir(Global : Boolean) : String;
  1471. begin
  1472. If Assigned(OnGetTempDir) then
  1473. Result:=OnGetTempDir(Global)
  1474. else
  1475. begin
  1476. {$ifdef android}
  1477. Result:=GetHomeDir + 'tmp';
  1478. ForceDirectories(Result);
  1479. {$else}
  1480. Result:=GetEnvironmentVariable('TEMP');
  1481. If (Result='') Then
  1482. Result:=GetEnvironmentVariable('TMP');
  1483. If (Result='') Then
  1484. Result:=GetEnvironmentVariable('TMPDIR');
  1485. if (Result='') then
  1486. Result:='/tmp/'; // fallback.
  1487. {$endif android}
  1488. end;
  1489. if (Result<>'') then
  1490. Result:=IncludeTrailingPathDelimiter(Result);
  1491. end;
  1492. {****************************************************************************
  1493. GetUserDir
  1494. ****************************************************************************}
  1495. Var
  1496. TheUserDir : String;
  1497. Function GetUserDir : String;
  1498. begin
  1499. If (TheUserDir='') then
  1500. begin
  1501. {$ifdef android}
  1502. TheUserDir:=GetHomeDir;
  1503. {$else}
  1504. TheUserDir:=GetEnvironmentVariable('HOME');
  1505. {$endif android}
  1506. if (TheUserDir<>'') then
  1507. TheUserDir:=IncludeTrailingPathDelimiter(TheUserDir)
  1508. else
  1509. TheUserDir:=GetTempDir(False);
  1510. end;
  1511. Result:=TheUserDir;
  1512. end;
  1513. Procedure SysBeep;
  1514. begin
  1515. Write(#7);
  1516. Flush(Output);
  1517. end;
  1518. function GetUniversalTime(var SystemTime: TSystemTime): Boolean;
  1519. var
  1520. usecs : Word;
  1521. begin
  1522. DoGetUniversalDateTime(SystemTime.Year, SystemTime.Month, SystemTime.Day,SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond, usecs);
  1523. Result:=True;
  1524. end;
  1525. function GetLocalTimeOffset: Integer;
  1526. begin
  1527. Result := -Tzseconds div 60;
  1528. end;
  1529. function GetLocalTimeOffset(const DateTime: TDateTime; const InputIsUTC: Boolean; out Offset: Integer): Boolean;
  1530. var
  1531. Year, Month, Day, Hour, Minute, Second, MilliSecond: word;
  1532. UnixTime: Int64;
  1533. lTZInfo: TTZInfo;
  1534. begin
  1535. DecodeDate(DateTime, Year, Month, Day);
  1536. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  1537. UnixTime:=UniversalToEpoch(Year, Month, Day, Hour, Minute, Second);
  1538. {$if declared(GetLocalTimezone)}
  1539. GetLocalTimeOffset:=GetLocalTimezone(UnixTime,InputIsUTC,lTZInfo);
  1540. if GetLocalTimeOffset then
  1541. Offset:=-lTZInfo.seconds div 60;
  1542. {$else}
  1543. GetLocalTimeOffset:=False;
  1544. {$endif}
  1545. end;
  1546. {$ifdef android}
  1547. procedure InitAndroid;
  1548. var
  1549. dlinfo: dl_info;
  1550. s: string;
  1551. begin
  1552. FillChar(dlinfo, sizeof(dlinfo), 0);
  1553. dladdr(@InitAndroid, @dlinfo);
  1554. s:=dlinfo.dli_fname;
  1555. if s <> '' then
  1556. SetDefaultSysLogTag(ExtractFileName(s));
  1557. end;
  1558. {$endif android}
  1559. {****************************************************************************
  1560. Initialization code
  1561. ****************************************************************************}
  1562. Initialization
  1563. InitExceptions; { Initialize exceptions. OS independent }
  1564. InitInternational; { Initialize internationalization settings }
  1565. SysConfigDir:='/etc'; { Initialize system config dir }
  1566. OnBeep:=@SysBeep;
  1567. {$ifdef android}
  1568. InitAndroid;
  1569. {$endif android}
  1570. Finalization
  1571. FreeDriveStr;
  1572. FreeTerminateProcs;
  1573. DoneExceptions;
  1574. end.