Commit e6e73e5504113b6c75f1b2e3dec3e22af8f41996

Tracey Emery 2022-06-30T08:41:14

move remaining got_opentemp's out of blame. ok op@

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/got/got.c b/got/got.c
index 2dceeed..fcf1748 100644
--- a/got/got.c
+++ b/got/got.c
@@ -5360,12 +5360,13 @@ cmd_blame(int argc, char *argv[])
 	struct got_blob_object *blob = NULL;
 	char *commit_id_str = NULL;
 	struct blame_cb_args bca;
-	int ch, obj_type, i, fd = -1, fd1 = -1;
+	int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
 	off_t filesize;
 	int *pack_fds = NULL;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return got_error_from_errno("got_opentempfd");
 
 	memset(&bca, 0, sizeof(bca));
@@ -5518,7 +5519,7 @@ cmd_blame(int argc, char *argv[])
 		goto done;
 	}
 
-	error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
+	error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
 	if (error)
 		goto done;
 	bca.f = got_opentemp();
@@ -5549,13 +5550,28 @@ cmd_blame(int argc, char *argv[])
 	}
 	bca.repo = repo;
 
-	fd1 = got_opentempfd();
-	if (fd1 == -1) {
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	fd3 = got_opentempfd();
+	if (fd3 == -1) {
 		error = got_error_from_errno("got_opentempfd");
 		goto done;
 	}
+	f1 = got_opentemp();
+	if (f1 == NULL) {
+		error = got_error_from_errno("got_opentemp");
+		goto done;
+	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		error = got_error_from_errno("got_opentemp");
+		goto done;
+	}
 	error = got_blame(link_target ? link_target : in_repo_path, commit_id,
-	    repo, blame_cb, &bca, check_cancelled, NULL, fd1);
+	    repo, blame_cb, &bca, check_cancelled, NULL, fd2, fd3, f1, f2);
 done:
 	free(in_repo_path);
 	free(link_target);
@@ -5565,10 +5581,18 @@ done:
 	free(obj_id);
 	if (commit)
 		got_object_commit_close(commit);
-	if (fd != -1 && close(fd) == -1 && error == NULL)
-		error = got_error_from_errno("close");
+
 	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");
+	if (fd3 != -1 && close(fd3) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (f1 && fclose(f1) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+	if (f2 && fclose(f2) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+
 	if (blob)
 		got_object_blob_close(blob);
 	if (worktree)
diff --git a/gotweb/gotweb.c b/gotweb/gotweb.c
index eb1c8f8..80bd351 100644
--- a/gotweb/gotweb.c
+++ b/gotweb/gotweb.c
@@ -4083,12 +4083,23 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
 	struct got_blob_object *blob = NULL;
 	char *path = NULL, *in_repo_path = NULL;
 	struct gw_blame_cb_args bca;
-	int i, obj_type, fd = -1, fd1 = -1;
+	int i, obj_type, fd1 = -1, fd2 = -1, fd3 = -1;
 	off_t filesize;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return got_error_from_errno("got_opentempfd");
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	fd3 = got_opentempfd();
+	if (fd3 == -1) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
 
 	memset(&bca, 0, sizeof(bca));
 
@@ -4133,7 +4144,7 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
 	}
 
 	error = got_object_open_as_blob(&blob, gw_trans->repo, obj_id, 8192,
-	    fd);
+	    fd1);
 	if (error)
 		goto done;
 
@@ -4172,18 +4183,35 @@ gw_output_file_blame(struct gw_trans *gw_trans, struct gw_header *header)
 		goto done;
 	}
 
+	f1 = got_opentemp();
+	if (f1 == NULL) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		error = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
 	error = got_blame(in_repo_path, commit_id, gw_trans->repo, gw_blame_cb,
-	    &bca, NULL, NULL, fd1);
+	    &bca, NULL, NULL, fd2, fd3, f1, f2);
 done:
 	free(in_repo_path);
 	free(commit_id);
 	free(obj_id);
 	free(path);
 
-	if (fd != -1 && close(fd) == -1 && error == NULL)
-		error = got_error_from_errno("close");
 	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");
