Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Do not remove temp dir if it already exists at start
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Oct 21, 2017
1 parent a420add commit 5b3b020
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions bookbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func main() {
log.Fatalf("Fatal error: %s\n", err)
}

tempdir, err := ioutil.TempDir("", "bookbrowser")
deftempdir, err := ioutil.TempDir("", "bookbrowser")
if err != nil {
tempdir = filepath.Join(workdir, "_temp")
deftempdir = filepath.Join(workdir, "_temp")
}

app := cli.NewApp()
Expand All @@ -42,8 +42,8 @@ func main() {
},
cli.StringFlag{
Name: "tempdir, t",
Value: tempdir,
Usage: "Use `DIR` as the location for storing temporary files such as cover thumbnails. The directory is created on start and deleted on exit.",
Value: deftempdir,
Usage: "Use `DIR` as the location for storing temporary files such as cover thumbnails. The directory is created on start and deleted on exit, unless it already exists.",
},
cli.StringFlag{
Name: "addr, a",
Expand All @@ -55,6 +55,8 @@ func main() {
app.Action = func(c *cli.Context) {
bookdir := c.String("bookdir")
tempdir := c.String("tempdir")
noRemoveTempDir := false

addr := c.String("addr")

log.Printf("BookBrowser %s\n", curversion)
Expand All @@ -65,6 +67,13 @@ func main() {
}
}

if fi, err := os.Stat(tempdir); err == nil || fi.IsDir() {
noRemoveTempDir = true
if tempdir == deftempdir {
noRemoveTempDir = false
}
}

bookdir, err = filepath.Abs(bookdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", bookdir, err)
Expand All @@ -83,8 +92,12 @@ func main() {
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
log.Println("Cleaning up covers")
os.RemoveAll(tempdir)
if noRemoveTempDir {
log.Println("Not removing temp dir because dir already existed at start")
} else {
log.Println("Cleaning up temp dir")
os.RemoveAll(tempdir)
}
os.Exit(0)
}()

Expand Down

0 comments on commit 5b3b020

Please sign in to comment.