Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move shares folder out to a separate mount and handle ref conflicts #1584

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/shares-folder-mount.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Move shares folder out from home directory to a separate mount

https://github.com/cs3org/reva/pull/1584
73 changes: 72 additions & 1 deletion internal/grpc/services/gateway/ocmshareprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func (s *svc) UpdateReceivedOCMShare(ctx context.Context, req *ocm.UpdateReceive
Status: createRefStatus,
}, err
case ocm.ShareState_SHARE_STATE_REJECTED:
s.removeReference(ctx, req.GetShare().GetShare().ResourceId) // error is logged inside removeReference
s.removeOCMReference(ctx, req.GetShare().GetShare().GetResourceId()) // error is logged inside removeReference
// FIXME we are ignoring an error from removeReference here
return res, nil
}
Expand Down Expand Up @@ -425,6 +425,77 @@ func (s *svc) GetReceivedOCMShare(ctx context.Context, req *ocm.GetReceivedOCMSh
return res, nil
}

func (s *svc) removeOCMReference(ctx context.Context, resourceID *provider.ResourceId) *rpc.Status {
log := appctx.GetLogger(ctx)

idReference := &provider.Reference{ResourceId: resourceID}
storageProvider, err := s.find(ctx, idReference)
if err != nil {
if _, ok := err.(errtypes.IsNotFound); ok {
return status.NewNotFound(ctx, "storage provider not found")
}
return status.NewInternal(ctx, err, "error finding storage provider")
}

statRes, err := storageProvider.Stat(ctx, &provider.StatRequest{Ref: idReference})
if err != nil {
return status.NewInternal(ctx, err, "gateway: error calling Stat for the share resource id: "+resourceID.String())
}

// FIXME how can we delete a reference if the original resource was deleted?
if statRes.Status.Code != rpc.Code_CODE_OK {
err := status.NewErrorFromCode(statRes.Status.GetCode(), "gateway")
return status.NewInternal(ctx, err, "could not delete share reference")
}

homeRes, err := s.GetHome(ctx, &provider.GetHomeRequest{})
if err != nil {
err := errors.Wrap(err, "gateway: error calling GetHome")
return status.NewInternal(ctx, err, "could not delete share reference")
}

sharePath := path.Join(homeRes.Path, s.c.ShareFolder, path.Base(statRes.Info.Path))
log.Debug().Str("share_path", sharePath).Msg("remove reference of share")

homeProvider, err := s.find(ctx, &provider.Reference{Path: sharePath})
if err != nil {
if _, ok := err.(errtypes.IsNotFound); ok {
return status.NewNotFound(ctx, "storage provider not found")
}
return status.NewInternal(ctx, err, "error finding storage provider")
}

deleteReq := &provider.DeleteRequest{
Opaque: &types.Opaque{
Map: map[string]*types.OpaqueEntry{
// This signals the storageprovider that we want to delete the share reference and not the underlying file.
"deleting_shared_resource": {},
},
},
Ref: &provider.Reference{Path: sharePath},
}

deleteResp, err := homeProvider.Delete(ctx, deleteReq)
if err != nil {
return status.NewInternal(ctx, err, "could not delete share reference")
}

switch deleteResp.Status.Code {
case rpc.Code_CODE_OK:
// we can continue deleting the reference
case rpc.Code_CODE_NOT_FOUND:
// This is fine, we wanted to delete it anyway
return status.NewOK(ctx)
default:
err := status.NewErrorFromCode(deleteResp.Status.GetCode(), "gateway")
return status.NewInternal(ctx, err, "could not delete share reference")
}

log.Debug().Str("share_path", sharePath).Msg("share reference successfully removed")

return status.NewOK(ctx)
}

