Commit f9d376997dd2e84bf84c3c9a4da842d1b5565e9d

Stefan Sperling 2022-06-28T20:33:41

move got_opentempfd() out of lib/diff.c again ok tracey

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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
diff --git a/got/got.c b/got/got.c
index 69e5ee6..6332563 100644
--- a/got/got.c
+++ b/got/got.c
@@ -3609,6 +3609,7 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 	struct got_tree_object *tree1 = NULL, *tree2 = NULL;
 	struct got_diff_blob_output_unidiff_arg arg;
 	FILE *f1 = NULL, *f2 = NULL;
+	int fd1 = -1, fd2 = -1;
 
 	if (tree_id1) {
 		err = got_object_open_as_tree(&tree1, repo, tree_id1);
@@ -3619,6 +3620,12 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 			err = got_error_from_errno("got_opentemp");
 			goto done;
 		}
+
+		fd1 = got_opentempfd();
+		if (fd1 == -1) {
+			err = got_error_from_errno("got_opentempfd");
+			goto done;
+		}
 	}
 
 	err = got_object_open_as_tree(&tree2, repo, tree_id2);
@@ -3630,7 +3637,11 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 		err = got_error_from_errno("got_opentemp");
 		goto done;
 	}
-
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
 	arg.diff_context = diff_context;
 	arg.ignore_whitespace = ignore_whitespace;
 	arg.force_text_diff = force_text_diff;
@@ -3639,7 +3650,7 @@ diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
 	arg.nlines = 0;
 	while (path[0] == '/')
 		path++;
-	err = got_diff_tree(tree1, tree2, f1, f2, path, path, repo,
+	err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
 	    got_diff_blob_output_unidiff, &arg, 1);
 done:
 	if (tree1)
@@ -3650,6 +3661,10 @@ done:
 		err = got_error_from_errno("fclose");
 	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
+	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
+		err = got_error_from_errno("close");
 	return err;
 }
 
@@ -3691,7 +3706,7 @@ get_changed_paths(struct got_pathlist_head *paths,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
+	err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
 	    got_diff_tree_collect_changed_paths, paths, 0);
 done:
 	if (tree1)
@@ -4634,7 +4649,7 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 	struct print_diff_arg *a = arg;
 	const struct got_error *err = NULL;
 	struct got_blob_object *blob1 = NULL;
-	int fd = -1, fd1 = -1;
+	int fd = -1, fd1 = -1, fd2 = -1;
 	FILE *f1 = NULL, *f2 = NULL;
 	char *abspath = NULL, *label1 = NULL;
 	struct stat sb;
@@ -4693,7 +4708,17 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 			err = got_error_from_errno("got_opentemp");
 			goto done;
 		}
-		err = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
+		fd1 = got_opentempfd();
+		if (fd1 == -1) {
+			err = got_error_from_errno("got_opentempfd");
+			goto done;
+		}
+		fd2 = got_opentempfd();
+		if (fd2 == -1) {
+			err = got_error_from_errno("got_opentempfd");
+			goto done;
+		}
+		err = got_diff_objects_as_blobs(NULL, NULL, f1, f2, fd1, fd2,
 		    blob_id, staged_blob_id, label1, label2, a->diff_context,
 		    a->ignore_whitespace, a->force_text_diff, a->repo, stdout);
 		goto done;
@@ -4794,6 +4819,8 @@ print_diff(void *arg, unsigned char status, unsigned char staged_status,
 done:
 	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
 		err = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
+		err = got_error_from_errno("close");
 	if (blob1)
 		got_object_blob_close(blob1);
 	if (f1 && fclose(f1) == EOF && err == NULL)
@@ -4825,6 +4852,7 @@ cmd_diff(int argc, char *argv[])
 	struct got_pathlist_head paths;
 	struct got_pathlist_entry *pe;
 	FILE *f1 = NULL, *f2 = NULL;
+	int fd1 = -1, fd2 = -1;
 	int *pack_fds = NULL;
 
 	TAILQ_INIT(&refs);
@@ -5134,22 +5162,34 @@ cmd_diff(int argc, char *argv[])
 		goto done;
 	}
 
