Skip to content

Commit

Permalink
fix owner for new nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Oct 6, 2020
1 parent 7d2d598 commit b93bc82
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 28 deletions.
28 changes: 19 additions & 9 deletions pkg/storage/fs/ocis/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,27 @@ func (t *Tree) CreateDir(ctx context.Context, node *Node) (err error) {
// create a directory node
node.ID = uuid.New().String()

if t.lu.Options.EnableHome {
if u, ok := user.ContextGetUser(ctx); ok {
err = createNode(node, u.Id)
} else {
log := appctx.GetLogger(ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
}
} else {
// who will become the owner?
u, ok := user.ContextGetUser(ctx)
switch {
case ok:
// we have a user in context
err = createNode(node, u.Id)
case t.lu.Options.EnableHome:
// enable home requires a user
log := appctx.GetLogger(ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
case t.lu.Options.Owner != "":
// fallback to owner?
err = createNode(node, &userpb.UserId{
OpaqueId: t.lu.Options.Owner,
})
default:
// fallback to parent owner?
err = createNode(node, nil)
}

if err != nil {
return nil
}
Expand Down
74 changes: 55 additions & 19 deletions pkg/storage/fs/ocis/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ func (fs *ocisfs) Upload(ctx context.Context, ref *provider.Reference, r io.Read
nodePath := fs.lu.toInternalPath(n.ID)

var tmp *os.File
tmp, err = ioutil.TempFile(nodePath, "._reva_atomic_upload")
tmp, err = ioutil.TempFile(filepath.Dir(nodePath), "._reva_atomic_upload")
if err != nil {
return errors.Wrap(err, "ocisfs: error creating tmp fn at "+nodePath)
}

_, err = io.Copy(tmp, r)
r.Close()
tmp.Close()
if err != nil {
return errors.Wrap(err, "ocisfs: error writing to tmp file "+tmp.Name())
}
Expand All @@ -97,25 +99,53 @@ 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")

appctx.GetLogger(ctx).Debug().Str("tmp", tmp.Name()).Str("ipath", nodePath).Msg("moving tmp to content")
if err = os.Rename(tmp.Name(), nodePath); err != nil {
return
}

if fs.o.EnableHome {
if u, ok := user.ContextGetUser(ctx); ok {
err = n.writeMetadata(u.Id)
} else {
log := appctx.GetLogger(ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
}
} else {
// who will become the owner?
u, ok := user.ContextGetUser(ctx)
switch {
case ok:
err = n.writeMetadata(u.Id)
case fs.o.EnableHome:
log := appctx.GetLogger(ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
case fs.o.Owner != "":
err = n.writeMetadata(&userpb.UserId{
OpaqueId: fs.o.Owner,
})
default:
// fallback to parent owner?
err = n.writeMetadata(nil)
}
if err != nil {
return
}

// link child name to parent if it is new
childNameLink := filepath.Join(fs.lu.toInternalPath(n.ParentID), n.Name)
var link string
link, err = os.Readlink(childNameLink)
if err == nil && link != "../"+n.ID {
log.Err(err).
Interface("node", n).
Str("childNameLink", childNameLink).
Str("link", link).
Msg("ocisfs: child name link has wrong target id, repairing")

if err = os.Remove(childNameLink); err != nil {
return errors.Wrap(err, "ocisfs: could not remove symlink child entry")
}
}
if os.IsNotExist(err) || link != "../"+n.ID {
if err = os.Symlink("../"+n.ID, childNameLink); err != nil {
return errors.Wrap(err, "ocisfs: could not symlink child entry")
}
}

if fs.o.TreeTimeAccounting {
// mark the home node as the end of propagation q
if err = xattr.Set(nodePath, propagationAttr, []byte("1")); err != nil {
Expand Down Expand Up @@ -442,15 +472,21 @@ func (upload *fileUpload) FinishUpload(ctx context.Context) (err error) {
Msg("ocisfs: could not rename")
return
}
if upload.fs.o.EnableHome {
if u, ok := user.ContextGetUser(upload.ctx); ok {
err = n.writeMetadata(u.Id)
} else {
log := appctx.GetLogger(upload.ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from ctx")
}
} else {
// who will become the owner?
u, ok := user.ContextGetUser(upload.ctx)
switch {
case ok:
err = n.writeMetadata(u.Id)
case upload.fs.o.EnableHome:
log := appctx.GetLogger(upload.ctx)
log.Error().Msg("home support enabled but no user in context")
err = errors.Wrap(errtypes.UserRequired("userrequired"), "error getting user from upload ctx")
case upload.fs.o.Owner != "":
err = n.writeMetadata(&userpb.UserId{
OpaqueId: upload.fs.o.Owner,
})
default:
// fallback to parent owner?
err = n.writeMetadata(nil)
}
if err != nil {
Expand Down

0 comments on commit b93bc82

Please sign in to comment.