Skip to content

Commit

Permalink
do not merge: debug rootPath
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksa Sarai <[email protected]>
  • Loading branch information
cyphar committed Jun 5, 2024
1 parent af5a749 commit 960520f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
14 changes: 14 additions & 0 deletions cache/contenthash/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package contenthash

import (
"fmt"
"os"
"path/filepath"
"strings"
Expand All @@ -19,6 +20,10 @@ var errTooManyLinks = errors.New("too many links")

const maxSymlinkLimit = 255

var logFn = func(format string, v ...interface{}) {
fmt.Fprintf(os.Stderr, format, v...)
}

type onSymlinkFunc func(string, string) error

// rootPath joins a path with a root, evaluating and bounding any symlink to
Expand All @@ -35,9 +40,13 @@ func rootPath(root, unsafePath string, followTrailing bool, cb onSymlinkFunc) (s
currentPath string
linksWalked int
)
logFn("rootPath(%s, %s, %v)\n", root, unsafePath, followTrailing)
for unsafePath != "" {
logFn("-- loop --\ncurrentPath: %s\nunsafePath: %s\n", currentPath, unsafePath)

// Windows-specific: remove any drive letters from the path.
if v := filepath.VolumeName(unsafePath); v != "" {
logFn("unsafePath %s -> [%s] %s\n", unsafePath, v, unsafePath[len(v):])
unsafePath = unsafePath[len(v):]
}

Expand All @@ -51,11 +60,13 @@ func rootPath(root, unsafePath string, followTrailing bool, cb onSymlinkFunc) (s
} else {
part, unsafePath = unsafePath[:i], unsafePath[i+1:]
}
logFn("unsafePath -> (%s, %s)\n", part, unsafePath)

// Apply the component lexically to the path we are building. path does
// not contain any symlinks, and we are lexically dealing with a single
// component, so it's okay to do filepath.Clean here.
nextPath := filepath.Join(string(filepath.Separator), currentPath, part)
logFn("nextPath <- %s\n", nextPath)
if nextPath == string(filepath.Separator) {
// If we end up back at the root, we don't need to re-evaluate /.
currentPath = ""
Expand All @@ -65,6 +76,7 @@ func rootPath(root, unsafePath string, followTrailing bool, cb onSymlinkFunc) (s

// Figure out whether the path is a symlink.
fi, err := os.Lstat(fullPath)
logFn("Lstat(%s) -> %v, %v\n", fullPath, fi, err)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return "", err
}
Expand All @@ -88,6 +100,7 @@ func rootPath(root, unsafePath string, followTrailing bool, cb onSymlinkFunc) (s
}

dest, err := os.Readlink(fullPath)
logFn("Readlink(%s) -> %v, %v\n", fullPath, dest, err)
if err != nil {
return "", err
}
Expand All @@ -97,6 +110,7 @@ func rootPath(root, unsafePath string, followTrailing bool, cb onSymlinkFunc) (s
}
}

logFn("unsafePath <- %s + %s\n", dest, unsafePath)
unsafePath = dest + string(filepath.Separator) + unsafePath
// Absolute symlinks reset any work we've already done.
if filepath.IsAbs(dest) {
Expand Down
7 changes: 4 additions & 3 deletions cache/contenthash/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func TestRootPathSymlinks(t *testing.T) {
t.Parallel()
tmpdir := t.TempDir()

// Make the following tree:
Expand Down Expand Up @@ -106,12 +105,14 @@ func TestRootPathSymlinks(t *testing.T) {
} {
test := test // capture range variable
t.Run(fmt.Sprintf("resolve(%q,followTrailing=%v)", test.path, test.followTrailing), func(t *testing.T) {
t.Parallel()
logFn = func(fmt string, v ...interface{}) {
t.Logf(fmt, v...)

Check failure on line 109 in cache/contenthash/path_test.go

View workflow job for this annotation

GitHub Actions / test-windows-amd64 (./..., 1)

Failed: cache/contenthash/TestRootPathSymlinks/resolve("link2/target_abs",followTrailing=true)

=== RUN TestRootPathSymlinks/resolve("link2/target_abs",followTrailing=true) path_test.go:109: rootPath(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001, link2\target_abs, true) path_test.go:109: -- loop -- currentPath: unsafePath: link2\target_abs path_test.go:109: unsafePath -> (link2, target_abs) path_test.go:109: nextPath <- \link2 path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2) -> &{link2 16 {845822210 31110890} {847720643 31110890} {847720643 31110890} 0 4096 0 0 {0 0} C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2 0 0 0 false}, <nil> path_test.go:109: -- loop -- currentPath: \link2 unsafePath: target_abs path_test.go:109: unsafePath -> (target_abs, ) path_test.go:109: nextPath <- \link2\target_abs path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\target_abs) -> &{target_abs 1056 {847706545 31110890} {847706545 31110890} {847706545 31110890} 0 0 2684354572 1 {0 0} 3902560915 196608 880843 false}, <nil> path_test.go:109: Readlink(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\target_abs) -> Z:\link2\final, <nil> path_test.go:109: unsafePath <- Z:\link2\final + path_test.go:109: -- loop -- currentPath: unsafePath: Z:\link2\final\ path_test.go:109: unsafePath Z:\link2\final\ -> [Z:] \link2\final\ path_test.go:109: unsafePath -> (, link2\final) path_test.go:109: nextPath <- \ path_test.go:109: -- loop -- currentPath: unsafePath: link2\final path_test.go:109: unsafePath -> (link2, final) path_test.go:109: nextPath <- \link2 path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2) -> &{link2 16 {845822210 31110890} {847720643 31110890} {847720643 31110890} 0 4096 0 0 {0 0} C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2 0 0 0 false}, <nil> path_test.go:109: -- loop -- currentPath: \link2 unsafePath: final path_test.go:109: unsafePath -> (final, ) path_test.go:109: nextPath <- \link2\final path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\final) -> <nil>, CreateFile C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\final: The system cannot find the file specified. path_test.go:116: Error Trace: D:/a/buildkit/buildkit/cache/contenthash/path_test.go:116 Error: Not equal: expected: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestRootPathSymlinks4233926366\\001\\target" actual : "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestRootPathSymlinks4233926366\\001\\link2\\final" Diff: --- Expected +++ Actual @@ -1 +1 @@ -C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\target +C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\link2\final Test: TestRootPathSymlinks/resolve("link2/target_abs",followTrailing=true) --- FAIL: TestRootPathSymlinks/resolve("link2/target_abs",followTrailing=true) (0.00s)

Check failure on line 109 in cache/contenthash/path_test.go

View workflow job for this annotation

GitHub Actions / test-windows-amd64 (./..., 1)

Failed: cache/contenthash/TestRootPathSymlinks/resolve("link3/target_abs",followTrailing=true)

=== RUN TestRootPathSymlinks/resolve("link3/target_abs",followTrailing=true) path_test.go:109: rootPath(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001, link3\target_abs, true) path_test.go:109: -- loop -- currentPath: unsafePath: link3\target_abs path_test.go:109: unsafePath -> (link3, target_abs) path_test.go:109: nextPath <- \link3 path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link3) -> &{link3 1056 {847720643 31110890} {847720643 31110890} {847720643 31110890} 0 0 2684354572 1 {0 0} 3902560915 131072 880869 false}, <nil> path_test.go:109: Readlink(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link3) -> Z:\link2, <nil> path_test.go:109: unsafePath <- Z:\link2 + target_abs path_test.go:109: -- loop -- currentPath: unsafePath: Z:\link2\target_abs path_test.go:109: unsafePath Z:\link2\target_abs -> [Z:] \link2\target_abs path_test.go:109: unsafePath -> (, link2\target_abs) path_test.go:109: nextPath <- \ path_test.go:109: -- loop -- currentPath: unsafePath: link2\target_abs path_test.go:109: unsafePath -> (link2, target_abs) path_test.go:109: nextPath <- \link2 path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2) -> &{link2 16 {845822210 31110890} {847720643 31110890} {847720643 31110890} 0 4096 0 0 {0 0} C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2 0 0 0 false}, <nil> path_test.go:109: -- loop -- currentPath: \link2 unsafePath: target_abs path_test.go:109: unsafePath -> (target_abs, ) path_test.go:109: nextPath <- \link2\target_abs path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\target_abs) -> &{target_abs 1056 {847706545 31110890} {847706545 31110890} {847706545 31110890} 0 0 2684354572 1 {0 0} 3902560915 196608 880843 false}, <nil> path_test.go:109: Readlink(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\target_abs) -> Z:\link2\final, <nil> path_test.go:109: unsafePath <- Z:\link2\final + path_test.go:109: -- loop -- currentPath: unsafePath: Z:\link2\final\ path_test.go:109: unsafePath Z:\link2\final\ -> [Z:] \link2\final\ path_test.go:109: unsafePath -> (, link2\final) path_test.go:109: nextPath <- \ path_test.go:109: -- loop -- currentPath: unsafePath: link2\final path_test.go:109: unsafePath -> (link2, final) path_test.go:109: nextPath <- \link2 path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2) -> &{link2 16 {845822210 31110890} {847720643 31110890} {847720643 31110890} 0 4096 0 0 {0 0} C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2 0 0 0 false}, <nil> path_test.go:109: -- loop -- currentPath: \link2 unsafePath: final path_test.go:109: unsafePath -> (final, ) path_test.go:109: nextPath <- \link2\final path_test.go:109: Lstat(C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\final) -> <nil>, CreateFile C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\\link2\final: The system cannot find the file specified. path_test.go:116: Error Trace: D:/a/buildkit/buildkit/cache/contenthash/path_test.go:116 Error: Not equal: expected: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestRootPathSymlinks4233926366\\001\\target" actual : "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestRootPathSymlinks4233926366\\001\\link2\\final" Diff: --- Expected +++ Actual @@ -1 +1 @@ -C:\Users\RUNNER~1\AppData\Local\Temp\TestRootPathSymlinks4233926366\001\target +C:\Use
}

resolvedPath, err := rootPath(tmpdir, test.path, test.followTrailing, nil)
require.NoError(t, err)

expectedPath := filepath.Join(tmpdir, test.expected)
expectedPath := filepath.Join(tmpdir, filepath.FromSlash(test.expected))
require.Equal(t, expectedPath, resolvedPath)
})
}
Expand Down

0 comments on commit 960520f

Please sign in to comment.