Merge pull request #1687 from harshavardhana/mirror-remove
mirror: Implement '--remove' feature.
This commit is contained in:
+2
-1
@@ -16,7 +16,8 @@ go:
|
||||
- 1.6
|
||||
|
||||
script:
|
||||
- make
|
||||
- make test
|
||||
- make test GOFLAGS="-race"
|
||||
- diff -au <(gofmt -d *.go) <(printf "")
|
||||
- diff -au <(gofmt -d pkg) <(printf "")
|
||||
|
||||
|
||||
+4
-4
@@ -182,7 +182,7 @@ func doCopyFake(cpURLs copyURLs, progressReader *progressBar) copyURLs {
|
||||
}
|
||||
|
||||
// doPrepareCopyURLs scans the source URL and prepares a list of objects for copying.
|
||||
func doPrepareCopyURLs(session *sessionV6, trapCh <-chan bool) {
|
||||
func doPrepareCopyURLs(session *sessionV7, trapCh <-chan bool) {
|
||||
// Separate source and target. 'cp' can take only one target,
|
||||
// but any number of sources.
|
||||
sourceURLs := session.Header.CommandArgs[:len(session.Header.CommandArgs)-1]
|
||||
@@ -250,7 +250,7 @@ func doPrepareCopyURLs(session *sessionV6, trapCh <-chan bool) {
|
||||
session.Save()
|
||||
}
|
||||
|
||||
func doCopySession(session *sessionV6) {
|
||||
func doCopySession(session *sessionV7) {
|
||||
trapCh := signalTrap(os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
if !session.HasData() {
|
||||
@@ -264,7 +264,7 @@ func doCopySession(session *sessionV6) {
|
||||
urlScanner := bufio.NewScanner(session.NewDataReader())
|
||||
// isCopied returns true if an object has been already copied
|
||||
// or not. This is useful when we resume from a session.
|
||||
isCopied := isCopiedFactory(session.Header.LastCopied)
|
||||
isCopied := isLastFactory(session.Header.LastCopied)
|
||||
|
||||
// Enable progress bar reader only during default mode.
|
||||
var progressReader *progressBar
|
||||
@@ -379,7 +379,7 @@ func mainCopy(ctx *cli.Context) {
|
||||
// Additional command speific theme customization.
|
||||
console.SetColor("Copy", color.New(color.FgGreen, color.Bold))
|
||||
|
||||
session := newSessionV6()
|
||||
session := newSessionV7()
|
||||
session.Header.CommandType = "cp"
|
||||
session.Header.CommandBoolFlags["recursive"] = ctx.Bool("recursive")
|
||||
|
||||
|
||||
+88
-18
@@ -48,6 +48,10 @@ var (
|
||||
Name: "fake",
|
||||
Usage: "Perform a fake mirror operation.",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "remove",
|
||||
Usage: "Remove extraneous file(s) on target.",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -82,6 +86,9 @@ EXAMPLES:
|
||||
5. Fake mirror a bucket from Minio cloud storage to a bucket on Amazon S3 cloud storage.
|
||||
$ mc {{.Name}} --fake play/photos/2014 s3/backup-photos/2014
|
||||
|
||||
6. Mirror a bucket from Minio cloud storage to a bucket on Amazon S3 cloud storage and remove any extraneous
|
||||
files on Amazon S3 cloud storage. NOTE: '--remove' is only supported with '--force'.
|
||||
$ mc {{.Name}} --force --remove play/photos/2014 s3/backup-photos/2014
|
||||
`,
|
||||
}
|
||||
|
||||
@@ -126,6 +133,25 @@ func (c mirrorStatMessage) String() string {
|
||||
return message
|
||||
}
|
||||
|
||||
// doRemove - removes files on target.
|
||||
func doRemove(sURLs mirrorURLs, fakeRemove bool) mirrorURLs {
|
||||
targetAlias := sURLs.TargetAlias
|
||||
targetURL := sURLs.TargetContent.URL
|
||||
|
||||
// We are not removing incomplete uploads.
|
||||
isIncomplete := false
|
||||
|
||||
// Remove extraneous file on target.
|
||||
err := rm(targetAlias, targetURL.String(), isIncomplete, fakeRemove)
|
||||
if err != nil {
|
||||
sURLs.Error = err.Trace(targetAlias, targetURL.String())
|
||||
return sURLs
|
||||
}
|
||||
|
||||
sURLs.Error = nil // just for safety
|
||||
return sURLs
|
||||
}
|
||||
|
||||
// doMirror - Mirror an object to multiple destination. mirrorURLs status contains a copy of sURLs and error if any.
|
||||
func doMirror(sURLs mirrorURLs, progressReader *progressBar, accountingReader *accounter, fakeMirror bool) mirrorURLs {
|
||||
if sURLs.Error != nil { // Errorneous sURLs passed.
|
||||
@@ -188,7 +214,7 @@ func doMirror(sURLs mirrorURLs, progressReader *progressBar, accountingReader *a
|
||||
}
|
||||
|
||||
// doPrepareMirrorURLs scans the source URL and prepares a list of objects for mirroring.
|
||||
func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh <-chan bool) {
|
||||
func doPrepareMirrorURLs(session *sessionV7, isForce bool, isFake bool, isRemove bool, trapCh <-chan bool) {
|
||||
sourceURL := session.Header.CommandArgs[0] // first one is source.
|
||||
targetURL := session.Header.CommandArgs[1]
|
||||
var totalBytes int64
|
||||
@@ -202,7 +228,7 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh <
|
||||
scanBar = scanBarFactory()
|
||||
}
|
||||
|
||||
URLsCh := prepareMirrorURLs(sourceURL, targetURL, isForce, isFake)
|
||||
URLsCh := prepareMirrorURLs(sourceURL, targetURL, isForce, isFake, isRemove)
|
||||
done := false
|
||||
for !done {
|
||||
select {
|
||||
@@ -229,10 +255,18 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh <
|
||||
}
|
||||
fmt.Fprintln(dataFP, string(jsonData))
|
||||
if !globalQuiet && !globalJSON {
|
||||
scanBar(sURLs.SourceContent.URL.String())
|
||||
// Source content is empty if removal is requested,
|
||||
// put targetContent on to scan bar.
|
||||
if sURLs.SourceContent != nil {
|
||||
scanBar(sURLs.SourceContent.URL.String())
|
||||
} else if sURLs.TargetContent != nil && isRemove {
|
||||
scanBar(sURLs.TargetContent.URL.String())
|
||||
}
|
||||
}
|
||||
// Remember totalBytes only for mirror not for removal,
|
||||
if sURLs.SourceContent != nil {
|
||||
totalBytes += sURLs.SourceContent.Size
|
||||
}
|
||||
|
||||
totalBytes += sURLs.SourceContent.Size
|
||||
totalObjects++
|
||||
case <-trapCh:
|
||||
// Print in new line and adjust to top so that we don't print over the ongoing scan bar
|
||||
@@ -249,15 +283,16 @@ func doPrepareMirrorURLs(session *sessionV6, isForce bool, isFake bool, trapCh <
|
||||
}
|
||||
|
||||
// Session'fied mirror command.
|
||||
func doMirrorSession(session *sessionV6) {
|
||||
func doMirrorSession(session *sessionV7) {
|
||||
isForce := session.Header.CommandBoolFlags["force"]
|
||||
isFake := session.Header.CommandBoolFlags["fake"]
|
||||
isRemove := session.Header.CommandBoolFlags["remove"]
|
||||
|
||||
// Initialize signal trap.
|
||||
trapCh := signalTrap(os.Interrupt, syscall.SIGTERM)
|
||||
|
||||
if !session.HasData() {
|
||||
doPrepareMirrorURLs(session, isForce, isFake, trapCh)
|
||||
doPrepareMirrorURLs(session, isForce, isFake, isRemove, trapCh)
|
||||
}
|
||||
|
||||
// Enable accounting reader by default.
|
||||
@@ -274,7 +309,11 @@ func doMirrorSession(session *sessionV6) {
|
||||
|
||||
// isCopied returns true if an object has been already copied
|
||||
// or not. This is useful when we resume from a session.
|
||||
isCopied := isCopiedFactory(session.Header.LastCopied)
|
||||
isCopied := isLastFactory(session.Header.LastCopied)
|
||||
|
||||
// isRemoved returns true if an object has been already removed or
|
||||
// not. This is useful when we resume from a session.
|
||||
isRemoved := isLastFactory(session.Header.LastRemoved)
|
||||
|
||||
// Wait on status of doMirror() operation.
|
||||
var statusCh = make(chan mirrorURLs)
|
||||
@@ -300,16 +339,37 @@ func doMirrorSession(session *sessionV6) {
|
||||
return
|
||||
}
|
||||
if sURLs.Error == nil {
|
||||
session.Header.LastCopied = sURLs.SourceContent.URL.String()
|
||||
session.Save()
|
||||
if sURLs.SourceContent != nil {
|
||||
session.Header.LastCopied = sURLs.SourceContent.URL.String()
|
||||
session.Save()
|
||||
} else if sURLs.TargetContent != nil && isRemove {
|
||||
session.Header.LastRemoved = sURLs.TargetContent.URL.String()
|
||||
session.Save()
|
||||
|
||||
// Construct user facing message and path.
|
||||
targetPath := filepath.ToSlash(filepath.Join(sURLs.TargetAlias, sURLs.TargetContent.URL.Path))
|
||||
if !globalQuiet && !globalJSON {
|
||||
console.Eraseline()
|
||||
}
|
||||
printMsg(rmMessage{
|
||||
Status: "success",
|
||||
URL: targetPath,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// Print in new line and adjust to top so that we
|
||||
// don't print over the ongoing progress bar.
|
||||
if !globalQuiet && !globalJSON {
|
||||
console.Eraseline()
|
||||
}
|
||||
errorIf(sURLs.Error.Trace(sURLs.SourceContent.URL.String()),
|
||||
fmt.Sprintf("Failed to copy ‘%s’.", sURLs.SourceContent.URL.String()))
|
||||
if sURLs.SourceContent != nil {
|
||||
errorIf(sURLs.Error.Trace(sURLs.SourceContent.URL.String()),
|
||||
fmt.Sprintf("Failed to copy ‘%s’.", sURLs.SourceContent.URL.String()))
|
||||
} else {
|
||||
// When sURLs.SourceContent is nil, we know that we have an error related to removing
|
||||
errorIf(sURLs.Error.Trace(sURLs.TargetContent.URL.String()),
|
||||
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) {
|
||||
@@ -331,13 +391,21 @@ func doMirrorSession(session *sessionV6) {
|
||||
// Loop through all urls and mirror.
|
||||
for urlScanner.Scan() {
|
||||
var sURLs mirrorURLs
|
||||
|
||||
// Unmarshal copyURLs from each line.
|
||||
json.Unmarshal([]byte(urlScanner.Text()), &sURLs)
|
||||
// Verify if previously copied or if its a fake mirror, set
|
||||
// fake mirror accordingly.
|
||||
fakeMirror := isCopied(sURLs.SourceContent.URL.String()) || isFake
|
||||
// Perform mirror operation.
|
||||
statusCh <- doMirror(sURLs, progressReader, accntReader, fakeMirror)
|
||||
|
||||
if sURLs.SourceContent != nil {
|
||||
// Verify if previously copied or if its a fake mirror, set
|
||||
// fake mirror accordingly.
|
||||
fakeMirror := isCopied(sURLs.SourceContent.URL.String()) || isFake
|
||||
// Perform mirror operation.
|
||||
statusCh <- doMirror(sURLs, progressReader, accntReader, fakeMirror)
|
||||
} else if sURLs.TargetContent != nil && isRemove {
|
||||
fakeRemove := isRemoved(sURLs.TargetContent.URL.String()) || isFake
|
||||
// Perform remove operation.
|
||||
statusCh <- doRemove(sURLs, fakeRemove)
|
||||
}
|
||||
}
|
||||
|
||||
// Close the goroutine.
|
||||
@@ -373,7 +441,7 @@ func mainMirror(ctx *cli.Context) {
|
||||
console.SetColor("Mirror", color.New(color.FgGreen, color.Bold))
|
||||
|
||||
var e error
|
||||
session := newSessionV6()
|
||||
session := newSessionV7()
|
||||
session.Header.CommandType = "mirror"
|
||||
session.Header.RootPath, e = os.Getwd()
|
||||
if e != nil {
|
||||
@@ -384,8 +452,10 @@ func mainMirror(ctx *cli.Context) {
|
||||
// Set command flags from context.
|
||||
isForce := ctx.Bool("force")
|
||||
isFake := ctx.Bool("fake")
|
||||
isRemove := ctx.Bool("remove")
|
||||
session.Header.CommandBoolFlags["force"] = isForce
|
||||
session.Header.CommandBoolFlags["fake"] = isFake
|
||||
session.Header.CommandBoolFlags["remove"] = isRemove
|
||||
|
||||
// extract URLs.
|
||||
session.Header.CommandArgs = ctx.Args()
|
||||
|
||||
+35
-11
@@ -36,8 +36,11 @@ func (m mirrorURLs) isEmpty() bool {
|
||||
if m.SourceContent == nil && m.TargetContent == nil && m.Error == nil {
|
||||
return true
|
||||
}
|
||||
if m.SourceContent.Size == 0 && m.TargetContent == nil && m.Error == nil {
|
||||
return true
|
||||
// If remove flag is set then sourceContent is usually nil.
|
||||
if m.SourceContent != nil {
|
||||
if m.SourceContent.Size == 0 && m.TargetContent == nil && m.Error == nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -90,7 +93,7 @@ func checkMirrorSyntax(ctx *cli.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, mirrorURLsCh chan<- mirrorURLs) {
|
||||
func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool, mirrorURLsCh chan<- mirrorURLs) {
|
||||
// source and targets are always directories
|
||||
sourceSeparator := string(newClientURL(sourceURL).Separator)
|
||||
if !strings.HasSuffix(sourceURL, sourceSeparator) {
|
||||
@@ -121,13 +124,14 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake
|
||||
|
||||
// List both source and target, compare and return values through channel.
|
||||
for diffMsg := range objectDifference(sourceClnt, targetClnt, sourceURL, targetURL) {
|
||||
if diffMsg.Diff == differInNone {
|
||||
switch diffMsg.Diff {
|
||||
case differInNone:
|
||||
// No difference, continue.
|
||||
continue
|
||||
} else if diffMsg.Diff == differInType {
|
||||
case differInType:
|
||||
mirrorURLsCh <- mirrorURLs{Error: errInvalidTarget(diffMsg.SecondURL)}
|
||||
continue
|
||||
} else if diffMsg.Diff == differInSize {
|
||||
case differInSize:
|
||||
if !isForce && !isFake {
|
||||
// Size differs and force not set
|
||||
mirrorURLsCh <- mirrorURLs{Error: errOverWriteNotAllowed(diffMsg.SecondURL)}
|
||||
@@ -145,7 +149,7 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake
|
||||
TargetContent: targetContent,
|
||||
}
|
||||
continue
|
||||
} else if diffMsg.Diff == differInFirst {
|
||||
case differInFirst:
|
||||
sourceSuffix := strings.TrimPrefix(diffMsg.FirstURL, sourceURL)
|
||||
// Either available only in source or size differs and force is set
|
||||
targetPath := urlJoinPath(targetURL, sourceSuffix)
|
||||
@@ -157,13 +161,33 @@ func deltaSourceTarget(sourceURL string, targetURL string, isForce bool, isFake
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: targetContent,
|
||||
}
|
||||
} // else if diffMsg.Diff == differInSecond {
|
||||
// Add for bi-directional mirror.
|
||||
case differInSecond:
|
||||
if isRemove {
|
||||
if !isForce && !isFake {
|
||||
// Object removal not allowed if force is not set.
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
Error: errDeleteNotAllowed(diffMsg.SecondURL),
|
||||
}
|
||||
continue
|
||||
}
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: diffMsg.secondContent,
|
||||
}
|
||||
}
|
||||
continue
|
||||
default:
|
||||
mirrorURLsCh <- mirrorURLs{
|
||||
Error: errUnrecognizedDiffType(diffMsg.Diff).Trace(diffMsg.FirstURL, diffMsg.SecondURL),
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool) <-chan mirrorURLs {
|
||||
// Prepares urls that need to be copied or removed based on requested options.
|
||||
func prepareMirrorURLs(sourceURL string, targetURL string, isForce bool, isFake bool, isRemove bool) <-chan mirrorURLs {
|
||||
mirrorURLsCh := make(chan mirrorURLs)
|
||||
go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, mirrorURLsCh)
|
||||
go deltaSourceTarget(sourceURL, targetURL, isForce, isFake, isRemove, mirrorURLsCh)
|
||||
return mirrorURLsCh
|
||||
}
|
||||
|
||||
+7
-7
@@ -77,7 +77,7 @@ EXAMPLES:
|
||||
}
|
||||
|
||||
// bySessionWhen is a type for sorting session metadata by time.
|
||||
type bySessionWhen []*sessionV6
|
||||
type bySessionWhen []*sessionV7
|
||||
|
||||
func (b bySessionWhen) Len() int { return len(b) }
|
||||
func (b bySessionWhen) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
|
||||
@@ -85,9 +85,9 @@ func (b bySessionWhen) Less(i, j int) bool { return b[i].Header.When.Before(b[j]
|
||||
|
||||
// listSessions list all current sessions.
|
||||
func listSessions() *probe.Error {
|
||||
var bySessions []*sessionV6
|
||||
var bySessions []*sessionV7
|
||||
for _, sid := range getSessionIDs() {
|
||||
s, err := loadSessionV6(sid)
|
||||
s, err := loadSessionV7(sid)
|
||||
if err != nil {
|
||||
return err.Trace(sid)
|
||||
}
|
||||
@@ -125,7 +125,7 @@ func (c clearSessionMessage) JSON() string {
|
||||
func clearSession(sid string) {
|
||||
if sid == "all" {
|
||||
for _, sid := range getSessionIDs() {
|
||||
session, err := loadSessionV6(sid)
|
||||
session, err := loadSessionV7(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.")
|
||||
|
||||
fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.")
|
||||
@@ -139,7 +139,7 @@ func clearSession(sid string) {
|
||||
fatalIf(errDummy().Trace(sid), "Session ‘"+sid+"’ not found.")
|
||||
}
|
||||
|
||||
session, err := loadSessionV6(sid)
|
||||
session, err := loadSessionV7(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.")
|
||||
|
||||
if session != nil {
|
||||
@@ -148,7 +148,7 @@ func clearSession(sid string) {
|
||||
}
|
||||
}
|
||||
|
||||
func sessionExecute(s *sessionV6) {
|
||||
func sessionExecute(s *sessionV7) {
|
||||
switch s.Header.CommandType {
|
||||
case "cp":
|
||||
doCopySession(s)
|
||||
@@ -230,7 +230,7 @@ func mainSession(ctx *cli.Context) {
|
||||
}
|
||||
fatalIf(errDummy().Trace(sid), errorMsg)
|
||||
}
|
||||
s, err := loadSessionV6(sid)
|
||||
s, err := loadSessionV7(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load session.")
|
||||
|
||||
// Restore the state of global variables from this previous session.
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Minio Client (C) 2016 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/minio/mc/pkg/console"
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
|
||||
// Migrates session header version '6' to '7'. Only change is
|
||||
// LastRemoved field which was added in version '7'.
|
||||
func migrateSessionV6ToV7() {
|
||||
for _, sid := range getSessionIDs() {
|
||||
sV6Header, err := loadSessionV6Header(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load version ‘6’. Migration failed please report this issue at https://github.com/minio/mc/issues.")
|
||||
if sV6Header.Version == "7" { // It is new format.
|
||||
continue
|
||||
}
|
||||
|
||||
sessionFile, err := getSessionFile(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to get session file.")
|
||||
|
||||
// Initialize v7 header and migrate to new config.
|
||||
sV7Header := &sessionV7Header{}
|
||||
sV7Header.Version = "7"
|
||||
sV7Header.When = sV6Header.When
|
||||
sV7Header.RootPath = sV6Header.RootPath
|
||||
sV7Header.GlobalBoolFlags = sV6Header.GlobalBoolFlags
|
||||
sV7Header.GlobalIntFlags = sV6Header.GlobalIntFlags
|
||||
sV7Header.GlobalStringFlags = sV6Header.GlobalStringFlags
|
||||
sV7Header.CommandType = sV6Header.CommandType
|
||||
sV7Header.CommandArgs = sV6Header.CommandArgs
|
||||
sV7Header.CommandBoolFlags = sV6Header.CommandBoolFlags
|
||||
sV7Header.CommandIntFlags = sV6Header.CommandIntFlags
|
||||
sV7Header.CommandStringFlags = sV6Header.CommandStringFlags
|
||||
sV7Header.LastCopied = sV6Header.LastCopied
|
||||
sV7Header.LastRemoved = ""
|
||||
sV7Header.TotalBytes = sV6Header.TotalBytes
|
||||
sV7Header.TotalObjects = sV6Header.TotalObjects
|
||||
|
||||
qs, err := quick.New(sV7Header)
|
||||
fatalIf(err.Trace(sid), "Unable to initialize quick config for session '7' header.")
|
||||
|
||||
err = qs.Save(sessionFile)
|
||||
fatalIf(err.Trace(sid, sessionFile), "Unable to migrate session from '6' to '7'.")
|
||||
|
||||
console.Println("Successfully migrated ‘" + sessionFile + "’ from version ‘" + sV6Header.Version + "’ to " + "‘" + sV7Header.Version + "’.")
|
||||
}
|
||||
}
|
||||
|
||||
// Migrate session version '5' to version '6', all older sessions are
|
||||
// in-fact removed and not migrated. All session files from '6' and
|
||||
// above should be migrated - See: migrateSessionV6ToV7().
|
||||
func migrateSessionV5ToV6() {
|
||||
for _, sid := range getSessionIDs() {
|
||||
sV6Header, err := loadSessionV6Header(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load version ‘6’. Migration failed please report this issue at https://github.com/minio/mc/issues.")
|
||||
|
||||
sessionVersion, e := strconv.Atoi(sV6Header.Version)
|
||||
fatalIf(probe.NewError(e), "Unable to load version ‘6’. Migration failed please report this issue at https://github.com/minio/mc/issues.")
|
||||
|
||||
if sessionVersion > 5 { // It is new format.
|
||||
continue
|
||||
}
|
||||
|
||||
/*** Remove all session files older than v6 ***/
|
||||
|
||||
sessionFile, err := getSessionFile(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to get session file.")
|
||||
|
||||
sessionDataFile, err := getSessionDataFile(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to get session data file.")
|
||||
|
||||
console.Println("Removing unsupported session file ‘" + sessionFile + "’ version ‘" + sV6Header.Version + "’.")
|
||||
if e := os.Remove(sessionFile); e != nil {
|
||||
fatalIf(probe.NewError(e), "Unable to remove version ‘"+sV6Header.Version+"’ session file ‘"+sessionFile+"’.")
|
||||
}
|
||||
if e := os.Remove(sessionDataFile); e != nil {
|
||||
fatalIf(probe.NewError(e), "Unable to remove version ‘"+sV6Header.Version+"’ session data file ‘"+sessionDataFile+"’.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Minio Client (C) 2016 Minio, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio/pkg/probe"
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
|
||||
/////////////////// Session V6 ///////////////////
|
||||
// sessionV6Header for resumable sessions.
|
||||
type sessionV6Header struct {
|
||||
Version string `json:"version"`
|
||||
When time.Time `json:"time"`
|
||||
RootPath string `json:"workingFolder"`
|
||||
GlobalBoolFlags map[string]bool `json:"globalBoolFlags"`
|
||||
GlobalIntFlags map[string]int `json:"globalIntFlags"`
|
||||
GlobalStringFlags map[string]string `json:"globalStringFlags"`
|
||||
CommandType string `json:"commandType"`
|
||||
CommandArgs []string `json:"cmdArgs"`
|
||||
CommandBoolFlags map[string]bool `json:"cmdBoolFlags"`
|
||||
CommandIntFlags map[string]int `json:"cmdIntFlags"`
|
||||
CommandStringFlags map[string]string `json:"cmdStringFlags"`
|
||||
LastCopied string `json:"lastCopied"`
|
||||
TotalBytes int64 `json:"totalBytes"`
|
||||
TotalObjects int `json:"totalObjects"`
|
||||
}
|
||||
|
||||
func loadSessionV6Header(sid string) (*sessionV6Header, *probe.Error) {
|
||||
if !isSessionDirExists() {
|
||||
return nil, errInvalidArgument().Trace()
|
||||
}
|
||||
|
||||
sessionFile, err := getSessionFile(sid)
|
||||
if err != nil {
|
||||
return nil, err.Trace(sid)
|
||||
}
|
||||
|
||||
if _, e := os.Stat(sessionFile); e != nil {
|
||||
return nil, probe.NewError(e)
|
||||
}
|
||||
|
||||
sV6Header := &sessionV6Header{}
|
||||
sV6Header.Version = "6"
|
||||
qs, err := quick.New(sV6Header)
|
||||
if err != nil {
|
||||
return nil, err.Trace(sid, sV6Header.Version)
|
||||
}
|
||||
err = qs.Load(sessionFile)
|
||||
if err != nil {
|
||||
return nil, err.Trace(sid, sV6Header.Version)
|
||||
}
|
||||
|
||||
sV6Header = qs.Data().(*sessionV6Header)
|
||||
return sV6Header, nil
|
||||
}
|
||||
|
||||
/////////////////// Session V7 ///////////////////
|
||||
// RESERVED FOR FUTURE
|
||||
@@ -32,33 +32,8 @@ import (
|
||||
"github.com/minio/minio/pkg/quick"
|
||||
)
|
||||
|
||||
func migrateSessionV5ToV6() {
|
||||
for _, sid := range getSessionIDs() {
|
||||
sessionV6, err := loadSessionV6(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to load version ‘6’. Migration failed please report this issue at https://github.com/minio/mc/issues.")
|
||||
if sessionV6.Header.Version == "6" { // It is new format.
|
||||
return
|
||||
}
|
||||
/*** Remove all session files older than v6 ***/
|
||||
|
||||
sessionFile, err := getSessionFile(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to get session file.")
|
||||
|
||||
sessionDataFile, err := getSessionDataFile(sid)
|
||||
fatalIf(err.Trace(sid), "Unable to get session data file.")
|
||||
|
||||
console.Println("Removing unsupported session file ‘" + sessionFile + "’ version ‘" + sessionV6.Header.Version + "’.")
|
||||
if e := os.Remove(sessionFile); e != nil {
|
||||
fatalIf(probe.NewError(e), "Unable to remove version ‘"+sessionV6.Header.Version+"’ session file ‘"+sessionFile+"’.")
|
||||
}
|
||||
if e := os.Remove(sessionDataFile); e != nil {
|
||||
fatalIf(probe.NewError(e), "Unable to remove version ‘"+sessionV6.Header.Version+"’ session data file ‘"+sessionDataFile+"’.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sessionV6Header for resumable sessions.
|
||||
type sessionV6Header struct {
|
||||
// sessionV7Header for resumable sessions.
|
||||
type sessionV7Header struct {
|
||||
Version string `json:"version"`
|
||||
When time.Time `json:"time"`
|
||||
RootPath string `json:"workingFolder"`
|
||||
@@ -71,6 +46,7 @@ type sessionV6Header struct {
|
||||
CommandIntFlags map[string]int `json:"cmdIntFlags"`
|
||||
CommandStringFlags map[string]string `json:"cmdStringFlags"`
|
||||
LastCopied string `json:"lastCopied"`
|
||||
LastRemoved string `json:"lastRemoved"`
|
||||
TotalBytes int64 `json:"totalBytes"`
|
||||
TotalObjects int `json:"totalObjects"`
|
||||
}
|
||||
@@ -84,9 +60,9 @@ type sessionMessage struct {
|
||||
CommandArgs []string `json:"commandArgs"`
|
||||
}
|
||||
|
||||
// sessionV6 resumable session container.
|
||||
type sessionV6 struct {
|
||||
Header *sessionV6Header
|
||||
// sessionV7 resumable session container.
|
||||
type sessionV7 struct {
|
||||
Header *sessionV7Header
|
||||
SessionID string
|
||||
mutex *sync.Mutex
|
||||
DataFP *sessionDataFP
|
||||
@@ -105,7 +81,7 @@ func (file *sessionDataFP) Write(p []byte) (int, error) {
|
||||
}
|
||||
|
||||
// String colorized session message.
|
||||
func (s sessionV6) String() string {
|
||||
func (s sessionV7) String() string {
|
||||
message := console.Colorize("SessionID", fmt.Sprintf("%s -> ", s.SessionID))
|
||||
message = message + console.Colorize("SessionTime", fmt.Sprintf("[%s]", s.Header.When.Local().Format(printDate)))
|
||||
message = message + console.Colorize("Command", fmt.Sprintf(" %s %s", s.Header.CommandType, strings.Join(s.Header.CommandArgs, " ")))
|
||||
@@ -113,7 +89,7 @@ func (s sessionV6) String() string {
|
||||
}
|
||||
|
||||
// JSON jsonified session message.
|
||||
func (s sessionV6) JSON() string {
|
||||
func (s sessionV7) JSON() string {
|
||||
sessionMsg := sessionMessage{
|
||||
SessionID: s.SessionID,
|
||||
Time: s.Header.When.Local(),
|
||||
@@ -127,8 +103,8 @@ func (s sessionV6) JSON() string {
|
||||
return string(sessionBytes)
|
||||
}
|
||||
|
||||
// loadSessionV6 - reads session file if exists and re-initiates internal variables
|
||||
func loadSessionV6(sid string) (*sessionV6, *probe.Error) {
|
||||
// loadSessionV7 - reads session file if exists and re-initiates internal variables
|
||||
func loadSessionV7(sid string) (*sessionV7, *probe.Error) {
|
||||
if !isSessionDirExists() {
|
||||
return nil, errInvalidArgument().Trace()
|
||||
}
|
||||
@@ -141,10 +117,10 @@ func loadSessionV6(sid string) (*sessionV6, *probe.Error) {
|
||||
return nil, probe.NewError(e)
|
||||
}
|
||||
|
||||
s := &sessionV6{}
|
||||
s.Header = &sessionV6Header{}
|
||||
s := &sessionV7{}
|
||||
s.Header = &sessionV7Header{}
|
||||
s.SessionID = sid
|
||||
s.Header.Version = "5"
|
||||
s.Header.Version = "7"
|
||||
qs, err := quick.New(s.Header)
|
||||
if err != nil {
|
||||
return nil, err.Trace(sid, s.Header.Version)
|
||||
@@ -155,7 +131,7 @@ func loadSessionV6(sid string) (*sessionV6, *probe.Error) {
|
||||
}
|
||||
|
||||
s.mutex = new(sync.Mutex)
|
||||
s.Header = qs.Data().(*sessionV6Header)
|
||||
s.Header = qs.Data().(*sessionV7Header)
|
||||
|
||||
sessionDataFile, err := getSessionDataFile(s.SessionID)
|
||||
if err != nil {
|
||||
@@ -171,11 +147,11 @@ func loadSessionV6(sid string) (*sessionV6, *probe.Error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// newSessionV6 provides a new session.
|
||||
func newSessionV6() *sessionV6 {
|
||||
s := &sessionV6{}
|
||||
s.Header = &sessionV6Header{}
|
||||
s.Header.Version = "6"
|
||||
// newSessionV7 provides a new session.
|
||||
func newSessionV7() *sessionV7 {
|
||||
s := &sessionV7{}
|
||||
s.Header = &sessionV7Header{}
|
||||
s.Header.Version = "7"
|
||||
// map of command and files copied.
|
||||
s.Header.GlobalBoolFlags = make(map[string]bool)
|
||||
s.Header.GlobalIntFlags = make(map[string]int)
|
||||
@@ -203,26 +179,26 @@ func newSessionV6() *sessionV6 {
|
||||
}
|
||||
|
||||
// HasData provides true if this is a session resume, false otherwise.
|
||||
func (s sessionV6) HasData() bool {
|
||||
return s.Header.LastCopied != ""
|
||||
func (s sessionV7) HasData() bool {
|
||||
return s.Header.LastCopied != "" || s.Header.LastRemoved != ""
|
||||
}
|
||||
|
||||
// NewDataReader provides reader interface to session data file.
|
||||
func (s *sessionV6) NewDataReader() io.Reader {
|
||||
func (s *sessionV7) NewDataReader() io.Reader {
|
||||
// DataFP is always intitialized, either via new or load functions.
|
||||
s.DataFP.Seek(0, os.SEEK_SET)
|
||||
return io.Reader(s.DataFP)
|
||||
}
|
||||
|
||||
// NewDataReader provides writer interface to session data file.
|
||||
func (s *sessionV6) NewDataWriter() io.Writer {
|
||||
func (s *sessionV7) NewDataWriter() io.Writer {
|
||||
// DataFP is always intitialized, either via new or load functions.
|
||||
s.DataFP.Seek(0, os.SEEK_SET)
|
||||
return io.Writer(s.DataFP)
|
||||
}
|
||||
|
||||
// Save this session.
|
||||
func (s *sessionV6) Save() *probe.Error {
|
||||
func (s *sessionV7) Save() *probe.Error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
@@ -247,7 +223,7 @@ func (s *sessionV6) Save() *probe.Error {
|
||||
|
||||
// setGlobals captures the state of global variables into session header.
|
||||
// Used by newSession.
|
||||
func (s *sessionV6) setGlobals() {
|
||||
func (s *sessionV7) setGlobals() {
|
||||
s.Header.GlobalBoolFlags["quiet"] = globalQuiet
|
||||
s.Header.GlobalBoolFlags["debug"] = globalDebug
|
||||
s.Header.GlobalBoolFlags["json"] = globalJSON
|
||||
@@ -256,7 +232,7 @@ func (s *sessionV6) setGlobals() {
|
||||
|
||||
// RestoreGlobals restores the state of global variables.
|
||||
// Used by resumeSession.
|
||||
func (s sessionV6) restoreGlobals() {
|
||||
func (s sessionV7) restoreGlobals() {
|
||||
quiet := s.Header.GlobalBoolFlags["quiet"]
|
||||
debug := s.Header.GlobalBoolFlags["debug"]
|
||||
json := s.Header.GlobalBoolFlags["json"]
|
||||
@@ -265,7 +241,7 @@ func (s sessionV6) restoreGlobals() {
|
||||
}
|
||||
|
||||
// Close ends this session and removes all associated session files.
|
||||
func (s *sessionV6) Close() *probe.Error {
|
||||
func (s *sessionV7) Close() *probe.Error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
@@ -286,7 +262,7 @@ func (s *sessionV6) Close() *probe.Error {
|
||||
}
|
||||
|
||||
// Delete removes all the session files.
|
||||
func (s *sessionV6) Delete() *probe.Error {
|
||||
func (s *sessionV7) Delete() *probe.Error {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
|
||||
@@ -321,27 +297,26 @@ func (s *sessionV6) Delete() *probe.Error {
|
||||
}
|
||||
|
||||
// Close a session and exit.
|
||||
func (s sessionV6) CloseAndDie() {
|
||||
func (s sessionV7) CloseAndDie() {
|
||||
s.Close()
|
||||
console.Fatalln("Session safely terminated. To resume session ‘mc session resume " + s.SessionID + "’")
|
||||
}
|
||||
|
||||
// Create a factory function to simplify checking if an
|
||||
// object has been copied or not.
|
||||
// isCopied(URL) -> true or false
|
||||
func isCopiedFactory(lastCopied string) func(string) bool {
|
||||
copied := true // closure
|
||||
// Create a factory function to simplify checking if
|
||||
// object was last operated on.
|
||||
func isLastFactory(lastURL string) func(string) bool {
|
||||
last := true // closure
|
||||
return func(sourceURL string) bool {
|
||||
if sourceURL == "" {
|
||||
fatalIf(errInvalidArgument().Trace(), "Empty source argument passed.")
|
||||
}
|
||||
if lastCopied == "" {
|
||||
if lastURL == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if copied {
|
||||
if lastCopied == sourceURL {
|
||||
copied = false // from next call onwards we say false.
|
||||
if last {
|
||||
if lastURL == sourceURL {
|
||||
last = false // from next call onwards we say false.
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -28,6 +28,9 @@ import (
|
||||
func migrateSession() {
|
||||
// We no longer support sessions older than v5. They will be removed.
|
||||
migrateSessionV5ToV6()
|
||||
|
||||
// Migrate V6 to V7.
|
||||
migrateSessionV6ToV7()
|
||||
}
|
||||
|
||||
// createSessionDir - create session directory.
|
||||
|
||||
+2
-2
@@ -34,14 +34,14 @@ func (s *TestSuite) TestSession(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(isSessionDirExists(), Equals, true)
|
||||
|
||||
session := newSessionV6()
|
||||
session := newSessionV7()
|
||||
c.Assert(session.Header.CommandArgs, IsNil)
|
||||
c.Assert(len(session.SessionID), Equals, 8)
|
||||
|
||||
err = session.Close()
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
savedSession, err := loadSessionV6(session.SessionID)
|
||||
savedSession, err := loadSessionV7(session.SessionID)
|
||||
c.Assert(err, IsNil)
|
||||
c.Assert(session.SessionID, Equals, savedSession.SessionID)
|
||||
|
||||
|
||||
@@ -31,6 +31,10 @@ var (
|
||||
return probe.NewError(errors.New("Invalid arguments provided, cannot proceed.")).Untrace()
|
||||
}
|
||||
|
||||
errUnrecognizedDiffType = func(diff differType) *probe.Error {
|
||||
return probe.NewError(errors.New("Unrecognized diffType: " + diff.String() + " provided.")).Untrace()
|
||||
}
|
||||
|
||||
errInvalidAliasedURL = func(URL string) *probe.Error {
|
||||
return probe.NewError(errors.New("Use ‘mc config host add mycloud " + URL + " ...’ to add an alias. Use the alias for S3 operations.")).Untrace()
|
||||
}
|
||||
@@ -51,6 +55,9 @@ var (
|
||||
return probe.NewError(errors.New("Overwrite not allowed for ‘" + URL + "’. Use ‘--force’ to override this behavior."))
|
||||
}
|
||||
|
||||
errDeleteNotAllowed = func(URL string) *probe.Error {
|
||||
return probe.NewError(errors.New("Delete not allowed for ‘" + URL + "’. Use ‘--force’ to override this behavior."))
|
||||
}
|
||||
errSourceIsDir = func(URL string) *probe.Error {
|
||||
return probe.NewError(errors.New("Source ‘" + URL + "’ is a folder.")).Untrace()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user