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>
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_object.h"
40 #include "got_lib_privsep.h"
41 #include "got_lib_ratelimit.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_pack_index.h"
46 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 static const struct got_error *
50 send_index_pack_progress(void *arg, uint32_t nobj_total, uint32_t nobj_indexed,
51 uint32_t nobj_loose, uint32_t nobj_resolved)
53 struct imsgbuf *ibuf = arg;
54 struct got_imsg_index_pack_progress iprogress;
56 iprogress.nobj_total = nobj_total;
57 iprogress.nobj_indexed = nobj_indexed;
58 iprogress.nobj_loose = nobj_loose;
59 iprogress.nobj_resolved = nobj_resolved;
61 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
62 &iprogress, sizeof(iprogress)) == -1)
63 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
65 return got_privsep_flush_imsg(ibuf);
68 static const struct got_error *
69 send_index_pack_done(struct imsgbuf *ibuf)
71 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
72 return got_error_from_errno("imsg_compose FETCH");
73 return got_privsep_flush_imsg(ibuf);
78 main(int argc, char **argv)
80 const struct got_error *err = NULL, *close_err;
84 int idxfd = -1, tmpfd = -1;
87 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
89 struct got_ratelimit rl;
96 got_ratelimit_init(&rl, 0, 500);
98 for (i = 0; i < nitems(tmpfiles); i++)
101 memset(&pack, 0, sizeof(pack));
103 err = got_delta_cache_alloc(&pack.delta_cache);
107 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
109 /* revoke access to most system calls */
110 if (pledge("stdio recvfd", NULL) == -1) {
111 err = got_error_from_errno("pledge");
112 got_privsep_send_error(&ibuf, err);
116 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
119 if (imsg.hdr.type == GOT_IMSG_STOP)
121 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
122 err = got_error(GOT_ERR_PRIVSEP_MSG);
125 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
126 err = got_error(GOT_ERR_PRIVSEP_LEN);
129 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
132 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
135 if (imsg.hdr.type == GOT_IMSG_STOP)
137 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
138 err = got_error(GOT_ERR_PRIVSEP_MSG);
141 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
142 err = got_error(GOT_ERR_PRIVSEP_LEN);
147 for (i = 0; i < nitems(tmpfiles); i++) {
148 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
151 if (imsg.hdr.type == GOT_IMSG_STOP)
153 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
154 err = got_error(GOT_ERR_PRIVSEP_MSG);
157 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
158 err = got_error(GOT_ERR_PRIVSEP_LEN);
162 tmpfiles[i] = fdopen(tmpfd, "w+");
163 if (tmpfiles[i] == NULL) {
164 err = got_error_from_errno("fdopen");
170 if (lseek(pack.fd, 0, SEEK_END) == -1) {
171 err = got_error_from_errno("lseek");
174 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
175 if (packfile_size == -1) {
176 err = got_error_from_errno("lseek");
179 pack.filesize = packfile_size;
181 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
182 err = got_error_from_errno("lseek");
186 #ifndef GOT_PACK_NO_MMAP
187 if (pack.filesize > 0 && pack.filesize <= SIZE_MAX) {
188 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
190 if (pack.map == MAP_FAILED)
191 pack.map = NULL; /* fall back to read(2) */
194 err = got_pack_index(&pack, idxfd, tmpfiles[0], tmpfiles[1],
195 tmpfiles[2], pack_hash, send_index_pack_progress, &ibuf, &rl);
197 close_err = got_pack_close(&pack);
198 if (close_err && err == NULL)
200 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
201 err = got_error_from_errno("close");
202 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
203 err = got_error_from_errno("close");
204 for (i = 0; i < nitems(tmpfiles); i++) {
205 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
207 err = got_error_from_errno("fclose");
211 err = send_index_pack_done(&ibuf);
213 got_privsep_send_error(&ibuf, err);
214 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
215 got_privsep_send_error(&ibuf, err);