Blame


1 7b19e0f1 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 7b19e0f1 2017-11-05 stsp *
4 7b19e0f1 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 7b19e0f1 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 7b19e0f1 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 7b19e0f1 2017-11-05 stsp *
8 7b19e0f1 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 7b19e0f1 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 7b19e0f1 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 7b19e0f1 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 7b19e0f1 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 7b19e0f1 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 7b19e0f1 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 7b19e0f1 2017-11-05 stsp */
16 7b19e0f1 2017-11-05 stsp
17 ad242220 2018-09-08 stsp #include <sys/types.h>
18 79b11c62 2018-03-09 stsp #include <sys/queue.h>
19 ad242220 2018-09-08 stsp #include <sys/uio.h>
20 deeca238 2018-03-12 stsp #include <sys/stat.h>
21 1510f469 2018-09-09 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 79b11c62 2018-03-09 stsp
24 e09a504c 2019-06-28 stsp #include <ctype.h>
25 1510f469 2018-09-09 stsp #include <fcntl.h>
26 3ce1b845 2019-07-15 stsp #include <fnmatch.h>
27 4027f31a 2017-11-04 stsp #include <limits.h>
28 1510f469 2018-09-09 stsp #include <dirent.h>
29 4027f31a 2017-11-04 stsp #include <stdlib.h>
30 4027f31a 2017-11-04 stsp #include <stdio.h>
31 4027f31a 2017-11-04 stsp #include <sha1.h>
32 4027f31a 2017-11-04 stsp #include <string.h>
33 79b11c62 2018-03-09 stsp #include <zlib.h>
34 85f51bba 2018-07-16 stsp #include <errno.h>
35 85f51bba 2018-07-16 stsp #include <libgen.h>
36 ad242220 2018-09-08 stsp #include <stdint.h>
37 ad242220 2018-09-08 stsp #include <imsg.h>
38 c442a90d 2019-03-10 stsp #include <uuid.h>
39 4027f31a 2017-11-04 stsp
40 4027f31a 2017-11-04 stsp #include "got_error.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 4027f31a 2017-11-04 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 442a3ddc 2018-04-23 stsp #include "got_worktree.h"
45 7bb0daa1 2018-06-21 stsp #include "got_object.h"
46 4027f31a 2017-11-04 stsp
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 3ce1b845 2019-07-15 stsp #include "got_lib_object_parse.h"
51 3ce1b845 2019-07-15 stsp #include "got_lib_object_create.h"
52 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
53 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
54 442a3ddc 2018-04-23 stsp #include "got_lib_worktree.h"
55 e09a504c 2019-06-28 stsp #include "got_lib_sha1.h"
56 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
57 6bef87be 2018-09-11 stsp #include "got_lib_repository.h"
58 c3f94f68 2017-11-05 stsp
59 79b11c62 2018-03-09 stsp #ifndef nitems
60 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
61 79b11c62 2018-03-09 stsp #endif
62 3b339b2f 2018-02-12 stsp
63 4027f31a 2017-11-04 stsp #define GOT_GIT_DIR ".git"
64 4027f31a 2017-11-04 stsp
65 4027f31a 2017-11-04 stsp /* Mandatory files and directories inside the git directory. */
66 4df642d9 2017-11-05 stsp #define GOT_OBJECTS_DIR "objects"
67 4df642d9 2017-11-05 stsp #define GOT_REFS_DIR "refs"
68 4df642d9 2017-11-05 stsp #define GOT_HEAD_FILE "HEAD"
69 4027f31a 2017-11-04 stsp
70 a1fd68d8 2018-01-12 stsp /* Other files and directories inside the git directory. */
71 4df642d9 2017-11-05 stsp #define GOT_FETCH_HEAD_FILE "FETCH_HEAD"
72 4df642d9 2017-11-05 stsp #define GOT_ORIG_HEAD_FILE "ORIG_HEAD"
73 a1fd68d8 2018-01-12 stsp #define GOT_OBJECTS_PACK_DIR "objects/pack"
74 fb79db15 2019-02-01 stsp #define GOT_PACKED_REFS_FILE "packed-refs"
75 4df642d9 2017-11-05 stsp
76 7839bc15 2019-01-06 stsp const char *
77 86c3caaf 2018-03-09 stsp got_repo_get_path(struct got_repository *repo)
78 86c3caaf 2018-03-09 stsp {
79 7839bc15 2019-01-06 stsp return repo->path;
80 86c3caaf 2018-03-09 stsp }
81 86c3caaf 2018-03-09 stsp
82 6e9da951 2019-01-06 stsp const char *
83 11995603 2017-11-05 stsp got_repo_get_path_git_dir(struct got_repository *repo)
84 4027f31a 2017-11-04 stsp {
85 6e9da951 2019-01-06 stsp return repo->path_git_dir;
86 04ca23f4 2018-07-16 stsp }
87 04ca23f4 2018-07-16 stsp
88 04ca23f4 2018-07-16 stsp int
89 04ca23f4 2018-07-16 stsp got_repo_is_bare(struct got_repository *repo)
90 04ca23f4 2018-07-16 stsp {
91 04ca23f4 2018-07-16 stsp return (strcmp(repo->path, repo->path_git_dir) == 0);
92 4027f31a 2017-11-04 stsp }
93 4027f31a 2017-11-04 stsp
94 4027f31a 2017-11-04 stsp static char *
95 4027f31a 2017-11-04 stsp get_path_git_child(struct got_repository *repo, const char *basename)
96 4027f31a 2017-11-04 stsp {
97 4027f31a 2017-11-04 stsp char *path_child;
98 4027f31a 2017-11-04 stsp
99 4986b9d5 2018-03-12 stsp if (asprintf(&path_child, "%s/%s", repo->path_git_dir,
100 4027f31a 2017-11-04 stsp basename) == -1)
101 4027f31a 2017-11-04 stsp return NULL;
102 4027f31a 2017-11-04 stsp
103 4027f31a 2017-11-04 stsp return path_child;
104 4027f31a 2017-11-04 stsp }
105 4027f31a 2017-11-04 stsp
106 11995603 2017-11-05 stsp char *
107 11995603 2017-11-05 stsp got_repo_get_path_objects(struct got_repository *repo)
108 4027f31a 2017-11-04 stsp {
109 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_OBJECTS_DIR);
110 4027f31a 2017-11-04 stsp }
111 4027f31a 2017-11-04 stsp
112 11995603 2017-11-05 stsp char *
113 a1fd68d8 2018-01-12 stsp got_repo_get_path_objects_pack(struct got_repository *repo)
114 a1fd68d8 2018-01-12 stsp {
115 a1fd68d8 2018-01-12 stsp return get_path_git_child(repo, GOT_OBJECTS_PACK_DIR);
116 a1fd68d8 2018-01-12 stsp }
117 a1fd68d8 2018-01-12 stsp
118 a1fd68d8 2018-01-12 stsp char *
119 11995603 2017-11-05 stsp got_repo_get_path_refs(struct got_repository *repo)
120 4027f31a 2017-11-04 stsp {
121 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_REFS_DIR);
122 4027f31a 2017-11-04 stsp }
123 4027f31a 2017-11-04 stsp
124 fb79db15 2019-02-01 stsp char *
125 fb79db15 2019-02-01 stsp got_repo_get_path_packed_refs(struct got_repository *repo)
126 fb79db15 2019-02-01 stsp {
127 fb79db15 2019-02-01 stsp return get_path_git_child(repo, GOT_PACKED_REFS_FILE);
128 fb79db15 2019-02-01 stsp }
129 fb79db15 2019-02-01 stsp
130 4027f31a 2017-11-04 stsp static char *
131 4027f31a 2017-11-04 stsp get_path_head(struct got_repository *repo)
132 4027f31a 2017-11-04 stsp {
133 4027f31a 2017-11-04 stsp return get_path_git_child(repo, GOT_HEAD_FILE);
134 4027f31a 2017-11-04 stsp }
135 4027f31a 2017-11-04 stsp
136 4027f31a 2017-11-04 stsp static int
137 4027f31a 2017-11-04 stsp is_git_repo(struct got_repository *repo)
138 4027f31a 2017-11-04 stsp {
139 6e9da951 2019-01-06 stsp const char *path_git = got_repo_get_path_git_dir(repo);
140 11995603 2017-11-05 stsp char *path_objects = got_repo_get_path_objects(repo);
141 11995603 2017-11-05 stsp char *path_refs = got_repo_get_path_refs(repo);
142 4027f31a 2017-11-04 stsp char *path_head = get_path_head(repo);
143 deeca238 2018-03-12 stsp int ret = 0;
144 deeca238 2018-03-12 stsp struct stat sb;
145 4847cca1 2018-03-12 stsp struct got_reference *head_ref;
146 4027f31a 2017-11-04 stsp
147 deeca238 2018-03-12 stsp if (lstat(path_git, &sb) == -1)
148 deeca238 2018-03-12 stsp goto done;
149 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
150 deeca238 2018-03-12 stsp goto done;
151 4027f31a 2017-11-04 stsp
152 deeca238 2018-03-12 stsp if (lstat(path_objects, &sb) == -1)
153 deeca238 2018-03-12 stsp goto done;
154 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
155 deeca238 2018-03-12 stsp goto done;
156 deeca238 2018-03-12 stsp
157 deeca238 2018-03-12 stsp if (lstat(path_refs, &sb) == -1)
158 deeca238 2018-03-12 stsp goto done;
159 deeca238 2018-03-12 stsp if (!S_ISDIR(sb.st_mode))
160 deeca238 2018-03-12 stsp goto done;
161 deeca238 2018-03-12 stsp
162 deeca238 2018-03-12 stsp if (lstat(path_head, &sb) == -1)
163 deeca238 2018-03-12 stsp goto done;
164 deeca238 2018-03-12 stsp if (!S_ISREG(sb.st_mode))
165 deeca238 2018-03-12 stsp goto done;
166 4847cca1 2018-03-12 stsp
167 4847cca1 2018-03-12 stsp /* Check if the HEAD reference can be opened. */
168 2f17228e 2019-05-12 stsp if (got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0) != NULL)
169 4847cca1 2018-03-12 stsp goto done;
170 4847cca1 2018-03-12 stsp got_ref_close(head_ref);
171 4847cca1 2018-03-12 stsp
172 deeca238 2018-03-12 stsp ret = 1;
173 deeca238 2018-03-12 stsp done:
174 4027f31a 2017-11-04 stsp free(path_objects);
175 4027f31a 2017-11-04 stsp free(path_refs);
176 4027f31a 2017-11-04 stsp free(path_head);
177 4027f31a 2017-11-04 stsp return ret;
178 4027f31a 2017-11-04 stsp
179 7bb0daa1 2018-06-21 stsp }
180 7bb0daa1 2018-06-21 stsp
181 f6be5c30 2018-06-22 stsp const struct got_error *
182 f6be5c30 2018-06-22 stsp got_repo_cache_object(struct got_repository *repo, struct got_object_id *id,
183 f6be5c30 2018-06-22 stsp struct got_object *obj)
184 f6be5c30 2018-06-22 stsp {
185 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
186 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
187 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->objcache, id, obj);
188 79c99a64 2019-05-23 stsp if (err) {
189 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
190 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
191 79c99a64 2019-05-23 stsp err = NULL;
192 f6be5c30 2018-06-22 stsp return err;
193 79c99a64 2019-05-23 stsp }
194 f6be5c30 2018-06-22 stsp obj->refcnt++;
195 ccfe88e6 2018-07-12 stsp #endif
196 f6be5c30 2018-06-22 stsp return NULL;
197 f6be5c30 2018-06-22 stsp }
198 f6be5c30 2018-06-22 stsp
199 7bb0daa1 2018-06-21 stsp struct got_object *
200 7bb0daa1 2018-06-21 stsp got_repo_get_cached_object(struct got_repository *repo,
201 7bb0daa1 2018-06-21 stsp struct got_object_id *id)
202 7bb0daa1 2018-06-21 stsp {
203 6bef87be 2018-09-11 stsp return (struct got_object *)got_object_cache_get(&repo->objcache, id);
204 7bb0daa1 2018-06-21 stsp }
205 7bb0daa1 2018-06-21 stsp
206 4027f31a 2017-11-04 stsp const struct got_error *
207 f6be5c30 2018-06-22 stsp got_repo_cache_tree(struct got_repository *repo, struct got_object_id *id,
208 f6be5c30 2018-06-22 stsp struct got_tree_object *tree)
209 f6be5c30 2018-06-22 stsp {
210 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
211 f6be5c30 2018-06-22 stsp const struct got_error *err = NULL;
212 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->treecache, id, tree);
213 79c99a64 2019-05-23 stsp if (err) {
214 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
215 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
216 79c99a64 2019-05-23 stsp err = NULL;
217 f6be5c30 2018-06-22 stsp return err;
218 79c99a64 2019-05-23 stsp }
219 f6be5c30 2018-06-22 stsp tree->refcnt++;
220 ccfe88e6 2018-07-12 stsp #endif
221 f6be5c30 2018-06-22 stsp return NULL;
222 f6be5c30 2018-06-22 stsp }
223 f6be5c30 2018-06-22 stsp
224 f6be5c30 2018-06-22 stsp struct got_tree_object *
225 f6be5c30 2018-06-22 stsp got_repo_get_cached_tree(struct got_repository *repo,
226 f6be5c30 2018-06-22 stsp struct got_object_id *id)
227 f6be5c30 2018-06-22 stsp {
228 6bef87be 2018-09-11 stsp return (struct got_tree_object *)got_object_cache_get(
229 6bef87be 2018-09-11 stsp &repo->treecache, id);
230 1943de01 2018-06-22 stsp }
231 1943de01 2018-06-22 stsp
232 1943de01 2018-06-22 stsp const struct got_error *
233 1943de01 2018-06-22 stsp got_repo_cache_commit(struct got_repository *repo, struct got_object_id *id,
234 1943de01 2018-06-22 stsp struct got_commit_object *commit)
235 1943de01 2018-06-22 stsp {
236 ccfe88e6 2018-07-12 stsp #ifndef GOT_NO_OBJ_CACHE
237 1943de01 2018-06-22 stsp const struct got_error *err = NULL;
238 6bef87be 2018-09-11 stsp err = got_object_cache_add(&repo->commitcache, id, commit);
239 79c99a64 2019-05-23 stsp if (err) {
240 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
241 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
242 79c99a64 2019-05-23 stsp err = NULL;
243 1943de01 2018-06-22 stsp return err;
244 79c99a64 2019-05-23 stsp }
245 1943de01 2018-06-22 stsp commit->refcnt++;
246 ccfe88e6 2018-07-12 stsp #endif
247 f6be5c30 2018-06-22 stsp return NULL;
248 f6be5c30 2018-06-22 stsp }
249 f6be5c30 2018-06-22 stsp
250 1943de01 2018-06-22 stsp struct got_commit_object *
251 1943de01 2018-06-22 stsp got_repo_get_cached_commit(struct got_repository *repo,
252 1943de01 2018-06-22 stsp struct got_object_id *id)
253 1943de01 2018-06-22 stsp {
254 6bef87be 2018-09-11 stsp return (struct got_commit_object *)got_object_cache_get(
255 6bef87be 2018-09-11 stsp &repo->commitcache, id);
256 f4a881ce 2018-11-17 stsp }
257 f4a881ce 2018-11-17 stsp
258 f4a881ce 2018-11-17 stsp const struct got_error *
259 f4a881ce 2018-11-17 stsp got_repo_cache_tag(struct got_repository *repo, struct got_object_id *id,
260 f4a881ce 2018-11-17 stsp struct got_tag_object *tag)
261 f4a881ce 2018-11-17 stsp {
262 f4a881ce 2018-11-17 stsp #ifndef GOT_NO_OBJ_CACHE
263 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
264 f4a881ce 2018-11-17 stsp err = got_object_cache_add(&repo->tagcache, id, tag);
265 79c99a64 2019-05-23 stsp if (err) {
266 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
267 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
268 79c99a64 2019-05-23 stsp err = NULL;
269 f4a881ce 2018-11-17 stsp return err;
270 79c99a64 2019-05-23 stsp }
271 f4a881ce 2018-11-17 stsp tag->refcnt++;
272 f4a881ce 2018-11-17 stsp #endif
273 f4a881ce 2018-11-17 stsp return NULL;
274 f4a881ce 2018-11-17 stsp }
275 f4a881ce 2018-11-17 stsp
276 f4a881ce 2018-11-17 stsp struct got_tag_object *
277 f4a881ce 2018-11-17 stsp got_repo_get_cached_tag(struct got_repository *repo, struct got_object_id *id)
278 f4a881ce 2018-11-17 stsp {
279 f4a881ce 2018-11-17 stsp return (struct got_tag_object *)got_object_cache_get(
280 f4a881ce 2018-11-17 stsp &repo->tagcache, id);
281 1943de01 2018-06-22 stsp }
282 1943de01 2018-06-22 stsp
283 f6be5c30 2018-06-22 stsp const struct got_error *
284 85f51bba 2018-07-16 stsp open_repo(struct got_repository *repo, const char *path)
285 4027f31a 2017-11-04 stsp {
286 85f51bba 2018-07-16 stsp const struct got_error *err = NULL;
287 85f51bba 2018-07-16 stsp
288 85f51bba 2018-07-16 stsp /* bare git repository? */
289 85f51bba 2018-07-16 stsp repo->path_git_dir = strdup(path);
290 ee645855 2019-02-05 stsp if (repo->path_git_dir == NULL)
291 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
292 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
293 85f51bba 2018-07-16 stsp repo->path = strdup(repo->path_git_dir);
294 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
295 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
296 85f51bba 2018-07-16 stsp goto done;
297 85f51bba 2018-07-16 stsp }
298 85f51bba 2018-07-16 stsp return NULL;
299 85f51bba 2018-07-16 stsp }
300 85f51bba 2018-07-16 stsp
301 85f51bba 2018-07-16 stsp /* git repository with working tree? */
302 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
303 85f51bba 2018-07-16 stsp if (asprintf(&repo->path_git_dir, "%s/%s", path, GOT_GIT_DIR) == -1) {
304 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
305 85f51bba 2018-07-16 stsp goto done;
306 85f51bba 2018-07-16 stsp }
307 85f51bba 2018-07-16 stsp if (is_git_repo(repo)) {
308 85f51bba 2018-07-16 stsp repo->path = strdup(path);
309 85f51bba 2018-07-16 stsp if (repo->path == NULL) {
310 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
311 85f51bba 2018-07-16 stsp goto done;
312 85f51bba 2018-07-16 stsp }
313 85f51bba 2018-07-16 stsp return NULL;
314 85f51bba 2018-07-16 stsp }
315 85f51bba 2018-07-16 stsp
316 ee645855 2019-02-05 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
317 ee645855 2019-02-05 stsp done:
318 85f51bba 2018-07-16 stsp if (err) {
319 ee645855 2019-02-05 stsp free(repo->path);
320 ee645855 2019-02-05 stsp repo->path = NULL;
321 85f51bba 2018-07-16 stsp free(repo->path_git_dir);
322 ee645855 2019-02-05 stsp repo->path_git_dir = NULL;
323 85f51bba 2018-07-16 stsp }
324 85f51bba 2018-07-16 stsp return err;
325 85f51bba 2018-07-16 stsp }
326 85f51bba 2018-07-16 stsp
327 85f51bba 2018-07-16 stsp const struct got_error *
328 85f51bba 2018-07-16 stsp got_repo_open(struct got_repository **repop, const char *path)
329 85f51bba 2018-07-16 stsp {
330 92af5469 2017-11-05 stsp struct got_repository *repo = NULL;
331 92af5469 2017-11-05 stsp const struct got_error *err = NULL;
332 85f51bba 2018-07-16 stsp char *abspath, *normpath = NULL;
333 ad242220 2018-09-08 stsp int i, tried_root = 0;
334 4027f31a 2017-11-04 stsp
335 85f51bba 2018-07-16 stsp *repop = NULL;
336 85f51bba 2018-07-16 stsp
337 2393f13b 2018-03-09 stsp if (got_path_is_absolute(path))
338 2393f13b 2018-03-09 stsp abspath = strdup(path);
339 2393f13b 2018-03-09 stsp else
340 2393f13b 2018-03-09 stsp abspath = got_path_get_absolute(path);
341 92af5469 2017-11-05 stsp if (abspath == NULL)
342 92af5469 2017-11-05 stsp return got_error(GOT_ERR_BAD_PATH);
343 4027f31a 2017-11-04 stsp
344 4027f31a 2017-11-04 stsp repo = calloc(1, sizeof(*repo));
345 92af5469 2017-11-05 stsp if (repo == NULL) {
346 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
347 92af5469 2017-11-05 stsp goto done;
348 92af5469 2017-11-05 stsp }
349 4027f31a 2017-11-04 stsp
350 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
351 3516b818 2018-09-08 stsp memset(&repo->privsep_children[i], 0,
352 3516b818 2018-09-08 stsp sizeof(repo->privsep_children[0]));
353 ad242220 2018-09-08 stsp repo->privsep_children[i].imsg_fd = -1;
354 ad242220 2018-09-08 stsp }
355 ad242220 2018-09-08 stsp
356 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->objcache,
357 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_OBJ);
358 6bef87be 2018-09-11 stsp if (err)
359 f6be5c30 2018-06-22 stsp goto done;
360 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->treecache,
361 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_TREE);
362 6bef87be 2018-09-11 stsp if (err)
363 1943de01 2018-06-22 stsp goto done;
364 6bef87be 2018-09-11 stsp err = got_object_cache_init(&repo->commitcache,
365 6bef87be 2018-09-11 stsp GOT_OBJECT_CACHE_TYPE_COMMIT);
366 6bef87be 2018-09-11 stsp if (err)
367 eb77ee11 2018-07-08 stsp goto done;
368 f4a881ce 2018-11-17 stsp err = got_object_cache_init(&repo->tagcache,
369 f4a881ce 2018-11-17 stsp GOT_OBJECT_CACHE_TYPE_TAG);
370 f4a881ce 2018-11-17 stsp if (err)
371 f4a881ce 2018-11-17 stsp goto done;
372 1943de01 2018-06-22 stsp
373 85f51bba 2018-07-16 stsp normpath = got_path_normalize(abspath);
374 85f51bba 2018-07-16 stsp if (normpath == NULL) {
375 92af5469 2017-11-05 stsp err = got_error(GOT_ERR_BAD_PATH);
376 92af5469 2017-11-05 stsp goto done;
377 92af5469 2017-11-05 stsp }
378 4027f31a 2017-11-04 stsp
379 85f51bba 2018-07-16 stsp path = normpath;
380 85f51bba 2018-07-16 stsp do {
381 85f51bba 2018-07-16 stsp err = open_repo(repo, path);
382 85f51bba 2018-07-16 stsp if (err == NULL)
383 85f51bba 2018-07-16 stsp break;
384 85f51bba 2018-07-16 stsp if (err->code != GOT_ERR_NOT_GIT_REPO)
385 85f51bba 2018-07-16 stsp break;
386 85f51bba 2018-07-16 stsp if (path[0] == '/' && path[1] == '\0') {
387 85f51bba 2018-07-16 stsp if (tried_root) {
388 85f51bba 2018-07-16 stsp err = got_error(GOT_ERR_NOT_GIT_REPO);
389 85f51bba 2018-07-16 stsp break;
390 442a3ddc 2018-04-23 stsp }
391 85f51bba 2018-07-16 stsp tried_root = 1;
392 442a3ddc 2018-04-23 stsp }
393 85f51bba 2018-07-16 stsp path = dirname(path);
394 85f51bba 2018-07-16 stsp if (path == NULL)
395 638f9024 2019-05-13 stsp err = got_error_from_errno2("dirname", path);
396 85f51bba 2018-07-16 stsp } while (path);
397 92af5469 2017-11-05 stsp done:
398 92af5469 2017-11-05 stsp if (err)
399 5c2f5761 2018-09-19 stsp got_repo_close(repo);
400 85f51bba 2018-07-16 stsp else
401 85f51bba 2018-07-16 stsp *repop = repo;
402 92af5469 2017-11-05 stsp free(abspath);
403 85f51bba 2018-07-16 stsp free(normpath);
404 92af5469 2017-11-05 stsp return err;
405 4027f31a 2017-11-04 stsp }
406 4027f31a 2017-11-04 stsp
407 ad242220 2018-09-08 stsp const struct got_error *
408 4027f31a 2017-11-04 stsp got_repo_close(struct got_repository *repo)
409 4027f31a 2017-11-04 stsp {
410 ad242220 2018-09-08 stsp const struct got_error *err = NULL, *child_err;
411 79b11c62 2018-03-09 stsp int i;
412 79b11c62 2018-03-09 stsp
413 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
414 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
415 79b11c62 2018-03-09 stsp break;
416 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i]);
417 79b11c62 2018-03-09 stsp }
418 bd1223b9 2018-03-14 stsp
419 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
420 7e656b93 2018-03-17 stsp if (repo->packs[i].path_packfile == NULL)
421 7e656b93 2018-03-17 stsp break;
422 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i]);
423 7e656b93 2018-03-17 stsp }
424 7e656b93 2018-03-17 stsp
425 4027f31a 2017-11-04 stsp free(repo->path);
426 4986b9d5 2018-03-12 stsp free(repo->path_git_dir);
427 cd717821 2018-06-22 stsp
428 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->objcache);
429 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->treecache);
430 6bef87be 2018-09-11 stsp got_object_cache_close(&repo->commitcache);
431 f4a881ce 2018-11-17 stsp got_object_cache_close(&repo->tagcache);
432 ad242220 2018-09-08 stsp
433 ad242220 2018-09-08 stsp for (i = 0; i < nitems(repo->privsep_children); i++) {
434 ad242220 2018-09-08 stsp if (repo->privsep_children[i].imsg_fd == -1)
435 ad242220 2018-09-08 stsp continue;
436 3516b818 2018-09-08 stsp imsg_clear(repo->privsep_children[i].ibuf);
437 3516b818 2018-09-08 stsp free(repo->privsep_children[i].ibuf);
438 ad242220 2018-09-08 stsp err = got_privsep_send_stop(repo->privsep_children[i].imsg_fd);
439 876c234b 2018-09-10 stsp child_err = got_privsep_wait_for_child(
440 876c234b 2018-09-10 stsp repo->privsep_children[i].pid);
441 ad242220 2018-09-08 stsp if (child_err && err == NULL)
442 ad242220 2018-09-08 stsp err = child_err;
443 3a6ce05a 2019-02-11 stsp if (close(repo->privsep_children[i].imsg_fd) != 0 &&
444 3a6ce05a 2019-02-11 stsp err == NULL)
445 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
446 ad242220 2018-09-08 stsp }
447 4027f31a 2017-11-04 stsp free(repo);
448 ad242220 2018-09-08 stsp
449 ad242220 2018-09-08 stsp return err;
450 4027f31a 2017-11-04 stsp }
451 04ca23f4 2018-07-16 stsp
452 04ca23f4 2018-07-16 stsp const struct got_error *
453 04ca23f4 2018-07-16 stsp got_repo_map_path(char **in_repo_path, struct got_repository *repo,
454 23721109 2018-10-22 stsp const char *input_path, int check_disk)
455 04ca23f4 2018-07-16 stsp {
456 04ca23f4 2018-07-16 stsp const struct got_error *err = NULL;
457 7839bc15 2019-01-06 stsp const char *repo_abspath = NULL;
458 04ca23f4 2018-07-16 stsp size_t repolen, cwdlen, len;
459 7839bc15 2019-01-06 stsp char *cwd, *canonpath, *path = NULL;
460 04ca23f4 2018-07-16 stsp
461 04ca23f4 2018-07-16 stsp *in_repo_path = NULL;
462 04ca23f4 2018-07-16 stsp
463 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
464 04ca23f4 2018-07-16 stsp if (cwd == NULL)
465 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
466 04ca23f4 2018-07-16 stsp
467 04ca23f4 2018-07-16 stsp canonpath = strdup(input_path);
468 04ca23f4 2018-07-16 stsp if (canonpath == NULL) {
469 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
470 04ca23f4 2018-07-16 stsp goto done;
471 04ca23f4 2018-07-16 stsp }
472 04ca23f4 2018-07-16 stsp err = got_canonpath(input_path, canonpath, strlen(canonpath) + 1);
473 04ca23f4 2018-07-16 stsp if (err)
474 04ca23f4 2018-07-16 stsp goto done;
475 04ca23f4 2018-07-16 stsp
476 04ca23f4 2018-07-16 stsp repo_abspath = got_repo_get_path(repo);
477 04ca23f4 2018-07-16 stsp
478 2840f715 2019-07-11 stsp if (!check_disk || canonpath[0] == '\0') {
479 23721109 2018-10-22 stsp path = strdup(canonpath);
480 b70703ad 2019-03-18 stsp if (path == NULL) {
481 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
482 04ca23f4 2018-07-16 stsp goto done;
483 04ca23f4 2018-07-16 stsp }
484 04ca23f4 2018-07-16 stsp } else {
485 04ca23f4 2018-07-16 stsp int is_repo_child = 0, is_cwd_child = 0;
486 04ca23f4 2018-07-16 stsp
487 04ca23f4 2018-07-16 stsp path = realpath(canonpath, NULL);
488 04ca23f4 2018-07-16 stsp if (path == NULL) {
489 b70703ad 2019-03-18 stsp if (errno != ENOENT) {
490 638f9024 2019-05-13 stsp err = got_error_from_errno2("realpath",
491 230a42bd 2019-05-11 jcs canonpath);
492 b70703ad 2019-03-18 stsp goto done;
493 b70703ad 2019-03-18 stsp }
494 b70703ad 2019-03-18 stsp /*
495 b70703ad 2019-03-18 stsp * Path is not on disk.
496 b70703ad 2019-03-18 stsp * Assume it is already relative to repository root.
497 b70703ad 2019-03-18 stsp */
498 b70703ad 2019-03-18 stsp path = strdup(canonpath);
499 b70703ad 2019-03-18 stsp if (path == NULL) {
500 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
501 b70703ad 2019-03-18 stsp goto done;
502 b70703ad 2019-03-18 stsp }
503 04ca23f4 2018-07-16 stsp }
504 04ca23f4 2018-07-16 stsp
505 04ca23f4 2018-07-16 stsp repolen = strlen(repo_abspath);
506 04ca23f4 2018-07-16 stsp cwdlen = strlen(cwd);
507 04ca23f4 2018-07-16 stsp len = strlen(path);
508 04ca23f4 2018-07-16 stsp
509 04ca23f4 2018-07-16 stsp if (len > repolen && strncmp(path, repo_abspath, repolen) == 0)
510 04ca23f4 2018-07-16 stsp is_repo_child = 1;
511 04ca23f4 2018-07-16 stsp if (len > cwdlen && strncmp(path, cwd, cwdlen) == 0)
512 04ca23f4 2018-07-16 stsp is_cwd_child = 1;
513 04ca23f4 2018-07-16 stsp
514 04ca23f4 2018-07-16 stsp if (strcmp(path, repo_abspath) == 0) {
515 04ca23f4 2018-07-16 stsp free(path);
516 04ca23f4 2018-07-16 stsp path = strdup("");
517 04ca23f4 2018-07-16 stsp if (path == NULL) {
518 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
519 04ca23f4 2018-07-16 stsp goto done;
520 04ca23f4 2018-07-16 stsp }
521 04ca23f4 2018-07-16 stsp } else if (is_repo_child && is_cwd_child) {
522 04ca23f4 2018-07-16 stsp char *child;
523 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
524 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
525 04ca23f4 2018-07-16 stsp repo_abspath, path);
526 04ca23f4 2018-07-16 stsp if (err)
527 04ca23f4 2018-07-16 stsp goto done;
528 04ca23f4 2018-07-16 stsp free(path);
529 04ca23f4 2018-07-16 stsp path = child;
530 04ca23f4 2018-07-16 stsp } else if (is_repo_child) {
531 04ca23f4 2018-07-16 stsp /* Matched an on-disk path inside repository. */
532 04ca23f4 2018-07-16 stsp if (got_repo_is_bare(repo)) {
533 04ca23f4 2018-07-16 stsp /*
534 04ca23f4 2018-07-16 stsp * Matched an on-disk path inside repository
535 04ca23f4 2018-07-16 stsp * database. Treat as repository-relative.
536 04ca23f4 2018-07-16 stsp */
537 04ca23f4 2018-07-16 stsp } else {
538 04ca23f4 2018-07-16 stsp char *child;
539 04ca23f4 2018-07-16 stsp /* Strip common prefix with repository path. */
540 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child,
541 04ca23f4 2018-07-16 stsp repo_abspath, path);
542 04ca23f4 2018-07-16 stsp if (err)
543 04ca23f4 2018-07-16 stsp goto done;
544 04ca23f4 2018-07-16 stsp free(path);
545 04ca23f4 2018-07-16 stsp path = child;
546 04ca23f4 2018-07-16 stsp }
547 04ca23f4 2018-07-16 stsp } else if (is_cwd_child) {
548 04ca23f4 2018-07-16 stsp char *child;
549 04ca23f4 2018-07-16 stsp /* Strip common prefix with cwd. */
550 04ca23f4 2018-07-16 stsp err = got_path_skip_common_ancestor(&child, cwd,
551 04ca23f4 2018-07-16 stsp path);
552 04ca23f4 2018-07-16 stsp if (err)
553 04ca23f4 2018-07-16 stsp goto done;
554 04ca23f4 2018-07-16 stsp free(path);
555 04ca23f4 2018-07-16 stsp path = child;
556 04ca23f4 2018-07-16 stsp } else {
557 04ca23f4 2018-07-16 stsp /*
558 04ca23f4 2018-07-16 stsp * Matched unrelated on-disk path.
559 04ca23f4 2018-07-16 stsp * Treat it as repository-relative.
560 04ca23f4 2018-07-16 stsp */
561 04ca23f4 2018-07-16 stsp }
562 04ca23f4 2018-07-16 stsp }
563 04ca23f4 2018-07-16 stsp
564 04ca23f4 2018-07-16 stsp /* Make in-repository path absolute */
565 04ca23f4 2018-07-16 stsp if (path[0] != '/') {
566 04ca23f4 2018-07-16 stsp char *abspath;
567 04ca23f4 2018-07-16 stsp if (asprintf(&abspath, "/%s", path) == -1) {
568 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
569 04ca23f4 2018-07-16 stsp goto done;
570 04ca23f4 2018-07-16 stsp }
571 04ca23f4 2018-07-16 stsp free(path);
572 04ca23f4 2018-07-16 stsp path = abspath;
573 04ca23f4 2018-07-16 stsp }
574 04ca23f4 2018-07-16 stsp
575 04ca23f4 2018-07-16 stsp done:
576 04ca23f4 2018-07-16 stsp free(cwd);
577 04ca23f4 2018-07-16 stsp free(canonpath);
578 04ca23f4 2018-07-16 stsp if (err)
579 04ca23f4 2018-07-16 stsp free(path);
580 04ca23f4 2018-07-16 stsp else
581 04ca23f4 2018-07-16 stsp *in_repo_path = path;
582 1510f469 2018-09-09 stsp return err;
583 1510f469 2018-09-09 stsp }
584 1510f469 2018-09-09 stsp
585 1510f469 2018-09-09 stsp const struct got_error *
586 1510f469 2018-09-09 stsp got_repo_cache_packidx(struct got_repository *repo, struct got_packidx *packidx)
587 1510f469 2018-09-09 stsp {
588 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
589 1510f469 2018-09-09 stsp int i;
590 1510f469 2018-09-09 stsp
591 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
592 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
593 1510f469 2018-09-09 stsp break;
594 1510f469 2018-09-09 stsp }
595 1510f469 2018-09-09 stsp if (i == nitems(repo->packidx_cache)) {
596 1510f469 2018-09-09 stsp err = got_packidx_close(repo->packidx_cache[i - 1]);
597 1510f469 2018-09-09 stsp if (err)
598 1510f469 2018-09-09 stsp return err;
599 1510f469 2018-09-09 stsp }
600 1510f469 2018-09-09 stsp
601 15fe583f 2018-11-05 stsp /*
602 15fe583f 2018-11-05 stsp * Insert the new pack index at the front so it will
603 15fe583f 2018-11-05 stsp * be searched first in the future.
604 15fe583f 2018-11-05 stsp */
605 15fe583f 2018-11-05 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
606 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache) -
607 15fe583f 2018-11-05 stsp sizeof(repo->packidx_cache[0]));
608 15fe583f 2018-11-05 stsp repo->packidx_cache[0] = packidx;
609 15fe583f 2018-11-05 stsp
610 1510f469 2018-09-09 stsp return NULL;
611 1510f469 2018-09-09 stsp }
612 1510f469 2018-09-09 stsp
613 1510f469 2018-09-09 stsp static int
614 1510f469 2018-09-09 stsp is_packidx_filename(const char *name, size_t len)
615 1510f469 2018-09-09 stsp {
616 1510f469 2018-09-09 stsp if (len != GOT_PACKIDX_NAMELEN)
617 1510f469 2018-09-09 stsp return 0;
618 1510f469 2018-09-09 stsp
619 1510f469 2018-09-09 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
620 1510f469 2018-09-09 stsp return 0;
621 1510f469 2018-09-09 stsp
622 1510f469 2018-09-09 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
623 1510f469 2018-09-09 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
624 1510f469 2018-09-09 stsp return 0;
625 1510f469 2018-09-09 stsp
626 1510f469 2018-09-09 stsp return 1;
627 1510f469 2018-09-09 stsp }
628 1510f469 2018-09-09 stsp
629 1510f469 2018-09-09 stsp const struct got_error *
630 1510f469 2018-09-09 stsp got_repo_search_packidx(struct got_packidx **packidx, int *idx,
631 1510f469 2018-09-09 stsp struct got_repository *repo, struct got_object_id *id)
632 1510f469 2018-09-09 stsp {
633 1510f469 2018-09-09 stsp const struct got_error *err;
634 1510f469 2018-09-09 stsp char *path_packdir;
635 1510f469 2018-09-09 stsp DIR *packdir;
636 1510f469 2018-09-09 stsp struct dirent *dent;
637 1510f469 2018-09-09 stsp char *path_packidx;
638 1510f469 2018-09-09 stsp int i;
639 1510f469 2018-09-09 stsp
640 1510f469 2018-09-09 stsp /* Search pack index cache. */
641 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
642 1510f469 2018-09-09 stsp if (repo->packidx_cache[i] == NULL)
643 1510f469 2018-09-09 stsp break;
644 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(repo->packidx_cache[i], id);
645 1510f469 2018-09-09 stsp if (*idx != -1) {
646 1510f469 2018-09-09 stsp *packidx = repo->packidx_cache[i];
647 1510f469 2018-09-09 stsp return NULL;
648 1510f469 2018-09-09 stsp }
649 1510f469 2018-09-09 stsp }
650 1510f469 2018-09-09 stsp /* No luck. Search the filesystem. */
651 1510f469 2018-09-09 stsp
652 1510f469 2018-09-09 stsp path_packdir = got_repo_get_path_objects_pack(repo);
653 1510f469 2018-09-09 stsp if (path_packdir == NULL)
654 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
655 1510f469 2018-09-09 stsp
656 1510f469 2018-09-09 stsp packdir = opendir(path_packdir);
657 1510f469 2018-09-09 stsp if (packdir == NULL) {
658 638f9024 2019-05-13 stsp err = got_error_from_errno2("opendir", path_packdir);
659 1510f469 2018-09-09 stsp goto done;
660 1510f469 2018-09-09 stsp }
661 1510f469 2018-09-09 stsp
662 1510f469 2018-09-09 stsp while ((dent = readdir(packdir)) != NULL) {
663 1510f469 2018-09-09 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
664 1510f469 2018-09-09 stsp continue;
665 1510f469 2018-09-09 stsp
666 1510f469 2018-09-09 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
667 1510f469 2018-09-09 stsp dent->d_name) == -1) {
668 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
669 1510f469 2018-09-09 stsp goto done;
670 1510f469 2018-09-09 stsp }
671 1510f469 2018-09-09 stsp
672 1510f469 2018-09-09 stsp err = got_packidx_open(packidx, path_packidx, 0);
673 1510f469 2018-09-09 stsp free(path_packidx);
674 1510f469 2018-09-09 stsp if (err)
675 1510f469 2018-09-09 stsp goto done;
676 1510f469 2018-09-09 stsp
677 1510f469 2018-09-09 stsp *idx = got_packidx_get_object_idx(*packidx, id);
678 1510f469 2018-09-09 stsp if (*idx != -1) {
679 1510f469 2018-09-09 stsp err = NULL; /* found the object */
680 1510f469 2018-09-09 stsp err = got_repo_cache_packidx(repo, *packidx);
681 1510f469 2018-09-09 stsp goto done;
682 1510f469 2018-09-09 stsp }
683 1510f469 2018-09-09 stsp
684 1510f469 2018-09-09 stsp err = got_packidx_close(*packidx);
685 1510f469 2018-09-09 stsp *packidx = NULL;
686 1510f469 2018-09-09 stsp if (err)
687 1510f469 2018-09-09 stsp goto done;
688 1510f469 2018-09-09 stsp }
689 1510f469 2018-09-09 stsp
690 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
691 1510f469 2018-09-09 stsp done:
692 1510f469 2018-09-09 stsp free(path_packdir);
693 d69bcdf7 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
694 638f9024 2019-05-13 stsp err = got_error_from_errno("closedir");
695 04ca23f4 2018-07-16 stsp return err;
696 04ca23f4 2018-07-16 stsp }
697 1510f469 2018-09-09 stsp
698 1510f469 2018-09-09 stsp static const struct got_error *
699 1510f469 2018-09-09 stsp read_packfile_hdr(int fd, struct got_packidx *packidx)
700 1510f469 2018-09-09 stsp {
701 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
702 1510f469 2018-09-09 stsp uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
703 1510f469 2018-09-09 stsp struct got_packfile_hdr hdr;
704 1510f469 2018-09-09 stsp ssize_t n;
705 1510f469 2018-09-09 stsp
706 1510f469 2018-09-09 stsp n = read(fd, &hdr, sizeof(hdr));
707 1510f469 2018-09-09 stsp if (n < 0)
708 638f9024 2019-05-13 stsp return got_error_from_errno("read");
709 1510f469 2018-09-09 stsp if (n != sizeof(hdr))
710 1510f469 2018-09-09 stsp return got_error(GOT_ERR_BAD_PACKFILE);
711 1510f469 2018-09-09 stsp
712 1510f469 2018-09-09 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
713 1510f469 2018-09-09 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
714 1510f469 2018-09-09 stsp betoh32(hdr.nobjects) != totobj)
715 1510f469 2018-09-09 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
716 1510f469 2018-09-09 stsp
717 1510f469 2018-09-09 stsp return err;
718 1510f469 2018-09-09 stsp }
719 1510f469 2018-09-09 stsp
720 1510f469 2018-09-09 stsp static const struct got_error *
721 1510f469 2018-09-09 stsp open_packfile(int *fd, const char *path_packfile, struct got_packidx *packidx)
722 1510f469 2018-09-09 stsp {
723 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
724 1510f469 2018-09-09 stsp
725 a5b57ccf 2019-04-11 stsp *fd = open(path_packfile, O_RDONLY | O_NOFOLLOW);
726 1510f469 2018-09-09 stsp if (*fd == -1)
727 638f9024 2019-05-13 stsp return got_error_from_errno2("open", path_packfile);
728 1510f469 2018-09-09 stsp
729 1510f469 2018-09-09 stsp if (packidx) {
730 1510f469 2018-09-09 stsp err = read_packfile_hdr(*fd, packidx);
731 1510f469 2018-09-09 stsp if (err) {
732 1510f469 2018-09-09 stsp close(*fd);
733 1510f469 2018-09-09 stsp *fd = -1;
734 1510f469 2018-09-09 stsp }
735 1510f469 2018-09-09 stsp }
736 1510f469 2018-09-09 stsp
737 1510f469 2018-09-09 stsp return err;
738 1510f469 2018-09-09 stsp }
739 1510f469 2018-09-09 stsp
740 1510f469 2018-09-09 stsp const struct got_error *
741 1510f469 2018-09-09 stsp got_repo_cache_pack(struct got_pack **packp, struct got_repository *repo,
742 1510f469 2018-09-09 stsp const char *path_packfile, struct got_packidx *packidx)
743 1510f469 2018-09-09 stsp {
744 1510f469 2018-09-09 stsp const struct got_error *err = NULL;
745 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
746 ff563a3d 2019-05-23 stsp struct stat sb;
747 1510f469 2018-09-09 stsp int i;
748 1510f469 2018-09-09 stsp
749 1510f469 2018-09-09 stsp if (packp)
750 1510f469 2018-09-09 stsp *packp = NULL;
751 1510f469 2018-09-09 stsp
752 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
753 1510f469 2018-09-09 stsp pack = &repo->packs[i];
754 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
755 1510f469 2018-09-09 stsp break;
756 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
757 1510f469 2018-09-09 stsp return NULL;
758 1510f469 2018-09-09 stsp }
759 1510f469 2018-09-09 stsp
760 1510f469 2018-09-09 stsp if (i == nitems(repo->packs) - 1) {
761 1510f469 2018-09-09 stsp err = got_pack_close(&repo->packs[i - 1]);
762 1510f469 2018-09-09 stsp if (err)
763 1510f469 2018-09-09 stsp return err;
764 1510f469 2018-09-09 stsp memmove(&repo->packs[1], &repo->packs[0],
765 1510f469 2018-09-09 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
766 1510f469 2018-09-09 stsp i = 0;
767 1510f469 2018-09-09 stsp }
768 1510f469 2018-09-09 stsp
769 1510f469 2018-09-09 stsp pack = &repo->packs[i];
770 1510f469 2018-09-09 stsp
771 1510f469 2018-09-09 stsp pack->path_packfile = strdup(path_packfile);
772 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL) {
773 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
774 1510f469 2018-09-09 stsp goto done;
775 1510f469 2018-09-09 stsp }
776 1510f469 2018-09-09 stsp
777 1510f469 2018-09-09 stsp err = open_packfile(&pack->fd, path_packfile, packidx);
778 1510f469 2018-09-09 stsp if (err)
779 1510f469 2018-09-09 stsp goto done;
780 1510f469 2018-09-09 stsp
781 ff563a3d 2019-05-23 stsp if (fstat(pack->fd, &sb) != 0) {
782 ff563a3d 2019-05-23 stsp err = got_error_from_errno("fstat");
783 1510f469 2018-09-09 stsp goto done;
784 ff563a3d 2019-05-23 stsp }
785 ff563a3d 2019-05-23 stsp pack->filesize = sb.st_size;
786 90636195 2018-09-11 stsp
787 90636195 2018-09-11 stsp pack->privsep_child = NULL;
788 1510f469 2018-09-09 stsp
789 1510f469 2018-09-09 stsp #ifndef GOT_PACK_NO_MMAP
790 1510f469 2018-09-09 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
791 1510f469 2018-09-09 stsp pack->fd, 0);
792 3a11398b 2019-02-21 stsp if (pack->map == MAP_FAILED) {
793 3a11398b 2019-02-21 stsp if (errno != ENOMEM) {
794 638f9024 2019-05-13 stsp err = got_error_from_errno("mmap");
795 3a11398b 2019-02-21 stsp goto done;
796 3a11398b 2019-02-21 stsp }
797 1510f469 2018-09-09 stsp pack->map = NULL; /* fall back to read(2) */
798 3a11398b 2019-02-21 stsp }
799 1510f469 2018-09-09 stsp #endif
800 1510f469 2018-09-09 stsp done:
801 1510f469 2018-09-09 stsp if (err) {
802 1510f469 2018-09-09 stsp if (pack) {
803 1510f469 2018-09-09 stsp free(pack->path_packfile);
804 1510f469 2018-09-09 stsp memset(pack, 0, sizeof(*pack));
805 1510f469 2018-09-09 stsp }
806 1510f469 2018-09-09 stsp } else if (packp)
807 1510f469 2018-09-09 stsp *packp = pack;
808 1510f469 2018-09-09 stsp return err;
809 1510f469 2018-09-09 stsp }
810 1510f469 2018-09-09 stsp
811 1510f469 2018-09-09 stsp struct got_pack *
812 1510f469 2018-09-09 stsp got_repo_get_cached_pack(struct got_repository *repo, const char *path_packfile)
813 1510f469 2018-09-09 stsp {
814 1510f469 2018-09-09 stsp struct got_pack *pack = NULL;
815 1510f469 2018-09-09 stsp int i;
816 1510f469 2018-09-09 stsp
817 1510f469 2018-09-09 stsp for (i = 0; i < nitems(repo->packs); i++) {
818 1510f469 2018-09-09 stsp pack = &repo->packs[i];
819 1510f469 2018-09-09 stsp if (pack->path_packfile == NULL)
820 1510f469 2018-09-09 stsp break;
821 1510f469 2018-09-09 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
822 1510f469 2018-09-09 stsp return pack;
823 2c7829a4 2019-06-17 stsp }
824 2c7829a4 2019-06-17 stsp
825 2c7829a4 2019-06-17 stsp return NULL;
826 2c7829a4 2019-06-17 stsp }
827 2c7829a4 2019-06-17 stsp
828 2c7829a4 2019-06-17 stsp const struct got_error *
829 2c7829a4 2019-06-17 stsp got_repo_init(const char *repo_path)
830 2c7829a4 2019-06-17 stsp {
831 2c7829a4 2019-06-17 stsp const struct got_error *err = NULL;
832 2c7829a4 2019-06-17 stsp const char *dirnames[] = {
833 2c7829a4 2019-06-17 stsp GOT_OBJECTS_DIR,
834 2c7829a4 2019-06-17 stsp GOT_OBJECTS_PACK_DIR,
835 2c7829a4 2019-06-17 stsp GOT_REFS_DIR,
836 2c7829a4 2019-06-17 stsp };
837 2c7829a4 2019-06-17 stsp const char *description_str = "Unnamed repository; "
838 2c7829a4 2019-06-17 stsp "edit this file 'description' to name the repository.";
839 2c7829a4 2019-06-17 stsp const char *headref_str = "ref: refs/heads/master";
840 2c7829a4 2019-06-17 stsp const char *gitconfig_str = "[core]\n"
841 2c7829a4 2019-06-17 stsp "\trepositoryformatversion = 0\n"
842 2c7829a4 2019-06-17 stsp "\tfilemode = true\n"
843 2c7829a4 2019-06-17 stsp "\tbare = true\n";
844 2c7829a4 2019-06-17 stsp char *path;
845 2c7829a4 2019-06-17 stsp int i;
846 2c7829a4 2019-06-17 stsp
847 2c7829a4 2019-06-17 stsp if (!got_path_dir_is_empty(repo_path))
848 2c7829a4 2019-06-17 stsp return got_error(GOT_ERR_DIR_NOT_EMPTY);
849 2c7829a4 2019-06-17 stsp
850 2c7829a4 2019-06-17 stsp for (i = 0; i < nitems(dirnames); i++) {
851 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, dirnames[i]) == -1) {
852 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
853 2c7829a4 2019-06-17 stsp }
854 2c7829a4 2019-06-17 stsp err = got_path_mkdir(path);
855 2c7829a4 2019-06-17 stsp free(path);
856 2c7829a4 2019-06-17 stsp if (err)
857 2c7829a4 2019-06-17 stsp return err;
858 1510f469 2018-09-09 stsp }
859 1510f469 2018-09-09 stsp
860 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "description") == -1)
861 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
862 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, description_str);
863 2c7829a4 2019-06-17 stsp free(path);
864 2c7829a4 2019-06-17 stsp if (err)
865 2c7829a4 2019-06-17 stsp return err;
866 2c7829a4 2019-06-17 stsp
867 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, GOT_HEAD_FILE) == -1)
868 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
869 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, headref_str);
870 2c7829a4 2019-06-17 stsp free(path);
871 2c7829a4 2019-06-17 stsp if (err)
872 2c7829a4 2019-06-17 stsp return err;
873 2c7829a4 2019-06-17 stsp
874 2c7829a4 2019-06-17 stsp if (asprintf(&path, "%s/%s", repo_path, "config") == -1)
875 2c7829a4 2019-06-17 stsp return got_error_from_errno("asprintf");
876 2c7829a4 2019-06-17 stsp err = got_path_create_file(path, gitconfig_str);
877 2c7829a4 2019-06-17 stsp free(path);
878 2c7829a4 2019-06-17 stsp if (err)
879 2c7829a4 2019-06-17 stsp return err;
880 2c7829a4 2019-06-17 stsp
881 1510f469 2018-09-09 stsp return NULL;
882 e09a504c 2019-06-28 stsp }
883 e09a504c 2019-06-28 stsp
884 e09a504c 2019-06-28 stsp static const struct got_error *
885 4277420a 2019-06-29 stsp match_packed_object(struct got_object_id **unique_id,
886 dd88155e 2019-06-29 stsp struct got_repository *repo, const char *id_str_prefix, int obj_type)
887 e09a504c 2019-06-28 stsp {
888 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
889 e09a504c 2019-06-28 stsp char *path_packdir;
890 e09a504c 2019-06-28 stsp DIR *packdir;
891 e09a504c 2019-06-28 stsp struct dirent *dent;
892 e09a504c 2019-06-28 stsp char *path_packidx;
893 dd88155e 2019-06-29 stsp struct got_object_id_queue matched_ids;
894 e09a504c 2019-06-28 stsp
895 dd88155e 2019-06-29 stsp SIMPLEQ_INIT(&matched_ids);
896 dd88155e 2019-06-29 stsp
897 e09a504c 2019-06-28 stsp path_packdir = got_repo_get_path_objects_pack(repo);
898 e09a504c 2019-06-28 stsp if (path_packdir == NULL)
899 e09a504c 2019-06-28 stsp return got_error_from_errno("got_repo_get_path_objects_pack");
900 e09a504c 2019-06-28 stsp
901 e09a504c 2019-06-28 stsp packdir = opendir(path_packdir);
902 e09a504c 2019-06-28 stsp if (packdir == NULL) {
903 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path_packdir);
904 e09a504c 2019-06-28 stsp goto done;
905 e09a504c 2019-06-28 stsp }
906 e09a504c 2019-06-28 stsp
907 e09a504c 2019-06-28 stsp while ((dent = readdir(packdir)) != NULL) {
908 e09a504c 2019-06-28 stsp struct got_packidx *packidx;
909 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
910 dd88155e 2019-06-29 stsp
911 e09a504c 2019-06-28 stsp
912 e09a504c 2019-06-28 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
913 e09a504c 2019-06-28 stsp continue;
914 e09a504c 2019-06-28 stsp
915 e09a504c 2019-06-28 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
916 e09a504c 2019-06-28 stsp dent->d_name) == -1) {
917 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
918 4277420a 2019-06-29 stsp break;
919 e09a504c 2019-06-28 stsp }
920 e09a504c 2019-06-28 stsp
921 e09a504c 2019-06-28 stsp err = got_packidx_open(&packidx, path_packidx, 0);
922 e09a504c 2019-06-28 stsp free(path_packidx);
923 e09a504c 2019-06-28 stsp if (err)
924 4277420a 2019-06-29 stsp break;
925 e09a504c 2019-06-28 stsp
926 dd88155e 2019-06-29 stsp err = got_packidx_match_id_str_prefix(&matched_ids,
927 4277420a 2019-06-29 stsp packidx, id_str_prefix);
928 4277420a 2019-06-29 stsp if (err) {
929 4277420a 2019-06-29 stsp got_packidx_close(packidx);
930 4277420a 2019-06-29 stsp break;
931 4277420a 2019-06-29 stsp }
932 e09a504c 2019-06-28 stsp err = got_packidx_close(packidx);
933 dd88155e 2019-06-29 stsp if (err)
934 e09a504c 2019-06-28 stsp break;
935 e09a504c 2019-06-28 stsp
936 dd88155e 2019-06-29 stsp SIMPLEQ_FOREACH(qid, &matched_ids, entry) {
937 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
938 dd88155e 2019-06-29 stsp int matched_type;
939 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
940 dd88155e 2019-06-29 stsp qid->id);
941 dd88155e 2019-06-29 stsp if (err)
942 dd88155e 2019-06-29 stsp goto done;
943 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
944 dd88155e 2019-06-29 stsp continue;
945 dd88155e 2019-06-29 stsp }
946 4277420a 2019-06-29 stsp if (*unique_id == NULL) {
947 dd88155e 2019-06-29 stsp *unique_id = got_object_id_dup(qid->id);
948 dd88155e 2019-06-29 stsp if (*unique_id == NULL) {
949 dd88155e 2019-06-29 stsp err = got_error_from_errno("malloc");
950 dd88155e 2019-06-29 stsp goto done;
951 dd88155e 2019-06-29 stsp }
952 4277420a 2019-06-29 stsp } else {
953 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
954 561c3678 2019-07-02 stsp goto done;
955 e09a504c 2019-06-28 stsp }
956 e09a504c 2019-06-28 stsp }
957 e09a504c 2019-06-28 stsp }
958 e09a504c 2019-06-28 stsp done:
959 dd88155e 2019-06-29 stsp got_object_id_queue_free(&matched_ids);
960 e09a504c 2019-06-28 stsp free(path_packdir);
961 e09a504c 2019-06-28 stsp if (packdir && closedir(packdir) != 0 && err == NULL)
962 e09a504c 2019-06-28 stsp err = got_error_from_errno("closedir");
963 e09a504c 2019-06-28 stsp if (err) {
964 e09a504c 2019-06-28 stsp free(*unique_id);
965 e09a504c 2019-06-28 stsp *unique_id = NULL;
966 e09a504c 2019-06-28 stsp }
967 e09a504c 2019-06-28 stsp return err;
968 e09a504c 2019-06-28 stsp }
969 e09a504c 2019-06-28 stsp
970 e09a504c 2019-06-28 stsp static const struct got_error *
971 4277420a 2019-06-29 stsp match_loose_object(struct got_object_id **unique_id, const char *path_objects,
972 dd88155e 2019-06-29 stsp const char *object_dir, const char *id_str_prefix, int obj_type,
973 e09a504c 2019-06-28 stsp struct got_repository *repo)
974 e09a504c 2019-06-28 stsp {
975 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
976 e09a504c 2019-06-28 stsp char *path;
977 e09a504c 2019-06-28 stsp DIR *dir = NULL;
978 e09a504c 2019-06-28 stsp struct dirent *dent;
979 e09a504c 2019-06-28 stsp struct got_object_id id;
980 e09a504c 2019-06-28 stsp
981 e09a504c 2019-06-28 stsp if (asprintf(&path, "%s/%s", path_objects, object_dir) == -1) {
982 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
983 e09a504c 2019-06-28 stsp goto done;
984 e09a504c 2019-06-28 stsp }
985 e09a504c 2019-06-28 stsp
986 e09a504c 2019-06-28 stsp dir = opendir(path);
987 e09a504c 2019-06-28 stsp if (dir == NULL) {
988 4277420a 2019-06-29 stsp if (errno == ENOENT) {
989 4277420a 2019-06-29 stsp err = NULL;
990 4277420a 2019-06-29 stsp goto done;
991 4277420a 2019-06-29 stsp }
992 e09a504c 2019-06-28 stsp err = got_error_from_errno2("opendir", path);
993 e09a504c 2019-06-28 stsp goto done;
994 e09a504c 2019-06-28 stsp }
995 e09a504c 2019-06-28 stsp while ((dent = readdir(dir)) != NULL) {
996 e09a504c 2019-06-28 stsp char *id_str;
997 5903ff6e 2019-06-29 stsp int cmp;
998 5903ff6e 2019-06-29 stsp
999 e09a504c 2019-06-28 stsp if (strcmp(dent->d_name, ".") == 0 ||
1000 e09a504c 2019-06-28 stsp strcmp(dent->d_name, "..") == 0)
1001 e09a504c 2019-06-28 stsp continue;
1002 e09a504c 2019-06-28 stsp
1003 e09a504c 2019-06-28 stsp if (asprintf(&id_str, "%s%s", object_dir, dent->d_name) == -1) {
1004 e09a504c 2019-06-28 stsp err = got_error_from_errno("asprintf");
1005 e09a504c 2019-06-28 stsp goto done;
1006 e09a504c 2019-06-28 stsp }
1007 e09a504c 2019-06-28 stsp
1008 e09a504c 2019-06-28 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
1009 e09a504c 2019-06-28 stsp continue;
1010 e09a504c 2019-06-28 stsp
1011 52d1d0d9 2019-07-07 stsp /*
1012 52d1d0d9 2019-07-07 stsp * Directory entries do not necessarily appear in
1013 52d1d0d9 2019-07-07 stsp * sorted order, so we must iterate over all of them.
1014 52d1d0d9 2019-07-07 stsp */
1015 5903ff6e 2019-06-29 stsp cmp = strncmp(id_str, id_str_prefix, strlen(id_str_prefix));
1016 52d1d0d9 2019-07-07 stsp if (cmp != 0) {
1017 e09a504c 2019-06-28 stsp free(id_str);
1018 e09a504c 2019-06-28 stsp continue;
1019 e09a504c 2019-06-28 stsp }
1020 e09a504c 2019-06-28 stsp
1021 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1022 dd88155e 2019-06-29 stsp if (obj_type != GOT_OBJ_TYPE_ANY) {
1023 dd88155e 2019-06-29 stsp int matched_type;
1024 dd88155e 2019-06-29 stsp err = got_object_get_type(&matched_type, repo,
1025 dd88155e 2019-06-29 stsp &id);
1026 dd88155e 2019-06-29 stsp if (err)
1027 dd88155e 2019-06-29 stsp goto done;
1028 dd88155e 2019-06-29 stsp if (matched_type != obj_type)
1029 dd88155e 2019-06-29 stsp continue;
1030 dd88155e 2019-06-29 stsp }
1031 e09a504c 2019-06-28 stsp *unique_id = got_object_id_dup(&id);
1032 e09a504c 2019-06-28 stsp if (*unique_id == NULL) {
1033 e09a504c 2019-06-28 stsp err = got_error_from_errno("got_object_id_dup");
1034 e09a504c 2019-06-28 stsp free(id_str);
1035 e09a504c 2019-06-28 stsp goto done;
1036 e09a504c 2019-06-28 stsp }
1037 e09a504c 2019-06-28 stsp } else {
1038 e09a504c 2019-06-28 stsp err = got_error(GOT_ERR_AMBIGUOUS_ID);
1039 e09a504c 2019-06-28 stsp free(id_str);
1040 e09a504c 2019-06-28 stsp goto done;
1041 e09a504c 2019-06-28 stsp }
1042 e09a504c 2019-06-28 stsp }
1043 e09a504c 2019-06-28 stsp done:
1044 b2df341b 2019-06-29 stsp if (dir && closedir(dir) != 0 && err == NULL)
1045 b2df341b 2019-06-29 stsp err = got_error_from_errno("closedir");
1046 e09a504c 2019-06-28 stsp if (err) {
1047 e09a504c 2019-06-28 stsp free(*unique_id);
1048 e09a504c 2019-06-28 stsp *unique_id = NULL;
1049 e09a504c 2019-06-28 stsp }
1050 e09a504c 2019-06-28 stsp free(path);
1051 e09a504c 2019-06-28 stsp return err;
1052 1510f469 2018-09-09 stsp }
1053 e09a504c 2019-06-28 stsp
1054 e09a504c 2019-06-28 stsp const struct got_error *
1055 4277420a 2019-06-29 stsp got_repo_match_object_id_prefix(struct got_object_id **id,
1056 dd88155e 2019-06-29 stsp const char *id_str_prefix, int obj_type, struct got_repository *repo)
1057 e09a504c 2019-06-28 stsp {
1058 e09a504c 2019-06-28 stsp const struct got_error *err = NULL;
1059 e09a504c 2019-06-28 stsp char *path_objects = got_repo_get_path_objects(repo);
1060 e09a504c 2019-06-28 stsp char *object_dir = NULL;
1061 e09a504c 2019-06-28 stsp size_t len;
1062 4277420a 2019-06-29 stsp int i;
1063 e09a504c 2019-06-28 stsp
1064 4277420a 2019-06-29 stsp *id = NULL;
1065 4277420a 2019-06-29 stsp
1066 4277420a 2019-06-29 stsp for (i = 0; i < strlen(id_str_prefix); i++) {
1067 4277420a 2019-06-29 stsp if (isxdigit((unsigned char)id_str_prefix[i]))
1068 4277420a 2019-06-29 stsp continue;
1069 4277420a 2019-06-29 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
1070 4277420a 2019-06-29 stsp }
1071 4277420a 2019-06-29 stsp
1072 e09a504c 2019-06-28 stsp len = strlen(id_str_prefix);
1073 e09a504c 2019-06-28 stsp if (len >= 2) {
1074 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, id_str_prefix, obj_type);
1075 4277420a 2019-06-29 stsp if (err)
1076 83c8b3b8 2019-06-29 stsp goto done;
1077 e09a504c 2019-06-28 stsp object_dir = strndup(id_str_prefix, 2);
1078 83c8b3b8 2019-06-29 stsp if (object_dir == NULL) {
1079 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("strdup");
1080 83c8b3b8 2019-06-29 stsp goto done;
1081 83c8b3b8 2019-06-29 stsp }
1082 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1083 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1084 e09a504c 2019-06-28 stsp } else if (len == 1) {
1085 e09a504c 2019-06-28 stsp int i;
1086 e09a504c 2019-06-28 stsp for (i = 0; i < 0xf; i++) {
1087 e09a504c 2019-06-28 stsp if (asprintf(&object_dir, "%s%.1x", id_str_prefix, i)
1088 83c8b3b8 2019-06-29 stsp == -1) {
1089 83c8b3b8 2019-06-29 stsp err = got_error_from_errno("asprintf");
1090 83c8b3b8 2019-06-29 stsp goto done;
1091 83c8b3b8 2019-06-29 stsp }
1092 dd88155e 2019-06-29 stsp err = match_packed_object(id, repo, object_dir,
1093 dd88155e 2019-06-29 stsp obj_type);
1094 4277420a 2019-06-29 stsp if (err)
1095 83c8b3b8 2019-06-29 stsp goto done;
1096 4277420a 2019-06-29 stsp err = match_loose_object(id, path_objects, object_dir,
1097 dd88155e 2019-06-29 stsp id_str_prefix, obj_type, repo);
1098 e09a504c 2019-06-28 stsp if (err)
1099 83c8b3b8 2019-06-29 stsp goto done;
1100 e09a504c 2019-06-28 stsp }
1101 83c8b3b8 2019-06-29 stsp } else {
1102 83c8b3b8 2019-06-29 stsp err = got_error(GOT_ERR_BAD_OBJ_ID_STR);
1103 83c8b3b8 2019-06-29 stsp goto done;
1104 83c8b3b8 2019-06-29 stsp }
1105 83c8b3b8 2019-06-29 stsp done:
1106 e09a504c 2019-06-28 stsp free(object_dir);
1107 4277420a 2019-06-29 stsp if (err) {
1108 4277420a 2019-06-29 stsp free(*id);
1109 4277420a 2019-06-29 stsp *id = NULL;
1110 4277420a 2019-06-29 stsp } else if (*id == NULL)
1111 4277420a 2019-06-29 stsp err = got_error(GOT_ERR_NO_OBJ);
1112 4277420a 2019-06-29 stsp
1113 e09a504c 2019-06-28 stsp return err;
1114 e09a504c 2019-06-28 stsp }
1115 3ce1b845 2019-07-15 stsp
1116 3ce1b845 2019-07-15 stsp static const struct got_error *
1117 3ce1b845 2019-07-15 stsp alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
1118 3ce1b845 2019-07-15 stsp const char *name, mode_t mode, struct got_object_id *blob_id)
1119 3ce1b845 2019-07-15 stsp {
1120 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1121 3ce1b845 2019-07-15 stsp
1122 3ce1b845 2019-07-15 stsp *new_te = NULL;
1123 3ce1b845 2019-07-15 stsp
1124 3ce1b845 2019-07-15 stsp *new_te = calloc(1, sizeof(**new_te));
1125 3ce1b845 2019-07-15 stsp if (*new_te == NULL)
1126 3ce1b845 2019-07-15 stsp return got_error_from_errno("calloc");
1127 3ce1b845 2019-07-15 stsp
1128 3ce1b845 2019-07-15 stsp (*new_te)->name = strdup(name);
1129 3ce1b845 2019-07-15 stsp if ((*new_te)->name == NULL) {
1130 3ce1b845 2019-07-15 stsp err = got_error_from_errno("strdup");
1131 3ce1b845 2019-07-15 stsp goto done;
1132 3ce1b845 2019-07-15 stsp }
1133 3ce1b845 2019-07-15 stsp
1134 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
1135 3ce1b845 2019-07-15 stsp (*new_te)->id = blob_id;
1136 3ce1b845 2019-07-15 stsp done:
1137 3ce1b845 2019-07-15 stsp if (err && *new_te) {
1138 3ce1b845 2019-07-15 stsp got_object_tree_entry_close(*new_te);
1139 3ce1b845 2019-07-15 stsp *new_te = NULL;
1140 3ce1b845 2019-07-15 stsp }
1141 3ce1b845 2019-07-15 stsp return err;
1142 3ce1b845 2019-07-15 stsp }
1143 3ce1b845 2019-07-15 stsp
1144 3ce1b845 2019-07-15 stsp static const struct got_error *
1145 3ce1b845 2019-07-15 stsp import_file(struct got_tree_entry **new_te, struct dirent *de,
1146 3ce1b845 2019-07-15 stsp const char *path, struct got_repository *repo)
1147 3ce1b845 2019-07-15 stsp {
1148 3ce1b845 2019-07-15 stsp const struct got_error *err;
1149 3ce1b845 2019-07-15 stsp struct got_object_id *blob_id = NULL;
1150 3ce1b845 2019-07-15 stsp char *filepath;
1151 3ce1b845 2019-07-15 stsp struct stat sb;
1152 3ce1b845 2019-07-15 stsp
1153 3ce1b845 2019-07-15 stsp if (asprintf(&filepath, "%s%s%s", path,
1154 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1155 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1156 3ce1b845 2019-07-15 stsp
1157 3ce1b845 2019-07-15 stsp if (lstat(filepath, &sb) != 0) {
1158 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("lstat", path);
1159 3ce1b845 2019-07-15 stsp goto done;
1160 3ce1b845 2019-07-15 stsp }
1161 3ce1b845 2019-07-15 stsp
1162 3ce1b845 2019-07-15 stsp err = got_object_blob_create(&blob_id, filepath, repo);
1163 3ce1b845 2019-07-15 stsp if (err)
1164 3ce1b845 2019-07-15 stsp goto done;
1165 3ce1b845 2019-07-15 stsp
1166 3ce1b845 2019-07-15 stsp err = alloc_added_blob_tree_entry(new_te, de->d_name, sb.st_mode,
1167 3ce1b845 2019-07-15 stsp blob_id);
1168 3ce1b845 2019-07-15 stsp done:
1169 3ce1b845 2019-07-15 stsp free(filepath);
1170 3ce1b845 2019-07-15 stsp if (err)
1171 3ce1b845 2019-07-15 stsp free(blob_id);
1172 3ce1b845 2019-07-15 stsp return err;
1173 3ce1b845 2019-07-15 stsp }
1174 3ce1b845 2019-07-15 stsp
1175 3ce1b845 2019-07-15 stsp static const struct got_error *
1176 3ce1b845 2019-07-15 stsp insert_tree_entry(struct got_tree_entry *new_te,
1177 3ce1b845 2019-07-15 stsp struct got_pathlist_head *paths)
1178 3ce1b845 2019-07-15 stsp {
1179 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1180 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *new_pe;
1181 3ce1b845 2019-07-15 stsp
1182 3ce1b845 2019-07-15 stsp err = got_pathlist_insert(&new_pe, paths, new_te->name, new_te);
1183 3ce1b845 2019-07-15 stsp if (err)
1184 3ce1b845 2019-07-15 stsp return err;
1185 3ce1b845 2019-07-15 stsp if (new_pe == NULL)
1186 3ce1b845 2019-07-15 stsp return got_error(GOT_ERR_TREE_DUP_ENTRY);
1187 3ce1b845 2019-07-15 stsp return NULL;
1188 3ce1b845 2019-07-15 stsp }
1189 3ce1b845 2019-07-15 stsp
1190 3ce1b845 2019-07-15 stsp static const struct got_error *write_tree(struct got_object_id **,
1191 3ce1b845 2019-07-15 stsp const char *, struct got_pathlist_head *, struct got_repository *,
1192 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg);
1193 3ce1b845 2019-07-15 stsp
1194 3ce1b845 2019-07-15 stsp static const struct got_error *
1195 3ce1b845 2019-07-15 stsp import_subdir(struct got_tree_entry **new_te, struct dirent *de,
1196 3ce1b845 2019-07-15 stsp const char *path, struct got_pathlist_head *ignores,
1197 3ce1b845 2019-07-15 stsp struct got_repository *repo,
1198 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1199 3ce1b845 2019-07-15 stsp {
1200 3ce1b845 2019-07-15 stsp const struct got_error *err;
1201 3ce1b845 2019-07-15 stsp char *subdirpath;
1202 3ce1b845 2019-07-15 stsp
1203 3ce1b845 2019-07-15 stsp if (asprintf(&subdirpath, "%s%s%s", path,
1204 3ce1b845 2019-07-15 stsp path[0] == '\0' ? "" : "/", de->d_name) == -1)
1205 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
1206 3ce1b845 2019-07-15 stsp
1207 3ce1b845 2019-07-15 stsp (*new_te) = calloc(1, sizeof(**new_te));
1208 3ce1b845 2019-07-15 stsp (*new_te)->mode = S_IFDIR;
1209 3ce1b845 2019-07-15 stsp (*new_te)->name = strdup(de->d_name);
1210 3ce1b845 2019-07-15 stsp if ((*new_te)->name == NULL) {
1211 3ce1b845 2019-07-15 stsp err = got_error_from_errno("strdup");
1212 3ce1b845 2019-07-15 stsp goto done;
1213 3ce1b845 2019-07-15 stsp }
1214 3ce1b845 2019-07-15 stsp
1215 3ce1b845 2019-07-15 stsp err = write_tree(&(*new_te)->id, subdirpath, ignores, repo,
1216 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1217 3ce1b845 2019-07-15 stsp done:
1218 3ce1b845 2019-07-15 stsp free(subdirpath);
1219 3ce1b845 2019-07-15 stsp if (err) {
1220 3ce1b845 2019-07-15 stsp got_object_tree_entry_close(*new_te);
1221 3ce1b845 2019-07-15 stsp *new_te = NULL;
1222 3ce1b845 2019-07-15 stsp }
1223 3ce1b845 2019-07-15 stsp return err;
1224 3ce1b845 2019-07-15 stsp }
1225 3ce1b845 2019-07-15 stsp
1226 3ce1b845 2019-07-15 stsp static const struct got_error *
1227 3ce1b845 2019-07-15 stsp write_tree(struct got_object_id **new_tree_id, const char *path_dir,
1228 3ce1b845 2019-07-15 stsp struct got_pathlist_head *ignores, struct got_repository *repo,
1229 3ce1b845 2019-07-15 stsp got_repo_import_cb progress_cb, void *progress_arg)
1230 3ce1b845 2019-07-15 stsp {
1231 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
1232 3ce1b845 2019-07-15 stsp DIR *dir;
1233 3ce1b845 2019-07-15 stsp struct dirent *de;
1234 3ce1b845 2019-07-15 stsp struct got_tree_entries new_tree_entries;
1235 3ce1b845 2019-07-15 stsp struct got_tree_entry *new_te = NULL;
1236 3ce1b845 2019-07-15 stsp struct got_pathlist_head paths;
1237 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
1238 3ce1b845 2019-07-15 stsp char *name;
1239 3ce1b845 2019-07-15 stsp
1240 3ce1b845 2019-07-15 stsp *new_tree_id = NULL;
1241 3ce1b845 2019-07-15 stsp
1242 3ce1b845 2019-07-15 stsp TAILQ_INIT(&paths);
1243 3ce1b845 2019-07-15 stsp new_tree_entries.nentries = 0;
1244 3ce1b845 2019-07-15 stsp SIMPLEQ_INIT(&new_tree_entries.head);
1245 3ce1b845 2019-07-15 stsp
1246 3ce1b845 2019-07-15 stsp dir = opendir(path_dir);
1247 3ce1b845 2019-07-15 stsp if (dir == NULL) {
1248 3ce1b845 2019-07-15 stsp err = got_error_from_errno2("opendir", path_dir);
1249 3ce1b845 2019-07-15 stsp goto done;
1250 3ce1b845 2019-07-15 stsp }
1251 3ce1b845 2019-07-15 stsp
1252 3ce1b845 2019-07-15 stsp while ((de = readdir(dir)) != NULL) {
1253 3ce1b845 2019-07-15 stsp int ignore = 0;
1254 3ce1b845 2019-07-15 stsp
1255 3ce1b845 2019-07-15 stsp if (strcmp(de->d_name, ".") == 0 ||
1256 3ce1b845 2019-07-15 stsp strcmp(de->d_name, "..") == 0)
1257 3ce1b845 2019-07-15 stsp continue;
1258 3ce1b845 2019-07-15 stsp
1259 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, ignores, entry) {
1260 3ce1b845 2019-07-15 stsp if (fnmatch(pe->path, de->d_name, 0) == 0) {
1261 3ce1b845 2019-07-15 stsp ignore = 1;
1262 3ce1b845 2019-07-15 stsp break;
1263 3ce1b845 2019-07-15 stsp }
1264 3ce1b845 2019-07-15 stsp }
1265 3ce1b845 2019-07-15 stsp if (ignore)
1266 3ce1b845 2019-07-15 stsp continue;
1267 3ce1b845 2019-07-15 stsp if (de->d_type == DT_DIR) {
1268 3ce1b845 2019-07-15 stsp err = import_subdir(&new_te, de, path_dir,
1269 3ce1b845 2019-07-15 stsp ignores, repo, progress_cb, progress_arg);
1270 3ce1b845 2019-07-15 stsp if (err)
1271 3ce1b845 2019-07-15 stsp goto done;
1272 3ce1b845 2019-07-15 stsp } else if (de->d_type == DT_REG) {
1273 3ce1b845 2019-07-15 stsp err = import_file(&new_te, de, path_dir, repo);
1274 3ce1b845 2019-07-15 stsp if (err)
1275 3ce1b845 2019-07-15 stsp goto done;
1276 3ce1b845 2019-07-15 stsp } else
1277 3ce1b845 2019-07-15 stsp continue;
1278 3ce1b845 2019-07-15 stsp
1279 3ce1b845 2019-07-15 stsp err = insert_tree_entry(new_te, &paths);
1280 3ce1b845 2019-07-15 stsp if (err)
1281 3ce1b845 2019-07-15 stsp goto done;
1282 3ce1b845 2019-07-15 stsp }
1283 3ce1b845 2019-07-15 stsp
1284 3ce1b845 2019-07-15 stsp TAILQ_FOREACH(pe, &paths, entry) {
1285 3ce1b845 2019-07-15 stsp struct got_tree_entry *te = pe->data;
1286 3ce1b845 2019-07-15 stsp char *path;
1287 3ce1b845 2019-07-15 stsp new_tree_entries.nentries++;
1288 3ce1b845 2019-07-15 stsp SIMPLEQ_INSERT_TAIL(&new_tree_entries.head, te, entry);
1289 3ce1b845 2019-07-15 stsp if (!S_ISREG(te->mode))
1290 3ce1b845 2019-07-15 stsp continue;
1291 3ce1b845 2019-07-15 stsp if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
1292 3ce1b845 2019-07-15 stsp err = got_error_from_errno("asprintf");
1293 3ce1b845 2019-07-15 stsp goto done;
1294 3ce1b845 2019-07-15 stsp }
1295 3ce1b845 2019-07-15 stsp err = (*progress_cb)(progress_arg, path);
1296 3ce1b845 2019-07-15 stsp free(path);
1297 3ce1b845 2019-07-15 stsp if (err)
1298 3ce1b845 2019-07-15 stsp goto done;
1299 3ce1b845 2019-07-15 stsp }
1300 3ce1b845 2019-07-15 stsp
1301 3ce1b845 2019-07-15 stsp name = basename(path_dir);
1302 3ce1b845 2019-07-15 stsp if (name == NULL) {
1303 3ce1b845 2019-07-15 stsp err = got_error_from_errno("basename");
1304 3ce1b845 2019-07-15 stsp goto done;
1305 3ce1b845 2019-07-15 stsp }
1306 3ce1b845 2019-07-15 stsp
1307 3ce1b845 2019-07-15 stsp err = got_object_tree_create(new_tree_id, &new_tree_entries, repo);
1308 3ce1b845 2019-07-15 stsp done:
1309 3ce1b845 2019-07-15 stsp if (dir)
1310 3ce1b845 2019-07-15 stsp closedir(dir);
1311 3ce1b845 2019-07-15 stsp got_object_tree_entries_close(&new_tree_entries);
1312 3ce1b845 2019-07-15 stsp got_pathlist_free(&paths);
1313 3ce1b845 2019-07-15 stsp return err;
1314 3ce1b845 2019-07-15 stsp }
1315 3ce1b845 2019-07-15 stsp
1316 3ce1b845 2019-07-15 stsp const struct got_error *
1317 3ce1b845 2019-07-15 stsp got_repo_import(struct got_object_id **new_commit_id, const char *path_dir,
1318 3ce1b845 2019-07-15 stsp const char *logmsg, const char *author, struct got_pathlist_head *ignores,
1319 3ce1b845 2019-07-15 stsp struct got_repository *repo, got_repo_import_cb progress_cb,
1320 3ce1b845 2019-07-15 stsp void *progress_arg)
1321 3ce1b845 2019-07-15 stsp {
1322 3ce1b845 2019-07-15 stsp const struct got_error *err;
1323 3ce1b845 2019-07-15 stsp struct got_object_id *new_tree_id;
1324 3ce1b845 2019-07-15 stsp
1325 3ce1b845 2019-07-15 stsp err = write_tree(&new_tree_id, path_dir, ignores, repo,
1326 3ce1b845 2019-07-15 stsp progress_cb, progress_arg);
1327 3ce1b845 2019-07-15 stsp if (err)
1328 3ce1b845 2019-07-15 stsp return err;
1329 3ce1b845 2019-07-15 stsp
1330 3ce1b845 2019-07-15 stsp err = got_object_commit_create(new_commit_id, new_tree_id, NULL, 0,
1331 3ce1b845 2019-07-15 stsp author, time(NULL), author, time(NULL), logmsg, repo);
1332 3ce1b845 2019-07-15 stsp free(new_tree_id);
1333 3ce1b845 2019-07-15 stsp return err;
1334 3ce1b845 2019-07-15 stsp }