bring back the original progress bar (#4866)

also cleanup 'mc put/get' behavior
This commit is contained in:
Harshavardhana
2024-03-02 16:13:08 -08:00
committed by GitHub
parent 66daa906eb
commit 98af07b69c
15 changed files with 158 additions and 128 deletions
+1 -1
View File
@@ -76,4 +76,4 @@ jobs:
- name: Staticcheck
# Run with defaults, but allow errors with other formats ST1005
run: go install honnef.co/go/tools/cmd/staticcheck@latest && staticcheck -tests=false -checks="all,-ST1000,-ST1003,-ST1016,-ST1020,-ST1021,-ST1022,-ST1023,-ST1005" ./...
run: go install honnef.co/go/tools/cmd/staticcheck@latest && make lint
+1 -1
View File
@@ -32,7 +32,7 @@ vet:
@echo "Running $@"
@GO111MODULE=on go vet github.com/minio/mc/...
lint:
lint: getdeps
@echo "Running $@ check"
@GO111MODULE=on ${GOPATH}/bin/golangci-lint run --timeout=5m --config ./.golangci.yml
@GO111MODULE=on ${GOPATH}/bin/staticcheck -tests=false -checks="all,-ST1000,-ST1003,-ST1016,-ST1020,-ST1021,-ST1022,-ST1023,-ST1005" ./...
+9 -2
View File
@@ -231,7 +231,15 @@ func catURL(ctx context.Context, sourceURL string, encKeyDB map[string][]prefixS
// are ignored since some of them have zero size though they
// have contents like files under /proc.
// 2. extract the version ID if rewind flag is passed
if client, content, err := url2Stat(ctx, url2StatOptions{urlStr: sourceURL, versionID: o.versionID, fileAttr: false, encKeyDB: encKeyDB, timeRef: o.timeRef, isZip: o.isZip, ignoreBucketExistsCheck: false}); err == nil {
if client, content, err := url2Stat(ctx, url2StatOptions{
urlStr: sourceURL,
versionID: o.versionID,
fileAttr: false,
encKeyDB: encKeyDB,
timeRef: o.timeRef,
isZip: o.isZip,
ignoreBucketExistsCheck: false,
}); err == nil {
if o.versionID == "" {
versionID = content.VersionID
}
@@ -256,7 +264,6 @@ func catURL(ctx context.Context, sourceURL string, encKeyDB map[string][]prefixS
gopts := GetOptions{VersionID: versionID, Zip: o.isZip, RangeStart: o.startO}
if reader, err = getSourceStreamFromURL(ctx, sourceURL, encKeyDB, getSourceOpts{
GetOptions: gopts,
fetchStat: false,
preserve: false,
}); err != nil {
return err.Trace(sourceURL)
+36 -4
View File
@@ -540,21 +540,53 @@ func (f *fsClient) Copy(ctx context.Context, source string, opts CopyOptions, pr
}
// Get returns reader and any additional metadata.
func (f *fsClient) Get(_ context.Context, opts GetOptions) (io.ReadCloser, *probe.Error) {
func (f *fsClient) Get(_ context.Context, opts GetOptions) (io.ReadCloser, *ClientContent, *probe.Error) {
fileData, e := os.Open(f.PathURL.Path)
if e != nil {
err := f.toClientError(e, f.PathURL.Path)
return nil, err.Trace(f.PathURL.Path)
return nil, nil, err.Trace(f.PathURL.Path)
}
if opts.RangeStart != 0 {
_, e := fileData.Seek(opts.RangeStart, io.SeekStart)
if e != nil {
err := f.toClientError(e, f.PathURL.Path)
return nil, err.Trace(f.PathURL.Path)
return nil, nil, err.Trace(f.PathURL.Path)
}
}
return fileData, nil
fi, e := fileData.Stat()
if e != nil {
return nil, nil, probe.NewError(e)
}
content := &ClientContent{}
content.URL = *f.PathURL
content.Size = fi.Size()
content.Time = fi.ModTime()
content.Type = fi.Mode()
content.Metadata = map[string]string{
"Content-Type": guessURLContentType(f.PathURL.Path),
}
path := f.PathURL.String()
// Populates meta data with file system attribute only in case of
// when preserve flag is passed.
if opts.Preserve {
fileAttr, err := disk.GetFileSystemAttrs(path)
if err != nil {
return nil, content, nil
}
metaData, pErr := getAllXattrs(path)
if pErr != nil {
return nil, content, nil
}
for k, v := range metaData {
content.Metadata[k] = v
}
content.Metadata[metadataKey] = fileAttr
}
return fileData, content, nil
}
// Check if the given error corresponds to ENOTEMPTY for unix
+2 -2
View File
@@ -290,7 +290,7 @@ func (s *TestSuite) TestGet(c *checkv1.C) {
c.Assert(err, checkv1.IsNil)
c.Assert(n, checkv1.Equals, int64(len(data)))
reader, err = fsClient.Get(context.Background(), GetOptions{})
reader, _, err = fsClient.Get(context.Background(), GetOptions{})
c.Assert(err, checkv1.IsNil)
var results bytes.Buffer
_, e = io.Copy(&results, reader)
@@ -319,7 +319,7 @@ func (s *TestSuite) TestGetRange(c *checkv1.C) {
c.Assert(err, checkv1.IsNil)
c.Assert(n, checkv1.Equals, int64(len(data)))
reader, err = fsClient.Get(context.Background(), GetOptions{})
reader, _, err = fsClient.Get(context.Background(), GetOptions{})
c.Assert(err, checkv1.IsNil)
var results bytes.Buffer
buf := make([]byte, 5)
+25 -7
View File
@@ -942,7 +942,7 @@ func (c *S3Client) Watch(ctx context.Context, options WatchOptions) (*WatchObjec
}
// Get - get object with GET options.
func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *probe.Error) {
func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *ClientContent, *probe.Error) {
bucket, object := c.url2BucketAndObject()
o := minio.GetObjectOptions{
ServerSideEncryption: opts.SSE,
@@ -954,7 +954,7 @@ func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *pr
if opts.RangeStart != 0 {
err := o.SetRange(opts.RangeStart, 0)
if err != nil {
return nil, probe.NewError(err)
return nil, nil, probe.NewError(err)
}
}
// Disallow automatic decompression for some objects with content-encoding set.
@@ -964,21 +964,39 @@ func (c *S3Client) Get(ctx context.Context, opts GetOptions) (io.ReadCloser, *pr
if e != nil {
errResponse := minio.ToErrorResponse(e)
if errResponse.Code == "NoSuchBucket" {
return nil, probe.NewError(BucketDoesNotExist{
return nil, nil, probe.NewError(BucketDoesNotExist{
Bucket: bucket,
})
}
if errResponse.Code == "InvalidBucketName" {
return nil, probe.NewError(BucketInvalid{
return nil, nil, probe.NewError(BucketInvalid{
Bucket: bucket,
})
}
if errResponse.Code == "NoSuchKey" {
return nil, probe.NewError(ObjectMissing{})
return nil, nil, probe.NewError(ObjectMissing{})
}
return nil, probe.NewError(e)
return nil, nil, probe.NewError(e)
}
return reader, nil
objStat, e := reader.Stat()
if e != nil {
errResponse := minio.ToErrorResponse(e)
if errResponse.Code == "NoSuchBucket" {
return nil, nil, probe.NewError(BucketDoesNotExist{
Bucket: bucket,
})
}
if errResponse.Code == "InvalidBucketName" {
return nil, nil, probe.NewError(BucketInvalid{
Bucket: bucket,
})
}
if errResponse.Code == "NoSuchKey" {
return nil, nil, probe.NewError(ObjectMissing{})
}
return nil, nil, probe.NewError(e)
}
return reader, c.objectInfo2ClientContent(bucket, objStat), nil
}
// Copy - copy object, uses server side copy API. Also uses an abstracted API
+1 -1
View File
@@ -265,7 +265,7 @@ func (s *TestSuite) TestObjectOperations(c *checkv1.C) {
c.Assert(err, checkv1.IsNil)
c.Assert(n, checkv1.Equals, int64(len(object.data)))
reader, err = s3c.Get(context.Background(), GetOptions{})
reader, _, err = s3c.Get(context.Background(), GetOptions{})
c.Assert(err, checkv1.IsNil)
var buffer bytes.Buffer
{
+2 -1
View File
@@ -49,6 +49,7 @@ type GetOptions struct {
VersionID string
Zip bool
RangeStart int64
Preserve bool
}
// PutOptions holds options for PUT operation
@@ -131,7 +132,7 @@ type Client interface {
Select(ctx context.Context, expression string, sse encrypt.ServerSide, opts SelectObjectOpts) (io.ReadCloser, *probe.Error)
// I/O operations with metadata.
Get(ctx context.Context, opts GetOptions) (reader io.ReadCloser, err *probe.Error)
Get(ctx context.Context, opts GetOptions) (reader io.ReadCloser, content *ClientContent, err *probe.Error)
Put(ctx context.Context, reader io.Reader, size int64, progress io.Reader, opts PutOptions) (n int64, err *probe.Error)
// Object Locking related API
+32 -88
View File
@@ -31,7 +31,6 @@ import (
"time"
"golang.org/x/net/http/httpguts"
"gopkg.in/h2non/filetype.v1"
"github.com/dustin/go-humanize"
"github.com/minio/cli"
@@ -142,7 +141,7 @@ func isAliasURLDir(ctx context.Context, aliasURL string, keys map[string][]prefi
// getSourceStreamMetadataFromURL gets a reader from URL.
func getSourceStreamMetadataFromURL(ctx context.Context, aliasedURL, versionID string, timeRef time.Time, encKeyDB map[string][]prefixSSEPair, zip bool) (reader io.ReadCloser,
metadata map[string]string, err *probe.Error,
content *ClientContent, err *probe.Error,
) {
alias, urlStrFull, _, err := expandAlias(aliasedURL)
if err != nil {
@@ -166,8 +165,7 @@ func getSourceStreamMetadataFromURL(ctx context.Context, aliasedURL, versionID s
type getSourceOpts struct {
GetOptions
fetchStat bool
preserve bool
preserve bool
}
// getSourceStreamFromURL gets a reader from URL.
@@ -181,30 +179,6 @@ func getSourceStreamFromURL(ctx context.Context, urlStr string, encKeyDB map[str
return reader, err
}
func probeContentType(reader io.Reader) (ctype string, err *probe.Error) {
ctype = "application/octet-stream"
// Read a chunk to decide between utf-8 text and binary
if s, ok := reader.(io.Seeker); ok {
var buf [512]byte
n, _ := io.ReadFull(reader, buf[:])
if n <= 0 {
return ctype, nil
}
kind, e := filetype.Match(buf[:n])
if e != nil {
return ctype, probe.NewError(e)
}
// rewind to output whole file
if _, e = s.Seek(0, io.SeekStart); e != nil {
return ctype, probe.NewError(e)
}
if kind.MIME.Value != "" {
ctype = kind.MIME.Value
}
}
return ctype, nil
}
// Verify if reader is a generic ReaderAt
func isReadAt(reader io.Reader) (ok bool) {
var v *os.File
@@ -229,63 +203,18 @@ func isReadAt(reader io.Reader) (ok bool) {
}
// getSourceStream gets a reader from URL.
func getSourceStream(ctx context.Context, alias, urlStr string, opts getSourceOpts) (reader io.ReadCloser, metadata map[string]string, err *probe.Error) {
func getSourceStream(ctx context.Context, alias, urlStr string, opts getSourceOpts) (reader io.ReadCloser, content *ClientContent, err *probe.Error) {
sourceClnt, err := newClientFromAlias(alias, urlStr)
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
}
reader, err = sourceClnt.Get(ctx, opts.GetOptions)
reader, content, err = sourceClnt.Get(ctx, opts.GetOptions)
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
}
metadata = make(map[string]string)
if opts.fetchStat {
var st *ClientContent
mo, mok := reader.(*minio.Object)
if mok {
oinfo, e := mo.Stat()
if e != nil {
return nil, nil, probe.NewError(e).Trace(alias, urlStr)
}
st = &ClientContent{}
st.Time = oinfo.LastModified
st.Size = oinfo.Size
st.ETag = oinfo.ETag
st.Expires = oinfo.Expires
st.Type = os.FileMode(0o664)
st.Metadata = map[string]string{}
for k := range oinfo.Metadata {
st.Metadata[k] = oinfo.Metadata.Get(k)
}
st.ETag = oinfo.ETag
} else {
st, err = sourceClnt.Stat(ctx, StatOptions{preserve: opts.preserve, sse: opts.SSE})
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
}
}
for k, v := range st.Metadata {
if httpguts.ValidHeaderFieldName(k) &&
httpguts.ValidHeaderFieldValue(v) {
metadata[k] = v
}
}
// All unrecognized files have `application/octet-stream`
// So we continue our detection process.
if ctype := metadata["Content-Type"]; ctype == "application/octet-stream" {
// Continue probing content-type if its filesystem stream.
if !mok {
metadata["Content-Type"], err = probeContentType(reader)
if err != nil {
return nil, nil, err.Trace(alias, urlStr)
}
}
}
}
return reader, metadata, nil
return reader, content, nil
}
// putTargetRetention sets retention headers if any
@@ -544,22 +473,37 @@ func uploadSourceToTargetURL(ctx context.Context, uploadOpts uploadSourceToTarge
return uploadOpts.urls.WithError(err.Trace(sourceURL.String()))
}
var reader io.ReadCloser
// Proceed with regular stream copy.
reader, metadata, err = getSourceStream(ctx, sourceAlias, sourceURL.String(), getSourceOpts{
var (
content *ClientContent
reader io.ReadCloser
)
reader, content, err = getSourceStream(ctx, sourceAlias, sourceURL.String(), getSourceOpts{
GetOptions: GetOptions{
VersionID: sourceVersion,
SSE: srcSSE,
Zip: uploadOpts.isZip,
Preserve: uploadOpts.preserve,
},
fetchStat: !uploadOpts.ignoreStat,
preserve: uploadOpts.preserve,
})
if err != nil {
return uploadOpts.urls.WithError(err.Trace(sourceURL.String()))
}
defer reader.Close()
if uploadOpts.updateProgressTotal {
pg, ok := uploadOpts.progress.(*progressBar)
if ok {
pg.SetTotal(content.Size)
}
}
metadata := make(map[string]string, len(content.Metadata))
for k, v := range content.Metadata {
metadata[k] = v
}
// Get metadata from target content as well
for k, v := range uploadOpts.urls.TargetContent.Metadata {
metadata[http.CanonicalHeaderKey(k)] = v
@@ -679,11 +623,11 @@ func ParseForm(r *http.Request) error {
}
type uploadSourceToTargetURLOpts struct {
urls URLs
progress io.Reader
encKeyDB map[string][]prefixSSEPair
preserve, isZip bool
multipartSize string
multipartThreads string
ignoreStat bool
urls URLs
progress io.Reader
encKeyDB map[string][]prefixSSEPair
preserve, isZip bool
multipartSize string
multipartThreads string
updateProgressTotal bool
}
+19 -3
View File
@@ -266,7 +266,16 @@ func doCopy(ctx context.Context, copyOpts doCopyOpts) URLs {
})
}
urls := uploadSourceToTargetURL(ctx, uploadSourceToTargetURLOpts{urls: copyOpts.cpURLs, progress: copyOpts.pg, encKeyDB: copyOpts.encKeyDB, preserve: copyOpts.preserve, isZip: copyOpts.isZip, multipartSize: copyOpts.multipartSize, multipartThreads: copyOpts.multipartThreads, ignoreStat: copyOpts.ignoreStat})
urls := uploadSourceToTargetURL(ctx, uploadSourceToTargetURLOpts{
urls: copyOpts.cpURLs,
progress: copyOpts.pg,
encKeyDB: copyOpts.encKeyDB,
preserve: copyOpts.preserve,
isZip: copyOpts.isZip,
multipartSize: copyOpts.multipartSize,
multipartThreads: copyOpts.multipartThreads,
updateProgressTotal: copyOpts.updateProgressTotal,
})
if copyOpts.isMvCmd && urls.Error == nil {
rmManager.add(ctx, sourceAlias, sourceURL.String())
}
@@ -562,7 +571,14 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
startContinue = false
}
parallel.queueTask(func() URLs {
return doCopy(ctx, doCopyOpts{cpURLs: cpURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: isMvCmd, preserve: preserve, isZip: isZip})
return doCopy(ctx, doCopyOpts{
cpURLs: cpURLs,
pg: pg,
encKeyDB: encKeyDB,
isMvCmd: isMvCmd,
preserve: preserve,
isZip: isZip,
})
}, cpURLs.SourceContent.Size)
}
}
@@ -764,7 +780,7 @@ type doCopyOpts struct {
pg ProgressReader
encKeyDB map[string][]prefixSSEPair
isMvCmd, preserve, isZip bool
updateProgressTotal bool
multipartSize string
multipartThreads string
ignoreStat bool
}
+8 -4
View File
@@ -66,9 +66,10 @@ func mainGet(cliCtx *cli.Context) (e error) {
fatalIf(err, "Unable to parse encryption keys.")
args := cliCtx.Args()
if len(args) < 2 {
if len(args) != 2 {
showCommandHelpAndExit(cliCtx, 1) // last argument is exit code.
}
// get source and target
sourceURLs := args[:len(args)-1]
targetURL := args[len(args)-1]
@@ -97,8 +98,6 @@ func mainGet(cliCtx *cli.Context) (e error) {
getURLsCh <- getURLs
break
}
totalBytes += getURLs.SourceContent.Size
pg.SetTotal(totalBytes)
totalObjects++
getURLsCh <- getURLs
}
@@ -119,7 +118,12 @@ func mainGet(cliCtx *cli.Context) (e error) {
showLastProgressBar(pg, getURLs.Error.ToGoError())
return
}
urls := doCopy(ctx, doCopyOpts{cpURLs: getURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: false, preserve: false, isZip: false, ignoreStat: true})
urls := doCopy(ctx, doCopyOpts{
cpURLs: getURLs,
pg: pg,
encKeyDB: encKeyDB,
updateProgressTotal: true,
})
if urls.Error != nil {
e = urls.Error.ToGoError()
showLastProgressBar(pg, e)
+4 -3
View File
@@ -99,11 +99,12 @@ func headURL(sourceURL, sourceVersion string, timeRef time.Time, encKeyDB map[st
reader = os.Stdin
default:
var err *probe.Error
var metadata map[string]string
if reader, metadata, err = getSourceStreamMetadataFromURL(context.Background(), sourceURL, sourceVersion, timeRef, encKeyDB, zip); err != nil {
var content *ClientContent
if reader, content, err = getSourceStreamMetadataFromURL(context.Background(), sourceURL, sourceVersion, timeRef, encKeyDB, zip); err != nil {
return err.Trace(sourceURL)
}
ctype := metadata["Content-Type"]
ctype := content.Metadata["Content-Type"]
if strings.Contains(ctype, "gzip") {
var e error
reader, e = gzip.NewReader(reader)
+7 -7
View File
@@ -35,7 +35,7 @@ type progressBar struct {
func newPB(total int64) *pb.ProgressBar {
// Progress bar specific theme customization.
console.SetColor("Bar", color.New(color.FgYellow, color.Bold))
console.SetColor("Bar", color.New(color.FgGreen, color.Bold))
// get the new original progress bar.
bar := pb.New64(total)
@@ -59,18 +59,19 @@ func newPB(total int64) *pb.ProgressBar {
// Use different unicodes for Linux, OS X and Windows.
switch runtime.GOOS {
case "linux", "darwin":
case "linux":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format("━\x00━\x00━\x00┉\x00━")
bar.Format("┃\x00▓\x00█\x00░\x00┃")
case "darwin":
// Need to add '\x00' as delimiter for unicode characters.
bar.Format(" \x00▓\x00 \x00░\x00 ")
default:
// Default to non unicode characters.
bar.Format("[=> ]")
}
// Start the progress bar.
bar.Start()
return bar
return bar.Start()
}
func newProgressReader(r io.Reader, caption string, total int64) *pb.Reader {
@@ -99,7 +100,6 @@ func (p *progressBar) SetCaption(caption string) *progressBar {
}
func (p *progressBar) Finish() {
console.SetColor("Bar", color.New(color.FgGreen, color.Bold))
p.ProgressBar.Finish()
}
+7 -1
View File
@@ -156,7 +156,13 @@ func mainPut(cliCtx *cli.Context) (e error) {
showLastProgressBar(pg, putURLs.Error.ToGoError())
return
}
urls := doCopy(ctx, doCopyOpts{cpURLs: putURLs, pg: pg, encKeyDB: encKeyDB, isMvCmd: false, preserve: false, isZip: false, multipartSize: size, multipartThreads: strconv.Itoa(threads)})
urls := doCopy(ctx, doCopyOpts{
cpURLs: putURLs,
pg: pg,
encKeyDB: encKeyDB,
multipartSize: size,
multipartThreads: strconv.Itoa(threads),
})
if urls.Error != nil {
e = urls.Error.ToGoError()
showLastProgressBar(pg, e)
+4 -3
View File
@@ -308,11 +308,12 @@ func getCSVHeader(sourceURL string, encKeyDB map[string][]prefixSSEPair) ([]stri
r = os.Stdin
default:
var err *probe.Error
var metadata map[string]string
if r, metadata, err = getSourceStreamMetadataFromURL(globalContext, sourceURL, "", time.Time{}, encKeyDB, false); err != nil {
var content *ClientContent
if r, content, err = getSourceStreamMetadataFromURL(globalContext, sourceURL, "", time.Time{}, encKeyDB, false); err != nil {
return nil, err.Trace(sourceURL)
}
ctype := metadata["Content-Type"]
ctype := content.Metadata["Content-Type"]
if strings.Contains(ctype, "gzip") {
var e error
r, e = gzip.NewReader(r)