From b60a04d98d95a5f114c477d2028a34d9b0fd4e8e 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 | 7 ++++++- pkg/filesystem/local_directory_test.go | 8 ++++++-- pkg/filesystem/local_directory_unix.go | 13 +++++++++++-- pkg/filesystem/local_directory_windows.go | 12 ++++++++++-- 5 files changed, 34 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..9065e039 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,11 @@ func (nc *simpleNestedBlobAccessCreator) newNestedBlobAccessBare(configuration * } else { // Persistency is enabled. Reload previous // persistent state from disk. - persistentStateDirectory, err := filesystem.NewLocalDirectory(persistent.StateDirectoryPath) + parser, err := path.NewLocalParser(persistent.StateDirectoryPath) + if err != nil { + return BlobAccessInfo{}, "", util.StatusWrapf(err, "Failed to parse persistent state directory path %#v", persistent.StateDirectoryPath) + } + persistentStateDirectory, err := filesystem.NewLocalDirectory(parser) 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 c50bd660..8e256605 100644 --- a/pkg/filesystem/local_directory_test.go +++ b/pkg/filesystem/local_directory_test.go @@ -13,13 +13,17 @@ import ( ) func openTmpDir(t *testing.T) filesystem.DirectoryCloser { - d, err := filesystem.NewLocalDirectory(t.TempDir()) + parser, err := path.NewLocalParser(t.TempDir()) + require.NoError(t, err) + d, err := filesystem.NewLocalDirectory(parser) require.NoError(t, err) return d } func TestLocalDirectoryCreationFailure(t *testing.T) { - _, err := filesystem.NewLocalDirectory("/nonexistent") + parser, err := path.NewUNIXParser("/nonexistent") + require.NoError(t, err) + _, err = filesystem.NewLocalDirectory(parser) require.True(t, os.IsNotExist(err)) } diff --git a/pkg/filesystem/local_directory_unix.go b/pkg/filesystem/local_directory_unix.go index 1d261ead..e499b83f 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 bee777ac..72f25404 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) }