2 * Copyright (c) 2006 Franck Bui-Huu
10 static const char upload_archive_usage
[] =
11 "git upload-archive <repo>";
13 static const char deadchild
[] =
14 "git upload-archive: archiver died with error";
16 static const char lostchild
[] =
17 "git upload-archive: archiver process was lost";
20 static int run_upload_archive(int argc
, const char **argv
, const char *prefix
)
22 const char *sent_argv
[MAX_ARGS
];
23 const char *arg_cmd
= "argument ";
29 usage(upload_archive_usage
);
31 if (strlen(argv
[1]) + 1 > sizeof(buf
))
32 die("insanely long repository name");
34 strcpy(buf
, argv
[1]); /* enter-repo smudges its argument */
36 if (!enter_repo(buf
, 0))
37 die("not a git archive");
39 /* put received options in sent_argv[] */
41 sent_argv
[0] = "git-upload-archive";
43 /* This will die if not enough free space in buf */
44 len
= packet_read_line(0, p
, (buf
+ sizeof buf
) - p
);
46 break; /* got a flush */
47 if (sent_argc
> MAX_ARGS
- 2)
48 die("Too many options (>29)");
50 if (p
[len
-1] == '\n') {
53 if (len
< strlen(arg_cmd
) ||
54 strncmp(arg_cmd
, p
, strlen(arg_cmd
)))
55 die("'argument' token or flush expected");
57 len
-= strlen(arg_cmd
);
58 memmove(p
, p
+ strlen(arg_cmd
), len
);
59 sent_argv
[sent_argc
++] = p
;
63 sent_argv
[sent_argc
] = NULL
;
65 /* parse all options sent by the client */
66 return write_archive(sent_argc
, sent_argv
, prefix
, 0);
69 static void error_clnt(const char *fmt
, ...)
75 va_start(params
, fmt
);
76 len
= vsprintf(buf
, fmt
, params
);
78 send_sideband(1, 3, buf
, len
, LARGE_PACKET_MAX
);
79 die("sent error to the client: %s", buf
);
82 static void process_input(int child_fd
, int band
)
85 ssize_t sz
= read(child_fd
, buf
, sizeof(buf
));
87 if (errno
!= EAGAIN
&& errno
!= EINTR
)
88 error_clnt("read error: %s\n", strerror(errno
));
91 send_sideband(1, band
, buf
, sz
, LARGE_PACKET_MAX
);
94 int cmd_upload_archive(int argc
, const char **argv
, const char *prefix
)
99 * Set up sideband subprocess.
101 * We (parent) monitor and read from child, sending its fd#1 and fd#2
102 * multiplexed out to our fd#1. If the child dies, we tell the other
103 * end over channel #3.
105 if (pipe(fd1
) < 0 || pipe(fd2
) < 0) {
107 packet_write(1, "NACK pipe failed on the remote side\n");
108 die("upload-archive: %s", strerror(err
));
113 packet_write(1, "NACK fork failed on the remote side\n");
114 die("upload-archive: %s", strerror(err
));
117 /* child - connect fd#1 and fd#2 to the pipe */
120 close(fd1
[1]); close(fd2
[1]);
121 close(fd1
[0]); close(fd2
[0]); /* we do not read from pipe */
123 exit(run_upload_archive(argc
, argv
, prefix
));
126 /* parent - read from child, multiplex and send out to fd#1 */
127 close(fd1
[1]); close(fd2
[1]); /* we do not write to pipe */
128 packet_write(1, "ACK\n");
132 struct pollfd pfd
[2];
136 pfd
[0].events
= POLLIN
;
138 pfd
[1].events
= POLLIN
;
139 if (poll(pfd
, 2, -1) < 0) {
140 if (errno
!= EINTR
) {
141 error("poll failed resuming: %s",
147 if (pfd
[0].revents
& POLLIN
)
148 /* Data stream ready */
149 process_input(pfd
[0].fd
, 1);
150 if (pfd
[1].revents
& POLLIN
)
151 /* Status stream ready */
152 process_input(pfd
[1].fd
, 2);
153 /* Always finish to read data when available */
154 if ((pfd
[0].revents
| pfd
[1].revents
) & POLLIN
)
157 if (waitpid(writer
, &status
, 0) < 0)
158 error_clnt("%s", lostchild
);
159 else if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0)
160 error_clnt("%s", deadchild
);