diff --git a/libpod/boltdb_state.go b/libpod/boltdb_state.go index 4c6159fca8..3f643d8f16 100644 --- a/libpod/boltdb_state.go +++ b/libpod/boltdb_state.go @@ -18,10 +18,10 @@ import ( // BoltState is a state implementation backed by a Bolt DB type BoltState struct { - valid bool - dbPath string - dbLock sync.Mutex - runtime *Runtime + valid bool + dbPath string + dbLock sync.Mutex + runtime *Runtime } // A brief description of the format of the BoltDB state: diff --git a/libpod/sqlite_state.go b/libpod/sqlite_state.go index 7e53c9da55..6a3a83632a 100644 --- a/libpod/sqlite_state.go +++ b/libpod/sqlite_state.go @@ -14,6 +14,7 @@ import ( "github.com/containers/storage" "github.com/sirupsen/logrus" + // SQLite backend for database/sql _ "github.com/mattn/go-sqlite3" ) @@ -54,7 +55,7 @@ func NewSqliteState(runtime *Runtime) (_ State, defErr error) { if _, err := state.conn.Exec("PRAGMA journal_mode = WAL;"); err != nil { return nil, fmt.Errorf("switching journal to WAL mode: %w", err) } - if _, err := state.conn.Exec("PRAGMA syncronous = FULL;"); err != nil { + if _, err := state.conn.Exec("PRAGMA synchronous = FULL;"); err != nil { return nil, fmt.Errorf("setting full fsync mode in db: %w", err) } @@ -92,7 +93,9 @@ func (s *SQLiteState) Refresh() (defErr error) { // Retrieve all containers, pods, and volumes. // Maps are indexed by ID (or volume name) so we know which goes where, // and store the marshalled state JSON - var ctrStates, podStates, volumeStates map[string]string + ctrStates := make(map[string]string) + podStates := make(map[string]string) + volumeStates := make(map[string]string) ctrRows, err := s.conn.Query("SELECT Id, Json FROM ContainerState;") if err != nil { @@ -117,12 +120,12 @@ func (s *SQLiteState) Refresh() (defErr error) { // Refresh the state resetContainerState(ctrState) - stateJson, err := json.Marshal(ctrState) + newJSON, err := json.Marshal(ctrState) if err != nil { return fmt.Errorf("marshalling container state json: %w", err) } - ctrStates[id] = string(stateJson) + ctrStates[id] = string(newJSON) } podRows, err := s.conn.Query("SELECT Id, Json FROM PodState;") @@ -148,12 +151,12 @@ func (s *SQLiteState) Refresh() (defErr error) { // Refresh the state resetPodState(podState) - stateJson, err := json.Marshal(podState) + newJSON, err := json.Marshal(podState) if err != nil { return fmt.Errorf("marshalling pod state json: %w", err) } - podStates[id] = string(stateJson) + podStates[id] = string(newJSON) } volRows, err := s.conn.Query("SELECT Name, Json FROM VolumeState;") @@ -168,7 +171,7 @@ func (s *SQLiteState) Refresh() (defErr error) { ) if err := volRows.Scan(&name, &stateJSON); err != nil { - return fmt.Errorf("scanning volume state row: %w") + return fmt.Errorf("scanning volume state row: %w", err) } volState := new(VolumeState) @@ -180,12 +183,12 @@ func (s *SQLiteState) Refresh() (defErr error) { // Refresh the state resetVolumeState(volState) - stateJson, err := json.Marshal(volState) + newJSON, err := json.Marshal(volState) if err != nil { return fmt.Errorf("marshalling volume state json: %w", err) } - volumeStates[name] = string(stateJson) + volumeStates[name] = string(newJSON) } // Write updated states back to DB, and perform additional maintenance @@ -549,7 +552,7 @@ func (s *SQLiteState) HasContainer(id string) (bool, error) { } return false, fmt.Errorf("looking up container %s in database: %w", id, err) } else if check != 1 { - return false, fmt.Errorf("check digit for container %s lookup incorrect", id, define.ErrInternal) + return false, fmt.Errorf("check digit for container %s lookup incorrect: %w", id, define.ErrInternal) } return true, nil diff --git a/libpod/sqlite_state_internal.go b/libpod/sqlite_state_internal.go index 2a83f69276..4d7f7d9f02 100644 --- a/libpod/sqlite_state_internal.go +++ b/libpod/sqlite_state_internal.go @@ -10,6 +10,7 @@ import ( "github.com/containers/podman/v4/libpod/define" "github.com/sirupsen/logrus" + // SQLite backend for database/sql _ "github.com/mattn/go-sqlite3" ) @@ -156,18 +157,18 @@ func sqliteInitTables(conn *sql.DB) (defErr error) { );` tables := map[string]string{ - "DBConfig": dbConfig, - "IDNamespace": idNamespace, - "ContainerConfig": containerConfig, - "ContainerState": containerState, + "DBConfig": dbConfig, + "IDNamespace": idNamespace, + "ContainerConfig": containerConfig, + "ContainerState": containerState, "ContainerExecSession": containerExecSession, - "ContainerDependency": containerDependency, - "ContainerVolume": containerVolume, - "ContainerExitCode": containerExitCode, - "PodConfig": podConfig, - "PodState": podState, - "VolumeConfig": volumeConfig, - "volumeState": volumeState, + "ContainerDependency": containerDependency, + "ContainerVolume": containerVolume, + "ContainerExitCode": containerExitCode, + "PodConfig": podConfig, + "PodState": podState, + "VolumeConfig": volumeConfig, + "volumeState": volumeState, } tx, err := conn.Begin() @@ -364,11 +365,12 @@ func (s *SQLiteState) addContainer(ctr *Container) (defErr error) { return fmt.Errorf("container dependency %s does not exist in database: %w", dep, define.ErrNoSuchCtr) } } - if ctr.config.Pod == "" && depPod.Valid { + switch { + case ctr.config.Pod == "" && depPod.Valid: return fmt.Errorf("container dependency %s is part of a pod, but container is not: %w", dep, define.ErrInvalidArg) - } else if ctr.config.Pod != "" && !depPod.Valid { - return fmt.Errorf("container dependency %s is not part of a pod, but this container belongs to pod %s", dep, ctr.config.Pod, define.ErrInvalidArg) - } else if ctr.config.Pod != "" && depPod.String != ctr.config.Pod { + case ctr.config.Pod != "" && !depPod.Valid: + return fmt.Errorf("container dependency %s is not part of pod, but this container belongs to pod %s: %w", dep, ctr.config.Pod, define.ErrInvalidArg) + case ctr.config.Pod != "" && depPod.String != ctr.config.Pod: return fmt.Errorf("container dependency %s is part of pod %s but container is part of pod %s, pods must match: %w", dep, depPod.String, ctr.config.Pod, define.ErrInvalidArg) }