In dump_delta_chain(), don't open the packfile if it's not needed.
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
diff --git a/lib/pack.c b/lib/pack.c
index 8d1a801..61c669a 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -1127,15 +1127,10 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
uint8_t *delta_buf = NULL;
size_t delta_len = 0;
- FILE *delta_file;
-
- delta_file = fopen(delta->path_packfile, "rb");
- if (delta_file == NULL) {
- err = got_error_from_errno();
- goto done;
- }
if (n == 0) {
+ FILE *delta_file;
+
/* Plain object types are the delta base. */
if (delta->type != GOT_OBJ_TYPE_COMMIT &&
delta->type != GOT_OBJ_TYPE_TREE &&
@@ -1145,6 +1140,12 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
goto done;
}
+ delta_file = fopen(delta->path_packfile, "rb");
+ if (delta_file == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
+
if (fseeko(delta_file, delta->offset + delta->tslen,
SEEK_SET) != 0) {
fclose(delta_file);
@@ -1164,6 +1165,11 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
get_cached_delta(&delta_buf, &delta_len, delta->data_offset,
path_packfile, repo);
if (delta_buf == NULL) {
+ FILE *delta_file = fopen(delta->path_packfile, "rb");
+ if (delta_file == NULL) {
+ err = got_error_from_errno();
+ goto done;
+ }
if (fseeko(delta_file, delta->data_offset, SEEK_CUR)
!= 0) {
fclose(delta_file);
@@ -1182,8 +1188,7 @@ dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile,
delta_len, path_packfile, repo);
if (err)
goto done;
- } else
- fclose(delta_file);
+ }
/* delta_buf is now cached */
err = got_delta_apply(base_file, delta_buf, delta_len,