I'm trying to copy some folders and files on a USB stick and right after everything is done, to remove the USB stick. The implementation is something like this:
- create the corresponding folder structure on USB stick using
mkdir
fsync
on each directory descriptor after the folder is created- copy the file content (using C++ streams)
fsync
on each file descriptor after each file is copied
Right after the last file is copied, I remove the USB stick (~500ms - 1s). But sometimes the whole folder structure is missing. In order to avoid that, I used again fsync
on each directory descriptor after the last file is copied. This time all files and folders are present on the USB stick but sometimes I have garbage in the folder structure or the USB stick become corrupted. The USB stick filesystem is vfat. Any idea about how to have the data saved correctly?