Skip to content

context: follow symlink roots during Walk - #319

Open
sueun-dev wants to merge 1 commit into
containerd:mainfrom
sueun-dev:fix-context-walk-symlink-root
Open

context: follow symlink roots during Walk#319
sueun-dev wants to merge 1 commit into
containerd:mainfrom
sueun-dev:fix-context-walk-symlink-root

Conversation

@sueun-dev

Copy link
Copy Markdown

This fixes the root-symlink case from #232.

Context.Walk currently replaces a symlink root with Readlink(root). That works only when the symlink target is absolute. For relative symlinks, Readlink returns the stored relative target, so filepath.Walk starts from the process working directory. For a symlink-to-symlink root it walks the second link instead of the target directory.

Keep walking through the context path and add a trailing separator when the root itself is a symlink. That makes filepath.Walk enter the referenced directory while keeping path containment relative to the context root.

The new test covers absolute symlink, relative symlink, and symlink-to-symlink roots.

Fixes #232.

Tests:

  • go test -run TestContextWalkSymlinkRoot -count=20 ./
  • go test -count=1 -mod=vendor ./...
  • go test -race -run TestContextWalkSymlinkRoot -count=10 ./
  • go test -race -count=1 -mod=vendor ./...
  • make build test
  • make lint
  • GOOS=linux make build
  • GOOS=windows make build
  • GOOS=freebsd make build
  • Linux Docker: go test -run TestContextWalkSymlinkRoot -count=20 . and go test -short -mod=vendor ./...
  • Linux Docker with Go 1.22: go test -run TestContextWalkSymlinkRoot -count=20 ., go test -short -mod=vendor ./..., and make build

Context.Walk currently replaces a symlink root with the raw os.Readlink result before calling filepath.Walk. That only works when the symlink target is absolute. For a relative symlink, filepath.Walk starts from the process working directory; for a symlink-to-symlink root, it walks the intermediate link instead of the referenced directory.

Use the context root path with a trailing separator when the root itself is a symlink, so filepath.Walk enters the target directory while the reported paths remain relative to the context root.

Add a regression test for absolute symlink, relative symlink, and symlink-to-symlink roots.

Signed-off-by: Sueun Cho <sueun.dev@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

continuity.context.Walk assumes incorrect behavior for os.Readlink

1 participant