When I use filesystem::copy_file() in Mingw, I cannot overwrite it even if I specify overwrite_existing.

Asked 2 years ago, Updated 2 years ago, 77 views

Examples include the following code:

#include<filesystem>
int main() {
    std::filesystem::copy_file("."/t.txt", "./test/t.txt", std::filesystem::copy_options:overwrite_existing);
}

./t.txt and ./test are both present, and only ./test/t.txt is not present.
The first time was successful and the second time an exception was thrown

terminate called after browsing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what(): filesystem error:cannot copy file:File exists [./t.txt] [./test/t.txt]

Is it a bug on the library side?
Or is it due to my own misrecognition (or mistake)?

Also, by the way,
Verifying that msvc(vs2019) and gcc(WSL(ubuntu18.04) do not throw exceptions and overwrite them

[Environment]
·OS: Windows 10
·Compilation options: Both are "-std=c++17" only
·Compiler
 g++(msys2:mingw-w64-x86_64-toolchain)

Using built-in specifics.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../gcc-9.3.0/configure --prefix=/mingw64 --with-local-prefix=/mingw64/local --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --with-native-system-header-dir=/mingw64/x86_64-w64-mingw32/include --libexecdir=/mingw64/lib --enable-bootstrap --with-arch=x86-64 --with-tune=generic --enable-languages=c,lto,c++,fortran,ada,objc,obj-c++ --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-filesystem-ts=yes --enable-libstdcxx-time=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-error --disable-symvers --enable-plugin --with-libiconv --with-system-zlib --with-gmp=/mingw64 --with-mpfr=/mingw64 --with-mpc=/mingw64 --with-isl=/mw64 --with-mww, with-mwYSys --with-by-by-by-by-mwS=1kg
Thread model: posix
gcc version 9.3.0 (Rev1, Build by MSYS2 project)

clang++(msys2:mingw-w64-x86_64-clang)

clang version 9.0.1 (https://github.com/msys2/MINGW-packages.git 5e3b8820ed9f04221affee4197e458aca2612e87)
Target: x86_64-w64-w64-windows-gnu
Thread model: posix
InstalledDir: C:\msys64\mingw64\bin

c++ mingw

2022-09-30 21:47

1 Answers

How does the file "./t.txt" protect?

If "./t.txt" is a read-only (not writable) file, the copied "./test/t.txt" will also be read-only.

If you create a read-only "./test/t.txt" in the first run and try to write (copy) to a read-only "./test/t.txt" in the second run, you will get a question-like error.
Check the permissions of the file.

If you change the permission of "./t.txt" to writable, the same error will not occur.


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.