forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
217 lines (184 loc) · 4.28 KB
/
repository.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package volumes
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"github.com/docker/docker/daemon/graphdriver"
"github.com/docker/docker/pkg/log"
"github.com/docker/docker/utils"
)
type Repository struct {
configPath string
driver graphdriver.Driver
volumes map[string]*Volume
lock sync.Mutex
}
func NewRepository(configPath string, driver graphdriver.Driver) (*Repository, error) {
abspath, err := filepath.Abs(configPath)
if err != nil {
return nil, err
}
// Create the config path
if err := os.MkdirAll(abspath, 0700); err != nil && !os.IsExist(err) {
return nil, err
}
repo := &Repository{
driver: driver,
configPath: abspath,
volumes: make(map[string]*Volume),
}
return repo, repo.restore()
}
func (r *Repository) newVolume(path string, writable bool) (*Volume, error) {
var (
isBindMount bool
err error
id = utils.GenerateRandomID()
)
if path != "" {
isBindMount = true
}
if path == "" {
path, err = r.createNewVolumePath(id)
if err != nil {
return nil, err
}
}
path = filepath.Clean(path)
path, err = filepath.EvalSymlinks(path)
if err != nil {
return nil, err
}
v := &Volume{
ID: id,
Path: path,
repository: r,
Writable: writable,
containers: make(map[string]struct{}),
configPath: r.configPath + "/" + id,
IsBindMount: isBindMount,
}
if err := v.initialize(); err != nil {
return nil, err
}
return v, r.add(v)
}
func (r *Repository) restore() error {
dir, err := ioutil.ReadDir(r.configPath)
if err != nil {
return err
}
for _, v := range dir {
id := v.Name()
path, err := r.driver.Get(id, "")
if err != nil {
log.Debugf("Could not find volume for %s: %v", id, err)
continue
}
vol := &Volume{
ID: id,
configPath: r.configPath + "/" + id,
containers: make(map[string]struct{}),
Path: path,
}
if err := vol.FromDisk(); err != nil {
if !os.IsNotExist(err) {
log.Debugf("Error restoring volume: %v", err)
continue
}
if err := vol.initialize(); err != nil {
log.Debugf("%s", err)
continue
}
}
if err := r.add(vol); err != nil {
log.Debugf("Error restoring volume: %v", err)
}
}
return nil
}
func (r *Repository) Get(path string) *Volume {
r.lock.Lock()
vol := r.get(path)
r.lock.Unlock()
return vol
}
func (r *Repository) get(path string) *Volume {
path, err := filepath.EvalSymlinks(path)
if err != nil {
return nil
}
return r.volumes[filepath.Clean(path)]
}
func (r *Repository) Add(volume *Volume) error {
r.lock.Lock()
defer r.lock.Unlock()
return r.add(volume)
}
func (r *Repository) add(volume *Volume) error {
if vol := r.get(volume.Path); vol != nil {
return fmt.Errorf("Volume exists: %s", volume.ID)
}
r.volumes[volume.Path] = volume
return nil
}
func (r *Repository) Remove(volume *Volume) {
r.lock.Lock()
r.remove(volume)
r.lock.Unlock()
}
func (r *Repository) remove(volume *Volume) {
delete(r.volumes, volume.Path)
}
func (r *Repository) Delete(path string) error {
r.lock.Lock()
defer r.lock.Unlock()
path, err := filepath.EvalSymlinks(path)
if err != nil {
return err
}
volume := r.get(filepath.Clean(path))
if volume == nil {
return fmt.Errorf("Volume %s does not exist", path)
}
if volume.IsBindMount {
return fmt.Errorf("Volume %s is a bind-mount and cannot be removed", volume.Path)
}
containers := volume.Containers()
if len(containers) > 0 {
return fmt.Errorf("Volume %s is being used and cannot be removed: used by containers %s", volume.Path, containers)
}
if err := os.RemoveAll(volume.configPath); err != nil {
return err
}
if err := r.driver.Remove(volume.ID); err != nil {
if !os.IsNotExist(err) {
return err
}
}
r.remove(volume)
return nil
}
func (r *Repository) createNewVolumePath(id string) (string, error) {
if err := r.driver.Create(id, ""); err != nil {
return "", err
}
path, err := r.driver.Get(id, "")
if err != nil {
return "", fmt.Errorf("Driver %s failed to get volume rootfs %s: %v", r.driver, id, err)
}
return path, nil
}
func (r *Repository) FindOrCreateVolume(path string, writable bool) (*Volume, error) {
r.lock.Lock()
defer r.lock.Unlock()
if path == "" {
return r.newVolume(path, writable)
}
if v := r.get(path); v != nil {
return v, nil
}
return r.newVolume(path, writable)
}