func (s *svc) createOCMReference(ctx context.Context, share *ocm.Share) (*rpc.Status, error) {

log := appctx.GetLogger(ctx)
Expand Down
163 changes: 163 additions & 0 deletions internal/grpc/services/gateway/sharesstorageprovider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// Copyright 2018-2021 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package gateway

import (
"context"
"path"
"strings"

"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"

rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/rgrpc/status"
"github.com/cs3org/reva/pkg/storage/utils/etag"
)

func (s *svc) getSharedFolder(ctx context.Context) string {
return path.Join("/", s.c.ShareFolder)
}

// check if the path contains the prefix of the shared folder
func (s *svc) inSharedFolder(ctx context.Context, p string) bool {
sharedFolder := s.getSharedFolder(ctx)
return strings.HasPrefix(p, sharedFolder)
}

// /MyShares/
func (s *svc) isSharedFolder(ctx context.Context, p string) bool {
return p == s.getSharedFolder(ctx)
}

// /MyShares/photos/
func (s *svc) isShareName(ctx context.Context, p string) bool {
sharedFolder := s.getSharedFolder(ctx)
rel := strings.Trim(strings.TrimPrefix(p, sharedFolder), "/")
return strings.HasPrefix(p, sharedFolder) && len(strings.Split(rel, "/")) == 1
}

// /MyShares/photos/Ibiza/beach.png
func (s *svc) isShareChild(ctx context.Context, p string) bool {
sharedFolder := s.getSharedFolder(ctx)
rel := strings.Trim(strings.TrimPrefix(p, sharedFolder), "/")
return strings.HasPrefix(p, sharedFolder) && len(strings.Split(rel, "/")) > 1
}

// path must contain a share path with share children, if not it will panic.
// should be called after checking isShareChild == true
func (s *svc) splitShare(ctx context.Context, p string) (string, string) {
sharedFolder := s.getSharedFolder(ctx)
p = strings.Trim(strings.TrimPrefix(p, sharedFolder), "/")
parts := strings.SplitN(p, "/", 2)
if len(parts) != 2 {
panic("gateway: path for splitShare does not contain 2 elements:" + p)
}

shareName := path.Join(sharedFolder, parts[0])
shareChild := path.Join("/", parts[1])
return shareName, shareChild
}

func (s *svc) statSharesFolder(ctx context.Context) (*provider.StatResponse, error) {
statRes, err := s.stat(ctx, &provider.StatRequest{
Ref: &provider.Reference{
Path: s.getSharedFolder(ctx),
},
})
if err != nil {
return &provider.StatResponse{
Status: status.NewInternal(ctx, err, "gateway: error stating shares folder"),
}, nil
}

if statRes.Status.Code != rpc.Code_CODE_OK {
return &provider.StatResponse{
Status: statRes.Status,
}, nil
}

lsRes, err := s.listSharesFolder(ctx)
if err != nil {
return &provider.StatResponse{
Status: status.NewInternal(ctx, err, "gateway: error listing shares folder"),
}, nil
}
if lsRes.Status.Code != rpc.Code_CODE_OK {
return &provider.StatResponse{
Status: lsRes.Status,
}, nil
}

etagCacheKey := statRes.Info.Owner.OpaqueId + ":" + statRes.Info.Path
if resEtag, err := s.etagCache.Get(etagCacheKey); err == nil {
statRes.Info.Etag = resEtag.(string)
} else {
statRes.Info.Etag = etag.GenerateEtagFromResources(statRes.Info, lsRes.Infos)
if s.c.EtagCacheTTL > 0 {
_ = s.etagCache.Set(etagCacheKey, statRes.Info.Etag)
}
}
return statRes, nil
}

func (s *svc) listSharesFolder(ctx context.Context) (*provider.ListContainerResponse, error) {
lcr, err := s.listContainer(ctx, &provider.ListContainerRequest{
Ref: &provider.Reference{
Path: s.getSharedFolder(ctx),
},
})
if err != nil {
return &provider.ListContainerResponse{
Status: status.NewInternal(ctx, err, "gateway: error listing shared folder"),
}, nil
}
if lcr.Status.Code != rpc.Code_CODE_OK {
return &provider.ListContainerResponse{
Status: lcr.Status,
}, nil
}
checkedInfos := make([]*provider.ResourceInfo, 0)
for i := range lcr.Infos {
info, protocol, err := s.checkRef(ctx, lcr.Infos[i])
if err != nil {
// Create status to log the proper messages
// This might arise when the shared resource has been moved to the recycle bin
// or when the resource was unshared, but the share reference was not removed
status.NewStatusFromErrType(ctx, "error resolving reference "+lcr.Infos[i].Target, err)
continue
}

if protocol == "webdav" {
info, err = s.webdavRefStat(ctx, lcr.Infos[i].Target)
if err != nil {
// This might arise when the webdav token has expired
continue
}
}

// It should be possible to delete and move share references, so expose all possible permissions
info.PermissionSet = conversions.NewManagerRole().CS3ResourcePermissions()
info.Path = lcr.Infos[i].GetPath()
checkedInfos = append(checkedInfos, info)
}
lcr.Infos = checkedInfos

return lcr, nil
}
Loading