From d1fca8e599837a694ea4e9a6892b018456e6e1ae Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Thu, 10 Feb 2022 18:58:08 -0800 Subject: [PATCH] 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 --- list.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/list.go b/list.go index 2a1b0adc7..f63c18d73 100644 --- a/list.go +++ b/list.go @@ -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 }