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 7e212e3d 2018-09-09 stsp static const struct got_error *
332 7e212e3d 2018-09-09 stsp extract_packed_object_to_mem(uint8_t **buf, size_t *len,
333 7e212e3d 2018-09-09 stsp struct got_object *obj, struct got_repository *repo)
335 7e212e3d 2018-09-09 stsp const struct got_error *err = NULL;
336 7e212e3d 2018-09-09 stsp struct got_pack *pack;
338 7e212e3d 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
339 7e212e3d 2018-09-09 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
341 7e212e3d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
342 7e212e3d 2018-09-09 stsp if (pack == NULL) {
343 7e212e3d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo,
344 7e212e3d 2018-09-09 stsp obj->path_packfile, NULL);
346 7e212e3d 2018-09-09 stsp return err;
349 7e212e3d 2018-09-09 stsp return got_packfile_extract_object_to_mem(buf, len, obj, pack);
352 d1cda826 2017-11-06 stsp const struct got_error *
353 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
354 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
356 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
358 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
359 1943de01 2018-06-22 stsp if (*commit != NULL) {
360 1943de01 2018-06-22 stsp (*commit)->refcnt++;
361 1943de01 2018-06-22 stsp return NULL;
364 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
365 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
367 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
368 ea35256b 2018-03-16 stsp uint8_t *buf;
369 ea35256b 2018-03-16 stsp size_t len;
370 7e212e3d 2018-09-09 stsp err = extract_packed_object_to_mem(&buf, &len, obj, repo);
372 ea35256b 2018-03-16 stsp return err;
373 b29656e2 2018-03-16 stsp obj->size = len;
374 a440fac0 2018-09-06 stsp err = got_object_parse_commit(commit, buf, len);
378 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
380 ea35256b 2018-03-16 stsp return err;
381 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
385 1943de01 2018-06-22 stsp if (err == NULL) {
386 1943de01 2018-06-22 stsp (*commit)->refcnt++;
387 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
390 d1cda826 2017-11-06 stsp return err;
393 0ffeb3c2 2017-11-26 stsp const struct got_error *
394 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
395 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
397 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
399 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
400 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
401 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
402 f6be5c30 2018-06-22 stsp return NULL;
405 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
406 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
408 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
409 e0ab43e7 2018-03-16 stsp uint8_t *buf;
410 e0ab43e7 2018-03-16 stsp size_t len;
411 7e212e3d 2018-09-09 stsp err = extract_packed_object_to_mem(&buf, &len, obj, repo);
413 e0ab43e7 2018-03-16 stsp return err;
414 b29656e2 2018-03-16 stsp obj->size = len;
415 a440fac0 2018-09-06 stsp err = got_object_parse_tree(tree, buf, len);
419 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
421 e0ab43e7 2018-03-16 stsp return err;
422 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
426 f6be5c30 2018-06-22 stsp if (err == NULL) {
427 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
428 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
431 776d4d29 2018-06-17 stsp return err;
434 776d4d29 2018-06-17 stsp const struct got_error *
435 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
436 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
438 776d4d29 2018-06-17 stsp const struct got_error *err;
439 776d4d29 2018-06-17 stsp struct got_object *obj;
441 835e0dbd 2018-06-21 stsp *tree = NULL;
443 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
445 776d4d29 2018-06-17 stsp return err;
446 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
447 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
451 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
453 776d4d29 2018-06-17 stsp got_object_close(obj);
454 0ffeb3c2 2017-11-26 stsp return err;
457 883f0469 2018-06-23 stsp const struct got_tree_entries *
458 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
460 883f0469 2018-06-23 stsp return &tree->entries;
463 24140570 2018-09-09 stsp static const struct got_error *
464 24140570 2018-09-09 stsp extract_packed_object(FILE **f, struct got_object *obj,
465 24140570 2018-09-09 stsp struct got_repository *repo)
467 24140570 2018-09-09 stsp const struct got_error *err = NULL;
468 24140570 2018-09-09 stsp struct got_pack *pack;
471 24140570 2018-09-09 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
472 24140570 2018-09-09 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
474 24140570 2018-09-09 stsp fd = got_opentempfd();
475 24140570 2018-09-09 stsp if (fd == -1)
476 24140570 2018-09-09 stsp return got_error_from_errno();
478 24140570 2018-09-09 stsp *f = fdopen(fd, "w+");
479 24140570 2018-09-09 stsp if (*f == NULL) {
480 24140570 2018-09-09 stsp err = got_error_from_errno();
484 24140570 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
485 24140570 2018-09-09 stsp if (pack == NULL) {
486 24140570 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo,
487 24140570 2018-09-09 stsp obj->path_packfile, NULL);
492 24140570 2018-09-09 stsp err = got_packfile_extract_object(pack, obj, *f);
495 24140570 2018-09-09 stsp if (*f == NULL)
498 24140570 2018-09-09 stsp fclose(*f);
501 24140570 2018-09-09 stsp return err;
504 68482ea3 2017-11-27 stsp const struct got_error *
505 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
506 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
508 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
510 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
511 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
513 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
514 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
516 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
517 4558fcd4 2018-01-14 stsp if (*blob == NULL)
518 0a585a0d 2018-03-17 stsp return got_error_from_errno();
520 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
521 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
522 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
525 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
526 24140570 2018-09-09 stsp err = extract_packed_object(&((*blob)->f), obj, repo);
530 3aca5731 2018-04-24 stsp int infd, outfd;
531 2967a784 2018-04-24 stsp size_t size;
532 2967a784 2018-04-24 stsp struct stat sb;
534 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
539 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
540 3aca5731 2018-04-24 stsp if (outfd == -1) {
541 3aca5731 2018-04-24 stsp err = got_error_from_errno();
542 3aca5731 2018-04-24 stsp close(infd);
546 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
547 3aca5731 2018-04-24 stsp close(infd);
551 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
552 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
553 2967a784 2018-04-24 stsp close(outfd);
557 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
558 2967a784 2018-04-24 stsp err = got_error_from_errno();
559 2967a784 2018-04-24 stsp close(outfd);
563 2967a784 2018-04-24 stsp if (sb.st_size != size) {
564 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
565 2967a784 2018-04-24 stsp close(outfd);
569 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
570 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
571 3aca5731 2018-04-24 stsp err = got_error_from_errno();
572 3aca5731 2018-04-24 stsp close(outfd);
577 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
578 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
579 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
582 c7254d79 2018-04-24 stsp if (err && *blob) {
583 c7254d79 2018-04-24 stsp if ((*blob)->f)
584 c7254d79 2018-04-24 stsp fclose((*blob)->f);
585 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
586 c7254d79 2018-04-24 stsp free(*blob);
587 c7254d79 2018-04-24 stsp *blob = NULL;
589 a19581a2 2018-06-21 stsp return err;
592 a19581a2 2018-06-21 stsp const struct got_error *
593 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
594 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
595 a19581a2 2018-06-21 stsp size_t blocksize)
597 a19581a2 2018-06-21 stsp const struct got_error *err;
598 a19581a2 2018-06-21 stsp struct got_object *obj;
600 835e0dbd 2018-06-21 stsp *blob = NULL;
602 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
604 a19581a2 2018-06-21 stsp return err;
605 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
606 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
610 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
612 a19581a2 2018-06-21 stsp got_object_close(obj);
613 68482ea3 2017-11-27 stsp return err;
617 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
619 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
620 68482ea3 2017-11-27 stsp fclose(blob->f);
621 68482ea3 2017-11-27 stsp free(blob);
625 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
627 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
631 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
633 f934cf2c 2018-02-12 stsp return blob->hdrlen;
636 f934cf2c 2018-02-12 stsp const uint8_t *
637 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
639 f934cf2c 2018-02-12 stsp return blob->read_buf;
642 68482ea3 2017-11-27 stsp const struct got_error *
643 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
647 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
648 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
649 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
650 eb651edf 2018-02-11 stsp *outlenp = n;
651 35e9ba5d 2018-06-21 stsp return NULL;
654 35e9ba5d 2018-06-21 stsp const struct got_error *
655 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
656 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
658 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
659 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
660 84451b3e 2018-07-10 stsp const uint8_t *buf;
663 84451b3e 2018-07-10 stsp if (total_len)
664 84451b3e 2018-07-10 stsp *total_len = 0;
665 84451b3e 2018-07-10 stsp if (nlines)
666 84451b3e 2018-07-10 stsp *nlines = 0;
668 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
670 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
672 35e9ba5d 2018-06-21 stsp return err;
673 35e9ba5d 2018-06-21 stsp if (len == 0)
675 84451b3e 2018-07-10 stsp if (total_len)
676 84451b3e 2018-07-10 stsp *total_len += len;
677 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
678 84451b3e 2018-07-10 stsp if (nlines) {
679 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
680 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
681 84451b3e 2018-07-10 stsp (*nlines)++;
684 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
685 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
686 35e9ba5d 2018-06-21 stsp hdrlen = 0;
687 35e9ba5d 2018-06-21 stsp } while (len != 0);
689 35e9ba5d 2018-06-21 stsp fflush(outfile);
690 35e9ba5d 2018-06-21 stsp rewind(outfile);
692 776d4d29 2018-06-17 stsp return NULL;
695 776d4d29 2018-06-17 stsp static struct got_tree_entry *
696 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
698 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
700 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
701 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
704 eb651edf 2018-02-11 stsp return NULL;
707 776d4d29 2018-06-17 stsp const struct got_error *
708 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
709 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
711 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
712 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
713 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
714 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
715 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
716 67606321 2018-06-21 stsp size_t len = strlen(path);
718 776d4d29 2018-06-17 stsp *obj = NULL;
720 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
721 776d4d29 2018-06-17 stsp if (path[0] != '/')
722 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
724 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
728 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
729 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
730 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
734 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
738 197aa481 2018-06-21 stsp s0 = strdup(path);
739 197aa481 2018-06-21 stsp if (s0 == NULL) {
740 776d4d29 2018-06-17 stsp err = got_error_from_errno();
743 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
748 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
751 67606321 2018-06-21 stsp while (len > 0) {
752 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
754 776d4d29 2018-06-17 stsp if (*s != '/') {
761 776d4d29 2018-06-17 stsp /* end of path segment */
764 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
765 db37e2c0 2018-06-21 stsp if (te == NULL) {
766 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
770 67606321 2018-06-21 stsp if (len == 0)
773 776d4d29 2018-06-17 stsp seg = s + 1;
777 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
782 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
783 776d4d29 2018-06-17 stsp tree = next_tree;
788 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
790 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
793 776d4d29 2018-06-17 stsp if (commit)
794 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
796 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
797 776d4d29 2018-06-17 stsp return err;