summaryrefslogtreecommitdiff
path: root/libpod/sql_state.go
diff options
context:
space:
mode:
authorDaniel J Walsh <dwalsh@redhat.com>2017-12-11 10:48:29 -0600
committerGitHub <noreply@github.com>2017-12-11 10:48:29 -0600
commit12682aa475db17d99eb0cfc5efad20e1b9f3685f (patch)
treee0f280f5f892025bea20a8cfd2c5cc4e7e1f0dc0 /libpod/sql_state.go
parent62e19beeecc8f4af97388c0e715c92b582fbe685 (diff)
parent190b05209f95cf611eb5cafc4bf4cce875c74e9e (diff)
downloadpodman-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/sql_state.go')
-rw-r--r--libpod/sql_state.go41
1 files changed, 13 insertions, 28 deletions
diff --git a/libpod/sql_state.go b/libpod/sql_state.go
index bba697d18..36114e9cc 100644
--- a/libpod/sql_state.go
+++ b/libpod/sql_state.go
@@ -6,7 +6,6 @@ import (
"io/ioutil"
"os"
- "github.com/containers/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@@ -22,24 +21,17 @@ const DBSchema = 2
type SQLState struct {
db *sql.DB
specsDir string
+ lockDir string
runtime *Runtime
- lock storage.Locker
valid bool
}
// NewSQLState initializes a SQL-backed state, created the database if necessary
-func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, error) {
+func NewSQLState(dbPath, specsDir, lockDir string, runtime *Runtime) (State, error) {
state := new(SQLState)
state.runtime = runtime
- // Make our lock file
- lock, err := storage.GetLockfile(lockPath)
- if err != nil {
- return nil, errors.Wrapf(err, "error creating lockfile for state")
- }
- state.lock = lock
-
// Make the directory that will hold JSON copies of container runtime specs
if err := os.MkdirAll(specsDir, 0750); err != nil {
// The directory is allowed to exist
@@ -49,9 +41,14 @@ func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, er
}
state.specsDir = specsDir
- // Acquire the lock while we open the database and perform initial setup
- state.lock.Lock()
- defer state.lock.Unlock()
+ // Make the directory that will hold container lockfiles
+ if err := os.MkdirAll(lockDir, 0750); err != nil {
+ // The directory is allowed to exist
+ if !os.IsExist(err) {
+ return nil, errors.Wrapf(err, "error creating lockfiles dir %s", lockDir)
+ }
+ }
+ state.lockDir = lockDir
// TODO add a separate temporary database for per-boot container
// state
@@ -87,9 +84,6 @@ func NewSQLState(dbPath, lockPath, specsDir string, runtime *Runtime) (State, er
// Close the state's database connection
func (s *SQLState) Close() error {
- s.lock.Lock()
- defer s.lock.Unlock()
-
if !s.valid {
return ErrDBClosed
}
@@ -176,7 +170,7 @@ func (s *SQLState) Container(id string) (*Container, error) {
row := s.db.QueryRow(query, id)
- ctr, err := ctrFromScannable(row, s.runtime, s.specsDir)
+ ctr, err := ctrFromScannable(row, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving container %s from database", id)
}
@@ -223,7 +217,7 @@ func (s *SQLState) LookupContainer(idOrName string) (*Container, error) {
}
var err error
- ctr, err = ctrFromScannable(rows, s.runtime, s.specsDir)
+ ctr, err = ctrFromScannable(rows, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving container %s from database", idOrName)
}
@@ -296,9 +290,6 @@ func (s *SQLState) AddContainer(ctr *Container) (err error) {
return errors.Wrapf(err, "error marshaling container %s labels to JSON", ctr.ID())
}
- s.lock.Lock()
- defer s.lock.Unlock()
-
tx, err := s.db.Begin()
if err != nil {
return errors.Wrapf(err, "error beginning database transaction")
@@ -477,9 +468,6 @@ func (s *SQLState) SaveContainer(ctr *Container) error {
Pid=?
WHERE Id=?;`
- s.lock.Lock()
- defer s.lock.Unlock()
-
if !s.valid {
return ErrDBClosed
}
@@ -537,9 +525,6 @@ func (s *SQLState) RemoveContainer(ctr *Container) error {
removeState = "DELETE FROM containerState WHERE ID=?;"
)
- s.lock.Lock()
- defer s.lock.Unlock()
-
if !s.valid {
return ErrDBClosed
}
@@ -622,7 +607,7 @@ func (s *SQLState) AllContainers() ([]*Container, error) {
containers := []*Container{}
for rows.Next() {
- ctr, err := ctrFromScannable(rows, s.runtime, s.specsDir)
+ ctr, err := ctrFromScannable(rows, s.runtime, s.specsDir, s.lockDir)
if err != nil {
return nil, err
}