2 * Copyright (c) 2006 Franck Bui-Huu
9 #include "repository.h"
10 #include "run-command.h"
13 static const char upload_archive_usage
[] =
14 "git upload-archive <repository>";
16 static const char deadchild
[] =
17 "git upload-archive: archiver died with error";
21 int cmd_upload_archive_writer(int argc
, const char **argv
, const char *prefix
)
23 struct strvec sent_argv
= STRVEC_INIT
;
24 const char *arg_cmd
= "argument ";
27 if (argc
!= 2 || !strcmp(argv
[1], "-h"))
28 usage(upload_archive_usage
);
30 if (!enter_repo(argv
[1], 0))
31 die("'%s' does not appear to be a git repository", argv
[1]);
35 /* put received options in sent_argv[] */
36 strvec_push(&sent_argv
, "git-upload-archive");
38 char *buf
= packet_read_line(0, NULL
);
40 break; /* got a flush */
41 if (sent_argv
.nr
> MAX_ARGS
)
42 die("Too many options (>%d)", MAX_ARGS
- 1);
44 if (!starts_with(buf
, arg_cmd
))
45 die("'argument' token or flush expected");
46 strvec_push(&sent_argv
, buf
+ strlen(arg_cmd
));
49 /* parse all options sent by the client */
50 ret
= write_archive(sent_argv
.nr
, sent_argv
.v
, prefix
,
51 the_repository
, NULL
, 1);
53 strvec_clear(&sent_argv
);
57 __attribute__((format (printf
, 1, 2)))
58 static void error_clnt(const char *fmt
, ...)
60 struct strbuf buf
= STRBUF_INIT
;
63 va_start(params
, fmt
);
64 strbuf_vaddf(&buf
, fmt
, params
);
66 send_sideband(1, 3, buf
.buf
, buf
.len
, LARGE_PACKET_MAX
);
67 die("sent error to the client: %s", buf
.buf
);
70 static ssize_t
process_input(int child_fd
, int band
)
73 ssize_t sz
= read(child_fd
, buf
, sizeof(buf
));
75 if (errno
!= EAGAIN
&& errno
!= EINTR
)
76 error_clnt("read error: %s\n", strerror(errno
));
79 send_sideband(1, band
, buf
, sz
, LARGE_PACKET_MAX
);
83 int cmd_upload_archive(int argc
, const char **argv
, const char *prefix
)
85 struct child_process writer
= CHILD_PROCESS_INIT
;
87 BUG_ON_NON_EMPTY_PREFIX(prefix
);
89 if (argc
== 2 && !strcmp(argv
[1], "-h"))
90 usage(upload_archive_usage
);
93 * Set up sideband subprocess.
95 * We (parent) monitor and read from child, sending its fd#1 and fd#2
96 * multiplexed out to our fd#1. If the child dies, we tell the other
97 * end over channel #3.
99 writer
.out
= writer
.err
= -1;
101 strvec_push(&writer
.args
, "upload-archive--writer");
102 strvec_pushv(&writer
.args
, argv
+ 1);
103 if (start_command(&writer
)) {
105 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
106 die("upload-archive: %s", strerror(err
));
109 packet_write_fmt(1, "ACK\n");
113 struct pollfd pfd
[2];
115 pfd
[0].fd
= writer
.out
;
116 pfd
[0].events
= POLLIN
;
117 pfd
[1].fd
= writer
.err
;
118 pfd
[1].events
= POLLIN
;
119 if (poll(pfd
, 2, -1) < 0) {
120 if (errno
!= EINTR
) {
121 error_errno("poll failed resuming");
126 if (pfd
[1].revents
& POLLIN
)
127 /* Status stream ready */
128 if (process_input(pfd
[1].fd
, 2))
130 if (pfd
[0].revents
& POLLIN
)
131 /* Data stream ready */
132 if (process_input(pfd
[0].fd
, 1))
135 if (finish_command(&writer
))
136 error_clnt("%s", deadchild
);