+	fd1 = got_opentempfd();
+	if (fd1 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
 	switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
 	case GOT_OBJ_TYPE_BLOB:
 		error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
-		    ids[0], ids[1], NULL, NULL, diff_context,
+		    fd1, fd2, ids[0], ids[1], NULL, NULL, diff_context,
 		    ignore_whitespace, force_text_diff, repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_TREE:
-		error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
+		error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
 		    ids[0], ids[1], &paths, "", "", diff_context,
 		    ignore_whitespace, force_text_diff, repo, stdout);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
 		printf("diff %s %s\n", labels[0], labels[1]);
 		error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
-		    ids[0], ids[1], &paths, diff_context, ignore_whitespace,
-		    force_text_diff, repo, stdout);
+		    fd1, fd2, ids[0], ids[1], &paths, diff_context,
+		    ignore_whitespace, force_text_diff, repo, stdout);
 		break;
 	default:
 		error = got_error(GOT_ERR_OBJ_TYPE);
@@ -5180,6 +5220,10 @@ done:
 		error = got_error_from_errno("fclose");
 	if (f2 && fclose(f2) == EOF && error == NULL)
 		error = got_error_from_errno("fclose");
+	if (fd1 != -1 && close(fd1) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && error == NULL)
+		error = got_error_from_errno("close");
 	return error;
 }
 
@@ -9393,7 +9437,7 @@ check_path_prefix(struct got_object_id *parent_id,
 		cpp_arg.path_prefix++;
 	cpp_arg.len = strlen(cpp_arg.path_prefix);
 	cpp_arg.errcode = errcode;
-	err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
+	err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
 	    check_path_prefix_in_diff, &cpp_arg, 0);
 done:
 	if (tree1)
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index bfc88e3..eb1c8f8 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -2855,6 +2855,7 @@ gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
 {
 	const struct got_error *error;
 	FILE *f = NULL, *f1 = NULL, *f2 = NULL;
+	int fd1 = -1, fd2 = -1;
 	struct got_object_id *id1 = NULL, *id2 = NULL;
 	char *label1 = NULL, *label2 = NULL, *line = NULL;
 	int obj_type;
@@ -2878,6 +2879,18 @@ gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
 		goto done;
 	}
 
+	fd1 = got_opentempfd();
+	if (fd1 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
 	if (header->parent_id != NULL &&
 	    strncmp(header->parent_id, "/dev/null", 9) != 0) {
 		error = got_repo_match_object_id(&id1, &label1,
@@ -2899,15 +2912,17 @@ gw_output_diff(struct gw_trans *gw_trans, struct gw_header *header)
 	switch (obj_type) {
 	case GOT_OBJ_TYPE_BLOB:
 		error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
-		    id1, id2, NULL, NULL, 3, 0, 0, gw_trans->repo, f);
+		    fd1, fd2, id1, id2, NULL, NULL, 3, 0, 0,
+		    gw_trans->repo, f);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		error = got_diff_objects_as_trees(NULL, NULL, f1, f2,
-		    id1, id2, NULL, "", "", 3, 0, 0, gw_trans->repo, f);
+		    fd1, fd2, id1, id2, NULL, "", "", 3, 0, 0,
+		    gw_trans->repo, f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT:
 		error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
-		    id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
+		    fd1, fd2, id1, id2, NULL, 3, 0, 0, gw_trans->repo, f);
 		break;
 	default:
 		error = got_error(GOT_ERR_OBJ_TYPE);
@@ -2941,6 +2956,10 @@ done:
 		error = got_error_from_errno("fclose");
 	if (f2 && fclose(f2) == EOF && error == NULL)
 		error = got_error_from_errno("fclose");
+	if (fd1 != -1 && close(fd1) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && error == NULL)
+		error = got_error_from_errno("close");
 	free(line);
 	free(label1);
 	free(label2);
diff --git a/include/got_diff.h b/include/got_diff.h
index 4f53711..c587513 100644
--- a/include/got_diff.h
+++ b/include/got_diff.h
@@ -102,14 +102,16 @@ const struct got_error *got_diff_blob_output_unidiff(void *,
  * Diffing of blob content can be suppressed by passing zero for the
  * 'diff_content' parameter. The callback will then only receive blob
  * object IDs and diff labels, but NULL pointers instead of blob objects.
- * If 'diff_content' is set, two open temporary files must be provided
- * for internal use; these files can be obtained from got_opentemp()
+ * If 'diff_content' is set, two open temporary FILEs and two open
+ * temporary file descriptors must be provided for internal use; these
+ * files can be obtained from got_opentemp() and got_opentempfd(),
  * and must be closed by the caller. Otherwise the files can be NULL.
  * The set of arguments relating to either tree may be NULL to indicate
  * that no content is present on its respective side of the diff.
  */
 const struct got_error *got_diff_tree(struct got_tree_object *,
-    struct got_tree_object *, FILE *, FILE *, const char *, const char *,
+    struct got_tree_object *, FILE *, FILE *, int, int,
+    const char *, const char *,
     struct got_repository *, got_diff_blob_cb cb, void *cb_arg, int);
 
 /*
@@ -137,9 +139,10 @@ const struct got_error *got_diff_tree_collect_changed_paths(void *,
  * Diff two objects, assuming both objects are blobs. Two const char * diff
  * header labels may be provided which will be used to identify each blob in
  * the diff output. If a label is NULL, use the blob's SHA1 checksum instead.
- * Two open temporary files must be provided for internal use; these files
- * can be obtained from got_opentemp() and must be closed by the caller.
- * The set of arguments relating to either blob may be NULL to indicate
+ * Two open temporary files and two temporary file descriptors must be
+ * provided for internal use; these files can be obtained from
+ * got_opentemp() and got_opentempfd(), and must be closed by the caller.
+ * The set of arguments relating to either blob may be NULL/-1 to indicate
  * that no content is present on its respective side of the diff.
  * The number of context lines to show in the diff must be specified as well.
  * Write unified diff text to the provided output FILE.
@@ -147,7 +150,7 @@ const struct got_error *got_diff_tree_collect_changed_paths(void *,
  * array of line offsets for, and the number of lines in, the unidiff text.
  */
 const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
-    FILE *, FILE *, struct got_object_id *, struct got_object_id *,
+    FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
     const char *, const char *, int, int, int,
     struct got_repository *, FILE *);
 
@@ -156,8 +159,10 @@ const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
  * header labels may be provided which will be used to identify each blob in
  * the trees. If a label is NULL, use the blob's SHA1 checksum instead.
  * The number of context lines to show in diffs must be specified.
- * Two open temporary files must be provided for internal use; these files
- * can be obtained from got_opentemp() and must be closed by the caller.
+ * Two open temporary files and two temporary file descriptors must be
+ * provided for internal use; these files can be obtained from
+ * got_opentemp() and got_opentempfd(), and must be closed by the caller.
+ * If 'diff_content' is not set, the files may be NULL / -1.
  * The set of arguments relating to either tree may be NULL to indicate
  * that no content is present on its respective side of the diff.
  * Write unified diff text to the provided output FILE.
@@ -165,15 +170,16 @@ const struct got_error *got_diff_objects_as_blobs(off_t **, size_t *,
  * array of line offsets for, and the number of lines in, the unidiff text.
  */
 const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
-    FILE *, FILE *, struct got_object_id *, struct got_object_id *,
+    FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
     struct got_pathlist_head *, const char *, const char *, int, int, int,
     struct got_repository *, FILE *);
 
 /*
  * Diff two objects, assuming both objects are commits.
  * The number of context lines to show in diffs must be specified.
- * Two open temporary files must be provided for internal use; these files
- * can be obtained from got_opentemp() and must be closed by the caller.
+ * Two open temporary files and two temporary file descriptors must be
+ * provided for internal use; these files can be obtained from
+ * got_opentemp() and got_opentempfd(), and must be closed by the caller.
  * The set of arguments relating to either commit may be NULL to indicate
  * that no content is present on its respective side of the diff.
  * Write unified diff text to the provided output FILE.
@@ -181,7 +187,7 @@ const struct got_error *got_diff_objects_as_trees(off_t **, size_t *,
  * array of line offsets for, and the number of lines in, the unidiff text.
  */
 const struct got_error *got_diff_objects_as_commits(off_t **, size_t *,
-    FILE *, FILE *, struct got_object_id *, struct got_object_id *,
+    FILE *, FILE *, int, int, struct got_object_id *, struct got_object_id *,
     struct got_pathlist_head *, int, int, int, struct got_repository *, FILE *);
 
 #define GOT_DIFF_MAX_CONTEXT	64
diff --git a/lib/diff.c b/lib/diff.c
index 7bc3ad0..13a1b0b 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -273,25 +273,18 @@ got_diff_blob_file(struct got_blob_object *blob1, FILE *f1, off_t size1,
 }
 
 static const struct got_error *
-diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
-    mode_t mode, struct got_repository *repo,
+diff_added_blob(struct got_object_id *id, FILE *f, int fd,
+    const char *label, mode_t mode, struct got_repository *repo,
     got_diff_blob_cb cb, void *cb_arg)
 {
 	const struct got_error *err;
 	struct got_blob_object  *blob = NULL;
 	struct got_object *obj = NULL;
-	int fd = -1;
 
 	err = got_object_open(&obj, repo, id);
 	if (err)
 		return err;
 
-	fd = got_opentempfd();
-	if (fd == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-
 	err = got_object_blob_open(&blob, repo, obj, 8192, fd);
 	if (err)
 		goto done;
@@ -299,8 +292,6 @@ diff_added_blob(struct got_object_id *id, FILE *f, const char *label,
 	    NULL, label, 0, mode, repo);
 done:
 	got_object_close(obj);
-	if (fd != -1 && close(fd) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob)
 		got_object_blob_close(blob);
 	return err;
@@ -308,7 +299,8 @@ done:
 
 static const struct got_error *
 diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
-    FILE *f1, FILE *f2, const char *label1, const char *label2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    const char *label1, const char *label2,
     mode_t mode1, mode_t mode2, struct got_repository *repo,
     got_diff_blob_cb cb, void *cb_arg)
 {
@@ -317,23 +309,11 @@ diff_modified_blob(struct got_object_id *id1, struct got_object_id *id2,
 	struct got_object *obj2 = NULL;
 	struct got_blob_object *blob1 = NULL;
 	struct got_blob_object *blob2 = NULL;
-	int fd1 = -1, fd2 = -1;
 
 	err = got_object_open(&obj1, repo, id1);
 	if (err)
 		return err;
 
-	fd1 = got_opentempfd();
-	if (fd1 == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-	fd2 = got_opentempfd();
-	if (fd2 == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-
 	if (obj1->type != GOT_OBJ_TYPE_BLOB) {
 		err = got_error(GOT_ERR_OBJ_TYPE);
 		goto done;
@@ -362,29 +342,21 @@ done:
 		got_object_close(obj1);
 	if (obj2)
 		got_object_close(obj2);
-	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob1)
 		got_object_blob_close(blob1);
-	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob2)
 		got_object_blob_close(blob2);
 	return err;
 }
 
 static const struct got_error *
-diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
-    mode_t mode, struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
+diff_deleted_blob(struct got_object_id *id, FILE *f, int fd,
+   const char *label, mode_t mode, struct got_repository *repo,
+   got_diff_blob_cb cb, void *cb_arg)
 {
 	const struct got_error *err;
 	struct got_blob_object  *blob = NULL;
 	struct got_object *obj = NULL;
-	int fd = -1;
-
-	fd = got_opentempfd();
-	if (fd == -1)
-		return got_error_from_errno("got_opentempfd");
 
 	err = got_object_open(&obj, repo, id);
 	if (err)
@@ -397,15 +369,13 @@ diff_deleted_blob(struct got_object_id *id, FILE *f, const char *label,
 	    mode, 0, repo);
 done:
 	got_object_close(obj);
-	if (fd != -1 && close(fd) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob)
 		got_object_blob_close(blob);
 	return err;
 }
 
 static const struct got_error *
-diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
+diff_added_tree(struct got_object_id *id, FILE *f, int fd, const char *label,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
     int diff_content)
 {
@@ -426,7 +396,7 @@ diff_added_tree(struct got_object_id *id, FILE *f, const char *label,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(NULL, tree, NULL, f, NULL, label,
+	err = got_diff_tree(NULL, tree, NULL, f, -1, fd, NULL, label,
 	    repo, cb, cb_arg, diff_content);
 done:
 	if (tree)
@@ -438,7 +408,8 @@ done:
 
 static const struct got_error *
 diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
-    FILE *f1, FILE *f2, const char *label1, const char *label2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    const char *label1, const char *label2,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
     int diff_content)
 {
@@ -474,8 +445,8 @@ diff_modified_tree(struct got_object_id *id1, struct got_object_id *id2,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
-	    repo, cb, cb_arg, diff_content);
+	err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
+	    label1, label2, repo, cb, cb_arg, diff_content);
 
 done:
 	if (tree1)
@@ -490,9 +461,9 @@ done:
 }
 
 static const struct got_error *
-diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
-    struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
-    int diff_content)
+diff_deleted_tree(struct got_object_id *id, FILE *f, int fd,
+    const char *label, struct got_repository *repo,
+    got_diff_blob_cb cb, void *cb_arg, int diff_content)
 {
 	const struct got_error *err;
 	struct got_object *treeobj = NULL;
@@ -511,7 +482,7 @@ diff_deleted_tree(struct got_object_id *id, FILE *f, const char *label,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree, NULL, f, NULL, label, NULL,
+	err = got_diff_tree(tree, NULL, f, NULL, fd, -1, label, NULL,
 	    repo, cb, cb_arg, diff_content);
 done:
 	if (tree)
@@ -532,7 +503,8 @@ diff_kind_mismatch(struct got_object_id *id1, struct got_object_id *id2,
 
 static const struct got_error *
 diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
-    FILE *f1, FILE *f2, const char *label1, const char *label2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    const char *label1, const char *label2,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
     int diff_content)
 {
@@ -544,12 +516,12 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
 
 	if (te2 == NULL) {
 		if (S_ISDIR(te1->mode))
-			err = diff_deleted_tree(&te1->id, f1, label1, repo,
-			    cb, cb_arg, diff_content);
+			err = diff_deleted_tree(&te1->id, f1, fd1, label1,
+			    repo, cb, cb_arg, diff_content);
 		else {
 			if (diff_content)
-				err = diff_deleted_blob(&te1->id, f1, label1,
-				    te1->mode, repo, cb, cb_arg);
+				err = diff_deleted_blob(&te1->id, f1, fd1,
+				    label1, te1->mode, repo, cb, cb_arg);
 			else
 				err = cb(cb_arg, NULL, NULL, NULL, NULL,
 				    &te1->id, NULL, label1, NULL,
@@ -563,7 +535,8 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
 	if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
 		if (!id_match)
 			return diff_modified_tree(&te1->id, &te2->id, f1, f2,
-			    label1, label2, repo, cb, cb_arg, diff_content);
+			    fd1, fd2, label1, label2, repo, cb, cb_arg,
+			    diff_content);
 	} else if ((S_ISREG(te1->mode) || S_ISLNK(te1->mode)) &&
 	    (S_ISREG(te2->mode) || S_ISLNK(te2->mode))) {
 		if (!id_match ||
@@ -571,8 +544,8 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
 		    (te2->mode & (S_IFLNK | S_IXUSR))) {
 			if (diff_content)
 				return diff_modified_blob(&te1->id, &te2->id,
-				    f1, f2, label1, label2, te1->mode,
-				    te2->mode, repo, cb, cb_arg);
+				    f1, f2, fd1, fd2, label1, label2,
+				    te1->mode, te2->mode, repo, cb, cb_arg);
 			else
 				return cb(cb_arg, NULL, NULL, NULL, NULL,
 				    &te1->id, &te2->id, label1, label2,
@@ -589,7 +562,7 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_entry *te2,
 
 static const struct got_error *
 diff_entry_new_old(struct got_tree_entry *te2,
-    struct got_tree_entry *te1, FILE *f2, const char *label2,
+    struct got_tree_entry *te1, FILE *f2, int fd2, const char *label2,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
     int diff_content)
 {
@@ -600,12 +573,12 @@ diff_entry_new_old(struct got_tree_entry *te2,
 		return NULL;
 
 	if (S_ISDIR(te2->mode))
-		return diff_added_tree(&te2->id, f2, label2,
+		return diff_added_tree(&te2->id, f2, fd2, label2,
 		    repo, cb, cb_arg, diff_content);
 
 	if (diff_content)
-		return diff_added_blob(&te2->id, f2, label2, te2->mode,
-		    repo, cb, cb_arg);
+		return diff_added_blob(&te2->id, f2, fd2,
+		    label2, te2->mode, repo, cb, cb_arg);
 
 	return cb(cb_arg, NULL, NULL, NULL, NULL, NULL, &te2->id,
 	    NULL, label2, 0, te2->mode, repo);
@@ -656,7 +629,8 @@ done:
 
 const struct got_error *
 got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
-    FILE *f1, FILE *f2, const char *label1, const char *label2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    const char *label1, const char *label2,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg,
     int diff_content)
 {
@@ -693,8 +667,8 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 					return
 					    got_error_from_errno("asprintf");
 			}
-			err = diff_entry_old_new(te1, te, f1, f2, l1, l2,
-			    repo, cb, cb_arg, diff_content);
+			err = diff_entry_old_new(te1, te, f1, f2, fd1, fd2,
+			    l1, l2, repo, cb, cb_arg, diff_content);
 			if (err)
 				break;
 		}
@@ -716,7 +690,7 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 					return
 					    got_error_from_errno("asprintf");
 			}
-			err = diff_entry_new_old(te2, te, f2, l2, repo,
+			err = diff_entry_new_old(te2, te, f2, fd2, l2, repo,
 			    cb, cb_arg, diff_content);
 			if (err)
 				break;
@@ -749,27 +723,18 @@ got_diff_tree(struct got_tree_object *tree1, struct got_tree_object *tree2,
 
 const struct got_error *
 got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
-    FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    struct got_object_id *id1, struct got_object_id *id2,
     const char *label1, const char *label2, int diff_context,
     int ignore_whitespace, int force_text_diff,
     struct got_repository *repo, FILE *outfile)
 {
 	const struct got_error *err;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
-	int fd1 = -1, fd2 = -1;
 
 	if (id1 == NULL && id2 == NULL)
 		return got_error(GOT_ERR_NO_OBJ);
 
-	fd1 = got_opentempfd();
-	if (fd1 == -1)
-		return got_error_from_errno("got_opentempfd");
-	fd2 = got_opentempfd();
-	if (fd2 == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-
 	if (id1) {
 		err = got_object_open_as_blob(&blob1, repo, id1, 8192, fd1);
 		if (err)
@@ -784,12 +749,8 @@ got_diff_objects_as_blobs(off_t **line_offsets, size_t *nlines,
 	    label1, label2, diff_context, ignore_whitespace, force_text_diff,
 	    outfile);
 done:
-	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob1)
 		got_object_blob_close(blob1);
-	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob2)
 		got_object_blob_close(blob2);
 	return err;
@@ -797,7 +758,7 @@ done:
 
 static const struct got_error *
 diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
-    FILE *f1, FILE *f2, struct got_pathlist_head *paths,
+    FILE *f1, FILE *f2, int fd1, int fd2, struct got_pathlist_head *paths,
     struct got_repository *repo, got_diff_blob_cb cb, void *cb_arg)
 {
 	const struct got_error *err = NULL;
@@ -805,16 +766,7 @@ diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
 	struct got_object_id *id1 = NULL, *id2 = NULL;
 	struct got_tree_object *subtree1 = NULL, *subtree2 = NULL;
 	struct got_blob_object *blob1 = NULL, *blob2 = NULL;
-	int fd1 = -1, fd2 = -1;
-
-	fd1 = got_opentempfd();
-	if (fd1 == -1)
-		return got_error_from_errno("got_opentempfd");
-	fd2 = got_opentempfd();
-	if (fd2 == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
+
 	TAILQ_FOREACH(pe, paths, entry) {
 		int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
 		mode_t mode1 = 0, mode2 = 0;
@@ -907,6 +859,7 @@ diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
 					goto done;
 			}
 			err = got_diff_tree(subtree1, subtree2, f1, f2,
+			    fd1, fd2,
 			    id1 ? pe->path : "/dev/null",
 			    id2 ? pe->path : "/dev/null",
 			    repo, cb, cb_arg, 1);
@@ -916,10 +869,6 @@ diff_paths(struct got_tree_object *tree1, struct got_tree_object *tree2,
 			err = got_error(GOT_ERR_OBJ_TYPE);
 			goto done;
 		}
-		if (ftruncate(fd1, 0L) == -1)
-			return got_error_from_errno("ftruncate");
-		if (ftruncate(fd2, 0L) == -1)
-			return got_error_from_errno("ftruncate");
 	}
 done:
 	free(id1);
@@ -928,12 +877,8 @@ done:
 		got_object_tree_close(subtree1);
 	if (subtree2)
 		got_object_tree_close(subtree2);
-	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob1)
 		got_object_blob_close(blob1);
-	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (blob2)
 		got_object_blob_close(blob2);
 	return err;
@@ -967,7 +912,8 @@ show_object_id(off_t **line_offsets, size_t *nlines, const char *obj_typestr,
 
 static const struct got_error *
 diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
-    FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    struct got_object_id *id1, struct got_object_id *id2,
     struct got_pathlist_head *paths, const char *label1, const char *label2,
     int diff_context, int ignore_whitespace, int force_text_diff,
     struct got_repository *repo, FILE *outfile)
@@ -1003,10 +949,11 @@ diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
 		arg.nlines = 0;
 	}
 	if (paths == NULL || TAILQ_EMPTY(paths)) {
-		err = got_diff_tree(tree1, tree2, f1, f2, label1, label2,
-		    repo, got_diff_blob_output_unidiff, &arg, 1);
+		err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2,
+		    label1, label2, repo,
+		    got_diff_blob_output_unidiff, &arg, 1);
 	} else {
-		err = diff_paths(tree1, tree2, f1, f2, paths, repo,
+		err = diff_paths(tree1, tree2, f1, f2, fd1, fd2, paths, repo,
 		    got_diff_blob_output_unidiff, &arg);
 	}
 	if (want_lineoffsets) {
@@ -1023,7 +970,8 @@ done:
 
 const struct got_error *
 got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
-    FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    struct got_object_id *id1, struct got_object_id *id2,
     struct got_pathlist_head *paths, const char *label1, const char *label2,
     int diff_context, int ignore_whitespace, int force_text_diff,
     struct got_repository *repo, FILE *outfile)
@@ -1068,8 +1016,8 @@ got_diff_objects_as_trees(off_t **line_offsets, size_t *nlines,
 			goto done;
 	}
 
-	err = diff_objects_as_trees(line_offsets, nlines, f1, f2, id1, id2,
-	    paths, label1, label2, diff_context, ignore_whitespace,
+	err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
+	    id1, id2, paths, label1, label2, diff_context, ignore_whitespace,
 	    force_text_diff, repo, outfile);
 done:
 	free(idstr);
@@ -1078,7 +1026,8 @@ done:
 
 const struct got_error *
 got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
-    FILE *f1, FILE *f2, struct got_object_id *id1, struct got_object_id *id2,
+    FILE *f1, FILE *f2, int fd1, int fd2,
+    struct got_object_id *id1, struct got_object_id *id2,
     struct got_pathlist_head *paths,
     int diff_context, int ignore_whitespace, int force_text_diff,
     struct got_repository *repo, FILE *outfile)
@@ -1122,7 +1071,7 @@ got_diff_objects_as_commits(off_t **line_offsets, size_t *nlines,
 	if (err)
 		goto done;
 
-	err = diff_objects_as_trees(line_offsets, nlines, f1, f2,
+	err = diff_objects_as_trees(line_offsets, nlines, f1, f2, fd1, fd2,
 	    commit1 ? got_object_commit_get_tree_id(commit1) : NULL,
 	    got_object_commit_get_tree_id(commit2), paths, "", "",
 	    diff_context, ignore_whitespace, force_text_diff, repo, outfile);
diff --git a/lib/error.c b/lib/error.c
index 8decec3..3ffd653 100644
--- a/lib/error.c
+++ b/lib/error.c
@@ -236,6 +236,7 @@ got_error(int code)
 {
 	size_t i;
 
+	if (code == GOT_ERR_PRIVSEP_LEN) abort();
 	for (i = 0; i < nitems(got_errors); i++) {
 		if (code == got_errors[i].code)
 			return &got_errors[i];
diff --git a/lib/object.c b/lib/object.c
index d2ba8bf..12ae8e5 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -1368,6 +1368,15 @@ open_blob(struct got_blob_object **blob, struct got_repository *repo,
 		goto done;
 	}
 
+	if (ftruncate(outfd, 0L) == -1) {
+		err = got_error_from_errno("ftruncate");
+		goto done;
+	}
+	if (lseek(outfd, SEEK_SET, 0) == -1) {
+		err = got_error_from_errno("lseek");
+		goto done;
+	}
+
 	err = got_repo_search_packidx(&packidx, &idx, repo, id);
 	if (err == NULL) {
 		struct got_pack *pack = NULL;
diff --git a/lib/worktree.c b/lib/worktree.c
index bde5fed..18f13a1 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3136,6 +3136,7 @@ merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
 	struct merge_file_cb_arg arg;
 	char *label_orig = NULL;
 	FILE *f1 = NULL, *f2 = NULL;
+	int fd1 = -1, fd2 = -1;
 
 	if (commit_id1) {
 		err = got_object_open_as_commit(&commit1, repo, commit_id1);
@@ -3191,10 +3192,22 @@ merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
 		goto done;
 	}
 
+	fd1 = got_opentempfd();
+	if (fd1 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
 	cmc_arg.worktree = worktree;
 	cmc_arg.fileindex = fileindex;
 	cmc_arg.repo = repo;
-	err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
+	err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
 	    check_merge_conflicts, &cmc_arg, 0);
 	if (err)
 		goto done;
@@ -3208,7 +3221,7 @@ merge_files(struct got_worktree *worktree, struct got_fileindex *fileindex,
 	arg.label_orig = label_orig;
 	arg.commit_id2 = commit_id2;
 	arg.allow_bad_symlinks = 1; /* preserve bad symlinks across merges */
-	err = got_diff_tree(tree1, tree2, f1, f2, "", "", repo,
+	err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
 	    merge_file_cb, &arg, 1);
 	sync_err = sync_fileindex(fileindex, fileindex_path);
 	if (sync_err && err == NULL)
@@ -3226,6 +3239,10 @@ done:
 		err = got_error_from_errno("fclose");
 	if (f2 && fclose(f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
+	if (fd1 != -1 && close(fd1) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	if (fd2 != -1 && close(fd2) == -1 && err == NULL)
+		err = got_error_from_errno("close");
 	free(label_orig);
 	return err;
 }
diff --git a/tog/tog.c b/tog/tog.c
index 0b32bf9..3ec7da1 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -309,6 +309,7 @@ struct tog_diff_view_state {
 	struct got_object_id *id1, *id2;
 	const char *label1, *label2;
 	FILE *f, *f1, *f2;
+	int fd1, fd2;
 	int first_displayed_line;
 	int last_displayed_line;
 	int eof;
@@ -3504,7 +3505,7 @@ get_changed_paths(struct got_pathlist_head *paths,
 	if (err)
 		goto done;
 
-	err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
+	err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
 	    got_diff_tree_collect_changed_paths, paths, 0);
 done:
 	if (tree1)
@@ -3724,14 +3725,15 @@ create_diff(struct tog_diff_view_state *s)
 	switch (obj_type) {
 	case GOT_OBJ_TYPE_BLOB:
 		err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
-		    s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
-		    s->diff_context, s->ignore_whitespace, s->force_text_diff,
-		    s->repo, s->f);
+		    s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2,
+		    s->label1, s->label2, s->diff_context,
+		    s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
 		break;
 	case GOT_OBJ_TYPE_TREE:
 		err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
-		    s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
-		    s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
+		    s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL, "", "",
+		    s->diff_context, s->ignore_whitespace, s->force_text_diff,
+		    s->repo, s->f);
 		break;
 	case GOT_OBJ_TYPE_COMMIT: {
 		const struct got_object_id_queue *parent_ids;
@@ -3765,8 +3767,9 @@ create_diff(struct tog_diff_view_state *s)
 		got_object_commit_close(commit2);
 
 		err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
-		    s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
-		    s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
+		    s->f1, s->f2, s->fd1, s->fd2, s->id1, s->id2, NULL,
+		    s->diff_context, s->ignore_whitespace, s->force_text_diff,
+		    s->repo, s->f);
 		break;
 	}
 	default:
@@ -3884,12 +3887,18 @@ close_diff_view(struct tog_view *view)
 	if (s->f && fclose(s->f) == EOF)
 		err = got_error_from_errno("fclose");
 	s->f = NULL;
-	if (s->f1 && fclose(s->f1) == EOF)
+	if (s->f1 && fclose(s->f1) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	s->f1 = NULL;
-	if (s->f2 && fclose(s->f2) == EOF)
+	if (s->f2 && fclose(s->f2) == EOF && err == NULL)
 		err = got_error_from_errno("fclose");
 	s->f2 = NULL;
+	if (s->fd1 != -1 && close(s->fd1) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	s->fd1 = -1;
+	if (s->fd2 != -1 && close(s->fd2) == -1 && err == NULL)
+		err = got_error_from_errno("close");
+	s->fd2 = -1;
 	free_colors(&s->colors);
 	free(s->line_offsets);
 	s->line_offsets = NULL;
@@ -3907,6 +3916,8 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
 	struct tog_diff_view_state *s = &view->state.diff;
 
 	memset(s, 0, sizeof(*s));
+	s->fd1 = -1;
+	s->fd2 = -1;
 
 	if (id1 != NULL && id2 != NULL) {
 	    int type1, type2;
@@ -3954,6 +3965,18 @@ open_diff_view(struct tog_view *view, struct got_object_id *id1,
 		goto done;
 	}
 
+	s->fd1 = got_opentempfd();
+	if (s->fd1 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	s->fd2 = got_opentempfd();
+	if (s->fd2 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
 	s->first_displayed_line = 1;
 	s->last_displayed_line = view->nlines;
 	s->diff_context = diff_context;