forked from microsoft/WSL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmountflags.cpp
More file actions
201 lines (175 loc) · 5.71 KB
/
Copy pathmountflags.cpp
File metadata and controls
201 lines (175 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (C) Microsoft Corporation. All rights reserved.
#include <string>
#include <string_view>
#include <cerrno>
#include <cstdio>
#include <functional>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <lxdef.h>
#include <lxwil.h>
#include "mountutilcpp.h"
namespace {
enum class ParseFlags
{
None = 0,
Remove = 0x1,
NoFail = 0x2,
OptionalValue = 0x4,
};
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
DEFINE_ENUM_FLAG_OPERATORS(ParseFlags);
#pragma clang diagnostic pop
struct MountFlag
{
const char* Name;
int MountFlags;
ParseFlags ParseFlags;
};
#define FLAG_WITH_NAMED_INVERSE(name, inverse, flag) \
{(name), (flag), ParseFlags::None}, \
{ \
(inverse), (flag), ParseFlags::Remove \
}
// "opt", "noopt" pair where the "noopt" version adds a flag, and "opt" removes it.
#define NO_FLAG_WITH_INVERSE(name, flag) FLAG_WITH_NAMED_INVERSE("no" name, name, flag)
// "opt", "noopt" pair where the "opt" version adds a flag, and "noopt" removes it.
#define FLAG_WITH_INVERSE(name, flag) FLAG_WITH_NAMED_INVERSE(name, "no" name, flag)
// List of mount options that translate into mount flags.
// This is based on the information in the mount(8) manpage. Note that not all options are present,
// since this is intended to be used by a mount helper, and not all options are forwarded to the
// helpers by /bin/mount.
const MountFlag c_flagMap[] = {
FLAG_WITH_NAMED_INVERSE("sync", "async", MS_SYNCHRONOUS),
NO_FLAG_WITH_INVERSE("atime", MS_NOATIME),
{"defaults", 0, ParseFlags::None},
NO_FLAG_WITH_INVERSE("dev", MS_NODEV),
NO_FLAG_WITH_INVERSE("diratime", MS_NODIRATIME),
{"dirsync", MS_DIRSYNC, ParseFlags::None},
NO_FLAG_WITH_INVERSE("exec", MS_NOEXEC),
{"group", MS_NOSUID | MS_NODEV, ParseFlags::None},
{"nogroup", 0, ParseFlags::None},
FLAG_WITH_INVERSE("iversion", MS_I_VERSION),
FLAG_WITH_INVERSE("mand", MS_MANDLOCK),
{"_netdev", 0, ParseFlags::None},
{"nofail", 0, ParseFlags::NoFail},
FLAG_WITH_INVERSE("relatime", MS_RELATIME),
FLAG_WITH_INVERSE("strictatime", MS_STRICTATIME),
FLAG_WITH_INVERSE("lazytime", MS_LAZYTIME),
NO_FLAG_WITH_INVERSE("suid", MS_NOSUID),
FLAG_WITH_NAMED_INVERSE("silent", "loud", MS_SILENT),
{"owner", MS_NODEV | MS_NOSUID, ParseFlags::None},
{"noowner", 0, ParseFlags::None},
{"remount", MS_REMOUNT, ParseFlags::None},
FLAG_WITH_NAMED_INVERSE("ro", "rw", MS_RDONLY),
{"user", MS_NOEXEC | MS_NODEV | MS_NOSUID, ParseFlags::OptionalValue},
{"nouser", 0, ParseFlags::None},
{"users", MS_NOEXEC | MS_NODEV | MS_NOSUID, ParseFlags::None},
{"nousers", 0, ParseFlags::None},
};
// Determine if an option should be a flag.
// Returns the flag information if found; otherwise, null.
const MountFlag* FindOption(std::string_view option)
{
// Check if the option has a value.
bool hasValue = false;
auto index = option.find_first_of('=');
if (index != std::string_view::npos)
{
hasValue = true;
option = option.substr(0, index);
}
for (auto& flag : c_flagMap)
{
// If the option has a value, ignore the entry if it doesn't allow one.
if (hasValue && !WI_IsFlagSet(flag.ParseFlags, ParseFlags::OptionalValue))
{
continue;
}
if (option == flag.Name)
{
return &flag;
}
}
return nullptr;
}
// Retrieves the next character-separated token from a string view, returning
// the token and updating the view to be the remainder of the string.
std::string_view NextToken(std::string_view& view, char separator)
{
std::string_view result;
auto pos = view.find_first_of(separator);
if (pos == view.npos)
{
result = view;
view = {};
}
else
{
result = view.substr(0, pos);
view = view.substr(pos + 1);
}
return result;
}
} // namespace
namespace mountutil {
ParsedOptions MountParseFlags(std::string_view options)
{
ParsedOptions result{};
while (!options.empty())
{
// Get the next option and check if it's a flag.
auto option = NextToken(options, ',');
auto flag = FindOption(option);
if (flag == nullptr)
{
// Not a flag, so append to the string options.
if (!result.StringOptions.empty())
{
result.StringOptions += ',';
}
result.StringOptions += option;
}
else
{
// Modify the mount flags.
if (WI_IsFlagSet(flag->ParseFlags, ParseFlags::Remove))
{
WI_ClearAllFlags(result.MountFlags, flag->MountFlags);
}
else
{
WI_SetAllFlags(result.MountFlags, flag->MountFlags);
}
if (WI_IsFlagSet(flag->ParseFlags, ParseFlags::NoFail))
{
result.NoFail = true;
}
}
}
return result;
}
int MountFilesystem(const char* source, const char* target, const char* type, const char* options)
{
auto parsedOptions = MountParseFlags(options);
int result = mount(source, target, type, parsedOptions.MountFlags, parsedOptions.StringOptions.c_str());
// If the nofail option was specified, ENOENT on the source only must be ignored.
if (result < 0 && errno == ENOENT && parsedOptions.NoFail)
{
struct stat st;
// If the target exists, the error must be about the source, so ignore it.
if (stat(target, &st) == 0)
{
return 0;
}
// In case stat changed errno.
errno = ENOENT;
}
return result;
}
} // namespace mountutil