diff --git a/volumes/repository.go b/volumes/repository.go index 06dbc8f0a4..7e095b8fe0 100644 --- a/volumes/repository.go +++ b/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 { diff --git a/volumes/volume.go b/volumes/volume.go index 9c677dbbb5..e2d7a726db 100644 --- a/volumes/volume.go +++ b/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() }