[PATCH] Trivial tidyups
[git.git] / connect.c
1 #include "cache.h"
2 #include "pkt-line.h"
3 #include "quote.h"
4 #include <sys/wait.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7 #include <arpa/inet.h>
8 #include <netdb.h>
9
10 /*
11  * Read all the refs from the other end
12  */
13 struct ref **get_remote_heads(int in, struct ref **list, int nr_match, char **match)
14 {
15         *list = NULL;
16         for (;;) {
17                 struct ref *ref;
18                 unsigned char old_sha1[20];
19                 static char buffer[1000];
20                 char *name;
21                 int len;
22
23                 len = packet_read_line(in, buffer, sizeof(buffer));
24                 if (!len)
25                         break;
26                 if (buffer[len-1] == '\n')
27                         buffer[--len] = 0;
28
29                 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
30                         die("protocol error: expected sha/ref, got '%s'", buffer);
31                 name = buffer + 41;
32                 if (nr_match && !path_match(name, nr_match, match))
33                         continue;
34                 ref = xmalloc(sizeof(*ref) + len - 40);
35                 memcpy(ref->old_sha1, old_sha1, 20);
36                 memset(ref->new_sha1, 0, 20);
37                 memcpy(ref->name, buffer + 41, len - 40);
38                 ref->next = NULL;
39                 *list = ref;
40                 list = &ref->next;
41         }
42         return list;
43 }
44
45 int get_ack(int fd, unsigned char *result_sha1)
46 {
47         static char line[1000];
48         int len = packet_read_line(fd, line, sizeof(line));
49
50         if (!len)
51                 die("git-fetch-pack: expected ACK/NAK, got EOF");
52         if (line[len-1] == '\n')
53                 line[--len] = 0;
54         if (!strcmp(line, "NAK"))
55                 return 0;
56         if (!strncmp(line, "ACK ", 3)) {
57                 if (!get_sha1_hex(line+4, result_sha1))
58                         return 1;
59         }
60         die("git-fetch_pack: expected ACK/NAK, got '%s'", line);
61 }
62
63 int path_match(const char *path, int nr, char **match)
64 {
65         int i;
66         int pathlen = strlen(path);
67
68         for (i = 0; i < nr; i++) {
69                 char *s = match[i];
70                 int len = strlen(s);
71
72                 if (!len || len > pathlen)
73                         continue;
74                 if (memcmp(path + pathlen - len, s, len))
75                         continue;
76                 if (pathlen > len && path[pathlen - len - 1] != '/')
77                         continue;
78                 *s = 0;
79                 return 1;
80         }
81         return 0;
82 }
83
84 enum protocol {
85         PROTO_LOCAL = 1,
86         PROTO_SSH,
87         PROTO_GIT,
88 };
89
90 static enum protocol get_protocol(const char *name)
91 {
92         if (!strcmp(name, "ssh"))
93                 return PROTO_SSH;
94         if (!strcmp(name, "git"))
95                 return PROTO_GIT;
96         die("I don't handle protocol '%s'", name);
97 }
98
99 #define STR_(s) # s
100 #define STR(s)  STR_(s)
101
102 static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
103 {
104         int sockfd = -1;
105         char *colon, *end;
106         char *port = STR(DEFAULT_GIT_PORT);
107         struct addrinfo hints, *ai0, *ai;
108         int gai;
109
110         if (host[0] == '[') {
111                 end = strchr(host + 1, ']');
112                 if (end) {
113                         *end = 0;
114                         end++;
115                         host++;
116                 } else
117                         end = host;
118         } else
119                 end = host;
120         colon = strchr(end, ':');
121
122         if (colon) {
123                 *colon = 0;
124                 port = colon + 1;
125         }
126
127         memset(&hints, 0, sizeof(hints));
128         hints.ai_socktype = SOCK_STREAM;
129         hints.ai_protocol = IPPROTO_TCP;
130
131         gai = getaddrinfo(host, port, &hints, &ai);
132         if (gai)
133                 die("Unable to look up %s (%s)", host, gai_strerror(gai));
134
135         for (ai0 = ai; ai; ai = ai->ai_next) {
136                 sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
137                 if (sockfd < 0)
138                         continue;
139                 if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
140                         close(sockfd);
141                         sockfd = -1;
142                         continue;
143                 }
144                 break;
145         }
146
147         freeaddrinfo(ai0);
148
149         if (sockfd < 0)
150                 die("unable to connect a socket (%s)", strerror(errno));
151
152         fd[0] = sockfd;
153         fd[1] = sockfd;
154         packet_write(sockfd, "%s %s\n", prog, path);
155         return 0;
156 }
157
158 /*
159  * Yeah, yeah, fixme. Need to pass in the heads etc.
160  */
161 int git_connect(int fd[2], char *url, const char *prog)
162 {
163         char command[1024];
164         char *host, *path;
165         char *colon;
166         int pipefd[2][2];
167         pid_t pid;
168         enum protocol protocol;
169
170         host = NULL;
171         path = url;
172         colon = strchr(url, ':');
173         protocol = PROTO_LOCAL;
174         if (colon) {
175                 *colon = 0;
176                 host = url;
177                 path = colon+1;
178                 protocol = PROTO_SSH;
179                 if (!memcmp(path, "//", 2)) {
180                         char *slash = strchr(path + 2, '/');
181                         if (slash) {
182                                 int nr = slash - path - 2;
183                                 memmove(path, path+2, nr);
184                                 path[nr] = 0;
185                                 protocol = get_protocol(url);
186                                 host = path;
187                                 path = slash;
188                         }
189                 }
190         }
191
192         if (protocol == PROTO_GIT)
193                 return git_tcp_connect(fd, prog, host, path);
194
195         if (pipe(pipefd[0]) < 0 || pipe(pipefd[1]) < 0)
196                 die("unable to create pipe pair for communication");
197         pid = fork();
198         if (!pid) {
199                 snprintf(command, sizeof(command), "%s %s", prog,
200                          sq_quote(path));
201                 dup2(pipefd[1][0], 0);
202                 dup2(pipefd[0][1], 1);
203                 close(pipefd[0][0]);
204                 close(pipefd[0][1]);
205                 close(pipefd[1][0]);
206                 close(pipefd[1][1]);
207                 if (protocol == PROTO_SSH)
208                         execlp("ssh", "ssh", host, command, NULL);
209                 else
210                         execlp("sh", "sh", "-c", command, NULL);
211                 die("exec failed");
212         }               
213         fd[0] = pipefd[0][0];
214         fd[1] = pipefd[1][1];
215         close(pipefd[0][1]);
216         close(pipefd[1][0]);
217         return pid;
218 }
219
220 int finish_connect(pid_t pid)
221 {
222         int ret;
223
224         for (;;) {
225                 ret = waitpid(pid, NULL, 0);
226                 if (!ret)
227                         break;
228                 if (errno != EINTR)
229                         break;
230         }
231         return ret;
232 }