2 * Copyright (c) 2006 Franck Bui-Huu
9 #include "run-command.h"
11 static const char upload_archive_usage
[] =
12 "git upload-archive <repo>";
14 static const char deadchild
[] =
15 "git upload-archive: archiver died with error";
19 int cmd_upload_archive_writer(int argc
, const char **argv
, const char *prefix
)
21 const char *sent_argv
[MAX_ARGS
];
22 const char *arg_cmd
= "argument ";
28 usage(upload_archive_usage
);
30 if (strlen(argv
[1]) + 1 > sizeof(buf
))
31 die("insanely long repository name");
33 strcpy(buf
, argv
[1]); /* enter-repo smudges its argument */
35 if (!enter_repo(buf
, 0))
36 die("'%s' does not appear to be a git repository", buf
);
38 /* put received options in sent_argv[] */
40 sent_argv
[0] = "git-upload-archive";
42 /* This will die if not enough free space in buf */
43 len
= packet_read_line(0, p
, (buf
+ sizeof buf
) - p
);
45 break; /* got a flush */
46 if (sent_argc
> MAX_ARGS
- 2)
47 die("Too many options (>%d)", MAX_ARGS
- 2);
49 if (p
[len
-1] == '\n') {
52 if (len
< strlen(arg_cmd
) ||
53 strncmp(arg_cmd
, p
, strlen(arg_cmd
)))
54 die("'argument' token or flush expected");
56 len
-= strlen(arg_cmd
);
57 memmove(p
, p
+ strlen(arg_cmd
), len
);
58 sent_argv
[sent_argc
++] = p
;
62 sent_argv
[sent_argc
] = NULL
;
64 /* parse all options sent by the client */
65 return write_archive(sent_argc
, sent_argv
, prefix
, 0, NULL
, 1);
68 __attribute__((format (printf
, 1, 2)))
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 ssize_t
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
);
95 int cmd_upload_archive(int argc
, const char **argv
, const char *prefix
)
97 struct child_process writer
= { argv
};
100 * Set up sideband subprocess.
102 * We (parent) monitor and read from child, sending its fd#1 and fd#2
103 * multiplexed out to our fd#1. If the child dies, we tell the other
104 * end over channel #3.
106 argv
[0] = "upload-archive--writer";
107 writer
.out
= writer
.err
= -1;
109 if (start_command(&writer
)) {
111 packet_write(1, "NACK unable to spawn subprocess\n");
112 die("upload-archive: %s", strerror(err
));
115 packet_write(1, "ACK\n");
119 struct pollfd pfd
[2];
121 pfd
[0].fd
= writer
.out
;
122 pfd
[0].events
= POLLIN
;
123 pfd
[1].fd
= writer
.err
;
124 pfd
[1].events
= POLLIN
;
125 if (poll(pfd
, 2, -1) < 0) {
126 if (errno
!= EINTR
) {
127 error("poll failed resuming: %s",
133 if (pfd
[1].revents
& POLLIN
)
134 /* Status stream ready */
135 if (process_input(pfd
[1].fd
, 2))
137 if (pfd
[0].revents
& POLLIN
)
138 /* Data stream ready */
139 if (process_input(pfd
[0].fd
, 1))
142 if (finish_command(&writer
))
143 error_clnt("%s", deadchild
);