@@ -4,46 +4,18 @@ import (
44 "context"
55 "io/ioutil"
66 "os"
7- "path/filepath"
8- "sort"
9- "strings"
107
118 "github.com/containerd/containerd/log"
129 "github.com/pkg/errors"
1310)
1411
15- func init () {
16- t , err := TempLocation ("/tmp" )
17- if err != nil {
18- panic (err )
19- }
20- DefaultTempLocation = t
21- }
22-
23- var DefaultTempLocation TempMounts
24-
25- func TempLocation (root string ) (TempMounts , error ) {
26- root , err := filepath .Abs (root )
27- if err != nil {
28- return DefaultTempLocation , err
29- }
30- if err := os .MkdirAll (root , 0700 ); err != nil {
31- return DefaultTempLocation , err
32- }
33- return TempMounts {
34- root : root ,
35- }, nil
36- }
37-
38- type TempMounts struct {
39- root string
40- }
12+ var tempMountLocation = os .TempDir ()
4113
4214// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
4315// The mounts are valid during the call to the f.
4416// Finally we will unmount and remove the temp dir regardless of the result of f.
45- func ( t TempMounts ) Mount (ctx context.Context , mounts []Mount , f func (root string ) error ) (err error ) {
46- root , uerr := ioutil .TempDir (t . root , "containerd-WithTempMount " )
17+ func WithTempMount (ctx context.Context , mounts []Mount , f func (root string ) error ) (err error ) {
18+ root , uerr := ioutil .TempDir (tempMountLocation , "containerd-mount " )
4719 if uerr != nil {
4820 return errors .Wrapf (uerr , "failed to create temp dir" )
4921 }
@@ -76,43 +48,3 @@ func (t TempMounts) Mount(ctx context.Context, mounts []Mount, f func(root strin
7648 }
7749 return errors .Wrapf (f (root ), "mount callback failed on %s" , root )
7850}
79-
80- // Unmount all temp mounts and remove the directories
81- func (t TempMounts ) Unmount (flags int ) error {
82- mounts , err := PID (os .Getpid ())
83- if err != nil {
84- return err
85- }
86- var toUnmount []string
87- for _ , m := range mounts {
88- if strings .HasPrefix (m .Mountpoint , t .root ) {
89- toUnmount = append (toUnmount , m .Mountpoint )
90- }
91- }
92- sort .Sort (sort .Reverse (mountSorter (toUnmount )))
93- for _ , path := range toUnmount {
94- if err := UnmountAll (path , flags ); err != nil {
95- return err
96- }
97- if err := os .Remove (path ); err != nil {
98- return err
99- }
100- }
101- return nil
102- }
103-
104- type mountSorter []string
105-
106- func (by mountSorter ) Len () int {
107- return len (by )
108- }
109-
110- func (by mountSorter ) Less (i , j int ) bool {
111- is := strings .Split (by [i ], string (os .PathSeparator ))
112- js := strings .Split (by [j ], string (os .PathSeparator ))
113- return len (is ) < len (js )
114- }
115-
116- func (by mountSorter ) Swap (i , j int ) {
117- by [i ], by [j ] = by [j ], by [i ]
118- }
0 commit comments