Browse Source

+ added error handling to the do_read and do_Write WASI routines

git-svn-id: branches/wasm@48310 -
nickysn 4 years ago
parent
commit
e14451c6b6
2 changed files with 66 additions and 22 deletions
  1. 32 8
      rtl/wasi/sysfile.inc
  2. 34 14
      rtl/wasi/sysos.inc

+ 32 - 8
rtl/wasi/sysfile.inc

@@ -37,22 +37,46 @@ function Do_Write(Handle:thandle;Addr:Pointer;Len:Longint):longint;
 var
 var
   our_iov: __wasi_ciovec_t;
   our_iov: __wasi_ciovec_t;
   our_nwritten: longint;
   our_nwritten: longint;
+  res: __wasi_errno_t;
 begin
 begin
-  our_iov.buf := Addr;
-  our_iov.buf_len := Len;
-  fd_write(Handle, @our_iov, 1, @our_nwritten);
-  Do_Write:=our_nwritten;
+  repeat
+    our_iov.buf := Addr;
+    our_iov.buf_len := Len;
+    res:=fd_write(Handle, @our_iov, 1, @our_nwritten);
+  until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
+  if res=__WASI_ERRNO_SUCCESS then
+    begin
+      Do_Write:=our_nwritten;
+      InOutRes:=0;
+    end
+  else
+    begin
+      Do_Write:=0;
+      InOutRes:=Errno2InoutRes(res);
+    end;
 end;
 end;
 
 
 function Do_Read(Handle:thandle;Addr:Pointer;Len:Longint):Longint;
 function Do_Read(Handle:thandle;Addr:Pointer;Len:Longint):Longint;
 var
 var
   our_iov: __wasi_iovec_t;
   our_iov: __wasi_iovec_t;
   our_nread: __wasi_size_t;
   our_nread: __wasi_size_t;
+  res: __wasi_errno_t;
 begin
 begin
-  our_iov.buf:=Addr;
-  our_iov.buf_len:=Len;
-  fd_read(Handle,@our_iov,1,@our_nread);
-  Do_Read:=our_nread;
+  repeat
+    our_iov.buf:=Addr;
+    our_iov.buf_len:=Len;
+    fd_read(Handle,@our_iov,1,@our_nread);
+  until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
+  if res=__WASI_ERRNO_SUCCESS then
+    begin
+      Do_Read:=our_nread;
+      InOutRes:=0;
+    end
+  else
+    begin
+      Do_Read:=0;
+      InOutRes:=Errno2InoutRes(res);
+    end;
 end;
 end;
 
 
 function Do_FilePos(Handle: thandle):Int64;
 function Do_FilePos(Handle: thandle):Int64;

+ 34 - 14
rtl/wasi/sysos.inc

@@ -15,22 +15,42 @@
 
 
  **********************************************************************}
  **********************************************************************}
 
 
-{procedure GetInOutRes(def: Word);
-var
-  regs : Registers;
+function Errno2InoutRes(errno: __wasi_errno_t): Word;
 begin
 begin
-  regs.AX:=$5900;
-  regs.BX:=$0;
-  MsDos(regs);
-  InOutRes:=regs.AX;
-  case InOutRes of
-   19 : InOutRes:=150;
-   21 : InOutRes:=152;
-   32 : InOutRes:=5;
+  case errno of
+    __WASI_ERRNO_NFILE,
+    __WASI_ERRNO_MFILE:
+      Errno2InoutRes:=4;
+    __WASI_ERRNO_NOENT:
+      Errno2InoutRes:=2;
+    __WASI_ERRNO_BADF:
+      Errno2InoutRes:=6;
+    __WASI_ERRNO_NOMEM,
+    __WASI_ERRNO_FAULT:
+      Errno2InoutRes:=217;
+    __WASI_ERRNO_INVAL:
+      Errno2InoutRes:=218;
+    __WASI_ERRNO_PIPE,
+    __WASI_ERRNO_INTR,
+    __WASI_ERRNO_IO,
+    __WASI_ERRNO_AGAIN,
+    __WASI_ERRNO_NOSPC:
+      Errno2InoutRes:=101;
+    __WASI_ERRNO_NAMETOOLONG:
+      Errno2InoutRes:=3;
+    __WASI_ERRNO_ROFS,
+    __WASI_ERRNO_EXIST,
+    __WASI_ERRNO_NOTEMPTY,
+    __WASI_ERRNO_ACCES:
+      Errno2InoutRes:=5;
+    __WASI_ERRNO_BUSY,
+    __WASI_ERRNO_NOTDIR,        // busy, enotdir, mantis #25931
+    __WASI_ERRNO_ISDIR:
+      Errno2InoutRes:=5;
+    else
+      Errno2InoutRes:=errno;
   end;
   end;
-  if InOutRes=0 then
-    InOutRes:=Def;
-end;}
+end;
 
 
 {*****************************************************************************
 {*****************************************************************************
                           Low Level File Routines
                           Low Level File Routines