Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
7 d71d75ad 2017-11-05 stsp *
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
24 d1cda826 2017-11-06 stsp
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.h>
36 788c352e 2018-06-16 stsp #include <time.h>
37 d71d75ad 2017-11-05 stsp
38 ab9a70b2 2017-11-06 stsp #include "got_error.h"
39 d71d75ad 2017-11-05 stsp #include "got_object.h"
40 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 d71d75ad 2017-11-05 stsp
43 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
54 1411938b 2018-02-12 stsp
55 ab9a70b2 2017-11-06 stsp #ifndef MIN
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 ab9a70b2 2017-11-06 stsp #endif
58 ef0981d5 2018-02-12 stsp
59 a1fd68d8 2018-01-12 stsp int
60 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
61 a1fd68d8 2018-01-12 stsp {
62 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 8bf5b3c9 2018-03-17 stsp }
64 8bf5b3c9 2018-03-17 stsp
65 8bf5b3c9 2018-03-17 stsp struct got_object_id *
66 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
67 8bf5b3c9 2018-03-17 stsp {
68 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
69 8bf5b3c9 2018-03-17 stsp
70 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
71 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
72 8bf5b3c9 2018-03-17 stsp return NULL;
73 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
74 8bf5b3c9 2018-03-17 stsp return id2;
75 3235492e 2018-04-01 stsp }
76 3235492e 2018-04-01 stsp
77 3235492e 2018-04-01 stsp struct got_object_id *
78 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
79 3235492e 2018-04-01 stsp {
80 6402fb3c 2018-09-15 stsp return &obj->id;
81 bacc9935 2018-05-20 stsp }
82 bacc9935 2018-05-20 stsp
83 bacc9935 2018-05-20 stsp const struct got_error *
84 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
85 bacc9935 2018-05-20 stsp {
86 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
87 a1fd68d8 2018-01-12 stsp }
88 d71d75ad 2017-11-05 stsp
89 b107e67f 2018-01-19 stsp int
90 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
91 a1fd68d8 2018-01-12 stsp {
92 b107e67f 2018-01-19 stsp switch (obj->type) {
93 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
94 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
95 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
96 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
97 b107e67f 2018-01-19 stsp return obj->type;
98 96f5e8b3 2018-01-23 stsp default:
99 96f5e8b3 2018-01-23 stsp abort();
100 96f5e8b3 2018-01-23 stsp break;
101 d71d75ad 2017-11-05 stsp }
102 d71d75ad 2017-11-05 stsp
103 96f5e8b3 2018-01-23 stsp /* not reached */
104 96f5e8b3 2018-01-23 stsp return 0;
105 d71d75ad 2017-11-05 stsp }
106 ab9a70b2 2017-11-06 stsp
107 ab9a70b2 2017-11-06 stsp static const struct got_error *
108 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
109 ab9a70b2 2017-11-06 stsp {
110 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
111 7a132809 2018-07-23 stsp char *hex = NULL;
112 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
113 e6b1056e 2018-04-22 stsp
114 e6b1056e 2018-04-22 stsp *path = NULL;
115 ab9a70b2 2017-11-06 stsp
116 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
118 ab9a70b2 2017-11-06 stsp
119 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
120 ef0981d5 2018-02-12 stsp if (err)
121 7a132809 2018-07-23 stsp goto done;
122 ab9a70b2 2017-11-06 stsp
123 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
124 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
125 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
126 ab9a70b2 2017-11-06 stsp
127 7a132809 2018-07-23 stsp done:
128 ef0981d5 2018-02-12 stsp free(hex);
129 d1cda826 2017-11-06 stsp free(path_objects);
130 d1cda826 2017-11-06 stsp return err;
131 d1cda826 2017-11-06 stsp }
132 d1cda826 2017-11-06 stsp
133 4ee4114f 2018-01-23 stsp static const struct got_error *
134 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
135 d1cda826 2017-11-06 stsp {
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
138 6c00b545 2018-01-17 stsp
139 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
140 d1cda826 2017-11-06 stsp if (err)
141 d1cda826 2017-11-06 stsp return err;
142 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
143 d5003b79 2018-04-22 stsp if (*fd == -1) {
144 6c00b545 2018-01-17 stsp err = got_error_from_errno();
145 6c00b545 2018-01-17 stsp goto done;
146 a1fd68d8 2018-01-12 stsp }
147 4558fcd4 2018-01-14 stsp done:
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
150 2090a03d 2018-09-09 stsp }
151 2090a03d 2018-09-09 stsp
152 2090a03d 2018-09-09 stsp static const struct got_error *
153 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
154 2090a03d 2018-09-09 stsp {
155 2090a03d 2018-09-09 stsp size_t size;
156 2090a03d 2018-09-09 stsp
157 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
158 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
159 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
160 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
161 2090a03d 2018-09-09 stsp
162 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
163 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
164 2090a03d 2018-09-09 stsp return got_error_from_errno();
165 2090a03d 2018-09-09 stsp
166 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
167 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
168 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
169 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
170 2090a03d 2018-09-09 stsp
171 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
172 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
173 2090a03d 2018-09-09 stsp
174 2090a03d 2018-09-09 stsp return NULL;
175 2090a03d 2018-09-09 stsp }
176 2090a03d 2018-09-09 stsp
177 2090a03d 2018-09-09 stsp static const struct got_error *
178 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
179 2090a03d 2018-09-09 stsp struct got_repository *repo)
180 2090a03d 2018-09-09 stsp {
181 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
182 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
183 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
184 2090a03d 2018-09-09 stsp int idx;
185 2090a03d 2018-09-09 stsp char *path_packfile;
186 2090a03d 2018-09-09 stsp
187 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 2090a03d 2018-09-09 stsp if (err)
189 2090a03d 2018-09-09 stsp return err;
190 2090a03d 2018-09-09 stsp
191 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
192 2090a03d 2018-09-09 stsp if (err)
193 2090a03d 2018-09-09 stsp return err;
194 2090a03d 2018-09-09 stsp
195 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
196 2090a03d 2018-09-09 stsp if (pack == NULL) {
197 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
198 2090a03d 2018-09-09 stsp if (err)
199 2090a03d 2018-09-09 stsp goto done;
200 2090a03d 2018-09-09 stsp }
201 2090a03d 2018-09-09 stsp
202 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
203 2090a03d 2018-09-09 stsp if (err)
204 2090a03d 2018-09-09 stsp goto done;
205 2090a03d 2018-09-09 stsp
206 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
207 2090a03d 2018-09-09 stsp done:
208 2090a03d 2018-09-09 stsp free(path_packfile);
209 4558fcd4 2018-01-14 stsp return err;
210 4558fcd4 2018-01-14 stsp }
211 a1fd68d8 2018-01-12 stsp
212 4558fcd4 2018-01-14 stsp const struct got_error *
213 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
214 4558fcd4 2018-01-14 stsp struct got_object_id *id)
215 4558fcd4 2018-01-14 stsp {
216 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
217 6c00b545 2018-01-17 stsp char *path;
218 2178c42e 2018-04-22 stsp int fd;
219 7bb0daa1 2018-06-21 stsp
220 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
221 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
222 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
223 7bb0daa1 2018-06-21 stsp return NULL;
224 7bb0daa1 2018-06-21 stsp }
225 4558fcd4 2018-01-14 stsp
226 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
227 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
228 59790a32 2018-09-15 stsp return err;
229 59790a32 2018-09-15 stsp if (*obj) {
230 59790a32 2018-09-15 stsp (*obj)->refcnt++;
231 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
232 59790a32 2018-09-15 stsp }
233 59790a32 2018-09-15 stsp
234 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
235 4558fcd4 2018-01-14 stsp if (err)
236 4558fcd4 2018-01-14 stsp return err;
237 4558fcd4 2018-01-14 stsp
238 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
239 2178c42e 2018-04-22 stsp if (fd == -1) {
240 59790a32 2018-09-15 stsp if (errno == ENOENT)
241 59790a32 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
242 59790a32 2018-09-15 stsp else
243 6c00b545 2018-01-17 stsp err = got_error_from_errno();
244 59790a32 2018-09-15 stsp goto done;
245 6c00b545 2018-01-17 stsp } else {
246 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
247 6c00b545 2018-01-17 stsp if (err)
248 6c00b545 2018-01-17 stsp goto done;
249 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
250 6c00b545 2018-01-17 stsp }
251 7bb0daa1 2018-06-21 stsp
252 59790a32 2018-09-15 stsp (*obj)->refcnt++;
253 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
254 6c00b545 2018-01-17 stsp done:
255 6c00b545 2018-01-17 stsp free(path);
256 2178c42e 2018-04-22 stsp if (fd != -1)
257 2178c42e 2018-04-22 stsp close(fd);
258 ab9a70b2 2017-11-06 stsp return err;
259 6c00b545 2018-01-17 stsp
260 ab9a70b2 2017-11-06 stsp }
261 ab9a70b2 2017-11-06 stsp
262 6dfa2fd3 2018-02-12 stsp const struct got_error *
263 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
264 6dfa2fd3 2018-02-12 stsp const char *id_str)
265 6dfa2fd3 2018-02-12 stsp {
266 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
267 6dfa2fd3 2018-02-12 stsp
268 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
269 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
270 6dfa2fd3 2018-02-12 stsp
271 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
272 434025f3 2018-09-16 stsp }
273 434025f3 2018-09-16 stsp
274 434025f3 2018-09-16 stsp static const struct got_error *
275 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
276 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
277 434025f3 2018-09-16 stsp {
278 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
279 434025f3 2018-09-16 stsp
280 434025f3 2018-09-16 stsp if (check_cache) {
281 434025f3 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
282 434025f3 2018-09-16 stsp if (*commit != NULL) {
283 434025f3 2018-09-16 stsp (*commit)->refcnt++;
284 434025f3 2018-09-16 stsp return NULL;
285 434025f3 2018-09-16 stsp }
286 434025f3 2018-09-16 stsp } else
287 434025f3 2018-09-16 stsp *commit = NULL;
288 434025f3 2018-09-16 stsp
289 434025f3 2018-09-16 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
290 434025f3 2018-09-16 stsp return got_error(GOT_ERR_OBJ_TYPE);
291 434025f3 2018-09-16 stsp
292 434025f3 2018-09-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
293 434025f3 2018-09-16 stsp struct got_pack *pack;
294 434025f3 2018-09-16 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
295 434025f3 2018-09-16 stsp if (pack == NULL) {
296 434025f3 2018-09-16 stsp err = got_repo_cache_pack(&pack, repo,
297 434025f3 2018-09-16 stsp obj->path_packfile, NULL);
298 434025f3 2018-09-16 stsp if (err)
299 434025f3 2018-09-16 stsp return err;
300 434025f3 2018-09-16 stsp }
301 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
302 434025f3 2018-09-16 stsp } else {
303 434025f3 2018-09-16 stsp int fd;
304 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
305 434025f3 2018-09-16 stsp if (err)
306 434025f3 2018-09-16 stsp return err;
307 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
308 434025f3 2018-09-16 stsp close(fd);
309 434025f3 2018-09-16 stsp }
310 434025f3 2018-09-16 stsp
311 434025f3 2018-09-16 stsp if (err == NULL) {
312 434025f3 2018-09-16 stsp (*commit)->refcnt++;
313 434025f3 2018-09-16 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
314 434025f3 2018-09-16 stsp }
315 434025f3 2018-09-16 stsp
316 434025f3 2018-09-16 stsp return err;
317 bff6ca00 2018-04-23 stsp }
318 bff6ca00 2018-04-23 stsp
319 bff6ca00 2018-04-23 stsp const struct got_error *
320 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
321 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
322 be6a1b5a 2018-06-11 stsp {
323 be6a1b5a 2018-06-11 stsp const struct got_error *err;
324 be6a1b5a 2018-06-11 stsp struct got_object *obj;
325 835e0dbd 2018-06-21 stsp
326 e8eb494a 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, id);
327 e8eb494a 2018-09-16 stsp if (*commit != NULL) {
328 e8eb494a 2018-09-16 stsp (*commit)->refcnt++;
329 e8eb494a 2018-09-16 stsp return NULL;
330 e8eb494a 2018-09-16 stsp }
331 be6a1b5a 2018-06-11 stsp
332 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
333 be6a1b5a 2018-06-11 stsp if (err)
334 be6a1b5a 2018-06-11 stsp return err;
335 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
336 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
337 be6a1b5a 2018-06-11 stsp goto done;
338 be6a1b5a 2018-06-11 stsp }
339 be6a1b5a 2018-06-11 stsp
340 434025f3 2018-09-16 stsp err = open_commit(commit, repo, obj, 0);
341 be6a1b5a 2018-06-11 stsp done:
342 be6a1b5a 2018-06-11 stsp got_object_close(obj);
343 be6a1b5a 2018-06-11 stsp return err;
344 434025f3 2018-09-16 stsp }
345 434025f3 2018-09-16 stsp
346 434025f3 2018-09-16 stsp const struct got_error *
347 434025f3 2018-09-16 stsp got_object_commit_open(struct got_commit_object **commit,
348 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
349 434025f3 2018-09-16 stsp {
350 434025f3 2018-09-16 stsp return open_commit(commit, repo, obj, 1);
351 be6a1b5a 2018-06-11 stsp }
352 be6a1b5a 2018-06-11 stsp
353 be6a1b5a 2018-06-11 stsp const struct got_error *
354 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
355 dbc6a6b6 2018-07-12 stsp {
356 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
357 dbc6a6b6 2018-07-12 stsp
358 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
359 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
360 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
361 dbc6a6b6 2018-07-12 stsp
362 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
363 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
364 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
365 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
366 dbc6a6b6 2018-07-12 stsp *qid = NULL;
367 dbc6a6b6 2018-07-12 stsp return err;
368 dbc6a6b6 2018-07-12 stsp }
369 dbc6a6b6 2018-07-12 stsp
370 dbc6a6b6 2018-07-12 stsp return NULL;
371 7e212e3d 2018-09-09 stsp }
372 7e212e3d 2018-09-09 stsp
373 71eb0e7f 2018-09-16 stsp static const struct got_error *
374 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
375 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
376 0ffeb3c2 2017-11-26 stsp {
377 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
378 f6be5c30 2018-06-22 stsp
379 71eb0e7f 2018-09-16 stsp if (check_cache) {
380 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
381 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
382 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
383 71eb0e7f 2018-09-16 stsp return NULL;
384 71eb0e7f 2018-09-16 stsp }
385 71eb0e7f 2018-09-16 stsp } else
386 71eb0e7f 2018-09-16 stsp *tree = NULL;
387 0ffeb3c2 2017-11-26 stsp
388 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
389 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
390 0ffeb3c2 2017-11-26 stsp
391 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
392 e7885405 2018-09-10 stsp struct got_pack *pack;
393 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
394 e7885405 2018-09-10 stsp if (pack == NULL) {
395 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
396 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
397 e7885405 2018-09-10 stsp if (err)
398 e7885405 2018-09-10 stsp return err;
399 e7885405 2018-09-10 stsp }
400 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
401 e0ab43e7 2018-03-16 stsp } else {
402 d5003b79 2018-04-22 stsp int fd;
403 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
404 e0ab43e7 2018-03-16 stsp if (err)
405 e0ab43e7 2018-03-16 stsp return err;
406 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
407 d5003b79 2018-04-22 stsp close(fd);
408 776d4d29 2018-06-17 stsp }
409 f6be5c30 2018-06-22 stsp
410 f6be5c30 2018-06-22 stsp if (err == NULL) {
411 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
412 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
413 f6be5c30 2018-06-22 stsp }
414 f6be5c30 2018-06-22 stsp
415 776d4d29 2018-06-17 stsp return err;
416 776d4d29 2018-06-17 stsp }
417 776d4d29 2018-06-17 stsp
418 776d4d29 2018-06-17 stsp const struct got_error *
419 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
420 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
421 776d4d29 2018-06-17 stsp {
422 776d4d29 2018-06-17 stsp const struct got_error *err;
423 776d4d29 2018-06-17 stsp struct got_object *obj;
424 835e0dbd 2018-06-21 stsp
425 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
426 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
427 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
428 e8eb494a 2018-09-16 stsp return NULL;
429 e8eb494a 2018-09-16 stsp }
430 776d4d29 2018-06-17 stsp
431 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
432 776d4d29 2018-06-17 stsp if (err)
433 776d4d29 2018-06-17 stsp return err;
434 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
435 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
436 776d4d29 2018-06-17 stsp goto done;
437 e0ab43e7 2018-03-16 stsp }
438 776d4d29 2018-06-17 stsp
439 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
440 776d4d29 2018-06-17 stsp done:
441 776d4d29 2018-06-17 stsp got_object_close(obj);
442 0ffeb3c2 2017-11-26 stsp return err;
443 57efb1af 2018-04-24 stsp }
444 ff6b18f8 2018-04-24 stsp
445 71eb0e7f 2018-09-16 stsp const struct got_error *
446 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
447 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
448 71eb0e7f 2018-09-16 stsp {
449 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
450 71eb0e7f 2018-09-16 stsp }
451 71eb0e7f 2018-09-16 stsp
452 883f0469 2018-06-23 stsp const struct got_tree_entries *
453 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
454 883f0469 2018-06-23 stsp {
455 883f0469 2018-06-23 stsp return &tree->entries;
456 883f0469 2018-06-23 stsp }
457 3840f4c9 2018-09-12 stsp
458 3840f4c9 2018-09-12 stsp static const struct got_error *
459 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
460 3840f4c9 2018-09-12 stsp struct got_pack *pack)
461 3840f4c9 2018-09-12 stsp {
462 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
463 3840f4c9 2018-09-12 stsp int outfd_child;
464 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
465 24140570 2018-09-09 stsp
466 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
467 3840f4c9 2018-09-12 stsp if (basefd == -1)
468 3840f4c9 2018-09-12 stsp return got_error_from_errno();
469 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
470 3840f4c9 2018-09-12 stsp if (accumfd == -1)
471 3840f4c9 2018-09-12 stsp return got_error_from_errno();
472 3840f4c9 2018-09-12 stsp
473 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
474 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
475 3840f4c9 2018-09-12 stsp return got_error_from_errno();
476 3840f4c9 2018-09-12 stsp
477 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
478 3840f4c9 2018-09-12 stsp if (err)
479 3840f4c9 2018-09-12 stsp return err;
480 3840f4c9 2018-09-12 stsp
481 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
482 3840f4c9 2018-09-12 stsp outfd_child);
483 3840f4c9 2018-09-12 stsp if (err) {
484 3840f4c9 2018-09-12 stsp close(outfd_child);
485 3840f4c9 2018-09-12 stsp return err;
486 3840f4c9 2018-09-12 stsp }
487 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
488 3840f4c9 2018-09-12 stsp basefd);
489 3840f4c9 2018-09-12 stsp if (err) {
490 3840f4c9 2018-09-12 stsp close(basefd);
491 3840f4c9 2018-09-12 stsp close(accumfd);
492 3840f4c9 2018-09-12 stsp close(outfd_child);
493 3840f4c9 2018-09-12 stsp return err;
494 3840f4c9 2018-09-12 stsp }
495 3840f4c9 2018-09-12 stsp
496 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
497 3840f4c9 2018-09-12 stsp accumfd);
498 3840f4c9 2018-09-12 stsp if (err) {
499 3840f4c9 2018-09-12 stsp close(accumfd);
500 3840f4c9 2018-09-12 stsp close(outfd_child);
501 3840f4c9 2018-09-12 stsp return err;
502 3840f4c9 2018-09-12 stsp }
503 3840f4c9 2018-09-12 stsp
504 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
505 3840f4c9 2018-09-12 stsp if (err)
506 3840f4c9 2018-09-12 stsp return err;
507 3840f4c9 2018-09-12 stsp
508 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
509 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
510 3840f4c9 2018-09-12 stsp
511 3840f4c9 2018-09-12 stsp return err;
512 3840f4c9 2018-09-12 stsp }
513 3840f4c9 2018-09-12 stsp
514 68482ea3 2017-11-27 stsp const struct got_error *
515 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
516 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
517 68482ea3 2017-11-27 stsp {
518 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
519 55da3778 2018-09-10 stsp int outfd;
520 55da3778 2018-09-10 stsp size_t size;
521 55da3778 2018-09-10 stsp struct stat sb;
522 68482ea3 2017-11-27 stsp
523 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
524 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
525 68482ea3 2017-11-27 stsp
526 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
527 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
528 7d283eee 2017-11-29 stsp
529 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
530 4558fcd4 2018-01-14 stsp if (*blob == NULL)
531 0a585a0d 2018-03-17 stsp return got_error_from_errno();
532 68482ea3 2017-11-27 stsp
533 55da3778 2018-09-10 stsp outfd = got_opentempfd();
534 55da3778 2018-09-10 stsp if (outfd == -1)
535 55da3778 2018-09-10 stsp return got_error_from_errno();
536 55da3778 2018-09-10 stsp
537 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
538 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
539 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
540 c7254d79 2018-04-24 stsp goto done;
541 15c8b0e6 2018-04-24 stsp }
542 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
543 55da3778 2018-09-10 stsp struct got_pack *pack;
544 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
545 55da3778 2018-09-10 stsp if (pack == NULL) {
546 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
547 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
548 55da3778 2018-09-10 stsp if (err)
549 55da3778 2018-09-10 stsp goto done;
550 55da3778 2018-09-10 stsp }
551 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
552 c7254d79 2018-04-24 stsp if (err)
553 c7254d79 2018-04-24 stsp goto done;
554 55da3778 2018-09-10 stsp obj->size = size;
555 eb651edf 2018-02-11 stsp } else {
556 55da3778 2018-09-10 stsp int infd;
557 c7254d79 2018-04-24 stsp
558 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
559 c7254d79 2018-04-24 stsp if (err)
560 c7254d79 2018-04-24 stsp goto done;
561 c7254d79 2018-04-24 stsp
562 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
563 3aca5731 2018-04-24 stsp close(infd);
564 57efb1af 2018-04-24 stsp if (err)
565 c7254d79 2018-04-24 stsp goto done;
566 3aca5731 2018-04-24 stsp
567 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
568 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
569 2967a784 2018-04-24 stsp goto done;
570 2967a784 2018-04-24 stsp }
571 55da3778 2018-09-10 stsp }
572 2967a784 2018-04-24 stsp
573 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
574 55da3778 2018-09-10 stsp err = got_error_from_errno();
575 55da3778 2018-09-10 stsp goto done;
576 55da3778 2018-09-10 stsp }
577 2967a784 2018-04-24 stsp
578 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
579 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
580 55da3778 2018-09-10 stsp goto done;
581 55da3778 2018-09-10 stsp }
582 2967a784 2018-04-24 stsp
583 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
584 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
585 55da3778 2018-09-10 stsp err = got_error_from_errno();
586 55da3778 2018-09-10 stsp close(outfd);
587 55da3778 2018-09-10 stsp goto done;
588 68482ea3 2017-11-27 stsp }
589 68482ea3 2017-11-27 stsp
590 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
591 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
592 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
593 7d283eee 2017-11-29 stsp
594 c7254d79 2018-04-24 stsp done:
595 55da3778 2018-09-10 stsp if (err) {
596 55da3778 2018-09-10 stsp if (*blob) {
597 55da3778 2018-09-10 stsp if ((*blob)->f)
598 55da3778 2018-09-10 stsp fclose((*blob)->f);
599 55da3778 2018-09-10 stsp free((*blob)->read_buf);
600 55da3778 2018-09-10 stsp free(*blob);
601 55da3778 2018-09-10 stsp *blob = NULL;
602 55da3778 2018-09-10 stsp } else if (outfd != -1)
603 55da3778 2018-09-10 stsp close(outfd);
604 a19581a2 2018-06-21 stsp }
605 a19581a2 2018-06-21 stsp return err;
606 a19581a2 2018-06-21 stsp }
607 a19581a2 2018-06-21 stsp
608 a19581a2 2018-06-21 stsp const struct got_error *
609 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
610 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
611 a19581a2 2018-06-21 stsp size_t blocksize)
612 a19581a2 2018-06-21 stsp {
613 a19581a2 2018-06-21 stsp const struct got_error *err;
614 a19581a2 2018-06-21 stsp struct got_object *obj;
615 835e0dbd 2018-06-21 stsp
616 835e0dbd 2018-06-21 stsp *blob = NULL;
617 a19581a2 2018-06-21 stsp
618 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
619 a19581a2 2018-06-21 stsp if (err)
620 a19581a2 2018-06-21 stsp return err;
621 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
622 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
623 a19581a2 2018-06-21 stsp goto done;
624 c7254d79 2018-04-24 stsp }
625 a19581a2 2018-06-21 stsp
626 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
627 a19581a2 2018-06-21 stsp done:
628 a19581a2 2018-06-21 stsp got_object_close(obj);
629 68482ea3 2017-11-27 stsp return err;
630 0ffeb3c2 2017-11-26 stsp }
631 68482ea3 2017-11-27 stsp
632 68482ea3 2017-11-27 stsp void
633 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
634 68482ea3 2017-11-27 stsp {
635 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
636 68482ea3 2017-11-27 stsp fclose(blob->f);
637 68482ea3 2017-11-27 stsp free(blob);
638 f934cf2c 2018-02-12 stsp }
639 f934cf2c 2018-02-12 stsp
640 f934cf2c 2018-02-12 stsp char *
641 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
642 f934cf2c 2018-02-12 stsp {
643 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
644 f934cf2c 2018-02-12 stsp }
645 f934cf2c 2018-02-12 stsp
646 f934cf2c 2018-02-12 stsp size_t
647 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
648 f934cf2c 2018-02-12 stsp {
649 f934cf2c 2018-02-12 stsp return blob->hdrlen;
650 68482ea3 2017-11-27 stsp }
651 68482ea3 2017-11-27 stsp
652 f934cf2c 2018-02-12 stsp const uint8_t *
653 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
654 f934cf2c 2018-02-12 stsp {
655 f934cf2c 2018-02-12 stsp return blob->read_buf;
656 f934cf2c 2018-02-12 stsp }
657 f934cf2c 2018-02-12 stsp
658 68482ea3 2017-11-27 stsp const struct got_error *
659 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
660 68482ea3 2017-11-27 stsp {
661 eb651edf 2018-02-11 stsp size_t n;
662 eb651edf 2018-02-11 stsp
663 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
664 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
665 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
666 eb651edf 2018-02-11 stsp *outlenp = n;
667 35e9ba5d 2018-06-21 stsp return NULL;
668 35e9ba5d 2018-06-21 stsp }
669 35e9ba5d 2018-06-21 stsp
670 35e9ba5d 2018-06-21 stsp const struct got_error *
671 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
672 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
673 35e9ba5d 2018-06-21 stsp {
674 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
675 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
676 84451b3e 2018-07-10 stsp const uint8_t *buf;
677 84451b3e 2018-07-10 stsp int i;
678 84451b3e 2018-07-10 stsp
679 84451b3e 2018-07-10 stsp if (total_len)
680 84451b3e 2018-07-10 stsp *total_len = 0;
681 84451b3e 2018-07-10 stsp if (nlines)
682 84451b3e 2018-07-10 stsp *nlines = 0;
683 35e9ba5d 2018-06-21 stsp
684 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
685 35e9ba5d 2018-06-21 stsp do {
686 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
687 35e9ba5d 2018-06-21 stsp if (err)
688 35e9ba5d 2018-06-21 stsp return err;
689 35e9ba5d 2018-06-21 stsp if (len == 0)
690 35e9ba5d 2018-06-21 stsp break;
691 84451b3e 2018-07-10 stsp if (total_len)
692 84451b3e 2018-07-10 stsp *total_len += len;
693 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
694 84451b3e 2018-07-10 stsp if (nlines) {
695 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
696 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
697 84451b3e 2018-07-10 stsp (*nlines)++;
698 84451b3e 2018-07-10 stsp }
699 84451b3e 2018-07-10 stsp }
700 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
701 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
702 35e9ba5d 2018-06-21 stsp hdrlen = 0;
703 35e9ba5d 2018-06-21 stsp } while (len != 0);
704 35e9ba5d 2018-06-21 stsp
705 35e9ba5d 2018-06-21 stsp fflush(outfile);
706 35e9ba5d 2018-06-21 stsp rewind(outfile);
707 35e9ba5d 2018-06-21 stsp
708 776d4d29 2018-06-17 stsp return NULL;
709 776d4d29 2018-06-17 stsp }
710 776d4d29 2018-06-17 stsp
711 776d4d29 2018-06-17 stsp static struct got_tree_entry *
712 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
713 776d4d29 2018-06-17 stsp {
714 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
715 776d4d29 2018-06-17 stsp
716 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
717 2673a8da 2018-09-19 stsp if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
718 776d4d29 2018-06-17 stsp return te;
719 776d4d29 2018-06-17 stsp }
720 eb651edf 2018-02-11 stsp return NULL;
721 776d4d29 2018-06-17 stsp }
722 776d4d29 2018-06-17 stsp
723 776d4d29 2018-06-17 stsp const struct got_error *
724 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
725 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
726 776d4d29 2018-06-17 stsp {
727 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
728 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
729 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
730 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
731 65a9bbe9 2018-09-15 stsp const char *seg, *s;
732 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
733 776d4d29 2018-06-17 stsp
734 27d434c2 2018-09-15 stsp *id = NULL;
735 776d4d29 2018-06-17 stsp
736 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
737 776d4d29 2018-06-17 stsp if (path[0] != '/')
738 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
739 776d4d29 2018-06-17 stsp
740 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
741 776d4d29 2018-06-17 stsp if (err)
742 776d4d29 2018-06-17 stsp goto done;
743 776d4d29 2018-06-17 stsp
744 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
745 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
746 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
747 27d434c2 2018-09-15 stsp if (*id == NULL)
748 27d434c2 2018-09-15 stsp err = got_error_from_errno();
749 ca008b32 2018-07-22 stsp goto done;
750 776d4d29 2018-06-17 stsp }
751 776d4d29 2018-06-17 stsp
752 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
753 776d4d29 2018-06-17 stsp if (err)
754 776d4d29 2018-06-17 stsp goto done;
755 776d4d29 2018-06-17 stsp
756 65a9bbe9 2018-09-15 stsp s = path;
757 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
758 67606321 2018-06-21 stsp len--;
759 776d4d29 2018-06-17 stsp seg = s;
760 65a9bbe9 2018-09-15 stsp seglen = 0;
761 67606321 2018-06-21 stsp while (len > 0) {
762 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
763 776d4d29 2018-06-17 stsp
764 776d4d29 2018-06-17 stsp if (*s != '/') {
765 776d4d29 2018-06-17 stsp s++;
766 67606321 2018-06-21 stsp len--;
767 65a9bbe9 2018-09-15 stsp seglen++;
768 00530cfb 2018-06-21 stsp if (*s)
769 00530cfb 2018-06-21 stsp continue;
770 776d4d29 2018-06-17 stsp }
771 776d4d29 2018-06-17 stsp
772 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
773 db37e2c0 2018-06-21 stsp if (te == NULL) {
774 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
775 776d4d29 2018-06-17 stsp goto done;
776 776d4d29 2018-06-17 stsp }
777 776d4d29 2018-06-17 stsp
778 67606321 2018-06-21 stsp if (len == 0)
779 67606321 2018-06-21 stsp break;
780 67606321 2018-06-21 stsp
781 776d4d29 2018-06-17 stsp seg = s + 1;
782 65a9bbe9 2018-09-15 stsp seglen = 0;
783 776d4d29 2018-06-17 stsp s++;
784 67606321 2018-06-21 stsp len--;
785 776d4d29 2018-06-17 stsp if (*s) {
786 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
787 db37e2c0 2018-06-21 stsp te->id);
788 db37e2c0 2018-06-21 stsp te = NULL;
789 776d4d29 2018-06-17 stsp if (err)
790 776d4d29 2018-06-17 stsp goto done;
791 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
792 776d4d29 2018-06-17 stsp tree = next_tree;
793 776d4d29 2018-06-17 stsp }
794 776d4d29 2018-06-17 stsp }
795 776d4d29 2018-06-17 stsp
796 27d434c2 2018-09-15 stsp if (te) {
797 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
798 27d434c2 2018-09-15 stsp if (*id == NULL)
799 27d434c2 2018-09-15 stsp return got_error_from_errno();
800 27d434c2 2018-09-15 stsp } else
801 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
802 776d4d29 2018-06-17 stsp done:
803 776d4d29 2018-06-17 stsp if (commit)
804 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
805 776d4d29 2018-06-17 stsp if (tree)
806 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
807 776d4d29 2018-06-17 stsp return err;
808 68482ea3 2017-11-27 stsp }
809 07862c20 2018-09-15 stsp
810 07862c20 2018-09-15 stsp const struct got_error *
811 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
812 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
813 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
814 07862c20 2018-09-15 stsp {
815 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
816 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
817 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
818 65a9bbe9 2018-09-15 stsp const char *seg, *s;
819 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
820 07862c20 2018-09-15 stsp
821 07862c20 2018-09-15 stsp *changed = 0;
822 07862c20 2018-09-15 stsp
823 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
824 07862c20 2018-09-15 stsp if (path[0] != '/')
825 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
826 07862c20 2018-09-15 stsp
827 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
828 07862c20 2018-09-15 stsp if (path[1] == '\0')
829 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
830 07862c20 2018-09-15 stsp
831 07862c20 2018-09-15 stsp tree1 = tree01;
832 07862c20 2018-09-15 stsp tree2 = tree02;
833 65a9bbe9 2018-09-15 stsp s = path;
834 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
835 07862c20 2018-09-15 stsp len--;
836 07862c20 2018-09-15 stsp seg = s;
837 65a9bbe9 2018-09-15 stsp seglen = 0;
838 07862c20 2018-09-15 stsp while (len > 0) {
839 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
840 07862c20 2018-09-15 stsp
841 07862c20 2018-09-15 stsp if (*s != '/') {
842 07862c20 2018-09-15 stsp s++;
843 07862c20 2018-09-15 stsp len--;
844 65a9bbe9 2018-09-15 stsp seglen++;
845 07862c20 2018-09-15 stsp if (*s)
846 07862c20 2018-09-15 stsp continue;
847 07862c20 2018-09-15 stsp }
848 07862c20 2018-09-15 stsp
849 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
850 07862c20 2018-09-15 stsp if (te1 == NULL) {
851 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
852 07862c20 2018-09-15 stsp goto done;
853 07862c20 2018-09-15 stsp }
854 07862c20 2018-09-15 stsp
855 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
856 07862c20 2018-09-15 stsp if (te2 == NULL) {
857 07862c20 2018-09-15 stsp *changed = 1;
858 07862c20 2018-09-15 stsp goto done;
859 07862c20 2018-09-15 stsp }
860 07862c20 2018-09-15 stsp
861 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
862 07862c20 2018-09-15 stsp *changed = 1;
863 07862c20 2018-09-15 stsp goto done;
864 07862c20 2018-09-15 stsp }
865 07862c20 2018-09-15 stsp
866 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
867 07862c20 2018-09-15 stsp *changed = 0;
868 07862c20 2018-09-15 stsp goto done;
869 07862c20 2018-09-15 stsp }
870 07862c20 2018-09-15 stsp
871 f970fa8a 2018-09-15 stsp if (len == 0) { /* final path element */
872 07862c20 2018-09-15 stsp *changed = 1;
873 07862c20 2018-09-15 stsp goto done;
874 07862c20 2018-09-15 stsp }
875 07862c20 2018-09-15 stsp
876 07862c20 2018-09-15 stsp seg = s + 1;
877 07862c20 2018-09-15 stsp s++;
878 07862c20 2018-09-15 stsp len--;
879 65a9bbe9 2018-09-15 stsp seglen = 0;
880 07862c20 2018-09-15 stsp if (*s) {
881 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
882 07862c20 2018-09-15 stsp te1->id);
883 07862c20 2018-09-15 stsp te1 = NULL;
884 07862c20 2018-09-15 stsp if (err)
885 07862c20 2018-09-15 stsp goto done;
886 a31cea73 2018-09-15 stsp if (tree1 != tree01)
887 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
888 07862c20 2018-09-15 stsp tree1 = next_tree1;
889 07862c20 2018-09-15 stsp
890 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
891 07862c20 2018-09-15 stsp te2->id);
892 07862c20 2018-09-15 stsp te2 = NULL;
893 07862c20 2018-09-15 stsp if (err)
894 07862c20 2018-09-15 stsp goto done;
895 a31cea73 2018-09-15 stsp if (tree2 != tree02)
896 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
897 07862c20 2018-09-15 stsp tree2 = next_tree2;
898 07862c20 2018-09-15 stsp }
899 07862c20 2018-09-15 stsp }
900 07862c20 2018-09-15 stsp done:
901 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
902 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
903 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
904 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
905 07862c20 2018-09-15 stsp return err;
906 07862c20 2018-09-15 stsp }