Changeset - r11331:1af5419c9807
[Not reviewed]
master
0 1 0
rubidium - 15 years ago 2009-03-12 14:22:17
rubidium@openttd.org
(svn r15686) -Codechange: make it a bit harder for crashes to trash your config file.
1 file changed with 30 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/ini.cpp
Show inline comments
 
@@ -7,12 +7,18 @@
 
#include "core/math_func.hpp"
 
#include "debug.h"
 
#include "ini_type.h"
 
#include "string_func.h"
 
#include "fileio_func.h"
 

	
 
#if (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L) || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500)
 
# define WITH_FDATASYNC
 
# include <unistd.h>
 
#endif
 

	
 

	
 
IniItem::IniItem(IniGroup *parent, const char *name, size_t len) : next(NULL), value(NULL), comment(NULL)
 
{
 
	if (len == 0) len = strlen(name);
 

	
 
	this->name = strndup(name, len);
 
	*parent->last_item = this;
 
@@ -250,13 +256,22 @@ void IniFile::LoadFromDisk(const char *f
 
	free(comment);
 
	fclose(in);
 
}
 

	
 
bool IniFile::SaveToDisk(const char *filename)
 
{
 
	FILE *f = fopen(filename, "w");
 
	/*
 
	 * First write the configuration to a (temporary) file and then rename
 
	 * that file. This to prevent that when OpenTTD crashes during the save
 
	 * you end up with a truncated configuration file.
 
	 */
 
	char file_new[MAX_PATH];
 

	
 
	strecpy(file_new, filename, lastof(file_new));
 
	strecat(file_new, ".new", lastof(file_new));
 
	FILE *f = fopen(file_new, "w");
 
	if (f == NULL) return false;
 

	
 
	for (const IniGroup *group = this->group; group != NULL; group = group->next) {
 
		if (group->comment) fputs(group->comment, f);
 
		fprintf(f, "[%s]\n", group->name);
 
		for (const IniItem *item = group->item; item != NULL; item = item->next) {
 
@@ -272,9 +287,23 @@ bool IniFile::SaveToDisk(const char *fil
 

	
 
			fprintf(f, " = %s\n", item->value);
 
		}
 
	}
 
	if (this->comment) fputs(this->comment, f);
 

	
 
/*
 
 * POSIX (and friends) do not guarantee that when a file is closed it is
 
 * flushed to the disk. So we manually flush it do disk if we have the
 
 * APIs to do so. We only need to flush the data as the metadata itself
 
 * (modification date etc.) is not important to us; only the real data is.
 
 */
 
#ifdef WITH_FDATASYNC
 
	int ret = fdatasync(fileno(f));
 
	fclose(f);
 
	if (ret != 0) return false;
 
#else
 
	fclose(f);
 
#endif
 

	
 
	rename(file_new, filename);
 
	return true;
 
}
0 comments (0 inline, 0 general)