Hacker News new | ask | show | jobs
by Jerry2 2685 days ago
I have a flash drive that I sometimes put a video on to watch it on a small TV in the basement and I've noticed that Linux doesn't copy the file right away. The 'cp' does finish quickly but the data is not on the flash drive yet. You either have to eject and wait or sync and wait for it to actually transfer.

Needless to say, this tripped me up few times and videos weren't fully transferred.

2 comments

I also noticed that on Gnome. I never investigated the implementation details of that progress bar but my gut feelings is that the file is read from (or written to) the buffer cache quickly and the progress bar goes near to 100%, then stays there until the last writes succeed and actually write to the USB stick. Then the eject button sometimes need extra time to finish the sync and tells me to wait a little. I always remove the stick when it tells me it's safe to do it.
Your intuition is correct. The writes are quickly buffered to RAM and then fsync or close writes them out to the slo media. The (naive) progress bar probably only tracks progress buffering the writes — it's the simplest way to track progress, if inaccurate.
Yes. You can also run "watch cat /proc/meminfo" in console and watch "Dirty" and "Writeback" fields to see dynamics (and how long you have to wait).
That behaviour is defined by a mount option.
What's the proper way to mount flash devices?
Try the sync option. From the man page:

> All I/O to the filesystem should be done synchronously. In the case of media with a limited number of write cycles (e.g. some flash drives), sync may cause life-cycle shortening.

Thanks so much!