From 04fbff76a922dc30f0359ca8126fee431a1b3649 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 25 Jul 2015 17:26:44 -0700 Subject: [PATCH] Add Remove() method for session, Close() becomes the new Save() --- cast-main.go | 8 +++++--- cp-main.go | 8 +++++--- session-main.go | 11 +++++++---- session-v2.go | 16 +++++++++++++++- session_test.go | 4 ++-- 5 files changed, 34 insertions(+), 13 deletions(-) diff --git a/cast-main.go b/cast-main.go index 43042764..fe8c5d4e 100644 --- a/cast-main.go +++ b/cast-main.go @@ -170,6 +170,7 @@ func doPrepareCastURLs(session *sessionV2, trapCh <-chan bool) { totalObjects++ case <-trapCh: session.Close() // If we are interrupted during the URL scanning, we drop the session. + session.Remove() os.Exit(0) } } @@ -222,7 +223,7 @@ func doCastCmdSession(session *sessionV2) { console.Errorf("Failed to cast ‘%s’, %s\n", sURLs.SourceContent.Name, NewIodine(sURLs.Error)) } case <-trapCh: // Receive interrupt notification. - session.Save() + session.Close() session.Info() os.Exit(0) } @@ -268,16 +269,17 @@ func runCastCmd(ctx *cli.Context) { session.Header.CommandType = "cast" session.Header.RootPath, err = os.Getwd() if err != nil { - session.Close() + session.Remove() console.Fatalf("Unable to get current working folder. %s\n", err) } // extract URLs. session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { - session.Close() + session.Remove() console.Fatalf("One or more unknown URL types found in %s. %s\n", ctx.Args(), err) } doCastCmdSession(session) + session.Remove() } diff --git a/cp-main.go b/cp-main.go index 1a897564..4e6962f1 100644 --- a/cp-main.go +++ b/cp-main.go @@ -171,6 +171,7 @@ func doPrepareCopyURLs(session *sessionV2, trapCh <-chan bool) { totalObjects++ case <-trapCh: session.Close() // If we are interrupted during the URL scanning, we drop the session. + session.Remove() os.Exit(0) } } @@ -224,7 +225,7 @@ func doCopyCmdSession(session *sessionV2) { console.Errorf("Failed to copy ‘%s’, %s\n", cpURLs.SourceContent.Name, NewIodine(iodine.New(cpURLs.Error, nil))) } case <-trapCh: // Receive interrupt notification. - session.Save() + session.Close() session.Info() os.Exit(0) } @@ -271,16 +272,17 @@ func runCopyCmd(ctx *cli.Context) { session.Header.CommandType = "cp" session.Header.RootPath, err = os.Getwd() if err != nil { - session.Close() + session.Remove() console.Fatalf("Unable to get current working folder. %s\n", err) } // extract URLs. session.Header.CommandArgs, err = args2URLs(ctx.Args()) if err != nil { - session.Close() + session.Remove() console.Fatalf("One or more unknown URL types found %s. %s\n", ctx.Args(), err) } doCopyCmdSession(session) + session.Remove() } diff --git a/session-main.go b/session-main.go index bff9c1fc..868181f9 100644 --- a/session-main.go +++ b/session-main.go @@ -88,7 +88,7 @@ func clearSession(sid string) { if err != nil { console.Fatalf("Unable to load session ‘%s’, %s", sid, NewIodine(iodine.New(err, nil))) } - session.Close() + session.Remove() } return } @@ -102,7 +102,7 @@ func clearSession(sid string) { console.Fatalf("Unable to load session ‘%s’, %s", sid, NewIodine(iodine.New(err, nil))) } if session != nil { - session.Close() + session.Remove() } } @@ -168,9 +168,12 @@ func runSessionCmd(ctx *cli.Context) { sessionExecute(s) err = s.Close() if err != nil { - console.Fatalln("Unable to close session file properly. %s\n", err) + console.Fatalf("Unable to close session file properly. %s\n", err) + } + err = s.Remove() + if err != nil { + console.Fatalf("Unable to clear session files properly. %s\n", err) } - // change dir back os.Chdir(savedCwd) diff --git a/session-v2.go b/session-v2.go index 0cdb6b0e..fa958b48 100644 --- a/session-v2.go +++ b/session-v2.go @@ -138,7 +138,20 @@ func (s *sessionV2) Close() error { return NewIodine(iodine.New(err, nil)) } - err = os.Remove(getSessionDataFile(s.SessionID)) + qs, err := quick.New(s.Header) + if err != nil { + return NewIodine(iodine.New(err, nil)) + } + + return qs.Save(getSessionFile(s.SessionID)) +} + +// Remove removes all the session files. +func (s *sessionV2) Remove() error { + s.mutex.Lock() + defer s.mutex.Unlock() + + err := os.Remove(getSessionDataFile(s.SessionID)) if err != nil { return NewIodine(iodine.New(err, nil)) } @@ -151,6 +164,7 @@ func (s *sessionV2) Close() error { if err != nil { return NewIodine(iodine.New(err, nil)) } + return nil } diff --git a/session_test.go b/session_test.go index 8504357a..4433047f 100644 --- a/session_test.go +++ b/session_test.go @@ -40,14 +40,14 @@ func (s *CmdTestSuite) TestSession(c *C) { c.Assert(session.Header.CommandArgs, IsNil) c.Assert(len(session.SessionID), Equals, 8) - err = session.Save() + err = session.Close() c.Assert(err, IsNil) savedSession, err := loadSessionV2(session.SessionID) c.Assert(err, IsNil) c.Assert(session.SessionID, Equals, savedSession.SessionID) - err = session.Close() + err = session.Remove() c.Assert(err, IsNil) }