-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathoperations.cpp
More file actions
5203 lines (4404 loc) · 175 KB
/
Copy pathoperations.cpp
File metadata and controls
5203 lines (4404 loc) · 175 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
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
// operations.cpp --------------------------------------------------------------------//
// Copyright 2002-2009, 2014 Beman Dawes
// Copyright 2001 Dietmar Kuehl
// Copyright 2018-2024 Andrey Semashev
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// See library home page at http://www.boost.org/libs/filesystem
//--------------------------------------------------------------------------------------//
#include "platform_config.hpp"
#include <boost/predef/os/bsd/open.h>
#include <boost/filesystem/config.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/file_status.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/directory.hpp>
#include <boost/system/error_code.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/core/bit.hpp>
#include <boost/cstdint.hpp>
#include <boost/assert.hpp>
#include <new> // std::bad_alloc, std::nothrow
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <cstddef>
#include <cstdlib> // for malloc, free
#include <cstring>
#include <cerrno>
#include <stdio.h> // for rename
// Default to POSIX under Emscripten
// If BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI is set, use WASI instead
#if defined(__wasm) && (!defined(__EMSCRIPTEN__) || defined(BOOST_FILESYSTEM_EMSCRIPTEN_USE_WASI))
#define BOOST_FILESYSTEM_USE_WASI
#endif
#ifdef BOOST_FILESYSTEM_POSIX_API
#include <sys/types.h>
#include <sys/stat.h>
#if defined(BOOST_FILESYSTEM_USE_WASI)
// WASI does not have statfs or statvfs.
#elif !defined(__APPLE__) && \
(!defined(__OpenBSD__) || BOOST_OS_BSD_OPEN >= BOOST_VERSION_NUMBER(4, 4, 0)) && \
!defined(__ANDROID__) && \
!defined(__VXWORKS__)
#include <sys/statvfs.h>
#define BOOST_STATVFS statvfs
#define BOOST_STATVFS_F_FRSIZE vfs.f_frsize
#else
#ifdef __OpenBSD__
#include <sys/param.h>
#elif defined(__ANDROID__)
#include <sys/vfs.h>
#endif
#if !defined(__VXWORKS__)
#include <sys/mount.h>
#endif
#define BOOST_STATVFS statfs
#define BOOST_STATVFS_F_FRSIZE static_cast< uintmax_t >(vfs.f_bsize)
#endif // BOOST_STATVFS definition
#include <unistd.h>
#include <fcntl.h>
#if !defined(BOOST_FILESYSTEM_HAS_POSIX_AT_APIS)
#include <utime.h>
#endif
#include <limits.h>
#if defined(linux) || defined(__linux) || defined(__linux__)
#include <sys/vfs.h>
#include <sys/utsname.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#if !defined(BOOST_FILESYSTEM_DISABLE_SENDFILE)
#include <sys/sendfile.h>
#define BOOST_FILESYSTEM_USE_SENDFILE
#endif // !defined(BOOST_FILESYSTEM_DISABLE_SENDFILE)
#if !defined(BOOST_FILESYSTEM_DISABLE_COPY_FILE_RANGE) && defined(__NR_copy_file_range)
#define BOOST_FILESYSTEM_USE_COPY_FILE_RANGE
#endif // !defined(BOOST_FILESYSTEM_DISABLE_COPY_FILE_RANGE) && defined(__NR_copy_file_range)
#if !defined(BOOST_FILESYSTEM_DISABLE_STATX) && (defined(BOOST_FILESYSTEM_HAS_STATX) || defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL))
#if !defined(BOOST_FILESYSTEM_HAS_STATX) && defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL)
#include <linux/stat.h>
#endif
#define BOOST_FILESYSTEM_USE_STATX
#endif // !defined(BOOST_FILESYSTEM_DISABLE_STATX) && (defined(BOOST_FILESYSTEM_HAS_STATX) || defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL))
#if defined(__has_include)
#if __has_include(<linux/magic.h>)
// This header was introduced in Linux kernel 2.6.19
#include <linux/magic.h>
#endif
#endif
// Some filesystem type magic constants are not defined in older kernel headers
#ifndef PROC_SUPER_MAGIC
#define PROC_SUPER_MAGIC 0x9fa0
#endif
#ifndef SYSFS_MAGIC
#define SYSFS_MAGIC 0x62656572
#endif
#ifndef TRACEFS_MAGIC
#define TRACEFS_MAGIC 0x74726163
#endif
#ifndef DEBUGFS_MAGIC
#define DEBUGFS_MAGIC 0x64626720
#endif
#endif // defined(linux) || defined(__linux) || defined(__linux__)
#include <boost/scope/unique_fd.hpp>
#if defined(POSIX_FADV_SEQUENTIAL) && (!defined(__ANDROID__) || __ANDROID_API__ >= 21)
#define BOOST_FILESYSTEM_HAS_POSIX_FADVISE
#endif
#if defined(BOOST_FILESYSTEM_HAS_STAT_ST_MTIM)
#define BOOST_FILESYSTEM_STAT_ST_MTIMENSEC st_mtim.tv_nsec
#elif defined(BOOST_FILESYSTEM_HAS_STAT_ST_MTIMESPEC)
#define BOOST_FILESYSTEM_STAT_ST_MTIMENSEC st_mtimespec.tv_nsec
#elif defined(BOOST_FILESYSTEM_HAS_STAT_ST_MTIMENSEC)
#define BOOST_FILESYSTEM_STAT_ST_MTIMENSEC st_mtimensec
#endif
#if defined(BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIM)
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIME st_birthtim.tv_sec
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIMENSEC st_birthtim.tv_nsec
#elif defined(BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMESPEC)
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIME st_birthtimespec.tv_sec
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIMENSEC st_birthtimespec.tv_nsec
#elif defined(BOOST_FILESYSTEM_HAS_STAT_ST_BIRTHTIMENSEC)
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIME st_birthtime
#define BOOST_FILESYSTEM_STAT_ST_BIRTHTIMENSEC st_birthtimensec
#endif
#include "posix_tools.hpp"
#else // BOOST_FILESYSTEM_WINDOWS_API
#include <boost/winapi/dll.hpp> // get_proc_address, GetModuleHandleW
#include <cwchar>
#include <io.h>
#include <windows.h>
#include <winnt.h>
#if defined(__BORLANDC__) || defined(__MWERKS__)
#if defined(BOOST_BORLANDC)
using std::time_t;
#endif
#include <utime.h>
#else
#include <sys/utime.h>
#endif
#include "windows_tools.hpp"
#endif // BOOST_FILESYSTEM_WINDOWS_API
#include "atomic_tools.hpp"
#include "error_handling.hpp"
#include "private_config.hpp"
#include <boost/filesystem/detail/header.hpp> // must be the last #include
namespace fs = boost::filesystem;
using boost::filesystem::path;
using boost::filesystem::filesystem_error;
using boost::filesystem::perms;
using boost::system::error_code;
using boost::system::system_category;
#if defined(BOOST_FILESYSTEM_POSIX_API)
// At least Mac OS X 10.6 and older doesn't support O_CLOEXEC
#ifndef O_CLOEXEC
#define O_CLOEXEC 0
#define BOOST_FILESYSTEM_NO_O_CLOEXEC
#endif
// At least Cygwin (cygwin1.dll 3.6.5, as of 2025-12-25) doesn't support AT_NO_AUTOMOUNT
#ifndef AT_NO_AUTOMOUNT
#define AT_NO_AUTOMOUNT 0
#endif
#if defined(_POSIX_SYNCHRONIZED_IO) && _POSIX_SYNCHRONIZED_IO > 0
#define BOOST_FILESYSTEM_HAS_FDATASYNC
#endif
#else // defined(BOOST_FILESYSTEM_POSIX_API)
#ifndef MAXIMUM_REPARSE_DATA_BUFFER_SIZE
#define MAXIMUM_REPARSE_DATA_BUFFER_SIZE (16 * 1024)
#endif
#ifndef FSCTL_GET_REPARSE_POINT
#define FSCTL_GET_REPARSE_POINT 0x900a8
#endif
#ifndef SYMLINK_FLAG_RELATIVE
#define SYMLINK_FLAG_RELATIVE 1
#endif
// Fallback for MinGW/Cygwin
#ifndef SYMBOLIC_LINK_FLAG_DIRECTORY
#define SYMBOLIC_LINK_FLAG_DIRECTORY 0x1
#endif
#ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
#endif
#endif // defined(BOOST_FILESYSTEM_POSIX_API)
// POSIX/Windows macros ----------------------------------------------------//
// Portions of the POSIX and Windows API's are very similar, except for name,
// order of arguments, and meaning of zero/non-zero returns. The macros below
// abstract away those differences. They follow Windows naming and order of
// arguments, and return true to indicate no error occurred. [POSIX naming,
// order of arguments, and meaning of return were followed initially, but
// found to be less clear and cause more coding errors.]
#if defined(BOOST_FILESYSTEM_POSIX_API)
#define BOOST_SET_CURRENT_DIRECTORY(P) (::chdir(P) == 0)
#define BOOST_MOVE_FILE(OLD, NEW) (::rename(OLD, NEW) == 0)
#define BOOST_RESIZE_FILE(P, SZ) (::truncate(P, SZ) == 0)
#else // BOOST_FILESYSTEM_WINDOWS_API
#define BOOST_SET_CURRENT_DIRECTORY(P) (::SetCurrentDirectoryW(P) != 0)
#define BOOST_MOVE_FILE(OLD, NEW) (::MoveFileExW(OLD, NEW, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED) != 0)
#define BOOST_RESIZE_FILE(P, SZ) (resize_file_impl(P, SZ) != 0)
#endif
namespace boost {
namespace filesystem {
namespace detail {
#if defined(linux) || defined(__linux) || defined(__linux__)
//! Initializes fill_random implementation pointer. Implemented in unique_path.cpp.
void init_fill_random_impl(unsigned int major_ver, unsigned int minor_ver, unsigned int patch_ver);
#endif // defined(linux) || defined(__linux) || defined(__linux__)
#if defined(BOOST_FILESYSTEM_WINDOWS_API)
//! Initializes directory iterator implementation. Implemented in directory.cpp.
void init_directory_iterator_impl() noexcept;
#endif // defined(BOOST_FILESYSTEM_WINDOWS_API)
namespace {
//--------------------------------------------------------------------------------------//
// //
// helpers (all operating systems) //
// //
//--------------------------------------------------------------------------------------//
// The number of retries remove_all should make if it detects that the directory it is about to enter has been replaced with a symlink or a regular file
BOOST_CONSTEXPR_OR_CONST unsigned int remove_all_directory_replaced_retry_count = 5u;
// Size of a small buffer for a path that can be placed on stack, in character code units
BOOST_CONSTEXPR_OR_CONST std::size_t small_path_size = 1024u;
#if defined(BOOST_FILESYSTEM_POSIX_API)
//--------------------------------------------------------------------------------------//
// //
// POSIX-specific helpers //
// //
//--------------------------------------------------------------------------------------//
// Absolute maximum path length, in character code units, that we're willing to accept from various system calls.
// This value is arbitrary, it is supposed to be a hard limit to avoid memory exhaustion
// in some of the algorithms below in case of some corrupted or maliciously broken filesystem.
// A few examples of path size limits:
// - Windows: 32767 UTF-16 code units or 260 bytes for legacy multibyte APIs.
// - Linux: 4096 bytes
// - IRIX, HP-UX, Mac OS, QNX, FreeBSD, OpenBSD: 1024 bytes
// - GNU/Hurd: no hard limit
BOOST_CONSTEXPR_OR_CONST std::size_t absolute_path_max = 32u * 1024u;
// Maximum number of resolved symlinks before we register a loop
BOOST_CONSTEXPR_OR_CONST unsigned int symloop_max =
#if defined(SYMLOOP_MAX)
SYMLOOP_MAX < 40 ? 40 : SYMLOOP_MAX
#else
40
#endif
;
inline bool not_found_error(int errval) noexcept
{
return errval == ENOENT || errval == ENOTDIR;
}
/*!
* Closes a file descriptor and returns the result, similar to close(2). Unlike close(2), guarantees that the file descriptor is closed even if EINTR error happens.
*
* Some systems don't close the file descriptor in case if the thread is interrupted by a signal and close(2) returns EINTR.
* Other (most) systems do close the file descriptor even when when close(2) returns EINTR, and attempting to close it
* again could close a different file descriptor that was opened by a different thread. This function hides this difference in behavior.
*
* Future POSIX standards will likely fix this by introducing posix_close (see https://www.austingroupbugs.net/view.php?id=529)
* and prohibiting returning EINTR from close(2), but we still have to support older systems where this new behavior is not available and close(2)
* behaves differently between systems.
*/
inline int close_fd(int fd)
{
#if defined(hpux) || defined(_hpux) || defined(__hpux)
int res;
while (true)
{
res = ::close(fd);
if (BOOST_UNLIKELY(res < 0))
{
int err = errno;
if (err == EINTR)
continue;
}
break;
}
return res;
#else
return ::close(fd);
#endif
}
#if defined(BOOST_FILESYSTEM_HAS_STATX)
//! A wrapper for statx libc function. Disable MSAN since at least on clang 10 it doesn't
//! know which fields of struct statx are initialized by the syscall and misdetects errors.
BOOST_FILESYSTEM_NO_SANITIZE_MEMORY
BOOST_FORCEINLINE int invoke_statx(int dirfd, const char* path, int flags, unsigned int mask, struct ::statx* stx)
{
return ::statx(dirfd, path, flags, mask, stx);
}
#elif defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL)
//! statx emulation through fstatat
int statx_fstatat(int dirfd, const char* path, int flags, unsigned int mask, struct ::statx* stx)
{
struct ::stat st;
flags &= AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_SYMLINK_NOFOLLOW;
int res = ::fstatat(dirfd, path, &st, flags);
if (BOOST_LIKELY(res == 0))
{
std::memset(stx, 0, sizeof(*stx));
stx->stx_mask = STATX_BASIC_STATS;
stx->stx_blksize = st.st_blksize;
stx->stx_nlink = st.st_nlink;
stx->stx_uid = st.st_uid;
stx->stx_gid = st.st_gid;
stx->stx_mode = st.st_mode;
stx->stx_ino = st.st_ino;
stx->stx_size = st.st_size;
stx->stx_blocks = st.st_blocks;
stx->stx_atime.tv_sec = st.st_atim.tv_sec;
stx->stx_atime.tv_nsec = st.st_atim.tv_nsec;
stx->stx_ctime.tv_sec = st.st_ctim.tv_sec;
stx->stx_ctime.tv_nsec = st.st_ctim.tv_nsec;
stx->stx_mtime.tv_sec = st.st_mtim.tv_sec;
stx->stx_mtime.tv_nsec = st.st_mtim.tv_nsec;
stx->stx_rdev_major = major(st.st_rdev);
stx->stx_rdev_minor = minor(st.st_rdev);
stx->stx_dev_major = major(st.st_dev);
stx->stx_dev_minor = minor(st.st_dev);
}
return res;
}
typedef int statx_t(int dirfd, const char* path, int flags, unsigned int mask, struct ::statx* stx);
//! Pointer to the actual implementation of the statx implementation
statx_t* statx_ptr = &statx_fstatat;
inline int invoke_statx(int dirfd, const char* path, int flags, unsigned int mask, struct ::statx* stx) noexcept
{
return filesystem::detail::atomic_load_relaxed(statx_ptr)(dirfd, path, flags, mask, stx);
}
//! A wrapper for the statx syscall. Disable MSAN since at least on clang 10 it doesn't
//! know which fields of struct statx are initialized by the syscall and misdetects errors.
BOOST_FILESYSTEM_NO_SANITIZE_MEMORY
int statx_syscall(int dirfd, const char* path, int flags, unsigned int mask, struct ::statx* stx)
{
int res = ::syscall(__NR_statx, dirfd, path, flags, mask, stx);
if (res < 0)
{
const int err = errno;
if (BOOST_UNLIKELY(err == ENOSYS))
{
filesystem::detail::atomic_store_relaxed(statx_ptr, &statx_fstatat);
return statx_fstatat(dirfd, path, flags, mask, stx);
}
}
return res;
}
#endif // defined(BOOST_FILESYSTEM_HAS_STATX)
#if defined(linux) || defined(__linux) || defined(__linux__)
//! Initializes statx implementation pointer
inline void init_statx_impl(unsigned int major_ver, unsigned int minor_ver, unsigned int patch_ver)
{
#if !defined(BOOST_FILESYSTEM_HAS_STATX) && defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL)
statx_t* stx = &statx_fstatat;
if (major_ver > 4u || (major_ver == 4u && minor_ver >= 11u))
stx = &statx_syscall;
filesystem::detail::atomic_store_relaxed(statx_ptr, stx);
#endif // !defined(BOOST_FILESYSTEM_HAS_STATX) && defined(BOOST_FILESYSTEM_HAS_STATX_SYSCALL)
}
#endif // defined(linux) || defined(__linux) || defined(__linux__)
#if defined(BOOST_FILESYSTEM_USE_STATX)
//! Returns \c true if the two \c statx structures refer to the same file
inline bool equivalent_stat(struct ::statx const& s1, struct ::statx const& s2) noexcept
{
return s1.stx_dev_major == s2.stx_dev_major && s1.stx_dev_minor == s2.stx_dev_minor && s1.stx_ino == s2.stx_ino;
}
//! Returns file type/access mode from \c statx structure
inline mode_t get_mode(struct ::statx const& st) noexcept
{
return st.stx_mode;
}
//! Returns file size from \c statx structure
inline uintmax_t get_size(struct ::statx const& st) noexcept
{
return st.stx_size;
}
//! Returns optimal block size from \c statx structure
inline std::size_t get_blksize(struct ::statx const& st) noexcept
{
return st.stx_blksize;
}
#else // defined(BOOST_FILESYSTEM_USE_STATX)
//! Returns \c true if the two \c stat structures refer to the same file
inline bool equivalent_stat(struct ::stat const& s1, struct ::stat const& s2) noexcept
{
// According to the POSIX stat specs, "The st_ino and st_dev fields
// taken together uniquely identify the file within the system."
return s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino;
}
//! Returns file type/access mode from \c stat structure
inline mode_t get_mode(struct ::stat const& st) noexcept
{
return st.st_mode;
}
//! Returns file size from \c stat structure
inline uintmax_t get_size(struct ::stat const& st) noexcept
{
return st.st_size;
}
//! Returns optimal block size from \c stat structure
inline std::size_t get_blksize(struct ::stat const& st) noexcept
{
#if defined(BOOST_FILESYSTEM_HAS_STAT_ST_BLKSIZE)
return st.st_blksize;
#else
return 4096u; // a suitable default used on most modern SSDs/HDDs
#endif
}
#endif // defined(BOOST_FILESYSTEM_USE_STATX)
} // namespace
//! status() implementation
file_status status_impl
(
path const& p,
system::error_code* ec
#if defined(BOOST_FILESYSTEM_HAS_POSIX_AT_APIS) || defined(BOOST_FILESYSTEM_USE_STATX)
, int basedir_fd
#endif
)
{
#if defined(BOOST_FILESYSTEM_USE_STATX)
struct ::statx path_stat;
int err = invoke_statx(basedir_fd, p.c_str(), AT_NO_AUTOMOUNT, STATX_TYPE | STATX_MODE, &path_stat);
#elif defined(BOOST_FILESYSTEM_HAS_POSIX_AT_APIS)
struct ::stat path_stat;
int err = ::fstatat(basedir_fd, p.c_str(), &path_stat, AT_NO_AUTOMOUNT);
#else
struct ::stat path_stat;
int err = ::stat(p.c_str(), &path_stat);
#endif
if (err != 0)
{
err = errno;
if (ec) // always report errno, even though some
ec->assign(err, system::system_category()); // errno values are not status_errors
if (not_found_error(err))
return fs::file_status(fs::file_not_found, fs::no_perms);
if (!ec)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::status", p, system::error_code(err, system::system_category())));
return fs::file_status(fs::status_error);
}
#if defined(BOOST_FILESYSTEM_USE_STATX)
if (BOOST_UNLIKELY((path_stat.stx_mask & (STATX_TYPE | STATX_MODE)) != (STATX_TYPE | STATX_MODE)))
{
emit_error(BOOST_ERROR_NOT_SUPPORTED, p, ec, "boost::filesystem::status");
return fs::file_status(fs::status_error);
}
#endif
const mode_t mode = get_mode(path_stat);
if (S_ISDIR(mode))
return fs::file_status(fs::directory_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISREG(mode))
return fs::file_status(fs::regular_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISBLK(mode))
return fs::file_status(fs::block_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISCHR(mode))
return fs::file_status(fs::character_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISFIFO(mode))
return fs::file_status(fs::fifo_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISSOCK(mode))
return fs::file_status(fs::socket_file, static_cast< perms >(mode) & fs::perms_mask);
return fs::file_status(fs::type_unknown);
}
//! symlink_status() implementation
file_status symlink_status_impl
(
path const& p,
system::error_code* ec
#if defined(BOOST_FILESYSTEM_HAS_POSIX_AT_APIS) || defined(BOOST_FILESYSTEM_USE_STATX)
, int basedir_fd
#endif
)
{
#if defined(BOOST_FILESYSTEM_USE_STATX)
struct ::statx path_stat;
int err = invoke_statx(basedir_fd, p.c_str(), AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT, STATX_TYPE | STATX_MODE, &path_stat);
#elif defined(BOOST_FILESYSTEM_HAS_POSIX_AT_APIS)
struct ::stat path_stat;
int err = ::fstatat(basedir_fd, p.c_str(), &path_stat, AT_SYMLINK_NOFOLLOW | AT_NO_AUTOMOUNT);
#else
struct ::stat path_stat;
int err = ::lstat(p.c_str(), &path_stat);
#endif
if (err != 0)
{
err = errno;
if (ec) // always report errno, even though some
ec->assign(err, system::system_category()); // errno values are not status_errors
if (not_found_error(err)) // these are not errors
return fs::file_status(fs::file_not_found, fs::no_perms);
if (!ec)
BOOST_FILESYSTEM_THROW(filesystem_error("boost::filesystem::symlink_status", p, system::error_code(err, system::system_category())));
return fs::file_status(fs::status_error);
}
#if defined(BOOST_FILESYSTEM_USE_STATX)
if (BOOST_UNLIKELY((path_stat.stx_mask & (STATX_TYPE | STATX_MODE)) != (STATX_TYPE | STATX_MODE)))
{
emit_error(BOOST_ERROR_NOT_SUPPORTED, p, ec, "boost::filesystem::symlink_status");
return fs::file_status(fs::status_error);
}
#endif
const mode_t mode = get_mode(path_stat);
if (S_ISREG(mode))
return fs::file_status(fs::regular_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISDIR(mode))
return fs::file_status(fs::directory_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISLNK(mode))
return fs::file_status(fs::symlink_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISBLK(mode))
return fs::file_status(fs::block_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISCHR(mode))
return fs::file_status(fs::character_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISFIFO(mode))
return fs::file_status(fs::fifo_file, static_cast< perms >(mode) & fs::perms_mask);
if (S_ISSOCK(mode))
return fs::file_status(fs::socket_file, static_cast< perms >(mode) & fs::perms_mask);
return fs::file_status(fs::type_unknown);
}
namespace {
//! Flushes buffered data and attributes written to the file to permanent storage
inline int full_sync(int fd)
{
while (true)
{
#if defined(__APPLE__) && defined(__MACH__) && defined(F_FULLFSYNC)
// Mac OS does not flush data to physical storage with fsync()
int err = ::fcntl(fd, F_FULLFSYNC);
#else
int err = ::fsync(fd);
#endif
if (BOOST_UNLIKELY(err < 0))
{
err = errno;
// POSIX says fsync can return EINTR (https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html).
// fcntl(F_FULLFSYNC) isn't documented to return EINTR, but it doesn't hurt to check.
if (err == EINTR)
continue;
return err;
}
break;
}
return 0;
}
//! Flushes buffered data written to the file to permanent storage
inline int data_sync(int fd)
{
#if defined(BOOST_FILESYSTEM_HAS_FDATASYNC) && !(defined(__APPLE__) && defined(__MACH__) && defined(F_FULLFSYNC))
while (true)
{
int err = ::fdatasync(fd);
if (BOOST_UNLIKELY(err != 0))
{
err = errno;
// POSIX says fsync can return EINTR (https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html).
// It doesn't say so for fdatasync, but it is reasonable to expect it as well.
if (err == EINTR)
continue;
return err;
}
break;
}
return 0;
#else
return full_sync(fd);
#endif
}
//! Hints the filesystem to opportunistically preallocate storage for a file
inline int preallocate_storage(int file, uintmax_t size)
{
#if defined(BOOST_FILESYSTEM_HAS_FALLOCATE)
if (BOOST_LIKELY(size > 0 && size <= static_cast< uintmax_t >((std::numeric_limits< off_t >::max)())))
{
while (true)
{
// Note: We intentionally use fallocate rather than posix_fallocate to avoid
// invoking glibc emulation that writes zeros to the end of the file.
// We want this call to act like a hint to the filesystem and an early
// check for the free storage space. We don't want to write zeros only
// to later overwrite them with the actual data.
int err = fallocate(file, FALLOC_FL_KEEP_SIZE, 0, static_cast< off_t >(size));
if (BOOST_UNLIKELY(err != 0))
{
err = errno;
// Ignore the error if the operation is not supported by the kernel or filesystem
if (err == EOPNOTSUPP || err == ENOSYS)
break;
if (err == EINTR)
continue;
return err;
}
break;
}
}
#endif
return 0;
}
//! copy_file implementation wrapper that preallocates storage for the target file
template< typename CopyFileData >
struct copy_file_data_preallocate
{
//! copy_file implementation wrapper that preallocates storage for the target file before invoking the underlying copy implementation
static int impl(int infile, int outfile, uintmax_t size, std::size_t blksize)
{
int err = preallocate_storage(outfile, size);
if (BOOST_UNLIKELY(err != 0))
return err;
return CopyFileData::impl(infile, outfile, size, blksize);
}
};
// Min and max buffer sizes are selected to minimize the overhead from system calls.
// The values are picked based on coreutils cp(1) benchmarking data described here:
// https://github.com/coreutils/coreutils/blob/d1b0257077c0b0f0ee25087efd46270345d1dd1f/src/ioblksize.h#L23-L72
BOOST_CONSTEXPR_OR_CONST uint_least32_t min_read_write_buf_size = 8u * 1024u;
BOOST_CONSTEXPR_OR_CONST uint_least32_t max_read_write_buf_size = 256u * 1024u;
//! copy_file read/write loop implementation
int copy_file_data_read_write_impl(int infile, int outfile, char* buf, std::size_t buf_size)
{
#if defined(BOOST_FILESYSTEM_HAS_POSIX_FADVISE)
::posix_fadvise(infile, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
// Don't use file size to limit the amount of data to copy since some filesystems, like procfs or sysfs,
// provide files with generated content and indicate that their size is zero or 4096. Just copy as much data
// as we can read from the input file.
while (true)
{
ssize_t sz_read = ::read(infile, buf, buf_size);
if (sz_read == 0)
break;
if (BOOST_UNLIKELY(sz_read < 0))
{
int err = errno;
if (err == EINTR)
continue;
return err;
}
// Allow for partial writes - see Advanced Unix Programming (2nd Ed.),
// Marc Rochkind, Addison-Wesley, 2004, page 94
for (ssize_t sz_wrote = 0; sz_wrote < sz_read;)
{
ssize_t sz = ::write(outfile, buf + sz_wrote, static_cast< std::size_t >(sz_read - sz_wrote));
if (BOOST_UNLIKELY(sz < 0))
{
int err = errno;
if (err == EINTR)
continue;
return err;
}
sz_wrote += sz;
}
}
return 0;
}
//! copy_file implementation that uses read/write loop (fallback using a stack buffer)
int copy_file_data_read_write_stack_buf(int infile, int outfile)
{
char stack_buf[min_read_write_buf_size];
return copy_file_data_read_write_impl(infile, outfile, stack_buf, sizeof(stack_buf));
}
//! copy_file implementation that uses read/write loop
int copy_file_data_read_write(int infile, int outfile, uintmax_t size, std::size_t blksize)
{
{
uintmax_t buf_sz = size;
// Prefer the buffer to be larger than the file size so that we don't have
// to perform an extra read if the file fits in the buffer exactly.
buf_sz += (buf_sz < ~static_cast< uintmax_t >(0u));
if (buf_sz < blksize)
buf_sz = blksize;
if (buf_sz < min_read_write_buf_size)
buf_sz = min_read_write_buf_size;
if (buf_sz > max_read_write_buf_size)
buf_sz = max_read_write_buf_size;
const std::size_t buf_size = static_cast< std::size_t >(boost::core::bit_ceil(static_cast< uint_least32_t >(buf_sz)));
std::unique_ptr< char[] > buf(new (std::nothrow) char[buf_size]);
if (BOOST_LIKELY(!!buf.get()))
return copy_file_data_read_write_impl(infile, outfile, buf.get(), buf_size);
}
return copy_file_data_read_write_stack_buf(infile, outfile);
}
typedef int copy_file_data_t(int infile, int outfile, uintmax_t size, std::size_t blksize);
//! Pointer to the actual implementation of copy_file_data
copy_file_data_t* copy_file_data = ©_file_data_read_write;
#if defined(BOOST_FILESYSTEM_USE_SENDFILE) || defined(BOOST_FILESYSTEM_USE_COPY_FILE_RANGE)
//! copy_file_data wrapper that tests if a read/write loop must be used for a given filesystem
template< typename CopyFileData >
int check_fs_type(int infile, int outfile, uintmax_t size, std::size_t blksize);
#endif // defined(BOOST_FILESYSTEM_USE_SENDFILE) || defined(BOOST_FILESYSTEM_USE_COPY_FILE_RANGE)
#if defined(BOOST_FILESYSTEM_USE_SENDFILE)
struct copy_file_data_sendfile
{
//! copy_file implementation that uses sendfile loop. Requires sendfile to support file descriptors.
static int impl(int infile, int outfile, uintmax_t size, std::size_t blksize)
{
// sendfile will not send more than this amount of data in one call
BOOST_CONSTEXPR_OR_CONST std::size_t max_batch_size = 0x7ffff000u;
uintmax_t offset = 0u;
while (offset < size)
{
uintmax_t size_left = size - offset;
std::size_t size_to_copy = max_batch_size;
if (size_left < static_cast< uintmax_t >(max_batch_size))
size_to_copy = static_cast< std::size_t >(size_left);
ssize_t sz = ::sendfile(outfile, infile, nullptr, size_to_copy);
if (BOOST_LIKELY(sz > 0))
{
offset += sz;
}
else if (sz < 0)
{
int err = errno;
if (err == EINTR)
continue;
if (offset == 0u)
{
// sendfile may fail with EINVAL if the underlying filesystem does not support it
if (err == EINVAL)
{
fallback_to_read_write:
return copy_file_data_read_write(infile, outfile, size, blksize);
}
if (err == ENOSYS)
{
filesystem::detail::atomic_store_relaxed(copy_file_data, ©_file_data_read_write);
goto fallback_to_read_write;
}
}
return err;
}
else
{
// EOF: the input file was truncated while copying was in progress
break;
}
}
return 0;
}
};
#endif // defined(BOOST_FILESYSTEM_USE_SENDFILE)
#if defined(BOOST_FILESYSTEM_USE_COPY_FILE_RANGE)
struct copy_file_data_copy_file_range
{
//! copy_file implementation that uses copy_file_range loop. Requires copy_file_range to support cross-filesystem copying.
static int impl(int infile, int outfile, uintmax_t size, std::size_t blksize)
{
// Although copy_file_range does not document any particular upper limit of one transfer, still use some upper bound to guarantee
// that size_t is not overflown in case if off_t is larger and the file size does not fit in size_t.
BOOST_CONSTEXPR_OR_CONST std::size_t max_batch_size = 0x7ffff000u;
uintmax_t offset = 0u;
while (offset < size)
{
uintmax_t size_left = size - offset;
std::size_t size_to_copy = max_batch_size;
if (size_left < static_cast< uintmax_t >(max_batch_size))
size_to_copy = static_cast< std::size_t >(size_left);
// Note: Use syscall directly to avoid depending on libc version. copy_file_range is added in glibc 2.27.
// uClibc-ng does not have copy_file_range as of the time of this writing (the latest uClibc-ng release is 1.0.33).
loff_t sz = ::syscall(__NR_copy_file_range, infile, (loff_t*)nullptr, outfile, (loff_t*)nullptr, size_to_copy, (unsigned int)0u);
if (BOOST_LIKELY(sz > 0))
{
offset += sz;
}
else if (sz < 0)
{
int err = errno;
if (err == EINTR)
continue;
if (offset == 0u)
{
// copy_file_range may fail with EINVAL if the underlying filesystem does not support it.
// In some RHEL/CentOS 7.7-7.8 kernel versions, copy_file_range on NFSv4 is also known to return EOPNOTSUPP
// if the remote server does not support COPY, despite that it is not a documented error code.
// See https://patchwork.kernel.org/project/linux-nfs/patch/[email protected]/
// and https://bugzilla.redhat.com/show_bug.cgi?id=1783554.
if (err == EINVAL || err == EOPNOTSUPP)
{
#if !defined(BOOST_FILESYSTEM_USE_SENDFILE)
fallback_to_read_write:
#endif
return copy_file_data_read_write(infile, outfile, size, blksize);
}
if (err == EXDEV)
{
#if defined(BOOST_FILESYSTEM_USE_SENDFILE)
fallback_to_sendfile:
return copy_file_data_sendfile::impl(infile, outfile, size, blksize);
#else
goto fallback_to_read_write;
#endif
}
if (err == ENOSYS)
{
#if defined(BOOST_FILESYSTEM_USE_SENDFILE)
filesystem::detail::atomic_store_relaxed(copy_file_data, &check_fs_type< copy_file_data_preallocate< copy_file_data_sendfile > >);
goto fallback_to_sendfile;
#else
filesystem::detail::atomic_store_relaxed(copy_file_data, ©_file_data_read_write);
goto fallback_to_read_write;
#endif
}
}
return err;
}
else
{
// EOF: the input file was truncated while copying was in progress
break;
}
}
return 0;
}
};
#endif // defined(BOOST_FILESYSTEM_USE_COPY_FILE_RANGE)
#if defined(BOOST_FILESYSTEM_USE_SENDFILE) || defined(BOOST_FILESYSTEM_USE_COPY_FILE_RANGE)
//! copy_file_data wrapper that tests if a read/write loop must be used for a given filesystem
template< typename CopyFileData >
int check_fs_type(int infile, int outfile, uintmax_t size, std::size_t blksize)
{
{
// Some filesystems have regular files with generated content. Such files have arbitrary size, including zero,
// but have actual content. Linux system calls sendfile or copy_file_range will not copy contents of such files,
// so we must use a read/write loop to handle them.
// https://lore.kernel.org/linux-fsdevel/[email protected]/T/
struct statfs sfs;
while (true)
{
int err = ::fstatfs(infile, &sfs);
if (BOOST_UNLIKELY(err < 0))
{
err = errno;
if (err == EINTR)
continue;
goto fallback_to_read_write;
}
break;
}
if (BOOST_UNLIKELY(sfs.f_type == PROC_SUPER_MAGIC ||
sfs.f_type == SYSFS_MAGIC ||
sfs.f_type == 0x63677270 || // CGROUP2_SUPER_MAGIC
sfs.f_type == 0x0027e0eb || // CGROUP_SUPER_MAGIC
sfs.f_type == 0x73636673 || // SECURITYFS_MAGIC
sfs.f_type == 0x62656570 || // CONFIGFS_MAGIC
sfs.f_type == TRACEFS_MAGIC ||
sfs.f_type == DEBUGFS_MAGIC))
{
fallback_to_read_write:
return copy_file_data_read_write(infile, outfile, size, blksize);
}
}
return CopyFileData::impl(infile, outfile, size, blksize);
}