From c1524b2e1c6acaa9c3de362bbc92935e997c409c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 28 Feb 2018 11:33:11 +0100 Subject: config: move the repository to the diskfile header We pass this around and when creating a new iterator we need to read the repository pointer. Put it in a common place so we can reach it regardless of whether we got a full object or a snapshot. --- src/config_file.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 792a3de7a..4d1399933 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -86,13 +86,13 @@ typedef struct { /* mutex to coordinate accessing the values */ git_mutex values_mutex; refcounted_strmap *values; + const git_repository *repo; } diskfile_header; typedef struct { diskfile_header header; git_config_level_t level; - const git_repository *repo; git_array_t(git_config_parser) readers; @@ -271,7 +271,7 @@ static int config_open(git_config_backend *cfg, git_config_level_t level, const diskfile_backend *b = (diskfile_backend *)cfg; b->level = level; - b->repo = repo; + b->header.repo = repo; if ((res = refcounted_strmap_alloc(&b->header.values)) < 0) return res; @@ -343,7 +343,7 @@ static int config_refresh(git_config_backend *cfg) } git_array_clear(b->file.includes); - if ((error = config_read(values->values, b->repo, &b->file, b->level, 0)) < 0) + if ((error = config_read(values->values, b->header.repo, &b->file, b->level, 0)) < 0) goto out; if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) { @@ -423,7 +423,7 @@ static int config_iterator_new( if ((error = config_snapshot(&snapshot, backend)) < 0) return error; - if ((error = snapshot->open(snapshot, b->level, b->repo)) < 0) + if ((error = snapshot->open(snapshot, b->level, b->header.repo)) < 0) return error; it = git__calloc(1, sizeof(git_config_file_iter)); -- cgit v1.2.1 From 1785de4e5c102defa56849403885b3d5ead24a12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 28 Feb 2018 11:46:17 +0100 Subject: config: move the level field into the header We use it in a few places where we might have a full object or a snapshot so move it to where we can actually access it. --- src/config_file.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 4d1399933..2816a892e 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -87,13 +87,12 @@ typedef struct { git_mutex values_mutex; refcounted_strmap *values; const git_repository *repo; + git_config_level_t level; } diskfile_header; typedef struct { diskfile_header header; - git_config_level_t level; - git_array_t(git_config_parser) readers; bool locked; @@ -270,7 +269,7 @@ static int config_open(git_config_backend *cfg, git_config_level_t level, const int res; diskfile_backend *b = (diskfile_backend *)cfg; - b->level = level; + b->header.level = level; b->header.repo = repo; if ((res = refcounted_strmap_alloc(&b->header.values)) < 0) @@ -343,7 +342,7 @@ static int config_refresh(git_config_backend *cfg) } git_array_clear(b->file.includes); - if ((error = config_read(values->values, b->header.repo, &b->file, b->level, 0)) < 0) + if ((error = config_read(values->values, b->header.repo, &b->file, b->header.level, 0)) < 0) goto out; if ((error = git_mutex_lock(&b->header.values_mutex)) < 0) { @@ -423,7 +422,7 @@ static int config_iterator_new( if ((error = config_snapshot(&snapshot, backend)) < 0) return error; - if ((error = snapshot->open(snapshot, b->level, b->header.repo)) < 0) + if ((error = snapshot->open(snapshot, b->header.level, b->header.repo)) < 0) return error; it = git__calloc(1, sizeof(git_config_file_iter)); -- cgit v1.2.1 From 2424e64c4edf55e823fa42c365e4b80f67ed4df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 28 Feb 2018 12:06:02 +0100 Subject: config: harden our use of the backend objects a bit When we create an iterator we don't actually know that we have a live config object and we must instead only rely on the header. We fixed it to use this in a previous commit, but this makes it harder to misuse by converting to use the header object in the typecast. We also guard inside the `config_refresh` function against being given a snapshot (although callers right now do check). --- src/config_file.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/config_file.c b/src/config_file.c index 2816a892e..a594e51dd 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -326,6 +326,9 @@ static int config_refresh(git_config_backend *cfg) int error, modified; uint32_t i; + if (b->header.parent.readonly) + return 0; + error = config_is_modified(&modified, &b->file); if (error < 0 && error != GIT_ENOTFOUND) goto out; @@ -416,13 +419,13 @@ static int config_iterator_new( diskfile_header *h; git_config_file_iter *it; git_config_backend *snapshot; - diskfile_backend *b = (diskfile_backend *) backend; + diskfile_header *bh = (diskfile_header *) backend; int error; if ((error = config_snapshot(&snapshot, backend)) < 0) return error; - if ((error = snapshot->open(snapshot, b->header.level, b->header.repo)) < 0) + if ((error = snapshot->open(snapshot, bh->level, bh->repo)) < 0) return error; it = git__calloc(1, sizeof(git_config_file_iter)); -- cgit v1.2.1 From 9cd0c6f1f1b8dc5fbb5957e1ff3291953b14379e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Wed, 28 Feb 2018 16:01:16 +0100 Subject: config: return an error if config_refresh is called on a snapshot Instead of treating it as a no-op, treat it as a programming error and return the same kind of error as if you called to set or delete variables on a snapshot. --- src/config_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config_file.c b/src/config_file.c index a594e51dd..aa9e83b9c 100644 --- a/src/config_file.c +++ b/src/config_file.c @@ -327,7 +327,7 @@ static int config_refresh(git_config_backend *cfg) uint32_t i; if (b->header.parent.readonly) - return 0; + return config_error_readonly(); error = config_is_modified(&modified, &b->file); if (error < 0 && error != GIT_ENOTFOUND) -- cgit v1.2.1