2018-09-05 14:54:38 -04:00
|
|
|
package wclayer
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
2020-05-09 11:16:54 -04:00
|
|
|
"context"
|
2018-03-19 18:57:30 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-05-09 11:16:54 -04:00
|
|
|
"strings"
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2020-05-09 11:16:54 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/oc"
|
|
|
|
"go.opencensus.io/trace"
|
2018-03-19 18:57:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ExportLayer will create a folder at exportFolderPath and fill that folder with
|
|
|
|
// the transport format version of the layer identified by layerId. This transport
|
|
|
|
// format includes any metadata required for later importing the layer (using
|
|
|
|
// ImportLayer), and requires the full list of parent layer paths in order to
|
|
|
|
// perform the export.
|
2020-05-09 11:16:54 -04:00
|
|
|
func ExportLayer(ctx context.Context, path string, exportFolderPath string, parentLayerPaths []string) (err error) {
|
2019-02-28 13:16:30 -05:00
|
|
|
title := "hcsshim::ExportLayer"
|
2020-05-09 11:16:54 -04:00
|
|
|
ctx, span := trace.StartSpan(ctx, title)
|
|
|
|
defer span.End()
|
|
|
|
defer func() { oc.SetSpanStatus(span, err) }()
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("path", path),
|
|
|
|
trace.StringAttribute("exportFolderPath", exportFolderPath),
|
|
|
|
trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Generate layer descriptors
|
2020-05-09 11:16:54 -04:00
|
|
|
layers, err := layerPathsToDescriptors(ctx, parentLayerPaths)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
err = exportLayer(&stdDriverInfo, path, exportFolderPath, layers)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2019-02-28 13:16:30 -05:00
|
|
|
return hcserror.New(err, title+" - failed", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type LayerReader interface {
|
|
|
|
Next() (string, int64, *winio.FileBasicInfo, error)
|
|
|
|
Read(b []byte) (int, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLayerReader returns a new layer reader for reading the contents of an on-disk layer.
|
|
|
|
// The caller must have taken the SeBackupPrivilege privilege
|
|
|
|
// to call this and any methods on the resulting LayerReader.
|
2020-05-09 11:16:54 -04:00
|
|
|
func NewLayerReader(ctx context.Context, path string, parentLayerPaths []string) (_ LayerReader, err error) {
|
|
|
|
ctx, span := trace.StartSpan(ctx, "hcsshim::NewLayerReader")
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
oc.SetSpanStatus(span, err)
|
|
|
|
span.End()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
span.AddAttributes(
|
|
|
|
trace.StringAttribute("path", path),
|
|
|
|
trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
|
|
|
|
|
2019-02-28 13:16:30 -05:00
|
|
|
exportPath, err := ioutil.TempDir("", "hcs")
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-09 11:16:54 -04:00
|
|
|
err = ExportLayer(ctx, path, exportPath, parentLayerPaths)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2019-02-28 13:16:30 -05:00
|
|
|
os.RemoveAll(exportPath)
|
|
|
|
return nil, err
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
2020-05-09 11:16:54 -04:00
|
|
|
return &legacyLayerReaderWrapper{
|
|
|
|
ctx: ctx,
|
|
|
|
s: span,
|
|
|
|
legacyLayerReader: newLegacyLayerReader(exportPath),
|
|
|
|
}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type legacyLayerReaderWrapper struct {
|
2020-05-09 11:16:54 -04:00
|
|
|
ctx context.Context
|
|
|
|
s *trace.Span
|
|
|
|
|
2018-03-19 18:57:30 -04:00
|
|
|
*legacyLayerReader
|
|
|
|
}
|
|
|
|
|
2020-05-09 11:16:54 -04:00
|
|
|
func (r *legacyLayerReaderWrapper) Close() (err error) {
|
|
|
|
defer r.s.End()
|
|
|
|
defer func() { oc.SetSpanStatus(r.s, err) }()
|
|
|
|
|
|
|
|
err = r.legacyLayerReader.Close()
|
2018-03-19 18:57:30 -04:00
|
|
|
os.RemoveAll(r.root)
|
|
|
|
return err
|
|
|
|
}
|