mount.CleanupTempMounts() use github.com/moby/sys/mountinfo

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-09-22 12:54:13 +02:00
parent 43578a9d6e
commit f6a0ec7c0a
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -22,7 +22,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"github.com/moby/sys/mountinfo"
) )
// SetTempMountLocation sets the temporary mount location // SetTempMountLocation sets the temporary mount location
@ -40,23 +41,20 @@ func SetTempMountLocation(root string) error {
// CleanupTempMounts all temp mounts and remove the directories // CleanupTempMounts all temp mounts and remove the directories
func CleanupTempMounts(flags int) (warnings []error, err error) { func CleanupTempMounts(flags int) (warnings []error, err error) {
mounts, err := Self() mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(tempMountLocation))
if err != nil { if err != nil {
return nil, err return nil, err
} }
var toUnmount []string // Make the deepest mount be first
for _, m := range mounts { sort.Slice(mounts, func(i, j int) bool {
if strings.HasPrefix(m.Mountpoint, tempMountLocation) { return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint)
toUnmount = append(toUnmount, m.Mountpoint) })
} for _, mount := range mounts {
} if err := UnmountAll(mount.Mountpoint, flags); err != nil {
sort.Sort(sort.Reverse(sort.StringSlice(toUnmount)))
for _, path := range toUnmount {
if err := UnmountAll(path, flags); err != nil {
warnings = append(warnings, err) warnings = append(warnings, err)
continue continue
} }
if err := os.Remove(path); err != nil { if err := os.Remove(mount.Mountpoint); err != nil {
warnings = append(warnings, err) warnings = append(warnings, err)
} }
} }