Commit a158c90155bc8066c8f6d87a4a9600d8c5bf54c8

Stefan Sperling 2018-12-23T23:35:37

make all the _packed_read_privsep() functions local to object.c

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
diff --git a/lib/got_lib_object_parse.h b/lib/got_lib_object_parse.h
index 5c3980f..ae71532 100644
--- a/lib/got_lib_object_parse.h
+++ b/lib/got_lib_object_parse.h
@@ -41,15 +41,5 @@ void got_object_tree_entry_close(struct got_tree_entry *);
 struct got_pack;
 struct got_packidx;
 
-const struct got_error *got_object_packed_read_privsep(struct got_object **,
-    struct got_repository *, struct got_pack *, struct got_packidx *, int,
-    struct got_object_id *);
-const struct got_error *
-got_object_read_packed_commit_privsep(struct got_commit_object **,
-    struct got_pack *, struct got_packidx *, int, struct got_object_id *);
-const struct got_error *got_object_read_packed_tree_privsep(
-    struct got_tree_object **, struct got_object *, struct got_pack *);
-const struct got_error *got_object_read_packed_tag_privsep(
-    struct got_tag_object **, struct got_object *, struct got_pack *);
 const struct got_error *got_object_parse_header(struct got_object **, char *, size_t);
 const struct got_error *got_object_read_header(struct got_object **, int);
diff --git a/lib/object.c b/lib/object.c
index 0406a8a..d47d917 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -178,6 +178,124 @@ get_packfile_path(char **path_packfile, struct got_packidx *packidx)
 	return NULL;
 }
 
+static void
+exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
+{
+	close(imsg_fds[0]);
+
+	if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
+		fprintf(stderr, "%s: %s\n", getprogname(),
+		    strerror(errno));
+		_exit(1);
+	}
+	if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
+		fprintf(stderr, "%s: %s\n", getprogname(),
+		    strerror(errno));
+		_exit(1);
+	}
+
+	if (execl(path, path, repo_path, (char *)NULL) == -1) {
+		fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
+		    strerror(errno));
+		_exit(1);
+	}
+}
+
+static const struct got_error *
+request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
+    struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+	struct imsgbuf *ibuf = pack->privsep_child->ibuf;
+
+	err = got_privsep_send_packed_obj_req(ibuf, idx, id);
+	if (err)
+		return err;
+
+	err = got_privsep_recv_obj(obj, ibuf);
+	if (err)
+		return err;
+
+	(*obj)->path_packfile = strdup(pack->path_packfile);
+	if ((*obj)->path_packfile == NULL) {
+		err = got_error_from_errno();
+		return err;
+	}
+	memcpy(&(*obj)->id, id, sizeof((*obj)->id));
+
+	return NULL;
+}
+
+static const struct got_error *
+read_packed_object_privsep(struct got_object **obj,
+    struct got_repository *repo, struct got_pack *pack,
+    struct got_packidx *packidx, int idx, struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+	int imsg_fds[2];
+	pid_t pid;
+	struct imsgbuf *ibuf;
+
+	if (pack->privsep_child)
+		return request_packed_object(obj, pack, idx, id);
+
+	ibuf = calloc(1, sizeof(*ibuf));
+	if (ibuf == NULL)
+		return got_error_from_errno();
+
+	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
+	if (pack->privsep_child == NULL) {
+		err = got_error_from_errno();
+		free(ibuf);
+		return err;
+	}
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		err = got_error_from_errno();
+		goto done;
+	} else if (pid == 0) {
+		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
+		    pack->path_packfile);
+		/* not reached */
+	}
+
+	close(imsg_fds[1]);
+	pack->privsep_child->imsg_fd = imsg_fds[0];
+	pack->privsep_child->pid = pid;
+	imsg_init(ibuf, imsg_fds[0]);
+	pack->privsep_child->ibuf = ibuf;
+
+	err = got_privsep_init_pack_child(ibuf, pack, packidx);
+	if (err) {
+		const struct got_error *child_err;
+		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
+		child_err = got_privsep_wait_for_child(
+		    pack->privsep_child->pid);
+		if (child_err && err == NULL)
+			err = child_err;
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+		return err;
+	}
+
+done:
+	if (err) {
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+	} else
+		err = request_packed_object(obj, pack, idx, id);
+	return err;
+}
+
+
 static const struct got_error *
 open_packed_object(struct got_object **obj, struct got_object_id *id,
     struct got_repository *repo)
