* 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
Родитель 52b9b48a2c 4c324c0050
Коммит 40e2524da9
2 изменённых файлов: 154 добавлений и 117 удалений

75
t/t5530-upload-pack-error.sh Executable 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

Просмотреть файл

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