0

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?

nobody
  • 19,814
  • 17
  • 56
  • 77
  • Initial suggestion is to replace the individual fsyncs by a single sync() in the end in the hope that you forgot to sync something else. If this doesn't work, can you force umount the device after the sync ? – hdante Apr 25 '14 at 21:36
  • Using sync(), the whole filesystem will be synchronized and i'm trying to avoid this. Umount is my current solution but if any other process is using the USB stick, umount will fail. – user3458705 Apr 25 '14 at 21:47
  • So you're removing the USB stick while another process is using it, then it is likely that the file system will be corrupt. – hdante Apr 25 '14 at 22:53
  • There is a possibility to be used by another process (just for reading) and I don't have the control for that. But the file system seems to be corrupted even though I am the only one who use USB stick. (without umonut) – user3458705 Apr 26 '14 at 04:11

2 Answers2

3

Unmount the device before you remove it. For a non-journalled filesystem like VFAT, there is no guarantee that the filesystem will be in a consistent state on disk while it is mounted.

1

After writing, do "mount" with options "-o remount,ro". That will write everything and change to read-only mode. Other applications can have files open.

For writing, do "mount" with options "-o remount,rw,noatime"

Hans
  • 61
  • 5