simply got_diff_prepare_file() by letting callers worry about file creation
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
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);