[m-rev.] for review: Allow rename_file to rename a directory on C# backend.

Peter Wang novalazy at gmail.com
Thu Aug 25 13:15:33 AEST 2022


io.file.m:
    As above.

diff --git a/library/io.file.m b/library/io.file.m
index 173db22f6..986de4842 100644
--- a/library/io.file.m
+++ b/library/io.file.m
@@ -46,9 +46,9 @@
 
     % rename_file(OldFileName, NewFileName, Result, !IO).
     %
-    % Attempts to rename the file OldFileName as NewFileName, binding
-    % Result to ok/0 if it succeeds, or error/1 if it fails. If OldFileName
-    % names a file that is currently open, the behaviour is
+    % Attempts to rename the file or directory OldFileName as NewFileName,
+    % binding Result to ok/0 if it succeeds, or error/1 if it fails.
+    % If OldFileName names a file that is currently open, the behaviour is
     % implementation-dependent. If NewFileName names a file that already
     % exists the behaviour is also implementation-dependent; on some systems,
     % the file previously named NewFileName will be deleted and replaced
@@ -398,10 +398,14 @@ rename_file(OldFileName, NewFileName, Result, !IO) :-
     [will_not_call_mercury, promise_pure, tabled_for_io, thread_safe],
 "
     try {
-        // XXX This won't clobber NewFileName, unlike the C and Java
-        // implementations.
-        // XXX This won't rename a directory.
-        System.IO.File.Move(OldFileName, NewFileName);
+        if (System.IO.Directory.Exists(OldFileName)) {
+            System.IO.Directory.Move(OldFileName, NewFileName);
+        } else {
+            // XXX This won't clobber NewFileName.
+            // .NET Core 3.0 and later versions support a overload of the
+            // Move() method with an overwrite parameter.
+            System.IO.File.Move(OldFileName, NewFileName);
+        }
         Error = null;
     } catch (System.Exception e) {
         Error = e;
-- 
2.37.1



More information about the reviews mailing list