[Udpcast] checking write() return value

Kyle Cordes kyle at kylecordes.com
Wed Oct 29 23:22:59 CET 2008


Trying to track down the problem I posted about earlier, I made a few 
tiny changes, including an assert-style check on write().

This case this code worries about, does not occur in my test with a ~50 
GB file.  Still, code like this is generally a good idea, rather than 
assuming that write() wrote the expected amount of data.

Kyle



diff --git a/receiver-diskio.c b/receiver-diskio.c
index 2076ba2..500ac2a 100644
--- a/receiver-diskio.c
+++ b/receiver-diskio.c
@@ -43,14 +43,18 @@ int writer(struct fifo *fifo, int outFile) {
          * liberate small chunks one by one rather than attempt to
          * write out a bigger chunk and block reception for too
          * long */
-       if (bytes > 128 * 1024)
+       if (bytes > 64 * 1024)
             bytes = 64 * 1024;

-       bytes = write(outFile, fifo->dataBuffer + pos, bytes);
-       if(bytes < 0) {
+       int bytesWritten = write(outFile, fifo->dataBuffer + pos, bytes);
+       if(bytesWritten < 0) {
             perror("write");
             exit(1);
         }
+       if(bytesWritten != bytes) {
+           perror("write, bytesWritten != bytes");
+           exit(1);
+       }
         pc_consumed(fifo->data, bytes);
         pc_produce(fifo->freeMemQueue, bytes);
      }



-- 
Kyle Cordes
http://kylecordes.com


More information about the Udpcast mailing list