Skip to content

Commit

Permalink
windows aware paths
Browse files Browse the repository at this point in the history
  • Loading branch information
itaiad200 committed Dec 3, 2020
1 parent e41a8a8 commit 0f4617f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
9 changes: 7 additions & 2 deletions pyramid/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ func (f *File) Close() error {
return f.fh.Close()
}

var (
errAlreadyPersisted = fmt.Errorf("file is already persisted")
errFileNotClosed = fmt.Errorf("file isn't closed")
)

// Store copies the closed file to all tiers of the pyramid.
func (f *File) Store(filename string) error {
if err := validateFilename(filename); err != nil {
return err
}

if f.persisted {
return fmt.Errorf("file is already persisted")
return errAlreadyPersisted
}
if !f.closed {
return fmt.Errorf("file isn't closed")
return errFileNotClosed
}

err := f.store(filename)
Expand Down
2 changes: 1 addition & 1 deletion pyramid/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestWriteValidate(t *testing.T) {
require.NoError(t, err)

require.NoError(t, sut.Close())
require.Error(t, sut.Store("workspace/"))
require.Error(t, sut.Store("workspace"+string(os.PathSeparator)))
require.Error(t, sut.Close())
require.NoError(t, sut.Store("validfilename"))
require.Error(t, sut.Store("validfilename"))
Expand Down
20 changes: 14 additions & 6 deletions pyramid/tierFS.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"runtime"
"strings"

"github.com/treeverse/lakefs/logging"
Expand Down Expand Up @@ -74,7 +75,7 @@ func NewFS(c *Config) (FS, error) {
return nil, fmt.Errorf("creating eviction control: %w", err)
}

if err := handleExistingFiles(c.logger, eviction, fsLocalBaseDir); err != nil {
if err := handleExistingFiles(eviction, fsLocalBaseDir); err != nil {
return nil, fmt.Errorf("handling existing files: %w", err)
}

Expand All @@ -87,7 +88,7 @@ func NewFS(c *Config) (FS, error) {
// 1. Adds stored files to the eviction control
// 2. Remove workspace directories and all its content if it
// exist under the namespace dir.
func handleExistingFiles(logger logging.Logger, eviction eviction, fsLocalBaseDir string) error {
func handleExistingFiles(eviction eviction, fsLocalBaseDir string) error {
if err := filepath.Walk(fsLocalBaseDir, func(rPath string, info os.FileInfo, err error) error {
if err != nil {
return err
Expand Down Expand Up @@ -258,15 +259,18 @@ func validateArgs(namespace, filename string) error {
return validateFilename(filename)
}

var (
errSeparatorInFS = errors.New("path contains separator")
errPathInWorkspace = errors.New("file cannot be located in the workspace")
)

func validateFilename(filename string) error {
if strings.HasPrefix(filename, workspaceDir+"/") {
return fmt.Errorf("file %s cannot be located in the workspace", filename)
if strings.HasPrefix(filename, workspaceDir+string(os.PathSeparator)) {
return errPathInWorkspace
}
return nil
}

var errSeparatorInFS = errors.New("path contains separator")

func validateNamespace(ns string) error {
if strings.ContainsRune(ns, os.PathSeparator) {
return errSeparatorInFS
Expand Down Expand Up @@ -298,6 +302,10 @@ func (tfs *TierFS) newLocalFileRef(namespace, filename string) localFileRef {
}

func (tfs *TierFS) objPointer(namespace, filename string) block.ObjectPointer {
if runtime.GOOS == "windows" {
filename = strings.Replace(filename, `\\'`, "/", -1)
}

return block.ObjectPointer{
StorageNamespace: namespace,
Identifier: tfs.blockStoragePath(filename),
Expand Down

0 comments on commit 0f4617f

Please sign in to comment.