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