From cc4227e3f24f08f4863e447f8221369f9bcf31aa Mon Sep 17 00:00:00 2001 From: Krishnan Parthasarathi Date: Tue, 24 May 2016 11:35:39 +0530 Subject: [PATCH] Made session list ignore broken sessions (#1728) * Made session list ignore broken sessions - `mc session list` - skips partial sessions[1] - `mc session clear all` - doesn't clear partial sessions. The idea is to prevent the user from clearing all sessions without realising that there were partial sessions. When there is one or more partial sessions, clear each one of them individually as follows, `mc session clear ` - clears the partial sessions. [1] - partial session - a session with missing `.data` file in the sessions directory. N B The presence of partial sessions indicate some underlying bug in session mangement code. This PR provides relief for users in the event of such a bug. * Added comments to session helper functions and to be provide context --- session-main.go | 21 ++++++++++++++++++--- session-v7.go | 6 +++--- session.go | 18 ++++++++++++++++++ session_test.go | 7 +++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/session-main.go b/session-main.go index b72ed08e..ccd571ed 100644 --- a/session-main.go +++ b/session-main.go @@ -89,7 +89,7 @@ func listSessions() *probe.Error { for _, sid := range getSessionIDs() { session, err := loadSessionV7(sid) if err != nil { - return err.Trace(sid) + continue // Skip 'broken' session during listing } session.Close() // Session close right here. bySessions = append(bySessions, session) @@ -110,7 +110,15 @@ type clearSessionMessage struct { // String colorized clear session message. func (c clearSessionMessage) String() string { - return console.Colorize("ClearSession", "Session ‘"+c.SessionID+"’ cleared successfully.") + msg := "Session ‘" + c.SessionID + "’" + var colorizedMsg string + switch c.Status { + case "success": + colorizedMsg = console.Colorize("ClearSession", msg+" cleared succesfully.") + case "forced": + colorizedMsg = console.Colorize("ClearSession", msg+" cleared forcefully.") + } + return colorizedMsg } // JSON jsonified clear session message. @@ -140,7 +148,14 @@ func clearSession(sid string) { } session, err := loadSessionV7(sid) - fatalIf(err.Trace(sid), "Unable to load session ‘"+sid+"’.") + if err != nil { + // `mc session clear ` assumes that user is aware that the session is unuseful + // and wants the associated session files to be removed + removeSessionFile(sid) + removeSessionDataFile(sid) + printMsg(clearSessionMessage{Status: "forced", SessionID: sid}) + return + } if session != nil { fatalIf(session.Delete().Trace(sid), "Unable to load session ‘"+sid+"’.") diff --git a/session-v7.go b/session-v7.go index b33b1e55..f2e662fb 100644 --- a/session-v7.go +++ b/session-v7.go @@ -138,10 +138,10 @@ func loadSessionV7(sid string) (*sessionV7, *probe.Error) { 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+"\".") - + if e != nil { + return nil, probe.NewError(e) + } s.DataFP = &sessionDataFP{false, dataFile} return s, nil diff --git a/session.go b/session.go index d90f3357..4159115c 100644 --- a/session.go +++ b/session.go @@ -115,3 +115,21 @@ func getSessionIDs() (sids []string) { } return sids } + +// removeSessionFile - remove the session file, ending with .json +func removeSessionFile(sid string) { + sessionFile, err := getSessionFile(sid) + if err != nil { + return + } + os.Remove(sessionFile) +} + +// removeSessionDataFile - remove the session data file, ending with .data +func removeSessionDataFile(sid string) { + dataFile, err := getSessionDataFile(sid) + if err != nil { + return + } + os.Remove(dataFile) +} diff --git a/session_test.go b/session_test.go index 33aafa91..f50e430e 100644 --- a/session_test.go +++ b/session_test.go @@ -17,6 +17,7 @@ package main import ( + "os" "regexp" . "gopkg.in/check.v1" @@ -37,9 +38,12 @@ func (s *TestSuite) TestSession(c *C) { session := newSessionV7() c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8) + _, e := os.Stat(session.DataFP.Name()) + c.Assert(e, IsNil) err = session.Close() c.Assert(err, IsNil) + c.Assert(isSessionExists(session.SessionID), Equals, true) savedSession, err := loadSessionV7(session.SessionID) c.Assert(err, IsNil) @@ -50,4 +54,7 @@ func (s *TestSuite) TestSession(c *C) { err = savedSession.Delete() c.Assert(err, IsNil) + c.Assert(isSessionExists(session.SessionID), Equals, false) + _, e = os.Stat(session.DataFP.Name()) + c.Assert(e, NotNil) }