Merge branch 'js/upload-pack'

* js/upload-pack:
  upload-pack: Use finish_{command,async}() instead of waitpid().
This commit is contained in:
Junio C Hamano 2007-11-11 15:19:57 -08:00
commit 40e2524da9
2 changed files with 154 additions and 117 deletions

75
t/t5530-upload-pack-error.sh Executable file
View File

@ -0,0 +1,75 @@
#!/bin/sh
test_description='errors in upload-pack'
. ./test-lib.sh
D=`pwd`
corrupt_repo () {
object_sha1=$(git rev-parse "$1") &&
ob=$(expr "$object_sha1" : "\(..\)") &&
ject=$(expr "$object_sha1" : "..\(..*\)") &&
rm -f ".git/objects/$ob/$ject"
}
test_expect_success 'setup and corrupt repository' '
echo file >file &&
git add file &&
git rev-parse :file &&
git commit -a -m original &&
test_tick &&
echo changed >file &&
git commit -a -m changed &&
corrupt_repo HEAD:file
'
test_expect_failure 'fsck fails' '
git fsck
'
test_expect_success 'upload-pack fails due to error in pack-objects' '
! echo "0032want $(git rev-parse HEAD)
00000009done
0000" | git-upload-pack . > /dev/null 2> output.err &&
grep "pack-objects died" output.err
'
test_expect_success 'corrupt repo differently' '
git hash-object -w file &&
corrupt_repo HEAD^^{tree}
'
test_expect_failure 'fsck fails' '
git fsck
'
test_expect_success 'upload-pack fails due to error in rev-list' '
! echo "0032want $(git rev-parse HEAD)
00000009done
0000" | git-upload-pack . > /dev/null 2> output.err &&
grep "waitpid (async) failed" output.err
'
test_expect_success 'create empty repository' '
mkdir foo &&
cd foo &&
git init
'
test_expect_failure 'fetch fails' '
git fetch .. master
'
test_done

View File

@ -144,6 +144,7 @@ static void create_pack_file(void)
char abort_msg[] = "aborting due to possible repository " char abort_msg[] = "aborting due to possible repository "
"corruption on the remote side."; "corruption on the remote side.";
int buffered = -1; int buffered = -1;
ssize_t sz;
const char *argv[10]; const char *argv[10];
int arg = 0; int arg = 0;
@ -168,22 +169,15 @@ static void create_pack_file(void)
pack_objects.git_cmd = 1; pack_objects.git_cmd = 1;
pack_objects.argv = argv; pack_objects.argv = argv;
if (start_command(&pack_objects)) { if (start_command(&pack_objects))
/* daemon sets things up to ignore TERM */
kill(rev_list.pid, SIGKILL);
die("git-upload-pack: unable to fork git-pack-objects"); die("git-upload-pack: unable to fork git-pack-objects");
}
/* We read from pack_objects.err to capture stderr output for /* We read from pack_objects.err to capture stderr output for
* progress bar, and pack_objects.out to capture the pack data. * progress bar, and pack_objects.out to capture the pack data.
*/ */
while (1) { while (1) {
const char *who;
struct pollfd pfd[2]; struct pollfd pfd[2];
pid_t pid;
int status;
ssize_t sz;
int pe, pu, pollsize; int pe, pu, pollsize;
reset_timeout(); reset_timeout();
@ -204,7 +198,9 @@ static void create_pack_file(void)
pollsize++; pollsize++;
} }
if (pollsize) { if (!pollsize)
break;
if (poll(pfd, pollsize, -1) < 0) { if (poll(pfd, pollsize, -1) < 0) {
if (errno != EINTR) { if (errno != EINTR) {
error("poll failed, resuming: %s", error("poll failed, resuming: %s",
@ -214,16 +210,14 @@ static void create_pack_file(void)
continue; continue;
} }
if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) { if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
/* Data ready; we keep the last byte /* Data ready; we keep the last byte to ourselves
* to ourselves in case we detect * in case we detect broken rev-list, so that we
* broken rev-list, so that we can * can leave the stream corrupted. This is
* leave the stream corrupted. This * unfortunate -- unpack-objects would happily
* is unfortunate -- unpack-objects * accept a valid packdata with trailing garbage,
* would happily accept a valid pack * so appending garbage after we pass all the
* data with trailing garbage, so * pack data is not good enough to signal
* appending garbage after we pass all * breakage to downstream.
* the pack data is not good enough to
* signal breakage to downstream.
*/ */
char *cp = data; char *cp = data;
ssize_t outsz = 0; ssize_t outsz = 0;
@ -269,40 +263,12 @@ static void create_pack_file(void)
} }
} }
/* See if the children are still there */ if (finish_command(&pack_objects)) {
if (rev_list.pid || pack_objects.pid) { error("git-upload-pack: git-pack-objects died with error.");
pid = waitpid(-1, &status, WNOHANG);
if (!pid)
continue;
who = ((pid == rev_list.pid) ? "git-rev-list" :
(pid == pack_objects.pid) ? "git-pack-objects" :
NULL);
if (!who) {
if (pid < 0) {
error("git-upload-pack: %s",
strerror(errno));
goto fail; goto fail;
} }
error("git-upload-pack: we weren't " if (finish_async(&rev_list))
"waiting for %d", pid); goto fail; /* error was already reported */
continue;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) > 0) {
error("git-upload-pack: %s died with error.",
who);
goto fail;
}
if (pid == rev_list.pid)
rev_list.pid = 0;
if (pid == pack_objects.pid)
pack_objects.pid = 0;
if (rev_list.pid || pack_objects.pid)
continue;
}
/* both died happily */
if (pollsize)
continue;
/* flush the data */ /* flush the data */
if (0 <= buffered) { if (0 <= buffered) {
@ -315,12 +281,8 @@ static void create_pack_file(void)
if (use_sideband) if (use_sideband)
packet_flush(1); packet_flush(1);
return; return;
}
fail: fail:
if (pack_objects.pid)
kill(pack_objects.pid, SIGKILL);
if (rev_list.pid)
kill(rev_list.pid, SIGKILL);
send_client_data(3, abort_msg, sizeof(abort_msg)); send_client_data(3, abort_msg, sizeof(abort_msg));
die("git-upload-pack: %s", abort_msg); die("git-upload-pack: %s", abort_msg);
} }