M10: clean up empty mount directories after unmount
Privileged unmount now does a best-effort rmdir on the mount point after umount succeeds. Only removes empty directories; non-empty dirs and errors are silently ignored. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -64,7 +64,8 @@ func Mount(devicePath, mountpoint string) (string, error) {
|
||||
return mountpoint, nil
|
||||
}
|
||||
|
||||
// Unmount unmounts the given mountpoint using privileged umount.
|
||||
// Unmount unmounts the given mountpoint using privileged umount, then
|
||||
// removes the mount directory if it is empty.
|
||||
func Unmount(mountpoint string) error {
|
||||
args := withPrivilege([]string{"umount", mountpoint})
|
||||
cmd := exec.Command(args[0], args[1:]...)
|
||||
@@ -73,6 +74,12 @@ func Unmount(mountpoint string) error {
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("umount %s: %w", mountpoint, err)
|
||||
}
|
||||
|
||||
// Clean up empty mount directory. Best-effort — ignore errors
|
||||
// (directory may not be empty or may be a system path).
|
||||
rmdirArgs := withPrivilege([]string{"rmdir", mountpoint})
|
||||
exec.Command(rmdirArgs[0], rmdirArgs[1:]...).Run()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user