2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
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)
68 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
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));
77 3235492e 2018-04-01 stsp struct got_object_id *
78 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
80 6402fb3c 2018-09-15 stsp return &obj->id;
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)
86 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
90 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
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;
103 96f5e8b3 2018-01-23 stsp /* not reached */
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)
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);
114 e6b1056e 2018-04-22 stsp *path = NULL;
116 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
119 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
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();
129 d1cda826 2017-11-06 stsp free(path_objects);
130 d1cda826 2017-11-06 stsp return err;
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)
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
139 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
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();
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
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)
155 2090a03d 2018-09-09 stsp size_t size;
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);
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();
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);
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);
174 2090a03d 2018-09-09 stsp return NULL;
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)
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;
185 2090a03d 2018-09-09 stsp char *path_packfile;
187 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
189 2090a03d 2018-09-09 stsp return err;
191 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
193 2090a03d 2018-09-09 stsp return err;
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);
202 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
206 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
208 2090a03d 2018-09-09 stsp free(path_packfile);
209 4558fcd4 2018-01-14 stsp return err;
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)
216 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
217 6c00b545 2018-01-17 stsp char *path;
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;
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);
234 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
236 4558fcd4 2018-01-14 stsp return err;
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);
243 6c00b545 2018-01-17 stsp err = got_error_from_errno();
246 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
249 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
252 59790a32 2018-09-15 stsp (*obj)->refcnt++;
253 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
255 6c00b545 2018-01-17 stsp free(path);
256 2178c42e 2018-04-22 stsp if (fd != -1)
258 ab9a70b2 2017-11-06 stsp return err;
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)
266 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
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);
271 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
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)
278 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
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;
287 434025f3 2018-09-16 stsp *commit = NULL;
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);
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);
299 434025f3 2018-09-16 stsp return err;
301 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
304 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
306 434025f3 2018-09-16 stsp return err;
307 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
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);
316 434025f3 2018-09-16 stsp return err;
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)
323 be6a1b5a 2018-06-11 stsp const struct got_error *err;
324 be6a1b5a 2018-06-11 stsp struct got_object *obj;
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;
332 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
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);
340 434025f3 2018-09-16 stsp err = open_commit(commit, repo, obj, 0);
342 be6a1b5a 2018-06-11 stsp got_object_close(obj);
343 be6a1b5a 2018-06-11 stsp return err;
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)
350 434025f3 2018-09-16 stsp return open_commit(commit, repo, obj, 1);
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)
356 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
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();
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;
370 dbc6a6b6 2018-07-12 stsp return NULL;
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)
377 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
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;
386 71eb0e7f 2018-09-16 stsp *tree = NULL;
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);
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);
398 e7885405 2018-09-10 stsp return err;
400 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
403 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
405 e0ab43e7 2018-03-16 stsp return err;
406 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
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);
415 776d4d29 2018-06-17 stsp return err;
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)
422 776d4d29 2018-06-17 stsp const struct got_error *err;
423 776d4d29 2018-06-17 stsp struct got_object *obj;
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;
431 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
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);
439 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
441 776d4d29 2018-06-17 stsp got_object_close(obj);
442 0ffeb3c2 2017-11-26 stsp return err;
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)
449 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
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)
455 883f0469 2018-06-23 stsp return &tree->entries;
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)
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 */
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();
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();
477 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
479 3840f4c9 2018-09-12 stsp return err;
481 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
482 3840f4c9 2018-09-12 stsp outfd_child);
484 3840f4c9 2018-09-12 stsp close(outfd_child);
485 3840f4c9 2018-09-12 stsp return err;
487 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
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;
496 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
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;
504 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
506 3840f4c9 2018-09-12 stsp return err;
508 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
509 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
511 3840f4c9 2018-09-12 stsp return err;
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)
518 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
520 55da3778 2018-09-10 stsp size_t size;
521 55da3778 2018-09-10 stsp struct stat sb;
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);
526 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
527 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
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();
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();
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();
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);
551 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
554 55da3778 2018-09-10 stsp obj->size = size;
558 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
562 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
563 3aca5731 2018-04-24 stsp close(infd);
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);
573 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
574 55da3778 2018-09-10 stsp err = got_error_from_errno();
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);
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);
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);
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);
605 a19581a2 2018-06-21 stsp return err;
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)
613 a19581a2 2018-06-21 stsp const struct got_error *err;
614 a19581a2 2018-06-21 stsp struct got_object *obj;
616 835e0dbd 2018-06-21 stsp *blob = NULL;
618 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
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);
626 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
628 a19581a2 2018-06-21 stsp got_object_close(obj);
629 68482ea3 2017-11-27 stsp return err;
633 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
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);
641 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
643 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
647 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
649 f934cf2c 2018-02-12 stsp return blob->hdrlen;
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)
655 f934cf2c 2018-02-12 stsp return blob->read_buf;
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)
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;
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)
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;
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;
684 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
686 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
688 35e9ba5d 2018-06-21 stsp return err;
689 35e9ba5d 2018-06-21 stsp if (len == 0)
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)++;
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);
705 35e9ba5d 2018-06-21 stsp fflush(outfile);
706 35e9ba5d 2018-06-21 stsp rewind(outfile);
708 776d4d29 2018-06-17 stsp return NULL;
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)
714 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
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')
720 eb651edf 2018-02-11 stsp return NULL;
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)
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);
734 27d434c2 2018-09-15 stsp *id = NULL;
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);
740 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
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();
752 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
757 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
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;
764 776d4d29 2018-06-17 stsp if (*s != '/') {
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);
778 67606321 2018-06-21 stsp if (len == 0)
781 776d4d29 2018-06-17 stsp seg = s + 1;
782 65a9bbe9 2018-09-15 stsp seglen = 0;
786 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
791 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
792 776d4d29 2018-06-17 stsp tree = next_tree;
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();
801 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
803 776d4d29 2018-06-17 stsp if (commit)
804 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
806 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
807 776d4d29 2018-06-17 stsp return err;
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)
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);
821 07862c20 2018-09-15 stsp *changed = 0;
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);
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);
831 07862c20 2018-09-15 stsp tree1 = tree01;
832 07862c20 2018-09-15 stsp tree2 = tree02;
834 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
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;
841 07862c20 2018-09-15 stsp if (*s != '/') {
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);
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;
861 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
862 07862c20 2018-09-15 stsp *changed = 1;
866 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
867 07862c20 2018-09-15 stsp *changed = 0;
871 f970fa8a 2018-09-15 stsp if (len == 0) { /* final path element */
872 07862c20 2018-09-15 stsp *changed = 1;
876 07862c20 2018-09-15 stsp seg = s + 1;
879 65a9bbe9 2018-09-15 stsp seglen = 0;
881 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
883 07862c20 2018-09-15 stsp te1 = NULL;
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;
890 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
892 07862c20 2018-09-15 stsp te2 = NULL;
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;
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;