libobs/util: Add config_save_safe function

This safely saves a configuration file, optionally with a backup.
master
jp9000 2015-08-21 17:42:24 -07:00
parent 3a77ac3516
commit f29431d8db
3 changed files with 55 additions and 0 deletions

View File

@ -348,6 +348,53 @@ int config_save(config_t *config)
return CONFIG_SUCCESS;
}
int config_save_safe(config_t *config, const char *temp_ext,
const char *backup_ext)
{
struct dstr temp_file = {0};
struct dstr backup_file = {0};
char *file = config->file;
int ret;
if (!temp_ext || !*temp_ext) {
blog(LOG_ERROR, "config_save_safe: invalid "
"temporary extension specified");
return CONFIG_ERROR;
}
dstr_copy(&temp_file, config->file);
if (*temp_ext != '.')
dstr_cat(&temp_file, ".");
dstr_cat(&temp_file, temp_ext);
config->file = temp_file.array;
ret = config_save(config);
config->file = file;
if (ret != CONFIG_SUCCESS) {
goto cleanup;
}
if (backup_ext && *backup_ext) {
dstr_copy(&backup_file, config->file);
if (*backup_ext != '.')
dstr_cat(&backup_file, ".");
dstr_cat(&backup_file, backup_ext);
os_unlink(backup_file.array);
os_rename(file, backup_file.array);
} else {
os_unlink(file);
}
os_rename(temp_file.array, file);
cleanup:
dstr_free(&temp_file);
dstr_free(&backup_file);
return ret;
}
void config_close(config_t *config)
{
struct config_section *defaults, *sections;

View File

@ -46,6 +46,8 @@ EXPORT int config_open(config_t **config, const char *file,
enum config_open_type open_type);
EXPORT int config_open_string(config_t **config, const char *str);
EXPORT int config_save(config_t *config);
EXPORT int config_save_safe(config_t *config, const char *temp_ext,
const char *backup_ext);
EXPORT void config_close(config_t *config);
EXPORT size_t config_num_sections(config_t *config);

View File

@ -90,6 +90,12 @@ public:
return config_save(config);
}
inline int SaveSafe(const char *temp_ext,
const char *backup_ext = nullptr)
{
return config_save_safe(config, temp_ext, backup_ext);
}
inline void Close()
{
config_close(config);