From 5ac9f82db4fb78fc5fff9563c966890d73e982fd Mon Sep 17 00:00:00 2001 From: Nils Wireklint Date: Tue, 11 Jun 2024 10:06:36 +0200 Subject: [PATCH] Create NewLocalDirectory with path.Parser --- pkg/blobstore/configuration/BUILD.bazel | 1 + pkg/blobstore/configuration/new_blob_access.go | 3 ++- pkg/filesystem/local_directory_test.go | 4 ++-- pkg/filesystem/local_directory_unix.go | 13 +++++++++++-- pkg/filesystem/local_directory_windows.go | 12 ++++++++++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/pkg/blobstore/configuration/BUILD.bazel b/pkg/blobstore/configuration/BUILD.bazel index f76b2c03..ca4d7981 100644 --- a/pkg/blobstore/configuration/BUILD.bazel +++ b/pkg/blobstore/configuration/BUILD.bazel @@ -37,6 +37,7 @@ go_library( "//pkg/digest", "//pkg/eviction", "//pkg/filesystem", + "//pkg/filesystem/path", "//pkg/grpc", "//pkg/http", "//pkg/program", diff --git a/pkg/blobstore/configuration/new_blob_access.go b/pkg/blobstore/configuration/new_blob_access.go index 1e748901..6f175005 100644 --- a/pkg/blobstore/configuration/new_blob_access.go +++ b/pkg/blobstore/configuration/new_blob_access.go @@ -18,6 +18,7 @@ import ( "github.com/buildbarn/bb-storage/pkg/digest" "github.com/buildbarn/bb-storage/pkg/eviction" "github.com/buildbarn/bb-storage/pkg/filesystem" + "github.com/buildbarn/bb-storage/pkg/filesystem/path" "github.com/buildbarn/bb-storage/pkg/grpc" "github.com/buildbarn/bb-storage/pkg/program" pb "github.com/buildbarn/bb-storage/pkg/proto/configuration/blobstore" @@ -217,7 +218,7 @@ func (nc *simpleNestedBlobAccessCreator) newNestedBlobAccessBare(configuration * } else { // Persistency is enabled. Reload previous // persistent state from disk. - persistentStateDirectory, err := filesystem.NewLocalDirectory(persistent.StateDirectoryPath) + persistentStateDirectory, err := filesystem.NewLocalDirectory(path.NewLocalParser(persistent.StateDirectoryPath)) if err != nil { return BlobAccessInfo{}, "", util.StatusWrapf(err, "Failed to open persistent state directory %#v", persistent.StateDirectoryPath) } diff --git a/pkg/filesystem/local_directory_test.go b/pkg/filesystem/local_directory_test.go index e998fc5b..96508806 100644 --- a/pkg/filesystem/local_directory_test.go +++ b/pkg/filesystem/local_directory_test.go @@ -13,13 +13,13 @@ import ( ) func openTmpDir(t *testing.T) filesystem.DirectoryCloser { - d, err := filesystem.NewLocalDirectory(t.TempDir()) + d, err := filesystem.NewLocalDirectory(path.NewLocalParser(t.TempDir())) require.NoError(t, err) return d } func TestLocalDirectoryCreationFailure(t *testing.T) { - _, err := filesystem.NewLocalDirectory("/nonexistent") + _, err := filesystem.NewLocalDirectory(path.NewUNIXParser("/nonexistent")) require.True(t, os.IsNotExist(err)) } diff --git a/pkg/filesystem/local_directory_unix.go b/pkg/filesystem/local_directory_unix.go index 85d4436b..81e39278 100644 --- a/pkg/filesystem/local_directory_unix.go +++ b/pkg/filesystem/local_directory_unix.go @@ -33,8 +33,17 @@ func newLocalDirectoryFromFileDescriptor(fd int) (*localDirectory, error) { // NewLocalDirectory creates a directory handle that corresponds to a // local path on the system. -func NewLocalDirectory(path string) (DirectoryCloser, error) { - fd, err := unix.Openat(unix.AT_FDCWD, path, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_RDONLY, 0) +func NewLocalDirectory(directoryParser path.Parser) (DirectoryCloser, error) { + directoryPath, scopeWalker := path.EmptyBuilder.Join(path.VoidScopeWalker) + if err := path.Resolve(directoryParser, scopeWalker); err != nil { + return nil, util.StatusWrap(err, "Failed to resolve directory") + } + pathString, err := path.GetLocalString(directoryPath) + if err != nil { + return nil, util.StatusWrap(err, "Failed to create local representation of build directory") + } + + fd, err := unix.Openat(unix.AT_FDCWD, pathString, unix.O_DIRECTORY|unix.O_NOFOLLOW|unix.O_RDONLY, 0) if err != nil { return nil, err } diff --git a/pkg/filesystem/local_directory_windows.go b/pkg/filesystem/local_directory_windows.go index 05c486e6..a2233ad4 100644 --- a/pkg/filesystem/local_directory_windows.go +++ b/pkg/filesystem/local_directory_windows.go @@ -108,8 +108,16 @@ func newLocalDirectory(absPath string, openReparsePoint bool) (DirectoryCloser, return newLocalDirectoryFromHandle(handle) } -func NewLocalDirectory(path string) (DirectoryCloser, error) { - absPath := "\\??\\" + path +func NewLocalDirectory(directoryParser path.Parser) (DirectoryCloser, error) { + directoryPath, scopeWalker := path.EmptyBuilder.Join(path.VoidScopeWalker) + if err := path.Resolve(directoryParser, scopeWalker); err != nil { + return nil, util.StatusWrap(err, "Failed to resolve directory") + } + pathString, err := path.GetLocalString(directoryPath) + if err != nil { + return nil, util.StatusWrap(err, "Failed to create local representation of build directory") + } + absPath := "\\??\\" + pathString return newLocalDirectory(absPath, true) }