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.
This commit is contained in:
Harshavardhana
2016-11-20 14:46:24 -08:00
committed by GitHub
parent 23f0a27acc
commit 38aec4d875
7 changed files with 63 additions and 46 deletions
+19 -14
View File
@@ -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)
+4 -14
View File
@@ -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
+21 -16
View File
@@ -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 {
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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
+2
View File
@@ -24,6 +24,8 @@ type URLs struct {
SourceContent *clientContent
TargetAlias string
TargetContent *clientContent
TotalCount int64
TotalSize int64
Error *probe.Error `json:"-"`
}
+15
View File
@@ -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.