2 93658fb9 2020-03-18 stsp * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 668a20f6 2020-03-18 stsp * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
5 93658fb9 2020-03-18 stsp * Permission to use, copy, modify, and distribute this software for any
6 93658fb9 2020-03-18 stsp * purpose with or without fee is hereby granted, provided that the above
7 93658fb9 2020-03-18 stsp * copyright notice and this permission notice appear in all copies.
9 93658fb9 2020-03-18 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 93658fb9 2020-03-18 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 93658fb9 2020-03-18 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 93658fb9 2020-03-18 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 93658fb9 2020-03-18 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 93658fb9 2020-03-18 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 93658fb9 2020-03-18 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 93658fb9 2020-03-18 stsp #include <sys/types.h>
19 142012ed 2022-10-20 thomas #include <sys/queue.h>
20 2e5a6fad 2020-03-18 stsp #include <sys/mman.h>
21 142012ed 2022-10-20 thomas #include <sys/uio.h>
23 93658fb9 2020-03-18 stsp #include <stdint.h>
24 93658fb9 2020-03-18 stsp #include <stdio.h>
25 93658fb9 2020-03-18 stsp #include <stdlib.h>
26 93658fb9 2020-03-18 stsp #include <string.h>
27 142012ed 2022-10-20 thomas #include <imsg.h>
28 142012ed 2022-10-20 thomas #include <limits.h>
29 142012ed 2022-10-20 thomas #include <time.h>
30 81a12da5 2020-09-09 naddy #include <unistd.h>
32 93658fb9 2020-03-18 stsp #include "got_error.h"
33 93658fb9 2020-03-18 stsp #include "got_object.h"
35 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
36 142012ed 2022-10-20 thomas #include "got_lib_delta_cache.h"
37 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
38 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
39 87c63bee 2022-02-23 thomas #include "got_lib_ratelimit.h"
40 142012ed 2022-10-20 thomas #include "got_lib_pack.h"
41 142012ed 2022-10-20 thomas #include "got_lib_pack_index.h"
43 d582f26c 2020-03-18 stsp #ifndef nitems
44 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
47 668a20f6 2020-03-18 stsp static const struct got_error *
48 eebe1fbb 2022-10-20 thomas send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
49 eebe1fbb 2022-10-20 thomas uint32_t nobj_loose, uint32_t nobj_resolved)
51 142012ed 2022-10-20 thomas struct imsgbuf *ibuf = arg;
52 e70bf110 2020-03-22 stsp struct got_imsg_index_pack_progress iprogress;
54 e70bf110 2020-03-22 stsp iprogress.nobj_total = nobj_total;
55 e70bf110 2020-03-22 stsp iprogress.nobj_indexed = nobj_indexed;
56 e70bf110 2020-03-22 stsp iprogress.nobj_loose = nobj_loose;
57 e70bf110 2020-03-22 stsp iprogress.nobj_resolved = nobj_resolved;
59 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
60 e70bf110 2020-03-22 stsp &iprogress, sizeof(iprogress)) == -1)
61 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
63 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
66 e70bf110 2020-03-22 stsp static const struct got_error *
67 e70bf110 2020-03-22 stsp send_index_pack_done(struct imsgbuf *ibuf)
69 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
70 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
71 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
76 93658fb9 2020-03-18 stsp main(int argc, char **argv)
78 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
79 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
80 93658fb9 2020-03-18 stsp struct imsg imsg;
82 6059809a 2020-12-17 stsp int idxfd = -1, tmpfd = -1;
83 d582f26c 2020-03-18 stsp FILE *tmpfiles[3];
84 668a20f6 2020-03-18 stsp struct got_pack pack;
85 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
86 668a20f6 2020-03-18 stsp off_t packfile_size;
87 aecd2225 2022-10-20 thomas struct got_ratelimit rl;
89 668a20f6 2020-03-18 stsp static int attached;
90 668a20f6 2020-03-18 stsp while (!attached)
94 aecd2225 2022-10-20 thomas got_ratelimit_init(&rl, 0, 500);
96 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++)
97 d582f26c 2020-03-18 stsp tmpfiles[i] = NULL;
99 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
100 668a20f6 2020-03-18 stsp pack.fd = -1;
101 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack.delta_cache);
105 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
106 861f3006 2020-03-18 stsp #ifndef PROFILE
107 861f3006 2020-03-18 stsp /* revoke access to most system calls */
108 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
109 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
110 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
114 97799ccd 2022-02-06 thomas /* revoke fs access */
115 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
116 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
117 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
118 97799ccd 2022-02-06 thomas return 1;
120 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
121 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
122 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
126 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
129 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
131 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
132 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
135 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
136 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
139 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
140 668a20f6 2020-03-18 stsp pack.fd = imsg.fd;
142 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
145 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
147 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
148 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
151 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
152 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
155 93658fb9 2020-03-18 stsp idxfd = imsg.fd;
157 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
158 d582f26c 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
161 d582f26c 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
163 d582f26c 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
164 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
167 d582f26c 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
168 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
171 d582f26c 2020-03-18 stsp tmpfd = imsg.fd;
172 d582f26c 2020-03-18 stsp tmpfiles[i] = fdopen(tmpfd, "w+");
173 d582f26c 2020-03-18 stsp if (tmpfiles[i] == NULL) {
174 d582f26c 2020-03-18 stsp err = got_error_from_errno("fdopen");
177 d582f26c 2020-03-18 stsp tmpfd = -1;
180 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
181 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
184 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
185 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
186 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
189 66e6097f 2022-10-27 thomas pack.filesize = packfile_size;
191 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
192 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
196 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
197 aa75acde 2022-10-25 thomas if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
198 aa75acde 2022-10-25 thomas pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
199 aa75acde 2022-10-25 thomas pack.fd, 0);
200 aa75acde 2022-10-25 thomas if (pack.map == MAP_FAILED)
201 aa75acde 2022-10-25 thomas pack.map = NULL; /* fall back to read(2) */
204 aecd2225 2022-10-20 thomas err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
205 aecd2225 2022-10-20 thomas tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
207 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
208 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
209 668a20f6 2020-03-18 stsp err = close_err;
210 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
211 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
212 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
213 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
214 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
215 d582f26c 2020-03-18 stsp if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
216 d582f26c 2020-03-18 stsp err == NULL)
217 812c6838 2021-10-15 thomas err = got_error_from_errno("fclose");
220 668a20f6 2020-03-18 stsp if (err == NULL)
221 e70bf110 2020-03-22 stsp err = send_index_pack_done(&ibuf);
223 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
224 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
225 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);