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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
|
#include <sys/mount.h>
#include <sys/time.h>
#include <cstdlib>
#include <filesystem>
#include <atomic>
#include <random>
#include <ranges>
#include <sys/xattr.h>
#include "c-calls.hh"
#include "lix/libutil/environment-variables.hh"
#include "lix/libutil/file-descriptor.hh"
#include "lix/libutil/file-system.hh"
#include "lix/libutil/finally.hh"
#include "lix/libutil/logging.hh"
#include "lix/libutil/serialise.hh"
#include "lix/libutil/signals.hh"
#include "lix/libutil/strings.hh"
#include "lix/libutil/types.hh"
#include "lix/libutil/users.hh"
#ifdef __EMSCRIPTEN__
#include "lix/libstore/wasm-store.hh"
#endif
namespace fs = std::filesystem;
namespace nix {
Path getCwd() {
char buf[PATH_MAX];
if (!getcwd(buf, sizeof(buf))) {
throw SysError("cannot get cwd");
}
return Path(buf);
}
Path absPath(Path path, std::optional<PathView> dir, bool resolveSymlinks)
{
if (path.empty() || path[0] != '/') {
if (!dir) {
path = concatStrings(getCwd(), "/", path);
} else {
path = concatStrings(*dir, "/", path);
}
}
return canonPath(path, resolveSymlinks);
}
Path canonPath(PathView path, bool resolveSymlinks)
{
if (path == "" || path[0] != '/')
throw Error("not an absolute path: '%1%'", path);
std::string s;
s.reserve(256);
std::string temp;
/* Count the number of times we follow a symlink and stop at some
arbitrary (but high) limit to prevent infinite loops. */
unsigned int followCount = 0, maxFollow = 1024;
while (1) {
/* Skip slashes. */
while (!path.empty() && path[0] == '/') path.remove_prefix(1);
if (path.empty()) break;
/* Ignore `.'. */
if (path == "." || path.substr(0, 2) == "./")
path.remove_prefix(1);
/* If `..', delete the last component. */
else if (path == ".." || path.substr(0, 3) == "../")
{
if (!s.empty()) s.erase(s.rfind('/'));
path.remove_prefix(2);
}
/* Normal component; copy it. */
else {
s += '/';
if (const auto slash = path.find('/'); slash == std::string::npos) {
s += path;
path = {};
} else {
s += path.substr(0, slash);
path = path.substr(slash);
}
/* If s points to a symlink, resolve it and continue from there */
if (resolveSymlinks && isLink(s)) {
if (++followCount >= maxFollow)
throw Error("infinite symlink recursion in path '%1%'", path);
temp = concatStrings(readLink(s), path);
path = temp;
if (!temp.empty() && temp[0] == '/') {
s.clear(); /* restart for symlinks pointing to absolute path */
} else {
s = dirOf(s);
if (s == "/") { // we don’t want trailing slashes here, which dirOf only produces if s = /
s.clear();
}
}
}
}
}
return s.empty() ? "/" : std::move(s);
}
Path realPath(Path const & path)
{
// With nullptr, realpath() malloc's and returns a new c-string.
char * resolved = realpath(requireCString(path), nullptr);
int saved = errno;
if (resolved == nullptr) {
throw SysError(saved, "cannot get realpath for '%s'", path);
}
Finally const _free([&] { free(resolved); });
// There's not really a from_raw_parts() for std::string.
// The copy is not a big deal.
Path ret(resolved);
return ret;
}
Path tildePath(Path const & path, const std::optional<Path> & home)
{
if (path.starts_with("~/")) {
if (home) {
return *home + "/" + path.substr(2);
} else {
throw UsageError("`~` path not allowed: %1%", path);
}
} else if (path.starts_with('~')) {
throw UsageError("`~` paths must start with `~/`: %1%", path);
} else {
return path;
}
}
void chmodPath(const Path & path, mode_t mode)
{
if (sys::chmod(path, mode) == -1) {
throw SysError("setting permissions on '%s'", path);
}
}
Path dirOf(const PathView path)
{
Path::size_type pos = path.rfind('/');
if (pos == std::string::npos)
return ".";
return pos == 0 ? "/" : Path(path, 0, pos);
}
std::string_view baseNameOf(std::string_view path)
{
if (path.empty())
return "";
auto last = path.size() - 1;
if (path[last] == '/' && last > 0)
last -= 1;
auto pos = path.rfind('/', last);
if (pos == std::string::npos)
pos = 0;
else
pos += 1;
return path.substr(pos, last - pos + 1);
}
std::string expandTilde(std::string_view path)
{
// TODO: expand ~user ?
auto tilde = path.substr(0, 2);
if (tilde == "~/" || tilde == "~")
return getHome() + std::string(path.substr(1));
else
return std::string(path);
}
bool isInDir(std::string_view path, std::string_view dir)
{
return path.substr(0, 1) == "/"
&& path.substr(0, dir.size()) == dir
&& path.size() >= dir.size() + 2
&& path[dir.size()] == '/';
}
bool isDirOrInDir(std::string_view path, std::string_view dir)
{
return path == dir || isInDir(path, dir);
}
struct stat stat(const Path & path)
{
struct stat st;
if (sys::stat(path, &st)) {
throw SysError("getting status of '%1%'", path);
}
return st;
}
struct stat lstat(const Path & path)
{
struct stat st;
if (sys::lstat(path, &st)) {
throw SysError("getting status of '%1%'", path);
}
return st;
}
std::optional<struct stat> maybeStat(const Path & path)
{
std::optional<struct stat> st{std::in_place};
if (sys::stat(path, &*st)) {
if (errno == ENOENT || errno == ENOTDIR)
st.reset();
else
throw SysError("getting status of '%s'", path);
}
return st;
}
std::optional<struct stat> maybeLstat(const Path & path)
{
std::optional<struct stat> st{std::in_place};
if (sys::lstat(path, &*st)) {
if (errno == ENOENT || errno == ENOTDIR)
st.reset();
else
throw SysError("getting status of '%s'", path);
}
return st;
}
bool pathExists(const Path & path)
{
return maybeLstat(path).has_value();
}
bool pathAccessible(const Path & path, bool resolveSymlinks)
{
try {
return resolveSymlinks ? maybeStat(path).has_value() : pathExists(path);
} catch (SysError & e) {
switch (e.errNo) {
case EPERM:
// operation not permitted error, can occur in darwin sandbox
case EACCES:
// permission error
case ELOOP:
// path component is a looping symlink
// this seems like a reasonable condition to handle as well
return false;
default:
throw;
}
}
}
Path readLink(const Path & path)
{
checkInterrupt();
std::vector<char> buf;
for (ssize_t bufSize = PATH_MAX/4; true; bufSize += bufSize/2) {
buf.resize(bufSize);
ssize_t rlSize = sys::readlink(path, buf.data(), bufSize);
if (rlSize == -1)
if (errno == EINVAL)
throw Error("'%1%' is not a symlink", path);
else
throw SysError("reading symbolic link '%1%'", path);
else if (rlSize < bufSize)
return std::string(buf.data(), rlSize);
}
}
bool isLink(const Path & path)
{
struct stat st = lstat(path);
return S_ISLNK(st.st_mode);
}
#ifdef __EMSCRIPTEN__
// ---------------------------------------------------------------------------
// WASM: readDirectory via Go host
//
// The Emscripten __syscall_getdents64 → __wasi_fd_readdir path loops
// infinitely due to a cookie-tracking bug. We bypass it entirely by calling
// lix_store_call (declared in wasm-store.hh, included above) with
// op="readDirectory".
//
// Wire format (same encoder/decoder as wasm-store.cc):
// request: len ":" "{" field("op","readDirectory") field("path",path) "}"
// response: len ":" "[" (name-scalar type-scalar)* "]"
// type-scalar: 1-byte DT_* value (DT_DIR=4, DT_LNK=10, DT_REG=8, DT_UNKNOWN=0)
// ---------------------------------------------------------------------------
static constexpr int32_t DIR_RESP_MAX = 4 * 1024 * 1024;
static char g_dir_resp_buf[DIR_RESP_MAX];
// Minimal encoder (same protocol as wasm-store.cc).
static std::string dirEncScalar(std::string_view s)
{
return std::to_string(s.size()) + ":" + std::string(s);
}
static std::string dirEncField(std::string_view k, const std::string & v)
{
return std::to_string(k.size()) + ":" + std::string(k) + "," + std::to_string(v.size()) + ":" + v + ";";
}
DirEntries readDirectory(const Path & path)
{
std::string inner = "{" + dirEncField("op", dirEncScalar("readDirectory"))
+ dirEncField("path", dirEncScalar(path)) + "}";
std::string msg = std::to_string(inner.size()) + ":" + inner;
int32_t n = lix_store_call(msg.data(), static_cast<int32_t>(msg.size()), g_dir_resp_buf, DIR_RESP_MAX);
if (n < 0) {
throw Error("readDirectory: lix_store_call returned %d", n);
}
std::string_view resp(g_dir_resp_buf, static_cast<size_t>(n));
// Parse envelope: len ":" payload
size_t i = 0;
while (i < resp.size() && resp[i] >= '0' && resp[i] <= '9') {
i++;
}
if (i == 0 || i >= resp.size() || resp[i] != ':') {
throw Error("readDirectory: malformed response");
}
size_t total = std::stoull(std::string(resp.substr(0, i)));
resp = resp.substr(i + 1, total);
auto readScalar = [&](std::string_view & sv) -> std::string {
size_t ln = 0;
size_t j = 0;
while (j < sv.size() && sv[j] >= '0' && sv[j] <= '9') {
ln = ln * 10 + (sv[j++] - '0');
}
if (j >= sv.size() || sv[j] != ':') {
throw Error("readDirectory: decode error");
}
j++;
if (j + ln > sv.size()) {
throw Error("readDirectory: scalar truncated");
}
std::string s(sv.substr(j, ln));
sv = sv.substr(j + ln);
return s;
};
// Check for error record { err: ... }
if (!resp.empty() && resp[0] == '{') {
auto sv = resp.substr(1);
if (!sv.empty() && sv[0] != '}') {
auto key = readScalar(sv);
if (!sv.empty()) {
sv = sv.substr(1); // consume ','
}
auto val = readScalar(sv);
if (key == "err") {
// val itself is a length-prefixed scalar; unwrap it
std::string_view vsv(val);
throw Error("readDirectory: %s", readScalar(vsv));
}
}
throw Error("readDirectory: unexpected record response");
}
// Parse list: "[" (name-scalar type-scalar)* "]"
if (resp.empty() || resp[0] != '[') {
throw Error("readDirectory: expected '['");
}
auto sv = resp.substr(1);
DirEntries entries;
while (!sv.empty() && sv[0] != ']') {
auto name = readScalar(sv);
if (sv.empty()) {
throw Error("readDirectory: missing type");
}
auto typebytes = readScalar(sv);
unsigned char dt = typebytes.empty() ? DT_UNKNOWN : static_cast<unsigned char>(typebytes[0]);
entries.emplace_back(std::move(name), 0 /* ino */, dt);
}
return entries;
}
#else // !__EMSCRIPTEN__
static DirEntries readDirectory(DIR *dir, const Path & path, bool interruptible)
{
DirEntries entries;
entries.reserve(64);
struct dirent * dirent;
while (errno = 0, dirent = readdir(dir)) { /* sic */
if (interruptible) {
checkInterrupt();
}
std::string name = dirent->d_name;
if (name == "." || name == "..") continue;
entries.emplace_back(name, dirent->d_ino,
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
dirent->d_type
#else
DT_UNKNOWN
#endif
);
}
if (errno) throw SysError("reading directory '%1%'", path);
return entries;
}
static DirEntries readDirectory(const Path & path, bool interruptible)
{
AutoCloseDir dir(sys::opendir(path));
if (!dir) {
throw SysError("opening directory '%1%'", path);
}
return readDirectory(dir.get(), path, interruptible);
}
DirEntries readDirectory(const Path & path)
{
return readDirectory(path, true);
}
#endif // __EMSCRIPTEN__
unsigned char getFileType(const Path & path)
{
struct stat st = lstat(path);
if (S_ISDIR(st.st_mode)) return DT_DIR;
if (S_ISLNK(st.st_mode)) return DT_LNK;
if (S_ISREG(st.st_mode)) return DT_REG;
return DT_UNKNOWN;
}
std::string readFile(const Path & path)
{
AutoCloseFD fd{sys::open(path, O_RDONLY | O_CLOEXEC)};
if (!fd)
throw SysError("opening file '%1%'", path);
return readFile(fd.get());
}
Generator<Bytes> readFileSource(const Path & path)
{
AutoCloseFD fd{sys::open(path, O_RDONLY | O_CLOEXEC)};
if (!fd)
throw SysError("opening file '%s'", path);
return [](AutoCloseFD fd) -> Generator<Bytes> {
co_yield drainFDSource(fd.get());
}(std::move(fd));
}
static AutoCloseFD openForWrite(const Path & path, mode_t mode)
{
AutoCloseFD fd{sys::open(path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode)};
if (!fd)
throw SysError("opening file '%1%'", path);
return fd;
}
static AutoCloseFD openForWriteExcl(const Path & path, mode_t mode)
{
AutoCloseFD fd{sys::open(path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC | O_EXCL, mode)};
if (!fd)
throw SysError("opening file '%1%'", path);
return fd;
}
void writeFile(const Path & path, std::string_view s, mode_t mode, bool allowInterrupts)
{
AutoCloseFD fd = openForWrite(path, mode);
writeFile(fd, s, mode, allowInterrupts);
/* Close explicitly to propagate the exceptions. */
fd.close();
}
void writeFileExcl(const Path & path, std::string_view s, mode_t mode, bool allowInterrupts)
{
AutoCloseFD fd = openForWriteExcl(path, mode);
writeFile(fd, s, mode, allowInterrupts);
// Close explicitly to propagate the exceptions.
fd.close();
}
void writeFile(AutoCloseFD & fd, std::string_view s, mode_t mode, bool allowInterrupts)
{
assert(fd);
try {
writeFull(fd.get(), s, allowInterrupts);
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", fd.guessOrInventPath());
throw;
}
}
void writeFileUninterruptible(const Path & path, std::string_view s, mode_t mode)
{
writeFile(path, s, mode, false);
}
void writeFileAndSync(const Path & path, std::string_view s, mode_t mode)
{
{
AutoCloseFD fd = openForWrite(path, mode);
writeFile(fd, s, mode);
fd.fsync();
/* Close explicitly to ensure that exceptions are propagated. */
fd.close();
}
syncParent(path);
}
static void closeForWrite(const Path & path, AutoCloseFD & fd, bool sync)
{
if (sync)
fd.fsync();
// Explicitly close to make sure exceptions are propagated.
fd.close();
if (sync)
syncParent(path);
}
void writeFile(const Path & path, Source & source, mode_t mode)
{
AutoCloseFD fd = openForWrite(path, mode);
std::vector<char> buf(64ul * 1024);
try {
while (true) {
try {
auto n = source.read(buf.data(), buf.size());
writeFull(fd.get(), {buf.data(), n});
} catch (EndOfFile &) { break; }
}
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", path);
throw;
}
closeForWrite(path, fd, false);
}
void writeFileExcl(const Path & path, Source & source, mode_t mode)
{
AutoCloseFD fd = openForWriteExcl(path, mode);
std::vector<char> buf(64ul * 1024);
try {
while (true) {
try {
auto n = source.read(buf.data(), buf.size());
writeFull(fd.get(), {buf.data(), n});
} catch (EndOfFile &) { break; }
}
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", path);
throw;
}
closeForWrite(path, fd, false);
}
kj::Promise<Result<void>> writeFile(const Path & path, AsyncInputStream & source, mode_t mode)
try {
AutoCloseFD fd = openForWrite(path, mode);
std::vector<char> buf(64ul * 1024);
try {
while (true) {
if (auto n = TRY_AWAIT(source.read(buf.data(), buf.size()))) {
writeFull(fd.get(), {buf.data(), *n});
} else {
break;
}
}
} catch (Error & e) {
e.addTrace({}, "writing file '%1%'", path);
throw;
}
closeForWrite(path, fd, false);
co_return result::success();
} catch (...) {
co_return result::current_exception();
}
void syncParent(const Path & path)
{
AutoCloseFD fd{sys::open(dirOf(path), O_RDONLY, 0)};
if (!fd)
throw SysError("opening file '%1%'", path);
fd.fsync();
}
#ifndef __EMSCRIPTEN__
// deletePath and helpers are not needed in the WASM evaluator (no GC, no path
// deletion) and they depend on the readDirectory(DIR*,...) overload which is
// also excluded from the WASM build.
/* TODO(horrors): a better structure that links all parent fds for the traversal root
* should be considered for this code
*/
static void _deletePath(int parentfd, const std::string & name, uint64_t & bytesFreed, bool interruptible)
{
/* This ensures that `name` is an immediate child of `parentfd`. */
assert(!name.empty() && name.find('/') == std::string::npos && "`name` is an immediate child to `parentfd`");
if (interruptible) {
checkInterrupt();
}
/* FIXME(horrors): there's a minor TOCTOU here.
* we fstatat the inode nofollow, check if this is a directory
* and then open it.
* a better alternative is open it as O_PATH as a namefd.
* if it's a directory, it can be openat with the namefd.
*/
struct stat st;
if (sys::fstatat(parentfd, name, &st, AT_SYMLINK_NOFOLLOW) == -1) {
if (errno == ENOENT) return;
throw SysError("getting status of '%1%' in directory '%2%'", name, guessOrInventPathFromFD(parentfd));
}
if (!S_ISDIR(st.st_mode)) {
/* We are about to delete a file. Will it likely free space? */
switch (st.st_nlink) {
/* Yes: last link. */
case 1:
bytesFreed += st.st_size;
break;
/* Maybe: yes, if 'auto-optimise-store' or manual optimisation
was performed. Instead of checking for real let's assume
it's an optimised file and space will be freed.
In worst case we will double count on freed space for files
with exactly two hardlinks for unoptimised packages.
*/
case 2:
bytesFreed += st.st_size;
break;
/* No: 3+ links. */
default:
break;
}
}
if (S_ISDIR(st.st_mode)) {
/* Make the directory accessible. */
const auto PERM_MASK = S_IRUSR | S_IWUSR | S_IXUSR;
if ((st.st_mode & PERM_MASK) != PERM_MASK) {
if (sys::fchmodat(parentfd, name, st.st_mode | PERM_MASK, 0) == -1) {
throw SysError("chmod '%1%' in directory '%2%'", name, guessOrInventPathFromFD(parentfd));
}
}
auto fd = sys::openat(parentfd, name, O_RDONLY | O_DIRECTORY | O_NOFOLLOW);
if (!fd) {
throw SysError(
"opening directory '%1%' in directory '%2%'",
name,
guessOrInventPathFromFD(parentfd)
);
}
AutoCloseDir dir(fdopendir(fd.get()));
if (!dir)
throw SysError("opening directory '%1%' in directory '%2%'", name, guessOrInventPathFromFD(parentfd));
fd.release();
for (auto & i : readDirectory(dir.get(), name, interruptible))
_deletePath(dirfd(dir.get()), i.name, bytesFreed, interruptible);
}
int flags = S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0;
if (sys::unlinkat(parentfd, name, flags) == -1) {
if (errno == ENOENT) return;
throw SysError("cannot unlink '%1%' in directory '%2%'", name, guessOrInventPathFromFD(parentfd));
}
}
static void _deletePath(const Path & path, uint64_t & bytesFreed, bool interruptible)
{
Path dir = dirOf(path);
if (dir == "")
dir = "/";
AutoCloseFD dirfd{sys::open(dir, O_RDONLY)};
if (!dirfd) {
if (errno == ENOENT) return;
throw SysError("opening directory '%1%'", path);
}
_deletePath(dirfd.get(), std::string(baseNameOf(path)), bytesFreed, interruptible);
}
void deletePath(const Path & path)
{
uint64_t dummy;
deletePath(path, dummy);
}
void deletePathUninterruptible(const Path & path)
{
uint64_t dummy;
_deletePath(path, dummy, false);
}
void deletePath(const Path & path, uint64_t & bytesFreed)
{
//Activity act(*logger, lvlDebug, "recursively deleting path '%1%'", path);
bytesFreed = 0;
_deletePath(path, bytesFreed, true);
}
#endif // __EMSCRIPTEN__
Paths createDirs(const Path & path)
{
Paths created;
if (path == "/") return created;
struct stat st;
if (sys::lstat(path, &st) == -1) {
created = createDirs(dirOf(path));
if (sys::mkdir(path, 0777) == -1 && errno != EEXIST) {
throw SysError("creating directory '%1%'", path);
}
st = lstat(path);
created.push_back(path);
}
if (S_ISLNK(st.st_mode) && sys::stat(path, &st) == -1) {
throw SysError("statting symlink '%1%'", path);
}
if (!S_ISDIR(st.st_mode)) throw Error("'%1%' is not a directory", path);
return created;
}
//////////////////////////////////////////////////////////////////////
AutoDelete::AutoDelete() : del{false} {}
AutoDelete::AutoDelete(const std::string & p, bool recursive) : path(p)
{
del = true;
this->recursive = recursive;
}
AutoDelete::~AutoDelete()
{
try {
if (del) {
if (recursive)
deletePath(path);
else {
if (sys::remove(path) == -1) {
throw SysError("cannot unlink '%1%'", path);
}
}
}
} catch (...) {
ignoreExceptionInDestructor();
}
}
void AutoDelete::cancel()
{
del = false;
}
void AutoDelete::reset(const Path & p, bool recursive) {
path = p;
this->recursive = recursive;
del = true;
}
//////////////////////////////////////////////////////////////////////
Path createTempSubdir(const Path & parent, const std::optional<Path> & prefix,
mode_t mode)
{
checkInterrupt();
Path tmpDir = makeTempPath(canonPath(parent, true) + "/", prefix);
if (sys::mkdir(tmpDir, mode) == 0) {
#if __FreeBSD__
/* Explicitly set the group of the directory. This is to
work around around problems caused by BSD's group
ownership semantics (directories inherit the group of
the parent). For instance, the group of /tmp on
FreeBSD is "wheel", so all directories created in /tmp
will be owned by "wheel"; but if the user is not in
"wheel", then "tar" will fail to unpack archives that
have the setgid bit set on directories. */
if (chown(tmpDir.c_str(), (uid_t) -1, getegid()) != 0) {
throw SysError("setting group of directory '%1%'", tmpDir);
}
#endif
return tmpDir;
}
throw SysError("creating directory '%1%'", tmpDir);
}
Path makeTempPath(const Path & root, const std::optional<Path> & prefix)
{
static thread_local std::random_device generator{};
std::uniform_int_distribution<uint64_t> uniform_dist{};
const uint64_t entropy[2] = {uniform_dist(generator), uniform_dist(generator)};
auto unique = base32Encode(std::as_bytes(std::span(entropy)));
if (prefix) {
return fmt("%s%s-%s", root, *prefix, unique);
} else {
return root + unique;
}
}
Path makeTempSiblingPath(const Path & path)
{
return makeTempPath(fs::path(path).remove_filename());
}
void createSymlink(const Path & target, const Path & link)
{
if (sys::symlink(target, link)) {
throw SysError("creating symlink from '%1%' to '%2%'", link, target);
}
}
void replaceSymlink(const Path & target, const Path & link)
{
Path tmp = canonPath(makeTempSiblingPath(link));
createSymlink(target, tmp);
renameFile(tmp, link);
}
void setWriteTime(const fs::path & p, const struct stat & st)
{
struct timeval times[2];
times[0] = {
.tv_sec = st.st_atime,
.tv_usec = 0,
};
times[1] = {
.tv_sec = st.st_mtime,
.tv_usec = 0,
};
if (sys::lutimes(p, times) != 0) {
throw SysError("changing modification time of '%s'", p);
}
}
void copy(const fs::directory_entry & from, const fs::path & to, CopyFileFlags flags)
{
// TODO: Rewrite the `is_*` to use `symlink_status()`
auto statOfFrom = lstat(from.path());
auto fromStatus = from.symlink_status();
// Mark the directory as writable so that we can delete its children
if (flags.deleteAfter && fs::is_directory(fromStatus)) {
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
}
if (fs::is_symlink(fromStatus) || fs::is_regular_file(fromStatus)) {
auto opts = fs::copy_options::overwrite_existing;
if (!flags.followSymlinks) {
opts |= fs::copy_options::copy_symlinks;
}
fs::copy(from.path(), to, opts);
} else if (fs::is_directory(fromStatus)) {
fs::create_directory(to);
for (auto & entry : fs::directory_iterator(from.path())) {
copy(entry, to / entry.path().filename(), flags);
}
} else {
throw Error("file '%s' has an unsupported type", from.path());
}
setWriteTime(to, statOfFrom);
if (flags.deleteAfter) {
if (!fs::is_symlink(fromStatus))
fs::permissions(from.path(), fs::perms::owner_write, fs::perm_options::add | fs::perm_options::nofollow);
fs::remove(from.path());
}
}
void copyFile(const Path & oldPath, const Path & newPath, CopyFileFlags flags)
{
return copy(fs::directory_entry(fs::path(oldPath)), fs::path(newPath), flags);
}
void renameFile(const Path & oldName, const Path & newName)
{
fs::rename(oldName, newName);
}
void moveFile(const Path & oldName, const Path & newName)
{
try {
renameFile(oldName, newName);
} catch (fs::filesystem_error & e) { // NOLINT(lix-foreign-exceptions)
auto oldPath = fs::path(oldName);
auto newPath = fs::path(newName);
// For the move to be as atomic as possible, copy to a temporary
// directory
fs::path temp = createTempSubdir(newPath.parent_path(), "rename-tmp");
Finally removeTemp = [&]() { fs::remove(temp); };
auto tempCopyTarget = temp / "copy-target";
if (e.code().value() == EXDEV) {
fs::remove(newPath);
printTaggedWarning("Can’t rename %s as %s, copying instead", oldName, newName);
copy(fs::directory_entry(oldPath), tempCopyTarget, { .deleteAfter = true });
renameFile(tempCopyTarget, newPath);
}
}
}
}
|