Reputation: 12837
I have an embedded device (Linux + BusyBox) on which I loaded a file "my_file.txt"
. I used cat
to check the contents of the file, and it was satisfactory. Then, I pulled the plug and after reboot, I saw the file was still there, but with 0 bytes size...
Can that be caused by an unsync-ed file-system? This is actually a double-sided question:
cat "my_file.txt"
], but what I actually see, is a cached version that will not be there after reboot, unless a sync
will be called? and by "not be there" I mean the contents, as the file itself remainsBTW, when does Linux flush filesystems? I know that stdout
, for example, is flushed (by default) when a "\n"
is introduced [and can be configured somehow, don't remember exactly how]. Is there a rule for filesystems as well?
Upvotes: 0
Views: 669
Reputation: 36431
- Is the createion of a file and the copy of its contens happen in different stages? (allowing a phase wehre a file with 0 bytes exists)
Yes, copying a file is not an atomic operation as you first call to open()
and then write()
after... Opening with O_CREAT
mode will create an empty file, so yes : first an empty file which is filled after.
- Is it possible that I "see" the file [meaning I successfully managed to cat "my_file.txt"], but what I actually see, is a cached version that will not be there after reboot, unless a sync will be called? and by "not be there" I mean the contens, as the file itself remains
Yes exactly, what you saw is a cached version of the previous operations.
when does Linux flush filesystems?
The general rule is that the kernel flushes things when it wants. The only thing you can do is to ask for the flushing, but alas even this is just an asking and doesn't means that the flush occurred, it just means that the flush will occur soon. The corresponding command line is sync
.
Is there a rule for filesystems as well?
You may mount a filesystem requesting that IOs will be made in direct mode, or you can request for it on a file-by-file basis (see O_DIRECT
and alikes in open
). But be aware that direct mode generally drops the performances...
Upvotes: 1
Reputation: 229264
Is the createion of a file and the copy of its contens happen in different stages? (allowing a phase where a file with 0 bytes exists)
Yes. The normal operation on files are
Is it possible that I "see" the file [meaning I successfully managed to cat "my_file.txt"], but what I actually see, is a cached version that will not be there after reboot, unless a sync will be called?
Yes. If step 1 above was synced to the hard drive, but step 2 was not, you lose the file content.
BTW, when does Linux flush filesystems? I know that stdout, for example, is flushed (by default) when a "\n" is introduced [and can be configured somehow, don't remember exactly how]. Is there a rule for filesystems as well?
No, there are no general rules - it's complicated. The OS/Kernel and filesystem caches data in RAM and writes it to disk when its internal algorithms figure out it's a good time to do so.
Note that there are flush/sync'ing on many levels. The flushing you talk about "when a "\n" is introduced", is only a flush from a program down to the operating system. The operating system might then keep the data in RAM only and flush it to a hard drive later. The hard drive might even cache it in RAM onboard the harddrive and write it to permanent storage later on.
Normally you can run the sync
command on a command line to ensure all cached data is written from the OS to the hard drive. (Albeit on low-end hard drives with onboard RAM that is not battery backed up, this could still lose data that resides in the ram onboard a harddrive if power is cut).
Upvotes: 0
Reputation: 369
if you turn off the device while you are working with file in your project it can be destroyed your file because for example you want erase your file's document and write again into it.my be you device turn off between these two stage and your file destroy.
Upvotes: -1