@@ -203,7 +321,7 @@ open_packed_object(struct got_object **obj, struct got_object_id *id,
 			goto done;
 	}
 
-	err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
+	err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
 	if (err)
 		goto done;
 
@@ -295,6 +413,90 @@ got_object_resolve_id_str(struct got_object_id **id,
 }
 
 static const struct got_error *
+request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
+    int pack_idx, struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+
+	err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
+	    pack_idx);
+	if (err)
+		return err;
+
+	return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
+}
+
+static const struct got_error *
+read_packed_commit_privsep(struct got_commit_object **commit,
+    struct got_pack *pack, struct got_packidx *packidx, int idx,
+    struct got_object_id *id)
+{
+	const struct got_error *err = NULL;
+	int imsg_fds[2];
+	pid_t pid;
+	struct imsgbuf *ibuf;
+
+	if (pack->privsep_child)
+		return request_packed_commit(commit, pack, idx, id);
+
+	ibuf = calloc(1, sizeof(*ibuf));
+	if (ibuf == NULL)
+		return got_error_from_errno();
+
+	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
+	if (pack->privsep_child == NULL) {
+		err = got_error_from_errno();
+		free(ibuf);
+		return err;
+	}
+
+	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
+		err = got_error_from_errno();
+		goto done;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		err = got_error_from_errno();
+		goto done;
+	} else if (pid == 0) {
+		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
+		    pack->path_packfile);
+		/* not reached */
+	}
+
+	close(imsg_fds[1]);
+	pack->privsep_child->imsg_fd = imsg_fds[0];
+	pack->privsep_child->pid = pid;
+	imsg_init(ibuf, imsg_fds[0]);
+	pack->privsep_child->ibuf = ibuf;
+
+	err = got_privsep_init_pack_child(ibuf, pack, packidx);
+	if (err) {
+		const struct got_error *child_err;
+		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
+		child_err = got_privsep_wait_for_child(
+		    pack->privsep_child->pid);
+		if (child_err && err == NULL)
+			err = child_err;
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+		return err;
+	}
+
+done:
+	if (err) {
+		free(ibuf);
+		free(pack->privsep_child);
+		pack->privsep_child = NULL;
+	} else
+		err = request_packed_commit(commit, pack, idx, id);
+	return err;
+}
+
+
+static const struct got_error *
 open_commit(struct got_commit_object **commit,
     struct got_repository *repo, struct got_object_id *id, int check_cache)
 {
@@ -327,7 +529,7 @@ open_commit(struct got_commit_object **commit,
 			if (err)
 				return err;
 		}
-		err = got_object_read_packed_commit_privsep(commit, pack,
+		err = read_packed_commit_privsep(commit, pack,
 		    packidx, idx, id);
 	} else if (err->code == GOT_ERR_NO_OBJ) {
 		int fd;
@@ -388,6 +590,19 @@ got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
 }
 
 static const struct got_error *
+read_packed_tree_privsep(struct got_tree_object **tree,
+    struct got_object *obj, struct got_pack *pack)
+{
+	const struct got_error *err = NULL;
+
+	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
+	if (err)
+		return err;
+
+	return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
+}
+
+static const struct got_error *
 open_tree(struct got_tree_object **tree,
     struct got_repository *repo, struct got_object *obj, int check_cache)
 {
@@ -414,7 +629,7 @@ open_tree(struct got_tree_object **tree,
 			if (err)
 				return err;
 		}
-		err = got_object_read_packed_tree_privsep(tree, obj, pack);
+		err = read_packed_tree_privsep(tree, obj, pack);
 	} else {
 		int fd;
 		err = open_loose_object(&fd, got_object_get_id(obj), repo);
@@ -726,6 +941,20 @@ got_object_blob_dump_to_file(size_t *total_len, int *nlines,
 }
 
 static const struct got_error *
+read_packed_tag_privsep(struct got_tag_object **tag,
+    struct got_object *obj, struct got_pack *pack)
+{
+	const struct got_error *err = NULL;
+
+	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
+	if (err)
+		return err;
+
+	return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
+}
+
+
+static const struct got_error *
 open_tag(struct got_tag_object **tag,
     struct got_repository *repo, struct got_object *obj, int check_cache)
 {
@@ -752,7 +981,7 @@ open_tag(struct got_tag_object **tag,
 			if (err)
 				return err;
 		}
-		err = got_object_read_packed_tag_privsep(tag, obj, pack);
+		err = read_packed_tag_privsep(tag, obj, pack);
 	} else {
 		int fd;
 		err = open_loose_object(&fd, got_object_get_id(obj), repo);
@@ -1001,29 +1230,6 @@ done:
 	return err;
 }
 
-static void
-exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
-{
-	close(imsg_fds[0]);
-
-	if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
-		fprintf(stderr, "%s: %s\n", getprogname(),
-		    strerror(errno));
-		_exit(1);
-	}
-	if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
-		fprintf(stderr, "%s: %s\n", getprogname(),
-		    strerror(errno));
-		_exit(1);
-	}
-
-	if (execl(path, path, repo_path, (char *)NULL) == -1) {
-		fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
-		    strerror(errno));
-		_exit(1);
-	}
-}
-
 static const struct got_error *
 request_object(struct got_object **obj, struct got_repository *repo, int fd)
 {
@@ -1077,100 +1283,6 @@ got_object_read_header_privsep(struct got_object **obj,
 }
 
 static const struct got_error *
-request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
-    struct got_object_id *id)
-{
-	const struct got_error *err = NULL;
-	struct imsgbuf *ibuf = pack->privsep_child->ibuf;
-
-	err = got_privsep_send_packed_obj_req(ibuf, idx, id);
-	if (err)
-		return err;
-
-	err = got_privsep_recv_obj(obj, ibuf);
-	if (err)
-		return err;
-
-	(*obj)->path_packfile = strdup(pack->path_packfile);
-	if ((*obj)->path_packfile == NULL) {
-		err = got_error_from_errno();
-		return err;
-	}
-	memcpy(&(*obj)->id, id, sizeof((*obj)->id));
-
-	return NULL;
-}
-
-const struct got_error *
-got_object_packed_read_privsep(struct got_object **obj,
-    struct got_repository *repo, struct got_pack *pack,
-    struct got_packidx *packidx, int idx, struct got_object_id *id)
-{
-	const struct got_error *err = NULL;
-	int imsg_fds[2];
-	pid_t pid;
-	struct imsgbuf *ibuf;
-
-	if (pack->privsep_child)
-		return request_packed_object(obj, pack, idx, id);
-
-	ibuf = calloc(1, sizeof(*ibuf));
-	if (ibuf == NULL)
-		return got_error_from_errno();
-
-	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
-	if (pack->privsep_child == NULL) {
-		err = got_error_from_errno();
-		free(ibuf);
-		return err;
-	}
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
-		err = got_error_from_errno();
-		goto done;
-	}
-
-	pid = fork();
-	if (pid == -1) {
-		err = got_error_from_errno();
-		goto done;
-	} else if (pid == 0) {
-		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
-		    pack->path_packfile);
-		/* not reached */
-	}
-
-	close(imsg_fds[1]);
-	pack->privsep_child->imsg_fd = imsg_fds[0];
-	pack->privsep_child->pid = pid;
-	imsg_init(ibuf, imsg_fds[0]);
-	pack->privsep_child->ibuf = ibuf;
-
-	err = got_privsep_init_pack_child(ibuf, pack, packidx);
-	if (err) {
-		const struct got_error *child_err;
-		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
-		child_err = got_privsep_wait_for_child(
-		    pack->privsep_child->pid);
-		if (child_err && err == NULL)
-			err = child_err;
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-		return err;
-	}
-
-done:
-	if (err) {
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-	} else
-		err = request_packed_object(obj, pack, idx, id);
-	return err;
-}
-
-static const struct got_error *
 request_commit(struct got_commit_object **commit, struct got_repository *repo,
     int fd)
 {
@@ -1186,89 +1298,6 @@ request_commit(struct got_commit_object **commit, struct got_repository *repo,
 	return got_privsep_recv_commit(commit, ibuf);
 }
 
-static const struct got_error *
-request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
-    int pack_idx, struct got_object_id *id)
-{
-	const struct got_error *err = NULL;
-
-	err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
-	    pack_idx);
-	if (err)
-		return err;
-
-	return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
-}
-
-const struct got_error *
-got_object_read_packed_commit_privsep(struct got_commit_object **commit,
-    struct got_pack *pack, struct got_packidx *packidx, int idx,
-    struct got_object_id *id)
-{
-	const struct got_error *err = NULL;
-	int imsg_fds[2];
-	pid_t pid;
-	struct imsgbuf *ibuf;
-
-	if (pack->privsep_child)
-		return request_packed_commit(commit, pack, idx, id);
-
-	ibuf = calloc(1, sizeof(*ibuf));
-	if (ibuf == NULL)
-		return got_error_from_errno();
-
-	pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
-	if (pack->privsep_child == NULL) {
-		err = got_error_from_errno();
-		free(ibuf);
-		return err;
-	}
-
-	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
-		err = got_error_from_errno();
-		goto done;
-	}
-
-	pid = fork();
-	if (pid == -1) {
-		err = got_error_from_errno();
-		goto done;
-	} else if (pid == 0) {
-		exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
-		    pack->path_packfile);
-		/* not reached */
-	}
-
-	close(imsg_fds[1]);
-	pack->privsep_child->imsg_fd = imsg_fds[0];
-	pack->privsep_child->pid = pid;
-	imsg_init(ibuf, imsg_fds[0]);
-	pack->privsep_child->ibuf = ibuf;
-
-	err = got_privsep_init_pack_child(ibuf, pack, packidx);
-	if (err) {
-		const struct got_error *child_err;
-		err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
-		child_err = got_privsep_wait_for_child(
-		    pack->privsep_child->pid);
-		if (child_err && err == NULL)
-			err = child_err;
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-		return err;
-	}
-
-done:
-	if (err) {
-		free(ibuf);
-		free(pack->privsep_child);
-		pack->privsep_child = NULL;
-	} else
-		err = request_packed_commit(commit, pack, idx, id);
-	return err;
-}
-
 const struct got_error *
 got_object_read_commit_privsep(struct got_commit_object **commit,
     int obj_fd, struct got_repository *repo)
@@ -1361,19 +1390,6 @@ got_object_read_tree_privsep(struct got_tree_object **tree,
 	return request_tree(tree, repo, obj, obj_fd);
 }
 
-const struct got_error *
-got_object_read_packed_tree_privsep(struct got_tree_object **tree,
-    struct got_object *obj, struct got_pack *pack)
-{
-	const struct got_error *err = NULL;
-
-	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
-	if (err)
-		return err;
-
-	return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
-}
-
 static const struct got_error *
 request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
 {
@@ -1460,19 +1476,6 @@ request_tag(struct got_tag_object **tag, struct got_repository *repo,
 }
 
 const struct got_error *
-got_object_read_packed_tag_privsep(struct got_tag_object **tag,
-    struct got_object *obj, struct got_pack *pack)
-{
-	const struct got_error *err = NULL;
-
-	err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
-	if (err)
-		return err;
-
-	return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
-}
-
-const struct got_error *
 got_object_read_tag_privsep(struct got_tag_object **tag,
     struct got_object *obj, int obj_fd, struct got_repository *repo)
 {