Skip to content

Commit

Permalink
Merge pull request #93 from infosiftr/gitfs-symlinks
Browse files Browse the repository at this point in the history
Fix gitfs symlink handling
  • Loading branch information
yosifkit authored Feb 26, 2024
2 parents ca06476 + 722f7d6 commit d23b943
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
21 changes: 14 additions & 7 deletions pkg/gitfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ func CommitHash(repo *goGit.Repository, commit string) (fs.FS, error) {
if err != nil {
return nil, err
}
f := &gitFS{
storer: repo.Storer,
tree: tree,
name: ".",
Mod: CommitTime(gitCommit),
}
f.root = f
return gitFSFS{
gitFS: &gitFS{
storer: repo.Storer,
tree: tree,
name: ".",
Mod: CommitTime(gitCommit),
},
gitFS: f,
}, nil
}

Expand All @@ -54,6 +56,8 @@ type gitFSFS struct {
// https://pkg.go.dev/io/fs#FileInfo
// https://pkg.go.dev/io/fs#DirEntry
type gitFS struct {
root *gitFS // used so we can rewind back to the root if we need to (see symlink handling code; should *only* be set in CommitHash / constructors)

storer goGitPlumbingStorer.EncodedObjectStorer
tree *goGitPlumbingObject.Tree
entry *goGitPlumbingObject.TreeEntry // might be nil ("." at the top-level of the repo)
Expand Down Expand Up @@ -172,7 +176,10 @@ func (f gitFS) statEntry(name string, entry *goGitPlumbingObject.TreeEntry, foll
if target, err := fi.resolveLink(); err != nil {
return nil, err
} else if target != "" {
return f.stat(target, followSymlinks)
// the value from resolveLink is relative to the root
return f.root.stat(target, followSymlinks)
// ideally this would "just" use "path.Rel" to make "target" relative to "f.name" instead, but "path.Rel" does not exist and only "filepath.Rel" does which would break this code on Windows, so instead we added a "root" pointer that we pass around forever that links us back to the root of our "Tree"
// we could technically solve this by judicious use of "../" (with enough "../" to catch all the "/" in "f.name"), but it seems simpler and more obvious (and less error prone) to just pass around a pointer to the root
}
}

Expand Down
80 changes: 79 additions & 1 deletion pkg/gitfs/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker-library/bashbrew/pkg/gitfs"

"github.com/go-git/go-git/v5"
goGitConfig "github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/storage/memory"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func TestCommitFS(t *testing.T) {
})
}

func TestSymlinkFS(t *testing.T) {
func TestRootSymlinkFS(t *testing.T) {
// TODO instead of cloning a remote repository, synthesize a very simple Git repository right in the test here (benefit of the remote repository is that it's much larger, so fstest.TestFS has a lot more data to test against)
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/tianon/gosu.git", // just a repository with a known symlink (`.dockerignore` -> `.gitignore`)
Expand Down Expand Up @@ -93,3 +94,80 @@ func TestSymlinkFS(t *testing.T) {
}
})
}

func TestSubdirSymlinkFS(t *testing.T) {
// TODO instead of cloning a remote repository, synthesize a very simple Git repository right in the test here (benefit of the remote repository is that it's much larger, so fstest.TestFS has a lot more data to test against)
// Init + CreateRemoteAnonymous + Fetch because Clone doesn't support fetch-by-commit
repo, err := git.Init(memory.NewStorage(), nil)
if err != nil {
t.Fatal(err)
}
remote, err := repo.CreateRemoteAnonymous(&goGitConfig.RemoteConfig{
Name: "anonymous",
URLs: []string{"https://github.com/docker-library/busybox.git"}, // just a repository with a known symlink at a non-root level (`latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318` -> `../../image-config.json`)
})
if err != nil {
t.Fatal(err)
}
commit := "668d52e6f0596e0fd0b1be1d8267c4b9240dc2b3"
err = remote.Fetch(&git.FetchOptions{
RefSpecs: []goGitConfig.RefSpec{goGitConfig.RefSpec(commit + ":FETCH_HEAD")},
Tags: git.NoTags,
})
if err != nil {
t.Fatal(err)
}
f, err := gitfs.CommitHash(repo, commit)
if err != nil {
t.Fatal(err)
}

t.Run("Open+ReadAll", func(t *testing.T) {
r, err := f.Open("latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318")
if err != nil {
t.Fatal(err)
}
defer func() {
if err := r.Close(); err != nil {
t.Fatal(err)
}
}()
b, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}
expected := `{
"config": {
"Cmd": [
"sh"
]
},
"created": "2023-05-18T22:34:17Z",
"history": [
{
"created": "2023-05-18T22:34:17Z",
"created_by": "BusyBox 1.36.1 (musl), Alpine 3.19.1"
}
],
"rootfs": {
"type": "layers",
"diff_ids": [
"sha256:994bf8f4adc78c5c1e4a6b5e3b59ad57902b301e0e79255a3e95ea4b213a76bd"
]
},
"architecture": "amd64",
"os": "linux"
}
`
if string(b) != expected {
t.Fatalf("expected %q, got %q", expected, string(b))
}
})

// might as well run fstest again, now that we have a new filesystem tree 😅
t.Run("fstest.TestFS", func(t *testing.T) {
if err := fstest.TestFS(f, "latest/musl/amd64/blobs/sha256/6e5e0f90c009d12db9478afe5656920e7bdd548e9fd8f50eab2be694102ae318", "latest/musl/amd64/index.json"); err != nil {
t.Fatal(err)
}
})
}

0 comments on commit d23b943

Please sign in to comment.