From 71b35bf59ec943b7de7dfc7d8ed654663b63d4d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Duffeck?= Date: Tue, 14 Sep 2021 14:23:31 +0200 Subject: [PATCH] Add support for renaming shares in the SharesStorageprovider --- .../sharesstorageprovider.go | 47 ++++++++++++++----- .../sharesstorageprovider_test.go | 7 ++- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/internal/grpc/services/sharesstorageprovider/sharesstorageprovider.go b/internal/grpc/services/sharesstorageprovider/sharesstorageprovider.go index 59078b2bd4..d030f5bf98 100644 --- a/internal/grpc/services/sharesstorageprovider/sharesstorageprovider.go +++ b/internal/grpc/services/sharesstorageprovider/sharesstorageprovider.go @@ -450,16 +450,28 @@ func (s *service) Move(ctx context.Context, req *provider.MoveRequest) (*provide Interface("destinationShare", destinationShare). Msg("sharesstorageprovider: Got Move request") - if reqShare == "" || reqPath == "" || destinationPath == "" { + stattedShare, err := s.statShare(ctx, reqShare) + if err != nil { return &provider.MoveResponse{ - Status: status.NewInvalid(ctx, "sharesstorageprovider: can not move top-level share"), + Status: status.NewInternal(ctx, err, "sharesstorageprovider: error stating the source share"), }, nil } - stattedShare, err := s.statShare(ctx, reqShare) - if err != nil { + if reqShare != destinationShare && reqPath == "" { + // Change the MountPoint of the share + stattedShare.ReceivedShare.MountPoint = &provider.Reference{Path: destinationShare} + + _, err = s.sharesProviderClient.UpdateReceivedShare(ctx, &collaboration.UpdateReceivedShareRequest{ + Share: stattedShare.ReceivedShare, + UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"state", "mount_point"}}, + }) + if err != nil { + return &provider.MoveResponse{ + Status: status.NewInternal(ctx, err, "sharesstorageprovider: can not change mountpoint of share"), + }, nil + } return &provider.MoveResponse{ - Status: status.NewInternal(ctx, err, "sharesstorageprovider: error stating the source share"), + Status: status.NewOK(ctx), }, nil } @@ -518,7 +530,7 @@ func (s *service) Stat(ctx context.Context, req *provider.StatRequest) (*provide stattedShare, err := s.statShare(ctx, reqShare) if err != nil { return &provider.StatResponse{ - Status: status.NewInternal(ctx, err, "sharesstorageprovider: error stating the source share"), + Status: status.NewNotFound(ctx, "sharesstorageprovider: error stating the source share"), }, nil } res := &provider.StatResponse{ @@ -541,7 +553,8 @@ func (s *service) Stat(ctx context.Context, req *provider.StatRequest) (*provide } } - relPath := strings.SplitAfterN(res.Info.Path, reqShare, 2)[1] + origReqShare := filepath.Base(stattedShare.Stat.Path) + relPath := strings.SplitAfterN(res.Info.Path, origReqShare, 2)[1] res.Info.Path = filepath.Join(s.mountPath, reqShare, relPath) appctx.GetLogger(ctx).Debug(). @@ -619,10 +632,11 @@ func (s *service) ListContainer(ctx context.Context, req *provider.ListContainer // continue // } - if reqShare != "" && name == reqShare { + if reqShare != "" && (name == reqShare || (stattedShare.ReceivedShare.MountPoint != nil && stattedShare.ReceivedShare.MountPoint.Path == reqShare)) { + origReqShare := filepath.Base(stattedShare.Stat.Path) gwListRes, err := s.gateway.ListContainer(ctx, &provider.ListContainerRequest{ Ref: &provider.Reference{ - Path: filepath.Join(filepath.Dir(stattedShare.Stat.Path), reqShare, reqPath), + Path: filepath.Join(filepath.Dir(stattedShare.Stat.Path), origReqShare, reqPath), }, }) if err != nil { @@ -631,13 +645,17 @@ func (s *service) ListContainer(ctx context.Context, req *provider.ListContainer }, nil } for _, info := range gwListRes.Infos { - relPath := strings.SplitAfterN(info.Path, reqShare, 2)[1] + relPath := strings.SplitAfterN(info.Path, origReqShare, 2)[1] info.Path = filepath.Join(s.mountPath, reqShare, relPath) info.PermissionSet = stattedShare.Stat.PermissionSet } return gwListRes, nil } else if reqShare == "" { - stattedShare.Stat.Path = filepath.Join(s.mountPath, filepath.Base(stattedShare.Stat.Path)) + path := stattedShare.Stat.Path + if stattedShare.ReceivedShare.MountPoint != nil { + path = stattedShare.ReceivedShare.MountPoint.Path + } + stattedShare.Stat.Path = filepath.Join(s.mountPath, filepath.Base(path)) res.Infos = append(res.Infos, stattedShare.Stat) } } @@ -787,6 +805,13 @@ func (s *service) statShare(ctx context.Context, share string) (*stattedReceived return nil, fmt.Errorf("sharesstorageprovider: error getting received shares") } stattedShare, ok := shares[share] + if !ok { + for _, ss := range shares { + if ss.ReceivedShare.MountPoint != nil && ss.ReceivedShare.MountPoint.Path == share { + stattedShare, ok = ss, true + } + } + } if !ok { return nil, fmt.Errorf("sharesstorageprovider: requested share not found") } diff --git a/internal/grpc/services/sharesstorageprovider/sharesstorageprovider_test.go b/internal/grpc/services/sharesstorageprovider/sharesstorageprovider_test.go index 9d5f84d22e..6d2c77db10 100644 --- a/internal/grpc/services/sharesstorageprovider/sharesstorageprovider_test.go +++ b/internal/grpc/services/sharesstorageprovider/sharesstorageprovider_test.go @@ -555,7 +555,9 @@ var _ = Describe("Sharesstorageprovider", func() { }, nil) }) - It("refuses to move a share", func() { + It("renames a share", func() { + sharesProviderClient.On("UpdateReceivedShare", mock.Anything, mock.Anything).Return(nil, nil) + req := &sprovider.MoveRequest{ Source: &sprovider.Reference{ Path: "/shares/share1-shareddir", @@ -568,7 +570,8 @@ var _ = Describe("Sharesstorageprovider", func() { gw.AssertNotCalled(GinkgoT(), "Move", mock.Anything, mock.Anything) Expect(err).ToNot(HaveOccurred()) Expect(res).ToNot(BeNil()) - Expect(res.Status.Code).To(Equal(rpc.Code_CODE_INVALID_ARGUMENT)) + Expect(res.Status.Code).To(Equal(rpc.Code_CODE_OK)) + sharesProviderClient.AssertCalled(GinkgoT(), "UpdateReceivedShare", mock.Anything, mock.Anything) }) It("refuses to move a file between shares", func() {