Skip to content

Commit

Permalink
enhancement: de duplicate mountpoint name resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Jun 7, 2024
1 parent be5b70e commit a9e53f3
Show file tree
Hide file tree
Showing 7 changed files with 383 additions and 154 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ require (
github.com/segmentio/kafka-go v0.4.47
github.com/sethvargo/go-password v0.2.0
github.com/shamaton/msgpack/v2 v2.1.1
github.com/shirou/gopsutil v3.21.11+incompatible
github.com/stretchr/testify v1.8.4
github.com/studio-b12/gowebdav v0.0.0-20221015232716-17255f2e7423
github.com/test-go/testify v1.1.4
Expand Down Expand Up @@ -199,7 +200,6 @@ require (
github.com/rs/xid v1.5.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 // indirect
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2052,8 +2052,6 @@ golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
46 changes: 16 additions & 30 deletions internal/grpc/services/usershareprovider/usershareprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ func (s *service) UpdateReceivedShare(ctx context.Context, req *collaboration.Up
switch {
case err != nil:
fallthrough
case receivedShare.GetStatus().GetCode() != rpc.Code_CODE_OK:
case resourceStat.GetStatus().GetCode() != rpc.Code_CODE_OK:
return &collaboration.UpdateReceivedShareResponse{
Status: receivedShare.GetStatus(),
Status: resourceStat.GetStatus(),
}, err
}

Expand All @@ -584,7 +584,7 @@ func (s *service) UpdateReceivedShare(ctx context.Context, req *collaboration.Up
}

// check if the requested mount point is available and if not, find a suitable one
availableMountpoint, _, err := GetMountpointAndUnmountedShares(ctx, gatewayClient,
availableMountpoint, err := GetAvailableMountpoint(ctx, gatewayClient,
resourceStat.GetInfo().GetId(),
requestedMountpoint,
)
Expand Down Expand Up @@ -618,22 +618,25 @@ func (s *service) UpdateReceivedShare(ctx context.Context, req *collaboration.Up
}, nil
}

// GetMountpointAndUnmountedShares returns a new or existing mountpoint for the given info and produces a list of unmounted received shares for the same resource
func GetMountpointAndUnmountedShares(ctx context.Context, gwc gateway.GatewayAPIClient, id *provider.ResourceId, name string) (string, []*collaboration.ReceivedShare, error) {
var unmountedShares []*collaboration.ReceivedShare
receivedShares, err := listReceivedShares(ctx, gwc)
// GetAvailableMountpoint returns a new or existing mountpoint
func GetAvailableMountpoint(ctx context.Context, gwc gateway.GatewayAPIClient, id *provider.ResourceId, name string) (string, error) {
listReceivedSharesRes, err := gwc.ListReceivedShares(ctx, &collaboration.ListReceivedSharesRequest{})
if err != nil {
return "", unmountedShares, err
return "", errtypes.InternalError("grpc list received shares request failed")
}

if err := errtypes.NewErrtypeFromStatus(listReceivedSharesRes.GetStatus()); err != nil {
return "", err
}

// we need to sort the received shares by mount point in order to make things easier to evaluate.
base := filepath.Clean(name)
mount := base
existingMountpoint := ""
mountedShares := make([]string, 0, len(receivedShares))
mountedShares := make([]string, 0, len(listReceivedSharesRes.GetShares()))
var pathExists bool

for _, s := range receivedShares {
for _, s := range listReceivedSharesRes.GetShares() {
resourceIDEqual := utils.ResourceIDEqual(s.GetShare().GetResourceId(), id)

if resourceIDEqual && s.State == collaboration.ShareState_SHARE_STATE_ACCEPTED {
Expand All @@ -644,11 +647,6 @@ func GetMountpointAndUnmountedShares(ctx context.Context, gwc gateway.GatewayAPI
}
}

if resourceIDEqual && s.State != collaboration.ShareState_SHARE_STATE_ACCEPTED {
// a share to the resource already exists but is not mounted, collect the unmounted share
unmountedShares = append(unmountedShares, s)
}

if s.State == collaboration.ShareState_SHARE_STATE_ACCEPTED {
// collect all accepted mount points
mountedShares = append(mountedShares, s.GetMountPoint().GetPath())
Expand All @@ -665,7 +663,7 @@ func GetMountpointAndUnmountedShares(ctx context.Context, gwc gateway.GatewayAPI

if existingMountpoint != "" {
// we want to reuse the same mountpoint for all unmounted shares to the same resource
return existingMountpoint, unmountedShares, nil
return existingMountpoint, nil
}

// If the mount point really already exists, we need to insert a number into the filename
Expand All @@ -681,22 +679,10 @@ func GetMountpointAndUnmountedShares(ctx context.Context, gwc gateway.GatewayAPI
}
mount = name + " (" + strconv.Itoa(i) + ")" + ext
if !slices.Contains(mountedShares, mount) {
return mount, unmountedShares, nil
return mount, nil
}
}
}
return mount, unmountedShares, nil
}

// listReceivedShares list all received shares for the current user.
func listReceivedShares(ctx context.Context, client gateway.GatewayAPIClient) ([]*collaboration.ReceivedShare, error) {
res, err := client.ListReceivedShares(ctx, &collaboration.ListReceivedSharesRequest{})
if err != nil {
return nil, errtypes.InternalError("grpc list received shares request failed")
}

if err := errtypes.NewErrtypeFromStatus(res.Status); err != nil {
return nil, err
}
return res.Shares, nil
return mount, nil
}
Loading

0 comments on commit a9e53f3

Please sign in to comment.