Skip to content

Commit a147551

Browse files
committed
FileOps.move(): improve cleanup of files that couldn't be overwritten and were rename instead
1 parent bfdc4ab commit a147551

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

modules/DependencyControl/FileOps.moon

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class FileOps
2626
openError: "Couldn't open %s file '%s' for reading: \n%s"
2727
}
2828
move: {
29-
inUseTryingRename: "Target file '%s' already exists and appears to be in use. Trying to rename..."
29+
inUseTryingRename: "Target file '%s' already exists and appears to be in use. Trying to rename and delete existing file..."
30+
renamedDeletionFailed: "The existing file was successfully renamed to '%s', but couldn't be deleted (%s).\n%s"
3031
overwritingFile: "File '%s' already exists, overwriting..."
3132
createdDir: "Created target directory '%s'."
3233
existsNoFile: "Couldn't move file '%s' to '%s' because a %s of the same name is already present."
@@ -163,14 +164,22 @@ class FileOps
163164
mode, err = FileOps.attributes target, "mode"
164165
if mode == "file"
165166
FileOps.logger\trace msgs.move.overwritingFile, target
166-
res, err = os.remove target
167-
unless res -- can't remove old target file, probably in use or lack of permissions
167+
res, _, err = FileOps.remove target
168+
unless res
169+
-- can't remove old target file, probably in use or lack of permissions
170+
-- try to rename and then delete it
168171
FileOps.logger\debug msgs.move.inUseTryingRename, target
169172
junkName = "#{target}.depCtrlRemoved"
170-
os.remove junkName
173+
-- There might be an old removed file we couldn't delete before
174+
FileOps.remove junkName
171175
res = os.rename target, junkName
172176
unless res
173177
return false, msgs.move.cantRemove\format target, err
178+
-- rename succeeded, now clean up after ourselves
179+
res, _, err = FileOps.remove junkName, false, true
180+
unless res
181+
FileOps.logger\debug msgs.move.renamedDeletionFailed, junkName, err, msgs.generic.deletionRescheduled
182+
174183
elseif mode -- a directory (or something else) of the same name as the target file is already present
175184
return false, msgs.move.existsNoFile\format source, target, mode
176185
elseif mode == nil -- if retrieving the attributes of a file fails, something is probably wrong

0 commit comments

Comments
 (0)