From 395f6cecb2e88f995203ae2e76a0c52bb76d0873 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Tue, 10 May 2022 09:55:08 +0200 Subject: [PATCH] Add GetContainerRoot to oci.State type This change adds a GetContainerRoot to the oci.State type to encapsulate the logic around determining the container root. This Fixes a bug where relative roots (e.g. as generated by contianerd) are not supported. Signed-off-by: Evan Lezar --- internal/oci/state.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/internal/oci/state.go b/internal/oci/state.go index d3939b68..2bb4e6e5 100644 --- a/internal/oci/state.go +++ b/internal/oci/state.go @@ -21,6 +21,7 @@ import ( "fmt" "io" "os" + "path/filepath" "github.com/opencontainers/runtime-spec/specs-go" ) @@ -56,7 +57,7 @@ func ReadContainerState(reader io.Reader) (*State, error) { } // LoadSpec loads the OCI spec associated with the container state -func (s State) LoadSpec() (*specs.Spec, error) { +func (s *State) LoadSpec() (*specs.Spec, error) { specFilePath := GetSpecFilePath(s.Bundle) specFile, err := os.Open(specFilePath) if err != nil { @@ -68,6 +69,25 @@ func (s State) LoadSpec() (*specs.Spec, error) { if err != nil { return nil, fmt.Errorf("failed to load OCI spec: %v", err) } - return spec, nil } + +// GetContainerRoot returns the root for the container from the associated spec. If the spec is not yet loaded, it is +// loaded and cached. +func (s *State) GetContainerRoot() (string, error) { + spec, err := s.LoadSpec() + if err != nil { + return "", err + } + + var containerRoot string + if spec.Root != nil { + containerRoot = spec.Root.Path + } + + if filepath.IsAbs(containerRoot) { + return containerRoot, nil + } + + return filepath.Join(s.Bundle, containerRoot), nil +}