From 38aec4d875b133f28792ad7736ee7b62ff603aca Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 20 Nov 2016 14:46:24 -0800 Subject: [PATCH] mirror and cp should print size in --json mode. (#1890) ```sh mc cp -r --json /usr/bin /tmp | jq . ... ... { "status": "success", "source": "/usr/bin/gcm-viewer", "target": "/tmp/bin/gcm-viewer", "size": 87128, "totalCount": 2602, "totalSize": 677422730 } ``` This allows scripts to build progress around this data. --- cmd/cp-main.go | 33 +++++++++++++++++++-------------- cmd/difference.go | 18 ++++-------------- cmd/mirror-main.go | 37 +++++++++++++++++++++---------------- cmd/session-migrate.go | 2 +- cmd/session-v8.go | 2 +- cmd/urls.go | 2 ++ cmd/utils.go | 15 +++++++++++++++ 7 files changed, 63 insertions(+), 46 deletions(-) diff --git a/cmd/cp-main.go b/cmd/cp-main.go index dfda2e45..42c47428 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -86,9 +86,12 @@ EXAMPLES: // copyMessage container for file copy messages type copyMessage struct { - Status string `json:"status"` - Source string `json:"source"` - Target string `json:"target"` + Status string `json:"status"` + Source string `json:"source"` + Target string `json:"target"` + Size int64 `json:"size"` + TotalCount int64 `json:"totalCount"` + TotalSize int64 `json:"totalSize"` } // String colorized copy message @@ -152,8 +155,11 @@ func doCopy(cpURLs URLs, progressReader *progressBar, accountingReader *accounte sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) targetPath := filepath.ToSlash(filepath.Join(targetAlias, targetURL.Path)) printMsg(copyMessage{ - Source: sourcePath, - Target: targetPath, + Source: sourcePath, + Target: targetPath, + Size: length, + TotalCount: cpURLs.TotalCount, + TotalSize: cpURLs.TotalSize, }) // Proxy reader to accounting reader only during quiet mode. if globalQuiet || globalJSON { @@ -228,7 +234,7 @@ func doPrepareCopyURLs(session *sessionV8, trapCh <-chan bool) { targetURL := session.Header.CommandArgs[len(session.Header.CommandArgs)-1] // Last one is target var totalBytes int64 - var totalObjects int + var totalObjects int64 // Access recursive flag inside the session header. isRecursive := session.Header.CommandBoolFlags["recursive"] @@ -345,14 +351,7 @@ func doCopySession(session *sessionV8) { } errorIf(cpURLs.Error.Trace(cpURLs.SourceContent.URL.String()), fmt.Sprintf("Failed to copy ā€˜%s’.", cpURLs.SourceContent.URL.String())) - // For all non critical errors we can continue for the - // remaining files. - switch cpURLs.Error.ToGoError().(type) { - // Handle this specifically for filesystem related errors. - case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: - continue - // Handle these specifically for object storage related errors. - case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists, BucketDoesNotExist, BucketInvalid, ObjectOnGlacier: + if isErrIgnored(cpURLs.Error) { continue } // For critical errors we should exit. Session @@ -370,6 +369,12 @@ func doCopySession(session *sessionV8) { // Unmarshal copyURLs from each line. json.Unmarshal([]byte(urlScanner.Text()), &cpURLs) + // Save total count. + cpURLs.TotalCount = session.Header.TotalObjects + + // Save totalSize. + cpURLs.TotalSize = session.Header.TotalBytes + // Verify if previously copied, notify progress bar. if isCopied(cpURLs.SourceContent.URL.String()) { statusCh <- doCopyFake(cpURLs, progressReader) diff --git a/cmd/difference.go b/cmd/difference.go index 5896a535..e5377fda 100644 --- a/cmd/difference.go +++ b/cmd/difference.go @@ -82,14 +82,9 @@ func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string } if !srcEOF && srcCtnt.Err != nil { - switch srcCtnt.Err.ToGoError().(type) { - // Handle this specifically for filesystem related errors. - case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: + if isErrIgnored(srcCtnt.Err) { errorIf(srcCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", sourceURL)) - // Handle these specifically for object storage related errors. - case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists, BucketDoesNotExist, BucketInvalid, ObjectOnGlacier: - errorIf(srcCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", sourceURL)) - default: + } else { fatalIf(srcCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", sourceURL)) } srcCtnt, srcOk = <-srcCh @@ -97,14 +92,9 @@ func objectDifference(sourceClnt, targetClnt Client, sourceURL, targetURL string } if !tgtEOF && tgtCtnt.Err != nil { - switch tgtCtnt.Err.ToGoError().(type) { - // Handle this specifically for filesystem related errors. - case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: + if isErrIgnored(tgtCtnt.Err) { errorIf(tgtCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", targetURL)) - // Handle these specifically for object storage related errors. - case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists, BucketDoesNotExist, BucketInvalid, ObjectOnGlacier: - errorIf(tgtCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", targetURL)) - default: + } else { fatalIf(tgtCtnt.Err.Trace(sourceURL, targetURL), fmt.Sprintf("Failed on '%s'", targetURL)) } tgtCtnt, tgtOk = <-tgtCh diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index ac68c633..447cecc4 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -140,9 +140,12 @@ type mirrorSession struct { // mirrorMessage container for file mirror messages type mirrorMessage struct { - Status string `json:"status"` - Source string `json:"source"` - Target string `json:"target"` + Status string `json:"status"` + Source string `json:"source"` + Target string `json:"target"` + Size int64 `json:"size"` + TotalCount int64 `json:"totalCount"` + TotalSize int64 `json:"totalSize"` } // String colorized mirror message @@ -223,8 +226,11 @@ func (ms *mirrorSession) doMirror(sURLs URLs) URLs { sourcePath := filepath.ToSlash(filepath.Join(sourceAlias, sourceURL.Path)) targetPath := filepath.ToSlash(filepath.Join(targetAlias, targetURL.Path)) ms.status.PrintMsg(mirrorMessage{ - Source: sourcePath, - Target: targetPath, + Source: sourcePath, + Target: targetPath, + Size: length, + TotalCount: sURLs.TotalCount, + TotalSize: sURLs.TotalSize, }) // If source size is <= 5GB and operation is across same server type try to use Copy. @@ -291,16 +297,8 @@ func (ms *mirrorSession) startStatus() { fmt.Sprintf("Failed to remove ā€˜%s’.", sURLs.TargetContent.URL.String())) } - // For all non critical errors we can continue for the - // remaining files. - switch sURLs.Error.ToGoError().(type) { - // Handle this specifically for filesystem related errors. - case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: - continue - // Handle these specifically for object storage related errors. - case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists: - continue - case ObjectAlreadyExistsAsDirectory, BucketDoesNotExist, BucketInvalid, ObjectOnGlacier: + // For all non critical errors we can continue for the remaining files. + if isErrIgnored(sURLs.Error) { continue } @@ -531,6 +529,13 @@ func (ms *mirrorSession) harvestSessionUrls() { continue } + // Save total count. + urls.TotalCount = ms.Header.TotalObjects + + // Save totalSize. + urls.TotalSize = ms.Header.TotalBytes + + // Send harvested urls. ms.harvestCh <- urls } @@ -564,7 +569,7 @@ func (ms *mirrorSession) harvest(recursive bool) { } var totalBytes int64 - var totalObjects int + var totalObjects int64 loop: for { diff --git a/cmd/session-migrate.go b/cmd/session-migrate.go index 555ac913..a3a9f5b2 100644 --- a/cmd/session-migrate.go +++ b/cmd/session-migrate.go @@ -62,7 +62,7 @@ func migrateSessionV7ToV8() { sV8Header.LastCopied = sV7.Header.LastCopied sV8Header.LastRemoved = sV7.Header.LastRemoved sV8Header.TotalBytes = sV7.Header.TotalBytes - sV8Header.TotalObjects = sV7.Header.TotalObjects + sV8Header.TotalObjects = int64(sV7.Header.TotalObjects) // Add insecure flag to the new V8 header sV8Header.GlobalBoolFlags["insecure"] = false diff --git a/cmd/session-v8.go b/cmd/session-v8.go index 1b16c2c3..1a38f158 100644 --- a/cmd/session-v8.go +++ b/cmd/session-v8.go @@ -48,7 +48,7 @@ type sessionV8Header struct { LastCopied string `json:"lastCopied"` LastRemoved string `json:"lastRemoved"` TotalBytes int64 `json:"totalBytes"` - TotalObjects int `json:"totalObjects"` + TotalObjects int64 `json:"totalObjects"` } // sessionMessage container for session messages diff --git a/cmd/urls.go b/cmd/urls.go index de2646cd..196d17ea 100644 --- a/cmd/urls.go +++ b/cmd/urls.go @@ -24,6 +24,8 @@ type URLs struct { SourceContent *clientContent TargetAlias string TargetContent *clientContent + TotalCount int64 + TotalSize int64 Error *probe.Error `json:"-"` } diff --git a/cmd/utils.go b/cmd/utils.go index 4401763d..a71770d7 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -25,8 +25,23 @@ import ( "time" "github.com/minio/mc/pkg/console" + "github.com/minio/minio/pkg/probe" ) +func isErrIgnored(err *probe.Error) bool { + // For all non critical errors we can continue for the remaining files. + switch err.ToGoError().(type) { + case BrokenSymlink, TooManyLevelsSymlink, PathNotFound, PathInsufficientPermission: + return true + // Handle these specifically for object storage related errors. + case BucketNameEmpty, ObjectMissing, ObjectAlreadyExists: + return true + case ObjectAlreadyExistsAsDirectory, BucketDoesNotExist, BucketInvalid, ObjectOnGlacier: + return true + } + return false +} + var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") // newRandomID generates a random id of regular lower case and uppercase english characters.