master
h 119 lines 2.47 KB
Raw
1 /*++
2
3 Copyright (c) Microsoft. All rights reserved.
4
5 Module Name:
6
7 MountSpecParsing.h
8
9 Abstract:
10
11 Docker-compatible mount specification parsing.
12
13 --*/
14
15 #pragma once
16
17 #include "wslc.h"
18 #include <cstdint>
19 #include <exception>
20 #include <optional>
21 #include <span>
22 #include <string>
23 #include <string_view>
24 #include <utility>
25
26 namespace wsl::windows::common::mount {
27
28 using Type = WSLCMountType;
29
30 enum class BindSourcePolicy
31 {
32 RequireExisting,
33 CreateIfMissing,
34 };
35
36 struct Spec
37 {
38 Type MountType = WSLCMountTypeVolume;
39 std::wstring Source;
40 std::string Target;
41 bool ReadOnly = false;
42 BindSourcePolicy BindSource = BindSourcePolicy::RequireExisting;
43 std::optional<int64_t> TmpfsSizeBytes;
44 std::optional<uint32_t> TmpfsMode;
45 std::optional<std::string> TmpfsOptions;
46 };
47
48 enum class ValidationError
49 {
50 InvalidSpecification,
51 DuplicateDestination,
52 };
53
54 class MountException : public std::exception
55 {
56 public:
57 explicit MountException(std::wstring reason) : m_reason(std::move(reason))
58 {
59 }
60
61 MountException(ValidationError error, std::wstring reason, std::string destination) :
62 m_error(error), m_reason(std::move(reason)), m_destination(std::move(destination))
63 {
64 }
65
66 const char* what() const noexcept override
67 {
68 return "mount error";
69 }
70
71 const std::wstring& Reason() const noexcept
72 {
73 return m_reason;
74 }
75
76 ValidationError Error() const noexcept
77 {
78 return m_error;
79 }
80
81 const std::string& Destination() const noexcept
82 {
83 return m_destination;
84 }
85
86 private:
87 ValidationError m_error = ValidationError::InvalidSpecification;
88 std::wstring m_reason;
89 std::string m_destination;
90 };
91
92 class MountParseException : public MountException
93 {
94 public:
95 using MountException::MountException;
96 };
97
98 class MountUnsupportedException : public MountException
99 {
100 public:
101 using MountException::MountException;
102 };
103
104 class MountValidationException : public MountException
105 {
106 public:
107 using MountException::MountException;
108 };
109
110 Spec ParseDockerMountString(const std::wstring& value);
111 Spec ParseDockerVolumeString(const std::wstring& value);
112 Spec ParseDockerTmpfsString(const std::wstring& value);
113 void ValidateMountSpec(const Spec& mount);
114 void ValidateMountCollection(std::span<const Spec> mounts);
115 std::string FormatTmpfsOptions(const Spec& mount);
116 std::string NormalizeDestination(std::string destination);
117 bool IsValidNamedVolumeName(std::wstring_view name);
118
119 } // namespace wsl::windows::common::mount