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>
22 #include <sys/syslimits.h>
32 #include "got_error.h"
33 #include "got_object.h"
35 #include "got_lib_delta.h"
36 #include "got_lib_inflate.h"
37 #include "got_lib_object.h"
38 #include "got_lib_privsep.h"
41 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 #define GOT_OBJ_TAG_COMMIT "commit"
45 #define GOT_OBJ_TAG_TREE "tree"
46 #define GOT_OBJ_TAG_BLOB "blob"
48 static const struct got_error *
49 parse_object_header(struct got_object **obj, char *buf, size_t len)
51 const char *obj_tags[] = {
56 const int obj_types[] = {
62 size_t size = 0, hdrlen = 0;
64 char *p = strchr(buf, '\0');
67 return got_error(GOT_ERR_BAD_OBJ_HDR);
69 hdrlen = strlen(buf) + 1 /* '\0' */;
71 for (i = 0; i < nitems(obj_tags); i++) {
72 const char *tag = obj_tags[i];
73 size_t tlen = strlen(tag);
76 if (strncmp(buf, tag, tlen) != 0)
81 return got_error(GOT_ERR_BAD_OBJ_HDR);
82 size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
84 return got_error(GOT_ERR_BAD_OBJ_HDR);
89 return got_error(GOT_ERR_BAD_OBJ_HDR);
91 *obj = calloc(1, sizeof(**obj));
93 return got_error_from_errno();
95 (*obj)->hdrlen = hdrlen;
100 static const struct got_error *
101 read_object_header(struct got_object **obj, int fd)
103 const struct got_error *err;
104 struct got_zstream_buf zb;
106 const size_t zbsize = 64;
107 size_t outlen, totlen;
110 buf = malloc(zbsize);
112 return got_error_from_errno();
114 err = got_inflate_init(&zb, buf, zbsize);
120 err = got_inflate_read_fd(&zb, fd, &outlen);
126 if (strchr(zb.outbuf, '\0') == NULL) {
129 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
130 if (newbuf == NULL) {
131 err = got_error_from_errno();
135 zb.outbuf = newbuf + totlen;
136 zb.outlen = (nbuf * zbsize) - totlen;
138 } while (strchr(zb.outbuf, '\0') == NULL);
140 err = parse_object_header(obj, buf, totlen);
143 got_inflate_end(&zb);
149 main(int argc, char *argv[])
151 const struct got_error *err = NULL;
152 struct got_object *obj = NULL;
157 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
160 /* revoke access to most system calls */
161 if (pledge("stdio recvfd", NULL) == -1) {
162 err = got_error_from_errno();
163 got_privsep_send_error(&ibuf, err);
169 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
171 if (err->code == GOT_ERR_PRIVSEP_PIPE)
176 if (imsg.hdr.type == GOT_IMSG_STOP)
179 if (imsg.hdr.type != GOT_IMSG_OBJECT_REQUEST) {
180 err = got_error(GOT_ERR_PRIVSEP_MSG);
184 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
186 err = got_error(GOT_ERR_PRIVSEP_LEN);
190 err = read_object_header(&obj, imsg.fd);
194 err = got_privsep_send_obj(&ibuf, obj);
199 got_object_close(obj);
201 if (err->code == GOT_ERR_PRIVSEP_PIPE)
204 got_privsep_send_error(&ibuf, err);
211 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
212 close(GOT_IMSG_FD_CHILD);