@matheusbsbs / matheus-bagliano / commits / b1daf300d0
Fork 0

Replace fork_with_pipe in bundle with run_command

Now that the run_command family supports all of the redirection modes needed by builtin-bundle, we can use those functions rather than the underlying POSIX primitives. This should help to make the bundle command slightly more portable to other systems, like Windows. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Mar 12, 2007 at 19:00 UTC b1daf300d0f56a01e5800a3d989b4cb150d99140
1 file changed +30 -99
builtin-bundle.c
+30 -99
@@ -4,7 +4,7 @@
4 #include "diff.h"
5 #include "revision.h"
6 #include "list-objects.h"
7 -#include "exec_cmd.h"
7 +#include "run-command.h"
8
9 /*
10 * Basic handler for bundle files to connect repositories via sneakernet.
@@ -99,67 +99,6 @@ static int read_header(const char *path, struct bundle_header *header) {
99 return fd;
100 }
101
102 -/* if in && *in >= 0, take that as input file descriptor instead */
103 -static int fork_with_pipe(const char **argv, int *in, int *out)
104 -{
105 - int needs_in, needs_out;
106 - int fdin[2], fdout[2], pid;
107 -
108 - needs_in = in && *in < 0;
109 - if (needs_in) {
110 - if (pipe(fdin) < 0)
111 - return error("could not setup pipe");
112 - *in = fdin[1];
113 - }
114 -
115 - needs_out = out && *out < 0;
116 - if (needs_out) {
117 - if (pipe(fdout) < 0)
118 - return error("could not setup pipe");
119 - *out = fdout[0];
120 - }
121 -
122 - if ((pid = fork()) < 0) {
123 - if (needs_in) {
124 - close(fdin[0]);
125 - close(fdin[1]);
126 - }
127 - if (needs_out) {
128 - close(fdout[0]);
129 - close(fdout[1]);
130 - }
131 - return error("could not fork");
132 - }
133 - if (!pid) {
134 - if (needs_in) {
135 - dup2(fdin[0], 0);
136 - close(fdin[0]);
137 - close(fdin[1]);
138 - } else if (in) {
139 - dup2(*in, 0);
140 - close(*in);
141 - }
142 - if (needs_out) {
143 - dup2(fdout[1], 1);
144 - close(fdout[0]);
145 - close(fdout[1]);
146 - } else if (out) {
147 - dup2(*out, 1);
148 - close(*out);
149 - }
150 - exit(execv_git_cmd(argv));
151 - }
152 - if (needs_in)
153 - close(fdin[0]);
154 - else if (in)
155 - close(*in);
156 - if (needs_out)
157 - close(fdout[1]);
158 - else if (out)
159 - close(*out);
160 - return pid;
161 -}
162 -
102 static int list_refs(struct ref_list *r, int argc, const char **argv)
103 {
104 int i;
@@ -263,9 +202,10 @@ static int create_bundle(struct bundle_header *header, const char *path,
202 int bundle_fd = -1;
203 const char **argv_boundary = xmalloc((argc + 4) * sizeof(const char *));
204 const char **argv_pack = xmalloc(5 * sizeof(const char *));
266 - int pid, in, out, i, status, ref_count = 0;
205 + int i, ref_count = 0;
206 char buffer[1024];
207 struct rev_info revs;
208 + struct child_process rls;
209
210 bundle_fd = (!strcmp(path, "-") ? 1 :
211 open(path, O_CREAT | O_EXCL | O_WRONLY, 0666));
@@ -285,11 +225,13 @@ static int create_bundle(struct bundle_header *header, const char *path,
225 argv_boundary[1] = "--boundary";
226 argv_boundary[2] = "--pretty=oneline";
227 argv_boundary[argc + 2] = NULL;
288 - out = -1;
289 - pid = fork_with_pipe(argv_boundary, NULL, &out);
290 - if (pid < 0)
228 + memset(&rls, 0, sizeof(rls));
229 + rls.argv = argv_boundary;
230 + rls.out = -1;
231 + rls.git_cmd = 1;
232 + if (start_command(&rls))
233 return -1;
292 - while ((i = read_string(out, buffer, sizeof(buffer))) > 0) {
234 + while ((i = read_string(rls.out, buffer, sizeof(buffer))) > 0) {
235 unsigned char sha1[20];
236 if (buffer[0] == '-') {
237 write_or_die(bundle_fd, buffer, i);
@@ -303,11 +245,8 @@ static int create_bundle(struct bundle_header *header, const char *path,
245 object->flags |= SHOWN;
246 }
247 }
306 - while ((i = waitpid(pid, &status, 0)) < 0)
307 - if (errno != EINTR)
308 - return error("rev-list died");
309 - if (!WIFEXITED(status) || WEXITSTATUS(status))
310 - return error("rev-list died %d", WEXITSTATUS(status));
248 + if (finish_command(&rls))
249 + return error("rev-list died");
250
251 /* write references */
252 argc = setup_revisions(argc, argv, &revs, NULL);
@@ -352,26 +291,23 @@ static int create_bundle(struct bundle_header *header, const char *path,
291 argv_pack[2] = "--stdout";
292 argv_pack[3] = "--thin";
293 argv_pack[4] = NULL;
355 - in = -1;
356 - out = bundle_fd;
357 - pid = fork_with_pipe(argv_pack, &in, &out);
358 - if (pid < 0)
294 + memset(&rls, 0, sizeof(rls));
295 + rls.argv = argv_pack;
296 + rls.in = -1;
297 + rls.out = bundle_fd;
298 + rls.git_cmd = 1;
299 + if (start_command(&rls))
300 return error("Could not spawn pack-objects");
301 for (i = 0; i < revs.pending.nr; i++) {
302 struct object *object = revs.pending.objects[i].item;
303 if (object->flags & UNINTERESTING)
363 - write(in, "^", 1);
364 - write(in, sha1_to_hex(object->sha1), 40);
365 - write(in, "\n", 1);
304 + write(rls.in, "^", 1);
305 + write(rls.in, sha1_to_hex(object->sha1), 40);
306 + write(rls.in, "\n", 1);
307 }
367 - close(in);
368 - while (waitpid(pid, &status, 0) < 0)
369 - if (errno != EINTR)
370 - return -1;
371 - if (!WIFEXITED(status) || WEXITSTATUS(status))
308 + if (finish_command(&rls))
309 return error ("pack-objects died");
373 -
374 - return status;
310 + return 0;
311 }
312
313 static int unbundle(struct bundle_header *header, int bundle_fd,
@@ -379,22 +315,17 @@ static int unbundle(struct bundle_header *header, int bundle_fd,
315 {
316 const char *argv_index_pack[] = {"index-pack",
317 "--fix-thin", "--stdin", NULL};
382 - int pid, status, dev_null;
318 + struct child_process ip;
319
320 if (verify_bundle(header, 0))
321 return -1;
386 - dev_null = open("/dev/null", O_WRONLY);
387 - if (dev_null < 0)
388 - return error("Could not open /dev/null");
389 - pid = fork_with_pipe(argv_index_pack, &bundle_fd, &dev_null);
390 - if (pid < 0)
391 - return error("Could not spawn index-pack");
392 - while (waitpid(pid, &status, 0) < 0)
393 - if (errno != EINTR)
394 - return error("index-pack died");
395 - if (!WIFEXITED(status) || WEXITSTATUS(status))
396 - return error("index-pack exited with status %d",
397 - WEXITSTATUS(status));
322 + memset(&ip, 0, sizeof(ip));
323 + ip.argv = argv_index_pack;
324 + ip.in = bundle_fd;
325 + ip.no_stdout = 1;
326 + ip.git_cmd = 1;
327 + if (run_command(&ip))
328 + return error("index-pack died");
329 return list_heads(header, argc, argv);
330 }
331