]>
| Commit | Line | Data |
|---|---|---|
| 39345a21 FBH |
1 | /* |
| 2 | * Copyright (c) 2006 Franck Bui-Huu | |
| 3 | */ | |
| 03eae9af | 4 | #define USE_THE_REPOSITORY_VARIABLE |
| 39345a21 FBH |
5 | #include "builtin.h" |
| 6 | #include "archive.h" | |
| c339932b | 7 | #include "path.h" |
| 39345a21 | 8 | #include "pkt-line.h" |
| 23d6d112 | 9 | #include "sideband.h" |
| 1bc01efe | 10 | #include "run-command.h" |
| dbbcd44f | 11 | #include "strvec.h" |
| 39345a21 FBH |
12 | |
| 13 | static const char upload_archive_usage[] = | |
| f6a8ef07 | 14 | "git upload-archive <repository>"; |
| 39345a21 | 15 | |
| 23d6d112 | 16 | static const char deadchild[] = |
| 1b1dd23f | 17 | "git upload-archive: archiver died with error"; |
| 39345a21 | 18 | |
| 7f4d0511 | 19 | #define MAX_ARGS (64) |
| 23d6d112 | 20 | |
| 9b1cb507 JC |
21 | int cmd_upload_archive_writer(int argc, |
| 22 | const char **argv, | |
| 23 | const char *prefix, | |
| 24 | struct repository *repo UNUSED) | |
| 39345a21 | 25 | { |
| 22f9b7f3 | 26 | struct strvec sent_argv = STRVEC_INIT; |
| 39345a21 | 27 | const char *arg_cmd = "argument "; |
| 149c9e20 | 28 | int ret; |
| 39345a21 | 29 | |
| a36a822d JH |
30 | show_usage_if_asked(argc, argv, upload_archive_usage); |
| 31 | if (argc != 2) | |
| f0c7fd49 JH |
32 | usage(upload_archive_usage); |
| 33 | ||
| 6379dd05 JK |
34 | if (!enter_repo(argv[1], 0)) |
| 35 | die("'%s' does not appear to be a git repository", argv[1]); | |
| f0c7fd49 | 36 | |
| 00436bf1 JS |
37 | init_archivers(); |
| 38 | ||
| 39345a21 | 39 | /* put received options in sent_argv[] */ |
| 22f9b7f3 | 40 | strvec_push(&sent_argv, "git-upload-archive"); |
| 090fd4fe | 41 | for (;;) { |
| 74543a04 JK |
42 | char *buf = packet_read_line(0, NULL); |
| 43 | if (!buf) | |
| 39345a21 | 44 | break; /* got a flush */ |
| d70a9eb6 | 45 | if (sent_argv.nr > MAX_ARGS) |
| 090fd4fe | 46 | die("Too many options (>%d)", MAX_ARGS - 1); |
| 39345a21 | 47 | |
| 59556548 | 48 | if (!starts_with(buf, arg_cmd)) |
| 090fd4fe | 49 | die("'argument' token or flush expected"); |
| 22f9b7f3 | 50 | strvec_push(&sent_argv, buf + strlen(arg_cmd)); |
| 39345a21 | 51 | } |
| f0c7fd49 JH |
52 | |
| 53 | /* parse all options sent by the client */ | |
| 149c9e20 PS |
54 | ret = write_archive(sent_argv.nr, sent_argv.v, prefix, |
| 55 | the_repository, NULL, 1); | |
| 56 | ||
| 57 | strvec_clear(&sent_argv); | |
| 58 | return ret; | |
| 23d6d112 JH |
59 | } |
| 60 | ||
| 28bea9e5 | 61 | __attribute__((format (printf, 1, 2))) |
| d3788e19 JH |
62 | static void error_clnt(const char *fmt, ...) |
| 63 | { | |
| 0cb9d6d6 | 64 | struct strbuf buf = STRBUF_INIT; |
| d3788e19 | 65 | va_list params; |
| d3788e19 JH |
66 | |
| 67 | va_start(params, fmt); | |
| 0cb9d6d6 | 68 | strbuf_vaddf(&buf, fmt, params); |
| d3788e19 | 69 | va_end(params); |
| 0cb9d6d6 JK |
70 | send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX); |
| 71 | die("sent error to the client: %s", buf.buf); | |
| d3788e19 JH |
72 | } |
| 73 | ||
| 1b19fa46 | 74 | static ssize_t process_input(int child_fd, int band) |
| d3788e19 JH |
75 | { |
| 76 | char buf[16384]; | |
| 77 | ssize_t sz = read(child_fd, buf, sizeof(buf)); | |
| 78 | if (sz < 0) { | |
| 93d26e4c | 79 | if (errno != EAGAIN && errno != EINTR) |
| d3788e19 | 80 | error_clnt("read error: %s\n", strerror(errno)); |
| 1b19fa46 | 81 | return sz; |
| d3788e19 JH |
82 | } |
| 83 | send_sideband(1, band, buf, sz, LARGE_PACKET_MAX); | |
| 1b19fa46 | 84 | return sz; |
| d3788e19 JH |
85 | } |
| 86 | ||
| 9b1cb507 JC |
87 | int cmd_upload_archive(int argc, |
| 88 | const char **argv, | |
| 89 | const char *prefix, | |
| 90 | struct repository *repo UNUSED) | |
| 23d6d112 | 91 | { |
| c8a4cd55 | 92 | struct child_process writer = CHILD_PROCESS_INIT; |
| 1bc01efe | 93 | |
| 79156913 JK |
94 | BUG_ON_NON_EMPTY_PREFIX(prefix); |
| 95 | ||
| f66d1423 | 96 | show_usage_if_asked(argc, argv, upload_archive_usage); |
| 619b6c17 | 97 | |
| f0c7fd49 JH |
98 | /* |
| 99 | * Set up sideband subprocess. | |
| 100 | * | |
| 101 | * We (parent) monitor and read from child, sending its fd#1 and fd#2 | |
| 102 | * multiplexed out to our fd#1. If the child dies, we tell the other | |
| 103 | * end over channel #3. | |
| 104 | */ | |
| 1bc01efe JK |
105 | writer.out = writer.err = -1; |
| 106 | writer.git_cmd = 1; | |
| c8a4cd55 ÆAB |
107 | strvec_push(&writer.args, "upload-archive--writer"); |
| 108 | strvec_pushv(&writer.args, argv + 1); | |
| 1bc01efe | 109 | if (start_command(&writer)) { |
| 23d6d112 | 110 | int err = errno; |
| 81c634e9 | 111 | packet_write_fmt(1, "NACK unable to spawn subprocess\n"); |
| 23d6d112 JH |
112 | die("upload-archive: %s", strerror(err)); |
| 113 | } | |
| 23d6d112 | 114 | |
| 81c634e9 | 115 | packet_write_fmt(1, "ACK\n"); |
| 39345a21 FBH |
116 | packet_flush(1); |
| 117 | ||
| 23d6d112 JH |
118 | while (1) { |
| 119 | struct pollfd pfd[2]; | |
| 23d6d112 | 120 | |
| 1bc01efe | 121 | pfd[0].fd = writer.out; |
| 23d6d112 | 122 | pfd[0].events = POLLIN; |
| 1bc01efe | 123 | pfd[1].fd = writer.err; |
| 23d6d112 JH |
124 | pfd[1].events = POLLIN; |
| 125 | if (poll(pfd, 2, -1) < 0) { | |
| 126 | if (errno != EINTR) { | |
| 17bef17e | 127 | error_errno("poll failed resuming"); |
| 23d6d112 JH |
128 | sleep(1); |
| 129 | } | |
| 130 | continue; | |
| 131 | } | |
| d3788e19 | 132 | if (pfd[1].revents & POLLIN) |
| 23d6d112 | 133 | /* Status stream ready */ |
| 6b59f51b NP |
134 | if (process_input(pfd[1].fd, 2)) |
| 135 | continue; | |
| 136 | if (pfd[0].revents & POLLIN) | |
| 137 | /* Data stream ready */ | |
| 138 | if (process_input(pfd[0].fd, 1)) | |
| 139 | continue; | |
| d3788e19 | 140 | |
| 1bc01efe | 141 | if (finish_command(&writer)) |
| d3788e19 | 142 | error_clnt("%s", deadchild); |
| 23d6d112 JH |
143 | packet_flush(1); |
| 144 | break; | |
| 145 | } | |
| 146 | return 0; | |
| 147 | } |