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>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 d71d75ad 2017-11-05 stsp #include <sha1.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
35 788c352e 2018-06-16 stsp #include <time.h>
37 ab9a70b2 2017-11-06 stsp #include "got_error.h"
38 d71d75ad 2017-11-05 stsp #include "got_object.h"
39 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
46 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
49 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
50 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
53 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 ef0981d5 2018-02-12 stsp const struct got_error *
57 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
59 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
61 062ebb78 2018-07-12 stsp *outbuf = malloc(len);
62 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
63 0a585a0d 2018-03-17 stsp return got_error_from_errno();
65 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
66 ef0981d5 2018-02-12 stsp free(*outbuf);
67 ef0981d5 2018-02-12 stsp *outbuf = NULL;
68 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
71 ef0981d5 2018-02-12 stsp return NULL;
75 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
80 8bf5b3c9 2018-03-17 stsp struct got_object_id *
81 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
83 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
85 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
86 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
87 8bf5b3c9 2018-03-17 stsp return NULL;
88 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
92 3235492e 2018-04-01 stsp struct got_object_id *
93 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
95 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
98 bacc9935 2018-05-20 stsp const struct got_error *
99 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
101 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
105 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
107 b107e67f 2018-01-19 stsp switch (obj->type) {
108 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
109 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
110 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
111 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
112 b107e67f 2018-01-19 stsp return obj->type;
118 96f5e8b3 2018-01-23 stsp /* not reached */
122 ab9a70b2 2017-11-06 stsp static const struct got_error *
123 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
125 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
126 7a132809 2018-07-23 stsp char *hex = NULL;
127 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
129 e6b1056e 2018-04-22 stsp *path = NULL;
131 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
132 0a585a0d 2018-03-17 stsp return got_error_from_errno();
134 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
138 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
139 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
140 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
144 d1cda826 2017-11-06 stsp free(path_objects);
145 d1cda826 2017-11-06 stsp return err;
148 4ee4114f 2018-01-23 stsp static const struct got_error *
149 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
151 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
152 a1fd68d8 2018-01-12 stsp char *path;
154 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
156 d1cda826 2017-11-06 stsp return err;
157 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
158 d5003b79 2018-04-22 stsp if (*fd == -1) {
159 6c00b545 2018-01-17 stsp err = got_error_from_errno();
163 4558fcd4 2018-01-14 stsp free(path);
164 2090a03d 2018-09-09 stsp return err;
167 2090a03d 2018-09-09 stsp static const struct got_error *
168 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
170 2090a03d 2018-09-09 stsp size_t size;
172 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
173 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
174 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
175 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
177 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
178 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
179 2090a03d 2018-09-09 stsp return got_error_from_errno();
181 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
182 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
183 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
184 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
186 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
187 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
189 2090a03d 2018-09-09 stsp return NULL;
192 2090a03d 2018-09-09 stsp static const struct got_error *
193 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
194 2090a03d 2018-09-09 stsp struct got_repository *repo)
196 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
197 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
198 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
200 2090a03d 2018-09-09 stsp char *path_packfile;
202 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
204 2090a03d 2018-09-09 stsp return err;
206 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
208 2090a03d 2018-09-09 stsp return err;
210 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
211 2090a03d 2018-09-09 stsp if (pack == NULL) {
212 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
217 2090a03d 2018-09-09 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
221 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
223 2090a03d 2018-09-09 stsp free(path_packfile);
224 4558fcd4 2018-01-14 stsp return err;
227 4558fcd4 2018-01-14 stsp const struct got_error *
228 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
229 4558fcd4 2018-01-14 stsp struct got_object_id *id)
231 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
232 6c00b545 2018-01-17 stsp char *path;
235 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
236 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
237 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
238 7bb0daa1 2018-06-21 stsp return NULL;
241 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
243 4558fcd4 2018-01-14 stsp return err;
245 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
246 2178c42e 2018-04-22 stsp if (fd == -1) {
247 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
248 6c00b545 2018-01-17 stsp err = got_error_from_errno();
251 2090a03d 2018-09-09 stsp err = open_packed_object(obj, id, repo);
254 6c00b545 2018-01-17 stsp if (*obj == NULL)
255 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
257 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
260 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
263 7bb0daa1 2018-06-21 stsp if (err == NULL) {
264 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
265 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
268 6c00b545 2018-01-17 stsp free(path);
269 2178c42e 2018-04-22 stsp if (fd != -1)
271 ab9a70b2 2017-11-06 stsp return err;
275 6dfa2fd3 2018-02-12 stsp const struct got_error *
276 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
277 6dfa2fd3 2018-02-12 stsp const char *id_str)
279 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
281 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
282 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
284 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
287 bff6ca00 2018-04-23 stsp const struct got_error *
288 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
289 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
291 be6a1b5a 2018-06-11 stsp const struct got_error *err;
292 be6a1b5a 2018-06-11 stsp struct got_object *obj;
294 835e0dbd 2018-06-21 stsp *commit = NULL;
296 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
298 be6a1b5a 2018-06-11 stsp return err;
299 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
300 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
304 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
306 be6a1b5a 2018-06-11 stsp got_object_close(obj);
307 be6a1b5a 2018-06-11 stsp return err;
310 be6a1b5a 2018-06-11 stsp const struct got_error *
311 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
313 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
315 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
316 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
317 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
319 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
320 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
321 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
322 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
323 dbc6a6b6 2018-07-12 stsp *qid = NULL;
324 dbc6a6b6 2018-07-12 stsp return err;
327 dbc6a6b6 2018-07-12 stsp return NULL;
330 d1cda826 2017-11-06 stsp const struct got_error *
331 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
332 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
334 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
336 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
337 1943de01 2018-06-22 stsp if (*commit != NULL) {
338 1943de01 2018-06-22 stsp (*commit)->refcnt++;
339 1943de01 2018-06-22 stsp return NULL;
342 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
343 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
345 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
346 ea35256b 2018-03-16 stsp uint8_t *buf;
347 ea35256b 2018-03-16 stsp size_t len;
348 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
350 ea35256b 2018-03-16 stsp return err;
351 b29656e2 2018-03-16 stsp obj->size = len;
352 a440fac0 2018-09-06 stsp err = got_object_parse_commit(commit, buf, len);
356 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
358 ea35256b 2018-03-16 stsp return err;
359 ad242220 2018-09-08 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
363 1943de01 2018-06-22 stsp if (err == NULL) {
364 1943de01 2018-06-22 stsp (*commit)->refcnt++;
365 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
368 d1cda826 2017-11-06 stsp return err;
371 0ffeb3c2 2017-11-26 stsp const struct got_error *
372 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
373 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
375 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
377 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
378 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
379 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
380 f6be5c30 2018-06-22 stsp return NULL;
383 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
384 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
386 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
387 e0ab43e7 2018-03-16 stsp uint8_t *buf;
388 e0ab43e7 2018-03-16 stsp size_t len;
389 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
391 e0ab43e7 2018-03-16 stsp return err;
392 b29656e2 2018-03-16 stsp obj->size = len;
393 a440fac0 2018-09-06 stsp err = got_object_parse_tree(tree, buf, len);
397 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
399 e0ab43e7 2018-03-16 stsp return err;
400 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
404 f6be5c30 2018-06-22 stsp if (err == NULL) {
405 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
406 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
409 776d4d29 2018-06-17 stsp return err;
412 776d4d29 2018-06-17 stsp const struct got_error *
413 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
414 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
416 776d4d29 2018-06-17 stsp const struct got_error *err;
417 776d4d29 2018-06-17 stsp struct got_object *obj;
419 835e0dbd 2018-06-21 stsp *tree = NULL;
421 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
423 776d4d29 2018-06-17 stsp return err;
424 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
425 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
429 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
431 776d4d29 2018-06-17 stsp got_object_close(obj);
432 0ffeb3c2 2017-11-26 stsp return err;
435 883f0469 2018-06-23 stsp const struct got_tree_entries *
436 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
438 883f0469 2018-06-23 stsp return &tree->entries;
441 68482ea3 2017-11-27 stsp const struct got_error *
442 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
443 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
445 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
447 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
448 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
450 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
451 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
453 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
454 4558fcd4 2018-01-14 stsp if (*blob == NULL)
455 0a585a0d 2018-03-17 stsp return got_error_from_errno();
457 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
458 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
459 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
462 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
463 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
467 3aca5731 2018-04-24 stsp int infd, outfd;
468 2967a784 2018-04-24 stsp size_t size;
469 2967a784 2018-04-24 stsp struct stat sb;
471 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
476 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
477 3aca5731 2018-04-24 stsp if (outfd == -1) {
478 3aca5731 2018-04-24 stsp err = got_error_from_errno();
479 3aca5731 2018-04-24 stsp close(infd);
483 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
484 3aca5731 2018-04-24 stsp close(infd);
488 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
489 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
490 2967a784 2018-04-24 stsp close(outfd);
494 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
495 2967a784 2018-04-24 stsp err = got_error_from_errno();
496 2967a784 2018-04-24 stsp close(outfd);
500 2967a784 2018-04-24 stsp if (sb.st_size != size) {
501 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
502 2967a784 2018-04-24 stsp close(outfd);
506 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
507 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
508 3aca5731 2018-04-24 stsp err = got_error_from_errno();
509 3aca5731 2018-04-24 stsp close(outfd);
514 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
515 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
516 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
519 c7254d79 2018-04-24 stsp if (err && *blob) {
520 c7254d79 2018-04-24 stsp if ((*blob)->f)
521 c7254d79 2018-04-24 stsp fclose((*blob)->f);
522 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
523 c7254d79 2018-04-24 stsp free(*blob);
524 c7254d79 2018-04-24 stsp *blob = NULL;
526 a19581a2 2018-06-21 stsp return err;
529 a19581a2 2018-06-21 stsp const struct got_error *
530 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
531 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
532 a19581a2 2018-06-21 stsp size_t blocksize)
534 a19581a2 2018-06-21 stsp const struct got_error *err;
535 a19581a2 2018-06-21 stsp struct got_object *obj;
537 835e0dbd 2018-06-21 stsp *blob = NULL;
539 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
541 a19581a2 2018-06-21 stsp return err;
542 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
543 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
547 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
549 a19581a2 2018-06-21 stsp got_object_close(obj);
550 68482ea3 2017-11-27 stsp return err;
554 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
556 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
557 68482ea3 2017-11-27 stsp fclose(blob->f);
558 68482ea3 2017-11-27 stsp free(blob);
562 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
564 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
568 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
570 f934cf2c 2018-02-12 stsp return blob->hdrlen;
573 f934cf2c 2018-02-12 stsp const uint8_t *
574 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
576 f934cf2c 2018-02-12 stsp return blob->read_buf;
579 68482ea3 2017-11-27 stsp const struct got_error *
580 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
584 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
585 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
586 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
587 eb651edf 2018-02-11 stsp *outlenp = n;
588 35e9ba5d 2018-06-21 stsp return NULL;
591 35e9ba5d 2018-06-21 stsp const struct got_error *
592 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
593 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
595 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
596 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
597 84451b3e 2018-07-10 stsp const uint8_t *buf;
600 84451b3e 2018-07-10 stsp if (total_len)
601 84451b3e 2018-07-10 stsp *total_len = 0;
602 84451b3e 2018-07-10 stsp if (nlines)
603 84451b3e 2018-07-10 stsp *nlines = 0;
605 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
607 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
609 35e9ba5d 2018-06-21 stsp return err;
610 35e9ba5d 2018-06-21 stsp if (len == 0)
612 84451b3e 2018-07-10 stsp if (total_len)
613 84451b3e 2018-07-10 stsp *total_len += len;
614 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
615 84451b3e 2018-07-10 stsp if (nlines) {
616 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
617 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
618 84451b3e 2018-07-10 stsp (*nlines)++;
621 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
622 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
623 35e9ba5d 2018-06-21 stsp hdrlen = 0;
624 35e9ba5d 2018-06-21 stsp } while (len != 0);
626 35e9ba5d 2018-06-21 stsp fflush(outfile);
627 35e9ba5d 2018-06-21 stsp rewind(outfile);
629 776d4d29 2018-06-17 stsp return NULL;
632 776d4d29 2018-06-17 stsp static struct got_tree_entry *
633 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
635 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
637 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
638 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
641 eb651edf 2018-02-11 stsp return NULL;
644 776d4d29 2018-06-17 stsp const struct got_error *
645 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
646 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
648 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
649 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
650 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
651 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
652 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
653 67606321 2018-06-21 stsp size_t len = strlen(path);
655 776d4d29 2018-06-17 stsp *obj = NULL;
657 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
658 776d4d29 2018-06-17 stsp if (path[0] != '/')
659 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
661 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
665 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
666 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
667 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
671 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
675 197aa481 2018-06-21 stsp s0 = strdup(path);
676 197aa481 2018-06-21 stsp if (s0 == NULL) {
677 776d4d29 2018-06-17 stsp err = got_error_from_errno();
680 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
685 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
688 67606321 2018-06-21 stsp while (len > 0) {
689 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
691 776d4d29 2018-06-17 stsp if (*s != '/') {
698 776d4d29 2018-06-17 stsp /* end of path segment */
701 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
702 db37e2c0 2018-06-21 stsp if (te == NULL) {
703 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
707 67606321 2018-06-21 stsp if (len == 0)
710 776d4d29 2018-06-17 stsp seg = s + 1;
714 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
719 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
720 776d4d29 2018-06-17 stsp tree = next_tree;
725 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
727 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
730 776d4d29 2018-06-17 stsp if (commit)
731 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
733 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
734 776d4d29 2018-06-17 stsp return err;