read packed blobs with privsep
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
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index b427544..d77cc53 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -44,3 +44,5 @@ const struct got_error *got_object_read_packed_commit_privsep(
struct got_commit_object **, struct got_object *, struct got_pack *);
const struct got_error *got_object_read_packed_tree_privsep(
struct got_tree_object **, struct got_object *, struct got_pack *);
+const struct got_error *got_object_read_packed_blob_privsep(size_t *, int,
+ struct got_object *, struct got_pack *);
diff --git a/lib/got_lib_privsep.h b/lib/got_lib_privsep.h
index 7a9837d..c36311d 100644
--- a/lib/got_lib_privsep.h
+++ b/lib/got_lib_privsep.h
@@ -210,7 +210,8 @@ const struct got_error *got_privsep_recv_imsg(struct imsg *, struct imsgbuf *,
void got_privsep_send_error(struct imsgbuf *, const struct got_error *);
const struct got_error *got_privsep_send_obj_req(struct imsgbuf *, int,
struct got_object *);
-const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int, int);
+const struct got_error *got_privsep_send_blob_req(struct imsgbuf *, int);
+const struct got_error *got_privsep_send_blob_outfd(struct imsgbuf *, int);
const struct got_error *got_privsep_send_obj(struct imsgbuf *,
struct got_object *);
const struct got_error *got_privsep_get_imsg_obj(struct got_object **,
diff --git a/lib/object.c b/lib/object.c
index dd27607..da1f8b5 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -441,52 +441,14 @@ got_object_tree_get_entries(struct got_tree_object *tree)
return &tree->entries;
}
-static const struct got_error *
-extract_packed_object(FILE **f, struct got_object *obj,
- struct got_repository *repo)
-{
- const struct got_error *err = NULL;
- struct got_pack *pack;
- int fd;
-
- if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
- return got_error(GOT_ERR_OBJ_NOT_PACKED);
-
- fd = got_opentempfd();
- if (fd == -1)
- return got_error_from_errno();
-
- *f = fdopen(fd, "w+");
- if (*f == NULL) {
- err = got_error_from_errno();
- goto done;
- }
-
- pack = got_repo_get_cached_pack(repo, obj->path_packfile);
- if (pack == NULL) {
- err = got_repo_cache_pack(&pack, repo,
- obj->path_packfile, NULL);
- if (err)
- goto done;
- }
-
- err = got_packfile_extract_object(pack, obj, *f);
-done:
- if (err) {
- if (*f == NULL)
- close(fd);
- else
- fclose(*f);
- *f = NULL;
- }
- return err;
-}
-
const struct got_error *
got_object_blob_open(struct got_blob_object **blob,
struct got_repository *repo, struct got_object *obj, size_t blocksize)
{
const struct got_error *err = NULL;
+ int outfd;
+ size_t size;
+ struct stat sb;
if (obj->type != GOT_OBJ_TYPE_BLOB)
return got_error(GOT_ERR_OBJ_TYPE);
@@ -498,32 +460,36 @@ got_object_blob_open(struct got_blob_object **blob,
if (*blob == NULL)
return got_error_from_errno();
+ outfd = got_opentempfd();
+ if (outfd == -1)
+ return got_error_from_errno();
+
(*blob)->read_buf = malloc(blocksize);
if ((*blob)->read_buf == NULL) {
err = got_error_from_errno();
goto done;
}
if (obj->flags & GOT_OBJ_FLAG_PACKED) {
- err = extract_packed_object(&((*blob)->f), obj, repo);
+ struct got_pack *pack;
+ pack = got_repo_get_cached_pack(repo, obj->path_packfile);
+ if (pack == NULL) {
+ err = got_repo_cache_pack(&pack, repo,
+ obj->path_packfile, NULL);
+ if (err)
+ goto done;
+ }
+ err = got_object_read_packed_blob_privsep(&size, outfd,
+ obj, pack);
if (err)
goto done;
+ obj->size = size;
} else {
- int infd, outfd;
- size_t size;
- struct stat sb;
+ int infd;
err = open_loose_object(&infd, obj, repo);
if (err)
goto done;
-
- outfd = got_opentempfd();
- if (outfd == -1) {
- err = got_error_from_errno();
- close(infd);
- goto done;
- }
-
err = got_object_read_blob_privsep(&size, outfd, infd, repo);
close(infd);
if (err)
@@ -531,28 +497,25 @@ got_object_blob_open(struct got_blob_object **blob,
if (size != obj->hdrlen + obj->size) {
err = got_error(GOT_ERR_PRIVSEP_LEN);
- close(outfd);
goto done;
}
+ }
- if (fstat(outfd, &sb) == -1) {
- err = got_error_from_errno();
- close(outfd);
- 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);
- close(outfd);
- goto done;
- }
+ if (sb.st_size != obj->hdrlen + obj->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);
- goto done;
- }
+ (*blob)->f = fdopen(outfd, "rb");
+ if ((*blob)->f == NULL) {
+ err = got_error_from_errno();
+ close(outfd);
+ goto done;
}
(*blob)->hdrlen = obj->hdrlen;
@@ -560,12 +523,15 @@ got_object_blob_open(struct got_blob_object **blob,
memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
done:
- if (err && *blob) {
- if ((*blob)->f)
- fclose((*blob)->f);
- free((*blob)->read_buf);
- free(*blob);
- *blob = NULL;
+ if (err) {
+ if (*blob) {
+ if ((*blob)->f)
+ fclose((*blob)->f);
+ free((*blob)->read_buf);
+ free(*blob);
+ *blob = NULL;
+ } else if (outfd != -1)
+ close(outfd);
}
return err;
}
diff --git a/lib/object_parse.c b/lib/object_parse.c
index ae91d51..fc364fd 100644
--- a/lib/object_parse.c
+++ b/lib/object_parse.c
@@ -837,23 +837,58 @@ got_object_read_packed_tree_privsep(struct got_tree_object **tree,
return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
}
-static const struct got_error *
-request_blob(size_t *size, int outfd, int infd, struct got_repository *repo)
+const struct got_error *
+got_object_read_packed_blob_privsep(size_t *size, int outfd,
+ struct got_object *obj, struct got_pack *pack)
{
const struct got_error *err = NULL;
int outfd_child;
- struct imsgbuf *ibuf;
outfd_child = dup(outfd);
if (outfd_child == -1)
return got_error_from_errno();
- ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
+ err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
+ if (err)
+ return err;
+
+ err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
+ outfd_child);
+ if (err) {
+ close(outfd_child);
+ return err;
+ }
+
+ err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
+ if (err)
+ return err;
+
+ if (lseek(outfd, SEEK_SET, 0) == -1)
+ err = got_error_from_errno();
+
+ return err;
+}
- err = got_privsep_send_blob_req(ibuf, outfd_child, infd);
+static const struct got_error *
+request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
+{
+ const struct got_error *err = NULL;
+ int outfd_child;
+
+ outfd_child = dup(outfd);
+ if (outfd_child == -1)
+ return got_error_from_errno();
+
+ err = got_privsep_send_blob_req(ibuf, infd);
if (err)
return err;
+ err = got_privsep_send_blob_outfd(ibuf, outfd_child);
+ if (err) {
+ close(outfd_child);
+ return err;
+ }
+
err = got_privsep_recv_blob(size, ibuf);
if (err)
return err;
@@ -872,8 +907,10 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
pid_t pid;
struct imsgbuf *ibuf;
- if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1)
- return request_blob(size, outfd, infd, repo);
+ 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, outfd, infd, ibuf);
+ }
ibuf = calloc(1, sizeof(*ibuf));
if (ibuf == NULL)
@@ -898,5 +935,5 @@ got_object_read_blob_privsep(size_t *size, int outfd, int infd,
imsg_init(ibuf, imsg_fds[0]);
repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
- return request_blob(size, outfd, infd, repo);
+ return request_blob(size, outfd, infd, ibuf);
}
diff --git a/lib/privsep.c b/lib/privsep.c
index faa53d4..b69b1fe 100644
--- a/lib/privsep.c
+++ b/lib/privsep.c
@@ -269,7 +269,9 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
case GOT_OBJ_TYPE_COMMIT:
imsg_code = GOT_IMSG_COMMIT_REQUEST;
break;
- /* Blobs are handled in got_privsep_send_blob_req(). */
+ case GOT_OBJ_TYPE_BLOB:
+ imsg_code = GOT_IMSG_BLOB_REQUEST;
+ break;
default:
return got_error(GOT_ERR_OBJ_TYPE);
}
@@ -306,28 +308,21 @@ got_privsep_send_obj_req(struct imsgbuf *ibuf, int fd, struct got_object *obj)
}
const struct got_error *
-got_privsep_send_blob_req(struct imsgbuf *ibuf, int outfd, int infd)
+got_privsep_send_blob_req(struct imsgbuf *ibuf, int infd)
{
- const struct got_error *err = NULL;
-
if (imsg_compose(ibuf, GOT_IMSG_BLOB_REQUEST, 0, 0, infd, NULL, 0)
- == -1) {
- close(infd);
- close(outfd);
+ == -1)
return got_error_from_errno();
- }
- err = flush_imsg(ibuf);
- if (err) {
- close(outfd);
- return err;
- }
+ return flush_imsg(ibuf);
+}
+const struct got_error *
+got_privsep_send_blob_outfd(struct imsgbuf *ibuf, int outfd)
+{
if (imsg_compose(ibuf, GOT_IMSG_BLOB_OUTFD, 0, 0, outfd, NULL, 0)
- == -1) {
- close(outfd);
+ == -1)
return got_error_from_errno();
- }
return flush_imsg(ibuf);
}
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index eaf315f..24a3577 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -59,7 +59,9 @@ main(int argc, char *argv[])
FILE *f = NULL;
size_t size;
+ memset(&imsg, 0, sizeof(imsg));
imsg.fd = -1;
+ memset(&imsg_outfd, 0, sizeof(imsg_outfd));
imsg_outfd.fd = -1;
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 8110b5f..c6634c6 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -146,7 +146,70 @@ static const struct got_error *
blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx)
{
- return got_error(GOT_ERR_NOT_IMPL);
+ const struct got_error *err = NULL;
+ struct got_object *obj = NULL;
+ FILE *f = NULL;
+ struct imsg imsg_outfd;
+ size_t datalen;
+
+ memset(&imsg_outfd, 0, sizeof(imsg_outfd));
+ imsg_outfd.fd = -1;
+
+ err = got_privsep_get_imsg_obj(&obj, imsg, ibuf);
+ if (err)
+ return err;
+
+ if (obj->type != GOT_OBJ_TYPE_BLOB)
+ return got_error(GOT_ERR_OBJ_TYPE);
+
+ if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
+ return got_error(GOT_ERR_OBJ_NOT_PACKED);
+
+ err = got_privsep_recv_imsg(&imsg_outfd, ibuf, 0);
+ if (err)
+ return err;
+
+ if (imsg_outfd.hdr.type != GOT_IMSG_BLOB_OUTFD) {
+ err = got_error(GOT_ERR_PRIVSEP_MSG);
+ goto done;
+ }
+
+ datalen = imsg_outfd.hdr.len - IMSG_HEADER_SIZE;
+ if (datalen != 0) {
+ err = got_error(GOT_ERR_PRIVSEP_LEN);
+ goto done;
+ }
+ if (imsg_outfd.fd == -1) {
+ err = got_error(GOT_ERR_PRIVSEP_NO_FD);
+ goto done;
+ }
+
+ f = fdopen(imsg_outfd.fd, "w+");
+ if (f == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
+ err = got_packfile_extract_object(pack, obj, f);
+ if (err)
+ goto done;
+
+ err = got_privsep_send_blob(ibuf, obj->size);
+done:
+ if (f)
+ fclose(f);
+ else if (imsg_outfd.fd != -1)
+ close(imsg_outfd.fd);
+ imsg_free(&imsg_outfd);
+
+ if (err) {
+ if (err->code == GOT_ERR_PRIVSEP_PIPE)
+ err = NULL;
+ else
+ got_privsep_send_error(ibuf, err);
+ }
+
+ return err;
}
static const struct got_error *