diff --git a/cp-main.go b/cp-main.go index 0c00c3ff..8087f411 100644 --- a/cp-main.go +++ b/cp-main.go @@ -190,7 +190,7 @@ func doCopyFake(cURLs copyURLs, progressReader *barSend) { } // doPrepareCopyURLs scans the source URL and prepares a list of objects for copying. -func doPrepareCopyURLs(session *sessionV3, trapCh <-chan bool) { +func doPrepareCopyURLs(session *sessionV4, trapCh <-chan bool) { // Separate source and target. 'cp' can take only one target, // but any number of sources, even the recursive URLs mixed in-between. sourceURLs := session.Header.CommandArgs[:len(session.Header.CommandArgs)-1] @@ -256,7 +256,7 @@ func doPrepareCopyURLs(session *sessionV3, trapCh <-chan bool) { session.Save() } -func doCopySession(session *sessionV3) { +func doCopySession(session *sessionV4) { trapCh := signalTrap(os.Interrupt, syscall.SIGTERM) if !session.HasData() { @@ -377,7 +377,7 @@ func mainCopy(ctx *cli.Context) { console.SetColor("Copy", color.New(color.FgGreen, color.Bold)) var e error - session := newSessionV3() + session := newSessionV4() session.Header.CommandType = "cp" session.Header.RootPath, e = os.Getwd() if e != nil { diff --git a/mirror-main.go b/mirror-main.go index 5b47309c..57db864a 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -191,7 +191,7 @@ func doMirrorFake(sURLs mirrorURLs, progressReader *barSend) { } // doPrepareMirrorURLs scans the source URL and prepares a list of objects for mirroring. -func doPrepareMirrorURLs(session *sessionV3, trapCh <-chan bool) { +func doPrepareMirrorURLs(session *sessionV4, trapCh <-chan bool) { sourceURL := session.Header.CommandArgs[0] // first one is source. targetURLs := session.Header.CommandArgs[1:] var totalBytes int64 @@ -206,7 +206,11 @@ func doPrepareMirrorURLs(session *sessionV3, trapCh <-chan bool) { } // will be true if '--force' is provided on the command line. - mirrorIsForce = session.Header.CommandBoolFlag.Value + if len(session.Header.CommandBoolFlag) > 0 { + if session.Header.CommandBoolFlag[0].Key == "force" { + mirrorIsForce = session.Header.CommandBoolFlag[0].Value + } + } URLsCh := prepareMirrorURLs(sourceURL, targetURLs) done := false @@ -255,7 +259,7 @@ func doPrepareMirrorURLs(session *sessionV3, trapCh <-chan bool) { } // Session'fied mirror command. -func doMirrorSession(session *sessionV3) { +func doMirrorSession(session *sessionV4) { trapCh := signalTrap(os.Interrupt, syscall.SIGTERM) if !session.HasData() { @@ -376,7 +380,7 @@ func mainMirror(ctx *cli.Context) { console.SetColor("Mirror", color.New(color.FgGreen, color.Bold)) var e error - session := newSessionV3() + session := newSessionV4() session.Header.CommandType = "mirror" session.Header.RootPath, e = os.Getwd() if e != nil { @@ -385,8 +389,7 @@ func mainMirror(ctx *cli.Context) { } // If force flag is set save it with in session - session.Header.CommandBoolFlag.Key = "force" - session.Header.CommandBoolFlag.Value = ctx.Bool("force") + session.Header.CommandBoolFlag = []cmdBoolFlag{cmdBoolFlag{Key: "force", Value: ctx.Bool("force")}} // extract URLs. var err *probe.Error diff --git a/session-legacy.go b/session-legacy.go index 9e88f5be..2369c057 100644 --- a/session-legacy.go +++ b/session-legacy.go @@ -129,6 +129,106 @@ func migrateSessionV1ToV2() { } } +func migrateSessionV2ToV3() { + for _, sid := range getSessionIDs() { + oldSessionV2Header, err := loadSessionV2(sid) + // 1.1.0 intermediate version number is actually v2. + fatalIf(err.Trace(sid), "Unable to load version ‘1.1.0’. Migration failed please report this issue at https://github.com/minio/mc/issues.") + if oldSessionV2Header.Version == "4" { + return + } + 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.") + + if e := os.Remove(sessionFile); e != nil { + fatalIf(probe.NewError(e), "Unable to remove version 2 session files.") + } + if e := os.Remove(sessionDataFile); e != nil { + fatalIf(probe.NewError(e), "Unable to remove version 2 session data files.") + } + } +} + +// sessionV3Header +type sessionV3Header struct { + Version string `json:"version"` + When time.Time `json:"time"` + RootPath string `json:"workingFolder"` + CommandType string `json:"commandType"` + CommandArgs []string `json:"cmdArgs"` + CommandBoolFlag struct { + Key string + Value bool + } `json:"cmdBoolFlag"` + CommandIntFlag struct { + Key string + Value int + } `json:"cmdIntFlag"` + CommandStringFlag struct { + Key string + Value string + } `json:"cmdStringFlag"` + LastCopied string `json:"lastCopied"` + TotalBytes int64 `json:"totalBytes"` + TotalObjects int `json:"totalObjects"` +} + +// sessionV3 +type sessionV3 struct { + Header *sessionV3Header + SessionID string + mutex *sync.Mutex + DataFP *sessionDataFP + sigCh bool +} + +// loadSessionV3 - reads session file if exists and re-initiates internal variables +func loadSessionV3(sid string) (*sessionV3, *probe.Error) { + if !isSessionDirExists() { + return nil, errInvalidArgument().Trace() + } + sessionFile, err := getSessionFile(sid) + if err != nil { + return nil, err.Trace(sid) + } + + if _, err := os.Stat(sessionFile); err != nil { + return nil, probe.NewError(err) + } + + s := &sessionV3{} + s.Header = &sessionV3Header{} + s.SessionID = sid + s.Header.Version = "3" + qs, err := quick.New(s.Header) + if err != nil { + return nil, err.Trace(sid, s.Header.Version) + } + err = qs.Load(sessionFile) + if err != nil { + return nil, err.Trace(sid, s.Header.Version) + } + + s.mutex = new(sync.Mutex) + s.Header = qs.Data().(*sessionV3Header) + + sessionDataFile, err := getSessionDataFile(s.SessionID) + if err != nil { + return nil, err.Trace(sid, s.Header.Version) + } + + var e error + dataFile, e := os.Open(sessionDataFile) + fatalIf(probe.NewError(e), "Unable to open session data file \""+sessionDataFile+"\".") + + s.DataFP = &sessionDataFP{false, dataFile} + + return s, nil +} + // sessionV2Header new session version 2 header. type sessionV2Header struct { Version string `json:"version"` diff --git a/session-main.go b/session-main.go index e51a8e67..c148d59a 100644 --- a/session-main.go +++ b/session-main.go @@ -74,7 +74,7 @@ EXAMPLES: } // bySessionWhen is a type for sorting session metadata by time. -type bySessionWhen []*sessionV3 +type bySessionWhen []*sessionV4 func (b bySessionWhen) Len() int { return len(b) } func (b bySessionWhen) Swap(i, j int) { b[i], b[j] = b[j], b[i] } @@ -82,9 +82,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 []*sessionV3 + var bySessions []*sessionV4 for _, sid := range getSessionIDs() { - s, err := loadSessionV3(sid) + s, err := loadSessionV4(sid) if err != nil { return err.Trace() } @@ -122,7 +122,7 @@ func (c clearSessionMessage) JSON() string { func clearSession(sid string) { if sid == "all" { for _, sid := range getSessionIDs() { - session, err := loadSessionV3(sid) + session, err := loadSessionV4(sid) fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.") fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.") @@ -136,7 +136,7 @@ func clearSession(sid string) { fatalIf(errDummy().Trace(), "Session ‘"+sid+"’ not found.") } - session, err := loadSessionV3(sid) + session, err := loadSessionV4(sid) fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.") if session != nil { @@ -145,7 +145,7 @@ func clearSession(sid string) { } } -func sessionExecute(s *sessionV3) { +func sessionExecute(s *sessionV4) { switch s.Header.CommandType { case "cp": doCopySession(s) @@ -199,7 +199,7 @@ func mainSession(ctx *cli.Context) { if !isSessionExists(sid) { fatalIf(errDummy().Trace(), "Session ‘"+sid+"’ not found.") } - s, err := loadSessionV3(sid) + s, err := loadSessionV4(sid) fatalIf(err.Trace(sid), "Unable to load session.") // extra check for testing purposes. if s == nil { diff --git a/session-v3.go b/session-v4.go similarity index 70% rename from session-v3.go rename to session-v4.go index 56ea27d1..6dc97827 100644 --- a/session-v3.go +++ b/session-v4.go @@ -32,57 +32,57 @@ import ( "github.com/minio/minio-xl/pkg/quick" ) -func migrateSessionV2ToV3() { +func migrateSessionV3ToV4() { for _, sid := range getSessionIDs() { - oldSessionV2Header, err := loadSessionV2(sid) - // 1.1.0 intermediate version number is actually v2. - fatalIf(err.Trace(sid), "Unable to load version ‘1.1.0’. Migration failed please report this issue at https://github.com/minio/mc/issues.") - if oldSessionV2Header.Version == "3" { + oldSessionV3, err := loadSessionV3(sid) + fatalIf(err.Trace(sid), "Unable to load version ‘3’. Migration failed please report this issue at https://github.com/minio/mc/issues.") + if oldSessionV3.Header.Version == "4" { return } - newSessionV3Header := &sessionV3Header{} - newSessionV3Header.Version = "3" - newSessionV3Header.When = oldSessionV2Header.When - newSessionV3Header.RootPath = oldSessionV2Header.RootPath - newSessionV3Header.CommandType = oldSessionV2Header.CommandType - newSessionV3Header.CommandArgs = oldSessionV2Header.CommandArgs - newSessionV3Header.LastCopied = oldSessionV2Header.LastCopied - newSessionV3Header.TotalBytes = oldSessionV2Header.TotalBytes - newSessionV3Header.TotalObjects = oldSessionV2Header.TotalObjects - - qs, err := quick.New(newSessionV3Header) - fatalIf(err.Trace(sid), "Unable to initialize quick config.") sessionFile, err := getSessionFile(sid) fatalIf(err.Trace(sid), "Unable to get session file.") - err = qs.Save(sessionFile) - fatalIf(err.Trace(sid), "Saving new session file for version ‘3’ failed.") + sessionDataFile, err := getSessionDataFile(sid) + fatalIf(err.Trace(sid), "Unable to get session data file.") + + if e := os.Remove(sessionFile); e != nil { + fatalIf(probe.NewError(e), "Unable to remove version 2 session files.") + } + if e := os.Remove(sessionDataFile); e != nil { + fatalIf(probe.NewError(e), "Unable to remove version 2 session data files.") + } } } -// sessionV3Header -type sessionV3Header struct { - Version string `json:"version"` - When time.Time `json:"time"` - RootPath string `json:"workingFolder"` - CommandType string `json:"commandType"` - CommandArgs []string `json:"cmdArgs"` - CommandBoolFlag struct { - Key string - Value bool - } `json:"cmdBoolFlag"` - CommandIntFlag struct { - Key string - Value int - } `json:"cmdIntFlag"` - CommandStringFlag struct { - Key string - Value string - } `json:"cmdStringFlag"` - LastCopied string `json:"lastCopied"` - TotalBytes int64 `json:"totalBytes"` - TotalObjects int `json:"totalObjects"` +type cmdBoolFlag struct { + Key string + Value bool +} + +type cmdIntFlag struct { + Key string + Value int +} + +type cmdStringFlag struct { + Key string + Value string +} + +// sessionV4Header +type sessionV4Header struct { + Version string `json:"version"` + When time.Time `json:"time"` + RootPath string `json:"workingFolder"` + CommandType string `json:"commandType"` + CommandArgs []string `json:"cmdArgs"` + CommandBoolFlag []cmdBoolFlag `json:"cmdBoolFlag"` + CommandIntFlag []cmdIntFlag `json:"cmdIntFlag"` + CommandStringFlag []cmdStringFlag `json:"cmdStringFlag"` + LastCopied string `json:"lastCopied"` + TotalBytes int64 `json:"totalBytes"` + TotalObjects int `json:"totalObjects"` } // sessionMessage container for session messages @@ -94,9 +94,9 @@ type sessionMessage struct { CommandArgs []string `json:"commandArgs"` } -// sessionV3 -type sessionV3 struct { - Header *sessionV3Header +// sessionV4 +type sessionV4 struct { + Header *sessionV4Header SessionID string mutex *sync.Mutex DataFP *sessionDataFP @@ -114,7 +114,7 @@ func (file *sessionDataFP) Write(p []byte) (int, error) { } // String colorized session message -func (s sessionV3) String() string { +func (s sessionV4) 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, " "))) @@ -122,7 +122,7 @@ func (s sessionV3) String() string { } // JSON jsonified session message -func (s sessionV3) JSON() string { +func (s sessionV4) JSON() string { sessionMsg := sessionMessage{ SessionID: s.SessionID, Time: s.Header.When.Local(), @@ -136,19 +136,16 @@ func (s sessionV3) JSON() string { return string(sessionBytes) } -// newSessionV3 provides a new session -func newSessionV3() *sessionV3 { - s := &sessionV3{} - s.Header = &sessionV3Header{} - s.Header.Version = "3" +// newSessionV4 provides a new session +func newSessionV4() *sessionV4 { + s := &sessionV4{} + s.Header = &sessionV4Header{} + s.Header.Version = "4" // map of command and files copied s.Header.CommandArgs = nil - s.Header.CommandBoolFlag.Key = "" - s.Header.CommandBoolFlag.Value = false - s.Header.CommandIntFlag.Key = "" - s.Header.CommandIntFlag.Value = 0 - s.Header.CommandStringFlag.Key = "" - s.Header.CommandStringFlag.Value = "" + s.Header.CommandBoolFlag = []cmdBoolFlag{cmdBoolFlag{Key: "", Value: false}} + s.Header.CommandIntFlag = []cmdIntFlag{cmdIntFlag{Key: "", Value: 0}} + s.Header.CommandStringFlag = []cmdStringFlag{cmdStringFlag{Key: "", Value: ""}} s.Header.When = time.Now().UTC() s.mutex = new(sync.Mutex) s.SessionID = newRandomID(8) @@ -164,7 +161,7 @@ func newSessionV3() *sessionV3 { } // HasData provides true if this is a session resume, false otherwise. -func (s sessionV3) HasData() bool { +func (s sessionV4) HasData() bool { if s.Header.LastCopied == "" { return false } @@ -172,21 +169,21 @@ func (s sessionV3) HasData() bool { } // NewDataReader provides reader interface to session data file. -func (s *sessionV3) NewDataReader() io.Reader { +func (s *sessionV4) 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 *sessionV3) NewDataWriter() io.Writer { +func (s *sessionV4) 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 *sessionV3) Save() *probe.Error { +func (s *sessionV4) Save() *probe.Error { s.mutex.Lock() defer s.mutex.Unlock() @@ -210,7 +207,7 @@ func (s *sessionV3) Save() *probe.Error { } // Close ends this session and removes all associated session files. -func (s *sessionV3) Close() *probe.Error { +func (s *sessionV4) Close() *probe.Error { s.mutex.Lock() defer s.mutex.Unlock() @@ -231,7 +228,7 @@ func (s *sessionV3) Close() *probe.Error { } // Delete removes all the session files. -func (s *sessionV3) Delete() *probe.Error { +func (s *sessionV4) Delete() *probe.Error { s.mutex.Lock() defer s.mutex.Unlock() @@ -261,14 +258,14 @@ func (s *sessionV3) Delete() *probe.Error { } // Close a session and exit. -func (s sessionV3) CloseAndDie() { +func (s sessionV4) CloseAndDie() { s.Close() console.Infoln("Session safely terminated. To resume session ‘mc session resume " + s.SessionID + "’") os.Exit(0) } -// loadSessionV3 - reads session file if exists and re-initiates internal variables -func loadSessionV3(sid string) (*sessionV3, *probe.Error) { +// loadSessionV4 - reads session file if exists and re-initiates internal variables +func loadSessionV4(sid string) (*sessionV4, *probe.Error) { if !isSessionDirExists() { return nil, errInvalidArgument().Trace() } @@ -281,10 +278,10 @@ func loadSessionV3(sid string) (*sessionV3, *probe.Error) { return nil, probe.NewError(err) } - s := &sessionV3{} - s.Header = &sessionV3Header{} + s := &sessionV4{} + s.Header = &sessionV4Header{} s.SessionID = sid - s.Header.Version = "3" + s.Header.Version = "4" qs, err := quick.New(s.Header) if err != nil { return nil, err.Trace(sid, s.Header.Version) @@ -295,7 +292,7 @@ func loadSessionV3(sid string) (*sessionV3, *probe.Error) { } s.mutex = new(sync.Mutex) - s.Header = qs.Data().(*sessionV3Header) + s.Header = qs.Data().(*sessionV4Header) sessionDataFile, err := getSessionDataFile(s.SessionID) if err != nil { diff --git a/session.go b/session.go index 4eec8ce7..e0c06343 100644 --- a/session.go +++ b/session.go @@ -31,6 +31,9 @@ func migrateSession() { // Migrate session V2 to V3. migrateSessionV2ToV3() + + // Migrate session V3 to V4. + migrateSessionV3ToV4() } // createSessionDir - create session directory. diff --git a/session_test.go b/session_test.go index aa04a9a6..cdb0964b 100644 --- a/session_test.go +++ b/session_test.go @@ -36,14 +36,14 @@ func (s *TestSuite) TestSession(c *C) { c.Assert(err, IsNil) c.Assert(isSessionDirExists(), Equals, true) - session := newSessionV3() + session := newSessionV4() c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8) err = session.Close() c.Assert(err, IsNil) - savedSession, err := loadSessionV3(session.SessionID) + savedSession, err := loadSessionV4(session.SessionID) c.Assert(err, IsNil) c.Assert(session.SessionID, Equals, savedSession.SessionID) @@ -90,7 +90,7 @@ func (s *TestSuite) TestSessionContext(c *C) { // reset back console.IsExited = false - session := newSessionV3() + session := newSessionV4() c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8)