Line | Count | Source |
1 | | #include "git-compat-util.h" |
2 | | #include "hash.h" |
3 | | #include "path.h" |
4 | | #include "object-file.h" |
5 | | #include "odb.h" |
6 | | #include "odb/source-files.h" |
7 | | #include "hex.h" |
8 | | #include "repository.h" |
9 | | #include "wrapper.h" |
10 | | #include "gettext.h" |
11 | | #include "loose.h" |
12 | | #include "lockfile.h" |
13 | | #include "oidtree.h" |
14 | | |
15 | | static const char *loose_object_header = "# loose-object-idx\n"; |
16 | | |
17 | 0 | #define LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS 1000 |
18 | | |
19 | | static inline int should_use_loose_object_map(struct repository *repo) |
20 | 0 | { |
21 | 0 | return repo->compat_hash_algo && repo->gitdir; |
22 | 0 | } |
23 | | |
24 | | void loose_object_map_init(struct loose_object_map **map) |
25 | 0 | { |
26 | 0 | struct loose_object_map *m; |
27 | 0 | m = xmalloc(sizeof(**map)); |
28 | 0 | m->to_compat = kh_init_oid_map(); |
29 | 0 | m->to_storage = kh_init_oid_map(); |
30 | 0 | *map = m; |
31 | 0 | } |
32 | | |
33 | | static int insert_oid_pair(kh_oid_map_t *map, const struct object_id *key, const struct object_id *value) |
34 | 0 | { |
35 | 0 | khiter_t pos; |
36 | 0 | int ret; |
37 | 0 | struct object_id *stored; |
38 | |
|
39 | 0 | pos = kh_put_oid_map(map, *key, &ret); |
40 | | |
41 | | /* This item already exists in the map. */ |
42 | 0 | if (ret == 0) |
43 | 0 | return 0; |
44 | | |
45 | 0 | stored = xmalloc(sizeof(*stored)); |
46 | 0 | oidcpy(stored, value); |
47 | 0 | kh_value(map, pos) = stored; |
48 | 0 | return 1; |
49 | 0 | } |
50 | | |
51 | | static int insert_loose_map(struct loose_object_map *map, |
52 | | struct oidtree *cache, |
53 | | const struct object_id *oid, |
54 | | const struct object_id *compat_oid) |
55 | 0 | { |
56 | 0 | int inserted = 0; |
57 | |
|
58 | 0 | inserted |= insert_oid_pair(map->to_compat, oid, compat_oid); |
59 | 0 | inserted |= insert_oid_pair(map->to_storage, compat_oid, oid); |
60 | 0 | if (inserted && cache) |
61 | 0 | oidtree_insert(cache, compat_oid, NULL); |
62 | |
|
63 | 0 | return inserted; |
64 | 0 | } |
65 | | |
66 | | static int loose_object_map_read(struct repository *repo, const char *path, |
67 | | struct loose_object_map *map, |
68 | | struct oidtree *cache) |
69 | 0 | { |
70 | 0 | struct strbuf buf = STRBUF_INIT; |
71 | 0 | FILE *fp; |
72 | 0 | int ret; |
73 | |
|
74 | 0 | fp = fopen(path, "rb"); |
75 | 0 | if (!fp) { |
76 | 0 | if (errno == ENOENT) |
77 | 0 | return 0; |
78 | 0 | return -1; |
79 | 0 | } |
80 | | |
81 | 0 | if (strbuf_getwholeline(&buf, fp, '\n') || strcmp(buf.buf, loose_object_header)) { |
82 | 0 | ret = -1; |
83 | 0 | goto out; |
84 | 0 | } |
85 | 0 | while (!strbuf_getline_lf(&buf, fp)) { |
86 | 0 | const char *p; |
87 | 0 | struct object_id oid, compat_oid; |
88 | 0 | if (parse_oid_hex_algop(buf.buf, &oid, &p, repo->hash_algo) || |
89 | 0 | *p++ != ' ' || |
90 | 0 | parse_oid_hex_algop(p, &compat_oid, &p, repo->compat_hash_algo) || |
91 | 0 | p != buf.buf + buf.len) { |
92 | 0 | ret = -1; |
93 | 0 | goto out; |
94 | 0 | } |
95 | 0 | insert_loose_map(map, cache, &oid, &compat_oid); |
96 | 0 | } |
97 | | |
98 | 0 | ret = ferror(fp) ? -1 : 0; |
99 | |
|
100 | 0 | out: |
101 | 0 | strbuf_release(&buf); |
102 | 0 | fclose(fp); |
103 | |
|
104 | 0 | return ret; |
105 | 0 | } |
106 | | |
107 | | int loose_object_map_load(struct odb_source_loose *loose) |
108 | 0 | { |
109 | 0 | struct repository *repo = loose->base.odb->repo; |
110 | 0 | char *path; |
111 | 0 | int ret; |
112 | |
|
113 | 0 | if (!should_use_loose_object_map(repo)) |
114 | 0 | return 0; |
115 | | |
116 | 0 | if (!loose->map) |
117 | 0 | loose_object_map_init(&loose->map); |
118 | 0 | if (!loose->cache) { |
119 | 0 | ALLOC_ARRAY(loose->cache, 1); |
120 | 0 | oidtree_init(loose->cache); |
121 | 0 | } |
122 | |
|
123 | 0 | insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_tree, |
124 | 0 | repo->compat_hash_algo->empty_tree); |
125 | 0 | insert_loose_map(loose->map, loose->cache, repo->hash_algo->empty_blob, |
126 | 0 | repo->compat_hash_algo->empty_blob); |
127 | 0 | insert_loose_map(loose->map, loose->cache, repo->hash_algo->null_oid, |
128 | 0 | repo->compat_hash_algo->null_oid); |
129 | |
|
130 | 0 | path = xstrfmt("%s/loose-object-idx", loose->base.path); |
131 | 0 | ret = loose_object_map_read(repo, path, loose->map, loose->cache); |
132 | |
|
133 | 0 | free(path); |
134 | 0 | return ret; |
135 | 0 | } |
136 | | |
137 | | int repo_read_loose_object_map(struct repository *repo) |
138 | 0 | { |
139 | 0 | struct odb_source_files *files = odb_source_files_downcast(repo->objects->source); |
140 | |
|
141 | 0 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
142 | 0 | if (loose_object_map_load(dir->loose) < 0) |
143 | 0 | return -1; |
144 | 0 | } |
145 | | |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | | static int loose_object_map_write(struct repository *repo, |
150 | | struct loose_object_map *loose_map, int fd) |
151 | 0 | { |
152 | 0 | kh_oid_map_t *map = loose_map->to_compat; |
153 | 0 | struct strbuf buf = STRBUF_INIT; |
154 | 0 | khiter_t iter; |
155 | 0 | int ret = -1; |
156 | |
|
157 | 0 | iter = kh_begin(map); |
158 | 0 | if (write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0) |
159 | 0 | goto out; |
160 | | |
161 | 0 | for (; iter != kh_end(map); iter++) { |
162 | 0 | if (kh_exist(map, iter)) { |
163 | 0 | if (oideq(&kh_key(map, iter), repo->hash_algo->empty_tree) || |
164 | 0 | oideq(&kh_key(map, iter), repo->hash_algo->empty_blob)) |
165 | 0 | continue; |
166 | 0 | strbuf_addf(&buf, "%s %s\n", oid_to_hex(&kh_key(map, iter)), oid_to_hex(kh_value(map, iter))); |
167 | 0 | if (write_in_full(fd, buf.buf, buf.len) < 0) |
168 | 0 | goto out; |
169 | 0 | strbuf_reset(&buf); |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | ret = 0; |
174 | |
|
175 | 0 | out: |
176 | 0 | strbuf_release(&buf); |
177 | |
|
178 | 0 | return ret; |
179 | 0 | } |
180 | | |
181 | | static int write_one_object(struct odb_source_loose *loose, |
182 | | const struct object_id *oid, |
183 | | const struct object_id *compat_oid) |
184 | 0 | { |
185 | 0 | struct lock_file lock; |
186 | 0 | int fd; |
187 | 0 | struct stat st; |
188 | 0 | struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT; |
189 | |
|
190 | 0 | strbuf_addf(&path, "%s/loose-object-idx", loose->base.path); |
191 | 0 | repo_hold_lock_file_for_update_timeout(loose->base.odb->repo, &lock, |
192 | 0 | path.buf, LOCK_DIE_ON_ERROR, |
193 | 0 | LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS); |
194 | |
|
195 | 0 | fd = open(path.buf, O_WRONLY | O_CREAT | O_APPEND, 0666); |
196 | 0 | if (fd < 0) |
197 | 0 | goto errout; |
198 | 0 | if (fstat(fd, &st) < 0) |
199 | 0 | goto errout; |
200 | 0 | if (!st.st_size && write_in_full(fd, loose_object_header, strlen(loose_object_header)) < 0) |
201 | 0 | goto errout; |
202 | | |
203 | 0 | strbuf_addf(&buf, "%s %s\n", oid_to_hex(oid), oid_to_hex(compat_oid)); |
204 | 0 | if (write_in_full(fd, buf.buf, buf.len) < 0) |
205 | 0 | goto errout; |
206 | 0 | if (close(fd)) |
207 | 0 | goto errout; |
208 | 0 | adjust_shared_perm(loose->base.odb->repo, path.buf); |
209 | 0 | rollback_lock_file(&lock); |
210 | 0 | strbuf_release(&buf); |
211 | 0 | strbuf_release(&path); |
212 | 0 | return 0; |
213 | 0 | errout: |
214 | 0 | error_errno(_("failed to write loose object index %s"), path.buf); |
215 | 0 | if (fd >= 0) |
216 | 0 | close(fd); |
217 | 0 | rollback_lock_file(&lock); |
218 | 0 | strbuf_release(&buf); |
219 | 0 | strbuf_release(&path); |
220 | 0 | return -1; |
221 | 0 | } |
222 | | |
223 | | int repo_add_loose_object_map(struct odb_source_loose *loose, |
224 | | const struct object_id *oid, |
225 | | const struct object_id *compat_oid) |
226 | 0 | { |
227 | 0 | int inserted = 0; |
228 | |
|
229 | 0 | if (!should_use_loose_object_map(loose->base.odb->repo)) |
230 | 0 | return 0; |
231 | | |
232 | 0 | inserted = insert_loose_map(loose->map, loose->cache, oid, compat_oid); |
233 | 0 | if (inserted) |
234 | 0 | return write_one_object(loose, oid, compat_oid); |
235 | 0 | return 0; |
236 | 0 | } |
237 | | |
238 | | int repo_loose_object_map_oid(struct repository *repo, |
239 | | const struct object_id *src, |
240 | | const struct git_hash_algo *to, |
241 | | struct object_id *dest) |
242 | 0 | { |
243 | 0 | struct odb_source_files *files = odb_source_files_downcast(repo->objects->source); |
244 | 0 | kh_oid_map_t *map; |
245 | 0 | khiter_t pos; |
246 | |
|
247 | 0 | for (struct odb_files_dir *dir = files->dirs; dir; dir = dir->next) { |
248 | 0 | struct loose_object_map *loose_map = dir->loose->map; |
249 | 0 | if (!loose_map) |
250 | 0 | continue; |
251 | 0 | map = (to == repo->compat_hash_algo) ? |
252 | 0 | loose_map->to_compat : |
253 | 0 | loose_map->to_storage; |
254 | 0 | pos = kh_get_oid_map(map, *src); |
255 | 0 | if (pos < kh_end(map)) { |
256 | 0 | oidcpy(dest, kh_value(map, pos)); |
257 | 0 | return 0; |
258 | 0 | } |
259 | 0 | } |
260 | 0 | return -1; |
261 | 0 | } |
262 | | |
263 | | void loose_object_map_clear(struct loose_object_map **map) |
264 | 0 | { |
265 | 0 | struct loose_object_map *m = *map; |
266 | 0 | struct object_id *oid; |
267 | |
|
268 | 0 | if (!m) |
269 | 0 | return; |
270 | | |
271 | 0 | kh_foreach_value(m->to_compat, oid, free(oid)); |
272 | 0 | kh_foreach_value(m->to_storage, oid, free(oid)); |
273 | 0 | kh_destroy_oid_map(m->to_compat); |
274 | 0 | kh_destroy_oid_map(m->to_storage); |
275 | 0 | free(m); |
276 | 0 | *map = NULL; |
277 | 0 | } |
278 | | |
279 | | int loose_object_idx_merge(struct repository *repo, const char *src, |
280 | | const char *dst) |
281 | 0 | { |
282 | 0 | struct lock_file lock = LOCK_INIT; |
283 | 0 | struct loose_object_map *map; |
284 | 0 | int ret; |
285 | 0 | int fd; |
286 | |
|
287 | 0 | loose_object_map_init(&map); |
288 | |
|
289 | 0 | if (loose_object_map_read(repo, src, map, NULL) < 0) { |
290 | 0 | ret = error(_("unable to read loose object index %s"), src); |
291 | 0 | goto out; |
292 | 0 | } |
293 | | |
294 | 0 | fd = repo_hold_lock_file_for_update_timeout(repo, &lock, dst, 0, |
295 | 0 | LOOSE_OBJECT_IDX_LOCK_TIMEOUT_MS); |
296 | 0 | if (fd < 0) { |
297 | 0 | ret = error_errno(_("unable to lock %s"), dst); |
298 | 0 | goto out; |
299 | 0 | } |
300 | | |
301 | 0 | if (loose_object_map_read(repo, dst, map, NULL) < 0) { |
302 | 0 | ret = error(_("unable to read loose object index %s"), dst); |
303 | 0 | goto out; |
304 | 0 | } |
305 | | |
306 | 0 | if (loose_object_map_write(repo, map, fd) < 0) { |
307 | 0 | ret = error_errno(_("unable to write %s"), |
308 | 0 | get_lock_file_path(&lock)); |
309 | 0 | goto out; |
310 | 0 | } |
311 | | |
312 | 0 | if (commit_lock_file(&lock) < 0) { |
313 | 0 | ret = error_errno(_("unable to write %s"), dst); |
314 | 0 | goto out; |
315 | 0 | } |
316 | | |
317 | 0 | ret = 0; |
318 | |
|
319 | 0 | out: |
320 | 0 | loose_object_map_clear(&map); |
321 | 0 | rollback_lock_file(&lock); |
322 | |
|
323 | 0 | return ret; |
324 | 0 | } |