Skip to content

Commit

Permalink
Fix creating new nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed Mar 18, 2024
1 parent 34d9937 commit d1a0f7a
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 26 deletions.
15 changes: 0 additions & 15 deletions pkg/storage/utils/decomposedfs/lookup/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ package lookup
import (
"context"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
"syscall"

user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
Expand Down Expand Up @@ -99,22 +97,9 @@ func readChildNodeFromLink(path string) (string, error) {
return nodeID, nil
}

// The os error is buried inside the fs.PathError error
func isNotDir(err error) bool {
if perr, ok := err.(*fs.PathError); ok {
if serr, ok2 := perr.Err.(syscall.Errno); ok2 {
return serr == syscall.ENOTDIR
}
}
return false
}

func (lu *Lookup) NodeIDFromParentAndName(ctx context.Context, parent *node.Node, name string) (string, error) {
nodeID, err := readChildNodeFromLink(filepath.Join(parent.InternalPath(), name))
if err != nil {
if errors.Is(err, fs.ErrNotExist) || isNotDir(err) {
return nodeID, nil // if the file does not exist we return a node that has Exists = false
}
return "", errors.Wrap(err, "decomposedfs: Wrap: readlink error")
}
return nodeID, nil
Expand Down
11 changes: 11 additions & 0 deletions pkg/storage/utils/decomposedfs/metadata/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package metadata

import (
"io/fs"
"os"
"syscall"

Expand Down Expand Up @@ -50,3 +51,13 @@ func IsAttrUnset(err error) bool {
}
return false
}

// The os error is buried inside the fs.PathError error
func IsNotDir(err error) bool {
if perr, ok := errors.Cause(err).(*fs.PathError); ok {
if serr, ok2 := perr.Err.(syscall.Errno); ok2 {
return serr == syscall.ENOTDIR
}
}
return false
}
8 changes: 3 additions & 5 deletions pkg/storage/utils/decomposedfs/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,10 @@ func (n *Node) Child(ctx context.Context, name string) (*Node, error) {
}

nodeID, err := n.lu.NodeIDFromParentAndName(ctx, n, name)
switch err.(type) {
case nil:
// ok
case errtypes.IsNotFound:
switch {
case metadata.IsNotExist(err) || metadata.IsNotDir(err):
return c, nil // if the file does not exist we return a node that has Exists = false
default:
case err != nil:
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/utils/decomposedfs/node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ var _ = Describe("Node", func() {
Expect(child.Blobsize).To(Equal(int64(1234)))
})

It("handles (broken) links including file segments by returning an non-existent node", func() {
It("handles broken links including file segments by returning an non-existent node", func() {
child, err := parent.Child(env.Ctx, "file1/broken")
Expect(err).ToNot(HaveOccurred())
Expect(child).ToNot(BeNil())
Expand Down
2 changes: 1 addition & 1 deletion pkg/storage/utils/decomposedfs/options/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var _ = Describe("Options", func() {
})

It("sets defaults", func() {
Expect(len(o.UserLayout) > 0).To(BeTrue())
Expect(o.MetadataBackend).ToNot(BeEmpty())
})

Context("with unclean root path configuration", func() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/utils/decomposedfs/upload/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ type OcisStore struct {
}

// NewSessionStore returns a new OcisStore
func NewSessionStore(lu node.PathLookup, tp Tree, root string, pub events.Publisher, async bool, tknopts options.TokenOptions, DisableVersioning bool) *OcisStore {
func NewSessionStore(lu node.PathLookup, tp Tree, root string, pub events.Publisher, async bool, tknopts options.TokenOptions, disableVersioning bool) *OcisStore {
return &OcisStore{
lu: lu,
tp: tp,
root: root,
pub: pub,
async: async,
tknopts: tknopts,
disableVersioning: DisableVersioning,
disableVersioning: disableVersioning,
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/storage/utils/decomposedfs/upload/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func TestInitNewNode(t *testing.T) {
if err != nil {
t.Fatalf(err.Error())
}
defer unlock()
defer func() {
_ = unlock()
}()

// try initializing the same new node again in case a concurrent requests tries to create a file with the same name
n = node.New("e48c4e7a-beac-4b82-b991-a5cff7b8c39c", "a6ede986-cfcd-41c5-a820-6eee955a1c2b", "e48c4e7a-beac-4b82-b991-a5cff7b8c39c", "newchild", 10, "26493c53-2634-45f8-949f-dc07b88df9b0", providerv1beta1.ResourceType_RESOURCE_TYPE_FILE, &userv1beta1.UserId{}, lookup)
Expand All @@ -47,5 +49,5 @@ func TestInitNewNode(t *testing.T) {
if _, ok := err.(errtypes.IsAlreadyExists); !ok {
t.Fatalf(`initNewNode(with same 'newchild' name), %v, want %v`, err, errtypes.AlreadyExists("newchild"))
}
unlock2()
_ = unlock2()
}

0 comments on commit d1a0f7a

Please sign in to comment.