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
|
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/port/path.h"
#include "td/utils/port/config.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/port/detail/skip_eintr.h"
#include "td/utils/ScopeGuard.h"
#include "td/utils/SliceBuilder.h"
#if TD_PORT_WINDOWS
#include "td/utils/port/FromApp.h"
#include "td/utils/port/wstring_convert.h"
#include "td/utils/Random.h"
#endif
#if TD_PORT_POSIX
#include <dirent.h>
#include <limits.h>
#include <stdio.h>
// We don't want warnings from system headers
#if TD_GCC
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif
#include <sys/stat.h>
#if TD_GCC
#pragma GCC diagnostic pop
#endif
#include <sys/types.h>
#include <unistd.h>
#endif
#if TD_DARWIN
#include <sys/syslimits.h>
#endif
#include <cerrno>
#include <cstdlib>
#include <string>
namespace td {
static string temporary_dir;
Status set_temporary_dir(CSlice dir) {
string input_dir = dir.str();
if (!dir.empty() && dir.back() != TD_DIR_SLASH) {
input_dir += TD_DIR_SLASH;
}
TRY_STATUS(mkpath(input_dir, 0750));
TRY_RESULT_ASSIGN(temporary_dir, realpath(input_dir));
return Status::OK();
}
Status mkpath(CSlice path, int32 mode) {
Status first_error = Status::OK();
Status last_error = Status::OK();
for (size_t i = 1; i < path.size(); i++) {
if (path[i] == TD_DIR_SLASH) {
last_error = mkdir(PSLICE() << path.substr(0, i), mode);
if (last_error.is_error() && first_error.is_ok()) {
first_error = last_error.clone();
}
}
}
if (last_error.is_error()) {
if (last_error.message() == first_error.message() && last_error.code() == first_error.code()) {
return first_error;
}
return last_error.move_as_error_suffix(PSLICE() << ": " << first_error);
}
return Status::OK();
}
Status rmrf(CSlice path) {
return walk_path(path, [](CSlice path, WalkPath::Type type) {
switch (type) {
case WalkPath::Type::EnterDir:
break;
case WalkPath::Type::ExitDir:
rmdir(path).ignore();
break;
case WalkPath::Type::NotDir:
unlink(path).ignore();
break;
}
});
}
#if TD_PORT_POSIX
Status mkdir(CSlice dir, int32 mode) {
int mkdir_res = [&] {
int res;
do {
errno = 0; // just in case
res = ::mkdir(dir.c_str(), static_cast<mode_t>(mode));
} while (res < 0 && (errno == EINTR || errno == EAGAIN));
return res;
}();
if (mkdir_res == 0) {
return Status::OK();
}
auto mkdir_errno = errno;
if (mkdir_errno == EEXIST) {
// TODO check that it is a directory
return Status::OK();
}
return Status::PosixError(mkdir_errno, PSLICE() << "Can't create directory \"" << dir << '"');
}
Status rename(CSlice from, CSlice to) {
int rename_res = detail::skip_eintr([&] { return ::rename(from.c_str(), to.c_str()); });
if (rename_res < 0) {
return OS_ERROR(PSLICE() << "Can't rename \"" << from << "\" to \"" << to << '\"');
}
return Status::OK();
}
Result<string> realpath(CSlice slice, bool ignore_access_denied) {
char full_path[PATH_MAX + 1];
string res;
char *err = detail::skip_eintr_cstr([&] { return ::realpath(slice.c_str(), full_path); });
if (err != full_path) {
if (ignore_access_denied && (errno == EACCES || errno == EPERM)) {
res = slice.str();
} else {
return OS_ERROR(PSLICE() << "Realpath failed for \"" << slice << '"');
}
} else {
res = full_path;
}
if (res.empty()) {
return Status::Error("Empty path");
}
if (!slice.empty() && slice.end()[-1] == TD_DIR_SLASH) {
if (res.back() != TD_DIR_SLASH) {
res += TD_DIR_SLASH;
}
}
return res;
}
Status chdir(CSlice dir) {
int chdir_res = detail::skip_eintr([&] { return ::chdir(dir.c_str()); });
if (chdir_res) {
return OS_ERROR(PSLICE() << "Can't change directory to \"" << dir << '"');
}
return Status::OK();
}
Status rmdir(CSlice dir) {
int rmdir_res = detail::skip_eintr([&] { return ::rmdir(dir.c_str()); });
if (rmdir_res) {
return OS_ERROR(PSLICE() << "Can't delete directory \"" << dir << '"');
}
return Status::OK();
}
Status unlink(CSlice path) {
int unlink_res = detail::skip_eintr([&] { return ::unlink(path.c_str()); });
if (unlink_res) {
return OS_ERROR(PSLICE() << "Can't unlink \"" << path << '"');
}
return Status::OK();
}
CSlice get_temporary_dir() {
static bool is_inited = [] {
if (temporary_dir.empty()) {
const char *s = std::getenv("TMPDIR");
if (s != nullptr && s[0] != '\0') {
temporary_dir = s;
} else if (P_tmpdir != nullptr && P_tmpdir[0] != '\0') {
temporary_dir = P_tmpdir;
} else {
return false;
}
}
if (temporary_dir.size() > 1 && temporary_dir.back() == TD_DIR_SLASH) {
temporary_dir.pop_back();
}
return true;
}();
LOG_IF(FATAL, !is_inited) << "Can't find temporary directory";
return temporary_dir;
}
Result<std::pair<FileFd, string>> mkstemp(CSlice dir) {
if (dir.empty()) {
dir = get_temporary_dir();
if (dir.empty()) {
return Status::Error("Can't find temporary directory");
}
}
TRY_RESULT(dir_real, realpath(dir));
CHECK(!dir_real.empty());
string file_pattern;
file_pattern.reserve(dir_real.size() + 14);
file_pattern = dir_real;
if (file_pattern.back() != TD_DIR_SLASH) {
file_pattern += TD_DIR_SLASH;
}
file_pattern += "tmpXXXXXXXXXX";
int fd = detail::skip_eintr([&] { return ::mkstemp(&file_pattern[0]); });
if (fd == -1) {
return OS_ERROR(PSLICE() << "Can't create temporary file \"" << file_pattern << '"');
}
if (close(fd)) {
return OS_ERROR(PSLICE() << "Can't close temporary file \"" << file_pattern << '"');
}
// TODO create file from fd
TRY_RESULT(file, FileFd::open(file_pattern, FileFd::Write | FileFd::Truncate | FileFd::Append));
return std::make_pair(std::move(file), std::move(file_pattern));
}
Result<string> mkdtemp(CSlice dir, Slice prefix) {
if (dir.empty()) {
dir = get_temporary_dir();
if (dir.empty()) {
return Status::Error("Can't find temporary directory");
}
}
TRY_RESULT(dir_real, realpath(dir));
CHECK(!dir_real.empty());
string dir_pattern;
dir_pattern.reserve(dir_real.size() + prefix.size() + 7);
dir_pattern = dir_real;
if (dir_pattern.back() != TD_DIR_SLASH) {
dir_pattern += TD_DIR_SLASH;
}
dir_pattern.append(prefix.begin(), prefix.size());
dir_pattern += "XXXXXX";
char *result = detail::skip_eintr_cstr([&] { return ::mkdtemp(&dir_pattern[0]); });
if (result == nullptr) {
return OS_ERROR(PSLICE() << "Can't create temporary directory \"" << dir_pattern << '"');
}
return result;
}
namespace detail {
using WalkFunction = std::function<WalkPath::Action(CSlice name, WalkPath::Type type)>;
Result<bool> walk_path_dir(string &path, FileFd fd, const WalkFunction &func) TD_WARN_UNUSED_RESULT;
Result<bool> walk_path_dir(string &path, const WalkFunction &func) TD_WARN_UNUSED_RESULT;
Result<bool> walk_path_file(string &path, const WalkFunction &func) TD_WARN_UNUSED_RESULT;
Result<bool> walk_path(string &path, const WalkFunction &func) TD_WARN_UNUSED_RESULT;
Result<bool> walk_path_subdir(string &path, DIR *dir, const WalkFunction &func) {
while (true) {
errno = 0;
auto *entry = readdir(dir);
auto readdir_errno = errno;
if (readdir_errno) {
return Status::PosixError(readdir_errno, "readdir");
}
if (entry == nullptr) {
return true;
}
Slice name = Slice(static_cast<const char *>(entry->d_name));
if (name == "." || name == "..") {
continue;
}
auto size = path.size();
if (path.back() != TD_DIR_SLASH) {
path += TD_DIR_SLASH;
}
path.append(name.begin(), name.size());
SCOPE_EXIT {
path.resize(size);
};
Result<bool> status = true;
#ifdef DT_DIR
if (entry->d_type == DT_UNKNOWN) {
status = walk_path(path, func);
} else if (entry->d_type == DT_DIR) {
status = walk_path_dir(path, func);
} else if (entry->d_type == DT_REG) {
status = walk_path_file(path, func);
}
#else
#if !TD_SOLARIS
#warning "Slow walk_path"
#endif
status = walk_path(path, func);
#endif
if (status.is_error() || !status.ok()) {
return status;
}
}
}
Result<bool> walk_path_dir(string &path, DIR *subdir, const WalkFunction &func) {
SCOPE_EXIT {
closedir(subdir);
};
switch (func(path, WalkPath::Type::EnterDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
return true;
case WalkPath::Action::Continue:
break;
}
auto status = walk_path_subdir(path, subdir, func);
if (status.is_error() || !status.ok()) {
return status;
}
switch (func(path, WalkPath::Type::ExitDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
case WalkPath::Action::Continue:
break;
}
return true;
}
Result<bool> walk_path_dir(string &path, FileFd fd, const WalkFunction &func) {
auto native_fd = fd.move_as_native_fd();
auto *subdir = fdopendir(native_fd.fd());
if (subdir == nullptr) {
return OS_ERROR("fdopendir");
}
native_fd.release();
return walk_path_dir(path, subdir, func);
}
Result<bool> walk_path_dir(string &path, const WalkFunction &func) {
auto *subdir = opendir(path.c_str());
if (subdir == nullptr) {
return OS_ERROR(PSLICE() << tag("opendir", path));
}
return walk_path_dir(path, subdir, func);
}
Result<bool> walk_path_file(string &path, const WalkFunction &func) {
switch (func(path, WalkPath::Type::NotDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
case WalkPath::Action::Continue:
break;
}
return true;
}
Result<bool> walk_path(string &path, const WalkFunction &func) {
TRY_RESULT(fd, FileFd::open(path, FileFd::Read));
TRY_RESULT(stat, fd.stat());
bool is_dir = stat.is_dir_;
bool is_reg = stat.is_reg_;
if (is_dir) {
return walk_path_dir(path, std::move(fd), func);
}
fd.close();
if (is_reg) {
return walk_path_file(path, func);
}
return true;
}
} // namespace detail
Status WalkPath::do_run(CSlice path, const detail::WalkFunction &func) {
string curr_path;
curr_path.reserve(PATH_MAX + 10);
curr_path = path.c_str();
TRY_STATUS(detail::walk_path(curr_path, func));
return Status::OK();
}
#endif
#if TD_PORT_WINDOWS
Status mkdir(CSlice dir, int32 mode) {
TRY_RESULT(wdir, to_wstring(dir));
while (!wdir.empty() && (wdir.back() == L'/' || wdir.back() == L'\\')) {
wdir.pop_back();
}
auto status = td::CreateDirectoryFromAppW(wdir.c_str(), nullptr);
if (status == 0 && GetLastError() != ERROR_ALREADY_EXISTS) {
return OS_ERROR(PSLICE() << "Can't create directory \"" << dir << '"');
}
return Status::OK();
}
Status rename(CSlice from, CSlice to) {
TRY_RESULT(wfrom, to_wstring(from));
TRY_RESULT(wto, to_wstring(to));
auto status = td::MoveFileExFromAppW(wfrom.c_str(), wto.c_str(), MOVEFILE_REPLACE_EXISTING);
if (status == 0) {
return OS_ERROR(PSLICE() << "Can't rename \"" << from << "\" to \"" << to << '\"');
}
return Status::OK();
}
Result<string> realpath(CSlice slice, bool ignore_access_denied) {
wchar_t buf[MAX_PATH + 1];
TRY_RESULT(wslice, to_wstring(slice));
auto status = GetFullPathNameW(wslice.c_str(), MAX_PATH, buf, nullptr);
string res;
if (status == 0) {
if (ignore_access_denied && errno == ERROR_ACCESS_DENIED) {
res = slice.str();
} else {
return OS_ERROR(PSLICE() << "GetFullPathNameW failed for \"" << slice << '"');
}
} else {
TRY_RESULT_ASSIGN(res, from_wstring(buf));
}
if (res.empty()) {
return Status::Error("Empty path");
}
if (!slice.empty() && slice.end()[-1] == TD_DIR_SLASH) {
if (res.back() != TD_DIR_SLASH) {
res += TD_DIR_SLASH;
}
}
return res;
}
Status chdir(CSlice dir) {
TRY_RESULT(wdir, to_wstring(dir));
auto res = SetCurrentDirectoryW(wdir.c_str());
if (res == 0) {
return OS_ERROR(PSLICE() << "Can't change directory to \"" << dir << '"');
}
return Status::OK();
}
Status rmdir(CSlice dir) {
TRY_RESULT(wdir, to_wstring(dir));
int status = td::RemoveDirectoryFromAppW(wdir.c_str());
if (!status) {
return OS_ERROR(PSLICE() << "Can't delete directory \"" << dir << '"');
}
return Status::OK();
}
Status unlink(CSlice path) {
TRY_RESULT(wpath, to_wstring(path));
int status = td::DeleteFileFromAppW(wpath.c_str());
if (!status) {
return OS_ERROR(PSLICE() << "Can't unlink \"" << path << '"');
}
return Status::OK();
}
CSlice get_temporary_dir() {
static bool is_inited = [] {
if (temporary_dir.empty()) {
wchar_t buf[MAX_PATH + 1];
if (GetTempPathW(MAX_PATH, buf) == 0) {
auto error = OS_ERROR("GetTempPathW failed");
LOG(FATAL) << error;
}
auto rs = from_wstring(buf);
LOG_IF(FATAL, rs.is_error()) << "GetTempPathW failed: " << rs.error();
temporary_dir = rs.move_as_ok();
}
if (temporary_dir.size() > 1 && temporary_dir.back() == TD_DIR_SLASH) {
temporary_dir.pop_back();
}
return true;
}();
LOG_IF(FATAL, !is_inited) << "Can't find temporary directory";
return temporary_dir;
}
Result<string> mkdtemp(CSlice dir, Slice prefix) {
if (dir.empty()) {
dir = get_temporary_dir();
if (dir.empty()) {
return Status::Error("Can't find temporary directory");
}
}
TRY_RESULT(dir_real, realpath(dir));
CHECK(!dir_real.empty());
string dir_pattern;
dir_pattern.reserve(dir_real.size() + prefix.size() + 7);
dir_pattern = dir_real;
if (dir_pattern.back() != TD_DIR_SLASH) {
dir_pattern += TD_DIR_SLASH;
}
dir_pattern.append(prefix.begin(), prefix.size());
for (auto iter = 0; iter < 20; iter++) {
auto path = dir_pattern;
for (int i = 0; i < 6 + iter / 5; i++) {
path += static_cast<char>(Random::fast('a', 'z'));
}
auto status = mkdir(path);
if (status.is_ok()) {
return path;
}
}
return Status::Error(PSLICE() << "Can't create temporary directory \"" << dir_pattern << '"');
}
Result<std::pair<FileFd, string>> mkstemp(CSlice dir) {
if (dir.empty()) {
dir = get_temporary_dir();
if (dir.empty()) {
return Status::Error("Can't find temporary directory");
}
}
TRY_RESULT(dir_real, realpath(dir));
CHECK(!dir_real.empty());
string file_pattern;
file_pattern.reserve(dir_real.size() + 14);
file_pattern = dir_real;
if (file_pattern.back() != TD_DIR_SLASH) {
file_pattern += TD_DIR_SLASH;
}
file_pattern += "tmp";
for (auto iter = 0; iter < 20; iter++) {
auto path = file_pattern;
for (int i = 0; i < 6 + iter / 5; i++) {
path += static_cast<char>(Random::fast('a', 'z'));
}
auto r_file = FileFd::open(path, FileFd::Write | FileFd::Read | FileFd::CreateNew);
if (r_file.is_ok()) {
return std::make_pair(r_file.move_as_ok(), path);
}
}
return Status::Error(PSLICE() << "Can't create temporary file \"" << file_pattern << '"');
}
static Result<bool> walk_path_dir(const std::wstring &dir_name,
const std::function<WalkPath::Action(CSlice name, WalkPath::Type type)> &func) {
std::wstring name = dir_name + L"\\*";
WIN32_FIND_DATA file_data;
auto handle =
td::FindFirstFileExFromAppW(name.c_str(), FindExInfoStandard, &file_data, FindExSearchNameMatch, nullptr, 0);
if (handle == INVALID_HANDLE_VALUE) {
return OS_ERROR(PSLICE() << "FindFirstFileEx" << tag("name", from_wstring(name).ok()));
}
SCOPE_EXIT {
FindClose(handle);
};
TRY_RESULT(dir_entry_name, from_wstring(dir_name));
switch (func(dir_entry_name, WalkPath::Type::EnterDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
return true;
case WalkPath::Action::Continue:
break;
}
while (true) {
auto full_name = dir_name + L"\\" + file_data.cFileName;
TRY_RESULT(entry_name, from_wstring(full_name));
if (file_data.cFileName[0] != '.') {
if ((file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
TRY_RESULT(is_ok, walk_path_dir(full_name, func));
if (!is_ok) {
return false;
}
} else {
switch (func(entry_name, WalkPath::Type::NotDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
case WalkPath::Action::Continue:
break;
}
}
}
auto status = FindNextFileW(handle, &file_data);
if (status == 0) {
auto last_error = GetLastError();
if (last_error == ERROR_NO_MORE_FILES) {
break;
}
return OS_ERROR("FindNextFileW");
}
}
switch (func(dir_entry_name, WalkPath::Type::ExitDir)) {
case WalkPath::Action::Abort:
return false;
case WalkPath::Action::SkipDir:
case WalkPath::Action::Continue:
break;
}
return true;
}
Status WalkPath::do_run(CSlice path, const std::function<Action(CSlice name, Type)> &func) {
TRY_RESULT(wpath, to_wstring(path));
Slice path_slice = path;
while (!path_slice.empty() && (path_slice.back() == '/' || path_slice.back() == '\\')) {
path_slice.remove_suffix(1);
wpath.pop_back();
}
TRY_STATUS(walk_path_dir(wpath, func));
return Status::OK();
}
#endif
} // namespace td
|