2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <sys/queue.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"
45 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
48 static const struct got_error *
49 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
50 uint32_t nobj_loose, uint32_t nobj_resolved)
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);
67 static const struct got_error *
68 send_index_pack_done(struct imsgbuf *ibuf)
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);
77 main(int argc, char **argv)
79 const struct got_error *err = NULL, *close_err;
83 int idxfd = -1, tmpfd = -1;
86 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
88 struct got_ratelimit rl;
95 got_ratelimit_init(&rl, 0, 500);
97 for (i = 0; i < nitems(tmpfiles); i++)
100 memset(&pack, 0, sizeof(pack));
102 err = got_delta_cache_alloc(&pack.delta_cache);
106 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
108 /* revoke access to most system calls */
109 if (pledge("stdio recvfd", NULL) == -1) {
110 err = got_error_from_errno("pledge");
111 got_privsep_send_error(&ibuf, err);
115 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
118 if (imsg.hdr.type == GOT_IMSG_STOP)
120 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
121 err = got_error(GOT_ERR_PRIVSEP_MSG);
124 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
125 err = got_error(GOT_ERR_PRIVSEP_LEN);
128 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
131 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
134 if (imsg.hdr.type == GOT_IMSG_STOP)
136 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
137 err = got_error(GOT_ERR_PRIVSEP_MSG);
140 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
141 err = got_error(GOT_ERR_PRIVSEP_LEN);
146 for (i = 0; i < nitems(tmpfiles); i++) {
147 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
150 if (imsg.hdr.type == GOT_IMSG_STOP)
152 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
153 err = got_error(GOT_ERR_PRIVSEP_MSG);
156 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
157 err = got_error(GOT_ERR_PRIVSEP_LEN);
161 tmpfiles[i] = fdopen(tmpfd, "w+");
162 if (tmpfiles[i] == NULL) {
163 err = got_error_from_errno("fdopen");
169 if (lseek(pack.fd, 0, SEEK_END) == -1) {
170 err = got_error_from_errno("lseek");
173 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
174 if (packfile_size == -1) {
175 err = got_error_from_errno("lseek");
178 pack.filesize = packfile_size;
180 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
181 err = got_error_from_errno("lseek");
185 #ifndef GOT_PACK_NO_MMAP
186 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
187 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
189 if (pack.map == MAP_FAILED)
190 pack.map = NULL; /* fall back to read(2) */
193 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
194 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
196 close_err = got_pack_close(&pack);
197 if (close_err && err == NULL)
199 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
200 err = got_error_from_errno("close");
201 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
202 err = got_error_from_errno("close");
203 for (i = 0; i < nitems(tmpfiles); i++) {
204 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
206 err = got_error_from_errno("fclose");
210 err = send_index_pack_done(&ibuf);
212 got_privsep_send_error(&ibuf, err);
213 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
214 got_privsep_send_error(&ibuf, err);