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 <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <imsg.h>
29 #include <limits.h>
30 #include <time.h>
31 #include <unistd.h>
33 #include "got_error.h"
34 #include "got_object.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_delta_cache.h"
38 #include "got_lib_object.h"
39 #include "got_lib_privsep.h"
40 #include "got_lib_ratelimit.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_pack_index.h"
44 #ifndef nitems
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
46 #endif
48 static const struct got_error *
49 send_index_pack_progress(void *arg, int nobj_total, int nobj_indexed,
50 int nobj_loose, int nobj_resolved)
51 {
52 struct imsgbuf *ibuf = arg;
53 struct got_imsg_index_pack_progress iprogress;
55 iprogress.nobj_total = nobj_total;
56 iprogress.nobj_indexed = nobj_indexed;
57 iprogress.nobj_loose = nobj_loose;
58 iprogress.nobj_resolved = nobj_resolved;
60 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
61 &iprogress, sizeof(iprogress)) == -1)
62 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
64 return got_privsep_flush_imsg(ibuf);
65 }
67 static const struct got_error *
68 send_index_pack_done(struct imsgbuf *ibuf)
69 {
70 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
71 return got_error_from_errno("imsg_compose FETCH");
72 return got_privsep_flush_imsg(ibuf);
73 }
76 int
77 main(int argc, char **argv)
78 {
79 const struct got_error *err = NULL, *close_err;
80 struct imsgbuf ibuf;
81 struct imsg imsg;
82 size_t i;
83 int idxfd = -1, tmpfd = -1;
84 FILE *tmpfiles[3];
85 struct got_pack pack;
86 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
87 off_t packfile_size;
88 #if 0
89 static int attached;
90 while (!attached)
91 sleep(1);
92 #endif
94 for (i = 0; i < nitems(tmpfiles); i++)
95 tmpfiles[i] = NULL;
97 memset(&pack, 0, sizeof(pack));
98 pack.fd = -1;
99 err = got_delta_cache_alloc(&pack.delta_cache);
100 if (err)
101 goto done;
103 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
104 #ifndef PROFILE
105 /* revoke access to most system calls */
106 if (pledge("stdio recvfd", NULL) == -1) {
107 err = got_error_from_errno("pledge");
108 got_privsep_send_error(&ibuf, err);
109 return 1;
111 #endif
112 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
113 if (err)
114 goto done;
115 if (imsg.hdr.type == GOT_IMSG_STOP)
116 goto done;
117 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
118 err = got_error(GOT_ERR_PRIVSEP_MSG);
119 goto done;
121 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
122 err = got_error(GOT_ERR_PRIVSEP_LEN);
123 goto done;
125 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
126 pack.fd = imsg.fd;
128 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
129 if (err)
130 goto done;
131 if (imsg.hdr.type == GOT_IMSG_STOP)
132 goto done;
133 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
134 err = got_error(GOT_ERR_PRIVSEP_MSG);
135 goto done;
137 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
138 err = got_error(GOT_ERR_PRIVSEP_LEN);
139 goto done;
141 idxfd = imsg.fd;
143 for (i = 0; i < nitems(tmpfiles); i++) {
144 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
145 if (err)
146 goto done;
147 if (imsg.hdr.type == GOT_IMSG_STOP)
148 goto done;
149 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
150 err = got_error(GOT_ERR_PRIVSEP_MSG);
151 goto done;
153 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
154 err = got_error(GOT_ERR_PRIVSEP_LEN);
155 goto done;
157 tmpfd = imsg.fd;
158 tmpfiles[i] = fdopen(tmpfd, "w+");
159 if (tmpfiles[i] == NULL) {
160 err = got_error_from_errno("fdopen");
161 goto done;
163 tmpfd = -1;
166 if (lseek(pack.fd, 0, SEEK_END) == -1) {
167 err = got_error_from_errno("lseek");
168 goto done;
170 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
171 if (packfile_size == -1) {
172 err = got_error_from_errno("lseek");
173 goto done;
175 pack.filesize = packfile_size; /* XXX off_t vs size_t */
177 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
178 err = got_error_from_errno("lseek");
179 goto done;
182 #ifndef GOT_PACK_NO_MMAP
183 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
184 pack.fd, 0);
185 if (pack.map == MAP_FAILED)
186 pack.map = NULL; /* fall back to read(2) */
187 #endif
188 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
189 pack_hash, send_index_pack_progress, &ibuf);
190 done:
191 close_err = got_pack_close(&pack);
192 if (close_err && err == NULL)
193 err = close_err;
194 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
195 err = got_error_from_errno("close");
196 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
197 err = got_error_from_errno("close");
198 for (i = 0; i < nitems(tmpfiles); i++) {
199 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
200 err == NULL)
201 err = got_error_from_errno("fclose");
204 if (err == NULL)
205 err = send_index_pack_done(&ibuf);
206 if (err) {
207 got_privsep_send_error(&ibuf, err);
208 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
209 got_privsep_send_error(&ibuf, err);
210 exit(1);
213 exit(0);