blob: 98d028dae679080af9785d57bc83525d5cd2872f [file] [log] [blame]
Franck Bui-Huu39345a22006-09-07 15:12:05 +02001/*
2 * Copyright (c) 2006 Franck Bui-Huu
3 */
Franck Bui-Huu39345a22006-09-07 15:12:05 +02004#include "cache.h"
5#include "builtin.h"
6#include "archive.h"
7#include "pkt-line.h"
Junio C Hamano23d6d112006-09-10 03:33:34 -07008#include "sideband.h"
Jeff King1bc01ef2011-11-19 02:40:04 -05009#include "run-command.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040010#include "strvec.h"
Franck Bui-Huu39345a22006-09-07 15:12:05 +020011
12static const char upload_archive_usage[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020013 "git upload-archive <repo>";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020014
Junio C Hamano23d6d112006-09-10 03:33:34 -070015static const char deadchild[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020016"git upload-archive: archiver died with error";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020017
Rene Scharfe7f4d0512008-07-25 12:41:23 +020018#define MAX_ARGS (64)
Junio C Hamano23d6d112006-09-10 03:33:34 -070019
Jeff King1bc01ef2011-11-19 02:40:04 -050020int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
Franck Bui-Huu39345a22006-09-07 15:12:05 +020021{
Jeff King22f9b7f2020-07-28 16:24:27 -040022 struct strvec sent_argv = STRVEC_INIT;
Franck Bui-Huu39345a22006-09-07 15:12:05 +020023 const char *arg_cmd = "argument ";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020024
Jeff King619b6c12017-05-30 01:13:43 -040025 if (argc != 2 || !strcmp(argv[1], "-h"))
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080026 usage(upload_archive_usage);
27
Jeff King6379dd02013-02-20 15:00:59 -050028 if (!enter_repo(argv[1], 0))
29 die("'%s' does not appear to be a git repository", argv[1]);
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080030
Josh Steadmon00436bf2018-10-25 13:32:14 -070031 init_archivers();
32
Franck Bui-Huu39345a22006-09-07 15:12:05 +020033 /* put received options in sent_argv[] */
Jeff King22f9b7f2020-07-28 16:24:27 -040034 strvec_push(&sent_argv, "git-upload-archive");
Jeff King090fd4f2013-02-20 15:01:26 -050035 for (;;) {
Jeff King74543a02013-02-20 15:02:57 -050036 char *buf = packet_read_line(0, NULL);
37 if (!buf)
Franck Bui-Huu39345a22006-09-07 15:12:05 +020038 break; /* got a flush */
Jeff Kingd70a9eb2020-07-28 20:37:20 -040039 if (sent_argv.nr > MAX_ARGS)
Jeff King090fd4f2013-02-20 15:01:26 -050040 die("Too many options (>%d)", MAX_ARGS - 1);
Franck Bui-Huu39345a22006-09-07 15:12:05 +020041
Christian Couder59556542013-11-30 21:55:40 +010042 if (!starts_with(buf, arg_cmd))
Jeff King090fd4f2013-02-20 15:01:26 -050043 die("'argument' token or flush expected");
Jeff King22f9b7f2020-07-28 16:24:27 -040044 strvec_push(&sent_argv, buf + strlen(arg_cmd));
Franck Bui-Huu39345a22006-09-07 15:12:05 +020045 }
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080046
47 /* parse all options sent by the client */
Jeff Kingd70a9eb2020-07-28 20:37:20 -040048 return write_archive(sent_argv.nr, sent_argv.v, prefix,
Nguyễn Thái Ngọc Duyb612ee22018-08-13 18:14:35 +020049 the_repository, NULL, 1);
Franck Bui-Huu39345a22006-09-07 15:12:05 +020050}
51
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080052__attribute__((format (printf, 1, 2)))
Junio C Hamanod3788e12006-09-12 00:26:57 -070053static void error_clnt(const char *fmt, ...)
54{
Jeff King0cb9d6d2015-09-24 17:07:25 -040055 struct strbuf buf = STRBUF_INIT;
Junio C Hamanod3788e12006-09-12 00:26:57 -070056 va_list params;
Junio C Hamanod3788e12006-09-12 00:26:57 -070057
58 va_start(params, fmt);
Jeff King0cb9d6d2015-09-24 17:07:25 -040059 strbuf_vaddf(&buf, fmt, params);
Junio C Hamanod3788e12006-09-12 00:26:57 -070060 va_end(params);
Jeff King0cb9d6d2015-09-24 17:07:25 -040061 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
62 die("sent error to the client: %s", buf.buf);
Junio C Hamanod3788e12006-09-12 00:26:57 -070063}
64
René Scharfe1b19fa42009-06-17 12:11:10 +020065static ssize_t process_input(int child_fd, int band)
Junio C Hamanod3788e12006-09-12 00:26:57 -070066{
67 char buf[16384];
68 ssize_t sz = read(child_fd, buf, sizeof(buf));
69 if (sz < 0) {
Andy Whitcroft93d26e42007-01-08 15:58:08 +000070 if (errno != EAGAIN && errno != EINTR)
Junio C Hamanod3788e12006-09-12 00:26:57 -070071 error_clnt("read error: %s\n", strerror(errno));
René Scharfe1b19fa42009-06-17 12:11:10 +020072 return sz;
Junio C Hamanod3788e12006-09-12 00:26:57 -070073 }
74 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
René Scharfe1b19fa42009-06-17 12:11:10 +020075 return sz;
Junio C Hamanod3788e12006-09-12 00:26:57 -070076}
77
Junio C Hamano23d6d112006-09-10 03:33:34 -070078int cmd_upload_archive(int argc, const char **argv, const char *prefix)
79{
Ævar Arnfjörð Bjarmasonc8a4cd552021-11-25 23:52:17 +010080 struct child_process writer = CHILD_PROCESS_INIT;
Jeff King1bc01ef2011-11-19 02:40:04 -050081
Jeff King619b6c12017-05-30 01:13:43 -040082 if (argc == 2 && !strcmp(argv[1], "-h"))
83 usage(upload_archive_usage);
84
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080085 /*
86 * Set up sideband subprocess.
87 *
88 * We (parent) monitor and read from child, sending its fd#1 and fd#2
89 * multiplexed out to our fd#1. If the child dies, we tell the other
90 * end over channel #3.
91 */
Jeff King1bc01ef2011-11-19 02:40:04 -050092 writer.out = writer.err = -1;
93 writer.git_cmd = 1;
Ævar Arnfjörð Bjarmasonc8a4cd552021-11-25 23:52:17 +010094 strvec_push(&writer.args, "upload-archive--writer");
95 strvec_pushv(&writer.args, argv + 1);
Jeff King1bc01ef2011-11-19 02:40:04 -050096 if (start_command(&writer)) {
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080097 int err = errno;
Lars Schneider81c634e2016-10-16 16:20:29 -070098 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080099 die("upload-archive: %s", strerror(err));
100 }
Junio C Hamanof0c7fd42011-11-15 15:39:33 -0800101
Lars Schneider81c634e2016-10-16 16:20:29 -0700102 packet_write_fmt(1, "ACK\n");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700103 packet_flush(1);
104
105 while (1) {
106 struct pollfd pfd[2];
Junio C Hamano23d6d112006-09-10 03:33:34 -0700107
Jeff King1bc01ef2011-11-19 02:40:04 -0500108 pfd[0].fd = writer.out;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700109 pfd[0].events = POLLIN;
Jeff King1bc01ef2011-11-19 02:40:04 -0500110 pfd[1].fd = writer.err;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700111 pfd[1].events = POLLIN;
112 if (poll(pfd, 2, -1) < 0) {
113 if (errno != EINTR) {
Nguyễn Thái Ngọc Duy17bef172016-05-08 16:47:33 +0700114 error_errno("poll failed resuming");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700115 sleep(1);
116 }
117 continue;
118 }
Junio C Hamanod3788e12006-09-12 00:26:57 -0700119 if (pfd[1].revents & POLLIN)
Junio C Hamano23d6d112006-09-10 03:33:34 -0700120 /* Status stream ready */
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500121 if (process_input(pfd[1].fd, 2))
122 continue;
123 if (pfd[0].revents & POLLIN)
124 /* Data stream ready */
125 if (process_input(pfd[0].fd, 1))
126 continue;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700127
Jeff King1bc01ef2011-11-19 02:40:04 -0500128 if (finish_command(&writer))
Junio C Hamanod3788e12006-09-12 00:26:57 -0700129 error_clnt("%s", deadchild);
Junio C Hamano23d6d112006-09-10 03:33:34 -0700130 packet_flush(1);
131 break;
132 }
133 return 0;
134}