Reputation: 145
I am trying to copy multiple folders and files from one folder (linux SBC) to another folder (USB mounted folder) from a process.
My process is completely written in C. The linux system is an SBC running YOCTO, and it doesnt have rsync
available.
I used popen
command for most of the commands like mount
, umount
, etc..
But for copying (cp
command) I am not sure how to wait for the copy to complete.
I am using below format in my C code and it works (copying works)
system("yes | cp -rf " USB_DATA_SOURCE_PATH " " USB_DATA_DESTINATION_PATH);
I need to copy multiple files and there can be any number of files with dynamic names. All I need is a way to know that the copying is completed and is safe to umount
and indicate to the user.
Upvotes: 4
Views: 1950
Reputation: 3999
As hinted in the comments:
system()
waits until cp
has finished, so you don't have to add any wait cycles.cp
returns the copied data may not necessarily have already been written to disk from the buffers. You can call sync
explicitly to make this happen.umount
already ensures the cache being synced/buffers flushed; that's why it sometimes takes some time until umount
returns.To wrap it all up, no need for an explicit sync
between cp
and umount
, except if there are additional actions between and you want to make sure the data is synced even when the USB disk is yanked from the machine without being properly unmounted. In that case your best option is to extend your system()
call:
system("yes | cp -rf " USB_DATA_SOURCE_PATH " " USB_DATA_DESTINATION_PATH "; sync");
to force sync
independent of the success of the copy command, or
system("yes | cp -rf " USB_DATA_SOURCE_PATH " " USB_DATA_DESTINATION_PATH " && sync");
if you need to get the unchanged exit code of cp
in case it fails.
Upvotes: 3