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 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
51 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
54 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 ef0981d5 2018-02-12 stsp const struct got_error *
58 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
60 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
62 062ebb78 2018-07-12 stsp *outbuf = malloc(len);
63 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
64 0a585a0d 2018-03-17 stsp return got_error_from_errno();
66 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
67 ef0981d5 2018-02-12 stsp free(*outbuf);
68 ef0981d5 2018-02-12 stsp *outbuf = NULL;
69 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
72 ef0981d5 2018-02-12 stsp return NULL;
76 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
78 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
81 8bf5b3c9 2018-03-17 stsp struct got_object_id *
82 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
84 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
86 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
87 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
88 8bf5b3c9 2018-03-17 stsp return NULL;
89 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
93 3235492e 2018-04-01 stsp struct got_object_id *
94 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
96 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
99 bacc9935 2018-05-20 stsp const struct got_error *
100 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
102 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
106 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
108 b107e67f 2018-01-19 stsp switch (obj->type) {
109 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
110 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
111 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
112 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
113 b107e67f 2018-01-19 stsp return obj->type;
119 96f5e8b3 2018-01-23 stsp /* not reached */
123 ab9a70b2 2017-11-06 stsp static const struct got_error *
124 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
126 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
127 7a132809 2018-07-23 stsp char *hex = NULL;
128 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
130 e6b1056e 2018-04-22 stsp *path = NULL;
132 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
133 0a585a0d 2018-03-17 stsp return got_error_from_errno();
135 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
139 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
140 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
141 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
145 d1cda826 2017-11-06 stsp free(path_objects);
146 d1cda826 2017-11-06 stsp return err;
149 4ee4114f 2018-01-23 stsp static const struct got_error *
150 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
152 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
153 a1fd68d8 2018-01-12 stsp char *path;
155 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
157 d1cda826 2017-11-06 stsp return err;
158 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
159 d5003b79 2018-04-22 stsp if (*fd == -1) {
160 6c00b545 2018-01-17 stsp err = got_error_from_errno();
164 4558fcd4 2018-01-14 stsp free(path);
165 2090a03d 2018-09-09 stsp return err;
168 2090a03d 2018-09-09 stsp static const struct got_error *
169 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
171 2090a03d 2018-09-09 stsp size_t size;
173 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
174 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
175 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
176 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
178 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
179 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
180 2090a03d 2018-09-09 stsp return got_error_from_errno();
182 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
183 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
184 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
185 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
187 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
188 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
190 2090a03d 2018-09-09 stsp return NULL;
193 2090a03d 2018-09-09 stsp static const struct got_error *
194 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
195 2090a03d 2018-09-09 stsp struct got_repository *repo)
197 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
198 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
199 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
201 2090a03d 2018-09-09 stsp char *path_packfile;
203 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
205 2090a03d 2018-09-09 stsp return err;
207 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
209 2090a03d 2018-09-09 stsp return err;
211 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
212 2090a03d 2018-09-09 stsp if (pack == NULL) {
213 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
218 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
222 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
224 2090a03d 2018-09-09 stsp free(path_packfile);
225 4558fcd4 2018-01-14 stsp return err;
228 4558fcd4 2018-01-14 stsp const struct got_error *
229 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
230 4558fcd4 2018-01-14 stsp struct got_object_id *id)
232 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
233 6c00b545 2018-01-17 stsp char *path;
236 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
237 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
238 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
239 7bb0daa1 2018-06-21 stsp return NULL;
242 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
244 4558fcd4 2018-01-14 stsp return err;
246 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
247 2178c42e 2018-04-22 stsp if (fd == -1) {
248 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
249 6c00b545 2018-01-17 stsp err = got_error_from_errno();
252 2090a03d 2018-09-09 stsp err = open_packed_object(obj, id, repo);
255 6c00b545 2018-01-17 stsp if (*obj == NULL)
256 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
258 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
261 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
264 7bb0daa1 2018-06-21 stsp if (err == NULL) {
265 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
266 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
269 6c00b545 2018-01-17 stsp free(path);
270 2178c42e 2018-04-22 stsp if (fd != -1)
272 ab9a70b2 2017-11-06 stsp return err;
276 6dfa2fd3 2018-02-12 stsp const struct got_error *
277 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
278 6dfa2fd3 2018-02-12 stsp const char *id_str)
280 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
282 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
283 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
285 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
288 bff6ca00 2018-04-23 stsp const struct got_error *
289 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
290 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
292 be6a1b5a 2018-06-11 stsp const struct got_error *err;
293 be6a1b5a 2018-06-11 stsp struct got_object *obj;
295 835e0dbd 2018-06-21 stsp *commit = NULL;
297 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
299 be6a1b5a 2018-06-11 stsp return err;
300 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
301 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
305 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
307 be6a1b5a 2018-06-11 stsp got_object_close(obj);
308 be6a1b5a 2018-06-11 stsp return err;
311 be6a1b5a 2018-06-11 stsp const struct got_error *
312 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
314 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
316 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
317 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
318 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
320 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
321 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
322 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
323 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
324 dbc6a6b6 2018-07-12 stsp *qid = NULL;
325 dbc6a6b6 2018-07-12 stsp return err;
328 dbc6a6b6 2018-07-12 stsp return NULL;
331 d1cda826 2017-11-06 stsp const struct got_error *
332 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
333 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
335 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
337 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
338 1943de01 2018-06-22 stsp if (*commit != NULL) {
339 1943de01 2018-06-22 stsp (*commit)->refcnt++;
340 1943de01 2018-06-22 stsp return NULL;
343 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
344 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
346 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
347 cfd633c2 2018-09-10 stsp struct got_pack *pack;
348 cfd633c2 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
349 cfd633c2 2018-09-10 stsp if (pack == NULL) {
350 cfd633c2 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
351 cfd633c2 2018-09-10 stsp obj->path_packfile, NULL);
353 cfd633c2 2018-09-10 stsp return err;
355 cfd633c2 2018-09-10 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
358 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
360 ea35256b 2018-03-16 stsp return err;
361 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
365 1943de01 2018-06-22 stsp if (err == NULL) {
366 1943de01 2018-06-22 stsp (*commit)->refcnt++;
367 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
370 d1cda826 2017-11-06 stsp return err;
373 0ffeb3c2 2017-11-26 stsp const struct got_error *
374 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
375 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
377 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
379 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
380 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
381 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
382 f6be5c30 2018-06-22 stsp return NULL;
385 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
386 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
388 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
389 e7885405 2018-09-10 stsp struct got_pack *pack;
390 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
391 e7885405 2018-09-10 stsp if (pack == NULL) {
392 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
393 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
395 e7885405 2018-09-10 stsp return err;
397 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
400 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
402 e0ab43e7 2018-03-16 stsp return err;
403 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
407 f6be5c30 2018-06-22 stsp if (err == NULL) {
408 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
409 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
412 776d4d29 2018-06-17 stsp return err;
415 776d4d29 2018-06-17 stsp const struct got_error *
416 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
417 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
419 776d4d29 2018-06-17 stsp const struct got_error *err;
420 776d4d29 2018-06-17 stsp struct got_object *obj;
422 835e0dbd 2018-06-21 stsp *tree = NULL;
424 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
426 776d4d29 2018-06-17 stsp return err;
427 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
428 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
432 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
434 776d4d29 2018-06-17 stsp got_object_close(obj);
435 0ffeb3c2 2017-11-26 stsp return err;
438 883f0469 2018-06-23 stsp const struct got_tree_entries *
439 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
441 883f0469 2018-06-23 stsp return &tree->entries;
444 68482ea3 2017-11-27 stsp const struct got_error *
445 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
446 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
448 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
450 55da3778 2018-09-10 stsp size_t size;
451 55da3778 2018-09-10 stsp struct stat sb;
453 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
454 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
456 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
457 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
459 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
460 4558fcd4 2018-01-14 stsp if (*blob == NULL)
461 0a585a0d 2018-03-17 stsp return got_error_from_errno();
463 55da3778 2018-09-10 stsp outfd = got_opentempfd();
464 55da3778 2018-09-10 stsp if (outfd == -1)
465 55da3778 2018-09-10 stsp return got_error_from_errno();
467 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
468 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
469 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
472 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
473 55da3778 2018-09-10 stsp struct got_pack *pack;
474 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
475 55da3778 2018-09-10 stsp if (pack == NULL) {
476 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
477 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
481 55da3778 2018-09-10 stsp err = got_object_read_packed_blob_privsep(&size, outfd,
482 55da3778 2018-09-10 stsp obj, pack);
485 55da3778 2018-09-10 stsp obj->size = size;
489 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
493 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
494 3aca5731 2018-04-24 stsp close(infd);
498 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
499 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
504 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
505 55da3778 2018-09-10 stsp err = got_error_from_errno();
509 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
510 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
514 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
515 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
516 55da3778 2018-09-10 stsp err = got_error_from_errno();
517 55da3778 2018-09-10 stsp close(outfd);
521 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
522 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
523 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
527 55da3778 2018-09-10 stsp if (*blob) {
528 55da3778 2018-09-10 stsp if ((*blob)->f)
529 55da3778 2018-09-10 stsp fclose((*blob)->f);
530 55da3778 2018-09-10 stsp free((*blob)->read_buf);
531 55da3778 2018-09-10 stsp free(*blob);
532 55da3778 2018-09-10 stsp *blob = NULL;
533 55da3778 2018-09-10 stsp } else if (outfd != -1)
534 55da3778 2018-09-10 stsp close(outfd);
536 a19581a2 2018-06-21 stsp return err;
539 a19581a2 2018-06-21 stsp const struct got_error *
540 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
541 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
542 a19581a2 2018-06-21 stsp size_t blocksize)
544 a19581a2 2018-06-21 stsp const struct got_error *err;
545 a19581a2 2018-06-21 stsp struct got_object *obj;
547 835e0dbd 2018-06-21 stsp *blob = NULL;
549 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
551 a19581a2 2018-06-21 stsp return err;
552 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
553 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
557 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
559 a19581a2 2018-06-21 stsp got_object_close(obj);
560 68482ea3 2017-11-27 stsp return err;
564 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
566 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
567 68482ea3 2017-11-27 stsp fclose(blob->f);
568 68482ea3 2017-11-27 stsp free(blob);
572 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
574 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
578 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
580 f934cf2c 2018-02-12 stsp return blob->hdrlen;
583 f934cf2c 2018-02-12 stsp const uint8_t *
584 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
586 f934cf2c 2018-02-12 stsp return blob->read_buf;
589 68482ea3 2017-11-27 stsp const struct got_error *
590 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
594 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
595 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
596 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
597 eb651edf 2018-02-11 stsp *outlenp = n;
598 35e9ba5d 2018-06-21 stsp return NULL;
601 35e9ba5d 2018-06-21 stsp const struct got_error *
602 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
603 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
605 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
606 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
607 84451b3e 2018-07-10 stsp const uint8_t *buf;
610 84451b3e 2018-07-10 stsp if (total_len)
611 84451b3e 2018-07-10 stsp *total_len = 0;
612 84451b3e 2018-07-10 stsp if (nlines)
613 84451b3e 2018-07-10 stsp *nlines = 0;
615 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
617 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
619 35e9ba5d 2018-06-21 stsp return err;
620 35e9ba5d 2018-06-21 stsp if (len == 0)
622 84451b3e 2018-07-10 stsp if (total_len)
623 84451b3e 2018-07-10 stsp *total_len += len;
624 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
625 84451b3e 2018-07-10 stsp if (nlines) {
626 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
627 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
628 84451b3e 2018-07-10 stsp (*nlines)++;
631 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
632 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
633 35e9ba5d 2018-06-21 stsp hdrlen = 0;
634 35e9ba5d 2018-06-21 stsp } while (len != 0);
636 35e9ba5d 2018-06-21 stsp fflush(outfile);
637 35e9ba5d 2018-06-21 stsp rewind(outfile);
639 776d4d29 2018-06-17 stsp return NULL;
642 776d4d29 2018-06-17 stsp static struct got_tree_entry *
643 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
645 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
647 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
648 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
651 eb651edf 2018-02-11 stsp return NULL;
654 776d4d29 2018-06-17 stsp const struct got_error *
655 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
656 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
658 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
659 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
660 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
661 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
662 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
663 67606321 2018-06-21 stsp size_t len = strlen(path);
665 776d4d29 2018-06-17 stsp *obj = NULL;
667 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
668 776d4d29 2018-06-17 stsp if (path[0] != '/')
669 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
671 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
675 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
676 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
677 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
681 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
685 197aa481 2018-06-21 stsp s0 = strdup(path);
686 197aa481 2018-06-21 stsp if (s0 == NULL) {
687 776d4d29 2018-06-17 stsp err = got_error_from_errno();
690 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
695 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
698 67606321 2018-06-21 stsp while (len > 0) {
699 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
701 776d4d29 2018-06-17 stsp if (*s != '/') {
708 776d4d29 2018-06-17 stsp /* end of path segment */
711 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
712 db37e2c0 2018-06-21 stsp if (te == NULL) {
713 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
717 67606321 2018-06-21 stsp if (len == 0)
720 776d4d29 2018-06-17 stsp seg = s + 1;
724 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
729 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
730 776d4d29 2018-06-17 stsp tree = next_tree;
735 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
737 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
740 776d4d29 2018-06-17 stsp if (commit)
741 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
743 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
744 776d4d29 2018-06-17 stsp return err;