exportlayer.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //go:build windows
  2. package wclayer
  3. import (
  4. "context"
  5. "os"
  6. "strings"
  7. "github.com/Microsoft/go-winio"
  8. "github.com/Microsoft/hcsshim/internal/hcserror"
  9. "github.com/Microsoft/hcsshim/internal/oc"
  10. "go.opencensus.io/trace"
  11. )
  12. // ExportLayer will create a folder at exportFolderPath and fill that folder with
  13. // the transport format version of the layer identified by layerId. This transport
  14. // format includes any metadata required for later importing the layer (using
  15. // ImportLayer), and requires the full list of parent layer paths in order to
  16. // perform the export.
  17. func ExportLayer(ctx context.Context, path string, exportFolderPath string, parentLayerPaths []string) (err error) {
  18. title := "hcsshim::ExportLayer"
  19. ctx, span := oc.StartSpan(ctx, title)
  20. defer span.End()
  21. defer func() { oc.SetSpanStatus(span, err) }()
  22. span.AddAttributes(
  23. trace.StringAttribute("path", path),
  24. trace.StringAttribute("exportFolderPath", exportFolderPath),
  25. trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
  26. // Generate layer descriptors
  27. layers, err := layerPathsToDescriptors(ctx, parentLayerPaths)
  28. if err != nil {
  29. return err
  30. }
  31. err = exportLayer(&stdDriverInfo, path, exportFolderPath, layers)
  32. if err != nil {
  33. return hcserror.New(err, title, "")
  34. }
  35. return nil
  36. }
  37. // LayerReader is an interface that supports reading an existing container image layer.
  38. type LayerReader interface {
  39. // Next advances to the next file and returns the name, size, and file info
  40. Next() (string, int64, *winio.FileBasicInfo, error)
  41. // LinkInfo returns the number of links and the file identifier for the current file.
  42. LinkInfo() (uint32, *winio.FileIDInfo, error)
  43. // Read reads data from the current file, in the format of a Win32 backup stream, and
  44. // returns the number of bytes read.
  45. Read(b []byte) (int, error)
  46. // Close finishes the layer reading process and releases any resources.
  47. Close() error
  48. }
  49. // NewLayerReader returns a new layer reader for reading the contents of an on-disk layer.
  50. // The caller must have taken the SeBackupPrivilege privilege
  51. // to call this and any methods on the resulting LayerReader.
  52. func NewLayerReader(ctx context.Context, path string, parentLayerPaths []string) (_ LayerReader, err error) {
  53. ctx, span := oc.StartSpan(ctx, "hcsshim::NewLayerReader")
  54. defer func() {
  55. if err != nil {
  56. oc.SetSpanStatus(span, err)
  57. span.End()
  58. }
  59. }()
  60. span.AddAttributes(
  61. trace.StringAttribute("path", path),
  62. trace.StringAttribute("parentLayerPaths", strings.Join(parentLayerPaths, ", ")))
  63. if len(parentLayerPaths) == 0 {
  64. // This is a base layer. It gets exported differently.
  65. return newBaseLayerReader(path, span), nil
  66. }
  67. exportPath, err := os.MkdirTemp("", "hcs")
  68. if err != nil {
  69. return nil, err
  70. }
  71. err = ExportLayer(ctx, path, exportPath, parentLayerPaths)
  72. if err != nil {
  73. os.RemoveAll(exportPath)
  74. return nil, err
  75. }
  76. return &legacyLayerReaderWrapper{
  77. ctx: ctx,
  78. s: span,
  79. legacyLayerReader: newLegacyLayerReader(exportPath),
  80. }, nil
  81. }
  82. type legacyLayerReaderWrapper struct {
  83. ctx context.Context
  84. s *trace.Span
  85. *legacyLayerReader
  86. }
  87. func (r *legacyLayerReaderWrapper) Close() (err error) {
  88. defer r.s.End()
  89. defer func() { oc.SetSpanStatus(r.s, err) }()
  90. err = r.legacyLayerReader.Close()
  91. os.RemoveAll(r.root)
  92. return err
  93. }