0

Copying files to a directory to another directory using this code

auto const copyOption = std::filesystem::copy_options::recursive | std::filesystem::copy_options::skip_symlinks;
std::filesystem::copy("/mnt/iso", "/mnt/usb", copyOption);

Copying big files can take a long time. So how to check when copy is ended ?

kramer
  • 177
  • 1
  • 4
  • 15
  • 9
    Isn't the function blocking? It should return only after it finishes copying. – HolyBlackCat Sep 07 '21 at 17:56
  • 1
    While I haven't checked the C++ specification, nothing in e.g. [this `copy` reference](https://en.cppreference.com/w/cpp/filesystem/copy) mentions that it's synchronous or asynchronous, so I would guess that it defaults to being synchronous (i.e. it's blocking and returns only when finished). Have you *tried* using it, and timing and checking when it returns? – Some programmer dude Sep 07 '21 at 17:59
  • but ```std::filesystem::copy``` has no return value, so i don't know how to control it – kramer Sep 07 '21 at 18:27
  • Toy *don't* "control" it. You call it, it copies the file (or directory), and it ends when everything is finished. – Some programmer dude Sep 08 '21 at 15:49
  • *Toy -> You (I mean) – Some programmer dude Sep 08 '21 at 16:09

1 Answers1

3

How to check if std::filesystem::copy is ended?

The call to a function ends by either (1) returning, by (2) throwing or by (3) terminating the program.

If the execution of the program proceeds to the next statement (or next sibling expression), then you know that the function call has ended by (1) returning. If the execution proceeds to unwind and eventually enters a catch block, then you know that the function has ended by (2) throwing. If the program no longer runs, then you know that the function has (3) terminated the program (std::filesystem::copy won't do this one directly, although it can happen if it throws without being caught). If none of those have happened, then you know that the function call hasn't ended yet.

For example try to unmount an usbkey just after copy finished will take one or two minutes longer if you have big files

There is no standard way in C++ to verify when data has physically been written onto a device.

You've tagged [ubuntu], so you may be interested in fsync function in the POSIX standard. You cannot use it with conjunction with std::filesystem::copy; you'll need to use the other POSIX file manipulation functions instead. fsync should guarantee that any writes have been passed on to the device by the kernel. From then on, you rely on the hardware.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • But because copy function is synchronous , even if the program proceeds to the next statement, copy isn't ended . For example try to unmount an usbkey just after copy finished will take one or two minutes longer if you have big files – kramer Sep 07 '21 at 19:23
  • 2
    If the `copy()` is just requesting the OS to start a copy, and the actual copy is done in the background, then there is no way you can query its status with `std::filesystem` functions. Look for a platform-specific solution, such as any OS native copy functions that provide status notifications. – Remy Lebeau Sep 07 '21 at 19:34