How to preserve integrity of a log file on SD card?
Posted: Thu Aug 24, 2023 3:40 pm
I'm writing a log file to an SD card. Right now, each entry has a fixed length (16 bytes). 1 data point is generated every second, and they're appended to the file en masse every 30 seconds. My issue is that if the device is turned off during the SD write (which is not visible to the user), the data may be corrupted partway through an entry.
The file is structured as an array (right now -- I could change it if need be): just each entry, in a row. Right now there is no data integrity checking, so there's no way to know if, say, one or two bytes extra got added right before a power off, and there's no way to know where the error is. That's what I would like to add now. I could just add a hash of each entry after it; the problem is, scanning through the entire file every time the device is turned on could take a long time.
Does anybody know how a good way to preserve data integrity in a file like this? Ensuring each write operation is atomic, efficiently checking hashes, etc. I tried Googling for a solution but could only find questions about specific systems, not about the data side.
The file is structured as an array (right now -- I could change it if need be): just each entry, in a row. Right now there is no data integrity checking, so there's no way to know if, say, one or two bytes extra got added right before a power off, and there's no way to know where the error is. That's what I would like to add now. I could just add a hash of each entry after it; the problem is, scanning through the entire file every time the device is turned on could take a long time.
Does anybody know how a good way to preserve data integrity in a file like this? Ensuring each write operation is atomic, efficiently checking hashes, etc. I tried Googling for a solution but could only find questions about specific systems, not about the data side.