Sfoglia il codice sorgente

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 10 anni fa
parent
commit
c5e728c953
2 ha cambiato i file con 19 aggiunte e 6 eliminazioni
  1. 4 3
      volumes/repository.go
  2. 15 3
      volumes/volume.go

+ 4 - 3
volumes/repository.go

@@ -65,7 +65,7 @@ func (r *Repository) newVolume(path string, writable bool) (*Volume, error) {
 		Path:        path,
 		repository:  r,
 		Writable:    writable,
-		Containers:  make(map[string]struct{}),
+		containers:  make(map[string]struct{}),
 		configPath:  r.configPath + "/" + id,
 		IsBindMount: isBindMount,
 	}
@@ -147,8 +147,9 @@ func (r *Repository) Delete(path string) error {
 	if volume.IsBindMount {
 		return fmt.Errorf("Volume %s is a bind-mount and cannot be removed", volume.Path)
 	}
-	if len(volume.Containers) > 0 {
-		return fmt.Errorf("Volume %s is being used and cannot be removed: used by containers %s", volume.Path, volume.Containers)
+	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 {

+ 15 - 3
volumes/volume.go

@@ -15,7 +15,7 @@ type Volume struct {
 	Path        string
 	IsBindMount bool
 	Writable    bool
-	Containers  map[string]struct{}
+	containers  map[string]struct{}
 	configPath  string
 	repository  *Repository
 	lock        sync.Mutex
@@ -30,15 +30,27 @@ func (v *Volume) IsDir() (bool, error) {
 	return stat.IsDir(), nil
 }
 
+func (v *Volume) Containers() []string {
+	v.lock.Lock()
+
+	var containers []string
+	for c := range v.containers {
+		containers = append(containers, c)
+	}
+
+	v.lock.Unlock()
+	return containers
+}
+
 func (v *Volume) RemoveContainer(containerId string) {
 	v.lock.Lock()
-	delete(v.Containers, containerId)
+	delete(v.containers, containerId)
 	v.lock.Unlock()
 }
 
 func (v *Volume) AddContainer(containerId string) {
 	v.lock.Lock()
-	v.Containers[containerId] = struct{}{}
+	v.containers[containerId] = struct{}{}
 	v.lock.Unlock()
 }