Hello everyone,
I'm working on a digital television receiver which can record a transport stream to an external USB hard disk drive.
Whenever I write to the HDD, whether a few bytes or an entire program, if I don't call sync() before I pull the USB plug out, when I re-plug the drive, the OS needs to run fsck.
(NB : filesystem is FAT32, OS is OS21/OS+)
I'm aware that the OS must be performing some form of caching, and marks the drive as dirty once I write something to the drive.
The documentation states:
""" File system state
Many subordinate file systems perform caching to improve performance and therefore need to track whether the on-disk structure is in a consistent state.
When the volume is first mounted, with vfs_mount(), it is clean. Any partial writes or unwritten cached data means that it may be dirty or inconsistent until the cache is flushed. The vfs_sync() and vfs_sync_all() functions flush the cache and mark the volume as clean. When the volume is unmounted, using vfs_umount(), the cache is flushed and the volume is marked as clean.
Many file systems record the current state in the on-disk structures and this value is one of the things checked by the vfs_mount() function. If the volume is dirty the vfs_mount() fails with errno set to EAGAIN to signify the volume needs vfs_fsck() to be run to restore it to a consistent state. """
If I'm streaming large (770 kB) blocks to the disk, I don't care about caching, as I'm not going to read these data for a long time.
I would need some way to turn caching off, which means every write should commit to the disk. Would this be equivalent to calling sync after every write?
When I'm done writing my file, I do call fflush and close on the file descriptor (the fflush should be redundant) but that does not seem sufficient to convince the OS to flush the file's data and metadata to the disk. (Perphaps the OS keeps a copy of the FAT in memory, and only commits that on sync?)
What happens if two threads call sync "at the same time"? Is sync supposed to be reentrant? or thread-safe? (I suppose it will depend on the OS?)
Regards.