Pārlūkot izejas kodu

Merge pull request #8372 from cpuguy83/change_volume_containers_to_private

Make volume.Containers private
Michael Crosby 10 gadi atpakaļ
vecāks
revīzija
6deeb103cf
2 mainītis faili ar 19 papildinājumiem un 6 dzēšanām
  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()
 }