File corruption in append mode
Main question is: can data be corrupted by appending new data to the end of the file (bearing in mind ha开发者_如何学Crd disk sector size for example of 512)?
What if for example i have file with 1023 bytes, append 1 byte, and i have crush (power loss, disk buffer issues etc...)?
In what condition will be the second sector in this scenario?
Impossible to say. There's multiple layers of abstraction, cacheing, and even simple electrical propagation delays to account for.
Your code may have written out a byte. But the OS is not going to immediately write out that byte. Hitting a drive is one of the absolute slowest operations that a computer can do these days, so it's going to buffer that byte and see if any further bytes getting stuck into the output buffer.
If nothing occurs within the cache's timeout period, then the bytes will be sent to the drive to be committed to media. But the drive itself may also do some cacheing, etc... There's rotational delay to account for (the actual sector the byte should go into has to actually be under the write head to be written, which can be several milliseconds).
In other words, writing a byte out to disk can take essentially a random amount of time to actually be written out onto the disk media. If the power failure hits during this interval, your data is lost.
精彩评论