Skip to content

Commit bfdc4ab

Browse files
committed
FileOps.remove(): return overall success, first error and restructure per-file information for more detailed logging
1 parent 46c88bd commit bfdc4ab

2 files changed

Lines changed: 38 additions & 18 deletions

File tree

macros/l0.DependencyControl.Toolbox.moon

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ msgs = {
1616
uninstall: {
1717
running: "Uninstalling %s '%s'..."
1818
success: "%s '%s' was removed sucessfully. Reload your automation scripts or restart Aegisub for the changes to take effect."
19-
lockedFiles: "%s Some script files are still in use and will be deleted during the next restart/reload."
19+
lockedFiles: "%s Some script files are still in use and will be deleted during the next restart/reload:\n%s"
2020
error: "Error: %s"
2121
}
2222
macroConfig: {
@@ -140,12 +140,19 @@ uninstall = ->
140140
return unless script
141141
scriptType = script.moduleName and "Module" or "Macro"
142142
logger\log msgs.uninstall.running, scriptType, script.name
143-
success, lockedFiles = DepCtrl(script)\uninstall!
144-
if success
143+
success, details = DepCtrl(script)\uninstall!
144+
if success == nil
145+
fileList = table.concat ["#{path}: #{res[2]}" for path, res in pairs details when res[1] == nil], "\n"
146+
logger\log msgs.uninstall.error, fileList
147+
else
145148
msg = msgs.uninstall.success\format scriptType, script.name
146-
logger\log lockedFiles and msgs.uninstall.lockedFiles\format(msg) or msg
149+
logger\log if success
150+
msg
151+
else
152+
fileList = table.concat ["#{path} (#{res[2]})" for path, res in pairs details when res[1] != true], "\n"
153+
msgs.uninstall.lockedFiles\format msg, fileList
147154

148-
else logger\log msgs.uninstall.error, lockedFiles
155+
return success
149156

150157
config = getConfig!
151158

modules/DependencyControl/FileOps.moon

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ local ConfigHandler
55

66
class FileOps
77
msgs = {
8+
generic: {
9+
deletionRescheduled: "Another deletion attempt has been rescheduled for the next restart."
10+
}
811
attributes: {
912
badPath: "Path failed verification: %s."
1013
genericError: "Can't retrieve attributes: %s."
@@ -34,7 +37,7 @@ class FileOps
3437
}
3538
rmdir: {
3639
emptyPath: "Argument #1 (path) must not be an empty string."
37-
couldntRemoveFiles: "The specified directory contains files and folders that couldn't be removed."
40+
couldntRemoveFiles: "Some of the files and folders in the specified directory couldn't be removed:\n%s"
3841
couldntRemoveDir: "Error removing empty directory: %s."
3942

4043
}
@@ -68,28 +71,37 @@ class FileOps
6871

6972
remove: (paths, recurse, reSchedule) ->
7073
config = createConfig true
71-
configLoaded, res = false, {}
74+
configLoaded, overallSuccess, details, firstErr = false, true, {}
7275
paths = {paths} unless type(paths) == "table"
7376

7477
for path in *paths
7578
mode, path = FileOps.attributes path, "mode"
7679
if mode
7780
rmFunc = mode == "file" and os.remove or FileOps.rmdir
78-
success, err = rmFunc path, recurse
79-
if not success
80-
unless reSchedule
81-
res[#res+1] = {nil, err}
81+
res, err = rmFunc path, recurse
82+
unless res
83+
firstErr or= err
84+
unless reSchedule -- delete operation failed entirely
85+
details[path] = {nil, err}
86+
overallSuccess = nil
8287
continue
88+
89+
-- load the FileOps configuration file and reschedule deletions
8390
unless configLoaded
8491
FileOps.config\load!
8592
configLoaded = true
8693
config.c.toRemove[path] = os.time!
87-
res[#res+1] = {false, err}
88-
else res[#res+1] = {true}
89-
else res[#res+1] = {nil, path}
94+
-- mark the operations as failed "for now", indicating a second attempt has been scheduled
95+
details[path] = {false, err}
96+
overallSuccess = false
97+
98+
-- delete operation succeeded
99+
else details[path] = {true}
100+
-- file not found or permission issue
101+
else details[path] = {nil, err}
90102

91103
config\write! if configLoaded
92-
return res, configLoaded
104+
return overallSuccess, details, firstErr
93105

94106
runScheduledRemoval: (configDir) ->
95107
config = createConfig false, configDir
@@ -186,9 +198,10 @@ class FileOps
186198
if recurse
187199
-- recursively remove contained files and directories
188200
toRemove = ["#{path}/#{file}" for file in lfs.dir path]
189-
res, err = FileOps.remove toRemove, true
190-
if err
191-
return nil, msgs.rmdir.couldntRemoveFiles, res
201+
res, details = FileOps.remove toRemove, true
202+
unless res
203+
fileList = table.concat ["#{path}: #{res[2]}" for path, res in pairs details when not res[1]], "\n"
204+
return nil, msgs.rmdir.couldntRemoveFiles\format fileList
192205

193206
-- remove empty directory
194207
success, err = lfs.rmdir path

0 commit comments

Comments
 (0)