mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
516010e92d
This subtle bug keeps lurking in because error checking for `Mkdir()` and `MkdirAll()` is slightly different wrt to `EEXIST`/`IsExist`: - for `Mkdir()`, `IsExist` error should (usually) be ignored (unless you want to make sure directory was not there before) as it means "the destination directory was already there" - for `MkdirAll()`, `IsExist` error should NEVER be ignored. Mostly, this commit just removes ignoring the IsExist error, as it should not be ignored. Also, there are a couple of cases then IsExist is handled as "directory already exist" which is wrong. As a result, some code that never worked as intended is now removed. NOTE that `idtools.MkdirAndChown()` behaves like `os.MkdirAll()` rather than `os.Mkdir()` -- so its description is amended accordingly, and its usage is handled as such (i.e. IsExist error is not ignored). For more details, a quote from my runc commit 6f82d4b (July 2015): TL;DR: check for IsExist(err) after a failed MkdirAll() is both redundant and wrong -- so two reasons to remove it. Quoting MkdirAll documentation: > MkdirAll creates a directory named path, along with any necessary > parents, and returns nil, or else returns an error. If path > is already a directory, MkdirAll does nothing and returns nil. This means two things: 1. If a directory to be created already exists, no error is returned. 2. If the error returned is IsExist (EEXIST), it means there exists a non-directory with the same name as MkdirAll need to use for directory. Example: we want to MkdirAll("a/b"), but file "a" (or "a/b") already exists, so MkdirAll fails. The above is a theory, based on quoted documentation and my UNIX knowledge. 3. In practice, though, current MkdirAll implementation [1] returns ENOTDIR in most of cases described in #2, with the exception when there is a race between MkdirAll and someone else creating the last component of MkdirAll argument as a file. In this very case MkdirAll() will indeed return EEXIST. Because of #1, IsExist check after MkdirAll is not needed. Because of #2 and #3, ignoring IsExist error is just plain wrong, as directory we require is not created. It's cleaner to report the error now. Note this error is all over the tree, I guess due to copy-paste, or trying to follow the same usage pattern as for Mkdir(), or some not quite correct examples on the Internet. [1] https://github.com/golang/go/blob/f9ed2f75/src/os/path.go Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
143 lines
4 KiB
Go
143 lines
4 KiB
Go
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/docker/docker/daemon/names"
|
|
)
|
|
|
|
var (
|
|
validCheckpointNameChars = names.RestrictedNameChars
|
|
validCheckpointNamePattern = names.RestrictedNamePattern
|
|
)
|
|
|
|
// getCheckpointDir verifies checkpoint directory for create,remove, list options and checks if checkpoint already exists
|
|
func getCheckpointDir(checkDir, checkpointID, ctrName, ctrID, ctrCheckpointDir string, create bool) (string, error) {
|
|
var checkpointDir string
|
|
var err2 error
|
|
if checkDir != "" {
|
|
checkpointDir = filepath.Join(checkDir, ctrID, "checkpoints")
|
|
} else {
|
|
checkpointDir = ctrCheckpointDir
|
|
}
|
|
checkpointAbsDir := filepath.Join(checkpointDir, checkpointID)
|
|
stat, err := os.Stat(checkpointAbsDir)
|
|
if create {
|
|
switch {
|
|
case err == nil && stat.IsDir():
|
|
err2 = fmt.Errorf("checkpoint with name %s already exists for container %s", checkpointID, ctrName)
|
|
case err != nil && os.IsNotExist(err):
|
|
err2 = os.MkdirAll(checkpointAbsDir, 0700)
|
|
case err != nil:
|
|
err2 = err
|
|
case err == nil:
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
}
|
|
} else {
|
|
switch {
|
|
case err != nil:
|
|
err2 = fmt.Errorf("checkpoint %s does not exists for container %s", checkpointID, ctrName)
|
|
case err == nil && stat.IsDir():
|
|
err2 = nil
|
|
case err == nil:
|
|
err2 = fmt.Errorf("%s exists and is not a directory", checkpointAbsDir)
|
|
}
|
|
}
|
|
return checkpointAbsDir, err2
|
|
}
|
|
|
|
// CheckpointCreate checkpoints the process running in a container with CRIU
|
|
func (daemon *Daemon) CheckpointCreate(name string, config types.CheckpointCreateOptions) error {
|
|
container, err := daemon.GetContainer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !container.IsRunning() {
|
|
return fmt.Errorf("Container %s not running", name)
|
|
}
|
|
|
|
if container.Config.Tty {
|
|
return fmt.Errorf("checkpoint not support on containers with tty")
|
|
}
|
|
|
|
if !validCheckpointNamePattern.MatchString(config.CheckpointID) {
|
|
return fmt.Errorf("Invalid checkpoint ID (%s), only %s are allowed", config.CheckpointID, validCheckpointNameChars)
|
|
}
|
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, config.CheckpointID, name, container.ID, container.CheckpointDir(), true)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot checkpoint container %s: %s", name, err)
|
|
}
|
|
|
|
err = daemon.containerd.CreateCheckpoint(context.Background(), container.ID, checkpointDir, config.Exit)
|
|
if err != nil {
|
|
os.RemoveAll(checkpointDir)
|
|
return fmt.Errorf("Cannot checkpoint container %s: %s", name, err)
|
|
}
|
|
|
|
daemon.LogContainerEvent(container, "checkpoint")
|
|
|
|
return nil
|
|
}
|
|
|
|
// CheckpointDelete deletes the specified checkpoint
|
|
func (daemon *Daemon) CheckpointDelete(name string, config types.CheckpointDeleteOptions) error {
|
|
container, err := daemon.GetContainer(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, config.CheckpointID, name, container.ID, container.CheckpointDir(), false)
|
|
if err == nil {
|
|
return os.RemoveAll(filepath.Join(checkpointDir, config.CheckpointID))
|
|
}
|
|
return err
|
|
}
|
|
|
|
// CheckpointList lists all checkpoints of the specified container
|
|
func (daemon *Daemon) CheckpointList(name string, config types.CheckpointListOptions) ([]types.Checkpoint, error) {
|
|
var out []types.Checkpoint
|
|
|
|
container, err := daemon.GetContainer(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
checkpointDir, err := getCheckpointDir(config.CheckpointDir, "", name, container.ID, container.CheckpointDir(), false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := os.MkdirAll(checkpointDir, 0755); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
dirs, err := ioutil.ReadDir(checkpointDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, d := range dirs {
|
|
if !d.IsDir() {
|
|
continue
|
|
}
|
|
path := filepath.Join(checkpointDir, d.Name(), "config.json")
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var cpt types.Checkpoint
|
|
if err := json.Unmarshal(data, &cpt); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, cpt)
|
|
}
|
|
|
|
return out, nil
|
|
}
|