From 53afb72ae12955edaac20533d9fcfda12b630336 Mon Sep 17 00:00:00 2001 From: mpl Date: Sat, 25 Jul 2020 01:44:34 +0200 Subject: [PATCH] Make a copy of .lastdone before updating it Fixes #20 --- main.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index a478ad3..5b457b2 100644 --- a/main.go +++ b/main.go @@ -445,8 +445,20 @@ func markDone(dldir, location string) error { if *verboseFlag { log.Printf("Marking %v as done", location) } - // TODO(mpl): back up .lastdone before overwriting it, in case writing it fails. - if err := ioutil.WriteFile(filepath.Join(dldir, ".lastdone"), []byte(location), 0600); err != nil { + oldPath := filepath.Join(dldir, ".lastdone") + newPath := oldPath + ".bak" + if err := os.Rename(oldPath, newPath); err != nil { + if !os.IsNotExist(err) { + return err + } + } + if err := ioutil.WriteFile(oldPath, []byte(location), 0600); err != nil { + // restore from backup + if err := os.Rename(newPath, oldPath); err != nil { + if !os.IsNotExist(err) { + return err + } + } return err } return nil @@ -519,6 +531,9 @@ func (s *Session) download(ctx context.Context, location string) (string, error) if v.Name() == ".lastdone" { continue } + if v.Name() == ".lastdone.bak" { + continue + } fileEntries = append(fileEntries, v) } if len(fileEntries) < 1 {