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

Commit 5b3b020

Browse files
committed
Do not remove temp dir if it already exists at start
1 parent a420add commit 5b3b020

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

bookbrowser.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ func main() {
2525
log.Fatalf("Fatal error: %s\n", err)
2626
}
2727

28-
tempdir, err := ioutil.TempDir("", "bookbrowser")
28+
deftempdir, err := ioutil.TempDir("", "bookbrowser")
2929
if err != nil {
30-
tempdir = filepath.Join(workdir, "_temp")
30+
deftempdir = filepath.Join(workdir, "_temp")
3131
}
3232

3333
app := cli.NewApp()
@@ -42,8 +42,8 @@ func main() {
4242
},
4343
cli.StringFlag{
4444
Name: "tempdir, t",
45-
Value: tempdir,
46-
Usage: "Use `DIR` as the location for storing temporary files such as cover thumbnails. The directory is created on start and deleted on exit.",
45+
Value: deftempdir,
46+
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.",
4747
},
4848
cli.StringFlag{
4949
Name: "addr, a",
@@ -55,6 +55,8 @@ func main() {
5555
app.Action = func(c *cli.Context) {
5656
bookdir := c.String("bookdir")
5757
tempdir := c.String("tempdir")
58+
noRemoveTempDir := false
59+
5860
addr := c.String("addr")
5961

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

70+
if fi, err := os.Stat(tempdir); err == nil || fi.IsDir() {
71+
noRemoveTempDir = true
72+
if tempdir == deftempdir {
73+
noRemoveTempDir = false
74+
}
75+
}
76+
6877
bookdir, err = filepath.Abs(bookdir)
6978
if err != nil {
7079
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", bookdir, err)
@@ -83,8 +92,12 @@ func main() {
8392
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
8493
go func() {
8594
<-sigs
86-
log.Println("Cleaning up covers")
87-
os.RemoveAll(tempdir)
95+
if noRemoveTempDir {
96+
log.Println("Not removing temp dir because dir already existed at start")
97+
} else {
98+
log.Println("Cleaning up temp dir")
99+
os.RemoveAll(tempdir)
100+
}
88101
os.Exit(0)
89102
}()
90103

0 commit comments

Comments
 (0)