Commit 72254787333eeb9d226c5341bbfee1b48c1c0f30

Stefan Sperling 2020-11-18T16:12:35

simply got_diff_prepare_file() by letting callers worry about file creation

diff --git a/lib/diff.c b/lib/diff.c
index 8e431b8..845c023 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -311,10 +311,16 @@ got_diff_blob_prepared_file(struct got_diffreg_result **resultp,
 			goto done;
 	} else {
 		idstr1 = "/dev/null";
+		f1_created = 1;
+		f1 = got_opentemp();
+		if (f1 == NULL) {
+			err = got_error_from_errno("got_opentemp");
+			goto done;
+		}
 	}
 
-	err = got_diff_prepare_file(&f1, &p1, &f1_created, &size,
-	    data1, cfg, ignore_whitespace);
+	err = got_diff_prepare_file(f1, &p1, &size, data1, cfg,
+	    ignore_whitespace);
 	if (err)
 		goto done;
 
diff --git a/lib/diffreg.c b/lib/diffreg.c
index e28256d..f986eb1 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -118,7 +118,7 @@ got_diff_get_config(enum got_diff_algorithm algorithm)
 }
 
 const struct got_error *
-got_diff_prepare_file(FILE **f, char **p, int *f_created, size_t *size,
+got_diff_prepare_file(FILE *f, char **p, size_t *size,
     struct diff_data *diff_data, const struct diff_config *cfg,
     int ignore_whitespace)
 {
@@ -132,28 +132,18 @@ got_diff_prepare_file(FILE **f, char **p, int *f_created, size_t *size,
 	if (ignore_whitespace)
 		diff_flags |= DIFF_FLAG_IGNORE_WHITESPACE;
 
-	if (f && *f) {
-		if (fstat(fileno(*f), &st) == -1) {
-			err = got_error_from_errno("fstat");
-			goto done;
-		}
-	#ifndef GOT_DIFF_NO_MMAP
-		*p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
-		    fileno(*f), 0);
-		if (*p == MAP_FAILED)
-	#endif
-			*p = NULL; /* fall back on file I/O */
-	} else {
-		*f_created = 1;
-		st.st_size = 0;
-		*f = got_opentemp();
-		if (*f == NULL) {
-			err = got_error_from_errno("got_opentemp");
-			goto done;
-		}
+	if (fstat(fileno(f), &st) == -1) {
+		err = got_error_from_errno("fstat");
+		goto done;
 	}
-
-	rc = diff_atomize_file(diff_data, cfg, *f, *p, st.st_size, diff_flags);
+#ifndef GOT_DIFF_NO_MMAP
+	*p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE,
+	    fileno(f), 0);
+	if (*p == MAP_FAILED)
+#endif
+		*p = NULL; /* fall back on file I/O */
+
+	rc = diff_atomize_file(diff_data, cfg, f, *p, st.st_size, diff_flags);
 	if (rc) {
 		err = got_error_set_errno(rc, "diff_atomize_file");
 		goto done;
@@ -232,20 +222,37 @@ got_diffreg(struct got_diffreg_result **diffreg_result, FILE *f1, FILE *f2,
 		left = &d_left;
 		right = &d_right;
 	}
-	
+
 	cfg = got_diff_get_config(algorithm);
 	if (cfg == NULL) {
 		err = got_error(GOT_ERR_NOT_IMPL);
 		goto done;
 	}
 
-	err = got_diff_prepare_file(&f1, &p1, &f1_created, &size1,
-	    left, cfg, ignore_whitespace);
+	if (f1 == NULL) {
+		f1_created = 1;
+		f1 = got_opentemp();
+		if (f1 == NULL) {
+			err = got_error_from_errno("got_opentemp");
+			goto done;
+		}
+	}
+	if (f2 == NULL) {
+		f2_created = 1;
+		f2 = got_opentemp();
+		if (f2 == NULL) {
+			err = got_error_from_errno("got_opentemp");
+			goto done;
+		}
+	}
+
+	err = got_diff_prepare_file(f1, &p1, &size1, left, cfg,
+	    ignore_whitespace);
 	if (err)
 		goto done;
 
-	err = got_diff_prepare_file(&f2, &p2, &f2_created, &size2,
-	    right, cfg, ignore_whitespace);
+	err = got_diff_prepare_file(f2, &p2, &size2, right, cfg,
+	    ignore_whitespace);
 	if (err)
 		goto done;
 
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index 6a88397..f78de6a 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -46,8 +46,8 @@ struct got_diffreg_result {
 #define GOT_DIFF_CONFLICT_MARKER_END	">>>>>>>"
 
 const struct diff_config *got_diff_get_config(enum got_diff_algorithm);
-const struct got_error *got_diff_prepare_file(FILE **, char **, int *,
-    size_t *, struct diff_data *, const struct diff_config *, int); 
+const struct got_error *got_diff_prepare_file(FILE *, char **, size_t *,
+    struct diff_data *, const struct diff_config *, int); 
 const struct got_error *got_diffreg_prepared_files(struct got_diffreg_result **,
     const struct diff_config *, struct diff_data *, FILE *, char *, size_t,
     struct diff_data *, FILE *, char *, size_t);