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 a440fac0 2018-09-06 stsp #include "got_lib_object_parse.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.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 4558fcd4 2018-01-14 stsp return err;
167 4558fcd4 2018-01-14 stsp const struct got_error *
168 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
169 4558fcd4 2018-01-14 stsp struct got_object_id *id)
171 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
172 6c00b545 2018-01-17 stsp char *path;
175 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
176 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
177 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
178 7bb0daa1 2018-06-21 stsp return NULL;
181 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
183 4558fcd4 2018-01-14 stsp return err;
185 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
186 2178c42e 2018-04-22 stsp if (fd == -1) {
187 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
188 6c00b545 2018-01-17 stsp err = got_error_from_errno();
191 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
194 6c00b545 2018-01-17 stsp if (*obj == NULL)
195 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
197 a440fac0 2018-09-06 stsp err = got_object_read_header_privsep(obj, fd);
200 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
203 7bb0daa1 2018-06-21 stsp if (err == NULL) {
204 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
205 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
208 6c00b545 2018-01-17 stsp free(path);
209 2178c42e 2018-04-22 stsp if (fd != -1)
211 ab9a70b2 2017-11-06 stsp return err;
215 6dfa2fd3 2018-02-12 stsp const struct got_error *
216 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
217 6dfa2fd3 2018-02-12 stsp const char *id_str)
219 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
221 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
222 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
224 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
228 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
230 7bb0daa1 2018-06-21 stsp if (obj->refcnt > 0) {
231 7bb0daa1 2018-06-21 stsp obj->refcnt--;
232 cc538cdd 2018-06-22 stsp if (obj->refcnt > 0)
236 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
237 c3703302 2018-01-23 stsp struct got_delta *delta;
238 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
239 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
240 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
241 c3703302 2018-01-23 stsp got_delta_close(delta);
244 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
245 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
249 bff6ca00 2018-04-23 stsp const struct got_error *
250 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
251 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
253 be6a1b5a 2018-06-11 stsp const struct got_error *err;
254 be6a1b5a 2018-06-11 stsp struct got_object *obj;
256 835e0dbd 2018-06-21 stsp *commit = NULL;
258 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
260 be6a1b5a 2018-06-11 stsp return err;
261 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
262 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
266 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
268 be6a1b5a 2018-06-11 stsp got_object_close(obj);
269 be6a1b5a 2018-06-11 stsp return err;
272 be6a1b5a 2018-06-11 stsp const struct got_error *
273 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
275 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
277 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
278 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
279 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
281 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
282 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
283 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
284 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
285 dbc6a6b6 2018-07-12 stsp *qid = NULL;
286 dbc6a6b6 2018-07-12 stsp return err;
289 dbc6a6b6 2018-07-12 stsp return NULL;
293 dbc6a6b6 2018-07-12 stsp got_object_qid_free(struct got_object_qid *qid)
295 dbc6a6b6 2018-07-12 stsp free(qid->id);
299 d1cda826 2017-11-06 stsp const struct got_error *
300 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
301 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
303 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
305 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
306 1943de01 2018-06-22 stsp if (*commit != NULL) {
307 1943de01 2018-06-22 stsp (*commit)->refcnt++;
308 1943de01 2018-06-22 stsp return NULL;
311 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
312 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
314 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
315 ea35256b 2018-03-16 stsp uint8_t *buf;
316 ea35256b 2018-03-16 stsp size_t len;
317 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
319 ea35256b 2018-03-16 stsp return err;
320 b29656e2 2018-03-16 stsp obj->size = len;
321 a440fac0 2018-09-06 stsp err = got_object_parse_commit(commit, buf, len);
325 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
327 ea35256b 2018-03-16 stsp return err;
328 a440fac0 2018-09-06 stsp err = got_object_read_commit_privsep(commit, obj, fd);
332 1943de01 2018-06-22 stsp if (err == NULL) {
333 1943de01 2018-06-22 stsp (*commit)->refcnt++;
334 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
337 d1cda826 2017-11-06 stsp return err;
341 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
343 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
345 1943de01 2018-06-22 stsp if (commit->refcnt > 0) {
346 1943de01 2018-06-22 stsp commit->refcnt--;
347 cc538cdd 2018-06-22 stsp if (commit->refcnt > 0)
351 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
352 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
353 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
354 fa2f6902 2018-07-23 stsp got_object_qid_free(qid);
357 59ece79d 2018-02-12 stsp free(commit->tree_id);
358 d1cda826 2017-11-06 stsp free(commit->author);
359 d1cda826 2017-11-06 stsp free(commit->committer);
360 d1cda826 2017-11-06 stsp free(commit->logmsg);
361 d1cda826 2017-11-06 stsp free(commit);
364 0ffeb3c2 2017-11-26 stsp const struct got_error *
365 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
366 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
368 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
370 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
371 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
372 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
373 f6be5c30 2018-06-22 stsp return NULL;
376 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
377 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
379 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
380 e0ab43e7 2018-03-16 stsp uint8_t *buf;
381 e0ab43e7 2018-03-16 stsp size_t len;
382 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
384 e0ab43e7 2018-03-16 stsp return err;
385 b29656e2 2018-03-16 stsp obj->size = len;
386 a440fac0 2018-09-06 stsp err = got_object_parse_tree(tree, buf, len);
390 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
392 e0ab43e7 2018-03-16 stsp return err;
393 a440fac0 2018-09-06 stsp err = got_object_read_tree_privsep(tree, obj, fd);
397 f6be5c30 2018-06-22 stsp if (err == NULL) {
398 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
399 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
402 776d4d29 2018-06-17 stsp return err;
405 776d4d29 2018-06-17 stsp const struct got_error *
406 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
407 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
409 776d4d29 2018-06-17 stsp const struct got_error *err;
410 776d4d29 2018-06-17 stsp struct got_object *obj;
412 835e0dbd 2018-06-21 stsp *tree = NULL;
414 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
416 776d4d29 2018-06-17 stsp return err;
417 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
418 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
422 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
424 776d4d29 2018-06-17 stsp got_object_close(obj);
425 0ffeb3c2 2017-11-26 stsp return err;
429 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
431 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
433 f6be5c30 2018-06-22 stsp if (tree->refcnt > 0) {
434 f6be5c30 2018-06-22 stsp tree->refcnt--;
435 cc538cdd 2018-06-22 stsp if (tree->refcnt > 0)
439 883f0469 2018-06-23 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
440 883f0469 2018-06-23 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
441 883f0469 2018-06-23 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
442 a440fac0 2018-09-06 stsp got_object_tree_entry_close(te);
445 f715ca7f 2017-11-27 stsp free(tree);
448 883f0469 2018-06-23 stsp const struct got_tree_entries *
449 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
451 883f0469 2018-06-23 stsp return &tree->entries;
454 68482ea3 2017-11-27 stsp const struct got_error *
455 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
456 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
458 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
460 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
461 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
463 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
464 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
466 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
467 4558fcd4 2018-01-14 stsp if (*blob == NULL)
468 0a585a0d 2018-03-17 stsp return got_error_from_errno();
470 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
471 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
472 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
475 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
476 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
480 3aca5731 2018-04-24 stsp int infd, outfd;
481 2967a784 2018-04-24 stsp size_t size;
482 2967a784 2018-04-24 stsp struct stat sb;
484 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
489 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
490 3aca5731 2018-04-24 stsp if (outfd == -1) {
491 3aca5731 2018-04-24 stsp err = got_error_from_errno();
492 3aca5731 2018-04-24 stsp close(infd);
496 a440fac0 2018-09-06 stsp err = got_object_read_blob_privsep(&size, outfd, infd);
497 3aca5731 2018-04-24 stsp close(infd);
501 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
502 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
503 2967a784 2018-04-24 stsp close(outfd);
507 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
508 2967a784 2018-04-24 stsp err = got_error_from_errno();
509 2967a784 2018-04-24 stsp close(outfd);
513 2967a784 2018-04-24 stsp if (sb.st_size != size) {
514 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
515 2967a784 2018-04-24 stsp close(outfd);
519 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
520 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
521 3aca5731 2018-04-24 stsp err = got_error_from_errno();
522 3aca5731 2018-04-24 stsp close(outfd);
527 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
528 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
529 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
532 c7254d79 2018-04-24 stsp if (err && *blob) {
533 c7254d79 2018-04-24 stsp if ((*blob)->f)
534 c7254d79 2018-04-24 stsp fclose((*blob)->f);
535 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
536 c7254d79 2018-04-24 stsp free(*blob);
537 c7254d79 2018-04-24 stsp *blob = NULL;
539 a19581a2 2018-06-21 stsp return err;
542 a19581a2 2018-06-21 stsp const struct got_error *
543 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
544 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
545 a19581a2 2018-06-21 stsp size_t blocksize)
547 a19581a2 2018-06-21 stsp const struct got_error *err;
548 a19581a2 2018-06-21 stsp struct got_object *obj;
550 835e0dbd 2018-06-21 stsp *blob = NULL;
552 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
554 a19581a2 2018-06-21 stsp return err;
555 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
556 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
560 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
562 a19581a2 2018-06-21 stsp got_object_close(obj);
563 68482ea3 2017-11-27 stsp return err;
567 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
569 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
570 68482ea3 2017-11-27 stsp fclose(blob->f);
571 68482ea3 2017-11-27 stsp free(blob);
575 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
577 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
581 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
583 f934cf2c 2018-02-12 stsp return blob->hdrlen;
586 f934cf2c 2018-02-12 stsp const uint8_t *
587 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
589 f934cf2c 2018-02-12 stsp return blob->read_buf;
592 68482ea3 2017-11-27 stsp const struct got_error *
593 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
597 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
598 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
599 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
600 eb651edf 2018-02-11 stsp *outlenp = n;
601 35e9ba5d 2018-06-21 stsp return NULL;
604 35e9ba5d 2018-06-21 stsp const struct got_error *
605 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
606 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
608 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
609 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
610 84451b3e 2018-07-10 stsp const uint8_t *buf;
613 84451b3e 2018-07-10 stsp if (total_len)
614 84451b3e 2018-07-10 stsp *total_len = 0;
615 84451b3e 2018-07-10 stsp if (nlines)
616 84451b3e 2018-07-10 stsp *nlines = 0;
618 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
620 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
622 35e9ba5d 2018-06-21 stsp return err;
623 35e9ba5d 2018-06-21 stsp if (len == 0)
625 84451b3e 2018-07-10 stsp if (total_len)
626 84451b3e 2018-07-10 stsp *total_len += len;
627 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
628 84451b3e 2018-07-10 stsp if (nlines) {
629 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
630 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
631 84451b3e 2018-07-10 stsp (*nlines)++;
634 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
635 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
636 35e9ba5d 2018-06-21 stsp hdrlen = 0;
637 35e9ba5d 2018-06-21 stsp } while (len != 0);
639 35e9ba5d 2018-06-21 stsp fflush(outfile);
640 35e9ba5d 2018-06-21 stsp rewind(outfile);
642 776d4d29 2018-06-17 stsp return NULL;
645 776d4d29 2018-06-17 stsp static struct got_tree_entry *
646 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
648 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
650 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
651 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
654 eb651edf 2018-02-11 stsp return NULL;
657 776d4d29 2018-06-17 stsp const struct got_error *
658 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
659 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
661 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
662 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
663 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
664 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
665 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
666 67606321 2018-06-21 stsp size_t len = strlen(path);
668 776d4d29 2018-06-17 stsp *obj = NULL;
670 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
671 776d4d29 2018-06-17 stsp if (path[0] != '/')
672 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
674 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
678 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
679 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
680 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
684 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
688 197aa481 2018-06-21 stsp s0 = strdup(path);
689 197aa481 2018-06-21 stsp if (s0 == NULL) {
690 776d4d29 2018-06-17 stsp err = got_error_from_errno();
693 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
698 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
701 67606321 2018-06-21 stsp while (len > 0) {
702 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
704 776d4d29 2018-06-17 stsp if (*s != '/') {
711 776d4d29 2018-06-17 stsp /* end of path segment */
714 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
715 db37e2c0 2018-06-21 stsp if (te == NULL) {
716 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
720 67606321 2018-06-21 stsp if (len == 0)
723 776d4d29 2018-06-17 stsp seg = s + 1;
727 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
732 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
733 776d4d29 2018-06-17 stsp tree = next_tree;
738 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
740 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
743 776d4d29 2018-06-17 stsp if (commit)
744 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
746 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
747 776d4d29 2018-06-17 stsp return err;