Skip to content

Commit dba385b

Browse files
iabervonLinus Torvalds
authored andcommitted
[PATCH] ssh-protocol version, command types, response code
This patch makes an incompatible change to the protocol used by rpull/rpush which will let it be extended in the future without incompatible changes. Signed-off-by: Daniel Barkalow <barkalow@iabervon.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent eeaa460 commit dba385b

File tree

2 files changed

+93
-32
lines changed

2 files changed

+93
-32
lines changed

rpull.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,39 @@
66
static int fd_in;
77
static int fd_out;
88

9+
static unsigned char remote_version = 0;
10+
static unsigned char local_version = 1;
11+
912
int fetch(unsigned char *sha1)
1013
{
1114
int ret;
15+
signed char remote;
16+
char type = 'o';
17+
if (has_sha1_file(sha1))
18+
return 0;
19+
write(fd_out, &type, 1);
1220
write(fd_out, sha1, 20);
21+
if (read(fd_in, &remote, 1) < 1)
22+
return -1;
23+
if (remote < 0)
24+
return remote;
1325
ret = write_sha1_from_fd(sha1, fd_in);
1426
if (!ret)
1527
pull_say("got %s\n", sha1_to_hex(sha1));
1628
return ret;
1729
}
1830

31+
int get_version(void)
32+
{
33+
char type = 'v';
34+
write(fd_out, &type, 1);
35+
write(fd_out, &local_version, 1);
36+
if (read(fd_in, &remote_version, 1) < 1) {
37+
return error("Couldn't read version from remote end");
38+
}
39+
return 0;
40+
}
41+
1942
int main(int argc, char **argv)
2043
{
2144
char *commit_id;
@@ -48,6 +71,9 @@ int main(int argc, char **argv)
4871
if (setup_connection(&fd_in, &fd_out, "git-rpush", url, arg, argv + 1))
4972
return 1;
5073

74+
if (get_version())
75+
return 1;
76+
5177
if (pull(commit_id))
5278
return 1;
5379

rpush.c

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,81 @@
33
#include <sys/socket.h>
44
#include <errno.h>
55

6-
static void service(int fd_in, int fd_out) {
6+
unsigned char local_version = 1;
7+
unsigned char remote_version = 0;
8+
9+
int serve_object(int fd_in, int fd_out) {
710
ssize_t size;
8-
int posn;
9-
char unsigned sha1[20];
11+
int posn = 0;
12+
char sha1[20];
1013
unsigned long objsize;
1114
void *buf;
15+
signed char remote;
16+
do {
17+
size = read(fd_in, sha1 + posn, 20 - posn);
18+
if (size < 0) {
19+
perror("git-rpush: read ");
20+
return -1;
21+
}
22+
if (!size)
23+
return -1;
24+
posn += size;
25+
} while (posn < 20);
26+
27+
/* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
28+
remote = 0;
29+
30+
buf = map_sha1_file(sha1, &objsize);
31+
32+
if (!buf) {
33+
fprintf(stderr, "git-rpush: could not find %s\n",
34+
sha1_to_hex(sha1));
35+
remote = -1;
36+
}
37+
38+
write(fd_out, &remote, 1);
39+
40+
if (remote < 0)
41+
return 0;
42+
43+
posn = 0;
1244
do {
13-
posn = 0;
14-
do {
15-
size = read(fd_in, sha1 + posn, 20 - posn);
16-
if (size < 0) {
17-
perror("git-rpush: read ");
18-
return;
45+
size = write(fd_out, buf + posn, objsize - posn);
46+
if (size <= 0) {
47+
if (!size) {
48+
fprintf(stderr, "git-rpush: write closed");
49+
} else {
50+
perror("git-rpush: write ");
1951
}
20-
if (!size)
21-
return;
22-
posn += size;
23-
} while (posn < 20);
52+
return -1;
53+
}
54+
posn += size;
55+
} while (posn < objsize);
56+
return 0;
57+
}
2458

25-
/* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
59+
int serve_version(int fd_in, int fd_out)
60+
{
61+
if (read(fd_in, &remote_version, 1) < 1)
62+
return -1;
63+
write(fd_out, &local_version, 1);
64+
return 0;
65+
}
2666

27-
buf = map_sha1_file(sha1, &objsize);
28-
if (!buf) {
29-
fprintf(stderr, "git-rpush: could not find %s\n",
30-
sha1_to_hex(sha1));
67+
void service(int fd_in, int fd_out) {
68+
char type;
69+
int retval;
70+
do {
71+
retval = read(fd_in, &type, 1);
72+
if (retval < 1) {
73+
if (retval < 0)
74+
perror("rpush: read ");
3175
return;
3276
}
33-
posn = 0;
34-
do {
35-
size = write(fd_out, buf + posn, objsize - posn);
36-
if (size <= 0) {
37-
if (!size) {
38-
fprintf(stderr, "git-rpush: write closed");
39-
} else {
40-
perror("git-rpush: write ");
41-
}
42-
return;
43-
}
44-
posn += size;
45-
} while (posn < objsize);
77+
if (type == 'v' && serve_version(fd_in, fd_out))
78+
return;
79+
if (type == 'o' && serve_object(fd_in, fd_out))
80+
return;
4681
} while (1);
4782
}
4883

@@ -56,7 +91,7 @@ int main(int argc, char **argv)
5691
arg++;
5792
}
5893
if (argc < arg + 2) {
59-
usage("git-rpush [-c] [-t] [-a] commit-id url");
94+
usage("git-rpush [-c] [-t] [-a] commit-id url");
6095
return 1;
6196
}
6297
commit_id = argv[arg];

0 commit comments

Comments
 (0)