blob: 1b09e5e1aa3f08e91bf6de731b111864ef405573 [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 "builtin.h"
5#include "archive.h"
Elijah Newrenc3399322023-05-16 06:33:59 +00006#include "path.h"
Franck Bui-Huu39345a22006-09-07 15:12:05 +02007#include "pkt-line.h"
Junio C Hamano23d6d112006-09-10 03:33:34 -07008#include "sideband.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +00009#include "repository.h"
Jeff King1bc01ef2011-11-19 02:40:04 -050010#include "run-command.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040011#include "strvec.h"
Franck Bui-Huu39345a22006-09-07 15:12:05 +020012
13static const char upload_archive_usage[] =
Ævar Arnfjörð Bjarmasonf6a8ef02022-10-13 17:39:10 +020014 "git upload-archive <repository>";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020015
Junio C Hamano23d6d112006-09-10 03:33:34 -070016static const char deadchild[] =
Stephan Beyer1b1dd232008-07-13 15:36:15 +020017"git upload-archive: archiver died with error";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020018
Rene Scharfe7f4d0512008-07-25 12:41:23 +020019#define MAX_ARGS (64)
Junio C Hamano23d6d112006-09-10 03:33:34 -070020
Jeff King1bc01ef2011-11-19 02:40:04 -050021int cmd_upload_archive_writer(int argc, const char **argv, const char *prefix)
Franck Bui-Huu39345a22006-09-07 15:12:05 +020022{
Jeff King22f9b7f2020-07-28 16:24:27 -040023 struct strvec sent_argv = STRVEC_INIT;
Franck Bui-Huu39345a22006-09-07 15:12:05 +020024 const char *arg_cmd = "argument ";
Franck Bui-Huu39345a22006-09-07 15:12:05 +020025
Jeff King619b6c12017-05-30 01:13:43 -040026 if (argc != 2 || !strcmp(argv[1], "-h"))
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080027 usage(upload_archive_usage);
28
Jeff King6379dd02013-02-20 15:00:59 -050029 if (!enter_repo(argv[1], 0))
30 die("'%s' does not appear to be a git repository", argv[1]);
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080031
Josh Steadmon00436bf2018-10-25 13:32:14 -070032 init_archivers();
33
Franck Bui-Huu39345a22006-09-07 15:12:05 +020034 /* put received options in sent_argv[] */
Jeff King22f9b7f2020-07-28 16:24:27 -040035 strvec_push(&sent_argv, "git-upload-archive");
Jeff King090fd4f2013-02-20 15:01:26 -050036 for (;;) {
Jeff King74543a02013-02-20 15:02:57 -050037 char *buf = packet_read_line(0, NULL);
38 if (!buf)
Franck Bui-Huu39345a22006-09-07 15:12:05 +020039 break; /* got a flush */
Jeff Kingd70a9eb2020-07-28 20:37:20 -040040 if (sent_argv.nr > MAX_ARGS)
Jeff King090fd4f2013-02-20 15:01:26 -050041 die("Too many options (>%d)", MAX_ARGS - 1);
Franck Bui-Huu39345a22006-09-07 15:12:05 +020042
Christian Couder59556542013-11-30 21:55:40 +010043 if (!starts_with(buf, arg_cmd))
Jeff King090fd4f2013-02-20 15:01:26 -050044 die("'argument' token or flush expected");
Jeff King22f9b7f2020-07-28 16:24:27 -040045 strvec_push(&sent_argv, buf + strlen(arg_cmd));
Franck Bui-Huu39345a22006-09-07 15:12:05 +020046 }
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080047
48 /* parse all options sent by the client */
Jeff Kingd70a9eb2020-07-28 20:37:20 -040049 return write_archive(sent_argv.nr, sent_argv.v, prefix,
Nguyễn Thái Ngọc Duyb612ee22018-08-13 18:14:35 +020050 the_repository, NULL, 1);
Franck Bui-Huu39345a22006-09-07 15:12:05 +020051}
52
Tarmigan Casebolt28bea9e2009-11-14 13:33:13 -080053__attribute__((format (printf, 1, 2)))
Junio C Hamanod3788e12006-09-12 00:26:57 -070054static void error_clnt(const char *fmt, ...)
55{
Jeff King0cb9d6d2015-09-24 17:07:25 -040056 struct strbuf buf = STRBUF_INIT;
Junio C Hamanod3788e12006-09-12 00:26:57 -070057 va_list params;
Junio C Hamanod3788e12006-09-12 00:26:57 -070058
59 va_start(params, fmt);
Jeff King0cb9d6d2015-09-24 17:07:25 -040060 strbuf_vaddf(&buf, fmt, params);
Junio C Hamanod3788e12006-09-12 00:26:57 -070061 va_end(params);
Jeff King0cb9d6d2015-09-24 17:07:25 -040062 send_sideband(1, 3, buf.buf, buf.len, LARGE_PACKET_MAX);
63 die("sent error to the client: %s", buf.buf);
Junio C Hamanod3788e12006-09-12 00:26:57 -070064}
65
René Scharfe1b19fa42009-06-17 12:11:10 +020066static ssize_t process_input(int child_fd, int band)
Junio C Hamanod3788e12006-09-12 00:26:57 -070067{
68 char buf[16384];
69 ssize_t sz = read(child_fd, buf, sizeof(buf));
70 if (sz < 0) {
Andy Whitcroft93d26e42007-01-08 15:58:08 +000071 if (errno != EAGAIN && errno != EINTR)
Junio C Hamanod3788e12006-09-12 00:26:57 -070072 error_clnt("read error: %s\n", strerror(errno));
René Scharfe1b19fa42009-06-17 12:11:10 +020073 return sz;
Junio C Hamanod3788e12006-09-12 00:26:57 -070074 }
75 send_sideband(1, band, buf, sz, LARGE_PACKET_MAX);
René Scharfe1b19fa42009-06-17 12:11:10 +020076 return sz;
Junio C Hamanod3788e12006-09-12 00:26:57 -070077}
78
Junio C Hamano23d6d112006-09-10 03:33:34 -070079int cmd_upload_archive(int argc, const char **argv, const char *prefix)
80{
Ævar Arnfjörð Bjarmasonc8a4cd552021-11-25 23:52:17 +010081 struct child_process writer = CHILD_PROCESS_INIT;
Jeff King1bc01ef2011-11-19 02:40:04 -050082
Jeff King79156912023-03-28 16:55:17 -040083 BUG_ON_NON_EMPTY_PREFIX(prefix);
84
Jeff King619b6c12017-05-30 01:13:43 -040085 if (argc == 2 && !strcmp(argv[1], "-h"))
86 usage(upload_archive_usage);
87
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080088 /*
89 * Set up sideband subprocess.
90 *
91 * We (parent) monitor and read from child, sending its fd#1 and fd#2
92 * multiplexed out to our fd#1. If the child dies, we tell the other
93 * end over channel #3.
94 */
Jeff King1bc01ef2011-11-19 02:40:04 -050095 writer.out = writer.err = -1;
96 writer.git_cmd = 1;
Ævar Arnfjörð Bjarmasonc8a4cd552021-11-25 23:52:17 +010097 strvec_push(&writer.args, "upload-archive--writer");
98 strvec_pushv(&writer.args, argv + 1);
Jeff King1bc01ef2011-11-19 02:40:04 -050099 if (start_command(&writer)) {
Junio C Hamanof0c7fd42011-11-15 15:39:33 -0800100 int err = errno;
Lars Schneider81c634e2016-10-16 16:20:29 -0700101 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
Junio C Hamanof0c7fd42011-11-15 15:39:33 -0800102 die("upload-archive: %s", strerror(err));
103 }
Junio C Hamanof0c7fd42011-11-15 15:39:33 -0800104
Lars Schneider81c634e2016-10-16 16:20:29 -0700105 packet_write_fmt(1, "ACK\n");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700106 packet_flush(1);
107
108 while (1) {
109 struct pollfd pfd[2];
Junio C Hamano23d6d112006-09-10 03:33:34 -0700110
Jeff King1bc01ef2011-11-19 02:40:04 -0500111 pfd[0].fd = writer.out;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700112 pfd[0].events = POLLIN;
Jeff King1bc01ef2011-11-19 02:40:04 -0500113 pfd[1].fd = writer.err;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700114 pfd[1].events = POLLIN;
115 if (poll(pfd, 2, -1) < 0) {
116 if (errno != EINTR) {
Nguyễn Thái Ngọc Duy17bef172016-05-08 16:47:33 +0700117 error_errno("poll failed resuming");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700118 sleep(1);
119 }
120 continue;
121 }
Junio C Hamanod3788e12006-09-12 00:26:57 -0700122 if (pfd[1].revents & POLLIN)
Junio C Hamano23d6d112006-09-10 03:33:34 -0700123 /* Status stream ready */
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500124 if (process_input(pfd[1].fd, 2))
125 continue;
126 if (pfd[0].revents & POLLIN)
127 /* Data stream ready */
128 if (process_input(pfd[0].fd, 1))
129 continue;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700130
Jeff King1bc01ef2011-11-19 02:40:04 -0500131 if (finish_command(&writer))
Junio C Hamanod3788e12006-09-12 00:26:57 -0700132 error_clnt("%s", deadchild);
Junio C Hamano23d6d112006-09-10 03:33:34 -0700133 packet_flush(1);
134 break;
135 }
136 return 0;
137}