2021-03-31 16:56:34 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-04-06 20:19:11 +01:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-03-31 16:56:34 +01:00
|
|
|
"strconv"
|
|
|
|
|
2021-04-06 20:19:11 +01:00
|
|
|
progressbar "github.com/cheggaaa/pb/v3"
|
2021-03-31 16:56:34 +01:00
|
|
|
"github.com/zeebo/clingy"
|
2021-04-06 20:19:11 +01:00
|
|
|
"github.com/zeebo/errs"
|
2021-05-06 17:56:57 +01:00
|
|
|
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulfs"
|
|
|
|
"storj.io/storj/cmd/uplinkng/ulloc"
|
2021-03-31 16:56:34 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type cmdCp struct {
|
|
|
|
projectProvider
|
|
|
|
|
|
|
|
recursive bool
|
2021-04-06 20:19:11 +01:00
|
|
|
dryrun bool
|
2021-05-12 17:16:56 +01:00
|
|
|
progress bool
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
source ulloc.Location
|
|
|
|
dest ulloc.Location
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdCp) Setup(a clingy.Arguments, f clingy.Flags) {
|
|
|
|
c.projectProvider.Setup(a, f)
|
|
|
|
|
|
|
|
c.recursive = f.New("recursive", "Peform a recursive copy", false,
|
|
|
|
clingy.Short('r'),
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-04-06 20:19:11 +01:00
|
|
|
c.dryrun = f.New("dryrun", "Print what operations would happen but don't execute them", false,
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-05-12 17:16:56 +01:00
|
|
|
c.progress = f.New("progress", "Show a progress bar when possible", true,
|
|
|
|
clingy.Transform(strconv.ParseBool),
|
|
|
|
).(bool)
|
2021-03-31 16:56:34 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
c.source = a.New("source", "Source to copy", clingy.Transform(ulloc.Parse)).(ulloc.Location)
|
|
|
|
c.dest = a.New("dest", "Desination to copy", clingy.Transform(ulloc.Parse)).(ulloc.Location)
|
2021-03-31 16:56:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *cmdCp) Execute(ctx clingy.Context) error {
|
2021-04-06 20:19:11 +01:00
|
|
|
fs, err := c.OpenFilesystem(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = fs.Close() }()
|
|
|
|
|
|
|
|
if c.recursive {
|
|
|
|
return c.copyRecursive(ctx, fs)
|
|
|
|
}
|
2021-05-12 17:16:56 +01:00
|
|
|
return c.copyFile(ctx, fs, c.source, c.dest, c.progress)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (c *cmdCp) copyRecursive(ctx clingy.Context, fs ulfs.Filesystem) error {
|
2021-04-06 20:19:11 +01:00
|
|
|
if c.source.Std() || c.dest.Std() {
|
|
|
|
return errs.New("cannot recursively copy to stdin/stdout")
|
|
|
|
}
|
|
|
|
|
|
|
|
var anyFailed bool
|
|
|
|
|
|
|
|
iter, err := fs.ListObjects(ctx, c.source, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for iter.Next() {
|
|
|
|
rel, err := c.source.RelativeTo(iter.Item().Loc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
source := iter.Item().Loc
|
|
|
|
dest := c.dest.AppendKey(rel)
|
|
|
|
|
|
|
|
if err := c.copyFile(ctx, fs, source, dest, false); err != nil {
|
|
|
|
fmt.Fprintln(ctx.Stderr(), copyVerb(source, dest), "failed:", err.Error())
|
|
|
|
anyFailed = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := iter.Err(); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
} else if anyFailed {
|
|
|
|
return errs.New("some downloads failed")
|
|
|
|
}
|
2021-03-31 16:56:34 +01:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-06 20:19:11 +01:00
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func (c *cmdCp) copyFile(ctx clingy.Context, fs ulfs.Filesystem, source, dest ulloc.Location, progress bool) error {
|
2021-04-06 20:19:11 +01:00
|
|
|
if isDir := fs.IsLocalDir(ctx, dest); isDir {
|
|
|
|
base, ok := source.Base()
|
|
|
|
if !ok {
|
|
|
|
return errs.New("destination is a directory and cannot find base name for %q", source)
|
|
|
|
}
|
|
|
|
dest = dest.AppendKey(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !source.Std() && !dest.Std() {
|
2021-05-12 17:16:56 +01:00
|
|
|
fmt.Fprintln(ctx.Stdout(), copyVerb(source, dest), source, "to", dest)
|
2021-04-06 20:19:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.dryrun {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rh, err := fs.Open(ctx, source)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = rh.Close() }()
|
|
|
|
|
|
|
|
wh, err := fs.Create(ctx, dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() { _ = wh.Abort() }()
|
|
|
|
|
|
|
|
var bar *progressbar.ProgressBar
|
|
|
|
var writer io.Writer = wh
|
|
|
|
|
|
|
|
if length := rh.Info().ContentLength; progress && length >= 0 && !c.dest.Std() {
|
|
|
|
bar = progressbar.New64(length).SetWriter(ctx.Stdout())
|
|
|
|
writer = bar.NewProxyWriter(writer)
|
|
|
|
bar.Start()
|
|
|
|
defer bar.Finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := io.Copy(writer, rh); err != nil {
|
|
|
|
return errs.Combine(err, wh.Abort())
|
|
|
|
}
|
|
|
|
return errs.Wrap(wh.Commit())
|
|
|
|
}
|
|
|
|
|
2021-05-06 17:56:57 +01:00
|
|
|
func copyVerb(source, dest ulloc.Location) string {
|
2021-04-06 20:19:11 +01:00
|
|
|
switch {
|
2021-05-06 17:56:57 +01:00
|
|
|
case dest.Remote():
|
2021-04-06 20:19:11 +01:00
|
|
|
return "upload"
|
2021-05-06 17:56:57 +01:00
|
|
|
case source.Remote():
|
2021-04-06 20:19:11 +01:00
|
|
|
return "download"
|
|
|
|
default:
|
|
|
|
return "copy"
|
|
|
|
}
|
|
|
|
}
|