layerutils.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package hcsshim
  2. // This file contains utility functions to support storage (graph) related
  3. // functionality.
  4. import (
  5. "path/filepath"
  6. "syscall"
  7. "github.com/Sirupsen/logrus"
  8. )
  9. /* To pass into syscall, we need a struct matching the following:
  10. enum GraphDriverType
  11. {
  12. DiffDriver,
  13. FilterDriver
  14. };
  15. struct DriverInfo {
  16. GraphDriverType Flavour;
  17. LPCWSTR HomeDir;
  18. };
  19. */
  20. type DriverInfo struct {
  21. Flavour int
  22. HomeDir string
  23. }
  24. type driverInfo struct {
  25. Flavour int
  26. HomeDirp *uint16
  27. }
  28. func convertDriverInfo(info DriverInfo) (driverInfo, error) {
  29. homedirp, err := syscall.UTF16PtrFromString(info.HomeDir)
  30. if err != nil {
  31. logrus.Debugf("Failed conversion of home to pointer for driver info: %s", err.Error())
  32. return driverInfo{}, err
  33. }
  34. return driverInfo{
  35. Flavour: info.Flavour,
  36. HomeDirp: homedirp,
  37. }, nil
  38. }
  39. /* To pass into syscall, we need a struct matching the following:
  40. typedef struct _WC_LAYER_DESCRIPTOR {
  41. //
  42. // The ID of the layer
  43. //
  44. GUID LayerId;
  45. //
  46. // Additional flags
  47. //
  48. union {
  49. struct {
  50. ULONG Reserved : 31;
  51. ULONG Dirty : 1; // Created from sandbox as a result of snapshot
  52. };
  53. ULONG Value;
  54. } Flags;
  55. //
  56. // Path to the layer root directory, null-terminated
  57. //
  58. PCWSTR Path;
  59. } WC_LAYER_DESCRIPTOR, *PWC_LAYER_DESCRIPTOR;
  60. */
  61. type WC_LAYER_DESCRIPTOR struct {
  62. LayerId GUID
  63. Flags uint32
  64. Pathp *uint16
  65. }
  66. func layerPathsToDescriptors(parentLayerPaths []string) ([]WC_LAYER_DESCRIPTOR, error) {
  67. // Array of descriptors that gets constructed.
  68. var layers []WC_LAYER_DESCRIPTOR
  69. for i := 0; i < len(parentLayerPaths); i++ {
  70. // Create a layer descriptor, using the folder name
  71. // as the source for a GUID LayerId
  72. _, folderName := filepath.Split(parentLayerPaths[i])
  73. g, err := NameToGuid(folderName)
  74. if err != nil {
  75. logrus.Debugf("Failed to convert name to guid %s", err)
  76. return nil, err
  77. }
  78. p, err := syscall.UTF16PtrFromString(parentLayerPaths[i])
  79. if err != nil {
  80. logrus.Debugf("Failed conversion of parentLayerPath to pointer %s", err)
  81. return nil, err
  82. }
  83. layers = append(layers, WC_LAYER_DESCRIPTOR{
  84. LayerId: g,
  85. Flags: 0,
  86. Pathp: p,
  87. })
  88. }
  89. return layers, nil
  90. }