From c1e66b2a4e2ccf3fc4f3396d97254fa058c6ae73 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sat, 14 Jan 2012 18:31:25 +0000 Subject: [PATCH] -LRN: fix pipe writing and progress write --- src/fs/fs_dirmetascan.c | 50 ++++++++++++++--------------- src/util/disk.c | 70 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/fs/fs_dirmetascan.c b/src/fs/fs_dirmetascan.c index 372579ccb..880710356 100644 --- a/src/fs/fs_dirmetascan.c +++ b/src/fs/fs_dirmetascan.c @@ -346,61 +346,61 @@ write_progress (struct AddDirContext *adc, const char *filename, char is_directory, enum GNUNET_DirScannerProgressUpdateReason reason) { size_t filename_len; - size_t wr; + ssize_t wr; size_t total_write; if ((adc->do_stop || should_stop (adc)) && reason != GNUNET_DIR_SCANNER_ASKED_TO_STOP && reason != GNUNET_DIR_SCANNER_FINISHED) return 1; - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, - &reason, sizeof (reason)); - while (wr > 0 && total_write < sizeof (reason)) + total_write = 0; + wr = 1; + while ((wr > 0 || errno == EAGAIN) && total_write < sizeof (reason)) { - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, + wr = GNUNET_DISK_file_write (adc->progress_write, &((char *)&reason)[total_write], sizeof (reason) - total_write); if (wr > 0) total_write += wr; } - if (sizeof (reason) != wr) - return 1; + if (sizeof (reason) != total_write) + return adc->do_stop = 1; if (filename) filename_len = strlen (filename) + 1; else filename_len = 0; - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, - &filename_len, sizeof (size_t)); - while (wr > 0 && total_write < sizeof (size_t)) + total_write = 0; + wr = 1; + while ((wr > 0 || errno == EAGAIN) && total_write < sizeof (size_t)) { - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, + wr = GNUNET_DISK_file_write (adc->progress_write, &((char *)&filename_len)[total_write], sizeof (size_t) - total_write); if (wr > 0) total_write += wr; } - if (sizeof (size_t) != wr) - return 1; + if (sizeof (size_t) != total_write) + return adc->do_stop = 1; if (filename) { - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, - filename, filename_len); - while (wr > 0 && total_write < filename_len) + total_write = 0; + wr = 1; + while ((wr > 0 || errno == EAGAIN) && total_write < filename_len) { - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, + wr = GNUNET_DISK_file_write (adc->progress_write, &((char *)filename)[total_write], filename_len - total_write); if (wr > 0) total_write += wr; } - if (filename_len != wr) - return 1; - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, - &is_directory, sizeof (char)); - while (wr > 0 && total_write < sizeof (char)) + if (filename_len != total_write) + return adc->do_stop = 1; + total_write = 0; + wr = 1; + while ((wr > 0 || errno == EAGAIN) && total_write < sizeof (char)) { - total_write = wr = GNUNET_DISK_file_write (adc->progress_write, + wr = GNUNET_DISK_file_write (adc->progress_write, &((char *)&is_directory)[total_write], sizeof (char) - total_write); if (wr > 0) total_write += wr; } - if (sizeof (char) != wr) - return 1; + if (sizeof (char) != total_write) + return adc->do_stop = 1; } return 0; } diff --git a/src/util/disk.c b/src/util/disk.c index 8d1fe3c12..eb707fd62 100644 --- a/src/util/disk.c +++ b/src/util/disk.c @@ -718,15 +718,27 @@ GNUNET_DISK_file_read (const struct GNUNET_DISK_FileHandle * h, void *result, } else { - if (!ReadFile (h->h, result, len, NULL, h->oOverlapRead)) +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to read\n"); +#endif + if (!ReadFile (h->h, result, len, &bytesRead, h->oOverlapRead)) { if (GetLastError () != ERROR_IO_PENDING) { +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Error reading from pipe: %u\n", GetLastError ()); +#endif SetErrnoFromWinError (GetLastError ()); return GNUNET_SYSERR; } +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); +#endif + GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); } - GetOverlappedResult (h->h, h->oOverlapRead, &bytesRead, TRUE); +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Read %u bytes\n", bytesRead); +#endif } return bytesRead; #else @@ -790,23 +802,67 @@ GNUNET_DISK_file_write (const struct GNUNET_DISK_FileHandle * h, else { #if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "It is a pipe trying to write %u bytes\n", n); #endif - if (!WriteFile (h->h, buffer, n, NULL, h->oOverlapWrite)) + if (!WriteFile (h->h, buffer, n, &bytesWritten, h->oOverlapWrite)) { if (GetLastError () != ERROR_IO_PENDING) { SetErrnoFromWinError (GetLastError ()); #if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Error writing to pipe: %u\n", + GetLastError ()); +#endif + return GNUNET_SYSERR; + } +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); +#endif + if (!GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE)) + { + SetErrnoFromWinError (GetLastError ()); +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Error getting overlapped result while writing to pipe: %u\n", + GetLastError ()); +#endif + return GNUNET_SYSERR; + } + } + else + { + DWORD ovr; + if (!GetOverlappedResult (h->h, h->oOverlapWrite, &ovr, TRUE)) + { +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Error getting control overlapped result while writing to pipe: %u\n", + GetLastError ()); +#endif + } + else + { +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, + "Wrote %u bytes (ovr says %u), picking the greatest\n", + bytesWritten, ovr); +#endif + } + } + if (bytesWritten == 0) + { + if (n > 0) + { +#if DEBUG_PIPE + LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes, returning -1 with EAGAIN\n", bytesWritten); #endif + errno = EAGAIN; return GNUNET_SYSERR; } } #if DEBUG_PIPE - LOG (GNUNET_ERROR_TYPE_DEBUG, "Will get overlapped result\n"); + LOG (GNUNET_ERROR_TYPE_DEBUG, "Wrote %u bytes\n", bytesWritten); #endif - GetOverlappedResult (h->h, h->oOverlapWrite, &bytesWritten, TRUE); } return bytesWritten; #else -- 2.25.1