From 812e83d3bbb99d4fa1ece4712a1ac85e84fe6ec3 Mon Sep 17 00:00:00 2001 From: Fabricio Voznika Date: Tue, 29 May 2018 17:57:26 -0700 Subject: Supress error when deleting non-existing container with --force This addresses the first issue reported in #59. CRI-O expects runsc to return success to delete when --force is used with a non-existing container. PiperOrigin-RevId: 198487418 Change-Id: If7660e8fdab1eb29549d0a7a45ea82e20a1d4f4a --- runsc/container/container.go | 35 ++++++++++------------------------- 1 file changed, 10 insertions(+), 25 deletions(-) (limited to 'runsc/container/container.go') diff --git a/runsc/container/container.go b/runsc/container/container.go index ae86e40c9..f20ec2453 100644 --- a/runsc/container/container.go +++ b/runsc/container/container.go @@ -93,21 +93,19 @@ type Container struct { } // Load loads a container with the given id from a metadata file. +// Returns ErrNotExist if container doesn't exits. func Load(rootDir, id string) (*Container, error) { log.Debugf("Load container %q %q", rootDir, id) if err := validateID(id); err != nil { return nil, err } - cRoot := filepath.Join(rootDir, id) - if !exists(cRoot) { - return nil, fmt.Errorf("container with id %q does not exist", id) - } - metaFile := filepath.Join(cRoot, metadataFilename) - if !exists(metaFile) { - return nil, fmt.Errorf("container with id %q does not have metadata file %q", id, metaFile) - } + metaFile := filepath.Join(rootDir, id, metadataFilename) metaBytes, err := ioutil.ReadFile(metaFile) if err != nil { + if os.IsNotExist(err) { + // Preserve error so that callers can distinguish 'not found' errors. + return nil, err + } return nil, fmt.Errorf("error reading container metadata file %q: %v", metaFile, err) } var c Container @@ -161,8 +159,10 @@ func Create(id string, spec *specs.Spec, conf *boot.Config, bundleDir, consoleSo } containerRoot := filepath.Join(conf.RootDir, id) - if exists(containerRoot) { - return nil, fmt.Errorf("container with id %q already exists: %q ", id, containerRoot) + if _, err := os.Stat(containerRoot); err == nil { + return nil, fmt.Errorf("container with id %q already exists: %q", id, containerRoot) + } else if !os.IsNotExist(err) { + return nil, fmt.Errorf("error looking for existing container in %q: %v", containerRoot, err) } c := &Container{ @@ -328,11 +328,6 @@ func (c *Container) Destroy() error { return err } - // Then destroy all the metadata. - if err := os.RemoveAll(c.Root); err != nil { - log.Warningf("Failed to delete container root directory %q, err: %v", c.Root, err) - } - // "If any poststop hook fails, the runtime MUST log a warning, but the // remaining hooks and lifecycle continue as if the hook had succeeded". if c.Spec.Hooks != nil && (c.Status == Created || c.Status == Running) { @@ -372,13 +367,3 @@ func (c *Container) save() error { } return nil } - -// exists returns true if the given file exists. -func exists(f string) bool { - if _, err := os.Stat(f); err == nil { - return true - } else if !os.IsNotExist(err) { - log.Warningf("error checking for file %q: %v", f, err) - } - return false -} -- cgit v1.2.3