-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathArgumentValidation.cpp
More file actions
193 lines (164 loc) · 6.15 KB
/
ArgumentValidation.cpp
File metadata and controls
193 lines (164 loc) · 6.15 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
/*++
Copyright (c) Microsoft. All rights reserved.
Module Name:
ArgumentValidation.cpp
Abstract:
Implementation of the Argument Validation.
--*/
#include "Argument.h"
#include "ArgumentTypes.h"
#include "ArgumentValidation.h"
#include "ContainerModel.h"
#include "Exceptions.h"
#include <charconv>
#include <format>
#include <unordered_map>
#include <wslc.h>
using namespace wsl::windows::common;
using namespace wsl::shared;
using namespace wsl::shared::string;
namespace wsl::windows::wslc {
// Common argument validation that occurs across multiple commands.
void Argument::Validate(const ArgMap& execArgs) const
{
switch (m_argType)
{
case ArgType::Format:
validation::ValidateFormatTypeFromString(execArgs.GetAll<ArgType::Format>(), m_name);
break;
case ArgType::Signal:
validation::ValidateWSLCSignalFromString(execArgs.GetAll<ArgType::Signal>(), m_name);
break;
case ArgType::Time:
validation::ValidateIntegerFromString<LONGLONG>(execArgs.GetAll<ArgType::Time>(), m_name);
break;
case ArgType::Volume:
validation::ValidateVolumeMount(execArgs.GetAll<ArgType::Volume>());
break;
case ArgType::WorkDir:
{
const auto& value = execArgs.Get<ArgType::WorkDir>();
if (value.empty() ||
std::all_of(value.begin(), value.end(), [](wchar_t c) { return std::iswspace(static_cast<wint_t>(c)); }))
{
throw ArgumentException(std::format(L"Invalid {} argument value: working directory cannot be empty or whitespace", m_name));
}
break;
}
case ArgType::CIDFile:
{
validation::ValidateCidFile(execArgs.Get<ArgType::CIDFile>());
break;
}
default:
break;
}
}
} // namespace wsl::windows::wslc
namespace wsl::windows::wslc::validation {
// Map of signal names to WSLCSignal enum values
static const std::unordered_map<std::wstring, WSLCSignal> SignalMap = {
{L"SIGHUP", WSLCSignalSIGHUP}, {L"SIGINT", WSLCSignalSIGINT}, {L"SIGQUIT", WSLCSignalSIGQUIT},
{L"SIGILL", WSLCSignalSIGILL}, {L"SIGTRAP", WSLCSignalSIGTRAP}, {L"SIGABRT", WSLCSignalSIGABRT},
{L"SIGIOT", WSLCSignalSIGIOT}, {L"SIGBUS", WSLCSignalSIGBUS}, {L"SIGFPE", WSLCSignalSIGFPE},
{L"SIGKILL", WSLCSignalSIGKILL}, {L"SIGUSR1", WSLCSignalSIGUSR1}, {L"SIGSEGV", WSLCSignalSIGSEGV},
{L"SIGUSR2", WSLCSignalSIGUSR2}, {L"SIGPIPE", WSLCSignalSIGPIPE}, {L"SIGALRM", WSLCSignalSIGALRM},
{L"SIGTERM", WSLCSignalSIGTERM}, {L"SIGTKFLT", WSLCSignalSIGTKFLT}, {L"SIGCHLD", WSLCSignalSIGCHLD},
{L"SIGCONT", WSLCSignalSIGCONT}, {L"SIGSTOP", WSLCSignalSIGSTOP}, {L"SIGTSTP", WSLCSignalSIGTSTP},
{L"SIGTTIN", WSLCSignalSIGTTIN}, {L"SIGTTOU", WSLCSignalSIGTTOU}, {L"SIGURG", WSLCSignalSIGURG},
{L"SIGXCPU", WSLCSignalSIGXCPU}, {L"SIGXFSZ", WSLCSignalSIGXFSZ}, {L"SIGVTALRM", WSLCSignalSIGVTALRM},
{L"SIGPROF", WSLCSignalSIGPROF}, {L"SIGWINCH", WSLCSignalSIGWINCH}, {L"SIGIO", WSLCSignalSIGIO},
{L"SIGPOLL", WSLCSignalSIGPOLL}, {L"SIGPWR", WSLCSignalSIGPWR}, {L"SIGSYS", WSLCSignalSIGSYS},
};
void ValidateWSLCSignalFromString(const std::vector<std::wstring>& values, const std::wstring& argName)
{
for (const auto& value : values)
{
std::ignore = GetWSLCSignalFromString(value, argName);
}
}
void ValidateVolumeMount(const std::vector<std::wstring>& values)
{
for (const auto& value : values)
{
std::ignore = models::VolumeMount::Parse(value);
}
}
void ValidateCidFile(const std::wstring& cidFile)
{
std::error_code ec;
if (std::filesystem::exists(std::filesystem::path{cidFile}, ec))
{
THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), Localization::WSLCCLI_CIDFileAlreadyExistsError(cidFile));
}
if (ec)
{
const auto errorMessage = MultiByteToWide(ec.message());
THROW_HR_WITH_USER_ERROR(HRESULT_FROM_WIN32(ec.value()), Localization::MessageWslcFailedToOpenFile(cidFile, errorMessage));
}
}
// Convert string to WSLCSignal enum - accepts either signal name (e.g., "SIGKILL") or number (e.g., "9")
WSLCSignal GetWSLCSignalFromString(const std::wstring& input, const std::wstring& argName)
{
constexpr int MIN_SIGNAL = WSLCSignalSIGHUP;
constexpr int MAX_SIGNAL = WSLCSignalSIGSYS;
constexpr std::wstring_view sigPrefix = L"SIG";
// Normalize input: ensure it has "SIG" prefix for map lookup
std::wstring normalizedInput;
if (IsEqual(input.substr(0, sigPrefix.size()), sigPrefix, true))
{
normalizedInput = input;
}
else
{
normalizedInput = std::wstring(sigPrefix) + input;
}
for (const auto& [signalName, signalValue] : SignalMap)
{
if (IsEqual(normalizedInput, signalName, true))
{
return signalValue;
}
}
// User may have input an integer representation instead.
int signalValue{};
try
{
signalValue = GetIntegerFromString<int>(input, argName);
}
// If it fails to be converted give a better user message than just the integer conversion
// failure since we also know it failed to be found in the map.
catch (ArgumentException)
{
throw ArgumentException(Localization::WSLCCLI_InvalidSignalError(argName, input));
}
if (signalValue < MIN_SIGNAL || signalValue > MAX_SIGNAL)
{
throw ArgumentException(Localization::WSLCCLI_SignalOutOfRangeError(argName, input, MIN_SIGNAL, MAX_SIGNAL));
}
return static_cast<WSLCSignal>(signalValue);
}
void ValidateFormatTypeFromString(const std::vector<std::wstring>& values, const std::wstring& argName)
{
for (const auto& value : values)
{
std::ignore = GetFormatTypeFromString(value, argName);
}
}
FormatType GetFormatTypeFromString(const std::wstring& input, const std::wstring& argName)
{
if (IsEqual(input, L"json"))
{
return FormatType::Json;
}
else if (IsEqual(input, L"table"))
{
return FormatType::Table;
}
else
{
throw ArgumentException(std::format(
L"Invalid {} value: {} is not a recognized format type. Supported format types are: json, table.", argName, input));
}
}
} // namespace wsl::windows::wslc::validation