1#include "cache.h"
2#include "rsh.h"
3#include <sys/socket.h>
4#include <errno.h>
5
6void service(int fd_in, int fd_out) {
7 ssize_t size;
8 int posn;
9 char sha1[20];
10 unsigned long objsize;
11 void *buf;
12 do {
13 posn = 0;
14 do {
15 size = read(fd_in, sha1 + posn, 20 - posn);
16 if (size < 0) {
17 perror("rpush: read ");
18 return;
19 }
20 if (!size)
21 return;
22 posn += size;
23 } while (posn < 20);
24
25 /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
26
27 buf = map_sha1_file(sha1, &objsize);
28 if (!buf) {
29 fprintf(stderr, "rpush: could not find %s\n",
30 sha1_to_hex(sha1));
31 return;
32 }
33 posn = 0;
34 do {
35 size = write(fd_out, buf + posn, objsize - posn);
36 if (size <= 0) {
37 if (!size) {
38 fprintf(stderr, "rpush: write closed");
39 } else {
40 perror("rpush: write ");
41 }
42 return;
43 }
44 posn += size;
45 } while (posn < objsize);
46 } while (1);
47}
48
49int main(int argc, char **argv)
50{
51 int arg = 1;
52 char *commit_id;
53 char *url;
54 int fd_in, fd_out;
55 while (arg < argc && argv[arg][0] == '-') {
56 arg++;
57 }
58 if (argc < arg + 2) {
59 usage("rpush [-c] [-t] [-a] commit-id url");
60 return 1;
61 }
62 commit_id = argv[arg];
63 url = argv[arg + 1];
64 if (setup_connection(&fd_in, &fd_out, "rpull", url, arg, argv + 1))
65 return 1;
66
67 service(fd_in, fd_out);
68 return 0;
69}