8
9
#define COMMAND_SIZE 4096
10
11
+/*
12
+ * Write a shell-quoted version of a string into a buffer, and
13
+ * return bytes that ought to be output excluding final null.
14
+ */
15
+static int shell_quote(char *buf, int nmax, const char *str)
16
+{
17
+ char ch;
18
+ int nq;
19
+ int oc = 0;
20
+
21
+ while ( (ch = *str++) ) {
22
+ nq = 0;
23
+ if ( strchr(" !\"#$%&\'()*;<=>?[\\]^`{|}", ch) )
24
+ nq = 1;
25
+
26
+ if ( nq ) {
27
+ if ( nmax > 1 ) {
28
+ *buf++ = '\\';
29
+ nmax--;
30
+ }
31
+ oc++;
32
+ }
33
+
34
+ if ( nmax > 1 ) {
35
+ *buf++ = ch;
36
+ nmax--;
37
+ }
38
+ oc++;
39
+ }
40
+
41
+ if ( nmax )
42
+ *buf = '\0';
43
+
44
+ return oc;
45
+}
46
+
47
+/*
48
+ * Append a string to a string buffer, with or without quoting. Return true
49
+ * if the buffer overflowed.
50
+ */
51
+static int add_to_string(char **ptrp, int *sizep, const char *str, int quote)
52
+{
53
+ char *p = *ptrp;
54
+ int size = *sizep;
55
+ int oc;
56
+
57
+ if ( quote ) {
58
+ oc = shell_quote(p, size, str);
59
+ } else {
60
+ oc = strlen(str);
61
+ memcpy(p, str, (oc >= size) ? size-1 : oc);
62
+ }
63
+
64
+ if ( oc >= size ) {
65
+ p[size-1] = '\0';
66
+ *ptrp += size-1;
67
+ *sizep = 1;
68
+ return 1; /* Overflow, string unusable */
69
+ }
70
+
71
+ *ptrp += oc;
72
+ *sizep -= oc;
73
+ return 0;
74
+}
75
+
76
int setup_connection(int *fd_in, int *fd_out, const char *remote_prog,
77
char *url, int rmt_argc, char **rmt_argv)
78
{
81
int sv[2];
82
char command[COMMAND_SIZE];
83
char *posn;
84
+ int sizen;
85
+ int of;
86
int i;
87
88
if (!strcmp(url, "-")) {
104
if (!path) {
105
return error("Bad URL: %s", url);
106
}
40
- /* ssh <host> 'cd <path>; stdio-pull <arg...> <commit-id>' */
41
- snprintf(command, COMMAND_SIZE,
42
- "%s='%s' %s",
43
- GIT_DIR_ENVIRONMENT, path, remote_prog);
44
- *path = '\0';
45
- posn = command + strlen(command);
46
- for (i = 0; i < rmt_argc; i++) {
47
- *(posn++) = ' ';
48
- strncpy(posn, rmt_argv[i], COMMAND_SIZE - (posn - command));
49
- posn += strlen(rmt_argv[i]);
50
- if (posn - command + 4 >= COMMAND_SIZE) {
51
- return error("Command line too long");
52
- }
107
+ /* $GIT_RSH <host> "env GIR_DIR=<path> <remote_prog> <args...>" */
108
+ sizen = COMMAND_SIZE;
109
+ posn = command;
110
+ of = 0;
111
+ of |= add_to_string(&posn, &sizen, "env ", 0);
112
+ of |= add_to_string(&posn, &sizen, GIT_DIR_ENVIRONMENT, 0);
113
+ of |= add_to_string(&posn, &sizen, "=", 0);
114
+ of |= add_to_string(&posn, &sizen, path, 1);
115
+ of |= add_to_string(&posn, &sizen, " ", 0);
116
+ of |= add_to_string(&posn, &sizen, remote_prog, 1);
117
+
118
+ for ( i = 0 ; i < rmt_argc ; i++ ) {
119
+ of |= add_to_string(&posn, &sizen, " ", 0);
120
+ of |= add_to_string(&posn, &sizen, rmt_argv[i], 1);
121
}
54
- strcpy(posn, " -");
55
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv)) {
122
+
123
+ of |= add_to_string(&posn, &sizen, " -", 0);
124
+
125
+ if ( of )
126
+ return error("Command line too long");
127
+
128
+ if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
129
return error("Couldn't create socket");
57
- }
130
+
131
if (!fork()) {
132
const char *ssh, *ssh_basename;
133
ssh = getenv("GIT_SSH");