2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 23:06:32 +00:00

Make sure cleanup is executed before exiting

Closes #708
This commit is contained in:
Alexander Neumann 2016-12-28 10:53:31 +01:00
parent b0997d05fb
commit 80457018d7
4 changed files with 19 additions and 11 deletions

View File

@ -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())
Exit(0)
}
}
// Exit runs the cleanup handlers and then terminates the process with the
// given exit code.
func Exit(code int) {
RunCleanupHandlers()
fmt.Println("exiting")
os.Exit(0)
}
os.Exit(code)
}

View File

@ -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.

View File

@ -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

View File

@ -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)
}