@@ -32,8 +32,15 @@ namespace Filesystem = std::filesystem;
3232// for non-Windows file loop fallback
3333#include < sys/types.h> // for off_t, ssize_t
3434#include < sys/stat.h>
35- #include < unistd.h> // read write
36- #include < fcntl.h> // open close
35+ #include < unistd.h> // for read, write
36+ #include < fcntl.h> // for open, close
37+
38+ // O_CLOEXEC is not defined on all systems. We're not spawning processes,
39+ // so it would only be meaningful possibly if we're invoked multithreaded.
40+ #if !defined(O_CLOEXEC)
41+ #define O_CLOEXEC 0
42+ #endif
43+
3744#endif
3845
3946#if defined(__APPLE__) && defined(__MACH__) && __has_include(<copyfile.h>)
@@ -81,14 +88,14 @@ bool fs_copy_file_range_or_loop(std::string_view source, std::string_view dest,
8188 bool useloop = fst == " debugfs" || fst == " procfs" || fst == " sysfs" || fst == " tracefs" ;
8289#endif
8390
84- int const rid = open (source.data (), O_RDONLY );
91+ int const rid = :: open (source.data (), O_RDONLY | O_CLOEXEC );
8592 if (rid == -1 )
8693 return false ;
8794
8895 // leave fstat here to avoid source file race condition
8996 struct stat stat;
9097 if (fstat (rid, &stat) == -1 ) {
91- close (rid);
98+ :: close (rid);
9299 return false ;
93100 }
94101
@@ -99,9 +106,9 @@ bool fs_copy_file_range_or_loop(std::string_view source, std::string_view dest,
99106 opt |= O_EXCL ;
100107
101108// https://linux.die.net/man/3/open
102- int const wid = open (dest.data (), opt, stat.st_mode );
109+ int const wid = :: open (dest.data (), opt, stat.st_mode );
103110 if (wid == -1 ) {
104- close (rid);
111+ :: close (rid);
105112 return false ;
106113 }
107114
0 commit comments