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
)
23 const char *sent_argv
[MAX_ARGS
];
24 const char *arg_cmd
= "argument ";
31 usage(upload_archive_usage
);
33 if (strlen(argv
[1]) > sizeof(buf
))
34 die("insanely long repository name");
36 strcpy(buf
, argv
[1]); /* enter-repo smudges its argument */
38 if (!enter_repo(buf
, 0))
39 die("not a git archive");
41 /* put received options in sent_argv[] */
43 sent_argv
[0] = "git-upload-archive";
45 /* This will die if not enough free space in buf */
46 len
= packet_read_line(0, p
, (buf
+ sizeof buf
) - p
);
48 break; /* got a flush */
49 if (sent_argc
> MAX_ARGS
- 2)
50 die("Too many options (>29)");
52 if (p
[len
-1] == '\n') {
55 if (len
< strlen(arg_cmd
) ||
56 strncmp(arg_cmd
, p
, strlen(arg_cmd
)))
57 die("'argument' token or flush expected");
59 len
-= strlen(arg_cmd
);
60 memmove(p
, p
+ strlen(arg_cmd
), len
);
61 sent_argv
[sent_argc
++] = p
;
65 sent_argv
[sent_argc
] = NULL
;
67 /* parse all options sent by the client */
68 treeish_idx
= parse_archive_args(sent_argc
, sent_argv
, &ar
);
70 parse_treeish_arg(sent_argv
+ treeish_idx
, &ar
.args
, prefix
);
71 parse_pathspec_arg(sent_argv
+ treeish_idx
+ 1, &ar
.args
);
73 return ar
.write_archive(&ar
.args
);
76 static void error_clnt(const char *fmt
, ...)
82 va_start(params
, fmt
);
83 len
= vsprintf(buf
, fmt
, params
);
85 send_sideband(1, 3, buf
, len
, LARGE_PACKET_MAX
);
86 die("sent error to the client: %s", buf
);
89 static void process_input(int child_fd
, int band
)
92 ssize_t sz
= read(child_fd
, buf
, sizeof(buf
));
94 if (errno
!= EAGAIN
&& errno
!= EINTR
)
95 error_clnt("read error: %s\n", strerror(errno
));
98 send_sideband(1, band
, buf
, sz
, LARGE_PACKET_MAX
);
101 int cmd_upload_archive(int argc
, const char **argv
, const char *prefix
)
106 * Set up sideband subprocess.
108 * We (parent) monitor and read from child, sending its fd#1 and fd#2
109 * multiplexed out to our fd#1. If the child dies, we tell the other
110 * end over channel #3.
112 if (pipe(fd1
) < 0 || pipe(fd2
) < 0) {
114 packet_write(1, "NACK pipe failed on the remote side\n");
115 die("upload-archive: %s", strerror(err
));
120 packet_write(1, "NACK fork failed on the remote side\n");
121 die("upload-archive: %s", strerror(err
));
124 /* child - connect fd#1 and fd#2 to the pipe */
127 close(fd1
[1]); close(fd2
[1]);
128 close(fd1
[0]); close(fd2
[0]); /* we do not read from pipe */
130 exit(run_upload_archive(argc
, argv
, prefix
));
133 /* parent - read from child, multiplex and send out to fd#1 */
134 close(fd1
[1]); close(fd2
[1]); /* we do not write to pipe */
135 packet_write(1, "ACK\n");
139 struct pollfd pfd
[2];
143 pfd
[0].events
= POLLIN
;
145 pfd
[1].events
= POLLIN
;
146 if (poll(pfd
, 2, -1) < 0) {
147 if (errno
!= EINTR
) {
148 error("poll failed resuming: %s",
154 if (pfd
[0].revents
& POLLIN
)
155 /* Data stream ready */
156 process_input(pfd
[0].fd
, 1);
157 if (pfd
[1].revents
& POLLIN
)
158 /* Status stream ready */
159 process_input(pfd
[1].fd
, 2);
160 /* Always finish to read data when available */
161 if ((pfd
[0].revents
| pfd
[1].revents
) & POLLIN
)
164 if (waitpid(writer
, &status
, 0) < 0)
165 error_clnt("%s", lostchild
);
166 else if (!WIFEXITED(status
) || WEXITSTATUS(status
) > 0)
167 error_clnt("%s", deadchild
);