]>
Commit | Line | Data |
---|---|---|
39345a21 FBH |
1 | /* |
2 | * Copyright (c) 2006 Franck Bui-Huu | |
3 | */ | |
39345a21 FBH |
4 | #include "cache.h" |
5 | #include "builtin.h" | |
6 | #include "archive.h" | |
7 | #include "pkt-line.h" | |
23d6d112 | 8 | #include "sideband.h" |
c09cd77e | 9 | #include "run-command.h" |
39345a21 FBH |
10 | |
11 | static const char upload_archive_usage[] = | |
1b1dd23f | 12 | "git upload-archive <repo>"; |
39345a21 | 13 | |
23d6d112 | 14 | static const char deadchild[] = |
1b1dd23f | 15 | "git upload-archive: archiver died with error"; |
39345a21 | 16 | |
d3788e19 | 17 | static const char lostchild[] = |
1b1dd23f | 18 | "git upload-archive: archiver process was lost"; |
d3788e19 | 19 | |
7f4d0511 | 20 | #define MAX_ARGS (64) |
23d6d112 | 21 | |
c09cd77e | 22 | static void prepare_argv(const char **sent_argv, const char **argv) |
39345a21 | 23 | { |
39345a21 FBH |
24 | const char *arg_cmd = "argument "; |
25 | char *p, buf[4096]; | |
39345a21 FBH |
26 | int sent_argc; |
27 | int len; | |
28 | ||
39345a21 | 29 | /* put received options in sent_argv[] */ |
c09cd77e EFL |
30 | sent_argc = 2; |
31 | sent_argv[0] = "archive"; | |
32 | sent_argv[1] = "--remote-request"; | |
39345a21 FBH |
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); | |
36 | if (len == 0) | |
37 | break; /* got a flush */ | |
38 | if (sent_argc > MAX_ARGS - 2) | |
7f4d0511 | 39 | die("Too many options (>%d)", MAX_ARGS - 2); |
39345a21 FBH |
40 | |
41 | if (p[len-1] == '\n') { | |
42 | p[--len] = 0; | |
43 | } | |
44 | if (len < strlen(arg_cmd) || | |
45 | strncmp(arg_cmd, p, strlen(arg_cmd))) | |
46 | die("'argument' token or flush expected"); | |
47 | ||
48 | len -= strlen(arg_cmd); | |
49 | memmove(p, p + strlen(arg_cmd), len); | |
50 | sent_argv[sent_argc++] = p; | |
51 | p += len; | |
52 | *p++ = 0; | |
53 | } | |
54 | sent_argv[sent_argc] = NULL; | |
23d6d112 JH |
55 | } |
56 | ||
28bea9e5 | 57 | __attribute__((format (printf, 1, 2))) |
d3788e19 JH |
58 | static void error_clnt(const char *fmt, ...) |
59 | { | |
60 | char buf[1024]; | |
61 | va_list params; | |
62 | int len; | |
63 | ||
64 | va_start(params, fmt); | |
65 | len = vsprintf(buf, fmt, params); | |
66 | va_end(params); | |
67 | send_sideband(1, 3, buf, len, LARGE_PACKET_MAX); | |
68 | die("sent error to the client: %s", buf); | |
69 | } | |
70 | ||
1b19fa46 | 71 | static ssize_t process_input(int child_fd, int band) |
d3788e19 JH |
72 | { |
73 | char buf[16384]; | |
74 | ssize_t sz = read(child_fd, buf, sizeof(buf)); | |
75 | if (sz < 0) { | |
93d26e4c | 76 | if (errno != EAGAIN && errno != EINTR) |
d3788e19 | 77 | error_clnt("read error: %s\n", strerror(errno)); |
1b19fa46 | 78 | return sz; |
d3788e19 JH |
79 | } |
80 | send_sideband(1, band, buf, sz, LARGE_PACKET_MAX); | |
1b19fa46 | 81 | return sz; |
d3788e19 JH |
82 | } |
83 | ||
23d6d112 JH |
84 | int cmd_upload_archive(int argc, const char **argv, const char *prefix) |
85 | { | |
c09cd77e EFL |
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)) { | |
23d6d112 JH |
99 | int err = errno; |
100 | packet_write(1, "NACK fork failed on the remote side\n"); | |
101 | die("upload-archive: %s", strerror(err)); | |
102 | } | |
23d6d112 JH |
103 | |
104 | /* parent - read from child, multiplex and send out to fd#1 */ | |
39345a21 FBH |
105 | packet_write(1, "ACK\n"); |
106 | packet_flush(1); | |
107 | ||
23d6d112 JH |
108 | while (1) { |
109 | struct pollfd pfd[2]; | |
23d6d112 JH |
110 | int status; |
111 | ||
c09cd77e | 112 | pfd[0].fd = cld.out; |
23d6d112 | 113 | pfd[0].events = POLLIN; |
c09cd77e | 114 | pfd[1].fd = cld.err; |
23d6d112 JH |
115 | pfd[1].events = POLLIN; |
116 | if (poll(pfd, 2, -1) < 0) { | |
117 | if (errno != EINTR) { | |
118 | error("poll failed resuming: %s", | |
119 | strerror(errno)); | |
120 | sleep(1); | |
121 | } | |
122 | continue; | |
123 | } | |
d3788e19 | 124 | if (pfd[1].revents & POLLIN) |
23d6d112 | 125 | /* Status stream ready */ |
6b59f51b NP |
126 | if (process_input(pfd[1].fd, 2)) |
127 | continue; | |
128 | if (pfd[0].revents & POLLIN) | |
129 | /* Data stream ready */ | |
130 | if (process_input(pfd[0].fd, 1)) | |
131 | continue; | |
d3788e19 | 132 | |
c09cd77e | 133 | if (waitpid(cld.pid, &status, 0) < 0) |
d3788e19 JH |
134 | error_clnt("%s", lostchild); |
135 | else if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) | |
136 | error_clnt("%s", deadchild); | |
23d6d112 JH |
137 | packet_flush(1); |
138 | break; | |
139 | } | |
140 | return 0; | |
141 | } |