Blame


1 93658fb9 2020-03-18 stsp /*
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>
4 93658fb9 2020-03-18 stsp *
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.
8 93658fb9 2020-03-18 stsp *
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.
16 93658fb9 2020-03-18 stsp */
17 93658fb9 2020-03-18 stsp
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>
22 93658fb9 2020-03-18 stsp
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>
31 93658fb9 2020-03-18 stsp
32 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
33 93658fb9 2020-03-18 stsp #include "got_error.h"
34 93658fb9 2020-03-18 stsp #include "got_object.h"
35 dd038bc6 2021-09-21 thomas.ad
36 93658fb9 2020-03-18 stsp #include "got_lib_delta.h"
37 142012ed 2022-10-20 thomas #include "got_lib_delta_cache.h"
38 b16893ba 2023-02-24 thomas #include "got_lib_hash.h"
39 93658fb9 2020-03-18 stsp #include "got_lib_object.h"
40 232c0ac1 2023-04-22 thomas #include "got_lib_object_qid.h"
41 93658fb9 2020-03-18 stsp #include "got_lib_privsep.h"
42 87c63bee 2022-02-23 thomas #include "got_lib_ratelimit.h"
43 142012ed 2022-10-20 thomas #include "got_lib_pack.h"
44 142012ed 2022-10-20 thomas #include "got_lib_pack_index.h"
45 93658fb9 2020-03-18 stsp
46 d582f26c 2020-03-18 stsp #ifndef nitems
47 d582f26c 2020-03-18 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 d582f26c 2020-03-18 stsp #endif
49 d582f26c 2020-03-18 stsp
50 668a20f6 2020-03-18 stsp static const struct got_error *
51 eebe1fbb 2022-10-20 thomas send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
52 eebe1fbb 2022-10-20 thomas uint32_t nobj_loose, uint32_t nobj_resolved)
53 93658fb9 2020-03-18 stsp {
54 142012ed 2022-10-20 thomas struct imsgbuf *ibuf = arg;
55 e70bf110 2020-03-22 stsp struct got_imsg_index_pack_progress iprogress;
56 87c63bee 2022-02-23 thomas
57 e70bf110 2020-03-22 stsp iprogress.nobj_total = nobj_total;
58 e70bf110 2020-03-22 stsp iprogress.nobj_indexed = nobj_indexed;
59 e70bf110 2020-03-22 stsp iprogress.nobj_loose = nobj_loose;
60 e70bf110 2020-03-22 stsp iprogress.nobj_resolved = nobj_resolved;
61 e70bf110 2020-03-22 stsp
62 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
63 e70bf110 2020-03-22 stsp &iprogress, sizeof(iprogress)) == -1)
64 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
65 e70bf110 2020-03-22 stsp
66 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
67 e70bf110 2020-03-22 stsp }
68 e70bf110 2020-03-22 stsp
69 e70bf110 2020-03-22 stsp static const struct got_error *
70 e70bf110 2020-03-22 stsp send_index_pack_done(struct imsgbuf *ibuf)
71 e70bf110 2020-03-22 stsp {
72 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
73 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose FETCH");
74 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
75 950de2cd 2020-03-18 stsp }
76 93658fb9 2020-03-18 stsp
77 93658fb9 2020-03-18 stsp
78 93658fb9 2020-03-18 stsp int
79 93658fb9 2020-03-18 stsp main(int argc, char **argv)
80 93658fb9 2020-03-18 stsp {
81 668a20f6 2020-03-18 stsp const struct got_error *err = NULL, *close_err;
82 93658fb9 2020-03-18 stsp struct imsgbuf ibuf;
83 93658fb9 2020-03-18 stsp struct imsg imsg;
84 6059809a 2020-12-17 stsp size_t i;
85 6059809a 2020-12-17 stsp int idxfd = -1, tmpfd = -1;
86 d582f26c 2020-03-18 stsp FILE *tmpfiles[3];
87 668a20f6 2020-03-18 stsp struct got_pack pack;
88 668a20f6 2020-03-18 stsp uint8_t pack_hash[SHA1_DIGEST_LENGTH];
89 668a20f6 2020-03-18 stsp off_t packfile_size;
90 aecd2225 2022-10-20 thomas struct got_ratelimit rl;
91 668a20f6 2020-03-18 stsp #if 0
92 668a20f6 2020-03-18 stsp static int attached;
93 668a20f6 2020-03-18 stsp while (!attached)
94 668a20f6 2020-03-18 stsp sleep(1);
95 668a20f6 2020-03-18 stsp #endif
96 93658fb9 2020-03-18 stsp
97 aecd2225 2022-10-20 thomas got_ratelimit_init(&rl, 0, 500);
98 aecd2225 2022-10-20 thomas
99 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++)
100 d582f26c 2020-03-18 stsp tmpfiles[i] = NULL;
101 d582f26c 2020-03-18 stsp
102 668a20f6 2020-03-18 stsp memset(&pack, 0, sizeof(pack));
103 668a20f6 2020-03-18 stsp pack.fd = -1;
104 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack.delta_cache);
105 a5061f77 2022-06-13 thomas if (err)
106 93658fb9 2020-03-18 stsp goto done;
107 668a20f6 2020-03-18 stsp
108 668a20f6 2020-03-18 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
109 861f3006 2020-03-18 stsp #ifndef PROFILE
110 861f3006 2020-03-18 stsp /* revoke access to most system calls */
111 861f3006 2020-03-18 stsp if (pledge("stdio recvfd", NULL) == -1) {
112 861f3006 2020-03-18 stsp err = got_error_from_errno("pledge");
113 861f3006 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
114 861f3006 2020-03-18 stsp return 1;
115 861f3006 2020-03-18 stsp }
116 97799ccd 2022-02-06 thomas
117 97799ccd 2022-02-06 thomas /* revoke fs access */
118 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
119 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
120 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
121 97799ccd 2022-02-06 thomas return 1;
122 97799ccd 2022-02-06 thomas }
123 5d120ea8 2022-06-23 op if (cap_enter() == -1) {
124 5d120ea8 2022-06-23 op err = got_error_from_errno("cap_enter");
125 5d120ea8 2022-06-23 op got_privsep_send_error(&ibuf, err);
126 5d120ea8 2022-06-23 op return 1;
127 5d120ea8 2022-06-23 op }
128 861f3006 2020-03-18 stsp #endif
129 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
130 668a20f6 2020-03-18 stsp if (err)
131 668a20f6 2020-03-18 stsp goto done;
132 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
133 93658fb9 2020-03-18 stsp goto done;
134 93658fb9 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
135 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
136 93658fb9 2020-03-18 stsp goto done;
137 93658fb9 2020-03-18 stsp }
138 668a20f6 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
139 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
140 93658fb9 2020-03-18 stsp goto done;
141 93658fb9 2020-03-18 stsp }
142 668a20f6 2020-03-18 stsp memcpy(pack_hash, imsg.data, sizeof(pack_hash));
143 3d97effa 2024-01-31 thomas pack.fd = imsg_get_fd(&imsg);
144 93658fb9 2020-03-18 stsp
145 668a20f6 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
146 668a20f6 2020-03-18 stsp if (err)
147 93658fb9 2020-03-18 stsp goto done;
148 93658fb9 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
149 93658fb9 2020-03-18 stsp goto done;
150 73ab1060 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
151 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
152 93658fb9 2020-03-18 stsp goto done;
153 93658fb9 2020-03-18 stsp }
154 93658fb9 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
155 93658fb9 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
156 93658fb9 2020-03-18 stsp goto done;
157 93658fb9 2020-03-18 stsp }
158 3d97effa 2024-01-31 thomas idxfd = imsg_get_fd(&imsg);
159 4788f1ce 2020-03-18 stsp
160 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
161 d582f26c 2020-03-18 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
162 d582f26c 2020-03-18 stsp if (err)
163 d582f26c 2020-03-18 stsp goto done;
164 d582f26c 2020-03-18 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
165 d582f26c 2020-03-18 stsp goto done;
166 d582f26c 2020-03-18 stsp if (imsg.hdr.type != GOT_IMSG_TMPFD) {
167 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
168 d582f26c 2020-03-18 stsp goto done;
169 d582f26c 2020-03-18 stsp }
170 d582f26c 2020-03-18 stsp if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
171 d582f26c 2020-03-18 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
172 d582f26c 2020-03-18 stsp goto done;
173 d582f26c 2020-03-18 stsp }
174 3d97effa 2024-01-31 thomas tmpfd = imsg_get_fd(&imsg);
175 d582f26c 2020-03-18 stsp tmpfiles[i] = fdopen(tmpfd, "w+");
176 d582f26c 2020-03-18 stsp if (tmpfiles[i] == NULL) {
177 d582f26c 2020-03-18 stsp err = got_error_from_errno("fdopen");
178 d582f26c 2020-03-18 stsp goto done;
179 d582f26c 2020-03-18 stsp }
180 d582f26c 2020-03-18 stsp tmpfd = -1;
181 4788f1ce 2020-03-18 stsp }
182 93658fb9 2020-03-18 stsp
183 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_END) == -1) {
184 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
185 668a20f6 2020-03-18 stsp goto done;
186 668a20f6 2020-03-18 stsp }
187 668a20f6 2020-03-18 stsp packfile_size = lseek(pack.fd, 0, SEEK_CUR);
188 668a20f6 2020-03-18 stsp if (packfile_size == -1) {
189 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
190 668a20f6 2020-03-18 stsp goto done;
191 668a20f6 2020-03-18 stsp }
192 66e6097f 2022-10-27 thomas pack.filesize = packfile_size;
193 668a20f6 2020-03-18 stsp
194 668a20f6 2020-03-18 stsp if (lseek(pack.fd, 0, SEEK_SET) == -1) {
195 668a20f6 2020-03-18 stsp err = got_error_from_errno("lseek");
196 668a20f6 2020-03-18 stsp goto done;
197 668a20f6 2020-03-18 stsp }
198 668a20f6 2020-03-18 stsp
199 2e5a6fad 2020-03-18 stsp #ifndef GOT_PACK_NO_MMAP
200 aa75acde 2022-10-25 thomas if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
201 aa75acde 2022-10-25 thomas pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
202 aa75acde 2022-10-25 thomas pack.fd, 0);
203 aa75acde 2022-10-25 thomas if (pack.map == MAP_FAILED)
204 aa75acde 2022-10-25 thomas pack.map = NULL; /* fall back to read(2) */
205 aa75acde 2022-10-25 thomas }
206 2e5a6fad 2020-03-18 stsp #endif
207 aecd2225 2022-10-20 thomas err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
208 aecd2225 2022-10-20 thomas tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
209 93658fb9 2020-03-18 stsp done:
210 668a20f6 2020-03-18 stsp close_err = got_pack_close(&pack);
211 668a20f6 2020-03-18 stsp if (close_err && err == NULL)
212 668a20f6 2020-03-18 stsp err = close_err;
213 668a20f6 2020-03-18 stsp if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
214 668a20f6 2020-03-18 stsp err = got_error_from_errno("close");
215 4788f1ce 2020-03-18 stsp if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
216 4788f1ce 2020-03-18 stsp err = got_error_from_errno("close");
217 d582f26c 2020-03-18 stsp for (i = 0; i < nitems(tmpfiles); i++) {
218 d582f26c 2020-03-18 stsp if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
219 d582f26c 2020-03-18 stsp err == NULL)
220 812c6838 2021-10-15 thomas err = got_error_from_errno("fclose");
221 d582f26c 2020-03-18 stsp }
222 668a20f6 2020-03-18 stsp
223 668a20f6 2020-03-18 stsp if (err == NULL)
224 e70bf110 2020-03-22 stsp err = send_index_pack_done(&ibuf);
225 668a20f6 2020-03-18 stsp if (err) {
226 668a20f6 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
227 93658fb9 2020-03-18 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
228 93658fb9 2020-03-18 stsp got_privsep_send_error(&ibuf, err);
229 668a20f6 2020-03-18 stsp exit(1);
230 93658fb9 2020-03-18 stsp }
231 93658fb9 2020-03-18 stsp
232 93658fb9 2020-03-18 stsp exit(0);
233 93658fb9 2020-03-18 stsp }