2018-09-05 14:54:38 -04:00
|
|
|
package wclayer
|
2018-03-19 18:57:30 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/Microsoft/go-winio"
|
2018-09-05 14:54:38 -04:00
|
|
|
"github.com/Microsoft/hcsshim/internal/hcserror"
|
|
|
|
"github.com/Microsoft/hcsshim/internal/interop"
|
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.
|
2018-09-05 14:54:38 -04:00
|
|
|
func ExportLayer(path string, exportFolderPath string, parentLayerPaths []string) error {
|
2018-03-19 18:57:30 -04:00
|
|
|
title := "hcsshim::ExportLayer "
|
2018-09-05 14:54:38 -04:00
|
|
|
logrus.Debugf(title+"path %s folder %s", path, exportFolderPath)
|
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 {
|
2018-09-05 14:54:38 -04:00
|
|
|
err = hcserror.Errorf(err, title, "path=%s folder=%s", path, exportFolderPath)
|
2018-03-19 18:57:30 -04:00
|
|
|
logrus.Error(err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-09-05 14:54:38 -04:00
|
|
|
logrus.Debugf(title+"succeeded path=%s folder=%s", path, exportFolderPath)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// FilterLayerReader provides an interface for extracting the contents of an on-disk layer.
|
|
|
|
type FilterLayerReader struct {
|
|
|
|
context uintptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next reads the next available file from a layer, ensuring that parent directories are always read
|
|
|
|
// before child files and directories.
|
|
|
|
//
|
|
|
|
// Next returns the file's relative path, size, and basic file metadata. Read() should be used to
|
|
|
|
// extract a Win32 backup stream with the remainder of the metadata and the data.
|
|
|
|
func (r *FilterLayerReader) Next() (string, int64, *winio.FileBasicInfo, error) {
|
|
|
|
var fileNamep *uint16
|
|
|
|
fileInfo := &winio.FileBasicInfo{}
|
|
|
|
var deleted uint32
|
|
|
|
var fileSize int64
|
|
|
|
err := exportLayerNext(r.context, &fileNamep, fileInfo, &fileSize, &deleted)
|
|
|
|
if err != nil {
|
|
|
|
if err == syscall.ERROR_NO_MORE_FILES {
|
|
|
|
err = io.EOF
|
|
|
|
} else {
|
2018-09-05 14:54:38 -04:00
|
|
|
err = hcserror.New(err, "ExportLayerNext", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
return "", 0, nil, err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
fileName := interop.ConvertAndFreeCoTaskMemString(fileNamep)
|
2018-03-19 18:57:30 -04:00
|
|
|
if deleted != 0 {
|
|
|
|
fileInfo = nil
|
|
|
|
}
|
|
|
|
if fileName[0] == '\\' {
|
|
|
|
fileName = fileName[1:]
|
|
|
|
}
|
|
|
|
return fileName, fileSize, fileInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read reads from the current file's Win32 backup stream.
|
|
|
|
func (r *FilterLayerReader) Read(b []byte) (int, error) {
|
|
|
|
var bytesRead uint32
|
|
|
|
err := exportLayerRead(r.context, b, &bytesRead)
|
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return 0, hcserror.New(err, "ExportLayerRead", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
if bytesRead == 0 {
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
return int(bytesRead), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close frees resources associated with the layer reader. It will return an
|
|
|
|
// error if there was an error while reading the layer or of the layer was not
|
|
|
|
// completely read.
|
|
|
|
func (r *FilterLayerReader) Close() (err error) {
|
|
|
|
if r.context != 0 {
|
|
|
|
err = exportLayerEnd(r.context)
|
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
err = hcserror.New(err, "ExportLayerEnd", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
r.context = 0
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2018-03-19 18:57:30 -04:00
|
|
|
if procExportLayerBegin.Find() != nil {
|
|
|
|
// The new layer reader is not available on this Windows build. Fall back to the
|
|
|
|
// legacy export code path.
|
2018-09-05 14:54:38 -04:00
|
|
|
exportPath, err := ioutil.TempDir("", "hcs")
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
err = ExportLayer(path, exportPath, parentLayerPaths)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
os.RemoveAll(exportPath)
|
2018-03-19 18:57:30 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-05 14:54:38 -04:00
|
|
|
return &legacyLayerReaderWrapper{newLegacyLayerReader(exportPath)}, nil
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
layers, err := layerPathsToDescriptors(parentLayerPaths)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &FilterLayerReader{}
|
2018-09-05 14:54:38 -04:00
|
|
|
err = exportLayerBegin(&stdDriverInfo, path, layers, &r.context)
|
2018-03-19 18:57:30 -04:00
|
|
|
if err != nil {
|
2018-09-05 14:54:38 -04:00
|
|
|
return nil, hcserror.New(err, "ExportLayerBegin", "")
|
2018-03-19 18:57:30 -04:00
|
|
|
}
|
|
|
|
return r, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type legacyLayerReaderWrapper struct {
|
|
|
|
*legacyLayerReader
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *legacyLayerReaderWrapper) Close() error {
|
|
|
|
err := r.legacyLayerReader.Close()
|
|
|
|
os.RemoveAll(r.root)
|
|
|
|
return err
|
|
|
|
}
|