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"
"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)
}
}