-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.cpp
More file actions
273 lines (241 loc) · 7.67 KB
/
Copy pathutilities.cpp
File metadata and controls
273 lines (241 loc) · 7.67 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <algorithm>
#include <cctype>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <stdexcept>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "logging.h"
#include "utilities.h"
#include "apci.h"
// TODO: write WaitUntilBitsMatch(__u8 offset, __u32 bmMask, __u32 bmPattern);
int WaitUntilRegisterBitIsLow(__u8 offset, __u32 bitMask)
{
__u32 value = 0;
int attempt = 0;
do
{
value = in(offset);
Trace("SPI Busy Bit at " + to_hex<__u8>(offset) + " is " + ((value & bitMask) ? "1" : "0"));
// if (status < 0)
// return -errno;
if (++attempt > 100)
{
Error("Timeout waiting for SPI to be not busy, at offset: " + to_hex<__u8>(offset));
return -ETIMEDOUT; // TODO: swap "attempt" with "timeout" RTC if benchmark proves RTC is not too slow
}
} while ((value & bitMask));
return 0;
}
bool sanitizePath(std::string &path)
{
std::filesystem::path basePath = std::filesystem::absolute("/home/acces/"); // Use absolute instead of canonical
// Check for null bytes in the path
if (path.find('\0') != std::string::npos)
{
Error("Error: Path contains null bytes.");
return false;
}
// Convert path to absolute path
std::filesystem::path p = std::filesystem::absolute(path);
std::string absPath = p.string();
// Replace incorrect file separators
std::replace(absPath.begin(), absPath.end(), '\\', std::filesystem::path::preferred_separator);
// Check if path is within allowed directory
if (absPath.compare(0, basePath.string().size(), basePath.string()) != 0)
{
Error("Error: Path is not within allowed directory.");
return false;
}
path = absPath;
return true;
}
std::string GetTempFileName()
{
std::filesystem::path tmpDir = std::filesystem::temp_directory_path();
std::filesystem::path tmpFile = tmpDir / "aioenet_upload_XXXXXX";
char filenameTemplate[1024];
strncpy(filenameTemplate, tmpFile.c_str(), tmpFile.string().size() + 1);
int fileDescriptor = mkstemp(filenameTemplate);
if (fileDescriptor == -1)
{
Error("Failed to create temp file.");
return "";
}
close(fileDescriptor);
Debug("Created temp file: " + std::string(filenameTemplate));
return filenameTemplate;
}
// A helper function to split a string by a given delimiter (e.g. '-').
std::vector<std::string> split(const std::string &s, char delim)
{
std::vector<std::string> tokens;
std::istringstream iss(s);
std::string token;
while (std::getline(iss, token, delim))
{
if (!token.empty())
{
tokens.push_back(token);
}
}
return tokens;
}
std::string GetTempFileNameInDir(const std::string &dir, const std::string &prefix)
{
std::filesystem::create_directories(dir);
std::filesystem::path tmpl = std::filesystem::path(dir) / (prefix + ".XXXXXX");
char filenameTemplate[PATH_MAX];
std::snprintf(filenameTemplate, sizeof(filenameTemplate), "%s", tmpl.c_str());
int fd = mkstemp(filenameTemplate);
if (fd == -1)
{
Error(std::string("mkstemp(") + filenameTemplate + ") failed: " + std::strerror(errno));
return "";
}
close(fd);
Debug(std::string("Created temp file in dir: ") + filenameTemplate);
return filenameTemplate;
}
// ---------- atomic write (fsync + rename within same FS) ----------
void WriteFileAtomic(const std::string &path, const std::string &content, mode_t mode)
{
const std::filesystem::path dst(path);
const std::string dir = dst.parent_path().empty() ? "." : dst.parent_path().string();
// Create temp in the SAME directory to keep rename() atomic
std::string tmp = GetTempFileNameInDir(dir, dst.filename().string());
if (tmp.empty())
{
throw std::runtime_error("WriteFileAtomic: could not create temp file");
}
int fd = ::open(tmp.c_str(), O_WRONLY | O_TRUNC, mode);
if (fd < 0)
{
std::string msg = std::string("open(") + tmp + "): " + std::strerror(errno);
unlink(tmp.c_str());
throw std::runtime_error(msg);
}
if (fchmod(fd, mode) != 0)
{
std::string msg = std::string("fchmod(") + tmp + "): " + std::strerror(errno);
close(fd);
unlink(tmp.c_str());
throw std::runtime_error(msg);
}
ssize_t w = ::write(fd, content.data(), content.size());
if (w < 0 || static_cast<size_t>(w) != content.size())
{
std::string msg = std::string("write(") + tmp + "): " + std::strerror(errno);
close(fd);
unlink(tmp.c_str());
throw std::runtime_error(msg);
}
if (fsync(fd) != 0)
{
std::string msg = std::string("fsync(") + tmp + "): " + std::strerror(errno);
close(fd);
unlink(tmp.c_str());
throw std::runtime_error(msg);
}
close(fd);
if (rename(tmp.c_str(), path.c_str()) != 0)
{
std::string msg = std::string("rename(") + tmp + " → " + path + "): " + std::strerror(errno);
unlink(tmp.c_str());
throw std::runtime_error(msg);
}
}
// ---------- dirs ----------
void EnsureDir(const std::string &path, mode_t mode)
{
std::error_code ec;
if (!std::filesystem::exists(path, ec))
{
std::filesystem::create_directories(path, ec);
if (ec)
{
throw std::runtime_error("EnsureDir(" + path + "): " + ec.message());
}
chmod(path.c_str(), mode);
}
}
// ---------- hostname validation/sanitization ----------
static bool IsValidHostnameLabel(const std::string &s)
{
if (s.empty() || s.size() > 63)
return false;
if (s.front() == '-' || s.back() == '-')
return false;
for (unsigned char c : s)
{
if (!(std::isalnum(c) || c == '-'))
return false;
}
return true;
}
std::string SanitizeHostname(std::string s)
{
// trim
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch)
{ return !std::isspace(ch); }));
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch)
{ return !std::isspace(ch); })
.base(),
s.end());
// lowercase
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
{ return std::tolower(c); });
return s;
}
bool IsValidHostname(const std::string &host)
{
if (host.empty() || host.size() > 253)
return false;
size_t i = 0, n = host.size();
while (i < n)
{
size_t j = host.find('.', i);
if (j == std::string::npos)
j = n;
if (!IsValidHostnameLabel(host.substr(i, j - i)))
return false;
i = j + 1;
}
return true;
}
// ---------- /etc/hosts updater ----------
void UpdateEtcHosts127011(const std::string &newHost)
{
std::ifstream in("/etc/hosts");
std::string out, line;
bool replaced = false;
while (std::getline(in, line))
{
std::string trimmed = line;
trimmed.erase(trimmed.begin(),
std::find_if(trimmed.begin(), trimmed.end(), [](int ch)
{ return !std::isspace(ch); }));
if (!trimmed.empty() && trimmed[0] != '#')
{
size_t sp = trimmed.find_first_of(" \t");
std::string ip = (sp == std::string::npos) ? trimmed : trimmed.substr(0, sp);
if (ip == "127.0.1.1")
{
out += "127.0.1.1\t" + newHost + "\n";
replaced = true;
continue;
}
}
out += line + "\n";
}
if (!replaced)
out += "127.0.1.1\t" + newHost + "\n";
WriteFileAtomic("/etc/hosts", out);
}