Blob


1 /*
2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/mman.h>
21 #include <sys/uio.h>
23 #include <sha1.h>
24 #include <sha2.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <imsg.h>
30 #include <limits.h>
31 #include <time.h>
32 #include <unistd.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_delta_cache.h"
39 #include "got_lib_hash.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_qid.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_ratelimit.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_pack_index.h"
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
51 static const struct got_error *
52 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
53 uint32_t nobj_loose, uint32_t nobj_resolved)
54 {
55 struct imsgbuf *ibuf = arg;
56 struct got_imsg_index_pack_progress iprogress;
58 iprogress.nobj_total = nobj_total;
59 iprogress.nobj_indexed = nobj_indexed;
60 iprogress.nobj_loose = nobj_loose;
61 iprogress.nobj_resolved = nobj_resolved;
63 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
64 &iprogress, sizeof(iprogress)) == -1)
65 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
67 return got_privsep_flush_imsg(ibuf);
68 }
70 static const struct got_error *
71 send_index_pack_done(struct imsgbuf *ibuf)
72 {
73 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
74 return got_error_from_errno("imsg_compose FETCH");
75 return got_privsep_flush_imsg(ibuf);
76 }
79 int
80 main(int argc, char **argv)
81 {
82 const struct got_error *err = NULL, *close_err;
83 struct imsgbuf ibuf;
84 struct imsg imsg;
85 size_t i;
86 int idxfd = -1, tmpfd = -1;
87 FILE *tmpfiles[3];
88 struct got_pack pack;
89 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
90 off_t packfile_size;
91 struct got_ratelimit rl;
92 #if 0
93 static int attached;
94 while (!attached)
95 sleep(1);
96 #endif
98 got_ratelimit_init(&rl, 0, 500);
100 for (i = 0; i < nitems(tmpfiles); i++)
101 tmpfiles[i] = NULL;
103 memset(&pack, 0, sizeof(pack));
104 pack.fd = -1;
105 err = got_delta_cache_alloc(&pack.delta_cache);
106 if (err)
107 goto done;
109 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
110 #ifndef PROFILE
111 /* revoke access to most system calls */
112 if (pledge("stdio recvfd", NULL) == -1) {
113 err = got_error_from_errno("pledge");
114 got_privsep_send_error(&ibuf, err);
115 return 1;
117 #endif
118 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
119 if (err)
120 goto done;
121 if (imsg.hdr.type == GOT_IMSG_STOP)
122 goto done;
123 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
124 err = got_error(GOT_ERR_PRIVSEP_MSG);
125 goto done;
127 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
128 err = got_error(GOT_ERR_PRIVSEP_LEN);
129 goto done;
131 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
132 pack.fd = imsg_get_fd(&imsg);
134 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
135 if (err)
136 goto done;
137 if (imsg.hdr.type == GOT_IMSG_STOP)
138 goto done;
139 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
140 err = got_error(GOT_ERR_PRIVSEP_MSG);
141 goto done;
143 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
144 err = got_error(GOT_ERR_PRIVSEP_LEN);
145 goto done;
147 idxfd = imsg_get_fd(&imsg);
149 for (i = 0; i < nitems(tmpfiles); i++) {
150 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
151 if (err)
152 goto done;
153 if (imsg.hdr.type == GOT_IMSG_STOP)
154 goto done;
155 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
156 err = got_error(GOT_ERR_PRIVSEP_MSG);
157 goto done;
159 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
160 err = got_error(GOT_ERR_PRIVSEP_LEN);
161 goto done;
163 tmpfd = imsg_get_fd(&imsg);
164 tmpfiles[i] = fdopen(tmpfd, "w+");
165 if (tmpfiles[i] == NULL) {
166 err = got_error_from_errno("fdopen");
167 goto done;
169 tmpfd = -1;
172 if (lseek(pack.fd, 0, SEEK_END) == -1) {
173 err = got_error_from_errno("lseek");
174 goto done;
176 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
177 if (packfile_size == -1) {
178 err = got_error_from_errno("lseek");
179 goto done;
181 pack.filesize = packfile_size;
183 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
184 err = got_error_from_errno("lseek");
185 goto done;
188 #ifndef GOT_PACK_NO_MMAP
189 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
190 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
191 pack.fd, 0);
192 if (pack.map == MAP_FAILED)
193 pack.map = NULL; /* fall back to read(2) */
195 #endif
196 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
197 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
198 done:
199 close_err = got_pack_close(&pack);
200 if (close_err && err == NULL)
201 err = close_err;
202 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
203 err = got_error_from_errno("close");
204 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
205 err = got_error_from_errno("close");
206 for (i = 0; i < nitems(tmpfiles); i++) {
207 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
208 err == NULL)
209 err = got_error_from_errno("fclose");
212 if (err == NULL)
213 err = send_index_pack_done(&ibuf);
214 if (err) {
215 got_privsep_send_error(&ibuf, err);
216 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
217 got_privsep_send_error(&ibuf, err);
218 exit(1);
221 exit(0);