pass smallish blobs directly via imsg buffer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index 14bd628..628c561 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -57,6 +57,7 @@ struct got_tree_object {
struct got_blob_object {
FILE *f;
+ uint8_t *data;
struct got_zstream_buf zb;
size_t hdrlen;
size_t blocksize;
diff --git a/lib/got_lib_pack.h b/lib/got_lib_pack.h
index 63c033d..8276351 100644
--- a/lib/got_lib_pack.h
+++ b/lib/got_lib_pack.h
@@ -164,6 +164,8 @@ int got_packidx_get_object_idx(struct got_packidx *, struct got_object_id *);
const struct got_error *got_packfile_open_object(struct got_object **,
struct got_pack *, struct got_packidx *, int, struct got_object_id *);
+const struct got_error *got_pack_get_object_size(uint64_t *,
+ struct got_object *);
const struct got_error *got_packfile_extract_object(struct got_pack *,
struct got_object *, FILE *, FILE *, FILE *);
const struct got_error *got_packfile_extract_object_to_mem(uint8_t **, size_t *,
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 265fdf3..9d915bb 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -166,8 +166,17 @@ struct got_imsg_tree_object {
struct got_imsg_blob {
size_t size;
size_t hdrlen;
+
+ /*
+ * If size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX, blob data follows
+ * in the imsg buffer. Otherwise, blob data has been written to a
+ * file descriptor passed via the GOT_IMSG_BLOB_OUTFD imsg.
+ */
+#define GOT_PRIVSEP_INLINE_BLOB_DATA_MAX \
+ (MAX_IMSGSIZE - IMSG_HEADER_SIZE - sizeof(struct got_imsg_blob))
};
+
/* Structure for GOT_IMSG_TAG data. */
struct got_imsg_tag_object {
uint8_t id[SHA1_DIGEST_LENGTH];
@@ -242,8 +251,9 @@ const struct got_error *got_privsep_recv_tree(struct got_tree_object **,
struct imsgbuf *);
const struct got_error *got_privsep_send_tree(struct imsgbuf *,
struct got_tree_object *);
-const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t);
-const struct got_error *got_privsep_recv_blob(size_t *, size_t *,
+const struct got_error *got_privsep_send_blob(struct imsgbuf *, size_t, size_t,
+ const uint8_t *);
+const struct got_error *got_privsep_recv_blob(uint8_t **, size_t *, size_t *,
struct imsgbuf *);
const struct got_error *got_privsep_send_tag(struct imsgbuf *,
struct got_tag_object *);
diff --git a/lib/object.c b/lib/object.c
index daab8a5..2e2c89a 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -815,7 +815,7 @@ got_object_tree_get_entries(struct got_tree_object *tree)
}
static const struct got_error *
-request_packed_blob(size_t *size, size_t *hdrlen, int outfd,
+request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
struct got_pack *pack, struct got_packidx *packidx, int idx,
struct got_object_id *id)
{
@@ -861,7 +861,7 @@ request_packed_blob(size_t *size, size_t *hdrlen, int outfd,
return err;
}
- err = got_privsep_recv_blob(size, hdrlen,
+ err = got_privsep_recv_blob(outbuf, size, hdrlen,
pack->privsep_child->ibuf);
if (err)
return err;
@@ -873,8 +873,8 @@ request_packed_blob(size_t *size, size_t *hdrlen, int outfd,
}
static const struct got_error *
-read_packed_blob_privsep(size_t *size, size_t *hdrlen, int outfd,
- struct got_pack *pack, struct got_packidx *packidx, int idx,
+read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
+ int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
struct got_object_id *id)
{
const struct got_error *err = NULL;
@@ -885,12 +885,13 @@ read_packed_blob_privsep(size_t *size, size_t *hdrlen, int outfd,
return err;
}
- return request_packed_blob(size, hdrlen, outfd, pack, packidx, idx, id);
+ return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
+ idx, id);
}
static const struct got_error *
-request_blob(size_t *size, size_t *hdrlen, int outfd, int infd,
- struct imsgbuf *ibuf)
+request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
+ int infd, struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
int outfd_child;
@@ -909,7 +910,7 @@ request_blob(size_t *size, size_t *hdrlen, int outfd, int infd,
return err;
}
- err = got_privsep_recv_blob(size, hdrlen, ibuf);
+ err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
if (err)
return err;
@@ -920,7 +921,7 @@ request_blob(size_t *size, size_t *hdrlen, int outfd, int infd,
}
static const struct got_error *
-read_blob_privsep(size_t *size, size_t *hdrlen,
+read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
int outfd, int infd, struct got_repository *repo)
{
int imsg_fds[2];
@@ -929,7 +930,7 @@ read_blob_privsep(size_t *size, size_t *hdrlen,
if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
- return request_blob(size, hdrlen, outfd, infd, ibuf);
+ return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
}
ibuf = calloc(1, sizeof(*ibuf));
@@ -955,7 +956,7 @@ read_blob_privsep(size_t *size, size_t *hdrlen,
imsg_init(ibuf, imsg_fds[0]);
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
- return request_blob(size, hdrlen, outfd, infd, ibuf);
+ return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
}
static const struct got_error *
@@ -966,6 +967,7 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
struct got_packidx *packidx = NULL;
int idx;
char *path_packfile;
+ uint8_t *outbuf;
int outfd;
size_t size, hdrlen;
struct stat sb;
@@ -999,40 +1001,54 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
if (err)
goto done;
}
- err = read_packed_blob_privsep(&size, &hdrlen, outfd, pack,
- packidx, idx, id);
+ err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
+ pack, packidx, idx, id);
} else if (err->code == GOT_ERR_NO_OBJ) {
int infd;
err = open_loose_object(&infd, id, repo);
if (err)
goto done;
- err = read_blob_privsep(&size, &hdrlen, outfd, infd, repo);
+ err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
+ repo);
close(infd);
}
if (err)
goto done;
- if (fstat(outfd, &sb) == -1) {
- err = got_error_from_errno();
- goto done;
- }
-
- if (sb.st_size != size) {
- err = got_error(GOT_ERR_PRIVSEP_LEN);
- goto done;
- }
-
if (hdrlen > size) {
err = got_error(GOT_ERR_BAD_OBJ_HDR);
goto done;
}
- (*blob)->f = fdopen(outfd, "rb");
- if ((*blob)->f == NULL) {
- err = got_error_from_errno();
+ if (outbuf) {
close(outfd);
- goto done;
+ outfd = -1;
+ (*blob)->f = fmemopen(outbuf, size, "rb");
+ if ((*blob)->f == NULL) {
+ err = got_error_from_errno();
+ free(outbuf);
+ goto done;
+ }
+ (*blob)->data = outbuf;
+ } else {
+ if (fstat(outfd, &sb) == -1) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ if (sb.st_size != size) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ goto done;
+ }
+
+ (*blob)->f = fdopen(outfd, "rb");
+ if ((*blob)->f == NULL) {
+ err = got_error_from_errno();
+ close(outfd);
+ outfd = -1;
+ goto done;
+ }
}
(*blob)->hdrlen = hdrlen;
@@ -1045,6 +1061,7 @@ done:
if ((*blob)->f)
fclose((*blob)->f);
free((*blob)->read_buf);
+ free((*blob)->data);
free(*blob);
*blob = NULL;
} else if (outfd != -1)
@@ -1073,6 +1090,7 @@ got_object_blob_close(struct got_blob_object *blob)
{
free(blob->read_buf);
fclose(blob->f);
+ free(blob->data);
free(blob);
}
diff --git a/lib/pack.c b/lib/pack.c
index 7319045..9a2d1c2 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -986,6 +986,12 @@ get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
return NULL;
}
+const struct got_error *
+got_pack_get_object_size(uint64_t *size, struct got_object *obj)
+{
+ return get_delta_chain_max_size(size, &obj->deltas);
+}
+
static const struct got_error *
dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
struct got_pack *pack, FILE *outfile, FILE *base_file, FILE *accum_file)
diff --git a/lib/privsep.c b/lib/privsep.c
index 923a10b..2f9a594 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -832,29 +832,54 @@ done:
}
const struct got_error *
-got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen)
+got_privsep_send_blob(struct imsgbuf *ibuf, size_t size, size_t hdrlen,
+ const uint8_t *data)
{
struct got_imsg_blob iblob;
iblob.size = size;
iblob.hdrlen = hdrlen;
- /* Data has already been written to file descriptor. */
- if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob, sizeof(iblob))
- == -1)
- return got_error_from_errno();
+ if (data) {
+ uint8_t *buf;
+
+ if (size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
+ return got_error(GOT_ERR_NO_SPACE);
+
+ buf = malloc(sizeof(iblob) + size);
+ if (buf == NULL)
+ return got_error_from_errno();
+
+ memcpy(buf, &iblob, sizeof(iblob));
+ memcpy(buf + sizeof(iblob), data, size);
+ if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, buf,
+ sizeof(iblob) + size) == -1) {
+ free(buf);
+ return got_error_from_errno();
+ }
+ free(buf);
+ } else {
+ /* Data has already been written to file descriptor. */
+ if (imsg_compose(ibuf, GOT_IMSG_BLOB, 0, 0, -1, &iblob,
+ sizeof(iblob)) == -1)
+ return got_error_from_errno();
+ }
+
return flush_imsg(ibuf);
}
const struct got_error *
-got_privsep_recv_blob(size_t *size, size_t *hdrlen, struct imsgbuf *ibuf)
+got_privsep_recv_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
+ struct imsgbuf *ibuf)
{
const struct got_error *err = NULL;
struct imsg imsg;
struct got_imsg_blob *iblob;
size_t datalen;
+ *outbuf = NULL;
+
err = got_privsep_recv_imsg(&imsg, ibuf, 0);
if (err)
return err;
@@ -863,14 +888,30 @@ got_privsep_recv_blob(size_t *size, size_t *hdrlen, struct imsgbuf *ibuf)
switch (imsg.hdr.type) {
case GOT_IMSG_BLOB:
- if (datalen != sizeof(*iblob)) {
+ if (datalen < sizeof(*iblob)) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
break;
}
iblob = imsg.data;
*size = iblob->size;
*hdrlen = iblob->hdrlen;
- /* Data has been written to file descriptor. */
+
+ if (datalen == sizeof(*iblob)) {
+ /* Data has been written to file descriptor. */
+ break;
+ }
+
+ if (*size > GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ break;
+ }
+
+ *outbuf = malloc(*size);
+ if (*outbuf == NULL) {
+ err = got_error_from_errno();
+ break;
+ }
+ memcpy(*outbuf, imsg.data + sizeof(*iblob), *size);
break;
default:
err = got_error(GOT_ERR_PRIVSEP_MSG);
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index d795af6..bbb68a8 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -72,6 +72,7 @@ main(int argc, char *argv[])
FILE *f = NULL;
size_t size;
struct got_object *obj = NULL;
+ uint8_t *buf = NULL;
memset(&imsg, 0, sizeof(imsg));
imsg.fd = -1;
@@ -143,16 +144,22 @@ main(int argc, char *argv[])
goto done;
}
- err = got_inflate_to_fd(&size, f, imsg_outfd.fd);
- if (err)
- goto done;
+ if (obj->size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX) {
+ err = got_inflate_to_mem(&buf, &size, f);
+ if (err)
+ goto done;
+ } else {
+ err = got_inflate_to_fd(&size, f, imsg_outfd.fd);
+ if (err)
+ goto done;
+ }
if (size < obj->hdrlen) {
err = got_error(GOT_ERR_BAD_OBJ_HDR);
goto done;
}
- err = got_privsep_send_blob(&ibuf, size, obj->hdrlen);
+ err = got_privsep_send_blob(&ibuf, size, obj->hdrlen, buf);
done:
if (f)
fclose(f);
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index eec0b58..1d422ec 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -222,6 +222,8 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
struct got_object_id id;
size_t datalen;
+ uint64_t blob_size;
+ uint8_t *buf = NULL;
datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
if (datalen != sizeof(iobj))
@@ -235,21 +237,33 @@ blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
if (err)
- return err;
+ goto done;
err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
if (err)
- return err;
+ goto done;
err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
if (err)
- return err;
+ goto done;
- err = got_packfile_extract_object(pack, obj, outfile, basefile,
- accumfile);
+ if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
+ err = got_pack_get_object_size(&blob_size, obj);
+ if (err)
+ goto done;
+ } else
+ blob_size = obj->size;
+
+ if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
+ err = got_packfile_extract_object_to_mem(&buf, &obj->size,
+ obj, pack);
+ else
+ err = got_packfile_extract_object(pack, obj, outfile, basefile,
+ accumfile);
if (err)
goto done;
- err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen);
+ err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
done:
+ free(buf);
if (outfile)
fclose(outfile);
if (basefile)