11package mount
22
33import (
4+ "context"
45 "io/ioutil"
56 "os"
67
@@ -34,20 +35,21 @@ func All(mounts []Mount, target string) error {
3435// WithTempMount mounts the provided mounts to a temp dir, and pass the temp dir to f.
3536// The mounts are valid during the call to the f.
3637// Finally we will unmount and remove the temp dir regardless of the result of f.
37- func WithTempMount (mounts []Mount , f func (root string ) error ) (err error ) {
38+ func WithTempMount (ctx context. Context , mounts []Mount , f func (root string ) error ) (err error ) {
3839 root , uerr := ioutil .TempDir ("" , "containerd-WithTempMount" )
3940 if uerr != nil {
40- return errors .Wrapf (uerr , "failed to create temp dir for %v" , mounts )
41+ return errors .Wrapf (uerr , "failed to create temp dir" )
4142 }
4243 // We use Remove here instead of RemoveAll.
4344 // The RemoveAll will delete the temp dir and all children it contains.
44- // When the Unmount fails, if we use RemoveAll, We will incorrectly delete data from mounted dir.
45- // if we use Remove,even though we won't successfully delete the temp dir,
46- // but we only leak a temp dir, we don't loss data from mounted dir.
45+ // When the Unmount fails, RemoveAll will incorrectly delete data from
46+ // the mounted dir. However, if we use Remove, even though we won't
47+ // successfully delete the temp dir and it may leak, we won't loss data
48+ // from the mounted dir.
4749 // For details, please refer to #1868 #1785.
4850 defer func () {
4951 if uerr = os .Remove (root ); uerr != nil {
50- log .L . Errorf ("Failed to remove the temp dir %s: %v" , root , uerr )
52+ log .G ( ctx ). WithError ( uerr ). WithField ( "dir" , root ). Errorf ("failed to remove mount temp dir" )
5153 }
5254 }()
5355
@@ -66,8 +68,5 @@ func WithTempMount(mounts []Mount, f func(root string) error) (err error) {
6668 return errors .Wrapf (uerr , "failed to mount %s" , root )
6769 }
6870
69- if uerr = f (root ); uerr != nil {
70- return errors .Wrapf (uerr , "failed to f(%s)" , root )
71- }
72- return nil
71+ return errors .Wrapf (f (root ), "mount callback failed on %s" , root )
7372}
0 commit comments