nconf icon indicating copy to clipboard operation
nconf copied to clipboard

Data loss on ext4

Open levitatejay opened this issue 10 years ago • 3 comments

When my device I'm coding for (Intel Edison) crashes or loses power up to a minute after writing the config file then the entire config file is 0 bytes after a system restart and attempting to load the empty config file throws an error.

After some googling I believe this is due to the way the config file is saved... it would appear that you truncate the old file and then write the new contents... If instead the new config was written to a temporary file and then renamed it over the old file there would be the following benefits:

  • If the application crashes while writing the new file, the original file is left in place
  • If an application reads the file the same time as someone is updating it the reading application gets either the old or the new file in its entirety. I.e. we will never read a partially finished file, a mixup of two files, or a missing file.
  • If two applications update the file at the same time we will at worst lose the changes from one of the writers, but never cause a corrupted file.

Taken from here: https://blogs.gnome.org/alexl/2009/03/16/ext4-vs-fsync-my-take/

This also highlights another problem... I'm not sure if its intended or not.... When attempting to load a config file that doesn't exist there is no problem and the config is simply created... If you attempt to load an empty 0 byte config file it throws an unexpected end of content error.

levitatejay avatar Aug 24 '15 00:08 levitatejay

same for me

goodmind avatar Oct 18 '15 10:10 goodmind

@levitatejay @goodmind can i confirm this is due to using nconf.save() somewhere in your application? All we need to do here is use an atomic file writer module and make our own.

jcrugzz avatar Oct 19 '15 15:10 jcrugzz

Yes it appears that if there is power loss within around 60 seconds of calling nconf.save() the config file will be 0 bytes on next system start.

levitatejay avatar Oct 19 '15 21:10 levitatejay