| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "config.h" |
| 9 |
|
| |
| 10 |
|
| #include "config_backend.h" |
| 11 |
|
| #include "config_parse.h" |
| 12 |
|
| #include "config_entries.h" |
| 13 |
|
| |
| 14 |
|
| typedef struct { |
| 15 |
|
| git_config_backend parent; |
| 16 |
|
| git_config_entries *entries; |
| 17 |
|
| git_str cfg; |
| 18 |
|
| } config_memory_backend; |
| 19 |
|
| |
| 20 |
|
| typedef struct { |
| 21 |
|
| git_config_entries *entries; |
| 22 |
|
| git_config_level_t level; |
| 23 |
|
| } config_memory_parse_data; |
| 24 |
|
| |
| 25 |
0 |
| static int config_error_readonly(void) |
| 26 |
|
| { |
| 27 |
0 |
| git_error_set(GIT_ERROR_CONFIG, "this backend is read-only"); |
| 28 |
0 |
| return -1; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
0 |
| static int read_variable_cb( |
| 32 |
|
| git_config_parser *reader, |
| 33 |
|
| const char *current_section, |
| 34 |
|
| const char *var_name, |
| 35 |
|
| const char *var_value, |
| 36 |
|
| const char *line, |
| 37 |
|
| size_t line_len, |
| 38 |
|
| void *payload) |
| 39 |
|
| { |
| 40 |
0 |
| config_memory_parse_data *parse_data = (config_memory_parse_data *) payload; |
| 41 |
0 |
| git_str buf = GIT_STR_INIT; |
| 42 |
|
| git_config_entry *entry; |
| 43 |
|
| const char *c; |
| 44 |
|
| int result; |
| 45 |
|
| |
| 46 |
0 |
| GIT_UNUSED(reader); |
| 47 |
0 |
| GIT_UNUSED(line); |
| 48 |
0 |
| GIT_UNUSED(line_len); |
| 49 |
|
| |
| 50 |
0 |
▶ | if (current_section) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (current_section) { |
0 |
0 |
0 |
|
| 51 |
|
| /* TODO: Once warnings land, we should likely warn |
| 52 |
|
| * here. Git appears to warn in most cases if it sees |
| 53 |
|
| * un-namespaced config options. |
| 54 |
|
| */ |
| 55 |
0 |
| git_str_puts(&buf, current_section); |
| 56 |
0 |
| git_str_putc(&buf, '.'); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
0 |
▶ | for (c = var_name; *c; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = var_name; *c; c++) |
0 |
0 |
0 |
|
| 60 |
0 |
| git_str_putc(&buf, git__tolower(*c)); |
| 61 |
|
| |
| 62 |
0 |
▶ | if (git_str_oom(&buf)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_oom(&buf)) |
0 |
0 |
0 |
|
| 63 |
0 |
| return -1; |
| 64 |
|
| |
| 65 |
0 |
| entry = git__calloc(1, sizeof(git_config_entry)); |
| 66 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(entry); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(entry); |
0 |
0 |
0 |
|
| 67 |
0 |
| entry->name = git_str_detach(&buf); |
| 68 |
0 |
▶ | entry->value = var_value ? git__strdup(var_value) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| entry->value = var_value ? git__strdup(var_value) : NULL; |
0 |
0 |
0 |
|
| 69 |
0 |
| entry->level = parse_data->level; |
| 70 |
0 |
| entry->include_depth = 0; |
| 71 |
|
| |
| 72 |
0 |
▶ | if ((result = git_config_entries_append(parse_data->entries, entry)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((result = git_config_entries_append(parse_data->entries, entry)) < 0) |
0 |
0 |
0 |
|
| 73 |
0 |
| return result; |
| 74 |
|
| |
| 75 |
0 |
| return result; |
| 76 |
|
| } |
| 77 |
|
| |
| 78 |
0 |
| static int config_memory_open(git_config_backend *backend, git_config_level_t level, const git_repository *repo) |
| 79 |
|
| { |
| 80 |
0 |
| config_memory_backend *memory_backend = (config_memory_backend *) backend; |
| 81 |
0 |
| git_config_parser parser = GIT_PARSE_CTX_INIT; |
| 82 |
|
| config_memory_parse_data parse_data; |
| 83 |
|
| int error; |
| 84 |
|
| |
| 85 |
0 |
| GIT_UNUSED(repo); |
| 86 |
|
| |
| 87 |
0 |
▶ | if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr, |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_parser_init(&parser, "in-memory", memory_backend->cfg.ptr, |
0 |
0 |
0 |
|
| 88 |
|
| memory_backend->cfg.size)) < 0) |
| 89 |
0 |
| goto out; |
| 90 |
0 |
| parse_data.entries = memory_backend->entries; |
| 91 |
0 |
| parse_data.level = level; |
| 92 |
|
| |
| 93 |
0 |
▶ | if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_parse(&parser, NULL, read_variable_cb, NULL, NULL, &parse_data)) < 0) |
0 |
0 |
0 |
|
| 94 |
0 |
| goto out; |
| 95 |
|
| |
| 96 |
0 |
| out: |
| 97 |
0 |
| git_config_parser_dispose(&parser); |
| 98 |
0 |
| return error; |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
0 |
| static int config_memory_get(git_config_backend *backend, const char *key, git_config_entry **out) |
| 102 |
|
| { |
| 103 |
0 |
| config_memory_backend *memory_backend = (config_memory_backend *) backend; |
| 104 |
0 |
| return git_config_entries_get(out, memory_backend->entries, key); |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
0 |
| static int config_memory_iterator( |
| 108 |
|
| git_config_iterator **iter, |
| 109 |
|
| git_config_backend *backend) |
| 110 |
|
| { |
| 111 |
0 |
| config_memory_backend *memory_backend = (config_memory_backend *) backend; |
| 112 |
|
| git_config_entries *entries; |
| 113 |
|
| int error; |
| 114 |
|
| |
| 115 |
0 |
▶ | if ((error = git_config_entries_dup(&entries, memory_backend->entries)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_entries_dup(&entries, memory_backend->entries)) < 0) |
0 |
0 |
0 |
|
| 116 |
0 |
| goto out; |
| 117 |
|
| |
| 118 |
0 |
▶ | if ((error = git_config_entries_iterator_new(iter, entries)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((error = git_config_entries_iterator_new(iter, entries)) < 0) |
0 |
0 |
0 |
|
| 119 |
0 |
| goto out; |
| 120 |
|
| |
| 121 |
0 |
| out: |
| 122 |
|
| /* Let iterator delete duplicated entries when it's done */ |
| 123 |
0 |
| git_config_entries_free(entries); |
| 124 |
0 |
| return error; |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
0 |
| static int config_memory_set(git_config_backend *backend, const char *name, const char *value) |
| 128 |
|
| { |
| 129 |
0 |
| GIT_UNUSED(backend); |
| 130 |
0 |
| GIT_UNUSED(name); |
| 131 |
0 |
| GIT_UNUSED(value); |
| 132 |
0 |
| return config_error_readonly(); |
| 133 |
|
| } |
| 134 |
|
| |
| 135 |
0 |
| static int config_memory_set_multivar( |
| 136 |
|
| git_config_backend *backend, const char *name, const char *regexp, const char *value) |
| 137 |
|
| { |
| 138 |
0 |
| GIT_UNUSED(backend); |
| 139 |
0 |
| GIT_UNUSED(name); |
| 140 |
0 |
| GIT_UNUSED(regexp); |
| 141 |
0 |
| GIT_UNUSED(value); |
| 142 |
0 |
| return config_error_readonly(); |
| 143 |
|
| } |
| 144 |
|
| |
| 145 |
0 |
| static int config_memory_delete(git_config_backend *backend, const char *name) |
| 146 |
|
| { |
| 147 |
0 |
| GIT_UNUSED(backend); |
| 148 |
0 |
| GIT_UNUSED(name); |
| 149 |
0 |
| return config_error_readonly(); |
| 150 |
|
| } |
| 151 |
|
| |
| 152 |
0 |
| static int config_memory_delete_multivar(git_config_backend *backend, const char *name, const char *regexp) |
| 153 |
|
| { |
| 154 |
0 |
| GIT_UNUSED(backend); |
| 155 |
0 |
| GIT_UNUSED(name); |
| 156 |
0 |
| GIT_UNUSED(regexp); |
| 157 |
0 |
| return config_error_readonly(); |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
0 |
| static int config_memory_lock(git_config_backend *backend) |
| 161 |
|
| { |
| 162 |
0 |
| GIT_UNUSED(backend); |
| 163 |
0 |
| return config_error_readonly(); |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
0 |
| static int config_memory_unlock(git_config_backend *backend, int success) |
| 167 |
|
| { |
| 168 |
0 |
| GIT_UNUSED(backend); |
| 169 |
0 |
| GIT_UNUSED(success); |
| 170 |
0 |
| return config_error_readonly(); |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
0 |
| static void config_memory_free(git_config_backend *_backend) |
| 174 |
|
| { |
| 175 |
0 |
| config_memory_backend *backend = (config_memory_backend *)_backend; |
| 176 |
|
| |
| 177 |
0 |
▶ | if (backend == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (backend == NULL) |
0 |
0 |
0 |
|
| 178 |
0 |
| return; |
| 179 |
|
| |
| 180 |
0 |
| git_config_entries_free(backend->entries); |
| 181 |
0 |
| git_str_dispose(&backend->cfg); |
| 182 |
0 |
| git__free(backend); |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
0 |
| int git_config_backend_from_string(git_config_backend **out, const char *cfg, size_t len) |
| 186 |
|
| { |
| 187 |
|
| config_memory_backend *backend; |
| 188 |
|
| |
| 189 |
0 |
| backend = git__calloc(1, sizeof(config_memory_backend)); |
| 190 |
0 |
▶ | GIT_ERROR_CHECK_ALLOC(backend); |
Branch: 1 branch
| Branch | True | False | Total |
| GIT_ERROR_CHECK_ALLOC(backend); |
0 |
0 |
0 |
|
| 191 |
|
| |
| 192 |
0 |
▶ | if (git_config_entries_new(&backend->entries) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_config_entries_new(&backend->entries) < 0) { |
0 |
0 |
0 |
|
| 193 |
0 |
| git__free(backend); |
| 194 |
0 |
| return -1; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
0 |
▶ | if (git_str_set(&backend->cfg, cfg, len) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_set(&backend->cfg, cfg, len) < 0) { |
0 |
0 |
0 |
|
| 198 |
0 |
| git_config_entries_free(backend->entries); |
| 199 |
0 |
| git__free(backend); |
| 200 |
0 |
| return -1; |
| 201 |
|
| } |
| 202 |
|
| |
| 203 |
0 |
| backend->parent.version = GIT_CONFIG_BACKEND_VERSION; |
| 204 |
0 |
| backend->parent.readonly = 1; |
| 205 |
0 |
| backend->parent.open = config_memory_open; |
| 206 |
0 |
| backend->parent.get = config_memory_get; |
| 207 |
0 |
| backend->parent.set = config_memory_set; |
| 208 |
0 |
| backend->parent.set_multivar = config_memory_set_multivar; |
| 209 |
0 |
| backend->parent.del = config_memory_delete; |
| 210 |
0 |
| backend->parent.del_multivar = config_memory_delete_multivar; |
| 211 |
0 |
| backend->parent.iterator = config_memory_iterator; |
| 212 |
0 |
| backend->parent.lock = config_memory_lock; |
| 213 |
0 |
| backend->parent.unlock = config_memory_unlock; |
| 214 |
0 |
| backend->parent.snapshot = git_config_backend_snapshot; |
| 215 |
0 |
| backend->parent.free = config_memory_free; |
| 216 |
|
| |
| 217 |
0 |
| *out = (git_config_backend *)backend; |
| 218 |
|
| |
| 219 |
0 |
| return 0; |
| 220 |
|
| } |