+	if (fd3 != -1 && close(fd3) == -1 && error == NULL)
+		error = got_error_from_errno("close");
+	if (f1 && fclose(f1) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
+	if (f2 && fclose(f2) == EOF && error == NULL)
+		error = got_error_from_errno("fclose");
 
 	if (blob) {
 		free(bca.line_offsets);
diff --git a/include/got_blame.h b/include/got_blame.h
index 56e2d80..5d01c00 100644
--- a/include/got_blame.h
+++ b/include/got_blame.h
@@ -36,4 +36,4 @@ typedef const struct got_error *(*got_blame_cb)(void *, int, int,
  */
 const struct got_error *got_blame(const char *,
     struct got_object_id *, struct got_repository *,
-    got_blame_cb, void *, got_cancel_cb, void *, int);
+    got_blame_cb, void *, got_cancel_cb, void *, int, int, FILE *, FILE *);
diff --git a/lib/blame.c b/lib/blame.c
index 68e7392..f08e9f1 100644
--- a/lib/blame.c
+++ b/lib/blame.c
@@ -65,6 +65,7 @@ struct got_blame {
 	 */
 	FILE *f1; /* older version from commit N-1. */
 	FILE *f2; /* newer version from commit N. */
+	int fd;
 	unsigned char *map1;
 	unsigned char *map2;
 	off_t size1;
@@ -204,7 +205,6 @@ blame_commit(struct got_blame *blame, struct got_object_id *id,
 	struct got_object_id *pblob_id = NULL;
 	struct got_blob_object *pblob = NULL;
 	struct diff_result *diff_result = NULL;
-	int fd = -1;
 
 	err = got_object_open_as_commit(&commit, repo, id);
 	if (err)
@@ -216,12 +216,6 @@ blame_commit(struct got_blame *blame, struct got_object_id *id,
 		return NULL;
 	}
 
-	fd = got_opentempfd();
-	if (fd == -1) {
-		err = got_error_from_errno("got_opentempfd");
-		goto done;
-	}
-
 	err = got_object_open_as_commit(&pcommit, repo, &pid->id);
 	if (err)
 		goto done;
@@ -233,16 +227,10 @@ blame_commit(struct got_blame *blame, struct got_object_id *id,
 		goto done;
 	}
 
-	err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, fd);
+	err = got_object_open_as_blob(&pblob, repo, pblob_id, 8192, blame->fd);
 	if (err)
 		goto done;
 
-	blame->f1 = got_opentemp();
-	if (blame->f1 == NULL) {
-		err = got_error_from_errno("got_opentemp");
-		goto done;
-	}
-
 	err = blame_prepare_file(blame->f1, &blame->map1, &blame->size1,
 	    &blame->nlines1, &blame->line_offsets1, blame->data1,
 	    blame->cfg, pblob);
@@ -280,8 +268,6 @@ done:
 	if (pcommit)
 		got_object_commit_close(pcommit);
 	free(pblob_id);
-	if (fd != -1 && close(fd) == -1 && err == NULL)
-		err = got_error_from_errno("close");
 	if (pblob)
 		got_object_blob_close(pblob);
 	return err;
@@ -304,10 +290,6 @@ blame_close(struct got_blame *blame)
 		if (munmap(blame->map2, blame->size2) == -1 && err == NULL)
 			err = got_error_from_errno("munmap");
 	}
-	if (blame->f1 && fclose(blame->f1) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
-	if (blame->f2 && fclose(blame->f2) == EOF && err == NULL)
-		err = got_error_from_errno("fclose");
 	free(blame->lines);
 	free(blame->line_offsets1);
 	free(blame->line_offsets2);
@@ -466,9 +448,11 @@ blame_atomize_file(void *arg, struct diff_data *d)
 }
 
 static const struct got_error *
-close_file2_and_reuse_file1(struct got_blame *blame)
+flip_files(struct got_blame *blame)
 {
+	const struct got_error *err = NULL;
 	struct diff_data *d;
+	FILE *tmp;
 
 	free(blame->line_offsets2);
 	blame->line_offsets2 = blame->line_offsets1;
@@ -483,15 +467,16 @@ close_file2_and_reuse_file1(struct got_blame *blame)
 			return got_error_from_errno("munmap");
 		blame->map2 = blame->map1;
 		blame->map1 = NULL;
-
 	}
 	blame->size2 = blame->size1;
-	blame->size1 = 0;
 
-	if (fclose(blame->f2) == EOF)
-		return got_error_from_errno("fclose");
+	err = got_opentemp_truncate(blame->f2);
+	if (err)
+		return err; 
+	tmp = blame->f2;
 	blame->f2 = blame->f1;
-	blame->f1 = NULL;
+	blame->f1 = tmp;
+	blame->size1 = 0;
 
 	blame->nlines2 = blame->nlines1;
 	blame->nlines1 = 0;
@@ -509,7 +494,7 @@ static const struct got_error *
 blame_open(struct got_blame **blamep, const char *path,
     struct got_object_id *start_commit_id, struct got_repository *repo,
     got_blame_cb cb, void *arg, got_cancel_cb cancel_cb, void *cancel_arg,
-    int fd)
+    int fd1, int fd2, FILE *f1, FILE *f2)
 {
 	const struct got_error *err = NULL;
 	struct got_commit_object *start_commit = NULL, *last_commit = NULL;
@@ -530,7 +515,7 @@ blame_open(struct got_blame **blamep, const char *path,
 	if (err)
 		goto done;
 
-	err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd);
+	err = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
 	if (err)
 		goto done;
 
@@ -551,11 +536,10 @@ blame_open(struct got_blame **blamep, const char *path,
 		goto done;
 	}
 
-	blame->f2 = got_opentemp();
-	if (blame->f2 == NULL) {
-		err = got_error_from_errno("got_opentemp");
-		goto done;
-	}
+	blame->f1 = f1;
+	blame->f2 = f2;
+	blame->fd = fd2;
+
 	err = got_diff_get_config(&blame->cfg, GOT_DIFF_ALGORITHM_PATIENCE,
 	    blame_atomize_file, blame);
 	if (err)
@@ -619,7 +603,7 @@ blame_open(struct got_blame **blamep, const char *path,
 			if (blame->nannotated == blame->nlines)
 				break;
 
-			err = close_file2_and_reuse_file1(blame);
+			err = flip_files(blame);
 			if (err)
 				goto done;
 		}
@@ -660,7 +644,8 @@ done:
 const struct got_error *
 got_blame(const char *path, struct got_object_id *commit_id,
     struct got_repository *repo, got_blame_cb cb, void *arg,
-    got_cancel_cb cancel_cb, void* cancel_arg, int fd)
+    got_cancel_cb cancel_cb, void* cancel_arg, int fd1, int fd2, FILE *f1,
+    FILE *f2)
 {
 	const struct got_error *err = NULL, *close_err = NULL;
 	struct got_blame *blame;
@@ -670,7 +655,7 @@ got_blame(const char *path, struct got_object_id *commit_id,
 		return got_error_from_errno2("asprintf", path);
 
 	err = blame_open(&blame, abspath, commit_id, repo, cb, arg,
-	    cancel_cb, cancel_arg, fd);
+	    cancel_cb, cancel_arg, fd1, fd2, f1, f2);
 	free(abspath);
 	if (blame)
 		close_err = blame_close(blame);
diff --git a/tog/tog.c b/tog/tog.c
index 6a0c6db..b458f29 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -4907,25 +4907,45 @@ blame_thread(void *arg)
 	const struct got_error *err, *close_err;
 	struct tog_blame_thread_args *ta = arg;
 	struct tog_blame_cb_args *a = ta->cb_args;
-	int errcode, fd = -1;
+	int errcode, fd1 = -1, fd2 = -1;
+	FILE *f1 = NULL, *f2 = NULL;
 
-	fd = got_opentempfd();
-	if (fd == -1)
+	fd1 = got_opentempfd();
+	if (fd1 == -1)
 		return (void *)got_error_from_errno("got_opentempfd");
 
+	fd2 = got_opentempfd();
+	if (fd2 == -1) {
+		err = got_error_from_errno("got_opentempfd");
+		goto done;
+	}
+
+	f1 = got_opentemp();
+	if (f1 == NULL) {
+		err = (void *)got_error_from_errno("got_opentemp");
+		goto done;
+	}
+	f2 = got_opentemp();
+	if (f2 == NULL) {
+		err = (void *)got_error_from_errno("got_opentemp");
+		goto done;
+	}
+
 	err = block_signals_used_by_main_thread();
 	if (err)
-		return (void *)err;
+		goto done;
 
 	err = got_blame(ta->path, a->commit_id, ta->repo,
-	    blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd);
+	    blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg, fd1, fd2, f1,
+	    f2);
 	if (err && err->code == GOT_ERR_CANCELLED)
 		err = NULL;
 
 	errcode = pthread_mutex_lock(&tog_mutex);
-	if (errcode)
-		return (void *)got_error_set_errno(errcode,
-		    "pthread_mutex_lock");
+	if (errcode) {
+		err = got_error_set_errno(errcode, "pthread_mutex_lock");
+		goto done;
+	}
 
 	close_err = got_repo_close(ta->repo);
 	if (err == NULL)
@@ -4937,8 +4957,15 @@ blame_thread(void *arg)
 	if (errcode && err == NULL)
 		err = got_error_set_errno(errcode, "pthread_mutex_unlock");
 
-	if (fd != -1 && close(fd) == -1 && err == NULL)
+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 (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");
 
 	return (void *)err;
 }