-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
tools.go
74 lines (66 loc) · 1.87 KB
/
tools.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package driver
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
"os/exec"
log "github.com/Sirupsen/logrus"
"github.com/docker/go-plugins-helpers/volume"
)
//GlusterPersistence represent struct of persistence file
type GlusterPersistence struct {
Version int `json:"version"`
Volumes map[string]*glusterVolume `json:"volumes"`
Mounts map[string]*glusterMountpoint `json:"mounts"`
}
func (d *GlusterDriver) saveConfig() error {
fi, err := os.Lstat(CfgFolder)
if os.IsNotExist(err) {
if err = os.MkdirAll(CfgFolder, 0700); err != nil {
return err
}
} else if err != nil {
return err
}
if fi != nil && !fi.IsDir() {
return fmt.Errorf("%v already exist and it's not a directory", d.root)
}
b, err := json.Marshal(GlusterPersistence{Version: CfgVersion, Volumes: d.volumes, Mounts: d.mounts})
if err != nil {
log.Warn("Unable to encode persistence struct, %v", err)
}
//log.Debug("Writing persistence struct, %v", b, d.volumes)
err = ioutil.WriteFile(CfgFolder+"/persistence.json", b, 0600)
if err != nil {
log.Warn("Unable to write persistence struct, %v", err)
}
//TODO display error messages
return err
}
// run deamon in context of this gvfs drive with custome env
func (d *GlusterDriver) runCmd(cmd string) error {
log.Debugf(cmd)
return exec.Command("sh", "-c", cmd).Run()
}
func getMountName(d *GlusterDriver, r volume.Request) string {
if d.mountUniqName {
return url.PathEscape(r.Options["voluri"])
}
return url.PathEscape(r.Name)
}
//based on: http://stackoverflow.com/questions/30697324/how-to-check-if-directory-on-path-is-empty
func isEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()
_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
}
return false, err // Either not empty or error, suits both cases
}