forked from asheplyakov/buildcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_utils.cpp
1075 lines (956 loc) · 34.2 KB
/
file_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
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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//--------------------------------------------------------------------------------------------------
// Copyright (c) 2018 Marcus Geelnard
//
// This software is provided 'as-is', without any express or implied warranty. In no event will the
// authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose, including commercial
// applications, and to alter it and redistribute it freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not claim that you wrote
// the original software. If you use this software in a product, an acknowledgment in the
// product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
// being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//--------------------------------------------------------------------------------------------------
#include <base/debug_utils.hpp>
#include <base/env_utils.hpp>
#include <base/file_utils.hpp>
#include <base/string_list.hpp>
#include <base/unicode_utils.hpp>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <algorithm>
#include <atomic>
#include <stdexcept>
#include <vector>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <direct.h>
#include <shlobj.h>
#include <userenv.h>
#include <windows.h>
#undef ERROR
#undef log
#else
#include <cstdlib>
#include <dirent.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <utime.h>
#endif
#include <sys/stat.h>
#include <sys/types.h>
// S_ISDIR/S_ISREG are not defined by MSVC, but _S_IFDIR/_S_IFREG are.
#if defined(_WIN32) && !defined(S_ISDIR)
#define S_ISDIR(x) (((x)&_S_IFDIR) != 0)
#endif
#if defined(_WIN32) && !defined(S_ISREG)
#define S_ISREG(x) (((x)&_S_IFREG) != 0)
#endif
namespace bcache {
namespace file {
namespace {
// Directory separator for paths.
#ifdef _WIN32
const char PATH_SEPARATOR_CHR = '\\';
#else
const char PATH_SEPARATOR_CHR = '/';
#endif
const auto PATH_SEPARATOR = std::string(1, PATH_SEPARATOR_CHR);
// Delimiter character for the PATH environment variable.
#ifdef _WIN32
const char PATH_DELIMITER_CHR = ';';
#else
const char PATH_DELIMITER_CHR = ':';
#endif
const auto PATH_DELIMITER = std::string(1, PATH_DELIMITER_CHR);
// This is a static variable that holds a strictly incrementing number used for generating unique
// temporary file names.
std::atomic_uint_fast32_t s_tmp_name_number;
int get_process_id() {
#ifdef _WIN32
return static_cast<int>(GetCurrentProcessId());
#else
return static_cast<int>(getpid());
#endif
}
std::string::size_type get_last_path_separator_pos(const std::string& path) {
#if defined(_WIN32)
const auto pos1 = path.rfind('/');
const auto pos2 = path.rfind('\\');
std::string::size_type pos;
if (pos1 == std::string::npos) {
pos = pos2;
} else if (pos2 == std::string::npos) {
pos = pos1;
} else {
pos = std::max(pos1, pos2);
}
#else
const auto pos = path.rfind(PATH_SEPARATOR_CHR);
#endif
return pos;
}
#ifdef _WIN32
int64_t two_dwords_to_int64(const DWORD low, const DWORD high) {
return static_cast<int64_t>(static_cast<uint64_t>(static_cast<uint32_t>(low)) |
(static_cast<uint64_t>(static_cast<uint32_t>(high)) << 32));
}
#endif
void remove_dir_internal(const std::string& path, const bool ignore_errors) {
#ifdef _WIN32
const auto success = (_wrmdir(utf8_to_ucs2(path).c_str()) == 0);
#else
const auto success = (rmdir(path.c_str()) == 0);
#endif
if ((!success) && (!ignore_errors)) {
throw std::runtime_error("Unable to remove dir.");
}
}
/// @brief Get a number based on a high resolution timer.
/// @note The time unit is unspecified, and taking the difference between two consecutive return
/// values is not supported as the time scale may be non-continuous.
uint64_t get_hires_time() {
#if defined(_WIN32)
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
return static_cast<uint64_t>(count.QuadPart);
#else
struct timeval tv;
gettimeofday(&tv, nullptr);
return (static_cast<uint64_t>(tv.tv_sec) << 20) | static_cast<uint64_t>(tv.tv_usec);
#endif
}
/// @brief Convert an integer to a human-readable string.
/// @param x The integer to convert.
/// @returns a string that consists of alphanumerical characters. The string is at least one
/// character long, and at most 13 characters long.
std::string to_id_part(const uint64_t x) {
static const char CHARS[] = "abcdefghijklmnopqrstuvwxyz0123456789";
static const auto NUM_CHARS = static_cast<uint64_t>(sizeof(CHARS) / sizeof(CHARS[0]) - 1);
std::string part;
if (x == 0U) {
part += 'u';
} else {
auto q = x;
while (q != 0U) {
part += CHARS[q % NUM_CHARS];
q = q / NUM_CHARS;
}
}
return part;
}
/// @brief Get implicit file extensions for executable files.
///
/// The list is on the form ["", ".foo", ".bar", ...]. The first item is an empty string
/// (representing "no extra extension"), and the list is guaranteed to contain at least one item.
///
/// @returns a list of valid extensions.
string_list_t get_exe_extensions() {
#if defined(_WIN32)
// Use PATHEXT to determine valid executable file extensions. For more info, see:
// https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/start
std::string path_ext_str = ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC";
const env_var_t path_ext_env("PATHEXT");
if (path_ext_env) {
path_ext_str = path_ext_env.as_string();
}
// Note: We use lower case since we want to do case insensitive string compares.
return string_list_t({""}) + string_list_t(lower_case(path_ext_str), ";");
#else
// On POSIX systems, there is no such thing as an exe extension that gets implicitly added when
// invoking a command.
return string_list_t({""});
#endif
}
bool is_absolute_path(const std::string& path) {
#ifdef _WIN32
const bool is_abs_drive =
(path.size() >= 3) && (path[1] == ':') && ((path[2] == '\\') || (path[2] == '/'));
const bool is_abs_net = (path.size() >= 2) && (path[0] == '\\') && (path[1] == '\\');
return is_abs_drive || is_abs_net;
#else
return (!path.empty()) && (path[0] == PATH_SEPARATOR_CHR);
#endif
}
bool is_relateive_path(const std::string& path) {
return get_last_path_separator_pos(path) != std::string::npos;
}
} // namespace
tmp_file_t::tmp_file_t(const std::string& dir, const std::string& extension) {
// Generate a file name based on a unique identifier.
const auto file_name = std::string("bcache-") + get_unique_id();
// Concatenate base dir, file name and extension into the full path.
m_path = append_path(dir, file_name + extension);
}
tmp_file_t::~tmp_file_t() {
try {
if (file_exists(m_path)) {
remove_file(m_path);
} else if (dir_exists(m_path)) {
remove_dir(m_path);
}
} catch (const std::exception& e) {
debug::log(debug::ERROR) << e.what();
}
}
scoped_work_dir_t::scoped_work_dir_t(const std::string& new_work_dir) {
if (!new_work_dir.empty()) {
m_old_work_dir = get_cwd();
set_cwd(new_work_dir);
}
}
scoped_work_dir_t::~scoped_work_dir_t() {
if (!m_old_work_dir.empty()) {
set_cwd(m_old_work_dir);
}
}
std::string append_path(const std::string& path, const std::string& append) {
if (path.empty() || append.empty() || path.back() == PATH_SEPARATOR_CHR) {
return path + append;
}
return path + PATH_SEPARATOR + append;
}
std::string append_path(const std::string& path, const char* append) {
return append_path(path, std::string(append));
}
std::string canonicalize_path(const std::string& path) {
#ifdef _WIN32
std::string result = path;
// Start by converting forward slashes to back slashes (GetFullPathNameW does not do that).
for (size_t i = 0; i < result.size(); ++i) {
const auto c = result[i];
result[i] = (c == '/') ? '\\' : c;
}
// Use a Win32 API function to resolve as much as possible. Unfortunately there does not seem to
// be a single Win32 API function that can give sane results (hence the pre/post processing).
{
wchar_t buf[MAX_PATH];
const DWORD l = GetFullPathNameW(utf8_to_ucs2(result).c_str(), MAX_PATH, &buf[0], NULL);
if (l == 0 || l >= MAX_PATH) {
throw std::runtime_error("Unable to canonicalize the path " + result);
}
result = ucs2_to_utf8(std::wstring(buf));
}
// Drop trailing back slash.
{
const auto last = static_cast<int>(result.length()) - 1;
if (last >= 2 && result[last] == '\\' && result[last - 1] != ':') {
result = result.substr(0, last);
}
}
// Convert drive letters to uppercase.
if (result.length() >= 2U && result[1] == ':') {
const auto drive_char = result.substr(0, 1);
result[0] = upper_case(drive_char)[0];
}
#else
std::string result = path;
// Resolve relative paths.
if (!is_absolute_path(result)) {
result = append_path(get_cwd(), result);
}
// Simplify "//" and "/./" etc into "/", and resolve "..".
string_list_t parts(result, PATH_SEPARATOR);
string_list_t filtered_parts;
for (const auto& part : parts) {
if (part == "..") {
if (filtered_parts.size() < 1U) {
throw std::runtime_error("Unable to canonicalize the path " + path);
}
filtered_parts.pop_back();
} else if (!part.empty() && part != ".") {
filtered_parts += part;
}
}
result = PATH_SEPARATOR + filtered_parts.join(PATH_SEPARATOR);
#endif
return result;
}
std::string get_extension(const std::string& path) {
const auto pos = path.rfind('.');
// Check that we did not pick up an extension before the last path separator.
const auto sep_pos = get_last_path_separator_pos(path);
if ((pos != std::string::npos) && (sep_pos != std::string::npos) && (pos < sep_pos)) {
return std::string();
}
return (pos != std::string::npos) ? path.substr(pos) : std::string();
}
std::string change_extension(const std::string& path, const std::string& new_ext) {
const auto pos = path.rfind('.');
// Check that we did not pick up an extension before the last path separator.
const auto sep_pos = get_last_path_separator_pos(path);
if ((pos != std::string::npos) && (sep_pos != std::string::npos) && (pos < sep_pos)) {
return path;
}
return (pos != std::string::npos) ? (path.substr(0, pos) + new_ext) : path;
}
std::string get_file_part(const std::string& path, const bool include_ext) {
const auto pos = get_last_path_separator_pos(path);
const auto file_name = (pos != std::string::npos) ? path.substr(pos + 1) : path;
const auto ext_pos = file_name.rfind('.');
return (include_ext || (ext_pos == std::string::npos) || (ext_pos == 0))
? file_name
: file_name.substr(0, ext_pos);
}
std::string get_dir_part(const std::string& path) {
const auto pos = get_last_path_separator_pos(path);
return (pos != std::string::npos) ? path.substr(0, pos) : std::string();
}
std::string get_temp_dir() {
#if defined(_WIN32)
WCHAR buf[MAX_PATH + 1] = {0};
DWORD path_len = GetTempPathW(MAX_PATH + 1, buf);
if (path_len > 0) {
return canonicalize_path(ucs2_to_utf8(std::wstring(buf, path_len)));
}
return std::string();
#else
// 1. Try $XDG_RUNTIME_DIR. See:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
env_var_t xdg_runtime_dir("XDG_RUNTIME_DIR");
if (xdg_runtime_dir && dir_exists(xdg_runtime_dir.as_string())) {
return canonicalize_path(xdg_runtime_dir.as_string());
}
// 2. Try $TMPDIR. See:
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08_03
env_var_t tmpdir("TMPDIR");
if (tmpdir && dir_exists(tmpdir.as_string())) {
return canonicalize_path(tmpdir.as_string());
}
// 3. Fall back to /tmp. See:
// http://refspecs.linuxfoundation.org/FHS_3.0/fhs/ch03s18.html
return std::string("/tmp");
#endif
}
std::string get_user_home_dir() {
#if defined(_WIN32)
#if 0
// TODO(m): We should use SHGetKnownFolderPath() for Vista and later, but this fails to build in
// older MinGW so we skip it a.t.m.
std::string local_app_data;
PWSTR path = nullptr;
try {
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, NULL, &path))) {
local_app_data = ucs2_to_utf8(std::wstring(path));
}
} finally {
if (path != nullptr) {
CoTaskMemFree(path);
}
}
return local_app_data;
#else
std::string user_home;
HANDLE token = nullptr;
if (SUCCEEDED(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token))) {
// Query the necessary buffer size and allocate memory for it.
DWORD buf_size = 0;
GetUserProfileDirectoryW(token, nullptr, &buf_size);
std::vector<WCHAR> buf(buf_size);
// Get the actual path.
if (SUCCEEDED(GetUserProfileDirectoryW(token, buf.data(), &buf_size))) {
user_home = ucs2_to_utf8(std::wstring(buf.data(), buf.size() - 1));
}
CloseHandle(token);
}
return user_home;
#endif
#else
return get_env("HOME");
#endif
}
std::string get_cwd() {
#if defined(_WIN32)
WCHAR buf[MAX_PATH + 1] = {0};
DWORD path_len = GetCurrentDirectoryW(MAX_PATH + 1, buf);
if (path_len > 0) {
return ucs2_to_utf8(std::wstring(buf, path_len));
}
#else
size_t size = 512;
for (; size <= 65536U; size *= 2) {
std::vector<char> buf(size);
auto* ptr = ::getcwd(buf.data(), size);
if (ptr != nullptr) {
return std::string(ptr);
}
}
#endif
throw std::runtime_error("Unable to determine the current working directory.");
}
void set_cwd(const std::string& path) {
#if defined(_WIN32)
const auto success = (SetCurrentDirectoryW(utf8_to_ucs2(path).c_str()) != 0);
#else
const auto success = (chdir(path.c_str()) == 0);
#endif
if (!success) {
throw std::runtime_error("Could not change the current working directory to " + path);
}
}
std::string resolve_path(const std::string& path) {
#if defined(_WIN32)
auto* handle = CreateFileW(utf8_to_ucs2(path).c_str(),
0,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (INVALID_HANDLE_VALUE != handle) {
std::wstring resolved_path;
auto resolved_size = GetFinalPathNameByHandleW(handle, nullptr, 0, FILE_NAME_NORMALIZED);
resolved_path.resize(resolved_size - 1); // terminating null character is added automatically
GetFinalPathNameByHandleW(handle, &resolved_path[0], resolved_size, FILE_NAME_NORMALIZED);
CloseHandle(handle);
if (resolved_path.substr(0, 4) == LR"(\\?\)") {
resolved_path = resolved_path.substr(4);
}
return ucs2_to_utf8(resolved_path);
}
return std::string();
#else
auto* char_ptr = realpath(path.c_str(), nullptr);
if (char_ptr != nullptr) {
auto result = std::string(char_ptr);
std::free(char_ptr);
return result;
}
return std::string();
#endif
}
exe_path_t find_executable(const std::string& program, const std::string& exclude) {
const auto extensions = get_exe_extensions();
std::string file_to_find;
// Handle absolute and relative paths. Examples:
// - "C:\Foo\foo.exe"
// - "somedir/../mysubdir/foo"
if (is_absolute_path(program) || is_relateive_path(program)) {
for (const auto& ext : extensions) {
auto path_with_ext = program + ext;
// Return the full path unless it points to the excluded executable.
auto true_path = resolve_path(path_with_ext);
if (true_path.empty()) {
// Unable to resolve. Try next ext.
continue;
}
if (lower_case(get_file_part(true_path, false)) != exclude) {
const auto& virtual_path = canonicalize_path(path_with_ext);
debug::log(debug::DEBUG) << "Found exe: " << true_path << " (" << program << ", "
<< virtual_path << ")";
return exe_path_t(true_path, virtual_path, program);
}
// ...otherwise search for the named file (which should be a symlink) in the PATH.
// This handles invokations of programs via symbolic links to the buildcache executable.
file_to_find = get_file_part(path_with_ext);
break;
}
} else {
// The path is just a file name without a path.
file_to_find = program;
}
if (!file_to_find.empty()) {
// Get the PATH environment variable.
string_list_t search_path;
#if defined(_WIN32)
{
// For Windows we prepend the current working directory to the PATH, as Windows searches the
// CWD before searching the PATH.
const auto cwd = get_cwd();
if (!cwd.empty()) {
search_path += cwd;
}
}
#endif
search_path += string_list_t(get_env("PATH"), PATH_DELIMITER);
// Iterate the path from start to end and see if we can find the executable file.
for (const auto& base_path : search_path) {
for (const auto& ext : extensions) {
const auto file_name = file_to_find + ext;
auto virtual_path = append_path(base_path, file_name);
auto true_path = resolve_path(virtual_path);
if ((!true_path.empty()) && file_exists(true_path)) {
// Check that this is not the excluded file name.
if (lower_case(get_file_part(true_path, false)) != exclude) {
debug::log(debug::DEBUG)
<< "Found exe: " << true_path << " (" << program << ", " << virtual_path << ")";
return exe_path_t(true_path, virtual_path, program);
}
}
}
}
}
throw std::runtime_error("Could not find the executable file.");
}
void create_dir(const std::string& path) {
#ifdef _WIN32
const auto success = (_wmkdir(utf8_to_ucs2(path).c_str()) == 0);
#else
const auto success = (mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
#endif
if (!success) {
throw std::runtime_error("Unable to create directory " + path);
}
}
void create_dir_with_parents(const std::string& path) {
// Recursively create parent directories if necessary.
const auto parent = get_dir_part(path);
if (parent.size() < path.size() && !parent.empty() && !dir_exists(parent)) {
create_dir_with_parents(parent);
}
// Create the requested directory unless it already exists.
if (!path.empty() && !dir_exists(path)) {
create_dir(path);
}
}
void remove_file(const std::string& path, const bool ignore_errors) {
#ifdef _WIN32
const auto success = (_wunlink(utf8_to_ucs2(path).c_str()) == 0);
#else
const auto success = (unlink(path.c_str()) == 0);
#endif
if ((!success) && (!ignore_errors)) {
throw std::runtime_error("Unable to remove file.");
}
}
void remove_dir(const std::string& path, const bool ignore_errors) {
const auto files = walk_directory(path);
for (const auto& file : files) {
if (file.is_dir()) {
remove_dir_internal(file.path(), ignore_errors);
} else {
remove_file(file.path(), ignore_errors);
}
}
remove_dir_internal(path, ignore_errors);
}
bool dir_exists(const std::string& path) {
#ifdef _WIN32
// Do a quick check if this is a drive letter and assume it exists. For performance reasons
// do not invoke Win32 APIs to verify the volume etc.
if (path.size() == 2 && path[1] == ':') {
return true;
}
struct __stat64 buffer;
const auto success = (_wstat64(utf8_to_ucs2(path).c_str(), &buffer) == 0);
return success && S_ISDIR(buffer.st_mode);
#else
struct stat buffer;
const auto success = (stat(path.c_str(), &buffer) == 0);
return success && S_ISDIR(buffer.st_mode);
#endif
}
bool file_exists(const std::string& path) {
#ifdef _WIN32
struct __stat64 buffer;
const auto success = (_wstat64(utf8_to_ucs2(path).c_str(), &buffer) == 0);
return success && S_ISREG(buffer.st_mode);
#else
struct stat buffer;
const auto success = (stat(path.c_str(), &buffer) == 0);
return success && S_ISREG(buffer.st_mode);
#endif
}
void move(const std::string& from_path, const std::string& to_path) {
// First remove the old target file, if any (otherwise the rename will fail).
if (file_exists(to_path)) {
remove_file(to_path);
}
// Rename the file.
#ifdef _WIN32
const auto success =
(_wrename(utf8_to_ucs2(from_path).c_str(), utf8_to_ucs2(to_path).c_str()) == 0);
#else
const auto success = (std::rename(from_path.c_str(), to_path.c_str()) == 0);
#endif
if (!success) {
throw std::runtime_error("Unable to move file.");
}
}
void copy(const std::string& from_path, const std::string& to_path) {
// Copy to a temporary file first and once the copy has succeeded rename it to the target file.
// This should prevent half-finished copies if the process is terminated prematurely (e.g.
// CTRL+C).
const auto base_path = get_dir_part(to_path);
auto tmp_file = tmp_file_t(base_path, ".tmp");
#ifdef _WIN32
// TODO(m): We could handle paths longer than MAX_PATH, e.g. by prepending strings with "\\?\"?
bool success =
(CopyFileW(utf8_to_ucs2(from_path).c_str(), utf8_to_ucs2(tmp_file.path()).c_str(), FALSE) !=
0);
#else
// For non-Windows systems we use a classic buffered read-write loop.
bool success = false;
auto* from_file = std::fopen(from_path.c_str(), "rb");
if (from_file != nullptr) {
auto* to_file = std::fopen(tmp_file.path().c_str(), "wb");
if (to_file != nullptr) {
// We use a buffer size that typically fits in an L1 cache.
static const int BUFFER_SIZE = 8192;
std::vector<std::uint8_t> buf(BUFFER_SIZE);
success = true;
while (std::feof(from_file) == 0) {
const auto bytes_read = std::fread(buf.data(), 1, buf.size(), from_file);
if (bytes_read == 0U) {
break;
}
const auto bytes_written = std::fwrite(buf.data(), 1, bytes_read, to_file);
if (bytes_written != bytes_read) {
success = false;
break;
}
}
std::fclose(to_file);
}
std::fclose(from_file);
}
#endif
if (!success) {
// Note: At this point the temporary file (if any) will be deleted.
throw std::runtime_error("Unable to copy file.");
}
// Move the temporary file to its target name.
move(tmp_file.path(), to_path);
}
void link_or_copy(const std::string& from_path, const std::string& to_path) {
// First remove the old file, if any (otherwise the hard link will fail).
if (file_exists(to_path)) {
remove_file(to_path);
}
// First try to make a hard link. However this may fail if the files are on different volumes for
// instance.
bool success;
#ifdef _WIN32
success = (CreateHardLinkW(
utf8_to_ucs2(to_path).c_str(), utf8_to_ucs2(from_path).c_str(), nullptr) != 0);
#else
success = (link(from_path.c_str(), to_path.c_str()) == 0);
#endif
// If the hard link failed, make a full copy instead.
if (!success) {
debug::log(debug::DEBUG) << "Hard link failed - copying instead.";
copy(from_path, to_path);
}
}
void touch(const std::string& path) {
#ifdef _WIN32
bool success = false;
HANDLE h = CreateFileW(utf8_to_ucs2(path).c_str(),
FILE_WRITE_ATTRIBUTES,
FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0);
if (h != nullptr) {
FILETIME mtime;
GetSystemTimeAsFileTime(&mtime);
success = (SetFileTime(h, 0, 0, &mtime) != FALSE);
CloseHandle(h);
}
#else
bool success = (utime(path.c_str(), nullptr) == 0);
#endif
if (!success) {
throw std::runtime_error("Unable to touch the file.");
}
}
std::string read(const std::string& path) {
FILE* f;
// Open the file.
#ifdef _WIN32
const auto err = _wfopen_s(&f, utf8_to_ucs2(path).c_str(), L"rb");
if (err != 0) {
throw std::runtime_error("Unable to open the file.");
}
#else
f = std::fopen(path.c_str(), "rb");
if (f == nullptr) {
throw std::runtime_error("Unable to open the file.");
}
#endif
// Get file size.
std::fseek(f, 0, SEEK_END);
const auto file_size = static_cast<size_t>(std::ftell(f));
std::fseek(f, 0, SEEK_SET);
// Read the data into a string.
std::string str;
str.resize(static_cast<std::string::size_type>(file_size));
auto bytes_left = file_size;
while ((bytes_left != 0U) && (std::feof(f) == 0)) {
auto* ptr = &str[file_size - bytes_left];
const auto bytes_read = std::fread(ptr, 1, bytes_left, f);
bytes_left -= bytes_read;
}
// Close the file.
std::fclose(f);
if (bytes_left != 0U) {
throw std::runtime_error("Unable to read the file.");
}
return str;
}
void write(const std::string& data, const std::string& path) {
FILE* f;
// Open the file.
#ifdef _WIN32
const auto err = _wfopen_s(&f, utf8_to_ucs2(path).c_str(), L"wb");
if (err != 0) {
throw std::runtime_error("Unable to open the file.");
}
#else
f = std::fopen(path.c_str(), "wb");
if (f == nullptr) {
throw std::runtime_error("Unable to open the file.");
}
#endif
// Write the data to the file.
const auto file_size = data.size();
auto bytes_left = file_size;
while ((bytes_left != 0U) && (std::ferror(f) == 0)) {
const auto* ptr = &data[file_size - bytes_left];
const auto bytes_written = std::fwrite(ptr, 1, bytes_left, f);
bytes_left -= bytes_written;
}
// Close the file.
std::fclose(f);
if (bytes_left != 0U) {
throw std::runtime_error("Unable to write the file.");
}
}
void write_atomic(const std::string& data, const std::string& path) {
// 1) Write to a temporary file.
const auto base_path = get_dir_part(path);
auto tmp_file = tmp_file_t(base_path, ".tmp");
write(data, tmp_file.path());
// 2) Remove the target path if it already exists.
remove_file(path, true);
// 3) Move the temporary file to the target file name.
move(tmp_file.path(), path);
}
void append(const std::string& data, const std::string& path) {
if (path.empty()) {
throw std::runtime_error("No file path given.");
}
#ifdef _WIN32
// Open the file.
auto* handle = CreateFileW(utf8_to_ucs2(path).c_str(),
FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE,
nullptr,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
nullptr);
if (handle == INVALID_HANDLE_VALUE) {
throw std::runtime_error("Unable to open the file.");
}
// Move to the end of the file.
const auto moved = SetFilePointer(handle, 0, nullptr, FILE_END);
if (moved == INVALID_SET_FILE_POINTER) {
CloseHandle(handle);
throw std::runtime_error("Unable to set file pointer to end-of-file.");
}
// Write the data.
const DWORD bytes_to_write = static_cast<DWORD>(data.size());
DWORD bytes_written;
const auto success =
(WriteFile(handle, data.c_str(), bytes_to_write, &bytes_written, nullptr) != FALSE);
if (!success || bytes_written != bytes_to_write) {
CloseHandle(handle);
throw std::runtime_error("Unable to write to the file.");
}
// Close the file.
CloseHandle(handle);
#else
// Open the file (write pointer is at the end of the file).
auto* f = std::fopen(path.c_str(), "ab");
if (f == nullptr) {
throw std::runtime_error("Unable to open the file.");
}
// Write the data to the file.
const auto file_size = data.size();
auto bytes_left = file_size;
while ((bytes_left != 0U) && (std::ferror(f) == 0)) {
const auto* ptr = &data[file_size - bytes_left];
const auto bytes_written = std::fwrite(ptr, 1, bytes_left, f);
bytes_left -= bytes_written;
}
// Close the file.
std::fclose(f);
if (bytes_left != 0U) {
throw std::runtime_error("Unable to write the file.");
}
#endif
}
file_info_t get_file_info(const std::string& path) {
// TODO(m): This is pretty much copy-paste from walk_directory(). Refactor.
#ifdef _WIN32
WIN32_FIND_DATAW find_data;
auto* find_handle = FindFirstFileW(utf8_to_ucs2(path).c_str(), &find_data);
if (find_handle != INVALID_HANDLE_VALUE) {
const auto name = ucs2_to_utf8(std::wstring(&find_data.cFileName[0]));
const auto file_path = append_path(path, name);
time::seconds_t modify_time = 0;
time::seconds_t access_time = 0;
int64_t size = 0;
bool is_dir = ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
if (!is_dir) {
size = two_dwords_to_int64(find_data.nFileSizeLow, find_data.nFileSizeHigh);
modify_time = time::win32_filetime_to_unix_epoch(find_data.ftLastWriteTime.dwLowDateTime,
find_data.ftLastWriteTime.dwHighDateTime);
access_time = time::win32_filetime_to_unix_epoch(find_data.ftLastAccessTime.dwLowDateTime,
find_data.ftLastAccessTime.dwHighDateTime);
}
return file_info_t(file_path, modify_time, access_time, size, is_dir);
}
#else
struct stat file_stat;
const bool stat_ok = (stat(path.c_str(), &file_stat) == 0);
if (stat_ok) {
time::seconds_t modify_time = 0;
time::seconds_t access_time = 0;
int64_t size = 0;
const bool is_dir = S_ISDIR(file_stat.st_mode);
const bool is_file = S_ISREG(file_stat.st_mode);
if (is_file) {
size = static_cast<int64_t>(file_stat.st_size);
#ifdef __APPLE__
modify_time = static_cast<time::seconds_t>(file_stat.st_mtimespec.tv_sec);
access_time = static_cast<time::seconds_t>(file_stat.st_atimespec.tv_sec);
#else
modify_time = static_cast<time::seconds_t>(file_stat.st_mtim.tv_sec);
access_time = static_cast<time::seconds_t>(file_stat.st_atim.tv_sec);
#endif
}
return file_info_t(path, modify_time, access_time, size, is_dir);
}
#endif
throw std::runtime_error("Unable to get file information.");
}
std::string human_readable_size(const int64_t byte_size) {
static const char* SUFFIX[6] = {"bytes", "KiB", "MiB", "GiB", "TiB", "PiB"};
static const int MAX_SUFFIX_IDX = (sizeof(SUFFIX) / sizeof(SUFFIX[0])) - 1;
double scaled_size = static_cast<double>(byte_size);
int suffix_idx = 0;
for (; scaled_size >= 1024.0 && suffix_idx < MAX_SUFFIX_IDX; ++suffix_idx) {
scaled_size /= 1024.0;
}
char buf[20];
if (suffix_idx >= 1) {
std::snprintf(buf, sizeof(buf), "%.1f %s", scaled_size, SUFFIX[suffix_idx]);
} else {
std::snprintf(buf, sizeof(buf), "%d %s", static_cast<int>(byte_size), SUFFIX[suffix_idx]);
}
return std::string(buf);
}
std::vector<file_info_t> walk_directory(const std::string& path) {
std::vector<file_info_t> files;
#ifdef _WIN32
const auto search_str = utf8_to_ucs2(append_path(path, "*"));
WIN32_FIND_DATAW find_data;
auto* find_handle = FindFirstFileW(search_str.c_str(), &find_data);
if (find_handle == INVALID_HANDLE_VALUE) {
throw std::runtime_error("Unable to walk the directory.");
}
do {
const auto name = ucs2_to_utf8(std::wstring(&find_data.cFileName[0]));
if ((name != ".") && (name != "..")) {
const auto file_path = append_path(path, name);
time::seconds_t modify_time = 0;
time::seconds_t access_time = 0;
int64_t size = 0;
bool is_dir = false;
if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) {
auto subdir_files = walk_directory(file_path);
for (const auto& entry : subdir_files) {
files.emplace_back(entry);
size += entry.size();
modify_time = std::max(modify_time, entry.modify_time());
access_time = std::max(access_time, entry.access_time());
}
is_dir = true;
} else {
size = two_dwords_to_int64(find_data.nFileSizeLow, find_data.nFileSizeHigh);
modify_time = time::win32_filetime_to_unix_epoch(find_data.ftLastWriteTime.dwLowDateTime,
find_data.ftLastWriteTime.dwHighDateTime);
access_time = time::win32_filetime_to_unix_epoch(find_data.ftLastAccessTime.dwLowDateTime,