1

I'm working on an app that writes/copies files and thought of updating the code to C++17's filesystem.

I'm on Windows 11 using g++ v12.2 with MSYS2 MinGW 64-bit.

Below is a summary of my app's behavior.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <filesystem>
namespace fs = std::filesystem;
 
int main() {
    fs::create_directories("sandbox");
    
    std::ofstream ofs;
    ofs.open ("sandbox/file1.txt", std::ofstream::out | std::ofstream::app);
        ofs << "hello";
    ofs.close();
    
    std::cout << "test copy file" << std::endl;
    fs::copy("sandbox/file1.txt", "sandbox/file2.txt", fs::copy_options::overwrite_existing); // copy file
    
    static_cast<void>(std::system("tree"));
    
    //sometime later, file is updated
    ofs.open ("sandbox/file1.txt", std::ofstream::out | std::ofstream::app);
        ofs << "world";
    ofs.close();
    
    //sometime later, need to update file2 via copy
    std::cout << "test copy file again" << std::endl;
    fs::copy("sandbox/file1.txt", "sandbox/file2.txt", fs::copy_options::overwrite_existing); // copy file

    fs::remove_all("sandbox");
}

Above code was compiled with:

g++ -c -std=gnu++17 testcopy.cpp -o testcopy.o
g++ ./testcopy.o  -o testcopy.exe

Then, you'll get this:

./testcopy.exe
test copy file
Folder PATH listing for volume Acer
Volume serial number is 38B3-48F3
C:.
└───sandbox
test copy file again
terminate called after throwing an instance of 'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot copy: File exists [sandbox/file1.txt] [sandbox/file2.txt]

I wonder if I have to release, free, or close file2.txt after first copy but I don't know how.

Or, possibly, copy_options is not supported by GCC on Windows, to which I'm okay defer porting my code to C++17.

I was expecting copy_options::overwrite_existing will handle the File exists problem. I've also used copy_options::update_existing to no avail.

  • Just for completeness, there's no difference when using [`copy_file`](https://en.cppreference.com/w/cpp/filesystem/copy_file) instead of [`copy`](https://en.cppreference.com/w/cpp/filesystem/copy)? – Some programmer dude Jun 25 '23 at 20:53
  • Are you running this exact program when you get the exception? It works fine for me on Windows 11. – Ted Lyngmo Jun 25 '23 at 21:02
  • 1
    File cannot be overwritten , maybe? Permissions do noy allow itor there is a folder with same name (kryptonite of many programs and even of some viruses) – Swift - Friday Pie Jun 25 '23 at 21:05
  • @TedLyngmo, I'm running exact code on Win11 (build 22621.1848). may I know your GCC version? – Mj Mendoza IV Jun 25 '23 at 21:23
  • 1
    @Someprogrammerdude, I updated to `copy_file` and still got the same error. – Mj Mendoza IV Jun 25 '23 at 21:24
  • 1
    Looks like it slipped through the net: https://sourceforge.net/p/mingw-w64/bugs/852/ – Paul Sanders Jun 25 '23 at 21:28
  • What does `fs::equivalent("sandbox/file1.txt", "sandbox/file2.txt")` just before attempting the failing copy say? Also `std::filesystem::is_regular_file` for both is true? – user17732522 Jun 25 '23 at 21:35
  • You should open an issue on GitHub msys2 is maintained there instead of sourceforge. – Fabian Keßler Jun 26 '23 at 00:34
  • ***may I know your GCC version?*** `gcc --version` is the command regardless of the OS. My gcc in msys2 returns this on the first line: ***gcc.exe (Rev6, Built by MSYS2 project) 13.1.0*** and a RockyLinux in a guest lxc container on a linux host returns this: ***gcc (GCC) 11.3.1 20221121 (Red Hat 11.3.1-4)*** – drescherjm Jun 26 '23 at 01:04

0 Answers0