You are here: atomic file saves « PHP Programming Language « IT news, forums, messages
atomic file saves

Posted by CptDondo on 10/02/06 23:59

I have an embedded system that will save configuration files based on
user input. This device will be used in areas that have uncertain
power, and a battery backup is not a realistic option.

It is not critical that the user's latest changes be saved, but it is
critical that the system have a valid configuration file, so I want to
minimize the chances of having a corrupt config file.

I've come up with this:

$oldConfFile = $confFile . ".bak";
$newConfFile = $confFile . ".new";
$fp = fopen($newConfFile, "w");
// FIXME error message here
if ($fp == false ) return array();
fputs($fp, $newConfig);
fclose($fp);
rename($confFile,$oldConfFile);
rename($newConfFile,$confFile);

which is about as atomic as I can think of.... And I can do some
checking on boot and (possibly) copy either the .bak or the .new file
over to the config file if it is valid and the main config file is not.

Can anyone offer any comments or suggestions for making this more robust?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация