Commit db59069162efd1ccd4f236abe7d8eb3ca4b791e4

Stefan Sperling 2021-06-02T09:24:51

make merge_file() accept FILEs instead of paths for orig and deriv inputs

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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
diff --git a/lib/buf.c b/lib/buf.c
index b26da2a..f17dddc 100644
--- a/lib/buf.c
+++ b/lib/buf.c
@@ -83,38 +83,34 @@ buf_alloc(BUF **b, size_t len)
  * Sets errno on error.
  */
 const struct got_error *
-buf_load(BUF **buf, const char *path)
+buf_load(BUF **buf, FILE *f)
 {
 	const struct got_error *err = NULL;
-	int fd;
-	ssize_t ret;
+	size_t ret;
 	size_t len;
 	u_char *bp;
 	struct stat st;
 
 	*buf = NULL;
 
-	fd = open(path, O_RDONLY, 0600);
-	if (fd == -1)
-		return got_error_from_errno2("open", path);
-
-	if (fstat(fd, &st) == -1) {
-		err = got_error_from_errno2("fstat", path);
+	if (fstat(fileno(f), &st) == -1) {
+		err = got_error_from_errno("fstat");
 		goto out;
 	}
 
 	if ((uintmax_t)st.st_size > SIZE_MAX) {
-		err = got_error_set_errno(EFBIG, path);
+		err = got_error_set_errno(EFBIG,
+		    "cannot fit file into memory buffer");
 		goto out;
 	}
 	err = buf_alloc(buf, st.st_size);
 	if (err)
 		goto out;
-	for (bp = (*buf)->cb_buf; ; bp += (size_t)ret) {
+	for (bp = (*buf)->cb_buf; ; bp += ret) {
 		len = SIZE_LEFT(*buf);
-		ret = read(fd, bp, len);
-		if (ret == -1) {
-			err = got_error_from_errno2("read", path);
+		ret = fread(bp, 1, len, f);
+		if (ret == 0 && ferror(f)) {
+			err = got_ferror(f, GOT_ERR_IO);
 			goto out;
 		} else if (ret == 0)
 			break;
@@ -123,8 +119,6 @@ buf_load(BUF **buf, const char *path)
 	}
 
 out:
-	if (close(fd) == -1 && err == NULL)
-		err = got_error_from_errno2("close", path);
 	if (err) {
 		buf_free(*buf);
 		*buf = NULL;
diff --git a/lib/buf.h b/lib/buf.h
index 9422c0f..354d7c4 100644
--- a/lib/buf.h
+++ b/lib/buf.h
@@ -50,7 +50,7 @@ struct buf {
 };
 
 const struct got_error *buf_alloc(BUF **, size_t);
-const struct got_error *buf_load(BUF **, const char *);
+const struct got_error *buf_load(BUF **, FILE *);
 void		 buf_free(BUF *);
 void		*buf_release(BUF *);
 u_char		 buf_getc(BUF *, size_t);
diff --git a/lib/diff3.c b/lib/diff3.c
index f9cd087..b8c328a 100644
--- a/lib/diff3.c
+++ b/lib/diff3.c
@@ -236,8 +236,12 @@ diffreg(BUF **d, const char *path1, const char *path2)
 		err = got_error_from_errno2("fflush", outpath);
 		goto done;
 	}
+	if (fseek(outfile, 0L, SEEK_SET) == -1) {
+		err = got_ferror(outfile, GOT_ERR_IO);
+		goto done;
+	}
 
-	err = buf_load(d, outpath);
+	err = buf_load(d, outfile);
 done:
 	if (outpath) {
 		if (unlink(outpath) == -1 && err == NULL)
@@ -257,8 +261,8 @@ done:
  * For merge(1).
  */
 const struct got_error *
-got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
-    const char *p3, const char *label1, const char *label2, const char *label3)
+got_merge_diff3(int *overlapcnt, int outfd, FILE *f1, FILE *f2,
+    FILE *f3, const char *label1, const char *label2, const char *label3)
 {
 	const struct got_error *err = NULL;
 	char *dp13, *dp23, *path1, *path2, *path3;
@@ -277,13 +281,13 @@ got_merge_diff3(int *overlapcnt, int outfd, const char *p1, const char *p2,
 	dp13 = dp23 = path1 = path2 = path3 = NULL;
 	data = patch = NULL;
 
-	err = buf_load(&b1, p1);
+	err = buf_load(&b1, f1);
 	if (err)
 		goto out;
-	err = buf_load(&b2, p2);
+	err = buf_load(&b2, f2);
 	if (err)
 		goto out;
-	err = buf_load(&b3, p3);
+	err = buf_load(&b3, f3);
 	if (err)
 		goto out;
 
diff --git a/lib/got_lib_diff.h b/lib/got_lib_diff.h
index ae0e6d7..c056426 100644
--- a/lib/got_lib_diff.h
+++ b/lib/got_lib_diff.h
@@ -62,8 +62,8 @@ const struct got_error *got_diffreg_result_free_right(
 const struct got_error *got_diffreg_close(FILE *, char *, size_t,
     FILE *, char *, size_t);
 
-const struct got_error *got_merge_diff3(int *, int, const char *, const char *,
-    const char *, const char *, const char *, const char *);
+const struct got_error *got_merge_diff3(int *, int, FILE *, FILE *, FILE *,
+    const char *, const char *, const char *);
 
 const struct got_error *got_diff_files(struct got_diffreg_result **, FILE *,
     const char *, FILE *, const char *, int, int, int, FILE *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 327fa4b..016b6dc 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -728,25 +728,19 @@ check_file_contents_equal(int *same, FILE *f1, FILE *f2)
 }
 
 static const struct got_error *
-check_files_equal(int *same, const char *f1_path, const char *f2_path)
+check_files_equal(int *same, FILE *f1, FILE *f2)
 {
-	const struct got_error *err = NULL;
 	struct stat sb;
 	size_t size1, size2;
-	FILE *f1 = NULL, *f2 = NULL;
 
 	*same = 1;
 
-	if (lstat(f1_path, &sb) != 0) {
-		err = got_error_from_errno2("lstat", f1_path);
-		goto done;
-	}
+	if (fstat(fileno(f1), &sb) != 0)
+		return got_error_from_errno("fstat");
 	size1 = sb.st_size;
 
-	if (lstat(f2_path, &sb) != 0) {
-		err = got_error_from_errno2("lstat", f2_path);
-		goto done;
-	}
+	if (fstat(fileno(f2), &sb) != 0)
+		return got_error_from_errno("fstat");
 	size2 = sb.st_size;
 
 	if (size1 != size2) {
@@ -754,48 +748,35 @@ check_files_equal(int *same, const char *f1_path, const char *f2_path)
 		return NULL;
 	}
 
-	f1 = fopen(f1_path, "r");
-	if (f1 == NULL)
-		return got_error_from_errno2("fopen", f1_path);
-
-	f2 = fopen(f2_path, "r");
-	if (f2 == NULL) {
-		err = got_error_from_errno2("fopen", f2_path);
-		goto done;
-	}
-
-	err = check_file_contents_equal(same, f1, f2);
-done:
-	if (f1 && fclose(f1) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
-	if (f2 && fclose(f2) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
+	if (fseek(f1, 0L, SEEK_SET) == -1)
+		return got_ferror(f1, GOT_ERR_IO);
+	if (fseek(f2, 0L, SEEK_SET) == -1)
+		return got_ferror(f2, GOT_ERR_IO);
 
-	return err;
+	return check_file_contents_equal(same, f1, f2);
 }
 
 /*
- * Perform a 3-way merge where the file at orig_path acts as the common
- * ancestor, the file at deriv_path acts as the first derived version,
- * and the file at ondisk_path acts as the second derived version.
+ * Perform a 3-way merge where the file f_orig acts as the common
+ * ancestor, the file f_deriv acts as the first derived version,
+ * and the file at ondisk_path acts as both the target and the
+ * second derived version
  */
 static const struct got_error *
 merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
-    const char *orig_path, const char *ondisk_path,
-    const char *path, uint16_t st_mode, const char *deriv_path,
+    FILE *f_orig, const char *ondisk_path,
+    const char *path, uint16_t st_mode, FILE *f_deriv,
     const char *label_orig, const char *label_deriv,
     struct got_repository *repo,
     got_worktree_checkout_cb progress_cb, void *progress_arg)
 {
 	const struct got_error *err = NULL;
 	int merged_fd = -1;
-	FILE *f_empty = NULL;
-	char *f_empty_path = NULL;
+	FILE *f_merged = NULL;
 	char *merged_path = NULL, *base_path = NULL;
 	int overlapcnt = 0;
 	char *parent = NULL;
-	char *symlink_path = NULL;
-	FILE *symlinkf = NULL;
+	FILE *f = NULL;
 
 	*local_changes_subsumed = 0;
 
@@ -812,25 +793,8 @@ merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
 	if (err)
 		goto done;
 
-	free(base_path);
-	if (asprintf(&base_path, "%s/got-merge-blob-orig", parent) == -1) {
-		err = got_error_from_errno("asprintf");
-		base_path = NULL;
-		goto done;
-	}
-
-	if (orig_path == NULL) {
-		/*
-		 * If the file has no blob, this is an "add vs add" conflict,
-		 * and we simply use an empty ancestor file to make both files
-		 * appear in the merged result in their entirety.
-		 */
-		err = got_opentemp_named(&f_empty_path, &f_empty, base_path);
-		if (err)
-			goto done;
-	}
-
 	/* 
+	 * Open the merge target file.
 	 * In order the run a 3-way merge with a symlink we copy the symlink's
 	 * target path into a temporary file and use that file with diff3.
 	 */
@@ -846,29 +810,46 @@ merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
 			base_path = NULL;
 			goto done;
 		}
-		err = got_opentemp_named(&symlink_path, &symlinkf, base_path);
-		if (err)
+		f = got_opentemp();
+		if (f == NULL) {
+			err = got_error_from_errno("got_opentemp");
 			goto done;
+		}
 		target_len = readlink(ondisk_path, target_path,
 		    sizeof(target_path));
 		if (target_len == -1) {
 			err = got_error_from_errno2("readlink", ondisk_path);
 			goto done;
 		}
-		n = fwrite(target_path, 1, target_len, symlinkf);
+		n = fwrite(target_path, 1, target_len, f);
 		if (n != target_len) {
-			err = got_ferror(symlinkf, GOT_ERR_IO);
+			err = got_ferror(f, GOT_ERR_IO);
+			goto done;
+		}
+		if (fflush(f) == EOF) {
+			err = got_error_from_errno("fflush");
+			goto done;
+		}
+		if (fseek(f, 0L, SEEK_SET) == -1) {
+			err = got_ferror(f, GOT_ERR_IO);
+			goto done;
+		}
+	} else {
+		int fd;
+		fd = open(ondisk_path, O_RDONLY | O_NOFOLLOW);
+		if (fd == -1) {
+			err = got_error_from_errno2("open", ondisk_path);
 			goto done;
 		}
-		if (fflush(symlinkf) == EOF) {
-			err = got_error_from_errno2("fflush", symlink_path);
+		f = fdopen(fd, "r");
+		if (f == NULL) {
+			err = got_error_from_errno2("fdopen", ondisk_path);
+			close(fd);
 			goto done;
 		}
 	}
 
-	err = got_merge_diff3(&overlapcnt, merged_fd, deriv_path,
-	    orig_path ? orig_path : f_empty_path,
-	    symlink_path ? symlink_path : ondisk_path,
+	err = got_merge_diff3(&overlapcnt, merged_fd, f_deriv, f_orig, f,
 	    label_deriv, label_orig, NULL);
 	if (err)
 		goto done;
@@ -883,15 +864,22 @@ merge_file(int *local_changes_subsumed, struct got_worktree *worktree,
 		goto done;
 	}
 
+	f_merged = fdopen(merged_fd, "r");
+	if (f_merged == NULL) {
+		err = got_error_from_errno("fdopen");
+		goto done;
+	}
+	merged_fd = -1;
+
 	/* Check if a clean merge has subsumed all local changes. */
 	if (overlapcnt == 0) {
-		err = check_files_equal(local_changes_subsumed, deriv_path,
-		    merged_path);
+		err = check_files_equal(local_changes_subsumed, f_deriv,
+		    f_merged);
 		if (err)
 			goto done;
 	}
 
-	if (fchmod(merged_fd, st_mode) != 0) {
+	if (fchmod(fileno(f_merged), st_mode) != 0) {
 		err = got_error_from_errno2("fchmod", merged_path);
 		goto done;
 	}
@@ -906,24 +894,14 @@ done:
 		if (merged_path)
 			unlink(merged_path);
 	}
-	if (symlink_path) {
-		if (unlink(symlink_path) == -1 && err == NULL)
-			err = got_error_from_errno2("unlink", symlink_path);
-	}
-	if (symlinkf && fclose(symlinkf) == EOF && err == NULL)
-		err = got_error_from_errno2("fclose", symlink_path);
-	free(symlink_path);
+	if (f && fclose(f) == EOF && err == NULL)
+		err = got_error_from_errno("fclose");
 	if (merged_fd != -1 && close(merged_fd) == -1 && err == NULL)
 		err = got_error_from_errno("close");
-	if (f_empty && fclose(f_empty) == EOF && err == NULL)
+	if (f_merged && fclose(f_merged) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	free(merged_path);
 	free(base_path);
-	if (f_empty_path) {
-		if (unlink(f_empty_path) == -1 && err == NULL)
-			err = got_error_from_errno2("unlink", f_empty_path);
-		free(f_empty_path);
-	}
 	free(parent);
 	return err;
 }
@@ -1157,8 +1135,18 @@ merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
 		    blob_orig);
 		if (err)
 			goto done;
-
 		free(base_path);
+	} else {
+		/*
+		 * No common ancestor exists. This is an "add vs add" conflict
+		 * and we simply use an empty ancestor file to make both files
+		 * appear in the merged result in their entirety.
+		 */
+		f_orig = got_opentemp();
+		if (f_orig == NULL) {
+			err = got_error_from_errno("got_opentemp");
+			goto done;
+		}
 	}
 
 	if (asprintf(&base_path, "%s/got-merge-blob-deriv", parent) == -1) {
@@ -1184,9 +1172,9 @@ merge_blob(int *local_changes_subsumed, struct got_worktree *worktree,
 		goto done;
 	}
 
-	err = merge_file(local_changes_subsumed, worktree, blob_orig_path,
-	    ondisk_path, path, st_mode, blob_deriv_path, label_orig,
-	    label_deriv, repo, progress_cb, progress_arg);
+	err = merge_file(local_changes_subsumed, worktree, f_orig,
+	    ondisk_path, path, st_mode, f_deriv, label_orig, label_deriv,
+	    repo, progress_cb, progress_arg);
 done:
 	if (f_orig && fclose(f_orig) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
@@ -7800,9 +7788,9 @@ unstage_hunks(struct got_object_id *staged_blob_id,
 			goto done;
 
 		err = merge_file(&local_changes_subsumed, worktree,
-		    blob_base_path, ondisk_path, ie->path,
+		    f_base, ondisk_path, ie->path,
 		    got_fileindex_perms_to_st(ie),
-		    path_unstaged_content, label_orig, "unstaged",
+		    f, label_orig, "unstaged",
 		    repo, progress_cb, progress_arg);
 	}
 	if (err)