55 "errors"
66 "os"
77 "path/filepath"
8+ "strings"
89 "syscall"
910
1011 "github.com/containerd/continuity/fs"
@@ -43,7 +44,7 @@ func MountStubsCleaner(ctx context.Context, dir string, mounts []Mount, recursiv
4344 }
4445
4546 realPathNext := filepath .Dir (realPath )
46- if realPath == realPathNext {
47+ if realPath == realPathNext || realPathNext == dir {
4748 break
4849 }
4950 realPath = realPathNext
@@ -52,6 +53,11 @@ func MountStubsCleaner(ctx context.Context, dir string, mounts []Mount, recursiv
5253
5354 return func () {
5455 for _ , p := range paths {
56+ p , err := fs .RootPath (dir , strings .TrimPrefix (p , dir ))
57+ if err != nil {
58+ continue
59+ }
60+
5561 st , err := os .Lstat (p )
5662 if err != nil {
5763 continue
@@ -70,8 +76,12 @@ func MountStubsCleaner(ctx context.Context, dir string, mounts []Mount, recursiv
7076
7177 // Back up the timestamps of the dir for reproducible builds
7278 // https://github.com/moby/buildkit/issues/3148
73- dir := filepath .Dir (p )
74- dirSt , err := os .Stat (dir )
79+ parent := filepath .Dir (p )
80+ if realPath , err := fs .RootPath (dir , strings .TrimPrefix (parent , dir )); err != nil || realPath != parent {
81+ continue
82+ }
83+
84+ dirSt , err := os .Stat (parent )
7585 if err != nil {
7686 bklog .G (ctx ).WithError (err ).Warnf ("Failed to stat %q (parent of mount stub %q)" , dir , p )
7787 continue
@@ -88,7 +98,7 @@ func MountStubsCleaner(ctx context.Context, dir string, mounts []Mount, recursiv
8898 }
8999
90100 // Restore the timestamps of the dir
91- if err := os .Chtimes (dir , atime , mtime ); err != nil {
101+ if err := os .Chtimes (parent , atime , mtime ); err != nil {
92102 bklog .G (ctx ).WithError (err ).Warnf ("Failed to restore time time mount stub timestamp (os.Chtimes(%q, %v, %v))" , dir , atime , mtime )
93103 }
94104 }
0 commit comments