2018-08-31 01:18:39 +00:00
|
|
|
package wclayer
|
2015-05-27 13:15:14 -07:00
|
|
|
|
2016-03-02 14:17:13 -08:00
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-08-31 01:18:39 +00:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
2017-07-26 15:03:47 -07:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-03-02 14:17:13 -08:00
|
|
|
)
|
2015-05-27 13:15:14 -07:00
|
|
|
|
2015-08-27 15:46:00 -07: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.
|
2019-02-05 09:52:31 -08: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")
|
|
|
|
}
|
|
|
|
}()
|
2015-05-27 13:15:14 -07:00
|
|
|
|
|
|
|
// Generate layer descriptors
|
|
|
|
layers, err := layerPathsToDescriptors(parentLayerPaths)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-08-31 01:18:39 +00:00
|
|
|
err = exportLayer(&stdDriverInfo, path, exportFolderPath, layers)
|
2015-05-27 13:15:14 -07:00
|
|
|
if err != nil {
|
2019-02-05 09:52:31 -08:00
|
|
|
return hcserror.New(err, title+" - failed", "")
|
2015-05-27 13:15:14 -07:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-03-02 14:17:13 -08:00
|
|
|
|
|
|
|
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.
|
2016-05-23 16:12:06 -07:00
|
|
|
// The caller must have taken the SeBackupPrivilege privilege
|
|
|
|
// to call this and any methods on the resulting LayerReader.
|
2018-08-31 01:18:39 +00:00
|
|
|
func NewLayerReader(path string, parentLayerPaths []string) (LayerReader, error) {
|
2018-12-03 11:42:55 -08:00
|
|
|
exportPath, err := ioutil.TempDir("", "hcs")
|
2016-03-02 14:17:13 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-03 11:42:55 -08:00
|
|
|
err = ExportLayer(path, exportPath, parentLayerPaths)
|
2016-03-02 14:17:13 -08:00
|
|
|
if err != nil {
|
2018-12-03 11:42:55 -08:00
|
|
|
os.RemoveAll(exportPath)
|
|
|
|
return nil, err
|
2016-03-02 14:17:13 -08:00
|
|
|
}
|
2018-12-03 11:42:55 -08:00
|
|
|
return &legacyLayerReaderWrapper{newLegacyLayerReader(exportPath)}, nil
|
2016-03-02 14:17:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
type legacyLayerReaderWrapper struct {
|
2016-03-30 14:23:51 -07:00
|
|
|
*legacyLayerReader
|
2016-03-02 14:17:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *legacyLayerReaderWrapper) Close() error {
|
2016-03-30 14:23:51 -07:00
|
|
|
err := r.legacyLayerReader.Close()
|
2016-03-02 14:17:13 -08:00
|
|
|
os.RemoveAll(r.root)
|
|
|
|
return err
|
|
|
|
}
|