What are possible reasons for binary files corruption on android devices

songlj picture songlj · Jan 26, 2016 · Viewed 8.3k times · Source

Recently I'm investigating reasons for binary file corruption. Specificly, we have an android app, the native part can read/write binary file on SD card. Sometimes, the binary file corrupts for unknown reasons. We have collected some of these files from different users, and found some interesting facts.

One majority kind of corruption is that, the first 4096 bytes of the binary file are erased. When I hexdump these files, the first 4096 bytes are all zeros. Not more than 4096 or less than 4096, but exactly 4096 bytes. I think this is not coincidence. I know 4096 bytes is one page size. But lacking experience, I cannot figure out the reason, and more importantly, I don't know how to avoid such things for other users/devices.

Besides that, in the middle of some binary files, there are also some continuous zeros segments, which it shouldn't be there. If it is not our programmes' bug, is there any possible reasons which may related to platform/device kernel, or anything else like device suddenly out of power?

I hope anyone who have experienced similar situations can give me some hint/advice/solutions etc. This really confused me a lot.

Many thanks~

Answer

Nikilator picture Nikilator · Feb 16, 2016

I have some simmilar experience in some embedded applications that corrupt binaries. First of all, double check your file handling (specially in multithreading environments), I can imagine you have done it thoroughly. Then, try to sync all the writings. The linux kernel doesn't write as you command your app to write, but buffers data before flushing to disk.

http://linux.die.net/man/2/sync

Hope this helps.