2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
31 #include "got_error.h"
32 #include "got_object.h"
34 #include "got_lib_delta.h"
35 #include "got_lib_inflate.h"
36 #include "got_lib_object.h"
37 #include "got_lib_privsep.h"
40 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
43 #define GOT_OBJ_TAG_COMMIT "commit"
44 #define GOT_OBJ_TAG_TREE "tree"
45 #define GOT_OBJ_TAG_BLOB "blob"
47 static const struct got_error *
48 parse_object_header(struct got_object **obj, char *buf, size_t len)
50 const char *obj_tags[] = {
55 const int obj_types[] = {
61 size_t size = 0, hdrlen = 0;
63 char *p = strchr(buf, '\0');
66 return got_error(GOT_ERR_BAD_OBJ_HDR);
68 hdrlen = strlen(buf) + 1 /* '\0' */;
70 for (i = 0; i < nitems(obj_tags); i++) {
71 const char *tag = obj_tags[i];
72 size_t tlen = strlen(tag);
75 if (strncmp(buf, tag, tlen) != 0)
80 return got_error(GOT_ERR_BAD_OBJ_HDR);
81 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
83 return got_error(GOT_ERR_BAD_OBJ_HDR);
88 return got_error(GOT_ERR_BAD_OBJ_HDR);
90 *obj = calloc(1, sizeof(**obj));
92 return got_error_from_errno();
94 (*obj)->hdrlen = hdrlen;
99 static const struct got_error *
100 read_object_header(struct got_object **obj, int fd)
102 const struct got_error *err;
103 struct got_zstream_buf zb;
105 const size_t zbsize = 64;
106 size_t outlen, totlen;
109 buf = malloc(zbsize);
111 return got_error_from_errno();
113 err = got_inflate_init(&zb, buf, zbsize);
119 err = got_inflate_read_fd(&zb, fd, &outlen);
125 if (strchr(zb.outbuf, '\0') == NULL) {
128 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
129 if (newbuf == NULL) {
130 err = got_error_from_errno();
134 zb.outbuf = newbuf + totlen;
135 zb.outlen = (nbuf * zbsize) - totlen;
137 } while (strchr(zb.outbuf, '\0') == NULL);
139 err = parse_object_header(obj, buf, totlen);
142 got_inflate_end(&zb);
148 main(int argc, char *argv[])
150 const struct got_error *err = NULL;
151 struct got_object *obj = NULL;
156 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
158 /* revoke access to most system calls */
159 if (pledge("stdio recvfd", NULL) == -1) {
160 err = got_error_from_errno();
161 got_privsep_send_error(&ibuf, err);
166 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
168 if (imsg.hdr.len == 0)
173 if (imsg.hdr.type == GOT_IMSG_STOP)
176 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
177 err = got_error(GOT_ERR_PRIVSEP_MSG);
181 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
183 err = got_error(GOT_ERR_PRIVSEP_LEN);
187 err = read_object_header(&obj, imsg.fd);
191 err = got_privsep_send_obj(&ibuf, obj, 0);
196 got_object_close(obj);
198 got_privsep_send_error(&ibuf, err);
205 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
206 close(GOT_IMSG_FD_CHILD);