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

[or-cvs] r12153: Make tor_mmap_file() set and preserve errno in a useful way. (in tor/trunk: . src/common)



Author: nickm
Date: 2007-10-24 11:45:42 -0400 (Wed, 24 Oct 2007)
New Revision: 12153

Modified:
   tor/trunk/
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/util.c
Log:
 r16100@catbus:  nickm | 2007-10-24 11:33:52 -0400
 Make tor_mmap_file() set and preserve errno in a useful way.



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r16100] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2007-10-24 14:28:21 UTC (rev 12152)
+++ tor/trunk/src/common/compat.c	2007-10-24 15:45:42 UTC (rev 12153)
@@ -126,7 +126,8 @@
                         * size, rounded up to the nearest page.) */
 } tor_mmap_impl_t;
 /** Try to create a memory mapping for <b>filename</b> and return it.  On
- * failure, return NULL. */
+ * failure, return NULL.  Sets errno properly, using ERANGE to mean
+ * "empty file". */
 tor_mmap_t *
 tor_mmap_file(const char *filename)
 {
@@ -140,9 +141,11 @@
 
   fd = open(filename, O_RDONLY, 0);
   if (fd<0) {
+    int save_errno = errno;
     int severity = (errno == ENOENT) ? LOG_INFO : LOG_WARN;
     log_fn(severity, LD_FS,"Could not open \"%s\" for mmap(): %s",filename,
            strerror(errno));
+    errno = save_errno;
     return NULL;
   }
 
@@ -156,14 +159,17 @@
     /* Zero-length file. If we call mmap on it, it will succeed but
      * return NULL, and bad things will happen. So just fail. */
     log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
+    errno = ERANGE;
     return NULL;
   }
 
   string = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, 0);
   if (string == MAP_FAILED) {
+    int save_errno = errno;
     close(fd);
     log_warn(LD_FS,"Could not mmap file \"%s\": %s", filename,
              strerror(errno));
+    errno = save_errno;
     return NULL;
   }
 
@@ -196,6 +202,7 @@
 tor_mmap_file(const char *filename)
 {
   win_mmap_t *res = tor_malloc_zero(sizeof(win_mmap_t));
+  int empty = 0;
   res->file_handle = INVALID_HANDLE_VALUE;
   res->mmap_handle = NULL;
 
@@ -213,6 +220,7 @@
 
   if (res->base.size == 0) {
     log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
+    empty = 1;
     goto err;
   }
 
@@ -243,7 +251,13 @@
     log_fn(severity, LD_FS, "Couldn't mmap file \"%s\": %s", filename, msg);
     tor_free(msg);
   }
+  if (e == ERROR_FILE_NOT_FOUND || e == ERROR_PATH_NOT_FOUND)
+    e = ENOENT;
+  else
+    e = EINVAL;
  err:
+  if (empty)
+    errno = ERANGE;
   tor_munmap_file(&res->base);
   return NULL;
 }

Modified: tor/trunk/src/common/util.c
===================================================================
--- tor/trunk/src/common/util.c	2007-10-24 14:28:21 UTC (rev 12152)
+++ tor/trunk/src/common/util.c	2007-10-24 15:45:42 UTC (rev 12153)
@@ -1782,16 +1782,20 @@
   fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
   if (fd<0) {
     int severity = LOG_WARN;
+    int save_errno = errno;
     if (errno == ENOENT && (flags & RFTS_IGNORE_MISSING))
       severity = LOG_INFO;
     log_fn(severity, LD_FS,"Could not open \"%s\": %s ",filename,
            strerror(errno));
+    errno = save_errno;
     return NULL;
   }
 
   if (fstat(fd, &statbuf)<0) {
+    int save_errno = errno;
     close(fd);
     log_warn(LD_FS,"Could not fstat \"%s\".",filename);
+    errno = save_errno;
     return NULL;
   }
 
@@ -1802,10 +1806,12 @@
 
   r = read_all(fd,string,(size_t)statbuf.st_size,0);
   if (r<0) {
+    int save_errno = errno;
     log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
              strerror(errno));
     tor_free(string);
     close(fd);
+    errno = save_errno;
     return NULL;
   }
   string[r] = '\0'; /* NUL-terminate the result. */
@@ -1825,10 +1831,12 @@
     if (r != statbuf.st_size) {
       /* Unless we're using text mode on win32, we'd better have an exact
        * match for size. */
+      int save_errno = errno;
       log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
                r, (long)statbuf.st_size,filename);
       tor_free(string);
       close(fd);
+      errno = save_errno;
       return NULL;
     }
   close(fd);