[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Make read_all distinguish between error and EOF; read_file_...



Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv23970/common

Modified Files:
	util.c 
Log Message:
Make read_all distinguish between error and EOF; read_file_to_string should only check bytes read against st_size when in binary mode.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.133
retrieving revision 1.134
diff -u -d -r1.133 -r1.134
--- util.c	10 Sep 2004 21:39:53 -0000	1.133
+++ util.c	26 Sep 2004 16:51:31 -0000	1.134
@@ -1087,7 +1087,9 @@
   return count;
 }
 
-/** Read <b>count</b> bytes from <b>fd</b> to <b>buf</b>.  isSocket must be 1 if fd
+/** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
+ * or reach the end of the file.
+ * isSocket must be 1 if fd
  * was returned by socket() or accept(), and 0 if fd was returned by
  * open().  Return the number of bytes read, or -1 on error. Only use
  * if fd is a blocking fd. */
@@ -1100,8 +1102,10 @@
       result = recv(fd, buf+numread, count-numread, 0);
     else
       result = read(fd, buf+numread, count-numread);
-    if(result<=0)
+    if(result<0)
       return -1;
+    else if (result == 0)
+      break;
     numread += result;
   }
   return count;
@@ -1525,6 +1529,7 @@
   int fd; /* router file */
   struct stat statbuf;
   char *string;
+  int r;
 
   tor_assert(filename);
 
@@ -1541,10 +1546,19 @@
 
   string = tor_malloc(statbuf.st_size+1);
 
-  if(read_all(fd,string,statbuf.st_size,0) != statbuf.st_size) {
+  r = read_all(fd,string,statbuf.st_size,0);
+  if (r<0) {
+    log_fn(LOG_WARN,"Error reading from file '%s': %s", filename,
+           strerror(errno));
+    tor_free(string);
+    close(fd);
+    return NULL;
+  } else if (bin && r != statbuf.st_size) {
+    /* If we're in binary mode, then we'd better have an exact match for
+     * size.  Otherwise, win32 encoding may throw us off, and that's okay. */
     log_fn(LOG_WARN,"Couldn't read all %ld bytes of file '%s'.",
            (long)statbuf.st_size,filename);
-    free(string);
+    tor_free(string);
     close(fd);
     return NULL;
   }