blob: 018879737aeedc245a1473247df16ed0dcd26e50 [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 King090fd4f2013-02-20 15:01:26 -050010#include "argv-array.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 King090fd4f2013-02-20 15:01:26 -050022 struct argv_array sent_argv = ARGV_ARRAY_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 King090fd4f2013-02-20 15:01:26 -050034 argv_array_push(&sent_argv, "git-upload-archive");
35 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 King090fd4f2013-02-20 15:01:26 -050039 if (sent_argv.argc > MAX_ARGS)
40 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");
44 argv_array_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 */
Nguyễn Thái Ngọc Duyb612ee22018-08-13 18:14:35 +020048 return write_archive(sent_argv.argc, sent_argv.argv, prefix,
49 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{
Jeff King1bc01ef2011-11-19 02:40:04 -050080 struct child_process writer = { argv };
81
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 argv[0] = "upload-archive--writer";
93 writer.out = writer.err = -1;
94 writer.git_cmd = 1;
95 if (start_command(&writer)) {
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080096 int err = errno;
Lars Schneider81c634e2016-10-16 16:20:29 -070097 packet_write_fmt(1, "NACK unable to spawn subprocess\n");
Junio C Hamanof0c7fd42011-11-15 15:39:33 -080098 die("upload-archive: %s", strerror(err));
99 }
Junio C Hamanof0c7fd42011-11-15 15:39:33 -0800100
Lars Schneider81c634e2016-10-16 16:20:29 -0700101 packet_write_fmt(1, "ACK\n");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700102 packet_flush(1);
103
104 while (1) {
105 struct pollfd pfd[2];
Junio C Hamano23d6d112006-09-10 03:33:34 -0700106
Jeff King1bc01ef2011-11-19 02:40:04 -0500107 pfd[0].fd = writer.out;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700108 pfd[0].events = POLLIN;
Jeff King1bc01ef2011-11-19 02:40:04 -0500109 pfd[1].fd = writer.err;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700110 pfd[1].events = POLLIN;
111 if (poll(pfd, 2, -1) < 0) {
112 if (errno != EINTR) {
Nguyễn Thái Ngọc Duy17bef172016-05-08 16:47:33 +0700113 error_errno("poll failed resuming");
Junio C Hamano23d6d112006-09-10 03:33:34 -0700114 sleep(1);
115 }
116 continue;
117 }
Junio C Hamanod3788e12006-09-12 00:26:57 -0700118 if (pfd[1].revents & POLLIN)
Junio C Hamano23d6d112006-09-10 03:33:34 -0700119 /* Status stream ready */
Nicolas Pitre6b59f512009-11-11 17:24:42 -0500120 if (process_input(pfd[1].fd, 2))
121 continue;
122 if (pfd[0].revents & POLLIN)
123 /* Data stream ready */
124 if (process_input(pfd[0].fd, 1))
125 continue;
Junio C Hamano23d6d112006-09-10 03:33:34 -0700126
Jeff King1bc01ef2011-11-19 02:40:04 -0500127 if (finish_command(&writer))
Junio C Hamanod3788e12006-09-12 00:26:57 -0700128 error_clnt("%s", deadchild);
Junio C Hamano23d6d112006-09-10 03:33:34 -0700129 packet_flush(1);
130 break;
131 }
132 return 0;
133}