list: report error when non-existent --root is specified

It is questionable whether runc list should return an empty list of
containers when non-existent --root is specified or not.

The current behavior is the directory is always created and then the
empty list of container is shown.

To my mind, specifying a non-existent root is an error and should be
reported as such. This is what this patch does.

For backward compatibility, if --root is not set (i.e. a default is
used), ENOENT is not reported.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2022-02-10 18:58:08 -08:00
parent 2b07e751b5
commit d1fca8e599

15
list.go
View File

@@ -110,12 +110,19 @@ To list containers created using a non-default value for "--root":
}
func getContainers(context *cli.Context) ([]containerState, error) {
factory, err := loadFactory(context)
if err != nil {
return nil, err
}
root := context.GlobalString("root")
list, err := os.ReadDir(root)
if err != nil {
if errors.Is(err, os.ErrNotExist) && context.IsSet("root") {
// Ignore non-existing default root directory
// (no containers created yet).
return nil, nil
}
// Report other errors, including non-existent custom --root.
return nil, err
}
factory, err := loadFactory(context)
if err != nil {
return nil, err
}