From: Junio C Hamano Date: Wed, 30 Oct 2013 19:10:45 +0000 (-0700) Subject: Merge branch 'jl/pack-transfer-avoid-double-close' X-Git-Tag: v1.8.5-rc0~7 X-Git-Url: https://git.lorimer.id.au/gitweb.git/diff_plain/832ee79ab89d5908bcfd6fda89eacb0e665de2ef?ds=inline;hp=-c Merge branch 'jl/pack-transfer-avoid-double-close' The codepath that send_pack() calls pack_objects() mistakenly closed the same file descriptor twice, leading to potentially closing a wrong file descriptor that was opened in the meantime. * jl/pack-transfer-avoid-double-close: Clear fd after closing to avoid double-close error --- 832ee79ab89d5908bcfd6fda89eacb0e665de2ef diff --combined fetch-pack.c index a0e0350ae6,29b711a98b..1042448fa0 --- a/fetch-pack.c +++ b/fetch-pack.c @@@ -9,7 -9,6 +9,7 @@@ #include "fetch-pack.h" #include "remote.h" #include "run-command.h" +#include "connect.h" #include "transport.h" #include "version.h" #include "prio-queue.h" @@@ -185,6 -184,36 +185,6 @@@ static void consume_shallow_list(struc } } -struct write_shallow_data { - struct strbuf *out; - int use_pack_protocol; - int count; -}; - -static int write_one_shallow(const struct commit_graft *graft, void *cb_data) -{ - struct write_shallow_data *data = cb_data; - const char *hex = sha1_to_hex(graft->sha1); - data->count++; - if (data->use_pack_protocol) - packet_buf_write(data->out, "shallow %s", hex); - else { - strbuf_addstr(data->out, hex); - strbuf_addch(data->out, '\n'); - } - return 0; -} - -static int write_shallow_commits(struct strbuf *out, int use_pack_protocol) -{ - struct write_shallow_data data; - data.out = out; - data.use_pack_protocol = use_pack_protocol; - data.count = 0; - for_each_commit_graft(write_one_shallow, &data); - return data.count; -} - static enum ack_type get_ack(int fd, unsigned char *result_sha1) { int len; @@@ -659,7 -688,7 +659,7 @@@ static int get_pack(struct fetch_pack_a const char *argv[22]; char keep_arg[256]; char hdr_arg[256]; - const char **av; + const char **av, *cmd_name; int do_keep = args->keep_pack; struct child_process cmd; int ret; @@@ -706,7 -735,7 +706,7 @@@ if (do_keep) { if (pack_lockfile) cmd.out = -1; - *av++ = "index-pack"; + *av++ = cmd_name = "index-pack"; *av++ = "--stdin"; if (!args->quiet && !args->no_progress) *av++ = "-v"; @@@ -723,7 -752,7 +723,7 @@@ *av++ = "--check-self-contained-and-connected"; } else { - *av++ = "unpack-objects"; + *av++ = cmd_name = "unpack-objects"; if (args->quiet || args->no_progress) *av++ = "-q"; args->check_self_contained_and_connected = 0; @@@ -741,19 -770,23 +741,23 @@@ cmd.in = demux.out; cmd.git_cmd = 1; if (start_command(&cmd)) - die("fetch-pack: unable to fork off %s", argv[0]); + die("fetch-pack: unable to fork off %s", cmd_name); if (do_keep && pack_lockfile) { *pack_lockfile = index_pack_lockfile(cmd.out); close(cmd.out); } + if (!use_sideband) + /* Closed by start_command() */ + xd[0] = -1; + ret = finish_command(&cmd); if (!ret || (args->check_self_contained_and_connected && ret == 1)) args->self_contained_and_connected = args->check_self_contained_and_connected && ret == 0; else - die("%s failed", argv[0]); + die("%s failed", cmd_name); if (use_sideband && finish_async(&demux)) die("error in sideband demultiplexer"); return 0; @@@ -766,6 -799,27 +770,6 @@@ static int cmp_ref_by_name(const void * return strcmp(a->name, b->name); } -static void setup_alternate_shallow(void) -{ - struct strbuf sb = STRBUF_INIT; - int fd; - - check_shallow_file_for_update(); - fd = hold_lock_file_for_update(&shallow_lock, git_path("shallow"), - LOCK_DIE_ON_ERROR); - if (write_shallow_commits(&sb, 0)) { - if (write_in_full(fd, sb.buf, sb.len) != sb.len) - die_errno("failed to write to %s", shallow_lock.filename); - alternate_shallow_file = shallow_lock.filename; - } else - /* - * is_repository_shallow() sees empty string as "no - * shallow file". - */ - alternate_shallow_file = ""; - strbuf_release(&sb); -} - static struct ref *do_fetch_pack(struct fetch_pack_args *args, int fd[2], const struct ref *orig_ref, @@@ -846,7 -900,7 +850,7 @@@ if (args->stateless_rpc) packet_flush(fd[1]); if (args->depth > 0) - setup_alternate_shallow(); + setup_alternate_shallow(&shallow_lock, &alternate_shallow_file); else alternate_shallow_file = NULL; if (get_pack(args, fd, pack_lockfile)) diff --combined send-pack.c index b228d65613,edbfd076a9..fab62e3da0 --- a/send-pack.c +++ b/send-pack.c @@@ -5,7 -5,6 +5,7 @@@ #include "sideband.h" #include "run-command.h" #include "remote.h" +#include "connect.h" #include "send-pack.h" #include "quote.h" #include "transport.h" @@@ -227,7 -226,6 +227,7 @@@ int send_pack(struct send_pack_args *ar case REF_STATUS_REJECT_ALREADY_EXISTS: case REF_STATUS_REJECT_FETCH_FIRST: case REF_STATUS_REJECT_NEEDS_FORCE: + case REF_STATUS_REJECT_STALE: case REF_STATUS_UPTODATE: continue; default: @@@ -302,8 -300,12 +302,12 @@@ shutdown(fd[0], SHUT_WR); if (use_sideband) finish_async(&demux); + fd[1] = -1; return -1; } + if (!args->stateless_rpc) + /* Closed by pack_objects() via start_command() */ + fd[1] = -1; } if (args->stateless_rpc && cmds_sent) packet_flush(out);