41
41
// since Linux 4.5 and FreeBSD 13
42
42
#if defined(__linux__) || defined(__FreeBSD__)
43
43
# define _LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE
44
- #elif __has_include(<sys/sendfile.h>)
44
+ #endif
45
+ #if __has_include(<sys/sendfile.h>)
45
46
# include < sys/sendfile.h>
46
47
# define _LIBCPP_FILESYSTEM_USE_SENDFILE
47
48
#elif defined(__APPLE__) || __has_include(<copyfile.h>)
@@ -277,8 +278,11 @@ bool copy_file_impl_sendfile(FileDescriptor& read_fd, FileDescriptor& write_fd,
277
278
}
278
279
#endif
279
280
280
- #if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)
281
+ #if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE) || defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
282
+ // If we have copy_file_range or sendfile, try both in succession (if available).
283
+ // If both fail, fall back to using fstream.
281
284
bool copy_file_impl (FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
285
+ # if defined(_LIBCPP_FILESYSTEM_USE_COPY_FILE_RANGE)
282
286
if (copy_file_impl_copy_file_range (read_fd, write_fd, ec)) {
283
287
return true ;
284
288
}
@@ -299,10 +303,9 @@ bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_cod
299
303
return false ;
300
304
}
301
305
ec.clear ();
302
- return copy_file_impl_fstream (read_fd, write_fd, ec);
303
- }
304
- #elif defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
305
- bool copy_file_impl (FileDescriptor& read_fd, FileDescriptor& write_fd, error_code& ec) {
306
+ # endif
307
+
308
+ # if defined(_LIBCPP_FILESYSTEM_USE_SENDFILE)
306
309
if (copy_file_impl_sendfile (read_fd, write_fd, ec)) {
307
310
return true ;
308
311
}
@@ -311,6 +314,8 @@ bool copy_file_impl(FileDescriptor& read_fd, FileDescriptor& write_fd, error_cod
311
314
return false ;
312
315
}
313
316
ec.clear ();
317
+ # endif
318
+
314
319
return copy_file_impl_fstream (read_fd, write_fd, ec);
315
320
}
316
321
#elif defined(_LIBCPP_FILESYSTEM_USE_COPYFILE)
0 commit comments