1
0
mirror of https://github.com/opencontainers/runc.git synced 2025-04-18 19:44:09 +03:00

reviseRootDir: skip default values, add validation

1. In case --root option is not provided, do nothing.

2. Instead of checking if root value is empty string, check it after
   filepath.Abs, and reject "/". Improve docstring while at it.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2022-02-11 13:18:56 -08:00
parent 899342b5d4
commit 2b07e751b5

View File

@ -1,6 +1,7 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -96,17 +97,25 @@ func revisePidFile(context *cli.Context) error {
return context.Set("pid-file", pidFile)
}
// reviseRootDir convert the root to absolute path
// reviseRootDir ensures that the --root option argument,
// if specified, is converted to an absolute and cleaned path,
// and that this path is sane.
func reviseRootDir(context *cli.Context) error {
root := context.GlobalString("root")
if root == "" {
if !context.IsSet("root") {
return nil
}
root, err := filepath.Abs(root)
root, err := filepath.Abs(context.GlobalString("root"))
if err != nil {
return err
}
if root == "/" {
// This can happen if --root argument is
// - "" (i.e. empty);
// - "." (and the CWD is /);
// - "../../.." (enough to get to /);
// - "/" (the actual /).
return errors.New("Option --root argument should not be set to /")
}
return context.GlobalSet("root", root)
}