Commit 9db65d415a148f4ac83ff7a61809965bc4867f33

Stefan Sperling 2018-03-14T23:42:16

In dump_delta_chain(), don't open the packfile if it's not needed.

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,