reuse temporary files which were not used by got_object_raw_open()
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
diff --git a/lib/got_lib_object.h b/lib/got_lib_object.h
index f107a85..3857f54 100644
--- a/lib/got_lib_object.h
+++ b/lib/got_lib_object.h
@@ -106,7 +106,7 @@ const struct got_error *got_object_read_header_privsep(struct got_object **,
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 *);
diff --git a/lib/object.c b/lib/object.c
index b9d3ec4..5c98d7f 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -529,24 +529,19 @@ got_object_open(struct got_object **obj, struct got_repository *repo,
}
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, struct got_repository *repo,
}
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, struct got_repository *repo,
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;
diff --git a/lib/pack_create.c b/lib/pack_create.c
index dce64b2..8ab75df 100644
--- a/lib/pack_create.c
+++ b/lib/pack_create.c
@@ -173,6 +173,7 @@ pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
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, int nours,
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, int nours,
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;
}