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