Skip to content

Commit

Permalink
utils: remove loadFactory
Browse files Browse the repository at this point in the history
 - move filepath.Abs to libcontainer.New;
 - remove loadFactory.

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Jan 21, 2022
1 parent a8fb256 commit 34afdfa
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
7 changes: 6 additions & 1 deletion libcontainer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"

securejoin "github.com/cyphar/filepath-securejoin"
Expand All @@ -27,11 +28,15 @@ var idRegex = regexp.MustCompile(`^[\w+-\.]+$`)

// New returns a linux based container factory based in the root directory.
func New(root string) (*Factory, error) {
absRoot, err := filepath.Abs(root)
if err != nil {
return nil, err
}
if err := os.MkdirAll(root, 0o700); err != nil {
return nil, err
}
return &Factory{
Root: root,
Root: absRoot,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion list.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ To list containers created using a non-default value for "--root":
}

func getContainers(context *cli.Context) ([]containerState, error) {
factory, err := loadFactory(context)
factory, err := libcontainer.New(context.GlobalString("root"))
if err != nil {
return nil, err
}
Expand Down
15 changes: 2 additions & 13 deletions utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,14 @@ import (

var errEmptyID = errors.New("container id cannot be empty")

// loadFactory returns the configured factory instance for execing containers.
func loadFactory(context *cli.Context) (*libcontainer.Factory, error) {
root := context.GlobalString("root")
abs, err := filepath.Abs(root)
if err != nil {
return nil, err
}

return libcontainer.New(abs)
}

// getContainer returns the specified container instance by loading it from state
// with the default factory.
func getContainer(context *cli.Context) (*libcontainer.Container, error) {
id := context.Args().First()
if id == "" {
return nil, errEmptyID
}
factory, err := loadFactory(context)
factory, err := libcontainer.New(context.GlobalString("root"))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -186,7 +175,7 @@ func createContainer(context *cli.Context, id string, spec *specs.Spec) (*libcon
return nil, err
}

factory, err := loadFactory(context)
factory, err := libcontainer.New(context.GlobalString("root"))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 34afdfa

Please sign in to comment.