refs/files: fix NULL pointer deref when releasing ref store
[git/gitster.git] / builtin / remote-fd.c
blob9020fab9c580b304d5099e12d35094eb1d1f3874
1 #include "builtin.h"
2 #include "transport.h"
4 static const char usage_msg[] =
5 "git remote-fd <remote> <url>";
7 /*
8 * URL syntax:
9 * 'fd::<inoutfd>[/<anything>]' Read/write socket pair
10 * <inoutfd>.
11 * 'fd::<infd>,<outfd>[/<anything>]' Read pipe <infd> and write
12 * pipe <outfd>.
13 * [foo] indicates 'foo' is optional. <anything> is any string.
15 * The data output to <outfd>/<inoutfd> should be passed unmolested to
16 * git-receive-pack/git-upload-pack/git-upload-archive and output of
17 * git-receive-pack/git-upload-pack/git-upload-archive should be passed
18 * unmolested to <infd>/<inoutfd>.
22 #define MAXCOMMAND 4096
24 static void command_loop(int input_fd, int output_fd)
26 char buffer[MAXCOMMAND];
28 while (1) {
29 size_t i;
30 if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
31 if (ferror(stdin))
32 die("Input error");
33 return;
35 /* Strip end of line characters. */
36 i = strlen(buffer);
37 while (i > 0 && isspace(buffer[i - 1]))
38 buffer[--i] = 0;
40 if (!strcmp(buffer, "capabilities")) {
41 printf("*connect\n\n");
42 fflush(stdout);
43 } else if (starts_with(buffer, "connect ")) {
44 printf("\n");
45 fflush(stdout);
46 if (bidirectional_transfer_loop(input_fd,
47 output_fd))
48 die("Copying data between file descriptors failed");
49 return;
50 } else {
51 die("Bad command: %s", buffer);
56 int cmd_remote_fd(int argc, const char **argv, const char *prefix)
58 int input_fd = -1;
59 int output_fd = -1;
60 char *end;
62 BUG_ON_NON_EMPTY_PREFIX(prefix);
64 if (argc != 3)
65 usage(usage_msg);
67 input_fd = (int)strtoul(argv[2], &end, 10);
69 if ((end == argv[2]) || (*end != ',' && *end != '/' && *end))
70 die("Bad URL syntax");
72 if (*end == '/' || !*end) {
73 output_fd = input_fd;
74 } else {
75 char *end2;
76 output_fd = (int)strtoul(end + 1, &end2, 10);
78 if ((end2 == end + 1) || (*end2 != '/' && *end2))
79 die("Bad URL syntax");
82 command_loop(input_fd, output_fd);
83 return 0;