-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure to remove all temporary directories
Previously, some files could not be removed because of the directories permissions, which made os.RemoveAll() fail silently and left a lot of garbage files in /tmp. This commit adds a new forceRemoveAll() function that removes all files and directories more reliably, in a new fs_utils.go file for filesystem utilities. isFile() is also moved to this new file. Also log errors when forceRemoveAll fails to detect these issues sooner in the future.
- Loading branch information
Showing
2 changed files
with
40 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// isFile checks if a path exists and is a file (not a directory). | ||
func isFile(path string) bool { | ||
if info, err := os.Stat(path); err == nil { | ||
return !info.IsDir() | ||
} | ||
return false | ||
} | ||
|
||
// forceRemoveAll is a more robust alternative to [os.RemoveAll] that tries | ||
// harder to remove all the files and directories. | ||
func forceRemoveAll(path string) error { | ||
// first pass to make sure all the directories are writable | ||
err := filepath.Walk(path, func(path string, info fs.FileInfo, err error) error { | ||
if info.IsDir() { | ||
os.Chmod(path, 0777) | ||
} else { | ||
// remove files by the way | ||
os.Remove(path) | ||
} | ||
return err | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
// remove the remaining directories | ||
return os.RemoveAll(path) | ||
} |