Commit 2bff84ba423e6a6693ec6c34fbc768484959c26a

Patrick Steinhardt 2019-07-26T21:02:56

config_file: separate out read-only backend To further distinguish the file writeable and readonly backends, separate the readonly backend into its own "config_snapshot.c" implementation. The snapshot backend can be generically used to snapshot any type of backend.

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
diff --git a/src/config_backend.h b/src/config_backend.h
index 6d678ae..dbb1905 100644
--- a/src/config_backend.h
+++ b/src/config_backend.h
@@ -26,6 +26,18 @@
 extern int git_config_backend_from_file(git_config_backend **out, const char *path);
 
 /**
+ * Create a readonly configuration file backend from another backend
+ *
+ * This copies the complete contents of the source backend to the
+ * new backend. The new backend will be completely read-only and
+ * cannot be modified.
+ *
+ * @param out the new snapshotted backend
+ * @param source the backend to copy
+ */
+extern int git_config_backend_snapshot(git_config_backend **out, git_config_backend *source);
+
+/**
  * Create an in-memory configuration file backend
  *
  * @param out the new backend
diff --git a/src/config_file.c b/src/config_file.c
index 448d7bf..bd5472f 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -7,17 +7,19 @@
 
 #include "config.h"
 
-#include "filebuf.h"
-#include "sysdir.h"
-#include "buffer.h"
-#include "buf_text.h"
 #include "git2/config.h"
 #include "git2/sys/config.h"
 #include "git2/types.h"
-#include "strmap.h"
+
 #include "array.h"
-#include "config_parse.h"
+#include "buf_text.h"
+#include "buffer.h"
+#include "config_backend.h"
 #include "config_entries.h"
+#include "config_parse.h"
+#include "filebuf.h"
+#include "strmap.h"
+#include "sysdir.h"
 #include "wildmatch.h"
 
 #include <ctype.h>
@@ -50,13 +52,6 @@ typedef struct {
 } diskfile_backend;
 
 typedef struct {
-	git_config_backend parent;
-	git_mutex values_mutex;
-	git_config_entries *entries;
-	git_config_backend *source;
-} diskfile_readonly_backend;
-
-typedef struct {
 	const git_repository *repo;
 	diskfile *file;
 	git_config_entries *entries;
@@ -69,14 +64,6 @@ static int config_read_buffer(git_config_entries *entries, const git_repository 
 static int config_write(diskfile_backend *cfg, const char *orig_key, const char *key, const p_regex_t *preg, const char *value);
 static char *escape_value(const char *ptr);
 
-static int config_snapshot(git_config_backend **out, git_config_backend *in);
-
-static int config_error_readonly(void)
-{
-	git_error_set(GIT_ERROR_CONFIG, "this backend is read-only");
-	return -1;
-}
-
 /**
  * Take the current values map from the backend and increase its
  * refcount. This is its own function to make sure we use the mutex to
@@ -181,8 +168,10 @@ static int config_set_entries(git_config_backend *cfg, git_config_entries *entri
 	int error;
 	uint32_t i;
 
-	if (b->parent.readonly)
-		return config_error_readonly();
+	if (b->parent.readonly) {
+		git_error_set(GIT_ERROR_CONFIG, "this backend is read-only");
+		return -1;
+	}
 
 	git_array_foreach(b->file.includes, i, include)
 		config_file_clear(include);
@@ -280,25 +269,6 @@ out:
 	return error;
 }
 
-static int config_iterator_new_readonly(
-	git_config_iterator **iter,
-	struct git_config_backend *backend)
-{
-	diskfile_readonly_backend *b = GIT_CONTAINER_OF(backend, diskfile_readonly_backend, parent);
-	git_config_entries *entries = NULL;
-	int error;
-
-	if ((error = git_config_entries_dup(&entries, b->entries)) < 0 ||
-	    (error = git_config_entries_iterator_new(iter, entries)) < 0)
-		goto out;
-
-out:
-	/* Let iterator delete duplicated entries when it's done */
-	git_config_entries_free(entries);
-	return error;
-}
-
-
 static int config_set(git_config_backend *cfg, const char *name, const char *value)
 {
 	diskfile_backend *b = GIT_CONTAINER_OF(cfg, diskfile_backend, parent);
@@ -376,34 +346,6 @@ static int config_get(git_config_backend *cfg, const char *key, git_config_entry
 	return 0;
 }
 
-static int config_get_readonly(git_config_backend *cfg, const char *key, git_config_entry **out)
-{
-	diskfile_readonly_backend *b = GIT_CONTAINER_OF(cfg, diskfile_readonly_backend, parent);
-	git_config_entries *entries = NULL;
-	git_config_entry *entry;
-	int error = 0;
-
-	if (git_mutex_lock(&b->values_mutex) < 0) {
-	    git_error_set(GIT_ERROR_OS, "failed to lock config backend");
-	    return -1;
-	}
-
-	entries = b->entries;
-	git_config_entries_incref(entries);
-	git_mutex_unlock(&b->values_mutex);
-
-	if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
-		git_config_entries_free(entries);
-		return error;
-	}
-
-	entry->free = free_diskfile_entry;
-	entry->payload = entries;
-	*out = entry;
-
-	return 0;
-}
-
 static int config_set_multivar(
 	git_config_backend *cfg, const char *name, const char *regexp, const char *value)
 {
@@ -561,7 +503,7 @@ int git_config_backend_from_file(git_config_backend **out, const char *path)
 	backend->parent.del = config_delete;
 	backend->parent.del_multivar = config_delete_multivar;
 	backend->parent.iterator = config_iterator_new;
-	backend->parent.snapshot = config_snapshot;
+	backend->parent.snapshot = git_config_backend_snapshot;
 	backend->parent.lock = config_lock;
 	backend->parent.unlock = config_unlock;
 	backend->parent.free = backend_free;
@@ -571,135 +513,6 @@ int git_config_backend_from_file(git_config_backend **out, const char *path)
 	return 0;
 }
 
