[browser][io] Workaround for issue MoveDirectory (#40310)
authorKenneth Pouncey <kjpou@pt.lu>
Tue, 4 Aug 2020 14:57:14 +0000 (16:57 +0200)
committerGitHub <noreply@github.com>
Tue, 4 Aug 2020 14:57:14 +0000 (16:57 +0200)
* [browser][io] Workaround for issue MoveDirectory

- Issue workaround while waiting for emscripten fix.  https://github.com/dotnet/runtime/issues/40305
- The following code checks for the existence of the source and destination directories which replaces the same code in the emscripten code.

emscripten tracking issue:  https://github.com/emscripten-core/emscripten/issues/11804

* Update src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs

Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
Co-authored-by: Alexander Köplinger <alex.koeplinger@outlook.com>
src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs

index cb39fcb..df790a5 100644 (file)
@@ -381,6 +381,19 @@ namespace System.IO
                 throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath));
             }
 
+#if TARGET_BROWSER
+            // renaming a file doesn't return correct error code on emscripten if one of the parent paths does not exist,
+            // manually workaround it for now (https://github.com/dotnet/runtime/issues/40305)
+            if (!Directory.Exists(Path.GetDirectoryName(sourceFullPath)))
+            {
+                throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath));
+            }
+            if (!Directory.Exists(Path.GetDirectoryName(destFullPath)))
+            {
+                throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destFullPath));
+            }
+#endif
+
             if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0)
             {
                 Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();