2021-10-29 14:44:31 +00:00
|
|
|
/*
|
|
|
|
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package oci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
2021-10-29 14:44:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileSpec struct {
|
2022-01-31 14:11:36 +00:00
|
|
|
memorySpec
|
2021-10-29 14:44:31 +00:00
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Spec = (*fileSpec)(nil)
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
// NewFileSpec creates an object that encapsulates a file-backed OCI spec.
|
2021-10-29 14:44:31 +00:00
|
|
|
// This can be used to read from the file, modify the spec, and write to the
|
|
|
|
// same file.
|
2022-01-31 14:11:36 +00:00
|
|
|
func NewFileSpec(filepath string) Spec {
|
2021-10-29 14:44:31 +00:00
|
|
|
oci := fileSpec{
|
|
|
|
path: filepath,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &oci
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load reads the contents of an OCI spec from file to be referenced internally.
|
|
|
|
// The file is opened "read-only"
|
|
|
|
func (s *fileSpec) Load() error {
|
|
|
|
specFile, err := os.Open(s.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening OCI specification file: %v", err)
|
|
|
|
}
|
|
|
|
defer specFile.Close()
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
spec, err := loadFrom(specFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error loading OCI specification from file: %v", err)
|
|
|
|
}
|
|
|
|
s.Spec = spec
|
|
|
|
return nil
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// loadFrom reads the contents of the OCI spec from the specified io.Reader.
|
2022-01-31 14:11:36 +00:00
|
|
|
func loadFrom(reader io.Reader) (*specs.Spec, error) {
|
2021-10-29 14:44:31 +00:00
|
|
|
decoder := json.NewDecoder(reader)
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
var spec specs.Spec
|
|
|
|
|
2021-10-29 14:44:31 +00:00
|
|
|
err := decoder.Decode(&spec)
|
|
|
|
if err != nil {
|
2022-01-31 14:11:36 +00:00
|
|
|
return nil, fmt.Errorf("error reading OCI specification: %v", err)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
return &spec, nil
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Modify applies the specified SpecModifier to the stored OCI specification.
|
2022-01-31 14:11:36 +00:00
|
|
|
func (s *fileSpec) Modify(m SpecModifier) error {
|
|
|
|
return s.memorySpec.Modify(m)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush writes the stored OCI specification to the filepath specifed by the path member.
|
|
|
|
// The file is truncated upon opening, overwriting any existing contents.
|
|
|
|
func (s fileSpec) Flush() error {
|
|
|
|
if s.Spec == nil {
|
|
|
|
return fmt.Errorf("no OCI specification loaded")
|
|
|
|
}
|
|
|
|
|
|
|
|
specFile, err := os.Create(s.path)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error opening OCI specification file: %v", err)
|
|
|
|
}
|
|
|
|
defer specFile.Close()
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
return flushTo(s.Spec, specFile)
|
2021-10-29 14:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// flushTo writes the stored OCI specification to the specified io.Writer.
|
2022-01-31 14:11:36 +00:00
|
|
|
func flushTo(spec *specs.Spec, writer io.Writer) error {
|
|
|
|
if spec == nil {
|
2021-10-29 14:44:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
encoder := json.NewEncoder(writer)
|
|
|
|
|
2022-01-31 14:11:36 +00:00
|
|
|
err := encoder.Encode(spec)
|
2021-10-29 14:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error writing OCI specification: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|