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 package main
import ( import (
"errors"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -96,17 +97,25 @@ func revisePidFile(context *cli.Context) error {
return context.Set("pid-file", pidFile) 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 { func reviseRootDir(context *cli.Context) error {
root := context.GlobalString("root") if !context.IsSet("root") {
if root == "" {
return nil return nil
} }
root, err := filepath.Abs(context.GlobalString("root"))
root, err := filepath.Abs(root)
if err != nil { if err != nil {
return err 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) return context.GlobalSet("root", root)
} }