Merge pull request #8372 from cpuguy83/change_volume_containers_to_private

Make volume.Containers private
This commit is contained in:
Michael Crosby 2014-10-03 10:44:21 -07:00
Родитель 06b74875b6 c5e728c953
Коммит 6deeb103cf
2 изменённых файлов: 19 добавлений и 6 удалений

Просмотреть файл

@ -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,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()
}