upload-archive: use start_command instead of fork

The POSIX-function fork is not supported on Windows. Use our start_command API instead. As this is the last call-site that depends on the fork-stub in compat/mingw.h, remove that as well. Add an undocumented flag to git-archive that tells it that the action originated from a remote, so features can be disabled. Thanks to Jeff King for work on this part. Remove the NOT_MINGW-prereq for t5000, as git-archive --remote now works. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Faye-Lund committed Oct 24, 2011 at 18:02 UTC c09cd77ea2fe3580b33918a99fe138d239ac2aaf
4 files changed +31 -55
builtin/archive.c
+5 -1
@@ -87,6 +87,7 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
87 const char *exec = "git-upload-archive";
88 const char *output = NULL;
89 const char *remote = NULL;
90 + int is_remote = 0;
91 struct option local_opts[] = {
92 OPT_STRING('o', "output", &output, "file",
93 "write the archive to this file"),
@@ -94,6 +95,9 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
95 "retrieve the archive from remote repository <repo>"),
96 OPT_STRING(0, "exec", &exec, "cmd",
97 "path to the remote git-upload-archive command"),
98 + { OPTION_BOOLEAN, 0, "remote-request", &is_remote, NULL,
99 + "indicate we are serving a remote request",
100 + PARSE_OPT_NOARG | PARSE_OPT_HIDDEN },
101 OPT_END()
102 };
103
@@ -108,5 +112,5 @@ int cmd_archive(int argc, const char **argv, const char *prefix)
112
113 setvbuf(stderr, NULL, _IOLBF, BUFSIZ);
114
111 - return write_archive(argc, argv, prefix, 1, output, 0);
115 + return write_archive(argc, argv, prefix, 1, output, is_remote);
116 }
builtin/upload-archive.c
+21 -47
@@ -6,6 +6,7 @@
6 #include "archive.h"
7 #include "pkt-line.h"
8 #include "sideband.h"
9 +#include "run-command.h"
10
11 static const char upload_archive_usage[] =
12 "git upload-archive <repo>";
@@ -18,28 +19,17 @@ static const char lostchild[] =
19
20 #define MAX_ARGS (64)
21
21 -static int run_upload_archive(int argc, const char **argv, const char *prefix)
22 +static void prepare_argv(const char **sent_argv, const char **argv)
23 {
23 - const char *sent_argv[MAX_ARGS];
24 const char *arg_cmd = "argument ";
25 char *p, buf[4096];
26 int sent_argc;
27 int len;
28
29 - if (argc != 2)
30 - usage(upload_archive_usage);
31 -
32 - if (strlen(argv[1]) + 1 > sizeof(buf))
33 - die("insanely long repository name");
34 -
35 - strcpy(buf, argv[1]); /* enter-repo smudges its argument */
36 -
37 - if (!enter_repo(buf, 0))
38 - die("'%s' does not appear to be a git repository", buf);
39 -
29 /* put received options in sent_argv[] */
41 - sent_argc = 1;
42 - sent_argv[0] = "git-upload-archive";
30 + sent_argc = 2;
31 + sent_argv[0] = "archive";
32 + sent_argv[1] = "--remote-request";
33 for (p = buf;;) {
34 /* This will die if not enough free space in buf */
35 len = packet_read_line(0, p, (buf + sizeof buf) - p);
@@ -62,9 +52,6 @@ static int run_upload_archive(int argc, const char **argv, const char *prefix)
52 *p++ = 0;
53 }
54 sent_argv[sent_argc] = NULL;
65 -
66 - /* parse all options sent by the client */
67 - return write_archive(sent_argc, sent_argv, prefix, 0, NULL, 1);
55 }
56
57 __attribute__((format (printf, 1, 2)))
@@ -96,38 +83,25 @@ static ssize_t process_input(int child_fd, int band)
83
84 int cmd_upload_archive(int argc, const char **argv, const char *prefix)
85 {
99 - pid_t writer;
100 - int fd1[2], fd2[2];
101 - /*
102 - * Set up sideband subprocess.
103 - *
104 - * We (parent) monitor and read from child, sending its fd#1 and fd#2
105 - * multiplexed out to our fd#1. If the child dies, we tell the other
106 - * end over channel #3.
107 - */
108 - if (pipe(fd1) < 0 || pipe(fd2) < 0) {
109 - int err = errno;
110 - packet_write(1, "NACK pipe failed on the remote side\n");
111 - die("upload-archive: %s", strerror(err));
112 - }
113 - writer = fork();
114 - if (writer < 0) {
86 + const char *sent_argv[MAX_ARGS];
87 + struct child_process cld = { sent_argv };
88 + cld.out = cld.err = -1;
89 + cld.git_cmd = 1;
90 +
91 + if (argc != 2)
92 + usage(upload_archive_usage);
93 +
94 + if (!enter_repo(argv[1], 0))
95 + die("'%s' does not appear to be a git repository", argv[1]);
96 +
97 + prepare_argv(sent_argv, argv);
98 + if (start_command(&cld)) {
99 int err = errno;
100 packet_write(1, "NACK fork failed on the remote side\n");
101 die("upload-archive: %s", strerror(err));
102 }
119 - if (!writer) {
120 - /* child - connect fd#1 and fd#2 to the pipe */
121 - dup2(fd1[1], 1);
122 - dup2(fd2[1], 2);
123 - close(fd1[1]); close(fd2[1]);
124 - close(fd1[0]); close(fd2[0]); /* we do not read from pipe */
125 -
126 - exit(run_upload_archive(argc, argv, prefix));
127 - }
103
104 /* parent - read from child, multiplex and send out to fd#1 */
130 - close(fd1[1]); close(fd2[1]); /* we do not write to pipe */
105 packet_write(1, "ACK\n");
106 packet_flush(1);
107
@@ -135,9 +109,9 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
109 struct pollfd pfd[2];
110 int status;
111
138 - pfd[0].fd = fd1[0];
112 + pfd[0].fd = cld.out;
113 pfd[0].events = POLLIN;
140 - pfd[1].fd = fd2[0];
114 + pfd[1].fd = cld.err;
115 pfd[1].events = POLLIN;
116 if (poll(pfd, 2, -1) < 0) {
117 if (errno != EINTR) {
@@ -156,7 +130,7 @@ int cmd_upload_archive(int argc, const char **argv, const char *prefix)
130 if (process_input(pfd[0].fd, 1))
131 continue;
132
159 - if (waitpid(writer, &status, 0) < 0)
133 + if (waitpid(cld.pid, &status, 0) < 0)
134 error_clnt("%s", lostchild);
135 else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0)
136 error_clnt("%s", deadchild);
compat/mingw.h
-2
@@ -85,8 +85,6 @@ static inline int symlink(const char *oldpath, const char *newpath)
85 { errno = ENOSYS; return -1; }
86 static inline int fchmod(int fildes, mode_t mode)
87 { errno = ENOSYS; return -1; }
88 -static inline pid_t fork(void)
89 -{ errno = ENOSYS; return -1; }
88 static inline unsigned int alarm(unsigned int seconds)
89 { return 0; }
90 static inline int fsync(int fd)
t/t5000-tar-tree.sh
+5 -5
@@ -96,7 +96,7 @@ test_expect_success 'git archive with --output' \
96 'git archive --output=b4.tar HEAD &&
97 test_cmp b.tar b4.tar'
98
99 -test_expect_success NOT_MINGW 'git archive --remote' \
99 +test_expect_success 'git archive --remote' \
100 'git archive --remote=. HEAD >b5.tar &&
101 test_cmp b.tar b5.tar'
102
@@ -266,7 +266,7 @@ test_expect_success 'archive --list mentions user filter' '
266 grep "^bar\$" output
267 '
268
269 -test_expect_success NOT_MINGW 'archive --list shows only enabled remote filters' '
269 +test_expect_success 'archive --list shows only enabled remote filters' '
270 git archive --list --remote=. >output &&
271 ! grep "^tar\.foo\$" output &&
272 grep "^bar\$" output
@@ -298,7 +298,7 @@ test_expect_success 'extension matching requires dot' '
298 test_cmp b.tar config-implicittar.foo
299 '
300
301 -test_expect_success NOT_MINGW 'only enabled filters are available remotely' '
301 +test_expect_success 'only enabled filters are available remotely' '
302 test_must_fail git archive --remote=. --format=tar.foo HEAD \
303 >remote.tar.foo &&
304 git archive --remote=. --format=bar >remote.bar HEAD &&
@@ -341,12 +341,12 @@ test_expect_success GZIP,GUNZIP 'extract tgz file' '
341 test_cmp b.tar j.tar
342 '
343
344 -test_expect_success GZIP,NOT_MINGW 'remote tar.gz is allowed by default' '
344 +test_expect_success GZIP 'remote tar.gz is allowed by default' '
345 git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
346 test_cmp j.tgz remote.tar.gz
347 '
348
349 -test_expect_success GZIP,NOT_MINGW 'remote tar.gz can be disabled' '
349 +test_expect_success GZIP 'remote tar.gz can be disabled' '
350 git config tar.tar.gz.remote false &&
351 test_must_fail git archive --remote=. --format=tar.gz HEAD \
352 >remote.tar.gz