Coverage Report

Created: 2024-09-08 06:24

/src/git/builtin/remote-fd.c
Line
Count
Source (jump to first uncovered line)
1
#include "builtin.h"
2
#include "transport.h"
3
4
static const char usage_msg[] =
5
  "git remote-fd <remote> <url>";
6
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.
14
 *
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>.
19
 *
20
 */
21
22
0
#define MAXCOMMAND 4096
23
24
static void command_loop(int input_fd, int output_fd)
25
0
{
26
0
  char buffer[MAXCOMMAND];
27
28
0
  while (1) {
29
0
    size_t i;
30
0
    if (!fgets(buffer, MAXCOMMAND - 1, stdin)) {
31
0
      if (ferror(stdin))
32
0
        die("Input error");
33
0
      return;
34
0
    }
35
    /* Strip end of line characters. */
36
0
    i = strlen(buffer);
37
0
    while (i > 0 && isspace(buffer[i - 1]))
38
0
      buffer[--i] = 0;
39
40
0
    if (!strcmp(buffer, "capabilities")) {
41
0
      printf("*connect\n\n");
42
0
      fflush(stdout);
43
0
    } else if (starts_with(buffer, "connect ")) {
44
0
      printf("\n");
45
0
      fflush(stdout);
46
0
      if (bidirectional_transfer_loop(input_fd,
47
0
        output_fd))
48
0
        die("Copying data between file descriptors failed");
49
0
      return;
50
0
    } else {
51
0
      die("Bad command: %s", buffer);
52
0
    }
53
0
  }
54
0
}
55
56
int cmd_remote_fd(int argc, const char **argv, const char *prefix)
57
0
{
58
0
  int input_fd = -1;
59
0
  int output_fd = -1;
60
0
  char *end;
61
62
0
  BUG_ON_NON_EMPTY_PREFIX(prefix);
63
64
0
  if (argc != 3)
65
0
    usage(usage_msg);
66
67
0
  input_fd = (int)strtoul(argv[2], &end, 10);
68
69
0
  if ((end == argv[2]) || (*end != ',' && *end != '/' && *end))
70
0
    die("Bad URL syntax");
71
72
0
  if (*end == '/' || !*end) {
73
0
    output_fd = input_fd;
74
0
  } else {
75
0
    char *end2;
76
0
    output_fd = (int)strtoul(end + 1, &end2, 10);
77
78
0
    if ((end2 == end + 1) || (*end2 != '/' && *end2))
79
0
      die("Bad URL syntax");
80
0
  }
81
82
0
  command_loop(input_fd, output_fd);
83
0
  return 0;
84
0
}