]>
Commit | Line | Data |
---|---|---|
ad897215 | 1 | #include "cache.h" |
77cb17e9 | 2 | #include "exec_cmd.h" |
583b7ea3 | 3 | #include "pkt-line.h" |
49a52b1d | 4 | #include "sideband.h" |
ad897215 | 5 | #include <sys/wait.h> |
ad897215 | 6 | |
583b7ea3 JH |
7 | static pid_t setup_sideband(int sideband, const char *me, int fd[2], int xd[2]) |
8 | { | |
9 | pid_t side_pid; | |
10 | ||
11 | if (!sideband) { | |
12 | fd[0] = xd[0]; | |
13 | fd[1] = xd[1]; | |
14 | return 0; | |
15 | } | |
16 | /* xd[] is talking with upload-pack; subprocess reads from | |
17 | * xd[0], spits out band#2 to stderr, and feeds us band#1 | |
18 | * through our fd[0]. | |
19 | */ | |
20 | if (pipe(fd) < 0) | |
21 | die("%s: unable to set up pipe", me); | |
22 | side_pid = fork(); | |
23 | if (side_pid < 0) | |
24 | die("%s: unable to fork off sideband demultiplexer", me); | |
25 | if (!side_pid) { | |
26 | /* subprocess */ | |
27 | close(fd[0]); | |
28 | if (xd[0] != xd[1]) | |
29 | close(xd[1]); | |
9ac13ec9 | 30 | if (recv_sideband(me, xd[0], fd[1], 2)) |
49a52b1d | 31 | exit(1); |
583b7ea3 JH |
32 | exit(0); |
33 | } | |
34 | close(xd[0]); | |
35 | close(fd[1]); | |
36 | fd[1] = xd[1]; | |
37 | return side_pid; | |
38 | } | |
39 | ||
d9c20ba1 | 40 | static int get_pack(int xd[2], const char *me, int sideband, const char **argv) |
ad897215 JH |
41 | { |
42 | int status; | |
583b7ea3 JH |
43 | pid_t pid, side_pid; |
44 | int fd[2]; | |
ad897215 | 45 | |
583b7ea3 | 46 | side_pid = setup_sideband(sideband, me, fd, xd); |
ad897215 JH |
47 | pid = fork(); |
48 | if (pid < 0) | |
5ca80784 | 49 | die("%s: unable to fork off %s", me, argv[0]); |
ad897215 JH |
50 | if (!pid) { |
51 | dup2(fd[0], 0); | |
52 | close(fd[0]); | |
53 | close(fd[1]); | |
d9c20ba1 NP |
54 | execv_git_cmd(argv); |
55 | die("%s exec failed", argv[0]); | |
ad897215 JH |
56 | } |
57 | close(fd[0]); | |
58 | close(fd[1]); | |
59 | while (waitpid(pid, &status, 0) < 0) { | |
60 | if (errno != EINTR) | |
d9c20ba1 | 61 | die("waiting for %s: %s", argv[0], strerror(errno)); |
ad897215 JH |
62 | } |
63 | if (WIFEXITED(status)) { | |
64 | int code = WEXITSTATUS(status); | |
65 | if (code) | |
d9c20ba1 | 66 | die("%s died with error code %d", argv[0], code); |
ad897215 JH |
67 | return 0; |
68 | } | |
69 | if (WIFSIGNALED(status)) { | |
70 | int sig = WTERMSIG(status); | |
d9c20ba1 | 71 | die("%s died of signal %d", argv[0], sig); |
ad897215 | 72 | } |
d9c20ba1 | 73 | die("%s died of unnatural causes %d", argv[0], status); |
ad897215 JH |
74 | } |
75 | ||
d9c20ba1 NP |
76 | int receive_unpack_pack(int xd[2], const char *me, int quiet, int sideband) |
77 | { | |
78 | const char *argv[3] = { "unpack-objects", quiet ? "-q" : NULL, NULL }; | |
79 | return get_pack(xd, me, sideband, argv); | |
80 | } | |
c548cf4e | 81 | |
583b7ea3 | 82 | int receive_keep_pack(int xd[2], const char *me, int quiet, int sideband) |
ad897215 | 83 | { |
d9c20ba1 NP |
84 | const char *argv[5] = { "index-pack", "--stdin", "--fix-thin", |
85 | quiet ? NULL : "-v", NULL }; | |
86 | return get_pack(xd, me, sideband, argv); | |
ad897215 | 87 | } |