public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/99290] New: std::filesystem::copy does not always report errors for recursion
@ 2021-02-26 15:42 fabian@ritter-vogt.de
  2021-02-26 15:49 ` [Bug libstdc++/99290] " marxin at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: fabian@ritter-vogt.de @ 2021-02-26 15:42 UTC (permalink / raw)
  To: gcc-bugs

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: fabian@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.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-06-14 19:21 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-26 15:42 [Bug libstdc++/99290] New: std::filesystem::copy does not always report errors for recursion fabian@ritter-vogt.de
2021-02-26 15:49 ` [Bug libstdc++/99290] " marxin at gcc dot gnu.org
2022-04-28 12:33 ` cvs-commit at gcc dot gnu.org
2022-04-28 13:31 ` redi at gcc dot gnu.org
2022-04-28 17:04 ` cvs-commit at gcc dot gnu.org
2022-04-28 17:05 ` redi at gcc dot gnu.org
2022-06-14 19:20 ` cvs-commit at gcc dot gnu.org
2022-06-14 19:21 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).