Improve sha1 object file writing.
[git/git.git] / rsh.c
CommitLineData
6eb7ed54
DB
1#include "rsh.h"
2
3#include <string.h>
ec8f8116 4#include <sys/types.h>
6eb7ed54
DB
5#include <sys/socket.h>
6
7#include "cache.h"
8
9#define COMMAND_SIZE 4096
10
11int setup_connection(int *fd_in, int *fd_out, char *remote_prog,
12 char *url, int rmt_argc, char **rmt_argv)
13{
14 char *host;
15 char *path;
16 int sv[2];
17 char command[COMMAND_SIZE];
18 char *posn;
19 int i;
20
21 if (!strcmp(url, "-")) {
22 *fd_in = 0;
23 *fd_out = 1;
24 return 0;
25 }
26
27 host = strstr(url, "//");
28 if (!host) {
29 return error("Bad URL: %s", url);
30 }
31 host += 2;
32 path = strchr(host, '/');
33 if (!path) {
34 return error("Bad URL: %s", url);
35 }
36 *(path++) = '\0';
37 /* ssh <host> 'cd /<path>; stdio-pull <arg...> <commit-id>' */
38 snprintf(command, COMMAND_SIZE,
39 "cd /%s; SHA1_FILE_DIRECTORY=objects %s",
40 path, remote_prog);
41 posn = command + strlen(command);
42 for (i = 0; i < rmt_argc; i++) {
43 *(posn++) = ' ';
44 strncpy(posn, rmt_argv[i], COMMAND_SIZE - (posn - command));
45 posn += strlen(rmt_argv[i]);
46 if (posn - command + 4 >= COMMAND_SIZE) {
47 return error("Command line too long");
48 }
49 }
50 strcpy(posn, " -");
ae200ee5 51 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
6eb7ed54
DB
52 return error("Couldn't create socket");
53 }
54 if (!fork()) {
55 close(sv[1]);
56 dup2(sv[0], 0);
57 dup2(sv[0], 1);
58 execlp("ssh", "ssh", host, command, NULL);
59 }
60 close(sv[0]);
61 *fd_in = sv[1];
62 *fd_out = sv[1];
63 return 0;
64}