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`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
diff --git a/src/config_file.c b/src/config_file.c
index 77edf13..aee1744 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -170,48 +170,56 @@ out:
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;
- git_config_entries *entries = NULL, *tmp;
+ git_config_entries *old = NULL;
git_config_file *include;
- int error, modified;
- uint32_t i;
+ int error;
+ size_t i;
if (b->header.parent.readonly)
return config_error_readonly();
- 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)
- goto out;
-
- /* Reparse the current configuration */
- git_array_foreach(b->file.includes, i, include) {
+ git_array_foreach(b->file.includes, i, include)
config_file_clear(include);
- }
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) {
git_error_set(GIT_ERROR_OS, "failed to lock config backend");
goto out;
}
- tmp = b->header.entries;
+ old = b->header.entries;
b->header.entries = entries;
- entries = tmp;
git_mutex_unlock(&b->header.values_mutex);
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);
return (error == GIT_ENOTFOUND) ? 0 : error;