layerutils.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package wclayer
  2. // This file contains utility functions to support storage (graph) related
  3. // functionality.
  4. import (
  5. "context"
  6. "syscall"
  7. "github.com/Microsoft/go-winio/pkg/guid"
  8. "github.com/sirupsen/logrus"
  9. )
  10. /* To pass into syscall, we need a struct matching the following:
  11. enum GraphDriverType
  12. {
  13. DiffDriver,
  14. FilterDriver
  15. };
  16. struct DriverInfo {
  17. GraphDriverType Flavour;
  18. LPCWSTR HomeDir;
  19. };
  20. */
  21. type driverInfo struct {
  22. Flavour int
  23. HomeDirp *uint16
  24. }
  25. var (
  26. utf16EmptyString uint16
  27. stdDriverInfo = driverInfo{1, &utf16EmptyString}
  28. )
  29. /* To pass into syscall, we need a struct matching the following:
  30. typedef struct _WC_LAYER_DESCRIPTOR {
  31. //
  32. // The ID of the layer
  33. //
  34. GUID LayerId;
  35. //
  36. // Additional flags
  37. //
  38. union {
  39. struct {
  40. ULONG Reserved : 31;
  41. ULONG Dirty : 1; // Created from sandbox as a result of snapshot
  42. };
  43. ULONG Value;
  44. } Flags;
  45. //
  46. // Path to the layer root directory, null-terminated
  47. //
  48. PCWSTR Path;
  49. } WC_LAYER_DESCRIPTOR, *PWC_LAYER_DESCRIPTOR;
  50. */
  51. type WC_LAYER_DESCRIPTOR struct {
  52. LayerId guid.GUID
  53. Flags uint32
  54. Pathp *uint16
  55. }
  56. func layerPathsToDescriptors(ctx context.Context, parentLayerPaths []string) ([]WC_LAYER_DESCRIPTOR, error) {
  57. // Array of descriptors that gets constructed.
  58. var layers []WC_LAYER_DESCRIPTOR
  59. for i := 0; i < len(parentLayerPaths); i++ {
  60. g, err := LayerID(ctx, parentLayerPaths[i])
  61. if err != nil {
  62. logrus.WithError(err).Debug("Failed to convert name to guid")
  63. return nil, err
  64. }
  65. p, err := syscall.UTF16PtrFromString(parentLayerPaths[i])
  66. if err != nil {
  67. logrus.WithError(err).Debug("Failed conversion of parentLayerPath to pointer")
  68. return nil, err
  69. }
  70. layers = append(layers, WC_LAYER_DESCRIPTOR{
  71. LayerId: g,
  72. Flags: 0,
  73. Pathp: p,
  74. })
  75. }
  76. return layers, nil
  77. }