Skip to content

Commit

Permalink
Fix path handling in storagegit (#2282)
Browse files Browse the repository at this point in the history
  • Loading branch information
bufdev committed Jul 12, 2023
1 parent 2ce1126 commit 1986493
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
16 changes: 12 additions & 4 deletions private/pkg/storage/storagegit/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func newBucket(
}

func (b *bucket) Get(ctx context.Context, path string) (storage.ReadObjectCloser, error) {
path, err := storageutil.ValidatePath(path)
if err != nil {
return nil, err
}
node, err := b.root.Descendant(path, b.objectReader)
if err != nil {
if errors.Is(err, git.ErrTreeNodeNotFound) {
Expand Down Expand Up @@ -76,7 +80,7 @@ func (b *bucket) Get(ctx context.Context, path string) (storage.ReadObjectCloser
path, err := normalpath.NormalizeAndValidate(
normalpath.Join(
normalpath.Base(path),
normalpath.Normalize(string(data)),
string(data),
),
)
if err != nil {
Expand Down Expand Up @@ -110,6 +114,10 @@ func (b *bucket) Stat(ctx context.Context, path string) (storage.ObjectInfo, err
}

func (b *bucket) Walk(ctx context.Context, prefix string, f func(storage.ObjectInfo) error) error {
prefix, err := storageutil.ValidatePrefix(prefix)
if err != nil {
return err
}
walkChecker := storageutil.NewWalkChecker()
return b.walk(b.root, b.objectReader, prefix, func(path string) error {
if err := walkChecker.Check(ctx); err != nil {
Expand All @@ -125,7 +133,6 @@ func (b *bucket) walk(
prefix string,
walkFn func(string) error,
) error {
prefix = normalpath.Normalize(prefix)
if prefix != "." {
node, err := parent.Descendant(prefix, b.objectReader)
if err != nil {
Expand Down Expand Up @@ -180,10 +187,11 @@ func (b *bucket) walkTree(
return nil
}

// path is expected to be normalized by calling functions
func (b *bucket) newObjectInfo(path string) storage.ObjectInfo {
return storageutil.NewObjectInfo(
normalpath.Normalize(path),
normalpath.Unnormalize(path),
path,
path,
)
}

Expand Down
3 changes: 1 addition & 2 deletions private/pkg/storage/storagegit/storagegit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (

"github.com/bufbuild/buf/private/pkg/git"
"github.com/bufbuild/buf/private/pkg/git/gittest"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/storage/storagetesting"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func TestNewBucketAtTreeHash(t *testing.T) {
t,
bucket,
"proto/acme/grocerystore/v1/c.proto",
normalpath.Unnormalize("proto/acme/grocerystore/v1/c.proto"),
"proto/acme/grocerystore/v1/c.proto",
)
storagetesting.AssertNotExist(t, bucket, "random-path")
storagetesting.AssertPathToContent(
Expand Down

0 comments on commit 1986493

Please sign in to comment.