1
0
mirror of https://github.com/restic/restic.git synced 2024-07-06 09:20:53 +02:00

Merge pull request #709 from restic/fix-708

Make sure cleanup is executed before exiting
This commit is contained in:
Alexander Neumann 2016-12-28 18:28:07 +01:00
commit 5132f5bfe6
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 { for s := range c {
debug.Log("signal %v received, cleaning up", s) debug.Log("signal %v received, cleaning up", s)
fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine()) fmt.Printf("%sInterrupt received, cleaning up\n", ClearLine())
RunCleanupHandlers() Exit(0)
fmt.Println("exiting")
os.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)
}

View File

@ -131,7 +131,7 @@ func Printf(format string, args ...interface{}) {
_, err := fmt.Fprintf(globalOptions.stdout, format, args...) _, err := fmt.Fprintf(globalOptions.stdout, format, args...)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to write to stdout: %v\n", err) 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...) _, err := fmt.Fprintf(globalOptions.stderr, format, args...)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "unable to write to stderr: %v\n", err) 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{}) { func Exitf(exitcode int, format string, args ...interface{}) {
if format[len(format)-1] != '\n' { if format[len(format)-1] != '\n' {
format += "\n" format += "\n"
} }
Warnf(format, args...) Warnf(format, args...)
os.Exit(exitcode) Exit(exitcode)
} }
// readPassword reads the password from the given reader directly. // 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 { if err != nil {
return nil, err return nil, err
} }
debug.Log("create lock %p (exclusive %v)", lock, exclusive)
globalLocks.Lock() globalLocks.Lock()
if globalLocks.cancelRefresh == nil { if globalLocks.cancelRefresh == nil {
@ -88,7 +89,7 @@ func unlockRepo(lock *restic.Lock) error {
globalLocks.Lock() globalLocks.Lock()
defer globalLocks.Unlock() defer globalLocks.Unlock()
debug.Log("unlocking repository") debug.Log("unlocking repository with lock %p", lock)
if err := lock.Unlock(); err != nil { if err := lock.Unlock(); err != nil {
debug.Log("error while unlocking: %v", err) debug.Log("error while unlocking: %v", err)
return err return err

View File

@ -49,9 +49,10 @@ func main() {
fmt.Fprintf(os.Stderr, "%+v\n", err) fmt.Fprintf(os.Stderr, "%+v\n", err)
} }
RunCleanupHandlers() var exitCode int
if err != nil { if err != nil {
os.Exit(1) exitCode = 1
} }
Exit(exitCode)
} }