Skip to content

Commit

Permalink
make linter even more happy
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
  • Loading branch information
butonic committed Oct 2, 2020
1 parent c5e3ebf commit 4419c28
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 50 deletions.
12 changes: 9 additions & 3 deletions pkg/storage/fs/ocis/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,16 @@ func ReadNode(ctx context.Context, lu *Lookup, id string) (n *Node, err error) {

// lookup parent id in extended attributes
var attrBytes []byte
if attrBytes, err = xattr.Get(nodePath, parentidAttr); err == nil {
attrBytes, err = xattr.Get(nodePath, parentidAttr)
switch {
case err == nil:
n.ParentID = string(attrBytes)
} else {
return
case isNoData(err):
return nil, errtypes.InternalError(err.Error())
case isNotFound(err):
return nil, errtypes.NotFound(n.ID)
default:
return nil, errtypes.InternalError(err.Error())
}
// lookup name in extended attributes
if attrBytes, err = xattr.Get(nodePath, nameAttr); err == nil {
Expand Down
19 changes: 11 additions & 8 deletions pkg/storage/fs/ocis/ocis.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,33 +224,36 @@ func (fs *ocisfs) GetPathByID(ctx context.Context, id *provider.ResourceId) (str
}

func (fs *ocisfs) CreateDir(ctx context.Context, fn string) (err error) {
var node *Node
if node, err = fs.lu.NodeFromPath(ctx, fn); err != nil {
var n *Node
if n, err = fs.lu.NodeFromPath(ctx, fn); err != nil {
return
}

if node.Exists {
if n.Exists {
return errtypes.AlreadyExists(fn)
}

pn, err := node.Parent()
pn, err := n.Parent()
if err != nil {
return errors.Wrap(err, "ocisfs: error getting parent "+n.ParentID)
}
ok, err := fs.p.HasPermission(ctx, pn, func(rp *provider.ResourcePermissions) bool {
return rp.CreateContainer
})
switch {
case err != nil:
return errtypes.InternalError(err.Error())
case !ok:
return errtypes.PermissionDenied(filepath.Join(node.ParentID, node.Name))
return errtypes.PermissionDenied(filepath.Join(n.ParentID, n.Name))
}

err = fs.tp.CreateDir(ctx, node)
err = fs.tp.CreateDir(ctx, n)

if fs.o.TreeTimeAccounting {
nodePath := node.lu.toInternalPath(node.ID)
nodePath := n.lu.toInternalPath(n.ID)
// mark the home node as the end of propagation
if err = xattr.Set(nodePath, propagationAttr, []byte("1")); err != nil {
appctx.GetLogger(ctx).Error().Err(err).Interface("node", node).Msg("could not mark node to propagate")
appctx.GetLogger(ctx).Error().Err(err).Interface("node", n).Msg("could not mark node to propagate")
return
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/fs/ocis/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Options struct {
}

// newOptions initializes the available default options.
/* for future use, commented to make linter happy
func newOptions(opts ...Option) Options {
opt := Options{}
Expand All @@ -52,6 +53,7 @@ func newOptions(opts ...Option) Options {
return opt
}
*/

// Root provides a function to set the root option.
func Root(val string) Option {
Expand Down
15 changes: 2 additions & 13 deletions pkg/storage/fs/ocis/permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
"github.com/cs3org/reva/pkg/storage/utils/ace"
"github.com/cs3org/reva/pkg/user"
"github.com/pkg/errors"
"github.com/pkg/xattr"
)

Expand Down Expand Up @@ -136,7 +136,7 @@ func (p *Permissions) HasPermission(ctx context.Context, n *Node, check func(*pr
}

if cn, err = cn.Parent(); err != nil {
return false, err
return false, errors.Wrap(err, "ocisfs: error getting parent "+cn.ParentID)
}
}

Expand Down Expand Up @@ -185,14 +185,3 @@ func isNotFound(err error) bool {
}
return false
}

func (fs *ocisfs) readACE(ctx context.Context, ip string, principal string) (e *ace.ACE, err error) {
var b []byte
if b, err = xattr.Get(ip, grantPrefix+principal); err != nil {
return nil, err
}
if e, err = ace.Unmarshal(principal, b); err != nil {
return nil, err
}
return
}
6 changes: 6 additions & 0 deletions pkg/storage/fs/ocis/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func (fs *ocisfs) DownloadRevision(ctx context.Context, ref *provider.Reference,

// check if the node is available and has not been deleted
n, err := ReadNode(ctx, fs.lu, kp[0])
if err != nil {
return nil, err
}
if !n.Exists {
err = errtypes.NotFound(filepath.Join(n.ParentID, n.Name))
return nil, err
Expand Down Expand Up @@ -130,6 +133,9 @@ func (fs *ocisfs) RestoreRevision(ctx context.Context, ref *provider.Reference,

// check if the node is available and has not been deleted
n, err := ReadNode(ctx, fs.lu, kp[0])
if err != nil {
return err
}
if !n.Exists {
err = errtypes.NotFound(filepath.Join(n.ParentID, n.Name))
return err
Expand Down
18 changes: 9 additions & 9 deletions pkg/storage/fs/ocis/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ func (t *Tree) ListFolder(ctx context.Context, node *Node) ([]*Node, error) {
}

// Delete deletes a node in the tree
func (t *Tree) Delete(ctx context.Context, node *Node) (err error) {
func (t *Tree) Delete(ctx context.Context, n *Node) (err error) {

// Prepare the trash
// TODO use layout?, but it requires resolving the owners user if the username is used instead of the id.
// the node knows the owner id so we use that for now
ownerid, _, err := node.Owner()
ownerid, _, err := n.Owner()
if err != nil {
return
}
Expand All @@ -244,13 +244,13 @@ func (t *Tree) Delete(ctx context.Context, node *Node) (err error) {
}

// get the original path
origin, err := t.lu.Path(ctx, node)
origin, err := t.lu.Path(ctx, n)
if err != nil {
return
}

// set origin location in metadata
nodePath := t.lu.toInternalPath(node.ID)
nodePath := t.lu.toInternalPath(n.ID)
if err := xattr.Set(nodePath, trashOriginAttr, []byte(origin)); err != nil {
return err
}
Expand All @@ -259,8 +259,8 @@ func (t *Tree) Delete(ctx context.Context, node *Node) (err error) {

// first make node appear in the owners (or root) trash
// parent id and name are stored as extended attributes in the node itself
trashLink := filepath.Join(t.lu.Options.Root, "trash", ownerid, node.ID)
err = os.Symlink("../nodes/"+node.ID+".T."+deletionTime, trashLink)
trashLink := filepath.Join(t.lu.Options.Root, "trash", ownerid, n.ID)
err = os.Symlink("../nodes/"+n.ID+".T."+deletionTime, trashLink)
if err != nil {
// To roll back changes
// TODO unset trashOriginAttr
Expand All @@ -280,7 +280,7 @@ func (t *Tree) Delete(ctx context.Context, node *Node) (err error) {
}

// finally remove the entry from the parent dir
src := filepath.Join(t.lu.toInternalPath(node.ParentID), node.Name)
src := filepath.Join(t.lu.toInternalPath(n.ParentID), n.Name)
err = os.Remove(src)
if err != nil {
// To roll back changes
Expand All @@ -290,9 +290,9 @@ func (t *Tree) Delete(ctx context.Context, node *Node) (err error) {
return
}

p, err := node.Parent()
p, err := n.Parent()
if err != nil {
return
return errors.Wrap(err, "ocisfs: error getting parent "+n.ParentID)
}
return t.Propagate(ctx, p)
}
Expand Down
33 changes: 16 additions & 17 deletions pkg/storage/fs/ocis/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ var defaultFilePerm = os.FileMode(0664)

// TODO deprecated ... use tus

func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) error {
func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) (err error) {

n, err := fs.lu.NodeFromResource(ctx, ref)
if err != nil {
return err
var n *Node
if n, err = fs.lu.NodeFromResource(ctx, ref); err != nil {
return
}

// check permissions
Expand All @@ -60,9 +60,9 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read
})
} else {
// check permissions of parent
p, err := n.Parent()
if err != nil {
return errors.Wrap(err, "ocisfs: error getting parent "+n.ParentID)
p, perr := n.Parent()
if perr != nil {
return errors.Wrap(perr, "ocisfs: error getting parent "+n.ParentID)
}

ok, err = fs.p.HasPermission(ctx, p, func(rp *provider.ResourcePermissions) bool {
Expand All @@ -82,7 +82,8 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read

nodePath := fs.lu.toInternalPath(n.ID)

tmp, err := ioutil.TempFile(nodePath, "._reva_atomic_upload")
var tmp *os.File
tmp, err = ioutil.TempFile(nodePath, "._reva_atomic_upload")
if err != nil {
return errors.Wrap(err, "ocisfs: error creating tmp fn at "+nodePath)
}
Expand All @@ -96,9 +97,8 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read
//_ = os.RemoveAll(path.Join(nodePath, "content"))
appctx.GetLogger(ctx).Warn().Msg("TODO move old content to version")

err = os.Rename(tmp.Name(), nodePath)
if err != nil {
return err
if err = os.Rename(tmp.Name(), nodePath); err != nil {
return
}

if fs.o.EnableHome {
Expand All @@ -113,19 +113,18 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read
err = n.writeMetadata(nil)
}
if err != nil {
return err
return
}

if fs.o.TreeTimeAccounting {
// mark the home node as the end of propagation q
if err = xattr.Set(nodePath, propagationAttr, []byte("1")); err != nil {
appctx.GetLogger(ctx).Error().Err(err).Interface("node", n).Msg("could not mark node to propagate")
return err
return
}
}

return fs.tp.Propagate(ctx, n)

}

// InitiateUpload returns an upload id that can be used for uploads with tus
Expand Down Expand Up @@ -217,9 +216,9 @@ func (fs *ocisfs) NewUpload(ctx context.Context, info tusd.FileInfo) (upload tus
})
} else {
// check permissions of parent
p, err := n.Parent()
if err != nil {
return nil, errors.Wrap(err, "ocisfs: error getting parent "+n.ParentID)
p, perr := n.Parent()
if perr != nil {
return nil, errors.Wrap(perr, "ocisfs: error getting parent "+n.ParentID)
}

ok, err = fs.p.HasPermission(ctx, p, func(rp *provider.ResourcePermissions) bool {
Expand Down

0 comments on commit 4419c28

Please sign in to comment.