-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
494 lines (423 loc) · 16.7 KB
/
Utils.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "Utils.h"
#include "Logging.h"
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <filesystem>
#include <sys/socket.h>
#include <sys/un.h>
#include <acl/libacl.h>
#include <sys/acl.h>
#include <chrono>
namespace fs = std::filesystem;
using namespace chrono;
namespace Utils {
vecstr SplitStr (string Src, const string &Pat) {
vecstr Toks;
while (Src.size()) {
auto TokEnd = Src.find (Pat);
string Tok = Src.substr (0, TokEnd);
Toks.push_back(Tok);
Src.erase (0, Tok.size() + Pat.size());
}
return Toks;
}
void TrimStr (string *Str) {
static const char *WhiteSpace = " \t\n";
// strip leading whitespace
size_t Begin = Str->find_first_not_of (WhiteSpace);
Str->erase (0, Begin);
// strip trailing whitespace
size_t End = Str->find_last_not_of (WhiteSpace);
Str->erase (End + 1);
}
string TrimStr (string Str) {
TrimStr (&Str);
return Str;
}
string JoinStrs (const vecstr &Parts, const string &Sep) {
string Joined;
bool first = 1;
for (auto &Part : Parts) {
if (!first)
Joined += Sep;
Joined += Part;
first = false;
}
return Joined;
}
string CanonizeFileNameNoCWD (const string &RawName) {
if (RawName == "")
return "";
// remember if the name starts with /
bool abs = RawName[0] == '/';
// tokenize by spliting on "/"
vecstr Toks = SplitStr (RawName, "/");
// discard all instances of "", ".", and "dir/.."
for (auto itr = Toks.begin(); itr < Toks.end(); itr++) {
if (*itr == "" || *itr == ".") {
Toks.erase (itr);
itr --;
} else if (*itr == "..") {
if (itr == Toks.begin()) {
// handle .. at beginning
Toks.erase (itr);
itr --;
} else {
Toks.erase (itr-1, itr+1);
itr -= 2;
}
}
}
// assemble the full path from the components
string CanName = JoinStrs (Toks, "/");
// restore leading /
if (abs)
CanName.insert (0, 1, '/');
return CanName;
}
string CanonizeFileName (const string &RawName, const string &CWD) {
string FullName;
// prepend current directory to name, if needed
if (RawName[0] == '/') {
FullName = RawName;
} else {
string CwdStr;
if (CWD.size()==0) {
char cwd [4000];
if (getcwd (cwd, sizeof(cwd)) == NULL)
THROW_PBEXCEPTION ("getcwd failed: ");
CwdStr = string (cwd);
} else {
CwdStr = CWD;
}
FullName = CwdStr + "/" + RawName;
}
return CanonizeFileNameNoCWD (FullName);
}
fstream OpenReadStream (const string &Name) {
fstream Strm (Name.c_str(), fstream::in);
if (Strm.fail())
THROW_PBEXCEPTION_IO ("Can't open %s for read", Name.c_str());
return Strm;
}
FILE* OpenReadBin (const string &Name) {
FILE* F;
if (!(F = fopen (Name.c_str(), "rb")))
THROW_PBEXCEPTION_IO ("Can't open %s for read", Name.c_str());
assert (F);
return F;
}
fstream OpenWriteStream (const string &Name) {
fstream Strm (Name.c_str(), fstream::out);
if (Strm.fail())
THROW_PBEXCEPTION_IO ("Can't open %s for write", Name.c_str());
return Strm;
}
FILE * OpenWriteBin (const string &Name) {
FILE* F;
if (!(F = fopen (Name.c_str(), "wb")))
THROW_PBEXCEPTION_IO ("Can't open %s for write", Name.c_str());
assert (F);
return F;
}
string ReadLine (fstream &Stream, bool DieOnEOF) {
string Line;
getline (Stream, Line);
if (DieOnEOF && Stream.eof())
THROW_PBEXCEPTION_IO ("Unexpected end of file");
if (Stream.fail())
THROW_PBEXCEPTION_IO ("ReadLine failed");
return Line;
}
int ReadBinary (FILE *F, char *Buf, int BufSize) {
assert (F);
int BytesRead;
if ((BytesRead = fread (Buf, 1, BufSize, F)) < 0)
THROW_PBEXCEPTION_IO ("Read failed");
return BytesRead;
}
int ReadBinary (FILE *F, string &Str, int MaxSize) {
assert (F);
Str.resize(MaxSize);
int Size = ReadBinary (F, Str.data(), MaxSize);
Str.resize (Size);
return Size;
}
void WriteBinary (FILE *F, const char *Buf, unsigned BufSize) {
assert (F);
if (fwrite (Buf, 1, BufSize, F) != BufSize)
THROW_PBEXCEPTION_IO ("Write failed");
}
void WriteBinary (FILE *F, const string &Str) {
assert (F);
WriteBinary (F, Str.c_str(), Str.size());
}
void CreateDir (const string Dir, bool CreateSubs) {
error_code ec;
if (CreateSubs) {
if (fs::create_directories (Dir, ec))
return;
} else {
if (fs::create_directory (Dir, ec))
return;
}
if (ec)
THROW_PBEXCEPTION ("Utils::CreateDir Failed to create %s: %s", Dir.c_str(), ec.message().c_str());
}
void MakeHardLink (const string &ExistingFile, const string &NewName) {
if (link (ExistingFile.c_str(), NewName.c_str()) != 0)
THROW_PBEXCEPTION_IO ("Can't create hard link (%s) to target (%s)", NewName.c_str(), ExistingFile.c_str());
}
void CloneBlockDir (const string &SrcDir, const string &DstDir, BlockList &DstBlocks) {
CreateDir (DstDir);
vecstr SubDirs, SubFiles;
SlurpDir (SrcDir, SubDirs, SubFiles);
// hardlink all the files
// add to allocated blocks
for (auto &File : SubFiles) {
MakeHardLink (SrcDir + "/" + File, DstDir + "/" + File);
DstBlocks.MarkAllocated (strtoull(File.c_str(), NULL, 10));
}
// recurse into subdirs
for (auto &Dir : SubDirs)
CloneBlockDir (SrcDir + "/" + Dir, DstDir + "/" + Dir, DstBlocks);
}
void SlurpDir (const string &Dir, vecstr &SubDirs, vecstr &SubFiles) {
for (const auto &entry : filesystem::directory_iterator(Dir)) {
fs::path SubPath = entry.path();
string SubName = SubPath.filename().string();
if (is_directory (SubPath))
SubDirs.push_back(SubName);
else if (is_regular_file (SubPath))
SubFiles.push_back(SubName);
}
}
void Touch (const string Name) {
fstream Strm (Name.c_str(), fstream::out | fstream::app);
if (Strm.fail())
THROW_PBEXCEPTION_IO ("Can't open %s for touch", Name.c_str());
Strm.close();
}
void Link (const string &Name, const string &Target) {
error_code ec;
fs::create_hard_link (Target, Name, ec);
if (ec)
THROW_PBEXCEPTION_IO ("Error creating link:%s to target:%s :%s", Name.c_str(), Target.c_str(), ec.message().c_str());
}
// extract standard stat type from archive file stats header
struct stat ParseStatsHeader (const string &Hdr) {
struct stat Stats;
vecstr Fields = SplitStr (Hdr, " ");
for (auto Field : Fields) {
vecstr Two = SplitStr (Field, ":");
if (Two.size() != 2)
THROW_PBEXCEPTION_FMT ("Illegal header field : %s", Field.c_str());
string &Name = Two[0];
string &Val = Two[1];
if (Name == "mode" ) Stats.st_mode = strtoull (Val.c_str(), NULL, 16);
else if (Name == "uid" ) Stats.st_uid = strtoull (Val.c_str(), NULL, 16);
else if (Name == "gid" ) Stats.st_gid = strtoull (Val.c_str(), NULL, 16);
else if (Name == "size" ) Stats.st_size = strtoull (Val.c_str(), NULL, 10);
else if (Name == "mtime") Stats.st_mtim = NsToTimeSpec (strtoull (Val.c_str(), NULL, 16));
}
return Stats;
}
// create archive file stats header from standard stat type
string CreateStatsHeader (const struct stat &Stats) {
stringstream res;
res << "mode:" << hex << Stats.st_mode << " ";
res << "uid:" << hex << Stats.st_uid << " ";
res << "gid:" << hex << Stats.st_gid << " ";
res << "size:" << dec << Stats.st_size << " "; // keep size in decimal to make it easier to read and debug
res << "mtime:" << hex << TimeSpecToNs (Stats.st_mtim) << " ";
return res.str();
}
// convert u64 nanosecond time to stats time structure
timespec NsToTimeSpec (u64 ns) {
timespec T;
static const u64 Ratio = 1000000000;
T.tv_sec = ns / Ratio;
T.tv_nsec = ns % Ratio;
return T;
}
// convert stats time_t to ns time
u64 TimeSpecToNs (const timespec &T) {
return (u64) T.tv_sec * 1000000000 + (u64) T.tv_nsec;
}
// return true if two timespecs are equal
bool TimeSpecsEqual (const timespec &T1, const timespec &T2) {
return T1.tv_sec == T2.tv_sec && T1.tv_nsec == T2.tv_nsec;
}
// convert unix timespec to human readable text
string TimeSpecToText (const timespec &T) {
// convert ns to duration
u64 ns = TimeSpecToNs (T);
auto dur = nanoseconds(ns);
// convert to text
system_clock::time_point Time (dur);
time_t Time_t = system_clock::to_time_t(Time);
string Result(30, '\0');
strftime (Result.data(), Result.size(), "%Y_%m_%d_%H%M_%S", std::localtime(&Time_t));
return Result.c_str();
}
// convert epoch nanoseconds to text time
string TimeSpecToText (u64 &ns) {
return TimeSpecToText (NsToTimeSpec(ns));
}
// convert epoch nanoseconds to text time
string NsToText (u64 ns) {
return TimeSpecToText (NsToTimeSpec (ns));
}
// get current time in ns
u64 TimeNowNs () {
system_clock::time_point now = system_clock::now();
u64 ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
return ns;
}
// create a socket
void CreateSocket (const string &Name) {
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
THROW_PBEXCEPTION_IO ("Can't create socket:", Name.c_str());
struct sockaddr_un server;
server.sun_family = AF_UNIX;
strncpy(server.sun_path, Name.c_str(), sizeof(server.sun_path)-1);
if (bind(sock, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) {
THROW_PBEXCEPTION_IO ("Can't bind socket: %s", Name.c_str());
}
close (sock);
}
void SetMode (const string &Name, mode_t Mode) {
if (chmod (Name.c_str(), Mode))
fprintf (stderr, "Can't set dir/file directory (%s) mode to %o: %s\n", Name.c_str(), Mode, strerror(errno));
}
void SetOwn (const string &Name, u32 Uid, u32 Gid, bool IsSLink) {
int res;
if (IsSLink)
res = lchown (Name.c_str(), Uid, Gid);
else
res = chown (Name.c_str(), Uid, Gid);
if (res)
fprintf (stderr, "Can't set dir/file (%s) owner/group to (%d/%d): %s\n",
Name.c_str(), Uid, Gid, strerror(errno));
}
void SetModTime (const string &Name, timespec Time) {
// set modification time
struct timespec TV[2] = {Time, Time};
if (utimensat (AT_FDCWD, Name.c_str(), TV, AT_SYMLINK_NOFOLLOW))
THROW_PBEXCEPTION_IO ("Can't set mod time of %s", Name.c_str());
}
string GetFileAcl (const string &Name, u16 Perm, u32 Type) {
// get file acl struture
auto Acl = acl_get_file(Name.c_str(), Type);
if (Acl == 0)
return ""; // no acl of this type
if (Acl < 0)
THROW_PBEXCEPTION_IO ("Can't get acl for: %s", Name.c_str());
// filter out base acl entries
acl_t NewACL = acl_init(0);
acl_entry_t Entry;
for (int EntStat = acl_get_entry (Acl, ACL_FIRST_ENTRY, &Entry);
EntStat == 1;
EntStat = acl_get_entry (Acl, ACL_NEXT_ENTRY , &Entry)) {
// get entry type
acl_tag_t tag;
acl_get_tag_type(Entry, &tag);
// compare base acl to normal permissions mask returned my lstat
if (Type == ACL_TYPE_ACCESS) {
// get permissions for this entry
acl_permset_t PermSet;
acl_get_permset (Entry, &PermSet);
// if base permission is the same as perm from lstat call, ignore this entry
if (tag == ACL_USER_OBJ && (*(u8 *)PermSet & 7) == ((Perm >> 6) & 7)) continue;
if (tag == ACL_GROUP_OBJ && (*(u8 *)PermSet & 7) == ((Perm >> 3) & 7)) continue;
if (tag == ACL_OTHER && (*(u8 *)PermSet & 7) == ((Perm >> 0) & 7)) continue;
}
acl_entry_t NewEntry;
if (acl_create_entry (&NewACL, &NewEntry) < 0)
THROW_PBEXCEPTION_IO ("Can't create acl entry\n");
if (acl_copy_entry (NewEntry, Entry) < 0)
THROW_PBEXCEPTION_IO ("Can't copy acl entry\n");
}
// if no acls remain, we're done
if (acl_entries(NewACL) <= 0) {
acl_free (Acl);
acl_free (NewACL);
return "";
}
// convert to short form text
char *AclText = acl_to_any_text (NewACL, NULL, ',', TEXT_ABBREVIATE | TEXT_NUMERIC_IDS);
string AclStr (AclText);
// release resources
acl_free (AclText);
acl_free (Acl);
acl_free (NewACL);
string Result;
Result += (Type == ACL_TYPE_DEFAULT ? 'D' : 'A');
Result += string ("|") + AclStr;
return Result;
}
string GetFileAcls (const string &Name, u16 Perm) {
vecstr ACLs;
for (int Type : {ACL_TYPE_ACCESS, ACL_TYPE_DEFAULT}) {
string ACLStr = GetFileAcl (Name, Perm, Type);
if (ACLStr.size())
ACLs.push_back(ACLStr);
}
if (ACLs.size())
return JoinStrs (ACLs, ";");
return "";
}
static bool LookupInitted = 0;
static mutex LookupMtx;
static map <u8, string> Lookup;
void SetFileAcls (const string &Name, u16 Perm, const string &Acls) {
if (!Acls.size())
return;
// build lookup table for perm field to string
LookupMtx.lock();
if (!LookupInitted) {
for (u8 i = 0; i <= 7; i++) {
string Val = string (1, i&4 ? 'r' : '-') +
string (1, i&2 ? 'w' : '-') +
string (1, i&1 ? 'x' : '-');
Lookup [i] = Val;
}
LookupInitted = 1;
}
LookupMtx.unlock();
// break acl spec into access and default portions
for (auto Acl : SplitStr (Acls, ";")) {
// split off type specifier
vecstr Parts = SplitStr (Acl, "|");
if (Parts.size() != 2)
THROW_PBEXCEPTION_FMT ("Illegal ACL format: %s\n", Acl.c_str());
u32 Type;
if (Parts[0] == "A")
Type = ACL_TYPE_ACCESS;
else if (Parts[0] == "D")
Type = ACL_TYPE_DEFAULT;
else
THROW_PBEXCEPTION_FMT ("Illegal ACL format: %s\n", Acl.c_str());
string AclShort = Parts[1];
// add base permissions, if needed
if (Type == ACL_TYPE_ACCESS) {
if (AclShort.find ("u::")==AclShort.npos) AclShort += ",u::" + Lookup [(Perm >> 6) & 7];
if (AclShort.find ("g::")==AclShort.npos) AclShort += ",g::" + Lookup [(Perm >> 3) & 7];
if (AclShort.find ("o::")==AclShort.npos) AclShort += ",o::" + Lookup [(Perm >> 0) & 7];
}
//fprintf (stderr, "Name=%s Perm =%03o\n", Name.c_str(), Perm & 0777);
//fprintf (stderr, "%c Type =%ld Parts[1]=%s\n", Parts[0][0], (long) Type, Parts[1].c_str());
//fprintf (stderr, "%c Type =%ld AclShort=%s\n", Parts[0][0], (long) Type, AclShort.c_str());
// set the file acl
acl_t acl = acl_from_text (AclShort.c_str());
if (!acl)
THROW_PBEXCEPTION_FMT ("Can't parse acl text: %s: %s", AclShort.c_str(), strerror(errno));
if (acl_set_file (Name.c_str(), Type, acl))
fprintf (stderr, "Can't set acl for file: %s: %s\n", Name.c_str(), strerror(errno));
acl_free (acl);
}
}
}