diff --git a/src/cmds/restic/cleanup.go b/src/cmds/restic/cleanup.go index f75d8ee5d..8e35e3345 100644 --- a/src/cmds/restic/cleanup.go +++ b/src/cmds/restic/cleanup.go @@ -62,8 +62,13 @@ func CleanupHandler(c <-chan os.Signal) { for s := range c { debug.Log("signal %v received, cleaning up", s) fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine()) - RunCleanupHandlers() - fmt.Println("exiting") - os.Exit(0) + Exit(0) } } + +// Exit runs the cleanup handlers and then terminates the process with the +// given exit code. +func Exit(code int) { + RunCleanupHandlers() + os.Exit(code) +} diff --git a/src/cmds/restic/global.go b/src/cmds/restic/global.go index c2f915ac3..8b4cdd81d 100644 --- a/src/cmds/restic/global.go +++ b/src/cmds/restic/global.go @@ -131,7 +131,7 @@ func Printf(format string, args ...interface{}) { _, err := fmt.Fprintf(globalOptions.stdout, format, args...) if err != nil { fmt.Fprintf(os.Stderr, "unable to write to stdout: %v\n", err) - os.Exit(100) + Exit(100) } } @@ -174,18 +174,19 @@ func Warnf(format string, args ...interface{}) { _, err := fmt.Fprintf(globalOptions.stderr, format, args...) if err != nil { fmt.Fprintf(os.Stderr, "unable to write to stderr: %v\n", err) - os.Exit(100) + Exit(100) } } -// Exitf uses Warnf to write the message and then calls os.Exit(exitcode). +// Exitf uses Warnf to write the message and then terminates the process with +// the given exit code. func Exitf(exitcode int, format string, args ...interface{}) { if format[len(format)-1] != '\n' { format += "\n" } Warnf(format, args...) - os.Exit(exitcode) + Exit(exitcode) } // readPassword reads the password from the given reader directly. diff --git a/src/cmds/restic/lock.go b/src/cmds/restic/lock.go index 13f4c2d8f..81bdafbc2 100644 --- a/src/cmds/restic/lock.go +++ b/src/cmds/restic/lock.go @@ -36,6 +36,7 @@ func lockRepository(repo *repository.Repository, exclusive bool) (*restic.Lock, if err != nil { return nil, err } + debug.Log("create lock %p (exclusive %v)", lock, exclusive) globalLocks.Lock() if globalLocks.cancelRefresh == nil { @@ -88,7 +89,7 @@ func unlockRepo(lock *restic.Lock) error { globalLocks.Lock() defer globalLocks.Unlock() - debug.Log("unlocking repository") + debug.Log("unlocking repository with lock %p", lock) if err := lock.Unlock(); err != nil { debug.Log("error while unlocking: %v", err) return err diff --git a/src/cmds/restic/main.go b/src/cmds/restic/main.go index 2e55ba42b..2ad9ff260 100644 --- a/src/cmds/restic/main.go +++ b/src/cmds/restic/main.go @@ -49,9 +49,10 @@ func main() { fmt.Fprintf(os.Stderr, "%+v\n", err) } - RunCleanupHandlers() - + var exitCode int if err != nil { - os.Exit(1) + exitCode = 1 } + + Exit(exitCode) }