-
Notifications
You must be signed in to change notification settings - Fork 3
/
local.go
177 lines (149 loc) · 3.35 KB
/
local.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package local
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/docker/docker/volume"
)
// VolumeDataPathName is the name of the directory where the volume data is stored.
// It uses a very distintive name to avoid colissions migrating data between
// Docker versions.
const (
VolumeDataPathName = "_data"
volumesPathName = "volumes"
)
var oldVfsDir = filepath.Join("vfs", "dir")
func New(scope string) (*Root, error) {
rootDirectory := filepath.Join(scope, volumesPathName)
if err := os.MkdirAll(rootDirectory, 0700); err != nil {
return nil, err
}
r := &Root{
scope: scope,
path: rootDirectory,
volumes: make(map[string]*Volume),
}
dirs, err := ioutil.ReadDir(rootDirectory)
if err != nil {
return nil, err
}
for _, d := range dirs {
name := filepath.Base(d.Name())
r.volumes[name] = &Volume{
driverName: r.Name(),
name: name,
path: r.DataPath(name),
}
}
return r, nil
}
type Root struct {
m sync.Mutex
scope string
path string
volumes map[string]*Volume
}
func (r *Root) DataPath(volumeName string) string {
return filepath.Join(r.path, volumeName, VolumeDataPathName)
}
func (r *Root) Name() string {
return "local"
}
func (r *Root) Create(name string) (volume.Volume, error) {
r.m.Lock()
defer r.m.Unlock()
v, exists := r.volumes[name]
if !exists {
path := r.DataPath(name)
if err := os.MkdirAll(path, 0755); err != nil {
if os.IsExist(err) {
return nil, fmt.Errorf("volume already exists under %s", filepath.Dir(path))
}
return nil, err
}
v = &Volume{
driverName: r.Name(),
name: name,
path: path,
}
r.volumes[name] = v
}
v.use()
return v, nil
}
func (r *Root) Remove(v volume.Volume) error {
r.m.Lock()
defer r.m.Unlock()
lv, ok := v.(*Volume)
if !ok {
return errors.New("unknown volume type")
}
lv.release()
if lv.usedCount == 0 {
realPath, err := filepath.EvalSymlinks(lv.path)
if err != nil {
return err
}
if !r.scopedPath(realPath) {
return fmt.Errorf("Unable to remove a directory of out the Docker root: %s", realPath)
}
if err := os.RemoveAll(realPath); err != nil {
return err
}
delete(r.volumes, lv.name)
return os.RemoveAll(filepath.Dir(lv.path))
}
return nil
}
// scopedPath verifies that the path where the volume is located
// is under Docker's root and the valid local paths.
func (r *Root) scopedPath(realPath string) bool {
// Volumes path for Docker version >= 1.7
if strings.HasPrefix(realPath, filepath.Join(r.scope, volumesPathName)) {
return true
}
// Volumes path for Docker version < 1.7
if strings.HasPrefix(realPath, filepath.Join(r.scope, oldVfsDir)) {
return true
}
return false
}
type Volume struct {
m sync.Mutex
usedCount int
// unique name of the volume
name string
// path is the path on the host where the data lives
path string
// driverName is the name of the driver that created the volume.
driverName string
}
func (v *Volume) Name() string {
return v.name
}
func (v *Volume) DriverName() string {
return v.driverName
}
func (v *Volume) Path() string {
return v.path
}
func (v *Volume) Mount() (string, error) {
return v.path, nil
}
func (v *Volume) Unmount() error {
return nil
}
func (v *Volume) use() {
v.m.Lock()
v.usedCount++
v.m.Unlock()
}
func (v *Volume) release() {
v.m.Lock()
v.usedCount--
v.m.Unlock()
}