Rework spawn's output pipe to avoid writing rubbish
authorJ. Ali Harlow <ali@juiblex.co.uk>
Wed Aug 26 22:34:00 2009 +0100 (2009-08-26)
changeset 662d265f584c4
parent 5 0ea90868d987
child 7 565f6683213f
Rework spawn's output pipe to avoid writing rubbish
configure.ac
whelk/spawn.c
     1.1 --- a/configure.ac	Wed Aug 26 14:48:19 2009 +0100
     1.2 +++ b/configure.ac	Wed Aug 26 22:34:00 2009 +0100
     1.3 @@ -27,7 +27,7 @@
     1.4  # See http://sources.redhat.com/autobook/autobook/autobook_91.html#SEC91 for details
     1.5  #
     1.6  LT_CURRENT=1
     1.7 -LT_REVISION=0
     1.8 +LT_REVISION=1
     1.9  LT_AGE=1
    1.10  AC_SUBST(LT_CURRENT)
    1.11  AC_SUBST(LT_REVISION)
     2.1 --- a/whelk/spawn.c	Wed Aug 26 14:48:19 2009 +0100
     2.2 +++ b/whelk/spawn.c	Wed Aug 26 22:34:00 2009 +0100
     2.3 @@ -109,8 +109,11 @@
     2.4      whelk_wait_add_object(&wait,stdout_pipe[0]);
     2.5      whelk_wait_add_object(&wait,stderr_pipe[0]);
     2.6      whelk_wait_add_object(&wait,pi.hProcess);
     2.7 -    if (standard_input)
     2.8 +    if (standard_input && stdin_len)
     2.9 +    {
    2.10 +	CloseHandle(stdin_pipe[0]);
    2.11  	whelk_wait_add_object(&wait,stdin_pipe[1]);
    2.12 +    }
    2.13      else
    2.14      {
    2.15  	whelk_close_pipe(stdin_pipe);
    2.16 @@ -141,21 +144,24 @@
    2.17  	    break;
    2.18  	else if (h==stdin_pipe[1])
    2.19  	{
    2.20 -	    nb=max(stdin_len,512);
    2.21 -	    WriteFile(stdin_pipe[1],standard_input,nb,NULL,NULL);
    2.22 -	    standard_input+=nb;
    2.23 -	    stdin_len-=nb;
    2.24 +	    if (WriteFile(stdin_pipe[1],standard_input,stdin_len,&nb,NULL))
    2.25 +	    {
    2.26 +		standard_input+=nb;
    2.27 +		stdin_len-=nb;
    2.28 +	    }
    2.29 +	    else
    2.30 +		stdin_len=0;
    2.31  	    if (!stdin_len)
    2.32  	    {
    2.33  		whelk_wait_remove_object(&wait,stdin_pipe[1]);
    2.34 -		whelk_close_pipe(stdin_pipe);
    2.35 -		standard_input=NULL;
    2.36 +		CloseHandle(stdin_pipe[1]);
    2.37 +		stdin_pipe[1]=INVALID_HANDLE_VALUE;
    2.38  	    }
    2.39  	}
    2.40      }
    2.41      whelk_wait_free(&wait);
    2.42 -    if (standard_input)
    2.43 -	whelk_close_pipe(stdin_pipe);
    2.44 +    if (stdin_pipe[1]!=INVALID_HANDLE_VALUE)
    2.45 +	CloseHandle(stdin_pipe[1]);
    2.46      if (GetExitCodeProcess(pi.hProcess,&nb))
    2.47  	exitcode=(int)nb;
    2.48      else