Skip to content

Commit

Permalink
Resolve one layer of symlinks in directory impl
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jul 7, 2024
1 parent 6e37175 commit 9345a46
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
40 changes: 39 additions & 1 deletion go/runfiles/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package runfiles

import (
"errors"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -43,5 +44,42 @@ func (d Directory) path(s string) (string, error) {
}

func (d Directory) open(name string) (fs.File, error) {
return os.DirFS(string(d)).Open(name)
f, err := os.DirFS(string(d)).Open(name)
if err != nil {
return nil, err
}
return &resolvedFile{f.(*os.File), func(child string) (fs.FileInfo, error) {
path := filepath.Join(string(d), filepath.FromSlash(name), child)
target, err := os.Readlink(path)
if err != nil {
if errors.Is(err, os.ErrInvalid) {
target = path
} else {
return nil, err
}
}
return os.Lstat(target)
}}, nil
}

type resolvedFile struct {
fs.ReadDirFile
lstatChildAfterReadlink func(string) (fs.FileInfo, error)
}

func (f *resolvedFile) ReadDir(n int) ([]fs.DirEntry, error) {
entries, err := f.ReadDirFile.ReadDir(n)
if err != nil {
return nil, err
}
for i, entry := range entries {
if entry.Type()&fs.ModeSymlink != 0 {
info, err := f.lstatChildAfterReadlink(entry.Name())
if err != nil {
return nil, err
}
entries[i] = renamedDirEntry{fs.FileInfoToDirEntry(info), entry.Name()}
}
}
return entries, nil
}
49 changes: 49 additions & 0 deletions tests/runfiles/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"

Expand Down Expand Up @@ -165,6 +166,9 @@ func testFS(t *testing.T, r *runfiles.Runfiles) {
testFile(t, r, "link_test.txt", "hi!\n")
testFile(t, r, "link_test_dir/file.txt", "file\n")
testFile(t, r, "link_test_dir/subdir/other_file.txt", "other_file\n")

testGlob(t, r)
testWalkDir(t, r)
}

func testFile(t *testing.T, r *runfiles.Runfiles, name, content string) {
Expand Down Expand Up @@ -198,6 +202,51 @@ func testFile(t *testing.T, r *runfiles.Runfiles, name, content string) {
}
}

func testGlob(t *testing.T, r *runfiles.Runfiles) {
matches, err := fs.Glob(r, "*/subdir/*.txt")
if err != nil {
t.Fatal(err)
}

expected := []string{
"link_test_dir/subdir/other_file.txt",
}
if !slices.Equal(matches, expected) {
t.Errorf("got %v, want %v", matches, expected)
}
}

func testWalkDir(t *testing.T, r *runfiles.Runfiles) {
var found []string
err := fs.WalkDir(r, "io_bazel_rules_go/tests/runfiles", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
found = append(found, path)
}
return nil
})
if err != nil {
t.Fatal(err)
}

exeSuffix := ""
if runtime.GOOS == "windows" {
exeSuffix = ".exe"
}
expected := []string{
"io_bazel_rules_go/tests/runfiles/runfiles_test_/runfiles_test" + exeSuffix,
"io_bazel_rules_go/tests/runfiles/test.txt",
"io_bazel_rules_go/tests/runfiles/test_dir/file.txt",
"io_bazel_rules_go/tests/runfiles/test_dir/subdir/other_file.txt",
"io_bazel_rules_go/tests/runfiles/testprog/testprog_/testprog" + exeSuffix,
}
if !slices.Equal(found, expected) {
t.Errorf("got %v, want %v", found, expected)
}
}

func TestFS_empty(t *testing.T) {
dir := t.TempDir()
manifest := filepath.Join(dir, "manifest")
Expand Down

0 comments on commit 9345a46

Please sign in to comment.