diff options
author | Daniel J Walsh <dwalsh@redhat.com> | 2017-12-11 10:48:29 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-11 10:48:29 -0600 |
commit | 12682aa475db17d99eb0cfc5efad20e1b9f3685f (patch) | |
tree | e0f280f5f892025bea20a8cfd2c5cc4e7e1f0dc0 /libpod/container.go | |
parent | 62e19beeecc8f4af97388c0e715c92b582fbe685 (diff) | |
parent | 190b05209f95cf611eb5cafc4bf4cce875c74e9e (diff) | |
download | podman-12682aa475db17d99eb0cfc5efad20e1b9f3685f.tar.gz podman-12682aa475db17d99eb0cfc5efad20e1b9f3685f.tar.bz2 podman-12682aa475db17d99eb0cfc5efad20e1b9f3685f.zip |
Merge pull request #72 from mheon/file_locking
Move containers to file locks from c/storage
Diffstat (limited to 'libpod/container.go')
-rw-r--r-- | libpod/container.go | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/libpod/container.go b/libpod/container.go index 70011963d..fce64b0dd 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -8,7 +8,6 @@ import ( "os" "path/filepath" "strconv" - "sync" "syscall" "time" @@ -64,9 +63,8 @@ type Container struct { state *containerRuntimeInfo - // TODO move to storage.Locker from sync.Mutex valid bool - lock sync.Mutex + lock storage.Locker runtime *Runtime } @@ -319,6 +317,8 @@ func (c *Container) attachSocketPath() string { // Sync this container with on-disk state and runc status // Should only be called with container lock held +// This function should suffice to ensure a container's state is accurate and +// it is valid for use. func (c *Container) syncContainer() error { if err := c.runtime.state.UpdateContainer(c); err != nil { return err @@ -343,7 +343,7 @@ func (c *Container) syncContainer() error { } // Make a new container -func newContainer(rspec *spec.Spec) (*Container, error) { +func newContainer(rspec *spec.Spec, lockDir string) (*Container, error) { if rspec == nil { return nil, errors.Wrapf(ErrInvalidArg, "must provide a valid runtime spec to create container") } @@ -360,6 +360,20 @@ func newContainer(rspec *spec.Spec) (*Container, error) { ctr.config.CreatedTime = time.Now() + // Path our lock file will reside at + lockPath := filepath.Join(lockDir, ctr.config.ID) + // Ensure there is no conflict - file does not exist + _, err := os.Stat(lockPath) + if err == nil || !os.IsNotExist(err) { + return nil, errors.Wrapf(ErrCtrExists, "lockfile for container ID %s already exists", ctr.config.ID) + } + // Grab a lockfile at the given path + lock, err := storage.GetLockfile(lockPath) + if err != nil { + return nil, errors.Wrapf(err, "error creating lockfile for new container") + } + ctr.lock = lock + return ctr, nil } @@ -904,11 +918,7 @@ func (c *Container) mountStorage() (err error) { } }() - if err := c.runtime.state.SaveContainer(c); err != nil { - return errors.Wrapf(err, "error saving container %s state", c.ID()) - } - - return nil + return c.save() } // CleanupStorage unmounts all mount points in container and cleans up container storage @@ -944,9 +954,5 @@ func (c *Container) cleanupStorage() error { c.state.Mountpoint = "" c.state.Mounted = false - if err := c.runtime.state.SaveContainer(c); err != nil { - return errors.Wrapf(err, "error saving container %s state", c.ID()) - } - - return nil + return c.save() } |