Skip to content

Commit

Permalink
include review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
micbar committed Oct 18, 2021
1 parent a300975 commit b09c230
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
45 changes: 24 additions & 21 deletions pkg/storage/utils/decomposedfs/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,10 @@ func (lu *Lookup) NodeFromID(ctx context.Context, id *provider.ResourceId) (n *n
if err != nil {
return nil, err
}
// we only need to look for the space root if this is not a space root itself
if n.SpaceRoot == nil {
err := lu.FindStorageSpaceRoot(n)
if err != nil {
return nil, err
}

err = lu.FindStorageSpaceRoot(n)
if err != nil {
return nil, err
}
return n, err
}
Expand Down Expand Up @@ -187,11 +185,8 @@ func (lu *Lookup) WalkPath(ctx context.Context, r *node.Node, p string, followRe
}
}
}

if spaceNameBytes, err := xattr.Get(r.InternalPath(), xattrs.SpaceNameAttr); err == nil {
if string(spaceNameBytes) != "" {
r.SpaceRoot = r
}
if isSpaceRoot(r) {
r.SpaceRoot = r
}

if !r.Exists && i < len(segments)-1 {
Expand All @@ -207,24 +202,32 @@ func (lu *Lookup) WalkPath(ctx context.Context, r *node.Node, p string, followRe
}

// FindStorageSpaceRoot calls n.Parent() and climbs the tree until it finds the space root node.
func (lu *Lookup) FindStorageSpaceRoot(r *node.Node) error {
func (lu *Lookup) FindStorageSpaceRoot(n *node.Node) error {
var err error
n := r
for i := 0; r.ParentID != ""; i++ {
if r, err = r.Parent(); err != nil {
// remember the node we ask for and use parent to climb the tree
parent := n
for parent.ParentID != "" {
if parent, err = parent.Parent(); err != nil {
return err
}
path := r.InternalPath()
if spaceNameBytes, err := xattr.Get(path, xattrs.SpaceNameAttr); err == nil {
if string(spaceNameBytes) != "" {
n.SpaceRoot = r
break
}
if isSpaceRoot(parent) {
n.SpaceRoot = parent
break
}
}
return nil
}

func isSpaceRoot(r *node.Node) bool {
path := r.InternalPath()
if spaceNameBytes, err := xattr.Get(path, xattrs.SpaceNameAttr); err == nil {
if string(spaceNameBytes) != "" {
return true
}
}
return false
}

// HomeOrRootNode returns the users home node when home support is enabled.
// it returns the storages root node otherwise
func (lu *Lookup) HomeOrRootNode(ctx context.Context) (node *node.Node, err error) {
Expand Down
5 changes: 1 addition & 4 deletions pkg/storage/utils/decomposedfs/testhelpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,7 @@ func NewTestEnv() (*TestEnv, error) {
if err != nil {
return nil, err
}
if err := xattr.Set(h.InternalPath(), xattrs.SpaceNameAttr, []byte("username")); err != nil {
return nil, err
}
if err != nil {
if err = xattr.Set(h.InternalPath(), xattrs.SpaceNameAttr, []byte("username")); err != nil {
return nil, err
}

Expand Down
13 changes: 5 additions & 8 deletions pkg/storage/utils/decomposedfs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,17 @@ func (fs *Decomposedfs) Upload(ctx context.Context, ref *provider.Reference, r i
// CheckQuota checks if both disk space and available quota are sufficient
var CheckQuota = func(ctx context.Context, fs *Decomposedfs, spaceRoot *node.Node, fileSize uint64) (quotaSufficient bool, err error) {
used, _ := spaceRoot.GetTreeSize()
enoughDiskSpace := enoughDiskSpace(fs, spaceRoot.InternalPath(), fileSize)
if !enoughDiskSpace {
if !enoughDiskSpace(fs, spaceRoot.InternalPath(), fileSize) {
return false, errtypes.InsufficientStorage("disk full")
}
quotaB, _ := xattr.Get(spaceRoot.InternalPath(), xattrs.QuotaAttr)
quotaByte, _ := xattr.Get(spaceRoot.InternalPath(), xattrs.QuotaAttr)
var total uint64
if quotaB != nil {
total, _ = strconv.ParseUint(string(quotaB), 10, 64)
} else {
if quotaByte == nil {
// if quota is not set, it means unlimited
return true, nil
}

if fileSize > total-used || total < used {
total, _ = strconv.ParseUint(string(quotaByte), 10, 64)
if fileSize > total-used {
return false, errtypes.InsufficientStorage("quota exceeded")
}
return true, nil
Expand Down

0 comments on commit b09c230

Please sign in to comment.