commit cc7a354afc4ae05de465ac2cf97b558fec5b271f from: Stefan Sperling date: Fri Oct 15 07:15:00 2021 UTC reuse temporary files which were not used by got_object_raw_open() commit - f8eebdd4e657c910bdc7d7d8777256f59ddb6465 commit + cc7a354afc4ae05de465ac2cf97b558fec5b271f blob - f107a850167bfb93f97e10313c9351627cd8eb68 blob + 3857f54da2b2d09d1775f659e036b7fa820b9b3b --- lib/got_lib_object.h +++ lib/got_lib_object.h @@ -106,7 +106,7 @@ const struct got_error *got_object_read_header_privsep struct got_object_id *, struct got_repository *, int); const struct got_error *got_object_open(struct got_object **, struct got_repository *, struct got_object_id *); -const struct got_error *got_object_raw_open(struct got_raw_object **, +const struct got_error *got_object_raw_open(struct got_raw_object **, int, struct got_repository *, struct got_object_id *, size_t); void got_object_raw_rewind(struct got_raw_object *); size_t got_object_raw_get_hdrlen(struct got_raw_object *); blob - b9d3ec4605ca7d5c2d7b59cde3def136e77c3adc blob + 5c98d7f542c6cc60b245e230796391a272d5a5ad --- lib/object.c +++ lib/object.c @@ -529,24 +529,19 @@ got_object_open(struct got_object **obj, struct got_re } const struct got_error * -got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo, - struct got_object_id *id, size_t blocksize) +got_object_raw_open(struct got_raw_object **obj, int outfd, + struct got_repository *repo, struct got_object_id *id, size_t blocksize) { const struct got_error *err = NULL; struct got_packidx *packidx = NULL; int idx; uint8_t *outbuf = NULL; - int outfd = -1; off_t size = 0; size_t hdrlen = 0; char *path_packfile = NULL; *obj = NULL; - outfd = got_opentempfd(); - if (outfd == -1) - return got_error_from_errno("got_opentempfd"); - err = got_repo_search_packidx(&packidx, &idx, repo, id); if (err == NULL) { struct got_pack *pack = NULL; @@ -592,11 +587,6 @@ got_object_raw_open(struct got_raw_object **obj, struc } if (outbuf) { - if (close(outfd) == -1) { - err = got_error_from_errno("close"); - goto done; - } - outfd = -1; (*obj)->f = fmemopen(outbuf, hdrlen + size, "r"); if ((*obj)->f == NULL) { err = got_error_from_errno("fdopen"); @@ -620,7 +610,6 @@ got_object_raw_open(struct got_raw_object **obj, struc err = got_error_from_errno("fdopen"); goto done; } - outfd = -1; (*obj)->data = NULL; } (*obj)->hdrlen = hdrlen; @@ -633,8 +622,6 @@ done: got_object_raw_close(*obj); *obj = NULL; } - if (outfd != -1) - close(outfd); free(outbuf); } return err; blob - dce64b25f35227709c626d4d561698c07375c735 blob + 8ab75df8dd3ab328895f4354591fe0d968fe613f --- lib/pack_create.c +++ lib/pack_create.c @@ -173,6 +173,7 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, in struct got_delta_instruction *deltas; int i, j, size, ndeltas, best; const int max_base_candidates = 10; + int outfd = -1; qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp); for (i = 0; i < nmeta; i++) { @@ -194,9 +195,18 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, in m->obj_type == GOT_OBJ_TYPE_TAG) continue; - err = got_object_raw_open(&raw, repo, &m->id, 8192); + if (outfd == -1) { + outfd = got_opentempfd(); + if (outfd == -1) { + err = got_error_from_errno("got_opentempfd"); + goto done; + } + } + err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192); if (err) goto done; + if (raw->data == NULL) + outfd = -1; /* outfd is now raw->f */ m->size = raw->size; err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen, @@ -224,10 +234,20 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, in base->obj_type != m->obj_type) continue; - err = got_object_raw_open(&base_raw, repo, &base->id, - 8192); + if (outfd == -1) { + outfd = got_opentempfd(); + if (outfd == -1) { + err = got_error_from_errno( + "got_opentempfd"); + goto done; + } + } + err = got_object_raw_open(&base_raw, outfd, repo, + &base->id, 8192); if (err) goto done; + if (base_raw->data == NULL) + outfd = -1; /* outfd is now base_raw->f */ err = got_deltify(&deltas, &ndeltas, raw->f, raw->hdrlen, raw->size + raw->hdrlen, base->dtab, base_raw->f, base_raw->hdrlen, @@ -271,6 +291,8 @@ done: got_object_raw_close(raw); if (base_raw) got_object_raw_close(base_raw); + if (outfd != -1 && close(outfd) == -1 && err == NULL) + err = got_error_from_errno("close"); return err; } @@ -1129,6 +1151,7 @@ genpack(uint8_t *pack_sha1, FILE *packfile, size_t outlen, n; struct got_deflate_checksum csum; off_t packfile_size = 0; + int outfd = -1; SHA1Init(&ctx); csum.output_sha1 = &ctx; @@ -1155,9 +1178,18 @@ genpack(uint8_t *pack_sha1, FILE *packfile, } m = meta[i]; m->off = ftello(packfile); - err = got_object_raw_open(&raw, repo, &m->id, 8192); + if (outfd == -1) { + outfd = got_opentempfd(); + if (outfd == -1) { + err = got_error_from_errno("got_opentempfd"); + goto done; + } + } + err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192); if (err) goto done; + if (raw->data == NULL) + outfd = -1; /* outfd is now raw->f */ if (m->deltas == NULL) { err = packhdr(&nh, buf, sizeof(buf), m->obj_type, raw->size); @@ -1249,6 +1281,8 @@ done: err = got_error_from_errno("fclose"); if (raw) got_object_raw_close(raw); + if (outfd != -1 && close(outfd) == -1 && err == NULL) + err = got_error_from_errno("close"); return err; }