@@ -22,7 +22,8 @@ import (
2222 "os"
2323 "path/filepath"
2424 "sort"
25- "strings"
25+
26+ "github.com/moby/sys/mountinfo"
2627)
2728
2829// SetTempMountLocation sets the temporary mount location
@@ -40,23 +41,20 @@ func SetTempMountLocation(root string) error {
4041
4142// CleanupTempMounts all temp mounts and remove the directories
4243func CleanupTempMounts (flags int ) (warnings []error , err error ) {
43- mounts , err := Self ( )
44+ mounts , err := mountinfo . GetMounts ( mountinfo . PrefixFilter ( tempMountLocation ) )
4445 if err != nil {
4546 return nil , err
4647 }
47- var toUnmount []string
48- for _ , m := range mounts {
49- if strings .HasPrefix (m .Mountpoint , tempMountLocation ) {
50- toUnmount = append (toUnmount , m .Mountpoint )
51- }
52- }
53- sort .Sort (sort .Reverse (sort .StringSlice (toUnmount )))
54- for _ , path := range toUnmount {
55- if err := UnmountAll (path , flags ); err != nil {
48+ // Make the deepest mount be first
49+ sort .Slice (mounts , func (i , j int ) bool {
50+ return len (mounts [i ].Mountpoint ) > len (mounts [j ].Mountpoint )
51+ })
52+ for _ , mount := range mounts {
53+ if err := UnmountAll (mount .Mountpoint , flags ); err != nil {
5654 warnings = append (warnings , err )
5755 continue
5856 }
59- if err := os .Remove (path ); err != nil {
57+ if err := os .Remove (mount . Mountpoint ); err != nil {
6058 warnings = append (warnings , err )
6159 }
6260 }
0 commit comments