https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99290

            Bug ID: 99290
           Summary: std::filesystem::copy does not always report errors
                    for recursion
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fab...@ritter-vogt.de
  Target Milestone: ---

For the case where a directory is copied to another directory with either
copy_options::recursive or copy_options::none, fs_copy.cc has this in
std::filesystem::copy:

      for (const directory_entry& x : directory_iterator(from))
        copy(x.path(), to/x.path().filename(), options, ec);

This does not check the value of "ec" after each call to copy. As successful
invocations of copy clear the error, only the last call to copy influences the
error code. As the order of entries returned by directory_iterator is
unspecified, it's also unspecified whether an error is reported if recursive
copying fails.

Test program:

#include <filesystem>

int main(int argc, char *argv[])
{
        std::filesystem::copy(std::filesystem::path{argv[1]},
                              std::filesystem::path{argv[2]},
                              std::filesystem::copy_options::none);
}

Compile and run like this:

> g++ fstest.cpp -std=c++17 -o fstest # Compile
> mkdir source dest
> mkdir source/emptydir dest/emptydir # Empty directories, noop for fs::copy
> touch source/file; echo "dst" > dest/file # Conflicting files to make 
> fs::copy fail
> ./fstest source dest # Doesn't fail, because the empty directories overwrote 
> the error code
> rmdir source/emptydir dest/emptydir # Drop the empty directories
> ./fstest source dest # Now it fails
terminate called after throwing an instance of
'std::filesystem::__cxx11::filesystem_error'
  what():  filesystem error: cannot copy: File exists [source] [dest]

Whether you see the same effect is highly dependent on your (file) system.

Reply via email to