I am using clang++, installed with MSYS2 on Windows 11. Checking the version of clang++ gives the below.
~ clang++ --version
clang version 19.1.4
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin
The code:
#include <filesystem>
int main(){
std::filesystem::path p = "C:/Users/Jaden/Documents/hi.txt";
std::filesystem::path p2 = "C:/Users/Jaden";
std::filesystem::copy(
p, p2, std::filesystem::copy_options::overwrite_existing);
}
Produces the error:
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: File exists [C:/Users/Jaden/Documents/hi.txt] [C:/Users/Jaden]
If I run std::filesystem::exists
beforehand, it returns false. The error persists no matter if I use or do not use and copy_option
flags.
The code was simply compiled with clang++ .\test.cpp
.
Edit: Similarly when I compile using g++, the same issue persists.
I am using clang++, installed with MSYS2 on Windows 11. Checking the version of clang++ gives the below.
~ clang++ --version
clang version 19.1.4
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/mingw64/bin
The code:
#include <filesystem>
int main(){
std::filesystem::path p = "C:/Users/Jaden/Documents/hi.txt";
std::filesystem::path p2 = "C:/Users/Jaden";
std::filesystem::copy(
p, p2, std::filesystem::copy_options::overwrite_existing);
}
Produces the error:
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
what(): filesystem error: cannot copy: File exists [C:/Users/Jaden/Documents/hi.txt] [C:/Users/Jaden]
If I run std::filesystem::exists
beforehand, it returns false. The error persists no matter if I use or do not use and copy_option
flags.
The code was simply compiled with clang++ .\test.cpp
.
Edit: Similarly when I compile using g++, the same issue persists.
Share Improve this question edited Jan 29 at 20:17 JadenJin asked Jan 29 at 20:07 JadenJinJadenJin 931 silver badge4 bronze badges 5 |1 Answer
Reset to default 5After some testing, this is a bug in libstdc++, the GCC's C++ standard library. It's already fixed in the newer versions.
This happens any time you do std::filesystem::copy("file", "directory")
, regardless of overwrite_existing
.
I've reported it here: https://gcc.gnu./bugzilla/show_bug.cgi?id=118699, and was told that it's fixed in 15.x, 14.3.0, and 13.4.0, while MSYS2 still sits at 14.2.
hi.txt
to a subdirectory ofDocuments
? Anything change if you use MSYS2-style paths (/c/Users/Jaden/Documents/hi.txt
)? – genpfault Commented Jan 29 at 20:20"C:/Users/Jaden/hi.txt"
behaves as expected. – user4581301 Commented Jan 29 at 20:26copy_file(from, to/from.filename(), options)
(creates a copy of from as a file in the directory to) which reads to me as this should work, but in the modern Windows world of groovy virtual folders representing other folders (sometimes in cloud storage) is the user folder REALLY a folder? – user4581301 Commented Jan 29 at 20:43File exists [D:/test/a/hi.txt] [D:/test/b]
– user4581301 Commented Jan 29 at 20:46