[PATCH] rsh.c environment variable
[git/jnareb-git.git] / ssh-push.c
blob9994ea7fc098468976c4a95daf972f796d88793b
1 #include "cache.h"
2 #include "rsh.h"
3 #include <sys/socket.h>
4 #include <errno.h>
6 unsigned char local_version = 1;
7 unsigned char remote_version = 0;
9 int serve_object(int fd_in, int fd_out) {
10 ssize_t size;
11 int posn = 0;
12 char sha1[20];
13 unsigned long objsize;
14 void *buf;
15 signed char remote;
16 do {
17 size = read(fd_in, sha1 + posn, 20 - posn);
18 if (size < 0) {
19 perror("git-ssh-push: read ");
20 return -1;
22 if (!size)
23 return -1;
24 posn += size;
25 } while (posn < 20);
27 /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
28 remote = 0;
30 buf = map_sha1_file(sha1, &objsize);
32 if (!buf) {
33 fprintf(stderr, "git-ssh-push: could not find %s\n",
34 sha1_to_hex(sha1));
35 remote = -1;
38 write(fd_out, &remote, 1);
40 if (remote < 0)
41 return 0;
43 posn = 0;
44 do {
45 size = write(fd_out, buf + posn, objsize - posn);
46 if (size <= 0) {
47 if (!size) {
48 fprintf(stderr, "git-ssh-push: write closed");
49 } else {
50 perror("git-ssh-push: write ");
52 return -1;
54 posn += size;
55 } while (posn < objsize);
56 return 0;
59 int serve_version(int fd_in, int fd_out)
61 if (read(fd_in, &remote_version, 1) < 1)
62 return -1;
63 write(fd_out, &local_version, 1);
64 return 0;
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("git-ssh-push: read ");
75 return;
77 if (type == 'v' && serve_version(fd_in, fd_out))
78 return;
79 if (type == 'o' && serve_object(fd_in, fd_out))
80 return;
81 } while (1);
84 int main(int argc, char **argv)
86 int arg = 1;
87 char *commit_id;
88 char *url;
89 int fd_in, fd_out;
90 while (arg < argc && argv[arg][0] == '-') {
91 arg++;
93 if (argc < arg + 2) {
94 usage("git-ssh-push [-c] [-t] [-a] commit-id url");
95 return 1;
97 commit_id = argv[arg];
98 url = argv[arg + 1];
99 if (setup_connection(&fd_in, &fd_out, "git-ssh-pull", url, arg, argv + 1))
100 return 1;
102 service(fd_in, fd_out);
103 return 0;