Skip to content

Commit 8e20a16

Browse files
committed
FileOps.move(): try copy+remove if renaming fails
This is required to make moving files across file systems possible on *nix (thanks jabashque for reporting this bug)
1 parent a147551 commit 8e20a16

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

modules/DependencyControl/FileOps.moon

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class FileOps
3434
genericError: "An error occured while moving file '%s' to '%s':\n%s"
3535
createDirError: "Moving '%s' to '%s' failed (%s)."
3636
cantRemove: "Couldn't overwrite file '%s': %s. Attempts at renaming the existing target file failed."
37-
cantRename: "Couldn't move file '%s' to '%s': %s"
37+
cantRenameTryingCopy: "Move operation failed to rename '%s' to '%s' (%s), trying copy+remove instead..."
38+
couldntRemoveFiles: "Move operation suceeded to copied the file(s) to the target location, but some of the source files couldn't be removed:\n%s\n%s"
39+
cantCopy: "Move operation failed to copy '%s' to '%s' (%s) after a failed rename attempt (%s)."
3840
}
3941
rmdir: {
4042
emptyPath: "Argument #1 (path) must not be an empty string."
@@ -194,8 +196,19 @@ class FileOps
194196

195197
-- at this point the target directory exists and the target file doesn't, move the file
196198
res, err = os.rename source, target
197-
unless res -- renaming the file failed, probably a permission issue
198-
return false, msgs.move.cantRename\format source, target, err
199+
unless res
200+
-- renaming the file failed, could be because of a permission issue
201+
-- but me might a well be trying to rename over file system boundaries on *nix
202+
-- so we should try copy + remove before giving up
203+
FileOps.logger\debug msgs.move.cantRenameTryingCopy, source, target, err
204+
renErr, res, err = err, FileOps.copy source, target
205+
unless res
206+
return false, msgs.move.cantCopy\format source, target, err, renErr
207+
res, details = FileOps.remove source, false, true -- TODO: also support directories/recursion, but also require copy to support it
208+
209+
unless res
210+
fileList = table.concat ["#{path}: #{res[2]}" for path, res in pairs details when not res[1]], "\n"
211+
FileOps.logger\debug msgs.move.couldntRemoveFiles, fileList, msgs.generic.deletionRescheduled
199212

200213
return true
201214

0 commit comments

Comments
 (0)