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

feat: Implement copy dir support #73

Merged
merged 3 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
27 changes: 19 additions & 8 deletions cmd/byctl/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package main

import (
"fmt"
"strings"

"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
"github.com/docker/go-units"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/beyondstorage/beyond-ctl/operations"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)

const (
cpFlagMultipartThresholdName = "multipart-threshold"
cpFlagDirName = "dir"
)

var cpFlags = []cli.Flag{
Expand All @@ -25,6 +27,14 @@ var cpFlags = []cli.Flag{
},
Value: "1GiB", // Use 1 GiB as the default value.
},
&cli.BoolFlag{
Name: cpFlagDirName,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> cp --help
  -R, -r, --recursive          copy directories recursively

How about to use the same flag with cp command?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I think it is feasible.

Aliases: []string{
"d",
"D",
},
Usage: "Used to copy a directory",
},
}

var cpCmd = &cli.Command{
Expand Down Expand Up @@ -58,6 +68,10 @@ var cpCmd = &cli.Command{
return err
}

if c.Bool(cpFlagDirName) && !strings.HasSuffix(srcKey, "/") {
srcKey += "/"
}

src, err := services.NewStoragerFromString(srcConn)
if err != nil {
logger.Error("init src storager", zap.Error(err), zap.String("conn string", srcConn))
Expand All @@ -78,11 +92,6 @@ var cpCmd = &cli.Command{
return err
}

if srcObject.Mode.IsDir() {
logger.Error("copy dir is not supported for now")
return fmt.Errorf("copy dir not supported")
}

size, ok := srcObject.GetContentLength()
if !ok {
logger.Error("can't get object content length", zap.String("path", srcKey))
Expand Down Expand Up @@ -134,7 +143,9 @@ var cpCmd = &cli.Command{
}

var ch chan *operations.EmptyResult
if size < multipartThreshold {
if c.Bool(cpFlagDirName) {
ch, err = do.CopyDir(srcKey, dstKey, multipartThreshold)
} else if size < multipartThreshold {
ch, err = do.CopyFileViaWrite(srcKey, dstKey, size)
} else {
// TODO: we will support other copy method later.
Expand Down
1 change: 1 addition & 0 deletions cmd/byctl/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"

"github.com/urfave/cli/v2"
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/byctl/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
"path/filepath"
"time"

"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/beyondstorage/beyond-ctl/operations"
"github.com/beyondstorage/go-storage/v4/services"
"github.com/beyondstorage/go-storage/v4/types"
)

const (
Expand Down
3 changes: 1 addition & 2 deletions cmd/byctl/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import (
"fmt"
"os"

"go.uber.org/zap"

"github.com/BurntSushi/toml"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/beyondstorage/beyond-ctl/config"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/byctl/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"errors"
"fmt"

"github.com/beyondstorage/go-storage/v4/services"
"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/beyondstorage/beyond-ctl/operations"
"github.com/beyondstorage/go-storage/v4/services"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions cmd/byctl/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package main

import (
"fmt"
"github.com/Xuanwo/go-bufferpool"
"time"

"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/types"
"github.com/Xuanwo/go-bufferpool"
"github.com/docker/go-units"
"github.com/urfave/cli/v2"
"golang.org/x/time/rate"

"github.com/beyondstorage/beyond-ctl/config"
"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/types"
)

var pool = bufferpool.New(128)
Expand Down
75 changes: 74 additions & 1 deletion operations/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"fmt"
"io"
"sort"
"strings"
"sync"

"go.uber.org/zap"

"github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/types"
"go.uber.org/zap"
)

// CopyFileViaWrite will copy a file via Write operation.
Expand Down Expand Up @@ -189,3 +191,74 @@ func (do *DualOperator) copyMultipart(
}
ch <- &PartResult{Part: p}
}

// CopyDir will copy a directory.
func (do *DualOperator) CopyDir(src, dst string, multipartThreshold int64) (errch chan *EmptyResult, err error) {
errch = make(chan *EmptyResult, 4)

so := NewSingleOperator(do.src)
och, err := so.ListRecursively(src)
if err != nil {
errch <- &EmptyResult{Error: err}
}

if !strings.HasSuffix(dst, "/") {
dst += "/"
}

go func() {
defer close(errch)

wg := &sync.WaitGroup{}

for or := range och {
if or.Error != nil {
errch <- &EmptyResult{Error: err}
break
}
object := or.Object

if object.Mode.IsDir() {
continue
}

path := dst + strings.TrimPrefix(object.Path, src)
size := object.MustGetContentLength()

wg.Add(1)
err = so.pool.Submit(func() {
defer wg.Done()

if size < multipartThreshold {
ch, err := do.CopyFileViaWrite(object.Path, path, size)
if err != nil {
errch <- &EmptyResult{Error: err}
}
for er := range ch {
if er.Error != nil {
errch <- &EmptyResult{Error: err}
}
}
} else {
ch, err := do.CopyFileViaMultipart(object.Path, path, size)
if err != nil {
errch <- &EmptyResult{Error: err}
}
for er := range ch {
if er.Error != nil {
errch <- &EmptyResult{Error: err}
}
}
}
})
if err != nil {
errch <- &EmptyResult{Error: err}
break
}
}

wg.Wait()
}()

return errch, nil
}
3 changes: 2 additions & 1 deletion operations/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package operations
import (
"fmt"

"github.com/beyondstorage/go-storage/v4/types"
"github.com/panjf2000/ants/v2"
"go.uber.org/zap"

"github.com/beyondstorage/go-storage/v4/types"
)

type SingleOperator struct {
Expand Down