|
@@ -100,6 +100,99 @@ begin
|
|
|
end;
|
|
|
|
|
|
procedure Do_Open(var f; p: pchar; flags: longint; pchangeable: boolean);
|
|
|
-begin
|
|
|
- DebugWriteLn('Do_Open');
|
|
|
+{
|
|
|
+ FileRec and textrec have both Handle and mode as the first items so
|
|
|
+ they could use the same routine for opening/creating.
|
|
|
+ when (flags and $100) the file will be append
|
|
|
+ when (flags and $1000) the file will be truncate/rewritten
|
|
|
+ when (flags and $10000) there is no check for close (needed for textfiles)
|
|
|
+}
|
|
|
+var
|
|
|
+ oflags : __wasi_oflags_t = 0;
|
|
|
+ fs_rights_base: __wasi_rights_t = 0;
|
|
|
+ fdflags: __wasi_fdflags_t = 0;
|
|
|
+ ourfd: __wasi_fd_t;
|
|
|
+ res: __wasi_errno_t;
|
|
|
+Begin
|
|
|
+{ close first if opened }
|
|
|
+ if ((flags and $10000)=0) then
|
|
|
+ begin
|
|
|
+ case FileRec(f).mode of
|
|
|
+ fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
|
|
|
+ fmclosed : ;
|
|
|
+ else
|
|
|
+ begin
|
|
|
+ inoutres:=102; {not assigned}
|
|
|
+ exit;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+{ reset file Handle }
|
|
|
+ FileRec(f).Handle:=UnusedHandle;
|
|
|
+{ We do the conversion of filemodes here, concentrated on 1 place }
|
|
|
+ case (flags and 3) of
|
|
|
+ 0 : begin
|
|
|
+ fs_rights_base :=__WASI_RIGHTS_FD_READ;
|
|
|
+ FileRec(f).mode:=fminput;
|
|
|
+ end;
|
|
|
+ 1 : begin
|
|
|
+ fs_rights_base :=__WASI_RIGHTS_FD_WRITE;
|
|
|
+ FileRec(f).mode:=fmoutput;
|
|
|
+ end;
|
|
|
+ 2 : begin
|
|
|
+ fs_rights_base :=__WASI_RIGHTS_FD_READ or __WASI_RIGHTS_FD_WRITE;
|
|
|
+ FileRec(f).mode:=fminout;
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ if (flags and $1000)=$1000 then
|
|
|
+ oflags:=oflags or (__WASI_OFLAGS_CREAT or __WASI_OFLAGS_TRUNC)
|
|
|
+ else
|
|
|
+ if (flags and $100)=$100 then
|
|
|
+ fdflags:=fdflags or __WASI_FDFLAGS_APPEND;
|
|
|
+{ empty name is special }
|
|
|
+ if p[0]=#0 then
|
|
|
+ begin
|
|
|
+ case FileRec(f).mode of
|
|
|
+ fminput :
|
|
|
+ FileRec(f).Handle:=StdInputHandle;
|
|
|
+ fminout, { this is set by rewrite }
|
|
|
+ fmoutput :
|
|
|
+ FileRec(f).Handle:=StdOutputHandle;
|
|
|
+ fmappend :
|
|
|
+ begin
|
|
|
+ FileRec(f).Handle:=StdOutputHandle;
|
|
|
+ FileRec(f).mode:=fmoutput; {fool fmappend}
|
|
|
+ end;
|
|
|
+ end;
|
|
|
+ exit;
|
|
|
+ end;
|
|
|
+{ real open call }
|
|
|
+ repeat
|
|
|
+ res:=path_open(0,
|
|
|
+ 0,
|
|
|
+ p,
|
|
|
+ strlen(p),
|
|
|
+ oflags,
|
|
|
+ fs_rights_base,
|
|
|
+ fs_rights_base,
|
|
|
+ fdflags,
|
|
|
+ @ourfd);
|
|
|
+ until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
|
|
|
+ {if (res=__WASI_ERRNO_ROFS) and ((OFlags and O_RDWR)<>0) then
|
|
|
+ begin
|
|
|
+ Oflags:=Oflags and not(O_RDWR);
|
|
|
+ repeat
|
|
|
+ FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
|
|
|
+ until (FileRec(f).Handle<>-1) or (geterrno<>ESysEINTR);
|
|
|
+ end;}
|
|
|
+ If res<>__WASI_ERRNO_SUCCESS Then
|
|
|
+ begin
|
|
|
+ FileRec(f).mode:=fmclosed;
|
|
|
+ InOutRes:=Errno2InoutRes(res);
|
|
|
+ end
|
|
|
+ else
|
|
|
+ begin
|
|
|
+ FileRec(f).Handle:=ourfd;
|
|
|
+ InOutRes:=0;
|
|
|
+ end;
|
|
|
end;
|