-
Notifications
You must be signed in to change notification settings - Fork 0
/
od.go
53 lines (45 loc) · 1.16 KB
/
od.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package engine
// Management of output directory.
import (
"github.com/mumax/3/util"
"log"
"os"
"path/filepath"
"strings"
)
var OD = "./" // Output directory
// SetOD sets the output directory where auto-saved files will be stored.
// The -o flag can also be used for this purpose.
func SetOD(od string, force bool) {
if OD != "./" {
log.Println("setod: output directory already set to " + OD)
}
OD = od
if !strings.HasSuffix(OD, "/") {
OD += "/"
}
LogOutput("output directory:", OD)
{ // make OD
wd, err := os.Getwd()
util.FatalErr(err, "create output directory:")
stat, err2 := os.Stat(wd)
util.FatalErr(err2, "create output directory:")
_ = os.Mkdir(od, stat.Mode()) // already exists is OK
}
// fail on non-empty OD
f, err3 := os.Open(od)
util.FatalErr(err3, "open output directory:")
files, _ := f.Readdir(1)
if !force && len(files) != 0 {
util.Fatal(od, " not empty, clean it or force with -f")
}
// clean output dir
if len(files) != 0 && OD != "." {
filepath.Walk(OD, func(path string, i os.FileInfo, err error) error {
if path != OD {
util.FatalErr(os.RemoveAll(path), "clean output directory:")
}
return nil
})
}
}