2018-09-05 14:54:38 -04:00
|
|
|
package wclayer
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2018-03-19 18:57:30 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2019-02-28 13:16:30 -05:00
|
|
|
func ExportLayer(path string, exportFolderPath string, parentLayerPaths []string) (err error) {
|
|
|
|
title := "hcsshim::ExportLayer"
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"path": path,
|
|
|
|
"exportFolderPath": exportFolderPath,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Debug(title)
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
fields[logrus.ErrorKey] = err
|
|
|
|
logrus.WithFields(fields).Error(err)
|
|
|
|
} else {
|
|
|
|
logrus.WithFields(fields).Debug(title + " - succeeded")
|
|
|
|
}
|
|
|
|
}()
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
// Generate layer descriptors
|
|
|
|
layers, err := layerPathsToDescriptors(parentLayerPaths)
|
|
|
|
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.
|
2018-09-05 14:54:38 -04:00
|
|
|
func NewLayerReader(path string, parentLayerPaths []string) (LayerReader, error) {
|
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
|
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
err = ExportLayer(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
|
|
|
}
|
2019-02-28 13:16:30 -05:00
|
|
|
return &legacyLayerReaderWrapper{newLegacyLayerReader(exportPath)}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type legacyLayerReaderWrapper struct {
|
|
|
|
*legacyLayerReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *legacyLayerReaderWrapper) Close() error {
|
|
|
|
err := r.legacyLayerReader.Close()
|
|
|
|
os.RemoveAll(r.root)
|
|
|
|
return err
|
|
|
|
}
|