File: | tog/../lib/repository.c |
Warning: | line 720, column 18 Access to field 'pack_cache_size' results in a dereference of a null pointer (loaded from variable 'repo') |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* | |||
2 | * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org> | |||
3 | * | |||
4 | * Permission to use, copy, modify, and distribute this software for any | |||
5 | * purpose with or without fee is hereby granted, provided that the above | |||
6 | * copyright notice and this permission notice appear in all copies. | |||
7 | * | |||
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |||
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |||
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |||
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |||
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |||
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |||
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |||
15 | */ | |||
16 | ||||
17 | #include <sys/types.h> | |||
18 | #include <sys/queue.h> | |||
19 | #include <sys/uio.h> | |||
20 | #include <sys/socket.h> | |||
21 | #include <sys/stat.h> | |||
22 | #include <sys/mman.h> | |||
23 | #include <sys/resource.h> | |||
24 | ||||
25 | #include <ctype.h> | |||
26 | #include <endian.h> | |||
27 | #include <fcntl.h> | |||
28 | #include <fnmatch.h> | |||
29 | #include <limits.h> | |||
30 | #include <dirent.h> | |||
31 | #include <stdlib.h> | |||
32 | #include <stdio.h> | |||
33 | #include <sha1.h> | |||
34 | #include <string.h> | |||
35 | #include <time.h> | |||
36 | #include <unistd.h> | |||
37 | #include <zlib.h> | |||
38 | #include <errno(*__errno()).h> | |||
39 | #include <libgen.h> | |||
40 | #include <stdint.h> | |||
41 | #include <imsg.h> | |||
42 | #include <uuid.h> | |||
43 | ||||
44 | #include "got_error.h" | |||
45 | #include "got_reference.h" | |||
46 | #include "got_repository.h" | |||
47 | #include "got_path.h" | |||
48 | #include "got_cancel.h" | |||
49 | #include "got_object.h" | |||
50 | ||||
51 | #include "got_lib_delta.h" | |||
52 | #include "got_lib_inflate.h" | |||
53 | #include "got_lib_object.h" | |||
54 | #include "got_lib_object_parse.h" | |||
55 | #include "got_lib_object_create.h" | |||
56 | #include "got_lib_pack.h" | |||
57 | #include "got_lib_privsep.h" | |||
58 | #include "got_lib_sha1.h" | |||
59 | #include "got_lib_object_cache.h" | |||
60 | #include "got_lib_repository.h" | |||
61 | #include "got_lib_gotconfig.h" | |||
62 | ||||
63 | #ifndef nitems | |||
64 | #define nitems(_a)(sizeof(_a) / sizeof((_a)[0])) (sizeof(_a) / sizeof((_a)[0])) | |||
65 | #endif | |||
66 | ||||
67 | const char * | |||
68 | got_repo_get_path(struct got_repository *repo) | |||
69 | { | |||
70 | return repo->path; | |||
71 | } | |||
72 | ||||
73 | const char * | |||
74 | got_repo_get_path_git_dir(struct got_repository *repo) | |||
75 | { | |||
76 | return repo->path_git_dir; | |||
77 | } | |||
78 | ||||
79 | int | |||
80 | got_repo_get_fd(struct got_repository *repo) | |||
81 | { | |||
82 | return repo->gitdir_fd; | |||
83 | } | |||
84 | ||||
85 | const char * | |||
86 | got_repo_get_gitconfig_author_name(struct got_repository *repo) | |||
87 | { | |||
88 | return repo->gitconfig_author_name; | |||
89 | } | |||
90 | ||||
91 | const char * | |||
92 | got_repo_get_gitconfig_author_email(struct got_repository *repo) | |||
93 | { | |||
94 | return repo->gitconfig_author_email; | |||
95 | } | |||
96 | ||||
97 | const char * | |||
98 | got_repo_get_global_gitconfig_author_name(struct got_repository *repo) | |||
99 | { | |||
100 | return repo->global_gitconfig_author_name; | |||
101 | } | |||
102 | ||||
103 | const char * | |||
104 | got_repo_get_global_gitconfig_author_email(struct got_repository *repo) | |||
105 | { | |||
106 | return repo->global_gitconfig_author_email; | |||
107 | } | |||
108 | ||||
109 | const char * | |||
110 | got_repo_get_gitconfig_owner(struct got_repository *repo) | |||
111 | { | |||
112 | return repo->gitconfig_owner; | |||
113 | } | |||
114 | ||||
115 | int | |||
116 | got_repo_is_bare(struct got_repository *repo) | |||
117 | { | |||
118 | return (strcmp(repo->path, repo->path_git_dir) == 0); | |||
119 | } | |||
120 | ||||
121 | static char * | |||
122 | get_path_git_child(struct got_repository *repo, const char *basename) | |||
123 | { | |||
124 | char *path_child; | |||
125 | ||||
126 | if (asprintf(&path_child, "%s/%s", repo->path_git_dir, | |||
127 | basename) == -1) | |||
128 | return NULL((void *)0); | |||
129 | ||||
130 | return path_child; | |||
131 | } | |||
132 | ||||
133 | char * | |||
134 | got_repo_get_path_objects(struct got_repository *repo) | |||
135 | { | |||
136 | return get_path_git_child(repo, GOT_OBJECTS_DIR"objects"); | |||
137 | } | |||
138 | ||||
139 | char * | |||
140 | got_repo_get_path_objects_pack(struct got_repository *repo) | |||
141 | { | |||
142 | return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR"objects/pack"); | |||
143 | } | |||
144 | ||||
145 | char * | |||
146 | got_repo_get_path_refs(struct got_repository *repo) | |||
147 | { | |||
148 | return get_path_git_child(repo, GOT_REFS_DIR"refs"); | |||
149 | } | |||
150 | ||||
151 | char * | |||
152 | got_repo_get_path_packed_refs(struct got_repository *repo) | |||
153 | { | |||
154 | return get_path_git_child(repo, GOT_PACKED_REFS_FILE"packed-refs"); | |||
155 | } | |||
156 | ||||
157 | static char * | |||
158 | get_path_head(struct got_repository *repo) | |||
159 | { | |||
160 | return get_path_git_child(repo, GOT_HEAD_FILE"HEAD"); | |||
161 | } | |||
162 | ||||
163 | char * | |||
164 | got_repo_get_path_gitconfig(struct got_repository *repo) | |||
165 | { | |||
166 | return get_path_git_child(repo, GOT_GITCONFIG"config"); | |||
167 | } | |||
168 | ||||
169 | char * | |||
170 | got_repo_get_path_gotconfig(struct got_repository *repo) | |||
171 | { | |||
172 | return get_path_git_child(repo, GOT_GOTCONFIG_FILENAME"got.conf"); | |||
173 | } | |||
174 | ||||
175 | const struct got_gotconfig * | |||
176 | got_repo_get_gotconfig(struct got_repository *repo) | |||
177 | { | |||
178 | return repo->gotconfig; | |||
179 | } | |||
180 | ||||
181 | void | |||
182 | got_repo_get_gitconfig_remotes(int *nremotes, | |||
183 | const struct got_remote_repo **remotes, struct got_repository *repo) | |||
184 | { | |||
185 | *nremotes = repo->ngitconfig_remotes; | |||
186 | *remotes = repo->gitconfig_remotes; | |||
187 | } | |||
188 | ||||
189 | static int | |||
190 | is_git_repo(struct got_repository *repo) | |||
191 | { | |||
192 | const char *path_git = got_repo_get_path_git_dir(repo); | |||
193 | char *path_objects = got_repo_get_path_objects(repo); | |||
194 | char *path_refs = got_repo_get_path_refs(repo); | |||
195 | char *path_head = get_path_head(repo); | |||
196 | int ret = 0; | |||
197 | struct stat sb; | |||
198 | struct got_reference *head_ref; | |||
199 | ||||
200 | if (lstat(path_git, &sb) == -1) | |||
201 | goto done; | |||
202 | if (!S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) | |||
203 | goto done; | |||
204 | ||||
205 | if (lstat(path_objects, &sb) == -1) | |||
206 | goto done; | |||
207 | if (!S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) | |||
208 | goto done; | |||
209 | ||||
210 | if (lstat(path_refs, &sb) == -1) | |||
211 | goto done; | |||
212 | if (!S_ISDIR(sb.st_mode)((sb.st_mode & 0170000) == 0040000)) | |||
213 | goto done; | |||
214 | ||||
215 | if (lstat(path_head, &sb) == -1) | |||
216 | goto done; | |||
217 | if (!S_ISREG(sb.st_mode)((sb.st_mode & 0170000) == 0100000)) | |||
218 | goto done; | |||
219 | ||||
220 | /* Check if the HEAD reference can be opened. */ | |||
221 | if (got_ref_open(&head_ref, repo, GOT_REF_HEAD"HEAD", 0) != NULL((void *)0)) | |||
222 | goto done; | |||
223 | got_ref_close(head_ref); | |||
224 | ||||
225 | ret = 1; | |||
226 | done: | |||
227 | free(path_objects); | |||
228 | free(path_refs); | |||
229 | free(path_head); | |||
230 | return ret; | |||
231 | ||||
232 | } | |||
233 | ||||
234 | const struct got_error * | |||
235 | got_repo_cache_object(struct got_repository *repo, struct got_object_id *id, | |||
236 | struct got_object *obj) | |||
237 | { | |||
238 | #ifndef GOT_NO_OBJ_CACHE | |||
239 | const struct got_error *err = NULL((void *)0); | |||
240 | err = got_object_cache_add(&repo->objcache, id, obj); | |||
241 | if (err) { | |||
242 | if (err->code == GOT_ERR_OBJ_EXISTS43 || | |||
243 | err->code == GOT_ERR_OBJ_TOO_LARGE78) | |||
244 | err = NULL((void *)0); | |||
245 | return err; | |||
246 | } | |||
247 | obj->refcnt++; | |||
248 | #endif | |||
249 | return NULL((void *)0); | |||
250 | } | |||
251 | ||||
252 | struct got_object * | |||
253 | got_repo_get_cached_object(struct got_repository *repo, | |||
254 | struct got_object_id *id) | |||
255 | { | |||
256 | return (struct got_object *)got_object_cache_get(&repo->objcache, id); | |||
257 | } | |||
258 | ||||
259 | const struct got_error * | |||
260 | got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id, | |||
261 | struct got_tree_object *tree) | |||
262 | { | |||
263 | #ifndef GOT_NO_OBJ_CACHE | |||
264 | const struct got_error *err = NULL((void *)0); | |||
265 | err = got_object_cache_add(&repo->treecache, id, tree); | |||
266 | if (err) { | |||
267 | if (err->code == GOT_ERR_OBJ_EXISTS43 || | |||
268 | err->code == GOT_ERR_OBJ_TOO_LARGE78) | |||
269 | err = NULL((void *)0); | |||
270 | return err; | |||
271 | } | |||
272 | tree->refcnt++; | |||
273 | #endif | |||
274 | return NULL((void *)0); | |||
275 | } | |||
276 | ||||
277 | struct got_tree_object * | |||
278 | got_repo_get_cached_tree(struct got_repository *repo, | |||
279 | struct got_object_id *id) | |||
280 | { | |||
281 | return (struct got_tree_object *)got_object_cache_get( | |||
282 | &repo->treecache, id); | |||
283 | } | |||
284 | ||||
285 | const struct got_error * | |||
286 | got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id, | |||
287 | struct got_commit_object *commit) | |||
288 | { | |||
289 | #ifndef GOT_NO_OBJ_CACHE | |||
290 | const struct got_error *err = NULL((void *)0); | |||
291 | err = got_object_cache_add(&repo->commitcache, id, commit); | |||
292 | if (err) { | |||
293 | if (err->code == GOT_ERR_OBJ_EXISTS43 || | |||
294 | err->code == GOT_ERR_OBJ_TOO_LARGE78) | |||
295 | err = NULL((void *)0); | |||
296 | return err; | |||
297 | } | |||
298 | commit->refcnt++; | |||
299 | #endif | |||
300 | return NULL((void *)0); | |||
301 | } | |||
302 | ||||
303 | struct got_commit_object * | |||
304 | got_repo_get_cached_commit(struct got_repository *repo, | |||
305 | struct got_object_id *id) | |||
306 | { | |||
307 | return (struct got_commit_object *)got_object_cache_get( | |||
308 | &repo->commitcache, id); | |||
309 | } | |||
310 | ||||
311 | const struct got_error * | |||
312 | got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id, | |||
313 | struct got_tag_object *tag) | |||
314 | { | |||
315 | #ifndef GOT_NO_OBJ_CACHE | |||
316 | const struct got_error *err = NULL((void *)0); | |||
317 | err = got_object_cache_add(&repo->tagcache, id, tag); | |||
318 | if (err) { | |||
319 | if (err->code == GOT_ERR_OBJ_EXISTS43 || | |||
320 | err->code == GOT_ERR_OBJ_TOO_LARGE78) | |||
321 | err = NULL((void *)0); | |||
322 | return err; | |||
323 | } | |||
324 | tag->refcnt++; | |||
325 | #endif | |||
326 | return NULL((void *)0); | |||
327 | } | |||
328 | ||||
329 | struct got_tag_object * | |||
330 | got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id) | |||
331 | { | |||
332 | return (struct got_tag_object *)got_object_cache_get( | |||
333 | &repo->tagcache, id); | |||
334 | } | |||
335 | ||||
336 | const struct got_error * | |||
337 | open_repo(struct got_repository *repo, const char *path) | |||
338 | { | |||
339 | const struct got_error *err = NULL((void *)0); | |||
340 | ||||
341 | repo->gitdir_fd = -1; | |||
342 | ||||
343 | /* bare git repository? */ | |||
344 | repo->path_git_dir = strdup(path); | |||
345 | if (repo->path_git_dir == NULL((void *)0)) | |||
346 | return got_error_from_errno("strdup"); | |||
347 | if (is_git_repo(repo)) { | |||
348 | repo->path = strdup(repo->path_git_dir); | |||
349 | if (repo->path == NULL((void *)0)) { | |||
350 | err = got_error_from_errno("strdup"); | |||
351 | goto done; | |||
352 | } | |||
353 | repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY0x20000); | |||
354 | if (repo->gitdir_fd == -1) { | |||
355 | err = got_error_from_errno2("open", | |||
356 | repo->path_git_dir); | |||
357 | goto done; | |||
358 | } | |||
359 | return NULL((void *)0); | |||
360 | } | |||
361 | ||||
362 | /* git repository with working tree? */ | |||
363 | free(repo->path_git_dir); | |||
364 | repo->path_git_dir = NULL((void *)0); | |||
365 | if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR".git") == -1) { | |||
366 | err = got_error_from_errno("asprintf"); | |||
367 | goto done; | |||
368 | } | |||
369 | if (is_git_repo(repo)) { | |||
370 | repo->path = strdup(path); | |||
371 | if (repo->path == NULL((void *)0)) { | |||
372 | err = got_error_from_errno("strdup"); | |||
373 | goto done; | |||
374 | } | |||
375 | repo->gitdir_fd = open(repo->path_git_dir, O_DIRECTORY0x20000); | |||
376 | if (repo->gitdir_fd == -1) { | |||
377 | err = got_error_from_errno2("open", | |||
378 | repo->path_git_dir); | |||
379 | goto done; | |||
380 | } | |||
381 | return NULL((void *)0); | |||
382 | } | |||
383 | ||||
384 | err = got_error(GOT_ERR_NOT_GIT_REPO2); | |||
385 | done: | |||
386 | if (err) { | |||
387 | free(repo->path); | |||
388 | repo->path = NULL((void *)0); | |||
389 | free(repo->path_git_dir); | |||
390 | repo->path_git_dir = NULL((void *)0); | |||
391 | if (repo->gitdir_fd != -1) | |||
392 | close(repo->gitdir_fd); | |||
393 | repo->gitdir_fd = -1; | |||
394 | ||||
395 | } | |||
396 | return err; | |||
397 | } | |||
398 | ||||
399 | static const struct got_error * | |||
400 | parse_gitconfig_file(int *gitconfig_repository_format_version, | |||
401 | char **gitconfig_author_name, char **gitconfig_author_email, | |||
402 | struct got_remote_repo **remotes, int *nremotes, | |||
403 | char **gitconfig_owner, char ***extensions, int *nextensions, | |||
404 | const char *gitconfig_path) | |||
405 | { | |||
406 | const struct got_error *err = NULL((void *)0), *child_err = NULL((void *)0); | |||
407 | int fd = -1; | |||
408 | int imsg_fds[2] = { -1, -1 }; | |||
409 | pid_t pid; | |||
410 | struct imsgbuf *ibuf; | |||
411 | ||||
412 | *gitconfig_repository_format_version = 0; | |||
413 | if (extensions) | |||
414 | *extensions = NULL((void *)0); | |||
415 | if (nextensions) | |||
416 | *nextensions = 0; | |||
417 | *gitconfig_author_name = NULL((void *)0); | |||
418 | *gitconfig_author_email = NULL((void *)0); | |||
419 | if (remotes) | |||
420 | *remotes = NULL((void *)0); | |||
421 | if (nremotes) | |||
422 | *nremotes = 0; | |||
423 | if (gitconfig_owner) | |||
424 | *gitconfig_owner = NULL((void *)0); | |||
425 | ||||
426 | fd = open(gitconfig_path, O_RDONLY0x0000); | |||
427 | if (fd == -1) { | |||
428 | if (errno(*__errno()) == ENOENT2) | |||
429 | return NULL((void *)0); | |||
430 | return got_error_from_errno2("open", gitconfig_path); | |||
431 | } | |||
432 | ||||
433 | ibuf = calloc(1, sizeof(*ibuf)); | |||
434 | if (ibuf == NULL((void *)0)) { | |||
435 | err = got_error_from_errno("calloc"); | |||
436 | goto done; | |||
437 | } | |||
438 | ||||
439 | if (socketpair(AF_UNIX1, SOCK_STREAM1, PF_UNSPEC0, imsg_fds) == -1) { | |||
440 | err = got_error_from_errno("socketpair"); | |||
441 | goto done; | |||
442 | } | |||
443 | ||||
444 | pid = fork(); | |||
445 | if (pid == -1) { | |||
446 | err = got_error_from_errno("fork"); | |||
447 | goto done; | |||
448 | } else if (pid == 0) { | |||
449 | got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_GITCONFIG"/home/ben/bin" "/" "got-read-gitconfig", | |||
450 | gitconfig_path); | |||
451 | /* not reached */ | |||
452 | } | |||
453 | ||||
454 | if (close(imsg_fds[1]) == -1) { | |||
455 | err = got_error_from_errno("close"); | |||
456 | goto done; | |||
457 | } | |||
458 | imsg_fds[1] = -1; | |||
459 | imsg_init(ibuf, imsg_fds[0]); | |||
460 | ||||
461 | err = got_privsep_send_gitconfig_parse_req(ibuf, fd); | |||
462 | if (err) | |||
463 | goto done; | |||
464 | fd = -1; | |||
465 | ||||
466 | err = got_privsep_send_gitconfig_repository_format_version_req(ibuf); | |||
467 | if (err) | |||
468 | goto done; | |||
469 | ||||
470 | err = got_privsep_recv_gitconfig_int( | |||
471 | gitconfig_repository_format_version, ibuf); | |||
472 | if (err) | |||
473 | goto done; | |||
474 | ||||
475 | if (extensions && nextensions) { | |||
476 | err = got_privsep_send_gitconfig_repository_extensions_req( | |||
477 | ibuf); | |||
478 | if (err) | |||
479 | goto done; | |||
480 | err = got_privsep_recv_gitconfig_int(nextensions, ibuf); | |||
481 | if (err) | |||
482 | goto done; | |||
483 | if (*nextensions > 0) { | |||
484 | int i; | |||
485 | *extensions = calloc(*nextensions, sizeof(char *)); | |||
486 | if (*extensions == NULL((void *)0)) { | |||
487 | err = got_error_from_errno("calloc"); | |||
488 | goto done; | |||
489 | } | |||
490 | for (i = 0; i < *nextensions; i++) { | |||
491 | char *ext; | |||
492 | err = got_privsep_recv_gitconfig_str(&ext, | |||
493 | ibuf); | |||
494 | if (err) | |||
495 | goto done; | |||
496 | (*extensions)[i] = ext; | |||
497 | } | |||
498 | } | |||
499 | } | |||
500 | ||||
501 | err = got_privsep_send_gitconfig_author_name_req(ibuf); | |||
502 | if (err) | |||
503 | goto done; | |||
504 | ||||
505 | err = got_privsep_recv_gitconfig_str(gitconfig_author_name, ibuf); | |||
506 | if (err) | |||
507 | goto done; | |||
508 | ||||
509 | err = got_privsep_send_gitconfig_author_email_req(ibuf); | |||
510 | if (err) | |||
511 | goto done; | |||
512 | ||||
513 | err = got_privsep_recv_gitconfig_str(gitconfig_author_email, ibuf); | |||
514 | if (err) | |||
515 | goto done; | |||
516 | ||||
517 | if (remotes && nremotes) { | |||
518 | err = got_privsep_send_gitconfig_remotes_req(ibuf); | |||
519 | if (err) | |||
520 | goto done; | |||
521 | ||||
522 | err = got_privsep_recv_gitconfig_remotes(remotes, | |||
523 | nremotes, ibuf); | |||
524 | if (err) | |||
525 | goto done; | |||
526 | } | |||
527 | ||||
528 | if (gitconfig_owner) { | |||
529 | err = got_privsep_send_gitconfig_owner_req(ibuf); | |||
530 | if (err) | |||
531 | goto done; | |||
532 | err = got_privsep_recv_gitconfig_str(gitconfig_owner, ibuf); | |||
533 | if (err) | |||
534 | goto done; | |||
535 | } | |||
536 | ||||
537 | imsg_clear(ibuf); | |||
538 | err = got_privsep_send_stop(imsg_fds[0]); | |||
539 | child_err = got_privsep_wait_for_child(pid); | |||
540 | if (child_err && err == NULL((void *)0)) | |||
541 | err = child_err; | |||
542 | done: | |||
543 | if (imsg_fds[0] != -1 && close(imsg_fds[0]) == -1 && err == NULL((void *)0)) | |||
544 | err = got_error_from_errno("close"); | |||
545 | if (imsg_fds[1] != -1 && close(imsg_fds[1]) == -1 && err == NULL((void *)0)) | |||
546 | err = got_error_from_errno("close"); | |||
547 | if (fd != -1 && close(fd) == -1 && err == NULL((void *)0)) | |||
548 | err = got_error_from_errno2("close", gitconfig_path); | |||
549 | free(ibuf); | |||
550 | return err; | |||
551 | } | |||
552 | ||||
553 | static const struct got_error * | |||
554 | read_gitconfig(struct got_repository *repo, const char *global_gitconfig_path) | |||
555 | { | |||
556 | const struct got_error *err = NULL((void *)0); | |||
557 | char *repo_gitconfig_path = NULL((void *)0); | |||
558 | ||||
559 | if (global_gitconfig_path) { | |||
560 | /* Read settings from ~/.gitconfig. */ | |||
561 | int dummy_repo_version; | |||
562 | err = parse_gitconfig_file(&dummy_repo_version, | |||
563 | &repo->global_gitconfig_author_name, | |||
564 | &repo->global_gitconfig_author_email, | |||
565 | NULL((void *)0), NULL((void *)0), NULL((void *)0), NULL((void *)0), NULL((void *)0), global_gitconfig_path); | |||
566 | if (err) | |||
567 | return err; | |||
568 | } | |||
569 | ||||
570 | /* Read repository's .git/config file. */ | |||
571 | repo_gitconfig_path = got_repo_get_path_gitconfig(repo); | |||
572 | if (repo_gitconfig_path == NULL((void *)0)) | |||
573 | return got_error_from_errno("got_repo_get_path_gitconfig"); | |||
574 | ||||
575 | err = parse_gitconfig_file(&repo->gitconfig_repository_format_version, | |||
576 | &repo->gitconfig_author_name, &repo->gitconfig_author_email, | |||
577 | &repo->gitconfig_remotes, &repo->ngitconfig_remotes, | |||
578 | &repo->gitconfig_owner, &repo->extensions, &repo->nextensions, | |||
579 | repo_gitconfig_path); | |||
580 | if (err) | |||
581 | goto done; | |||
582 | done: | |||
583 | free(repo_gitconfig_path); | |||
584 | return err; | |||
585 | } | |||
586 | ||||
587 | static const struct got_error * | |||
588 | read_gotconfig(struct got_repository *repo) | |||
589 | { | |||
590 | const struct got_error *err = NULL((void *)0); | |||
591 | char *gotconfig_path; | |||
592 | ||||
593 | gotconfig_path = got_repo_get_path_gotconfig(repo); | |||
594 | if (gotconfig_path == NULL((void *)0)) | |||
595 | return got_error_from_errno("got_repo_get_path_gotconfig"); | |||
596 | ||||
597 | err = got_gotconfig_read(&repo->gotconfig, gotconfig_path); | |||
598 | free(gotconfig_path); | |||
599 | return err; | |||
600 | } | |||
601 | ||||
602 | /* Supported repository format extensions. */ | |||
603 | static const char *repo_extensions[] = { | |||
604 | "noop", /* Got supports repository format version 1. */ | |||
605 | "preciousObjects", /* Got has no garbage collection yet. */ | |||
606 | "worktreeConfig", /* Got does not care about Git work trees. */ | |||
607 | }; | |||
608 | ||||
609 | const struct got_error * | |||
610 | got_repo_open(struct got_repository **repop, const char *path, | |||
611 | const char *global_gitconfig_path) | |||
612 | { | |||
613 | struct got_repository *repo = NULL((void *)0); | |||
614 | const struct got_error *err = NULL((void *)0); | |||
615 | char *repo_path = NULL((void *)0); | |||
616 | size_t i; | |||
617 | struct rlimit rl; | |||
618 | ||||
619 | *repop = NULL((void *)0); | |||
620 | ||||
621 | if (getrlimit(RLIMIT_NOFILE8, &rl) == -1) | |||
| ||||
622 | return got_error_from_errno("getrlimit"); | |||
623 | ||||
624 | repo = calloc(1, sizeof(*repo)); | |||
625 | if (repo == NULL((void *)0)) { | |||
626 | err = got_error_from_errno("calloc"); | |||
627 | goto done; | |||
628 | } | |||
629 | ||||
630 | for (i = 0; i < nitems(repo->privsep_children)(sizeof(repo->privsep_children) / sizeof((repo->privsep_children )[0])); i++) { | |||
631 | memset(&repo->privsep_children[i], 0, | |||
632 | sizeof(repo->privsep_children[0])); | |||
633 | repo->privsep_children[i].imsg_fd = -1; | |||
634 | } | |||
635 | ||||
636 | err = got_object_cache_init(&repo->objcache, | |||
637 | GOT_OBJECT_CACHE_TYPE_OBJ); | |||
638 | if (err) | |||
639 | goto done; | |||
640 | err = got_object_cache_init(&repo->treecache, | |||
641 | GOT_OBJECT_CACHE_TYPE_TREE); | |||
642 | if (err) | |||
643 | goto done; | |||
644 | err = got_object_cache_init(&repo->commitcache, | |||
645 | GOT_OBJECT_CACHE_TYPE_COMMIT); | |||
646 | if (err) | |||
647 | goto done; | |||
648 | err = got_object_cache_init(&repo->tagcache, | |||
649 | GOT_OBJECT_CACHE_TYPE_TAG); | |||
650 | if (err) | |||
651 | goto done; | |||
652 | ||||
653 | repo->pack_cache_size = GOT_PACK_CACHE_SIZE64; | |||
654 | if (repo->pack_cache_size > rl.rlim_cur / 8) | |||
655 | repo->pack_cache_size = rl.rlim_cur / 8; | |||
656 | ||||
657 | repo_path = realpath(path, NULL((void *)0)); | |||
658 | if (repo_path == NULL((void *)0)) { | |||
659 | err = got_error_from_errno2("realpath", path); | |||
660 | goto done; | |||
661 | } | |||
662 | ||||
663 | for (;;) { | |||
664 | char *parent_path; | |||
665 | ||||
666 | err = open_repo(repo, repo_path); | |||
667 | if (err == NULL((void *)0)) | |||
668 | break; | |||
669 | if (err->code != GOT_ERR_NOT_GIT_REPO2) | |||
670 | goto done; | |||
671 | if (repo_path[0] == '/' && repo_path[1] == '\0') { | |||
672 | err = got_error(GOT_ERR_NOT_GIT_REPO2); | |||
673 | goto done; | |||
674 | } | |||
675 | err = got_path_dirname(&parent_path, repo_path); | |||
676 | if (err) | |||
677 | goto done; | |||
678 | free(repo_path); | |||
679 | repo_path = parent_path; | |||
680 | } | |||
681 | ||||
682 | err = read_gotconfig(repo); | |||
683 | if (err) | |||
684 | goto done; | |||
685 | ||||
686 | err = read_gitconfig(repo, global_gitconfig_path); | |||
687 | if (err) | |||
688 | goto done; | |||
689 | if (repo->gitconfig_repository_format_version != 0) | |||
690 | err = got_error_path(path, GOT_ERR_GIT_REPO_FORMAT110); | |||
691 | for (i = 0; i < repo->nextensions; i++) { | |||
692 | char *ext = repo->extensions[i]; | |||
693 | int j, supported = 0; | |||
694 | for (j = 0; j < nitems(repo_extensions)(sizeof(repo_extensions) / sizeof((repo_extensions)[0])); j++) { | |||
695 | if (strcmp(ext, repo_extensions[j]) == 0) { | |||
696 | supported = 1; | |||
697 | break; | |||
698 | } | |||
699 | } | |||
700 | if (!supported) { | |||
701 | err = got_error_path(ext, GOT_ERR_GIT_REPO_EXT130); | |||
702 | goto done; | |||
703 | } | |||
704 | } | |||
705 | done: | |||
706 | if (err) | |||
707 | got_repo_close(repo); | |||
708 | else | |||
709 | *repop = repo; | |||
710 | free(repo_path); | |||
711 | return err; | |||
712 | } | |||
713 | ||||
714 | const struct got_error * | |||
715 | got_repo_close(struct got_repository *repo) | |||
716 | { | |||
717 | const struct got_error *err = NULL((void *)0), *child_err; | |||
718 | size_t i; | |||
719 | ||||
720 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
| ||||
721 | if (repo->packidx_cache[i] == NULL((void *)0)) | |||
722 | break; | |||
723 | got_packidx_close(repo->packidx_cache[i]); | |||
724 | } | |||
725 | ||||
726 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
727 | if (repo->packs[i].path_packfile == NULL((void *)0)) | |||
728 | break; | |||
729 | got_pack_close(&repo->packs[i]); | |||
730 | } | |||
731 | ||||
732 | free(repo->path); | |||
733 | free(repo->path_git_dir); | |||
734 | ||||
735 | got_object_cache_close(&repo->objcache); | |||
736 | got_object_cache_close(&repo->treecache); | |||
737 | got_object_cache_close(&repo->commitcache); | |||
738 | got_object_cache_close(&repo->tagcache); | |||
739 | ||||
740 | for (i = 0; i < nitems(repo->privsep_children)(sizeof(repo->privsep_children) / sizeof((repo->privsep_children )[0])); i++) { | |||
741 | if (repo->privsep_children[i].imsg_fd == -1) | |||
742 | continue; | |||
743 | imsg_clear(repo->privsep_children[i].ibuf); | |||
744 | free(repo->privsep_children[i].ibuf); | |||
745 | err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd); | |||
746 | child_err = got_privsep_wait_for_child( | |||
747 | repo->privsep_children[i].pid); | |||
748 | if (child_err && err == NULL((void *)0)) | |||
749 | err = child_err; | |||
750 | if (close(repo->privsep_children[i].imsg_fd) == -1 && | |||
751 | err == NULL((void *)0)) | |||
752 | err = got_error_from_errno("close"); | |||
753 | } | |||
754 | ||||
755 | if (repo->gotconfig) | |||
756 | got_gotconfig_free(repo->gotconfig); | |||
757 | free(repo->gitconfig_author_name); | |||
758 | free(repo->gitconfig_author_email); | |||
759 | for (i = 0; i < repo->ngitconfig_remotes; i++) | |||
760 | got_repo_free_remote_repo_data(&repo->gitconfig_remotes[i]); | |||
761 | free(repo->gitconfig_remotes); | |||
762 | for (i = 0; i < repo->nextensions; i++) | |||
763 | free(repo->extensions[i]); | |||
764 | free(repo->extensions); | |||
765 | free(repo); | |||
766 | ||||
767 | return err; | |||
768 | } | |||
769 | ||||
770 | void | |||
771 | got_repo_free_remote_repo_data(struct got_remote_repo *repo) | |||
772 | { | |||
773 | int i; | |||
774 | ||||
775 | free(repo->name); | |||
776 | repo->name = NULL((void *)0); | |||
777 | free(repo->url); | |||
778 | repo->url = NULL((void *)0); | |||
779 | for (i = 0; i < repo->nbranches; i++) | |||
780 | free(repo->branches[i]); | |||
781 | free(repo->branches); | |||
782 | repo->branches = NULL((void *)0); | |||
783 | repo->nbranches = 0; | |||
784 | } | |||
785 | ||||
786 | const struct got_error * | |||
787 | got_repo_map_path(char **in_repo_path, struct got_repository *repo, | |||
788 | const char *input_path) | |||
789 | { | |||
790 | const struct got_error *err = NULL((void *)0); | |||
791 | const char *repo_abspath = NULL((void *)0); | |||
792 | size_t repolen, len; | |||
793 | char *canonpath, *path = NULL((void *)0); | |||
794 | ||||
795 | *in_repo_path = NULL((void *)0); | |||
796 | ||||
797 | canonpath = strdup(input_path); | |||
798 | if (canonpath == NULL((void *)0)) { | |||
799 | err = got_error_from_errno("strdup"); | |||
800 | goto done; | |||
801 | } | |||
802 | err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1); | |||
803 | if (err) | |||
804 | goto done; | |||
805 | ||||
806 | repo_abspath = got_repo_get_path(repo); | |||
807 | ||||
808 | if (canonpath[0] == '\0') { | |||
809 | path = strdup(canonpath); | |||
810 | if (path == NULL((void *)0)) { | |||
811 | err = got_error_from_errno("strdup"); | |||
812 | goto done; | |||
813 | } | |||
814 | } else { | |||
815 | path = realpath(canonpath, NULL((void *)0)); | |||
816 | if (path == NULL((void *)0)) { | |||
817 | if (errno(*__errno()) != ENOENT2) { | |||
818 | err = got_error_from_errno2("realpath", | |||
819 | canonpath); | |||
820 | goto done; | |||
821 | } | |||
822 | /* | |||
823 | * Path is not on disk. | |||
824 | * Assume it is already relative to repository root. | |||
825 | */ | |||
826 | path = strdup(canonpath); | |||
827 | if (path == NULL((void *)0)) { | |||
828 | err = got_error_from_errno("strdup"); | |||
829 | goto done; | |||
830 | } | |||
831 | } | |||
832 | ||||
833 | repolen = strlen(repo_abspath); | |||
834 | len = strlen(path); | |||
835 | ||||
836 | ||||
837 | if (strcmp(path, repo_abspath) == 0) { | |||
838 | free(path); | |||
839 | path = strdup(""); | |||
840 | if (path == NULL((void *)0)) { | |||
841 | err = got_error_from_errno("strdup"); | |||
842 | goto done; | |||
843 | } | |||
844 | } else if (len > repolen && | |||
845 | got_path_is_child(path, repo_abspath, repolen)) { | |||
846 | /* Matched an on-disk path inside repository. */ | |||
847 | if (got_repo_is_bare(repo)) { | |||
848 | /* | |||
849 | * Matched an on-disk path inside repository | |||
850 | * database. Treat input as repository-relative. | |||
851 | */ | |||
852 | free(path); | |||
853 | path = canonpath; | |||
854 | canonpath = NULL((void *)0); | |||
855 | } else { | |||
856 | char *child; | |||
857 | /* Strip common prefix with repository path. */ | |||
858 | err = got_path_skip_common_ancestor(&child, | |||
859 | repo_abspath, path); | |||
860 | if (err) | |||
861 | goto done; | |||
862 | free(path); | |||
863 | path = child; | |||
864 | } | |||
865 | } else { | |||
866 | /* | |||
867 | * Matched unrelated on-disk path. | |||
868 | * Treat input as repository-relative. | |||
869 | */ | |||
870 | free(path); | |||
871 | path = canonpath; | |||
872 | canonpath = NULL((void *)0); | |||
873 | } | |||
874 | } | |||
875 | ||||
876 | /* Make in-repository path absolute */ | |||
877 | if (path[0] != '/') { | |||
878 | char *abspath; | |||
879 | if (asprintf(&abspath, "/%s", path) == -1) { | |||
880 | err = got_error_from_errno("asprintf"); | |||
881 | goto done; | |||
882 | } | |||
883 | free(path); | |||
884 | path = abspath; | |||
885 | } | |||
886 | ||||
887 | done: | |||
888 | free(canonpath); | |||
889 | if (err) | |||
890 | free(path); | |||
891 | else | |||
892 | *in_repo_path = path; | |||
893 | return err; | |||
894 | } | |||
895 | ||||
896 | static const struct got_error * | |||
897 | cache_packidx(struct got_repository *repo, struct got_packidx *packidx, | |||
898 | const char *path_packidx) | |||
899 | { | |||
900 | const struct got_error *err = NULL((void *)0); | |||
901 | size_t i; | |||
902 | ||||
903 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
904 | if (repo->packidx_cache[i] == NULL((void *)0)) | |||
905 | break; | |||
906 | if (strcmp(repo->packidx_cache[i]->path_packidx, | |||
907 | path_packidx) == 0) { | |||
908 | return got_error(GOT_ERR_CACHE_DUP_ENTRY116); | |||
909 | } | |||
910 | } | |||
911 | if (i == repo->pack_cache_size) { | |||
912 | err = got_packidx_close(repo->packidx_cache[i - 1]); | |||
913 | if (err) | |||
914 | return err; | |||
915 | } | |||
916 | ||||
917 | /* | |||
918 | * Insert the new pack index at the front so it will | |||
919 | * be searched first in the future. | |||
920 | */ | |||
921 | memmove(&repo->packidx_cache[1], &repo->packidx_cache[0], | |||
922 | sizeof(repo->packidx_cache) - | |||
923 | sizeof(repo->packidx_cache[0])); | |||
924 | repo->packidx_cache[0] = packidx; | |||
925 | ||||
926 | return NULL((void *)0); | |||
927 | } | |||
928 | ||||
929 | static int | |||
930 | is_packidx_filename(const char *name, size_t len) | |||
931 | { | |||
932 | if (len != GOT_PACKIDX_NAMELEN(strlen("pack-") + (20 * 2 + 1) - 1 + strlen(".idx"))) | |||
933 | return 0; | |||
934 | ||||
935 | if (strncmp(name, GOT_PACK_PREFIX"pack-", strlen(GOT_PACK_PREFIX"pack-")) != 0) | |||
936 | return 0; | |||
937 | ||||
938 | if (strcmp(name + strlen(GOT_PACK_PREFIX"pack-") + | |||
939 | SHA1_DIGEST_STRING_LENGTH(20 * 2 + 1) - 1, GOT_PACKIDX_SUFFIX".idx") != 0) | |||
940 | return 0; | |||
941 | ||||
942 | return 1; | |||
943 | } | |||
944 | ||||
945 | const struct got_error * | |||
946 | got_repo_search_packidx(struct got_packidx **packidx, int *idx, | |||
947 | struct got_repository *repo, struct got_object_id *id) | |||
948 | { | |||
949 | const struct got_error *err; | |||
950 | DIR *packdir = NULL((void *)0); | |||
951 | struct dirent *dent; | |||
952 | char *path_packidx; | |||
953 | size_t i; | |||
954 | int packdir_fd; | |||
955 | ||||
956 | /* Search pack index cache. */ | |||
957 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
958 | if (repo->packidx_cache[i] == NULL((void *)0)) | |||
959 | break; | |||
960 | *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id); | |||
961 | if (*idx != -1) { | |||
962 | *packidx = repo->packidx_cache[i]; | |||
963 | /* | |||
964 | * Move this cache entry to the front. Repeatedly | |||
965 | * searching a wrong pack index can be expensive. | |||
966 | */ | |||
967 | if (i > 0) { | |||
968 | struct got_packidx *p; | |||
969 | p = repo->packidx_cache[0]; | |||
970 | repo->packidx_cache[0] = *packidx; | |||
971 | repo->packidx_cache[i] = p; | |||
972 | } | |||
973 | return NULL((void *)0); | |||
974 | } | |||
975 | } | |||
976 | /* No luck. Search the filesystem. */ | |||
977 | ||||
978 | packdir_fd = openat(got_repo_get_fd(repo), | |||
979 | GOT_OBJECTS_PACK_DIR"objects/pack", O_DIRECTORY0x20000); | |||
980 | if (packdir_fd == -1) { | |||
981 | if (errno(*__errno()) == ENOENT2) | |||
982 | err = got_error_no_obj(id); | |||
983 | else | |||
984 | err = got_error_from_errno_fmt("openat: %s/%s", | |||
985 | got_repo_get_path_git_dir(repo), | |||
986 | GOT_OBJECTS_PACK_DIR"objects/pack"); | |||
987 | goto done; | |||
988 | } | |||
989 | ||||
990 | packdir = fdopendir(packdir_fd); | |||
991 | if (packdir == NULL((void *)0)) { | |||
992 | err = got_error_from_errno("fdopendir"); | |||
993 | goto done; | |||
994 | } | |||
995 | ||||
996 | while ((dent = readdir(packdir)) != NULL((void *)0)) { | |||
997 | int is_cached = 0; | |||
998 | ||||
999 | if (!is_packidx_filename(dent->d_name, dent->d_namlen)) | |||
1000 | continue; | |||
1001 | ||||
1002 | if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR"objects/pack", | |||
1003 | dent->d_name) == -1) { | |||
1004 | err = got_error_from_errno("asprintf"); | |||
1005 | goto done; | |||
1006 | } | |||
1007 | ||||
1008 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
1009 | if (repo->packidx_cache[i] == NULL((void *)0)) | |||
1010 | break; | |||
1011 | if (strcmp(repo->packidx_cache[i]->path_packidx, | |||
1012 | path_packidx) == 0) { | |||
1013 | is_cached = 1; | |||
1014 | break; | |||
1015 | } | |||
1016 | } | |||
1017 | if (is_cached) { | |||
1018 | free(path_packidx); | |||
1019 | continue; /* already searched */ | |||
1020 | } | |||
1021 | ||||
1022 | err = got_packidx_open(packidx, got_repo_get_fd(repo), | |||
1023 | path_packidx, 0); | |||
1024 | if (err) { | |||
1025 | free(path_packidx); | |||
1026 | goto done; | |||
1027 | } | |||
1028 | ||||
1029 | err = cache_packidx(repo, *packidx, path_packidx); | |||
1030 | free(path_packidx); | |||
1031 | if (err) | |||
1032 | goto done; | |||
1033 | ||||
1034 | *idx = got_packidx_get_object_idx(*packidx, id); | |||
1035 | if (*idx != -1) { | |||
1036 | err = NULL((void *)0); /* found the object */ | |||
1037 | goto done; | |||
1038 | } | |||
1039 | } | |||
1040 | ||||
1041 | err = got_error_no_obj(id); | |||
1042 | done: | |||
1043 | if (packdir && closedir(packdir) != 0 && err == NULL((void *)0)) | |||
1044 | err = got_error_from_errno("closedir"); | |||
1045 | return err; | |||
1046 | } | |||
1047 | ||||
1048 | static const struct got_error * | |||
1049 | read_packfile_hdr(int fd, struct got_packidx *packidx) | |||
1050 | { | |||
1051 | const struct got_error *err = NULL((void *)0); | |||
1052 | uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table [0xff]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table [0xff]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr .fanout_table[0xff]) & 0xff00) << 8 | ((__uint32_t) (packidx->hdr.fanout_table[0xff]) & 0xff0000) >> 8 | ((__uint32_t)(packidx->hdr.fanout_table[0xff]) & 0xff000000 ) >> 24) : __swap32md(packidx->hdr.fanout_table[0xff ])); | |||
1053 | struct got_packfile_hdr hdr; | |||
1054 | ssize_t n; | |||
1055 | ||||
1056 | n = read(fd, &hdr, sizeof(hdr)); | |||
1057 | if (n < 0) | |||
1058 | return got_error_from_errno("read"); | |||
1059 | if (n != sizeof(hdr)) | |||
1060 | return got_error(GOT_ERR_BAD_PACKFILE16); | |||
1061 | ||||
1062 | if (be32toh(hdr.signature)(__uint32_t)(__builtin_constant_p(hdr.signature) ? (__uint32_t )(((__uint32_t)(hdr.signature) & 0xff) << 24 | ((__uint32_t )(hdr.signature) & 0xff00) << 8 | ((__uint32_t)(hdr .signature) & 0xff0000) >> 8 | ((__uint32_t)(hdr.signature ) & 0xff000000) >> 24) : __swap32md(hdr.signature)) != GOT_PACKFILE_SIGNATURE0x5041434b || | |||
1063 | be32toh(hdr.version)(__uint32_t)(__builtin_constant_p(hdr.version) ? (__uint32_t) (((__uint32_t)(hdr.version) & 0xff) << 24 | ((__uint32_t )(hdr.version) & 0xff00) << 8 | ((__uint32_t)(hdr.version ) & 0xff0000) >> 8 | ((__uint32_t)(hdr.version) & 0xff000000) >> 24) : __swap32md(hdr.version)) != GOT_PACKFILE_VERSION2 || | |||
1064 | be32toh(hdr.nobjects)(__uint32_t)(__builtin_constant_p(hdr.nobjects) ? (__uint32_t )(((__uint32_t)(hdr.nobjects) & 0xff) << 24 | ((__uint32_t )(hdr.nobjects) & 0xff00) << 8 | ((__uint32_t)(hdr. nobjects) & 0xff0000) >> 8 | ((__uint32_t)(hdr.nobjects ) & 0xff000000) >> 24) : __swap32md(hdr.nobjects)) != totobj) | |||
1065 | err = got_error(GOT_ERR_BAD_PACKFILE16); | |||
1066 | ||||
1067 | return err; | |||
1068 | } | |||
1069 | ||||
1070 | static const struct got_error * | |||
1071 | open_packfile(int *fd, struct got_repository *repo, | |||
1072 | const char *relpath, struct got_packidx *packidx) | |||
1073 | { | |||
1074 | const struct got_error *err = NULL((void *)0); | |||
1075 | ||||
1076 | *fd = openat(got_repo_get_fd(repo), relpath, O_RDONLY0x0000 | O_NOFOLLOW0x0100); | |||
1077 | if (*fd == -1) | |||
1078 | return got_error_from_errno_fmt("openat: %s/%s", | |||
1079 | got_repo_get_path_git_dir(repo), relpath); | |||
1080 | ||||
1081 | if (packidx) { | |||
1082 | err = read_packfile_hdr(*fd, packidx); | |||
1083 | if (err) { | |||
1084 | close(*fd); | |||
1085 | *fd = -1; | |||
1086 | } | |||
1087 | } | |||
1088 | ||||
1089 | return err; | |||
1090 | } | |||
1091 | ||||
1092 | const struct got_error * | |||
1093 | got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo, | |||
1094 | const char *path_packfile, struct got_packidx *packidx) | |||
1095 | { | |||
1096 | const struct got_error *err = NULL((void *)0); | |||
1097 | struct got_pack *pack = NULL((void *)0); | |||
1098 | struct stat sb; | |||
1099 | size_t i; | |||
1100 | ||||
1101 | if (packp) | |||
1102 | *packp = NULL((void *)0); | |||
1103 | ||||
1104 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
1105 | pack = &repo->packs[i]; | |||
1106 | if (pack->path_packfile == NULL((void *)0)) | |||
1107 | break; | |||
1108 | if (strcmp(pack->path_packfile, path_packfile) == 0) | |||
1109 | return got_error(GOT_ERR_CACHE_DUP_ENTRY116); | |||
1110 | } | |||
1111 | ||||
1112 | if (i == repo->pack_cache_size) { | |||
1113 | err = got_pack_close(&repo->packs[i - 1]); | |||
1114 | if (err) | |||
1115 | return err; | |||
1116 | memmove(&repo->packs[1], &repo->packs[0], | |||
1117 | sizeof(repo->packs) - sizeof(repo->packs[0])); | |||
1118 | i = 0; | |||
1119 | } | |||
1120 | ||||
1121 | pack = &repo->packs[i]; | |||
1122 | ||||
1123 | pack->path_packfile = strdup(path_packfile); | |||
1124 | if (pack->path_packfile == NULL((void *)0)) { | |||
1125 | err = got_error_from_errno("strdup"); | |||
1126 | goto done; | |||
1127 | } | |||
1128 | ||||
1129 | err = open_packfile(&pack->fd, repo, path_packfile, packidx); | |||
1130 | if (err) | |||
1131 | goto done; | |||
1132 | ||||
1133 | if (fstat(pack->fd, &sb) != 0) { | |||
1134 | err = got_error_from_errno("fstat"); | |||
1135 | goto done; | |||
1136 | } | |||
1137 | pack->filesize = sb.st_size; | |||
1138 | ||||
1139 | pack->privsep_child = NULL((void *)0); | |||
1140 | ||||
1141 | #ifndef GOT_PACK_NO_MMAP | |||
1142 | pack->map = mmap(NULL((void *)0), pack->filesize, PROT_READ0x01, MAP_PRIVATE0x0002, | |||
1143 | pack->fd, 0); | |||
1144 | if (pack->map == MAP_FAILED((void *)-1)) { | |||
1145 | if (errno(*__errno()) != ENOMEM12) { | |||
1146 | err = got_error_from_errno("mmap"); | |||
1147 | goto done; | |||
1148 | } | |||
1149 | pack->map = NULL((void *)0); /* fall back to read(2) */ | |||
1150 | } | |||
1151 | #endif | |||
1152 | done: | |||
1153 | if (err) { | |||
1154 | if (pack) { | |||
1155 | free(pack->path_packfile); | |||
1156 | memset(pack, 0, sizeof(*pack)); | |||
1157 | } | |||
1158 | } else if (packp) | |||
1159 | *packp = pack; | |||
1160 | return err; | |||
1161 | } | |||
1162 | ||||
1163 | struct got_pack * | |||
1164 | got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile) | |||
1165 | { | |||
1166 | struct got_pack *pack = NULL((void *)0); | |||
1167 | size_t i; | |||
1168 | ||||
1169 | for (i = 0; i < repo->pack_cache_size; i++) { | |||
1170 | pack = &repo->packs[i]; | |||
1171 | if (pack->path_packfile == NULL((void *)0)) | |||
1172 | break; | |||
1173 | if (strcmp(pack->path_packfile, path_packfile) == 0) | |||
1174 | return pack; | |||
1175 | } | |||
1176 | ||||
1177 | return NULL((void *)0); | |||
1178 | } | |||
1179 | ||||
1180 | const struct got_error * | |||
1181 | got_repo_init(const char *repo_path) | |||
1182 | { | |||
1183 | const struct got_error *err = NULL((void *)0); | |||
1184 | const char *dirnames[] = { | |||
1185 | GOT_OBJECTS_DIR"objects", | |||
1186 | GOT_OBJECTS_PACK_DIR"objects/pack", | |||
1187 | GOT_REFS_DIR"refs", | |||
1188 | }; | |||
1189 | const char *description_str = "Unnamed repository; " | |||
1190 | "edit this file 'description' to name the repository."; | |||
1191 | const char *headref_str = "ref: refs/heads/main"; | |||
1192 | const char *gitconfig_str = "[core]\n" | |||
1193 | "\trepositoryformatversion = 0\n" | |||
1194 | "\tfilemode = true\n" | |||
1195 | "\tbare = true\n"; | |||
1196 | char *path; | |||
1197 | size_t i; | |||
1198 | ||||
1199 | if (!got_path_dir_is_empty(repo_path)) | |||
1200 | return got_error(GOT_ERR_DIR_NOT_EMPTY75); | |||
1201 | ||||
1202 | for (i = 0; i < nitems(dirnames)(sizeof(dirnames) / sizeof((dirnames)[0])); i++) { | |||
1203 | if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) { | |||
1204 | return got_error_from_errno("asprintf"); | |||
1205 | } | |||
1206 | err = got_path_mkdir(path); | |||
1207 | free(path); | |||
1208 | if (err) | |||
1209 | return err; | |||
1210 | } | |||
1211 | ||||
1212 | if (asprintf(&path, "%s/%s", repo_path, "description") == -1) | |||
1213 | return got_error_from_errno("asprintf"); | |||
1214 | err = got_path_create_file(path, description_str); | |||
1215 | free(path); | |||
1216 | if (err) | |||
1217 | return err; | |||
1218 | ||||
1219 | if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE"HEAD") == -1) | |||
1220 | return got_error_from_errno("asprintf"); | |||
1221 | err = got_path_create_file(path, headref_str); | |||
1222 | free(path); | |||
1223 | if (err) | |||
1224 | return err; | |||
1225 | ||||
1226 | if (asprintf(&path, "%s/%s", repo_path, "config") == -1) | |||
1227 | return got_error_from_errno("asprintf"); | |||
1228 | err = got_path_create_file(path, gitconfig_str); | |||
1229 | free(path); | |||
1230 | if (err) | |||
1231 | return err; | |||
1232 | ||||
1233 | return NULL((void *)0); | |||
1234 | } | |||
1235 | ||||
1236 | static const struct got_error * | |||
1237 | match_packed_object(struct got_object_id **unique_id, | |||
1238 | struct got_repository *repo, const char *id_str_prefix, int obj_type) | |||
1239 | { | |||
1240 | const struct got_error *err = NULL((void *)0); | |||
1241 | DIR *packdir = NULL((void *)0); | |||
1242 | struct dirent *dent; | |||
1243 | char *path_packidx; | |||
1244 | struct got_object_id_queue matched_ids; | |||
1245 | int packdir_fd; | |||
1246 | ||||
1247 | SIMPLEQ_INIT(&matched_ids)do { (&matched_ids)->sqh_first = ((void *)0); (&matched_ids )->sqh_last = &(&matched_ids)->sqh_first; } while (0); | |||
1248 | ||||
1249 | packdir_fd = openat(got_repo_get_fd(repo), | |||
1250 | GOT_OBJECTS_PACK_DIR"objects/pack", O_DIRECTORY0x20000); | |||
1251 | if (packdir_fd == -1) { | |||
1252 | if (errno(*__errno()) != ENOENT2) | |||
1253 | err = got_error_from_errno2("openat", GOT_OBJECTS_PACK_DIR"objects/pack"); | |||
1254 | goto done; | |||
1255 | } | |||
1256 | ||||
1257 | packdir = fdopendir(packdir_fd); | |||
1258 | if (packdir == NULL((void *)0)) { | |||
1259 | err = got_error_from_errno("fdopendir"); | |||
1260 | goto done; | |||
1261 | } | |||
1262 | ||||
1263 | while ((dent = readdir(packdir)) != NULL((void *)0)) { | |||
1264 | struct got_packidx *packidx; | |||
1265 | struct got_object_qid *qid; | |||
1266 | ||||
1267 | if (!is_packidx_filename(dent->d_name, dent->d_namlen)) | |||
1268 | continue; | |||
1269 | ||||
1270 | if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR"objects/pack", | |||
1271 | dent->d_name) == -1) { | |||
1272 | err = got_error_from_errno("strdup"); | |||
1273 | break; | |||
1274 | } | |||
1275 | ||||
1276 | err = got_packidx_open(&packidx, got_repo_get_fd(repo), | |||
1277 | path_packidx, 0); | |||
1278 | free(path_packidx); | |||
1279 | if (err) | |||
1280 | break; | |||
1281 | ||||
1282 | err = got_packidx_match_id_str_prefix(&matched_ids, | |||
1283 | packidx, id_str_prefix); | |||
1284 | if (err) { | |||
1285 | got_packidx_close(packidx); | |||
1286 | break; | |||
1287 | } | |||
1288 | err = got_packidx_close(packidx); | |||
1289 | if (err) | |||
1290 | break; | |||
1291 | ||||
1292 | SIMPLEQ_FOREACH(qid, &matched_ids, entry)for((qid) = ((&matched_ids)->sqh_first); (qid) != ((void *)0); (qid) = ((qid)->entry.sqe_next)) { | |||
1293 | if (obj_type != GOT_OBJ_TYPE_ANY0) { | |||
1294 | int matched_type; | |||
1295 | err = got_object_get_type(&matched_type, repo, | |||
1296 | qid->id); | |||
1297 | if (err) | |||
1298 | goto done; | |||
1299 | if (matched_type != obj_type) | |||
1300 | continue; | |||
1301 | } | |||
1302 | if (*unique_id == NULL((void *)0)) { | |||
1303 | *unique_id = got_object_id_dup(qid->id); | |||
1304 | if (*unique_id == NULL((void *)0)) { | |||
1305 | err = got_error_from_errno("malloc"); | |||
1306 | goto done; | |||
1307 | } | |||
1308 | } else { | |||
1309 | if (got_object_id_cmp(*unique_id, qid->id) == 0) | |||
1310 | continue; /* packed multiple times */ | |||
1311 | err = got_error(GOT_ERR_AMBIGUOUS_ID13); | |||
1312 | goto done; | |||
1313 | } | |||
1314 | } | |||
1315 | } | |||
1316 | done: | |||
1317 | got_object_id_queue_free(&matched_ids); | |||
1318 | if (packdir && closedir(packdir) != 0 && err == NULL((void *)0)) | |||
1319 | err = got_error_from_errno("closedir"); | |||
1320 | if (err) { | |||
1321 | free(*unique_id); | |||
1322 | *unique_id = NULL((void *)0); | |||
1323 | } | |||
1324 | return err; | |||
1325 | } | |||
1326 | ||||
1327 | static const struct got_error * | |||
1328 | match_loose_object(struct got_object_id **unique_id, const char *path_objects, | |||
1329 | const char *object_dir, const char *id_str_prefix, int obj_type, | |||
1330 | struct got_repository *repo) | |||
1331 | { | |||
1332 | const struct got_error *err = NULL((void *)0); | |||
1333 | char *path; | |||
1334 | DIR *dir = NULL((void *)0); | |||
1335 | struct dirent *dent; | |||
1336 | struct got_object_id id; | |||
1337 | ||||
1338 | if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) { | |||
1339 | err = got_error_from_errno("asprintf"); | |||
1340 | goto done; | |||
1341 | } | |||
1342 | ||||
1343 | dir = opendir(path); | |||
1344 | if (dir == NULL((void *)0)) { | |||
1345 | if (errno(*__errno()) == ENOENT2) { | |||
1346 | err = NULL((void *)0); | |||
1347 | goto done; | |||
1348 | } | |||
1349 | err = got_error_from_errno2("opendir", path); | |||
1350 | goto done; | |||
1351 | } | |||
1352 | while ((dent = readdir(dir)) != NULL((void *)0)) { | |||
1353 | char *id_str; | |||
1354 | int cmp; | |||
1355 | ||||
1356 | if (strcmp(dent->d_name, ".") == 0 || | |||
1357 | strcmp(dent->d_name, "..") == 0) | |||
1358 | continue; | |||
1359 | ||||
1360 | if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) { | |||
1361 | err = got_error_from_errno("asprintf"); | |||
1362 | goto done; | |||
1363 | } | |||
1364 | ||||
1365 | if (!got_parse_sha1_digest(id.sha1, id_str)) | |||
1366 | continue; | |||
1367 | ||||
1368 | /* | |||
1369 | * Directory entries do not necessarily appear in | |||
1370 | * sorted order, so we must iterate over all of them. | |||
1371 | */ | |||
1372 | cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix)); | |||
1373 | if (cmp != 0) { | |||
1374 | free(id_str); | |||
1375 | continue; | |||
1376 | } | |||
1377 | ||||
1378 | if (*unique_id == NULL((void *)0)) { | |||
1379 | if (obj_type != GOT_OBJ_TYPE_ANY0) { | |||
1380 | int matched_type; | |||
1381 | err = got_object_get_type(&matched_type, repo, | |||
1382 | &id); | |||
1383 | if (err) | |||
1384 | goto done; | |||
1385 | if (matched_type != obj_type) | |||
1386 | continue; | |||
1387 | } | |||
1388 | *unique_id = got_object_id_dup(&id); | |||
1389 | if (*unique_id == NULL((void *)0)) { | |||
1390 | err = got_error_from_errno("got_object_id_dup"); | |||
1391 | free(id_str); | |||
1392 | goto done; | |||
1393 | } | |||
1394 | } else { | |||
1395 | if (got_object_id_cmp(*unique_id, &id) == 0) | |||
1396 | continue; /* both packed and loose */ | |||
1397 | err = got_error(GOT_ERR_AMBIGUOUS_ID13); | |||
1398 | free(id_str); | |||
1399 | goto done; | |||
1400 | } | |||
1401 | } | |||
1402 | done: | |||
1403 | if (dir && closedir(dir) != 0 && err == NULL((void *)0)) | |||
1404 | err = got_error_from_errno("closedir"); | |||
1405 | if (err) { | |||
1406 | free(*unique_id); | |||
1407 | *unique_id = NULL((void *)0); | |||
1408 | } | |||
1409 | free(path); | |||
1410 | return err; | |||
1411 | } | |||
1412 | ||||
1413 | const struct got_error * | |||
1414 | got_repo_match_object_id_prefix(struct got_object_id **id, | |||
1415 | const char *id_str_prefix, int obj_type, struct got_repository *repo) | |||
1416 | { | |||
1417 | const struct got_error *err = NULL((void *)0); | |||
1418 | char *path_objects = got_repo_get_path_objects(repo); | |||
1419 | char *object_dir = NULL((void *)0); | |||
1420 | size_t len; | |||
1421 | int i; | |||
1422 | ||||
1423 | *id = NULL((void *)0); | |||
1424 | ||||
1425 | for (i = 0; i < strlen(id_str_prefix); i++) { | |||
1426 | if (isxdigit((unsigned char)id_str_prefix[i])) | |||
1427 | continue; | |||
1428 | return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR23); | |||
1429 | } | |||
1430 | ||||
1431 | len = strlen(id_str_prefix); | |||
1432 | if (len >= 2) { | |||
1433 | err = match_packed_object(id, repo, id_str_prefix, obj_type); | |||
1434 | if (err) | |||
1435 | goto done; | |||
1436 | object_dir = strndup(id_str_prefix, 2); | |||
1437 | if (object_dir == NULL((void *)0)) { | |||
1438 | err = got_error_from_errno("strdup"); | |||
1439 | goto done; | |||
1440 | } | |||
1441 | err = match_loose_object(id, path_objects, object_dir, | |||
1442 | id_str_prefix, obj_type, repo); | |||
1443 | } else if (len == 1) { | |||
1444 | int i; | |||
1445 | for (i = 0; i < 0xf; i++) { | |||
1446 | if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i) | |||
1447 | == -1) { | |||
1448 | err = got_error_from_errno("asprintf"); | |||
1449 | goto done; | |||
1450 | } | |||
1451 | err = match_packed_object(id, repo, object_dir, | |||
1452 | obj_type); | |||
1453 | if (err) | |||
1454 | goto done; | |||
1455 | err = match_loose_object(id, path_objects, object_dir, | |||
1456 | id_str_prefix, obj_type, repo); | |||
1457 | if (err) | |||
1458 | goto done; | |||
1459 | } | |||
1460 | } else { | |||
1461 | err = got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR23); | |||
1462 | goto done; | |||
1463 | } | |||
1464 | done: | |||
1465 | free(object_dir); | |||
1466 | if (err) { | |||
1467 | free(*id); | |||
1468 | *id = NULL((void *)0); | |||
1469 | } else if (*id == NULL((void *)0)) | |||
1470 | err = got_error_path(id_str_prefix, GOT_ERR_NO_OBJ17); | |||
1471 | ||||
1472 | return err; | |||
1473 | } | |||
1474 | ||||
1475 | const struct got_error * | |||
1476 | got_repo_match_object_id(struct got_object_id **id, char **label, | |||
1477 | const char *id_str, int obj_type, struct got_reflist_head *refs, | |||
1478 | struct got_repository *repo) | |||
1479 | { | |||
1480 | const struct got_error *err; | |||
1481 | struct got_tag_object *tag; | |||
1482 | struct got_reference *ref = NULL((void *)0); | |||
1483 | ||||
1484 | *id = NULL((void *)0); | |||
1485 | if (label) | |||
1486 | *label = NULL((void *)0); | |||
1487 | ||||
1488 | if (refs) { | |||
1489 | err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY0, | |||
1490 | refs, repo); | |||
1491 | if (err == NULL((void *)0)) { | |||
1492 | *id = got_object_id_dup( | |||
1493 | got_object_tag_get_object_id(tag)); | |||
1494 | if (*id == NULL((void *)0)) | |||
1495 | err = got_error_from_errno("got_object_id_dup"); | |||
1496 | else if (label && asprintf(label, "refs/tags/%s", | |||
1497 | got_object_tag_get_name(tag)) == -1) { | |||
1498 | err = got_error_from_errno("asprintf"); | |||
1499 | free(*id); | |||
1500 | *id = NULL((void *)0); | |||
1501 | } | |||
1502 | got_object_tag_close(tag); | |||
1503 | return err; | |||
1504 | } else if (err->code != GOT_ERR_OBJ_TYPE11 && | |||
1505 | err->code != GOT_ERR_NO_OBJ17) | |||
1506 | return err; | |||
1507 | } | |||
1508 | ||||
1509 | err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo); | |||
1510 | if (err) { | |||
1511 | if (err->code != GOT_ERR_BAD_OBJ_ID_STR23) | |||
1512 | return err; | |||
1513 | err = got_ref_open(&ref, repo, id_str, 0); | |||
1514 | if (err != NULL((void *)0)) | |||
1515 | goto done; | |||
1516 | if (label) { | |||
1517 | *label = strdup(got_ref_get_name(ref)); | |||
1518 | if (*label == NULL((void *)0)) { | |||
1519 | err = got_error_from_errno("strdup"); | |||
1520 | goto done; | |||
1521 | } | |||
1522 | } | |||
1523 | err = got_ref_resolve(id, repo, ref); | |||
1524 | } else if (label) { | |||
1525 | err = got_object_id_str(label, *id); | |||
1526 | if (*label == NULL((void *)0)) { | |||
1527 | err = got_error_from_errno("strdup"); | |||
1528 | goto done; | |||
1529 | } | |||
1530 | } | |||
1531 | done: | |||
1532 | if (ref) | |||
1533 | got_ref_close(ref); | |||
1534 | return err; | |||
1535 | } | |||
1536 | ||||
1537 | const struct got_error * | |||
1538 | got_repo_object_match_tag(struct got_tag_object **tag, const char *name, | |||
1539 | int obj_type, struct got_reflist_head *refs, struct got_repository *repo) | |||
1540 | { | |||
1541 | const struct got_error *err = NULL((void *)0); | |||
1542 | struct got_reflist_entry *re; | |||
1543 | struct got_object_id *tag_id; | |||
1544 | int name_is_absolute = (strncmp(name, "refs/", 5) == 0); | |||
1545 | ||||
1546 | *tag = NULL((void *)0); | |||
1547 | ||||
1548 | TAILQ_FOREACH(re, refs, entry)for((re) = ((refs)->tqh_first); (re) != ((void *)0); (re) = ((re)->entry.tqe_next)) { | |||
1549 | const char *refname; | |||
1550 | refname = got_ref_get_name(re->ref); | |||
1551 | if (got_ref_is_symbolic(re->ref)) | |||
1552 | continue; | |||
1553 | if (strncmp(refname, "refs/tags/", 10) != 0) | |||
1554 | continue; | |||
1555 | if (!name_is_absolute) | |||
1556 | refname += strlen("refs/tags/"); | |||
1557 | if (strcmp(refname, name) != 0) | |||
1558 | continue; | |||
1559 | err = got_ref_resolve(&tag_id, repo, re->ref); | |||
1560 | if (err) | |||
1561 | break; | |||
1562 | err = got_object_open_as_tag(tag, repo, tag_id); | |||
1563 | free(tag_id); | |||
1564 | if (err) | |||
1565 | break; | |||
1566 | if (obj_type == GOT_OBJ_TYPE_ANY0 || | |||
1567 | got_object_tag_get_object_type(*tag) == obj_type) | |||
1568 | break; | |||
1569 | got_object_tag_close(*tag); | |||
1570 | *tag = NULL((void *)0); | |||
1571 | } | |||
1572 | ||||
1573 | if (err == NULL((void *)0) && *tag == NULL((void *)0)) | |||
1574 | err = got_error_path(name, GOT_ERR_NO_OBJ17); | |||
1575 | return err; | |||
1576 | } | |||
1577 | ||||
1578 | static const struct got_error * | |||
1579 | alloc_added_blob_tree_entry(struct got_tree_entry **new_te, | |||
1580 | const char *name, mode_t mode, struct got_object_id *blob_id) | |||
1581 | { | |||
1582 | const struct got_error *err = NULL((void *)0); | |||
1583 | ||||
1584 | *new_te = NULL((void *)0); | |||
1585 | ||||
1586 | *new_te = calloc(1, sizeof(**new_te)); | |||
1587 | if (*new_te == NULL((void *)0)) | |||
1588 | return got_error_from_errno("calloc"); | |||
1589 | ||||
1590 | if (strlcpy((*new_te)->name, name, sizeof((*new_te)->name)) >= | |||
1591 | sizeof((*new_te)->name)) { | |||
1592 | err = got_error(GOT_ERR_NO_SPACE9); | |||
1593 | goto done; | |||
1594 | } | |||
1595 | ||||
1596 | if (S_ISLNK(mode)((mode & 0170000) == 0120000)) { | |||
1597 | (*new_te)->mode = S_IFLNK0120000; | |||
1598 | } else { | |||
1599 | (*new_te)->mode = S_IFREG0100000; | |||
1600 | (*new_te)->mode |= (mode & (S_IRWXU0000700 | S_IRWXG0000070 | S_IRWXO0000007)); | |||
1601 | } | |||
1602 | memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id)); | |||
1603 | done: | |||
1604 | if (err && *new_te) { | |||
1605 | free(*new_te); | |||
1606 | *new_te = NULL((void *)0); | |||
1607 | } | |||
1608 | return err; | |||
1609 | } | |||
1610 | ||||
1611 | static const struct got_error * | |||
1612 | import_file(struct got_tree_entry **new_te, struct dirent *de, | |||
1613 | const char *path, struct got_repository *repo) | |||
1614 | { | |||
1615 | const struct got_error *err; | |||
1616 | struct got_object_id *blob_id = NULL((void *)0); | |||
1617 | char *filepath; | |||
1618 | struct stat sb; | |||
1619 | ||||
1620 | if (asprintf(&filepath, "%s%s%s", path, | |||
1621 | path[0] == '\0' ? "" : "/", de->d_name) == -1) | |||
1622 | return got_error_from_errno("asprintf"); | |||
1623 | ||||
1624 | if (lstat(filepath, &sb) != 0) { | |||
1625 | err = got_error_from_errno2("lstat", path); | |||
1626 | goto done; | |||
1627 | } | |||
1628 | ||||
1629 | err = got_object_blob_create(&blob_id, filepath, repo); | |||
1630 | if (err) | |||
1631 | goto done; | |||
1632 | ||||
1633 | err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode, | |||
1634 | blob_id); | |||
1635 | done: | |||
1636 | free(filepath); | |||
1637 | if (err) | |||
1638 | free(blob_id); | |||
1639 | return err; | |||
1640 | } | |||
1641 | ||||
1642 | static const struct got_error * | |||
1643 | insert_tree_entry(struct got_tree_entry *new_te, | |||
1644 | struct got_pathlist_head *paths) | |||
1645 | { | |||
1646 | const struct got_error *err = NULL((void *)0); | |||
1647 | struct got_pathlist_entry *new_pe; | |||
1648 | ||||
1649 | err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te); | |||
1650 | if (err) | |||
1651 | return err; | |||
1652 | if (new_pe == NULL((void *)0)) | |||
1653 | return got_error(GOT_ERR_TREE_DUP_ENTRY58); | |||
1654 | return NULL((void *)0); | |||
1655 | } | |||
1656 | ||||
1657 | static const struct got_error *write_tree(struct got_object_id **, | |||
1658 | const char *, struct got_pathlist_head *, struct got_repository *, | |||
1659 | got_repo_import_cb progress_cb, void *progress_arg); | |||
1660 | ||||
1661 | static const struct got_error * | |||
1662 | import_subdir(struct got_tree_entry **new_te, struct dirent *de, | |||
1663 | const char *path, struct got_pathlist_head *ignores, | |||
1664 | struct got_repository *repo, | |||
1665 | got_repo_import_cb progress_cb, void *progress_arg) | |||
1666 | { | |||
1667 | const struct got_error *err; | |||
1668 | struct got_object_id *id = NULL((void *)0); | |||
1669 | char *subdirpath; | |||
1670 | ||||
1671 | if (asprintf(&subdirpath, "%s%s%s", path, | |||
1672 | path[0] == '\0' ? "" : "/", de->d_name) == -1) | |||
1673 | return got_error_from_errno("asprintf"); | |||
1674 | ||||
1675 | (*new_te) = calloc(1, sizeof(**new_te)); | |||
1676 | if (*new_te == NULL((void *)0)) | |||
1677 | return got_error_from_errno("calloc"); | |||
1678 | (*new_te)->mode = S_IFDIR0040000; | |||
1679 | if (strlcpy((*new_te)->name, de->d_name, sizeof((*new_te)->name)) >= | |||
1680 | sizeof((*new_te)->name)) { | |||
1681 | err = got_error(GOT_ERR_NO_SPACE9); | |||
1682 | goto done; | |||
1683 | } | |||
1684 | err = write_tree(&id, subdirpath, ignores, repo, | |||
1685 | progress_cb, progress_arg); | |||
1686 | if (err) | |||
1687 | goto done; | |||
1688 | memcpy(&(*new_te)->id, id, sizeof((*new_te)->id)); | |||
1689 | ||||
1690 | done: | |||
1691 | free(id); | |||
1692 | free(subdirpath); | |||
1693 | if (err) { | |||
1694 | free(*new_te); | |||
1695 | *new_te = NULL((void *)0); | |||
1696 | } | |||
1697 | return err; | |||
1698 | } | |||
1699 | ||||
1700 | static const struct got_error * | |||
1701 | write_tree(struct got_object_id **new_tree_id, const char *path_dir, | |||
1702 | struct got_pathlist_head *ignores, struct got_repository *repo, | |||
1703 | got_repo_import_cb progress_cb, void *progress_arg) | |||
1704 | { | |||
1705 | const struct got_error *err = NULL((void *)0); | |||
1706 | DIR *dir; | |||
1707 | struct dirent *de; | |||
1708 | int nentries; | |||
1709 | struct got_tree_entry *new_te = NULL((void *)0); | |||
1710 | struct got_pathlist_head paths; | |||
1711 | struct got_pathlist_entry *pe; | |||
1712 | ||||
1713 | *new_tree_id = NULL((void *)0); | |||
1714 | ||||
1715 | TAILQ_INIT(&paths)do { (&paths)->tqh_first = ((void *)0); (&paths)-> tqh_last = &(&paths)->tqh_first; } while (0); | |||
1716 | ||||
1717 | dir = opendir(path_dir); | |||
1718 | if (dir == NULL((void *)0)) { | |||
1719 | err = got_error_from_errno2("opendir", path_dir); | |||
1720 | goto done; | |||
1721 | } | |||
1722 | ||||
1723 | nentries = 0; | |||
1724 | while ((de = readdir(dir)) != NULL((void *)0)) { | |||
1725 | int ignore = 0; | |||
1726 | int type; | |||
1727 | ||||
1728 | if (strcmp(de->d_name, ".") == 0 || | |||
1729 | strcmp(de->d_name, "..") == 0) | |||
1730 | continue; | |||
1731 | ||||
1732 | TAILQ_FOREACH(pe, ignores, entry)for((pe) = ((ignores)->tqh_first); (pe) != ((void *)0); (pe ) = ((pe)->entry.tqe_next)) { | |||
1733 | if (fnmatch(pe->path, de->d_name, 0) == 0) { | |||
1734 | ignore = 1; | |||
1735 | break; | |||
1736 | } | |||
1737 | } | |||
1738 | if (ignore) | |||
1739 | continue; | |||
1740 | ||||
1741 | err = got_path_dirent_type(&type, path_dir, de); | |||
1742 | if (err) | |||
1743 | goto done; | |||
1744 | ||||
1745 | if (type == DT_DIR4) { | |||
1746 | err = import_subdir(&new_te, de, path_dir, | |||
1747 | ignores, repo, progress_cb, progress_arg); | |||
1748 | if (err) { | |||
1749 | if (err->code != GOT_ERR_NO_TREE_ENTRY50) | |||
1750 | goto done; | |||
1751 | err = NULL((void *)0); | |||
1752 | continue; | |||
1753 | } | |||
1754 | } else if (type == DT_REG8 || type == DT_LNK10) { | |||
1755 | err = import_file(&new_te, de, path_dir, repo); | |||
1756 | if (err) | |||
1757 | goto done; | |||
1758 | } else | |||
1759 | continue; | |||
1760 | ||||
1761 | err = insert_tree_entry(new_te, &paths); | |||
1762 | if (err) | |||
1763 | goto done; | |||
1764 | nentries++; | |||
1765 | } | |||
1766 | ||||
1767 | if (TAILQ_EMPTY(&paths)(((&paths)->tqh_first) == ((void *)0))) { | |||
1768 | err = got_error_msg(GOT_ERR_NO_TREE_ENTRY50, | |||
1769 | "cannot create tree without any entries"); | |||
1770 | goto done; | |||
1771 | } | |||
1772 | ||||
1773 | TAILQ_FOREACH(pe, &paths, entry)for((pe) = ((&paths)->tqh_first); (pe) != ((void *)0); (pe) = ((pe)->entry.tqe_next)) { | |||
1774 | struct got_tree_entry *te = pe->data; | |||
1775 | char *path; | |||
1776 | if (!S_ISREG(te->mode)((te->mode & 0170000) == 0100000) && !S_ISLNK(te->mode)((te->mode & 0170000) == 0120000)) | |||
1777 | continue; | |||
1778 | if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) { | |||
1779 | err = got_error_from_errno("asprintf"); | |||
1780 | goto done; | |||
1781 | } | |||
1782 | err = (*progress_cb)(progress_arg, path); | |||
1783 | free(path); | |||
1784 | if (err) | |||
1785 | goto done; | |||
1786 | } | |||
1787 | ||||
1788 | err = got_object_tree_create(new_tree_id, &paths, nentries, repo); | |||
1789 | done: | |||
1790 | if (dir) | |||
1791 | closedir(dir); | |||
1792 | got_pathlist_free(&paths); | |||
1793 | return err; | |||
1794 | } | |||
1795 | ||||
1796 | const struct got_error * | |||
1797 | got_repo_import(struct got_object_id **new_commit_id, const char *path_dir, | |||
1798 | const char *logmsg, const char *author, struct got_pathlist_head *ignores, | |||
1799 | struct got_repository *repo, got_repo_import_cb progress_cb, | |||
1800 | void *progress_arg) | |||
1801 | { | |||
1802 | const struct got_error *err; | |||
1803 | struct got_object_id *new_tree_id; | |||
1804 | ||||
1805 | err = write_tree(&new_tree_id, path_dir, ignores, repo, | |||
1806 | progress_cb, progress_arg); | |||
1807 | if (err) | |||
1808 | return err; | |||
1809 | ||||
1810 | err = got_object_commit_create(new_commit_id, new_tree_id, NULL((void *)0), 0, | |||
1811 | author, time(NULL((void *)0)), author, time(NULL((void *)0)), logmsg, repo); | |||
1812 | free(new_tree_id); | |||
1813 | return err; | |||
1814 | } | |||
1815 | ||||
1816 | const struct got_error * | |||
1817 | got_repo_get_loose_object_info(int *nobjects, off_t *ondisk_size, | |||
1818 | struct got_repository *repo) | |||
1819 | { | |||
1820 | const struct got_error *err = NULL((void *)0); | |||
1821 | char *path_objects = NULL((void *)0), *path = NULL((void *)0); | |||
1822 | DIR *dir = NULL((void *)0); | |||
1823 | struct got_object_id id; | |||
1824 | int i; | |||
1825 | ||||
1826 | *nobjects = 0; | |||
1827 | *ondisk_size = 0; | |||
1828 | ||||
1829 | path_objects = got_repo_get_path_objects(repo); | |||
1830 | if (path_objects == NULL((void *)0)) | |||
1831 | return got_error_from_errno("got_repo_get_path_objects"); | |||
1832 | ||||
1833 | for (i = 0; i <= 0xff; i++) { | |||
1834 | struct dirent *dent; | |||
1835 | ||||
1836 | if (asprintf(&path, "%s/%.2x", path_objects, i) == -1) { | |||
1837 | err = got_error_from_errno("asprintf"); | |||
1838 | break; | |||
1839 | } | |||
1840 | ||||
1841 | dir = opendir(path); | |||
1842 | if (dir == NULL((void *)0)) { | |||
1843 | if (errno(*__errno()) == ENOENT2) { | |||
1844 | err = NULL((void *)0); | |||
1845 | continue; | |||
1846 | } | |||
1847 | err = got_error_from_errno2("opendir", path); | |||
1848 | break; | |||
1849 | } | |||
1850 | ||||
1851 | while ((dent = readdir(dir)) != NULL((void *)0)) { | |||
1852 | char *id_str; | |||
1853 | int fd; | |||
1854 | struct stat sb; | |||
1855 | ||||
1856 | if (strcmp(dent->d_name, ".") == 0 || | |||
1857 | strcmp(dent->d_name, "..") == 0) | |||
1858 | continue; | |||
1859 | ||||
1860 | if (asprintf(&id_str, "%.2x%s", i, dent->d_name) == -1) { | |||
1861 | err = got_error_from_errno("asprintf"); | |||
1862 | goto done; | |||
1863 | } | |||
1864 | ||||
1865 | if (!got_parse_sha1_digest(id.sha1, id_str)) { | |||
1866 | free(id_str); | |||
1867 | continue; | |||
1868 | } | |||
1869 | free(id_str); | |||
1870 | ||||
1871 | err = got_object_open_loose_fd(&fd, &id, repo); | |||
1872 | if (err) | |||
1873 | goto done; | |||
1874 | ||||
1875 | if (fstat(fd, &sb) == -1) { | |||
1876 | err = got_error_from_errno("fstat"); | |||
1877 | close(fd); | |||
1878 | goto done; | |||
1879 | } | |||
1880 | (*nobjects)++; | |||
1881 | (*ondisk_size) += sb.st_size; | |||
1882 | ||||
1883 | if (close(fd) == -1) { | |||
1884 | err = got_error_from_errno("close"); | |||
1885 | goto done; | |||
1886 | } | |||
1887 | } | |||
1888 | ||||
1889 | if (closedir(dir) != 0) { | |||
1890 | err = got_error_from_errno("closedir"); | |||
1891 | goto done; | |||
1892 | } | |||
1893 | dir = NULL((void *)0); | |||
1894 | ||||
1895 | free(path); | |||
1896 | path = NULL((void *)0); | |||
1897 | } | |||
1898 | done: | |||
1899 | if (dir && closedir(dir) != 0 && err == NULL((void *)0)) | |||
1900 | err = got_error_from_errno("closedir"); | |||
1901 | ||||
1902 | if (err) { | |||
1903 | *nobjects = 0; | |||
1904 | *ondisk_size = 0; | |||
1905 | } | |||
1906 | free(path_objects); | |||
1907 | free(path); | |||
1908 | return err; | |||
1909 | } | |||
1910 | ||||
1911 | const struct got_error * | |||
1912 | got_repo_get_packfile_info(int *npackfiles, int *nobjects, | |||
1913 | off_t *total_packsize, struct got_repository *repo) | |||
1914 | { | |||
1915 | const struct got_error *err; | |||
1916 | DIR *packdir = NULL((void *)0); | |||
1917 | struct dirent *dent; | |||
1918 | struct got_packidx *packidx = NULL((void *)0); | |||
1919 | char *path_packidx; | |||
1920 | char *path_packfile; | |||
1921 | int packdir_fd; | |||
1922 | struct stat sb; | |||
1923 | ||||
1924 | *npackfiles = 0; | |||
1925 | *nobjects = 0; | |||
1926 | *total_packsize = 0; | |||
1927 | ||||
1928 | packdir_fd = openat(got_repo_get_fd(repo), | |||
1929 | GOT_OBJECTS_PACK_DIR"objects/pack", O_DIRECTORY0x20000); | |||
1930 | if (packdir_fd == -1) { | |||
1931 | return got_error_from_errno_fmt("openat: %s/%s", | |||
1932 | got_repo_get_path_git_dir(repo), | |||
1933 | GOT_OBJECTS_PACK_DIR"objects/pack"); | |||
1934 | } | |||
1935 | ||||
1936 | packdir = fdopendir(packdir_fd); | |||
1937 | if (packdir == NULL((void *)0)) { | |||
1938 | err = got_error_from_errno("fdopendir"); | |||
1939 | goto done; | |||
1940 | } | |||
1941 | ||||
1942 | while ((dent = readdir(packdir)) != NULL((void *)0)) { | |||
1943 | if (!is_packidx_filename(dent->d_name, dent->d_namlen)) | |||
1944 | continue; | |||
1945 | ||||
1946 | if (asprintf(&path_packidx, "%s/%s", GOT_OBJECTS_PACK_DIR"objects/pack", | |||
1947 | dent->d_name) == -1) { | |||
1948 | err = got_error_from_errno("asprintf"); | |||
1949 | goto done; | |||
1950 | } | |||
1951 | ||||
1952 | err = got_packidx_open(&packidx, got_repo_get_fd(repo), | |||
1953 | path_packidx, 0); | |||
1954 | free(path_packidx); | |||
1955 | if (err) | |||
1956 | goto done; | |||
1957 | ||||
1958 | if (fstat(packidx->fd, &sb) == -1) | |||
1959 | goto done; | |||
1960 | *total_packsize += sb.st_size; | |||
1961 | ||||
1962 | err = got_packidx_get_packfile_path(&path_packfile, packidx); | |||
1963 | if (err) | |||
1964 | goto done; | |||
1965 | ||||
1966 | if (fstatat(got_repo_get_fd(repo), path_packfile, &sb, | |||
1967 | 0) == -1) { | |||
1968 | free(path_packfile); | |||
1969 | goto done; | |||
1970 | } | |||
1971 | free(path_packfile); | |||
1972 | *total_packsize += sb.st_size; | |||
1973 | ||||
1974 | *nobjects += be32toh(packidx->hdr.fanout_table[0xff])(__uint32_t)(__builtin_constant_p(packidx->hdr.fanout_table [0xff]) ? (__uint32_t)(((__uint32_t)(packidx->hdr.fanout_table [0xff]) & 0xff) << 24 | ((__uint32_t)(packidx->hdr .fanout_table[0xff]) & 0xff00) << 8 | ((__uint32_t) (packidx->hdr.fanout_table[0xff]) & 0xff0000) >> 8 | ((__uint32_t)(packidx->hdr.fanout_table[0xff]) & 0xff000000 ) >> 24) : __swap32md(packidx->hdr.fanout_table[0xff ])); | |||
1975 | ||||
1976 | (*npackfiles)++; | |||
1977 | ||||
1978 | got_packidx_close(packidx); | |||
1979 | packidx = NULL((void *)0); | |||
1980 | } | |||
1981 | done: | |||
1982 | if (packidx) | |||
1983 | got_packidx_close(packidx); | |||
1984 | if (packdir && closedir(packdir) != 0 && err == NULL((void *)0)) | |||
1985 | err = got_error_from_errno("closedir"); | |||
1986 | if (err) { | |||
1987 | *npackfiles = 0; | |||
1988 | *nobjects = 0; | |||
1989 | *total_packsize = 0; | |||
1990 | } | |||
1991 | return err; | |||
1992 | } |