Cygwin: pipe_data_available: cleanup code

* Don't use a bool var to store three states (-1, 0, 1).
* Correctly check for NT_SUCCESS of a function returning NTSTATUS.
* Straighten out code for better readability.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2017-11-15 20:11:05 +01:00
parent 0f88d21e4d
commit 57732f9b4b

View File

@ -568,45 +568,58 @@ pipe_data_available (int fd, fhandler_base *fh, HANDLE h, bool writing)
{ {
IO_STATUS_BLOCK iosb = {{0}, 0}; IO_STATUS_BLOCK iosb = {{0}, 0};
FILE_PIPE_LOCAL_INFORMATION fpli = {0}; FILE_PIPE_LOCAL_INFORMATION fpli = {0};
NTSTATUS status;
bool res;
if (fh->has_ongoing_io ()) if (fh->has_ongoing_io ())
res = false; return 0;
else if (NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
FilePipeLocalInformation)) status = NtQueryInformationFile (h, &iosb, &fpli, sizeof (fpli),
FilePipeLocalInformation);
if (!NT_SUCCESS (status))
{ {
/* If NtQueryInformationFile fails, optimistically assume the /* If NtQueryInformationFile fails, optimistically assume the
pipe is writable. This could happen if we somehow pipe is writable. This could happen if we somehow
inherit a pipe that doesn't permit FILE_READ_ATTRIBUTES inherit a pipe that doesn't permit FILE_READ_ATTRIBUTES
access on the write end. */ access on the write end. */
select_printf ("fd %d, %s, NtQueryInformationFile failed", select_printf ("fd %d, %s, NtQueryInformationFile failed, status %y",
fd, fh->get_name ()); fd, fh->get_name (), status);
res = writing ? true : -1; return writing ? 1 : -1;
} }
else if (!writing) if (writing)
{ {
paranoid_printf ("fd %d, %s, read avail %u", fd, fh->get_name (),
fpli.ReadDataAvailable);
res = !!fpli.ReadDataAvailable;
}
else if ((res = (fpli.WriteQuotaAvailable = (fpli.OutboundQuota -
fpli.ReadDataAvailable))))
/* If there is anything available in the pipe buffer then signal /* If there is anything available in the pipe buffer then signal
that. This means that a pipe could still block since you could that. This means that a pipe could still block since you could
be trying to write more to the pipe than is available in the be trying to write more to the pipe than is available in the
buffer but that is the hazard of select(). */ buffer but that is the hazard of select(). */
fpli.WriteQuotaAvailable = fpli.OutboundQuota - fpli.ReadDataAvailable;
if (fpli.WriteQuotaAvailable > 0)
{
paranoid_printf ("fd %d, %s, write: size %u, avail %u", fd, paranoid_printf ("fd %d, %s, write: size %u, avail %u", fd,
fh->get_name (), fpli.OutboundQuota, fh->get_name (), fpli.OutboundQuota,
fpli.WriteQuotaAvailable); fpli.WriteQuotaAvailable);
else if ((res = (fpli.OutboundQuota < PIPE_BUF && return 1;
fpli.WriteQuotaAvailable == fpli.OutboundQuota))) }
/* If we somehow inherit a tiny pipe (size < PIPE_BUF), then consider /* If we somehow inherit a tiny pipe (size < PIPE_BUF), then consider
the pipe writable only if it is completely empty, to minimize the the pipe writable only if it is completely empty, to minimize the
probability that a subsequent write will block. */ probability that a subsequent write will block. */
if (fpli.OutboundQuota < PIPE_BUF
&& fpli.WriteQuotaAvailable == fpli.OutboundQuota)
{
select_printf ("fd, %s, write tiny pipe: size %u, avail %u", select_printf ("fd, %s, write tiny pipe: size %u, avail %u",
fd, fh->get_name (), fpli.OutboundQuota, fd, fh->get_name (), fpli.OutboundQuota,
fpli.WriteQuotaAvailable); fpli.WriteQuotaAvailable);
return res ?: -!!(fpli.NamedPipeState & FILE_PIPE_CLOSING_STATE); return 1;
}
}
else if (fpli.ReadDataAvailable)
{
paranoid_printf ("fd %d, %s, read avail %u", fd, fh->get_name (),
fpli.ReadDataAvailable);
return 1;
}
if (fpli.NamedPipeState & FILE_PIPE_CLOSING_STATE)
return -1;
return 0;
} }
static int static int