diff --git a/cp-main.go b/cp-main.go index 9458b6ea..367a2d5a 100644 --- a/cp-main.go +++ b/cp-main.go @@ -254,13 +254,14 @@ func doCopyCmdSession(session *sessionV2) { } if cpURLs.Error == nil { session.Header.LastCopied = cpURLs.SourceContent.Name + session.Save() } else { // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { console.Printf("%c[2K\n", 27) console.Printf("%c[A", 27) } - errorIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name)) + fatalIf(cpURLs.Error.Trace(), fmt.Sprintf("Failed to copy ‘%s’.", cpURLs.SourceContent.Name)) } case <-trapCh: // Receive interrupt notification. // Print in new line and adjust to top so that we don't print over the ongoing progress bar diff --git a/mirror-main.go b/mirror-main.go index 00d43047..f71010c7 100644 --- a/mirror-main.go +++ b/mirror-main.go @@ -252,13 +252,14 @@ func doMirrorCmdSession(session *sessionV2) { } if sURLs.Error == nil { session.Header.LastCopied = sURLs.SourceContent.Name + session.Save() } else { // Print in new line and adjust to top so that we don't print over the ongoing progress bar if !globalQuietFlag && !globalJSONFlag { console.Printf("%c[2K\n", 27) console.Printf("%c[A", 27) } - errorIf(sURLs.Error.Trace(), "Failed to mirror.") + fatalIf(sURLs.Error.Trace(), "Failed to mirror.") } case <-trapCh: // Receive interrupt notification. // Print in new line and adjust to top so that we don't print over the ongoing progress bar diff --git a/session-v2.go b/session-v2.go index e5c51c2d..e15fb978 100644 --- a/session-v2.go +++ b/session-v2.go @@ -69,10 +69,20 @@ type sessionV2 struct { Header *sessionV2Header SessionID string mutex *sync.Mutex - DataFP *os.File + DataFP *sessionDataFP sigCh bool } +type sessionDataFP struct { + dirty bool + *os.File +} + +func (file *sessionDataFP) Write(p []byte) (int, error) { + file.dirty = true + return file.File.Write(p) +} + // String colorized session message func (s sessionV2) String() string { message := console.Colorize("SessionID", fmt.Sprintf("%s -> ", s.SessionID)) @@ -109,10 +119,10 @@ func newSessionV2() *sessionV2 { sessionDataFile, perr := getSessionDataFile(s.SessionID) fatalIf(perr.Trace(s.SessionID), "Unable to create session data file \""+sessionDataFile+"\".") - var err error - s.DataFP, err = os.Create(sessionDataFile) + dataFile, err := os.Create(sessionDataFile) fatalIf(probe.NewError(err), "Unable to create session data file \""+sessionDataFile+"\".") + s.DataFP = &sessionDataFP{false, dataFile} return s } @@ -148,8 +158,11 @@ func (s *sessionV2) Save() *probe.Error { s.mutex.Lock() defer s.mutex.Unlock() - if err := s.DataFP.Sync(); err != nil { - return probe.NewError(err) + if s.DataFP.dirty { + if err := s.DataFP.Sync(); err != nil { + return probe.NewError(err) + } + s.DataFP.dirty = false } qs, err := quick.New(s.Header) @@ -251,8 +264,10 @@ func loadSessionV2(sid string) (*sessionV2, *probe.Error) { } var e error - s.DataFP, e = os.Open(sessionDataFile) - fatalIf(probe.NewError(e), "Unable to open session data file ‘"+sessionDataFile+"’.") + dataFile, e := os.Open(sessionDataFile) + fatalIf(probe.NewError(e), "Unable to open session data file \""+sessionDataFile+"\".") + + s.DataFP = &sessionDataFP{false, dataFile} return s, nil }