extract objects smaller than DELTA_RESULT_SIZE_CACHED_MAX to memory
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
diff --git a/lib/pack.c b/lib/pack.c
index 74ddaf8..53496c9 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1344,12 +1344,6 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
return got_error(GOT_ERR_OBJ_NOT_PACKED);
- *f = got_opentemp();
- if (*f == NULL) {
- err = got_error(GOT_ERR_FILE_OPEN);
- goto done;
- }
-
pack = get_cached_pack(obj->path_packfile, repo);
if (pack == NULL) {
err = cache_pack(&pack, obj->path_packfile, NULL, repo);
@@ -1363,10 +1357,33 @@ got_packfile_extract_object(FILE **f, struct got_object *obj,
goto done;
}
+ if (obj->size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
+ *f = fmemopen(NULL, obj->size, "w+");
+ else
+ *f = got_opentemp();
+ if (*f == NULL) {
+ err = got_error(GOT_ERR_FILE_OPEN);
+ goto done;
+ }
err = got_inflate_to_file(&obj->size, pack->packfile, *f);
- } else
+ } else {
+ uint64_t max_size;
+
+ err = get_delta_chain_max_size(&max_size, &obj->deltas,
+ pack->packfile);
+ if (err)
+ return err;
+ if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
+ *f = fmemopen(NULL, max_size, "w+");
+ else
+ *f = got_opentemp();
+ if (*f == NULL) {
+ err = got_error(GOT_ERR_FILE_OPEN);
+ goto done;
+ }
err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
pack, repo);
+ }
done:
if (err && *f)
fclose(*f);