-static int config_set_readonly(git_config_backend *cfg, const char *name, const char *value)
-{
-	GIT_UNUSED(cfg);
-	GIT_UNUSED(name);
-	GIT_UNUSED(value);
-
-	return config_error_readonly();
-}
-
-static int config_set_multivar_readonly(
-	git_config_backend *cfg, const char *name, const char *regexp, const char *value)
-{
-	GIT_UNUSED(cfg);
-	GIT_UNUSED(name);
-	GIT_UNUSED(regexp);
-	GIT_UNUSED(value);
-
-	return config_error_readonly();
-}
-
-static int config_delete_multivar_readonly(git_config_backend *cfg, const char *name, const char *regexp)
-{
-	GIT_UNUSED(cfg);
-	GIT_UNUSED(name);
-	GIT_UNUSED(regexp);
-
-	return config_error_readonly();
-}
-
-static int config_delete_readonly(git_config_backend *cfg, const char *name)
-{
-	GIT_UNUSED(cfg);
-	GIT_UNUSED(name);
-
-	return config_error_readonly();
-}
-
-static int config_lock_readonly(git_config_backend *_cfg)
-{
-	GIT_UNUSED(_cfg);
-
-	return config_error_readonly();
-}
-
-static int config_unlock_readonly(git_config_backend *_cfg, int success)
-{
-	GIT_UNUSED(_cfg);
-	GIT_UNUSED(success);
-
-	return config_error_readonly();
-}
-
-static void backend_readonly_free(git_config_backend *_backend)
-{
-	diskfile_readonly_backend *backend = GIT_CONTAINER_OF(_backend, diskfile_readonly_backend, parent);
-
-	if (backend == NULL)
-		return;
-
-	git_config_entries_free(backend->entries);
-	git_mutex_free(&backend->values_mutex);
-	git__free(backend);
-}
-
-static int config_readonly_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
-{
-	diskfile_readonly_backend *b = GIT_CONTAINER_OF(cfg, diskfile_readonly_backend, parent);
-	git_config_entries *entries = NULL;
-	git_config_iterator *it = NULL;
-	git_config_entry *entry;
-	int error;
-
-	/* We're just copying data, don't care about the level or repo*/
-	GIT_UNUSED(level);
-	GIT_UNUSED(repo);
-
-	if ((error = git_config_entries_new(&entries)) < 0 ||
-	    (error = b->source->iterator(&it, b->source)) < 0)
-		goto out;
-
-	while ((error = git_config_next(&entry, it)) == 0)
-		if ((error = git_config_entries_dup_entry(entries, entry)) < 0)
-			goto out;
-
-	if (error < 0) {
-		if (error != GIT_ITEROVER)
-			goto out;
-		error = 0;
-	}
-
-	b->entries = entries;
-
-out:
-	git_config_iterator_free(it);
-	if (error)
-		git_config_entries_free(entries);
-	return error;
-}
-
-static int config_snapshot(git_config_backend **out, git_config_backend *source)
-{
-	diskfile_readonly_backend *backend;
-
-	backend = git__calloc(1, sizeof(diskfile_readonly_backend));
-	GIT_ERROR_CHECK_ALLOC(backend);
-
-	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
-	git_mutex_init(&backend->values_mutex);
-
-	backend->source = source;
-
-	backend->parent.readonly = 1;
-	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
-	backend->parent.open = config_readonly_open;
-	backend->parent.get = config_get_readonly;
-	backend->parent.set = config_set_readonly;
-	backend->parent.set_multivar = config_set_multivar_readonly;
-	backend->parent.del = config_delete_readonly;
-	backend->parent.del_multivar = config_delete_multivar_readonly;
-	backend->parent.iterator = config_iterator_new_readonly;
-	backend->parent.lock = config_lock_readonly;
-	backend->parent.unlock = config_unlock_readonly;
-	backend->parent.free = backend_readonly_free;
-
-	*out = &backend->parent;
-
-	return 0;
-}
-
 static int included_path(git_buf *out, const char *dir, const char *path)
 {
 	/* From the user's home */
diff --git a/src/config_snapshot.c b/src/config_snapshot.c
new file mode 100644
index 0000000..59617ce
--- /dev/null
+++ b/src/config_snapshot.c
@@ -0,0 +1,205 @@
+/*
+ * Copyright (C) the libgit2 contributors. All rights reserved.
+ *
+ * This file is part of libgit2, distributed under the GNU GPL v2 with
+ * a Linking Exception. For full terms see the included COPYING file.
+ */
+
+#include "config.h"
+
+#include "config_entries.h"
+
+typedef struct {
+	git_config_backend parent;
+	git_mutex values_mutex;
+	git_config_entries *entries;
+	git_config_backend *source;
+} diskfile_readonly_backend;
+
+static int config_error_readonly(void)
+{
+	git_error_set(GIT_ERROR_CONFIG, "this backend is read-only");
+	return -1;
+}
+
+static int config_iterator_new_readonly(
+	git_config_iterator **iter,
+	struct git_config_backend *backend)
+{
+	diskfile_readonly_backend *b = GIT_CONTAINER_OF(backend, diskfile_readonly_backend, parent);
+	git_config_entries *entries = NULL;
+	int error;
+
+	if ((error = git_config_entries_dup(&entries, b->entries)) < 0 ||
+	    (error = git_config_entries_iterator_new(iter, entries)) < 0)
+		goto out;
+
+out:
+	/* Let iterator delete duplicated entries when it's done */
+	git_config_entries_free(entries);
+	return error;
+}
+
+/* release the map containing the entry as an equivalent to freeing it */
+static void free_diskfile_entry(git_config_entry *entry)
+{
+	git_config_entries *entries = (git_config_entries *) entry->payload;
+	git_config_entries_free(entries);
+}
+
+static int config_get_readonly(git_config_backend *cfg, const char *key, git_config_entry **out)
+{
+	diskfile_readonly_backend *b = GIT_CONTAINER_OF(cfg, diskfile_readonly_backend, parent);
+	git_config_entries *entries = NULL;
+	git_config_entry *entry;
+	int error = 0;
+
+	if (git_mutex_lock(&b->values_mutex) < 0) {
+	    git_error_set(GIT_ERROR_OS, "failed to lock config backend");
+	    return -1;
+	}
+
+	entries = b->entries;
+	git_config_entries_incref(entries);
+	git_mutex_unlock(&b->values_mutex);
+
+	if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
+		git_config_entries_free(entries);
+		return error;
+	}
+
+	entry->free = free_diskfile_entry;
+	entry->payload = entries;
+	*out = entry;
+
+	return 0;
+}
+
+static int config_set_readonly(git_config_backend *cfg, const char *name, const char *value)
+{
+	GIT_UNUSED(cfg);
+	GIT_UNUSED(name);
+	GIT_UNUSED(value);
+
+	return config_error_readonly();
+}
+
+static int config_set_multivar_readonly(
+	git_config_backend *cfg, const char *name, const char *regexp, const char *value)
+{
+	GIT_UNUSED(cfg);
+	GIT_UNUSED(name);
+	GIT_UNUSED(regexp);
+	GIT_UNUSED(value);
+
+	return config_error_readonly();
+}
+
+static int config_delete_multivar_readonly(git_config_backend *cfg, const char *name, const char *regexp)
+{
+	GIT_UNUSED(cfg);
+	GIT_UNUSED(name);
+	GIT_UNUSED(regexp);
+
+	return config_error_readonly();
+}
+
+static int config_delete_readonly(git_config_backend *cfg, const char *name)
+{
+	GIT_UNUSED(cfg);
+	GIT_UNUSED(name);
+
+	return config_error_readonly();
+}
+
+static int config_lock_readonly(git_config_backend *_cfg)
+{
+	GIT_UNUSED(_cfg);
+
+	return config_error_readonly();
+}
+
+static int config_unlock_readonly(git_config_backend *_cfg, int success)
+{
+	GIT_UNUSED(_cfg);
+	GIT_UNUSED(success);
+
+	return config_error_readonly();
+}
+
+static void backend_readonly_free(git_config_backend *_backend)
+{
+	diskfile_readonly_backend *backend = GIT_CONTAINER_OF(_backend, diskfile_readonly_backend, parent);
+
+	if (backend == NULL)
+		return;
+
+	git_config_entries_free(backend->entries);
+	git_mutex_free(&backend->values_mutex);
+	git__free(backend);
+}
+
+static int config_readonly_open(git_config_backend *cfg, git_config_level_t level, const git_repository *repo)
+{
+	diskfile_readonly_backend *b = GIT_CONTAINER_OF(cfg, diskfile_readonly_backend, parent);
+	git_config_entries *entries = NULL;
+	git_config_iterator *it = NULL;
+	git_config_entry *entry;
+	int error;
+
+	/* We're just copying data, don't care about the level or repo*/
+	GIT_UNUSED(level);
+	GIT_UNUSED(repo);
+
+	if ((error = git_config_entries_new(&entries)) < 0 ||
+	    (error = b->source->iterator(&it, b->source)) < 0)
+		goto out;
+
+	while ((error = git_config_next(&entry, it)) == 0)
+		if ((error = git_config_entries_dup_entry(entries, entry)) < 0)
+			goto out;
+
+	if (error < 0) {
+		if (error != GIT_ITEROVER)
+			goto out;
+		error = 0;
+	}
+
+	b->entries = entries;
+
+out:
+	git_config_iterator_free(it);
+	if (error)
+		git_config_entries_free(entries);
+	return error;
+}
+
+int git_config_backend_snapshot(git_config_backend **out, git_config_backend *source)
+{
+	diskfile_readonly_backend *backend;
+
+	backend = git__calloc(1, sizeof(diskfile_readonly_backend));
+	GIT_ERROR_CHECK_ALLOC(backend);
+
+	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
+	git_mutex_init(&backend->values_mutex);
+
+	backend->source = source;
+
+	backend->parent.readonly = 1;
+	backend->parent.version = GIT_CONFIG_BACKEND_VERSION;
+	backend->parent.open = config_readonly_open;
+	backend->parent.get = config_get_readonly;
+	backend->parent.set = config_set_readonly;
+	backend->parent.set_multivar = config_set_multivar_readonly;
+	backend->parent.del = config_delete_readonly;
+	backend->parent.del_multivar = config_delete_multivar_readonly;
+	backend->parent.iterator = config_iterator_new_readonly;
+	backend->parent.lock = config_lock_readonly;
+	backend->parent.unlock = config_unlock_readonly;
+	backend->parent.free = backend_readonly_free;
+
+	*out = &backend->parent;
+
+	return 0;
+}