mirror of
https://github.com/octoleo/restic.git
synced 2024-11-05 04:47:51 +00:00
Merge pull request #709 from restic/fix-708
Make sure cleanup is executed before exiting
This commit is contained in:
commit
5132f5bfe6
@ -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())
|
||||||
|
Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Exit runs the cleanup handlers and then terminates the process with the
|
||||||
|
// given exit code.
|
||||||
|
func Exit(code int) {
|
||||||
RunCleanupHandlers()
|
RunCleanupHandlers()
|
||||||
fmt.Println("exiting")
|
os.Exit(code)
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -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.
|
||||||
|
@ -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
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user