1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

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)
This commit is contained in:
Brian Goff 2014-10-02 20:46:17 -04:00
parent 06b74875b6
commit c5e728c953
2 changed files with 19 additions and 6 deletions

View file

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

View file

@ -15,7 +15,7 @@ type Volume struct {
Path string Path string
IsBindMount bool IsBindMount bool
Writable bool Writable bool
Containers map[string]struct{} containers map[string]struct{}
configPath string configPath string
repository *Repository repository *Repository
lock sync.Mutex lock sync.Mutex
@ -30,15 +30,27 @@ func (v *Volume) IsDir() (bool, error) {
return stat.IsDir(), nil 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) { func (v *Volume) RemoveContainer(containerId string) {
v.lock.Lock() v.lock.Lock()
delete(v.Containers, containerId) delete(v.containers, containerId)
v.lock.Unlock() v.lock.Unlock()
} }
func (v *Volume) AddContainer(containerId string) { func (v *Volume) AddContainer(containerId string) {
v.lock.Lock() v.lock.Lock()
v.Containers[containerId] = struct{}{} v.containers[containerId] = struct{}{}
v.lock.Unlock() v.lock.Unlock()
} }