Browse code

Make volume.Containers private

Also wrap access in mutex.
Makes sure we don't have any pontential for races in accessing this.
It also doesn't really need to be/shouldn't be in the config.json anyway

Docker-DCO-1.1-Signed-off-by: Brian Goff <bgoff@cpuguy83-mbp.home> (github: cpuguy83)

Brian Goff authored on 2014/10/03 09:46:17
Showing 2 changed files
... ...
@@ -65,7 +65,7 @@ func (r *Repository) newVolume(path string, writable bool) (*Volume, error) {
65 65
 		Path:        path,
66 66
 		repository:  r,
67 67
 		Writable:    writable,
68
-		Containers:  make(map[string]struct{}),
68
+		containers:  make(map[string]struct{}),
69 69
 		configPath:  r.configPath + "/" + id,
70 70
 		IsBindMount: isBindMount,
71 71
 	}
... ...
@@ -147,8 +147,9 @@ func (r *Repository) Delete(path string) error {
147 147
 	if volume.IsBindMount {
148 148
 		return fmt.Errorf("Volume %s is a bind-mount and cannot be removed", volume.Path)
149 149
 	}
150
-	if len(volume.Containers) > 0 {
151
-		return fmt.Errorf("Volume %s is being used and cannot be removed: used by containers %s", volume.Path, volume.Containers)
150
+	containers := volume.Containers()
151
+	if len(containers) > 0 {
152
+		return fmt.Errorf("Volume %s is being used and cannot be removed: used by containers %s", volume.Path, containers)
152 153
 	}
153 154
 
154 155
 	if err := os.RemoveAll(volume.configPath); err != nil {
... ...
@@ -15,7 +15,7 @@ type Volume struct {
15 15
 	Path        string
16 16
 	IsBindMount bool
17 17
 	Writable    bool
18
-	Containers  map[string]struct{}
18
+	containers  map[string]struct{}
19 19
 	configPath  string
20 20
 	repository  *Repository
21 21
 	lock        sync.Mutex
... ...
@@ -30,15 +30,27 @@ func (v *Volume) IsDir() (bool, error) {
30 30
 	return stat.IsDir(), nil
31 31
 }
32 32
 
33
+func (v *Volume) Containers() []string {
34
+	v.lock.Lock()
35
+
36
+	var containers []string
37
+	for c := range v.containers {
38
+		containers = append(containers, c)
39
+	}
40
+
41
+	v.lock.Unlock()
42
+	return containers
43
+}
44
+
33 45
 func (v *Volume) RemoveContainer(containerId string) {
34 46
 	v.lock.Lock()
35
-	delete(v.Containers, containerId)
47
+	delete(v.containers, containerId)
36 48
 	v.lock.Unlock()
37 49
 }
38 50
 
39 51
 func (v *Volume) AddContainer(containerId string) {
40 52
 	v.lock.Lock()
41
-	v.Containers[containerId] = struct{}{}
53
+	v.containers[containerId] = struct{}{}
42 54
 	v.lock.Unlock()
43 55
 }
44 56