From f6a0ec7c0aeba58be83f7e3926eceeee6aa328ea Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 22 Sep 2020 12:54:13 +0200 Subject: [PATCH] mount.CleanupTempMounts() use github.com/moby/sys/mountinfo Signed-off-by: Sebastiaan van Stijn --- mount/temp_unix.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/mount/temp_unix.go b/mount/temp_unix.go index 3d490e8a7..ed190b8f9 100644 --- a/mount/temp_unix.go +++ b/mount/temp_unix.go @@ -22,7 +22,8 @@ import ( "os" "path/filepath" "sort" - "strings" + + "github.com/moby/sys/mountinfo" ) // SetTempMountLocation sets the temporary mount location @@ -40,23 +41,20 @@ func SetTempMountLocation(root string) error { // CleanupTempMounts all temp mounts and remove the directories func CleanupTempMounts(flags int) (warnings []error, err error) { - mounts, err := Self() + mounts, err := mountinfo.GetMounts(mountinfo.PrefixFilter(tempMountLocation)) if err != nil { return nil, err } - var toUnmount []string - for _, m := range mounts { - if strings.HasPrefix(m.Mountpoint, tempMountLocation) { - toUnmount = append(toUnmount, m.Mountpoint) - } - } - sort.Sort(sort.Reverse(sort.StringSlice(toUnmount))) - for _, path := range toUnmount { - if err := UnmountAll(path, flags); err != nil { + // Make the deepest mount be first + sort.Slice(mounts, func(i, j int) bool { + return len(mounts[i].Mountpoint) > len(mounts[j].Mountpoint) + }) + for _, mount := range mounts { + if err := UnmountAll(mount.Mountpoint, flags); err != nil { warnings = append(warnings, err) continue } - if err := os.Remove(path); err != nil { + if err := os.Remove(mount.Mountpoint); err != nil { warnings = append(warnings, err) } }