sysutils.pp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2021 by the Free Pascal development team.
  4. Sysutils unit for The WebAssembly System Interface (WASI).
  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. {$inline on}
  12. {$IFNDEF FPC_DOTTEDUNITS}
  13. unit sysutils;
  14. {$ENDIF FPC_DOTTEDUNITS}
  15. interface
  16. {$MODE objfpc}
  17. {$MODESWITCH OUT}
  18. {$IFDEF UNICODERTL}
  19. {$MODESWITCH UNICODESTRINGS}
  20. {$ELSE}
  21. {$H+}
  22. {$ENDIF}
  23. {$modeswitch typehelpers}
  24. {$modeswitch advancedrecords}
  25. {$IFDEF FPC_DOTTEDUNITS}
  26. uses
  27. WASIApi.WASIApi, WASIApi.WASIUtil;
  28. {$ELSE FPC_DOTTEDUNITS}
  29. uses
  30. wasiapi, wasiutil;
  31. {$ENDIF FPC_DOTTEDUNITS}
  32. {$DEFINE OS_FILESETDATEBYNAME}
  33. {$DEFINE HAS_SLEEP}
  34. {$DEFINE HAS_GETTICKCOUNT64}
  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. type
  40. TWasiFindData = TWasiSearchRec;
  41. { Include platform independent interface part }
  42. {$i sysutilh.inc}
  43. implementation
  44. {$IFDEF FPC_DOTTEDUNITS}
  45. uses
  46. System.SysConst;
  47. {$ELSE FPC_DOTTEDUNITS}
  48. uses
  49. sysconst;
  50. {$ENDIF FPC_DOTTEDUNITS}
  51. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  52. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  53. {$DEFINE HAS_LOCALTIMEZONEOFFSET}
  54. {$DEFINE executeprocuni} (* Only 1 byte version of ExecuteProcess is provided by the OS *)
  55. Function UniversalToEpoch(year,month,day,hour,minute,second:Word):int64;
  56. begin
  57. Result:={$IFDEF FPC_DOTTEDUNITS}WASIApi.{$ENDIF}WasiUtil.UniversalToEpoch(year,month,day,hour,minute,second);
  58. end;
  59. Function LocalToEpoch(year,month,day,hour,minute,second:Word):int64;
  60. begin
  61. Result:={$IFDEF FPC_DOTTEDUNITS}WASIApi.{$ENDIF}WasiUtil.LocalToEpoch(year,month,day,hour,minute,second);
  62. end;
  63. Procedure EpochToUniversal(epoch:int64;var year,month,day,hour,minute,second:Word);
  64. begin
  65. {$IFDEF FPC_DOTTEDUNITS}WASIApi.{$ENDIF}WasiUtil.EpochToUniversal(epoch,year,month,day,hour,minute,second);
  66. end;
  67. Procedure EpochToLocal(epoch:int64;var year,month,day,hour,minute,second:Word);
  68. begin
  69. {$IFDEF FPC_DOTTEDUNITS}WASIApi.{$ENDIF}WasiUtil.EpochToLocal(epoch,year,month,day,hour,minute,second);
  70. end;
  71. { Include platform independent implementation part }
  72. {$i sysutils.inc}
  73. function GetTickCount64: QWord;
  74. var
  75. NanoSecsPast: __wasi_timestamp_t;
  76. begin
  77. if __wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC,1000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then
  78. Result:=NanoSecsPast div 1000000
  79. else
  80. Result:=0;
  81. end;
  82. {****************************************************************************
  83. File Functions
  84. ****************************************************************************}
  85. Function WasiToWinAttr (const FN : RawByteString; fd: __wasi_fd_t; pr: PAnsiChar; pr_len: size_t; Const Info : __wasi_filestat_t) : Longint;
  86. Var
  87. LinkInfo : __wasi_filestat_t;
  88. nm : RawByteString;
  89. begin
  90. Result:=faArchive;
  91. if Info.filetype=__WASI_FILETYPE_DIRECTORY then
  92. Result:=Result or faDirectory;
  93. nm:=ExtractFileName(FN);
  94. If (Length(nm)>=2) and
  95. (nm[1]='.') and
  96. (nm[2]<>'.') then
  97. Result:=Result or faHidden;
  98. If (Info.filetype=__WASI_FILETYPE_BLOCK_DEVICE) or
  99. (Info.filetype=__WASI_FILETYPE_CHARACTER_DEVICE) or
  100. (Info.filetype=__WASI_FILETYPE_SOCKET_DGRAM) or
  101. (Info.filetype=__WASI_FILETYPE_SOCKET_STREAM) then
  102. Result:=Result or faSysFile;
  103. if Info.filetype=__WASI_FILETYPE_SYMBOLIC_LINK then
  104. begin
  105. Result:=Result or faSymLink;
  106. // Windows reports if the link points to a directory.
  107. if (__wasi_path_filestat_get(fd,__WASI_LOOKUPFLAGS_SYMLINK_FOLLOW,pr,pr_len,@LinkInfo)=__WASI_ERRNO_SUCCESS) and
  108. (LinkInfo.filetype=__WASI_FILETYPE_DIRECTORY) then
  109. Result := Result or faDirectory;
  110. end;
  111. end;
  112. Function FileOpen (Const FileName : RawByteString; Mode : Integer) : THandle;
  113. Var
  114. fs_rights_base: __wasi_rights_t = 0;
  115. ourfd: __wasi_fd_t;
  116. res: __wasi_errno_t;
  117. pr: RawByteString;
  118. fd: __wasi_fd_t;
  119. Begin
  120. case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
  121. fmOpenRead:
  122. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  123. __WASI_RIGHTS_FD_FILESTAT_GET or
  124. __WASI_RIGHTS_FD_SEEK or
  125. __WASI_RIGHTS_FD_TELL or
  126. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  127. __WASI_RIGHTS_FD_ADVISE or
  128. __WASI_RIGHTS_POLL_FD_READWRITE;
  129. fmOpenWrite:
  130. fs_rights_base :=__WASI_RIGHTS_FD_WRITE or
  131. __WASI_RIGHTS_FD_FILESTAT_GET or
  132. __WASI_RIGHTS_FD_SEEK or
  133. __WASI_RIGHTS_FD_TELL or
  134. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  135. __WASI_RIGHTS_FD_ADVISE or
  136. __WASI_RIGHTS_POLL_FD_READWRITE or
  137. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  138. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  139. __WASI_RIGHTS_FD_ALLOCATE or
  140. __WASI_RIGHTS_FD_DATASYNC or
  141. __WASI_RIGHTS_FD_SYNC;
  142. fmOpenReadWrite:
  143. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  144. __WASI_RIGHTS_FD_WRITE or
  145. __WASI_RIGHTS_FD_FILESTAT_GET or
  146. __WASI_RIGHTS_FD_SEEK or
  147. __WASI_RIGHTS_FD_TELL or
  148. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  149. __WASI_RIGHTS_FD_ADVISE or
  150. __WASI_RIGHTS_POLL_FD_READWRITE or
  151. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  152. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  153. __WASI_RIGHTS_FD_ALLOCATE or
  154. __WASI_RIGHTS_FD_DATASYNC or
  155. __WASI_RIGHTS_FD_SYNC;
  156. end;
  157. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  158. begin
  159. result:=-1;
  160. exit;
  161. end;
  162. repeat
  163. res:=__wasi_path_open(fd,
  164. 0,
  165. PAnsiChar(pr),
  166. length(pr),
  167. 0,
  168. fs_rights_base,
  169. fs_rights_base,
  170. 0,
  171. @ourfd);
  172. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  173. If res=__WASI_ERRNO_SUCCESS Then
  174. Result:=ourfd
  175. else
  176. Result:=-1;
  177. end;
  178. Function FileCreate (Const FileName : RawByteString) : THandle;
  179. Const
  180. fs_rights_base: __wasi_rights_t =
  181. __WASI_RIGHTS_FD_READ or
  182. __WASI_RIGHTS_FD_WRITE or
  183. __WASI_RIGHTS_FD_FILESTAT_GET or
  184. __WASI_RIGHTS_FD_SEEK or
  185. __WASI_RIGHTS_FD_TELL or
  186. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  187. __WASI_RIGHTS_FD_ADVISE or
  188. __WASI_RIGHTS_POLL_FD_READWRITE or
  189. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  190. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  191. __WASI_RIGHTS_FD_ALLOCATE or
  192. __WASI_RIGHTS_FD_DATASYNC or
  193. __WASI_RIGHTS_FD_SYNC;
  194. Var
  195. ourfd: __wasi_fd_t;
  196. res: __wasi_errno_t;
  197. pr: RawByteString;
  198. fd: __wasi_fd_t;
  199. Begin
  200. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  201. begin
  202. result:=-1;
  203. exit;
  204. end;
  205. repeat
  206. res:=__wasi_path_open(fd,
  207. 0,
  208. PAnsiChar(pr),
  209. length(pr),
  210. __WASI_OFLAGS_CREAT or __WASI_OFLAGS_TRUNC,
  211. fs_rights_base,
  212. fs_rights_base,
  213. 0,
  214. @ourfd);
  215. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  216. If res=__WASI_ERRNO_SUCCESS Then
  217. Result:=ourfd
  218. else
  219. Result:=-1;
  220. end;
  221. Function FileCreate (Const FileName : RawByteString; ShareMode:integer; Rights : integer) : THandle;
  222. begin
  223. FileCreate:=FileCreate(FileName);
  224. end;
  225. Function FileCreate (Const FileName : RawByteString; Rights:integer) : THandle;
  226. begin
  227. FileCreate:=FileCreate(FileName);
  228. end;
  229. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
  230. var
  231. our_iov: __wasi_iovec_t;
  232. our_nread: __wasi_size_t;
  233. res: __wasi_errno_t;
  234. begin
  235. repeat
  236. our_iov.buf:=@Buffer;
  237. our_iov.buf_len:=Count;
  238. res:=__wasi_fd_read(Handle,@our_iov,1,@our_nread);
  239. until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
  240. if res=__WASI_ERRNO_SUCCESS then
  241. Result:=our_nread
  242. else
  243. Result:=-1;
  244. end;
  245. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  246. var
  247. our_iov: __wasi_ciovec_t;
  248. our_nwritten: longint;
  249. res: __wasi_errno_t;
  250. begin
  251. repeat
  252. our_iov.buf:=@Buffer;
  253. our_iov.buf_len:=Count;
  254. res:=__wasi_fd_write(Handle,@our_iov,1,@our_nwritten);
  255. until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
  256. if res=__WASI_ERRNO_SUCCESS then
  257. Result:=our_nwritten
  258. else
  259. Result:=-1;
  260. end;
  261. Function FileSeek (Handle : THandle; FOffset, Origin : Longint) : Longint;
  262. begin
  263. result:=longint(FileSeek(Handle,int64(FOffset),Origin));
  264. end;
  265. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: Longint) : Int64;
  266. var
  267. res: __wasi_errno_t;
  268. newoffset: __wasi_filesize_t;
  269. whence: __wasi_whence_t;
  270. begin
  271. case Origin of
  272. fsFromBeginning:
  273. whence:=__WASI_WHENCE_SET;
  274. fsFromCurrent:
  275. whence:=__WASI_WHENCE_CUR;
  276. fsFromEnd:
  277. whence:=__WASI_WHENCE_END;
  278. else
  279. begin
  280. Result:=-1;
  281. exit;
  282. end;
  283. end;
  284. res:=__wasi_fd_seek(Handle,FOffset,whence,@newoffset);
  285. if res=__WASI_ERRNO_SUCCESS then
  286. Result:=newoffset
  287. else
  288. Result:=-1;
  289. end;
  290. Procedure FileClose (Handle : THandle);
  291. var
  292. res: __wasi_errno_t;
  293. begin
  294. repeat
  295. res:=__wasi_fd_close(Handle);
  296. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  297. end;
  298. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  299. var
  300. res: __wasi_errno_t;
  301. begin
  302. Result:=__wasi_fd_filestat_set_size(handle,Size)=__WASI_ERRNO_SUCCESS;
  303. end;
  304. Function FileAge (Const FileName : RawByteString): Int64;
  305. var
  306. res: __wasi_errno_t;
  307. pr: RawByteString;
  308. fd: __wasi_fd_t;
  309. Info: __wasi_filestat_t;
  310. begin
  311. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  312. begin
  313. result:=-1;
  314. exit;
  315. end;
  316. res:=__wasi_path_filestat_get(fd,0,PAnsiChar(pr),length(pr),@Info);
  317. if res=__WASI_ERRNO_SUCCESS then
  318. result:=Info.mtim div 1000000000
  319. else
  320. result:=-1;
  321. end;
  322. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  323. var
  324. pr: RawByteString;
  325. fd: __wasi_fd_t;
  326. Info: __wasi_filestat_t;
  327. symlink: RawByteString;
  328. res: __wasi_errno_t;
  329. begin
  330. FillChar(SymLinkRec, SizeOf(SymLinkRec), 0);
  331. result:=false;
  332. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  333. exit;
  334. if __wasi_path_filestat_get(fd,0,PAnsiChar(pr),length(pr),@Info)<>__WASI_ERRNO_SUCCESS then
  335. exit;
  336. if Info.filetype<>__WASI_FILETYPE_SYMBOLIC_LINK then
  337. exit;
  338. if fpc_wasi_path_readlink_ansistring(fd,PAnsiChar(pr),Length(pr),symlink)<>__WASI_ERRNO_SUCCESS then
  339. exit;
  340. SymLinkRec.TargetName:=symlink;
  341. res:=__wasi_path_filestat_get(fd,__WASI_LOOKUPFLAGS_SYMLINK_FOLLOW,PAnsiChar(pr),length(pr),@Info);
  342. if res<>__WASI_ERRNO_SUCCESS then
  343. raise EDirectoryNotFoundException.Create('Error ' + IntToStr(res){todo: SysErrorMessage SysErrorMessage(GetLastOSError)});
  344. SymLinkRec.Attr := WasiToWinAttr(FileName,fd,PAnsiChar(pr),length(pr),Info);
  345. SymLinkRec.Size := Info.size;
  346. result:=true;
  347. end;
  348. function FileExists (const FileName: RawByteString; FollowLink : Boolean): boolean;
  349. var
  350. pr: RawByteString;
  351. fd: __wasi_fd_t;
  352. Info: __wasi_filestat_t;
  353. flags: __wasi_lookupflags_t;
  354. begin
  355. if FileName='' then
  356. exit(false);
  357. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  358. exit(false);
  359. if FollowLink then
  360. flags:=__WASI_LOOKUPFLAGS_SYMLINK_FOLLOW
  361. else
  362. flags:=0;
  363. if __wasi_path_filestat_get(fd,flags,PAnsiChar(pr),length(pr),@Info)=__WASI_ERRNO_SUCCESS then
  364. result:=Info.filetype<>__WASI_FILETYPE_DIRECTORY
  365. else
  366. result:=false;
  367. end;
  368. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  369. var
  370. pr: RawByteString;
  371. fd: __wasi_fd_t;
  372. Info: __wasi_filestat_t;
  373. flags: __wasi_lookupflags_t;
  374. begin
  375. if Directory='' then
  376. exit(false);
  377. if ConvertToFdRelativePath(Directory,fd,pr)<>0 then
  378. exit(false);
  379. if FollowLink then
  380. flags:=__WASI_LOOKUPFLAGS_SYMLINK_FOLLOW
  381. else
  382. flags:=0;
  383. if __wasi_path_filestat_get(fd,flags,PAnsiChar(pr),length(pr),@Info)=__WASI_ERRNO_SUCCESS then
  384. result:=Info.filetype=__WASI_FILETYPE_DIRECTORY
  385. else
  386. result:=false;
  387. end;
  388. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  389. var
  390. derror: longint;
  391. begin
  392. Result:=-1;
  393. { this is safe even though Rslt actually contains a refcounted field, because
  394. it is declared as "out" and hence has already been initialised }
  395. fillchar(Rslt,sizeof(Rslt),0);
  396. if Path='' then
  397. exit;
  398. derror:=WasiFindFirst(Path, Attr, Rslt.FindData);
  399. if derror=0 then
  400. result:=0
  401. else
  402. result:=-1;
  403. Name:=Rslt.FindData.Name;
  404. Rslt.Attr:=Rslt.FindData.Attr;
  405. Rslt.Size:=Rslt.FindData.Size;
  406. Rslt.Time:=Rslt.FindData.Time div 1000000000;
  407. end;
  408. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  409. var
  410. derror: longint;
  411. begin
  412. derror:=WasiFindNext(Rslt.FindData);
  413. if derror=0 then
  414. result:=0
  415. else
  416. result:=-1;
  417. Name:=Rslt.FindData.Name;
  418. Rslt.Attr:=Rslt.FindData.Attr;
  419. Rslt.Size:=Rslt.FindData.Size;
  420. Rslt.Time:=Rslt.FindData.Time div 1000000000;
  421. end;
  422. Procedure InternalFindClose(var Handle: THandle; var FindData: TFindData);
  423. begin
  424. WasiFindClose(FindData);
  425. end;
  426. Function FileGetDate (Handle : THandle) : Int64;
  427. var
  428. res: __wasi_errno_t;
  429. Info: __wasi_filestat_t;
  430. begin
  431. res:=__wasi_fd_filestat_get(Handle,@Info);
  432. if res=__WASI_ERRNO_SUCCESS then
  433. result:=Info.mtim div 1000000000
  434. else
  435. result:=-1;
  436. end;
  437. Function FileSetDate (Handle : THandle; Age : Int64) : Longint;
  438. begin
  439. if __wasi_fd_filestat_set_times(Handle,Age*1000000000,Age*1000000000,
  440. __WASI_FSTFLAGS_MTIM or __WASI_FSTFLAGS_ATIM)=__WASI_ERRNO_SUCCESS then
  441. result:=0
  442. else
  443. result:=-1;
  444. end;
  445. Function FileSetDate (Const FileName : RawByteString; Age : Int64) : Longint;
  446. var
  447. pr: RawByteString;
  448. fd: __wasi_fd_t;
  449. begin
  450. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  451. begin
  452. result:=-1;
  453. exit;
  454. end;
  455. if __wasi_path_filestat_set_times(fd,0,PAnsiChar(pr),length(pr),Age*1000000000,Age*1000000000,
  456. __WASI_FSTFLAGS_MTIM or __WASI_FSTFLAGS_ATIM)=__WASI_ERRNO_SUCCESS then
  457. result:=0
  458. else
  459. result:=-1;
  460. end;
  461. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  462. var
  463. pr: RawByteString;
  464. fd: __wasi_fd_t;
  465. Info: __wasi_filestat_t;
  466. begin
  467. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  468. begin
  469. result:=-1;
  470. exit;
  471. end;
  472. if __wasi_path_filestat_get(fd,0,PAnsiChar(pr),length(pr),@Info)=__WASI_ERRNO_SUCCESS then
  473. result:=WasiToWinAttr(FileName,fd,PAnsiChar(pr),length(pr),Info)
  474. else
  475. result:=-1;
  476. end;
  477. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  478. begin
  479. Result:=-1;
  480. end;
  481. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  482. var
  483. fd: __wasi_fd_t;
  484. pr: RawByteString;
  485. res: __wasi_errno_t;
  486. begin
  487. if ConvertToFdRelativePath(FileName,fd,pr)<>0 then
  488. begin
  489. result:=false;
  490. exit;
  491. end;
  492. result:=__wasi_path_unlink_file(fd,PAnsiChar(pr),Length(pr))=__WASI_ERRNO_SUCCESS;
  493. end;
  494. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  495. var
  496. fd1,fd2: __wasi_fd_t;
  497. pr1,pr2: RawByteString;
  498. res: __wasi_errno_t;
  499. begin
  500. result:=false;
  501. if ConvertToFdRelativePath(OldName,fd1,pr1)<>0 then
  502. exit;
  503. if ConvertToFdRelativePath(NewName,fd2,pr2)<>0 then
  504. exit;
  505. result:=__wasi_path_rename(fd1,PAnsiChar(pr1),Length(pr1),fd2,PAnsiChar(pr2),Length(pr2))=__WASI_ERRNO_SUCCESS;
  506. end;
  507. {****************************************************************************
  508. Disk Functions
  509. ****************************************************************************}
  510. function diskfree(drive : byte) : int64;
  511. begin
  512. end;
  513. function disksize(drive : byte) : int64;
  514. begin
  515. end;
  516. {****************************************************************************
  517. Time Functions
  518. ****************************************************************************}
  519. {$I tzenv.inc}
  520. Procedure GetLocalTime(var SystemTime: TSystemTime);
  521. var
  522. NanoSecsPast: __wasi_timestamp_t;
  523. begin
  524. if __wasi_clock_time_get(__WASI_CLOCKID_REALTIME,1000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then
  525. begin
  526. EpochToLocal(NanoSecsPast div 1000000000,
  527. SystemTime.Year,SystemTime.Month,SystemTime.Day,
  528. SystemTime.Hour,SystemTime.Minute,SystemTime.Second);
  529. SystemTime.MilliSecond := (NanoSecsPast div 1000000) mod 1000;
  530. SystemTime.DayOfWeek := DayOfWeek(EncodeDate(SystemTime.Year,SystemTime.Month,SystemTime.Day))-1;
  531. end
  532. else
  533. FillChar(SystemTime,SizeOf(SystemTime),0);
  534. end;
  535. {****************************************************************************
  536. Misc Functions
  537. ****************************************************************************}
  538. procedure sysBeep;
  539. begin
  540. end;
  541. {****************************************************************************
  542. Locale Functions
  543. ****************************************************************************}
  544. procedure InitAnsi;
  545. Var
  546. i : longint;
  547. begin
  548. { Fill table entries 0 to 127 }
  549. for i := 0 to 96 do
  550. UpperCaseTable[i] := chr(i);
  551. for i := 97 to 122 do
  552. UpperCaseTable[i] := chr(i - 32);
  553. for i := 123 to 191 do
  554. UpperCaseTable[i] := chr(i);
  555. Move (CPISO88591UCT,UpperCaseTable[192],SizeOf(CPISO88591UCT));
  556. for i := 0 to 64 do
  557. LowerCaseTable[i] := chr(i);
  558. for i := 65 to 90 do
  559. LowerCaseTable[i] := chr(i + 32);
  560. for i := 91 to 191 do
  561. LowerCaseTable[i] := chr(i);
  562. Move (CPISO88591LCT,LowerCaseTable[192],SizeOf(CPISO88591UCT));
  563. end;
  564. Procedure InitInternational;
  565. begin
  566. InitInternationalGeneric;
  567. InitAnsi;
  568. end;
  569. function SysErrorMessage(ErrorCode: Integer): String;
  570. begin
  571. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  572. end;
  573. {****************************************************************************
  574. Os utils
  575. ****************************************************************************}
  576. Function GetEnvironmentVariable(Const EnvVar : AnsiString) : AnsiString;
  577. var
  578. hp : PPAnsiChar;
  579. hs : string;
  580. eqpos : longint;
  581. begin
  582. result:='';
  583. hp:=envp;
  584. if hp<>nil then
  585. while assigned(hp^) do
  586. begin
  587. hs:=strpas(hp^);
  588. eqpos:=pos('=',hs);
  589. if copy(hs,1,eqpos-1)=envvar then
  590. begin
  591. result:=copy(hs,eqpos+1,length(hs)-eqpos);
  592. break;
  593. end;
  594. inc(hp);
  595. end;
  596. end;
  597. Function GetEnvironmentVariableCount : Integer;
  598. var
  599. p: PPAnsiChar;
  600. begin
  601. result:=0;
  602. p:=envp; {defined in system}
  603. if p<>nil then
  604. while p^<>nil do
  605. begin
  606. inc(result);
  607. inc(p);
  608. end;
  609. end;
  610. Function GetEnvironmentString(Index : Integer) : RTLString;
  611. Var
  612. i : longint;
  613. p : PPAnsiChar;
  614. begin
  615. if (Index <= 0) or (envp=nil) then
  616. result:=''
  617. else
  618. begin
  619. p:=envp; {defined in system}
  620. i:=1;
  621. while (i<Index) and (p^<>nil) do
  622. begin
  623. inc(i);
  624. inc(p);
  625. end;
  626. if p^=nil then
  627. result:=''
  628. else
  629. result:=strpas(p^)
  630. end;
  631. end;
  632. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  633. begin
  634. end;
  635. function ExecuteProcess (const Path: RawByteString;
  636. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  637. begin
  638. end;
  639. {*************************************************************************
  640. Sleep
  641. *************************************************************************}
  642. procedure Sleep (MilliSeconds: Cardinal);
  643. var
  644. subscription: __wasi_subscription_t;
  645. event: __wasi_event_t;
  646. nevents: __wasi_size_t;
  647. begin
  648. FillChar(subscription,SizeOf(subscription),0);
  649. subscription.u.tag:=__WASI_EVENTTYPE_CLOCK;
  650. subscription.u.u.clock.id:=__WASI_CLOCKID_MONOTONIC;
  651. subscription.u.u.clock.timeout:=MilliSeconds*1000000;
  652. subscription.u.u.clock.precision:=1000000;
  653. subscription.u.u.clock.flags:=0; { timeout value is relative }
  654. __wasi_poll_oneoff(@subscription,@event,1,@nevents);
  655. end;
  656. {****************************************************************************
  657. Initialization code
  658. ****************************************************************************}
  659. Initialization
  660. InitExceptions; { Initialize exceptions. OS independent }
  661. InitInternational; { Initialize internationalization settings }
  662. OnBeep:=@SysBeep;
  663. InitTZ;
  664. Finalization
  665. FreeTerminateProcs;
  666. DoneExceptions;
  667. end.