kramer
kramer

Reputation: 327

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

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 ?

Upvotes: 0

Views: 996

Answers (1)

eerorika
eerorika

Reputation: 238391

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.

Upvotes: 3

Related Questions