Commit a0dc3027 by Patrick Steinhardt

config_file: split out function that sets config entries

Updating a config file backend's config entries is a bit more
involved, as it requires clearing of the old config entries as
well as handling locking correctly. As we will need this
functionality in a future patch to refresh config entries from a
buffer, let's extract this into its own function
`config_set_entries`.
parent 985f5cdf
...@@ -170,48 +170,56 @@ out: ...@@ -170,48 +170,56 @@ out:
return error; return error;
} }
static int config_refresh(git_config_backend *cfg) static int config_set_entries(git_config_backend *cfg, git_config_entries *entries)
{ {
diskfile_backend *b = (diskfile_backend *)cfg; diskfile_backend *b = (diskfile_backend *)cfg;
git_config_entries *entries = NULL, *tmp; git_config_entries *old = NULL;
git_config_file *include; git_config_file *include;
int error, modified; int error;
uint32_t i; size_t i;
if (b->header.parent.readonly) if (b->header.parent.readonly)
return config_error_readonly(); return config_error_readonly();
error = config_is_modified(&modified, &b->file); git_array_foreach(b->file.includes, i, include)
if (error < 0 && error != GIT_ENOTFOUND)
goto out;
if (!modified)
return 0;
if ((error = git_config_entries_new(&entries)) < 0)
goto out;
/* Reparse the current configuration */
git_array_foreach(b->file.includes, i, include) {
config_file_clear(include); config_file_clear(include);
}
git_array_clear(b->file.includes); git_array_clear(b->file.includes);
if ((error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0)
goto out;
if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) { if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) {
git_error_set(GIT_ERROR_OS, "failed to lock config backend"); git_error_set(GIT_ERROR_OS, "failed to lock config backend");
goto out; goto out;
} }
tmp = b->header.entries; old = b->header.entries;
b->header.entries = entries; b->header.entries = entries;
entries = tmp;
git_mutex_unlock(&b->header.values_mutex); git_mutex_unlock(&b->header.values_mutex);
out: out:
git_config_entries_free(old);
return error;
}
static int config_refresh(git_config_backend *cfg)
{
diskfile_backend *b = (diskfile_backend *)cfg;
git_config_entries *entries = NULL;
int error, modified;
error = config_is_modified(&modified, &b->file);
if (error < 0 && error != GIT_ENOTFOUND)
goto out;
if (!modified)
return 0;
if ((error = git_config_entries_new(&entries)) < 0 ||
(error = config_read(entries, b->header.repo, &b->file, b->header.level, 0)) < 0 ||
(error = config_set_entries(cfg, entries)) < 0)
goto out;
entries = NULL;
out:
git_config_entries_free(entries); git_config_entries_free(entries);
return (error == GIT_ENOTFOUND) ? 0 : error; return (error == GIT_ENOTFOUND) ? 0 : error;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment