[tor-commits] [tor/master] Close the windows file handle after CreateFileMapping; it isn't needed

nickm at torproject.org nickm at torproject.org
Thu May 31 16:43:31 UTC 2012


commit ab1b81e838150ae070b1c6818b9c95faeea56c06
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed May 23 12:39:05 2012 -0400

    Close the windows file handle after CreateFileMapping; it isn't needed
    
    I did the changes file; the rest came pseudonymously
---
 changes/close_file_handle |    4 ++++
 src/common/compat.c       |   15 ++++++++-------
 src/common/compat.h       |    1 -
 3 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/changes/close_file_handle b/changes/close_file_handle
new file mode 100644
index 0000000..128ef81
--- /dev/null
+++ b/changes/close_file_handle
@@ -0,0 +1,4 @@
+  o Minor bugfixes:
+    - Don't hold a windows file handle open for every file mapping;
+      the file mapping handle is sufficient. Fix for bug 5951; bugfix on
+      0.1.2.1-alpha.
diff --git a/src/common/compat.c b/src/common/compat.c
index a4e5074..3174da6 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -175,24 +175,24 @@ tor_mmap_file(const char *filename)
   TCHAR tfilename[MAX_PATH]= {0};
   tor_mmap_t *res = tor_malloc_zero(sizeof(tor_mmap_t));
   int empty = 0;
-  res->file_handle = INVALID_HANDLE_VALUE;
+  HANDLE file_handle = INVALID_HANDLE_VALUE;
   res->mmap_handle = NULL;
 #ifdef UNICODE
   mbstowcs(tfilename,filename,MAX_PATH);
 #else
   strlcpy(tfilename,filename,MAX_PATH);
 #endif
-  res->file_handle = CreateFile(tfilename,
+  file_handle = CreateFile(tfilename,
                                 GENERIC_READ, FILE_SHARE_READ,
                                 NULL,
                                 OPEN_EXISTING,
                                 FILE_ATTRIBUTE_NORMAL,
                                 0);
 
-  if (res->file_handle == INVALID_HANDLE_VALUE)
+  if (file_handle == INVALID_HANDLE_VALUE)
     goto win_err;
 
-  res->size = GetFileSize(res->file_handle, NULL);
+  res->size = GetFileSize(file_handle, NULL);
 
   if (res->size == 0) {
     log_info(LD_FS,"File \"%s\" is empty. Ignoring.",filename);
@@ -200,7 +200,7 @@ tor_mmap_file(const char *filename)
     goto err;
   }
 
-  res->mmap_handle = CreateFileMapping(res->file_handle,
+  res->mmap_handle = CreateFileMapping(file_handle,
                                        NULL,
                                        PAGE_READONLY,
 #if SIZEOF_SIZE_T > 4
@@ -218,6 +218,7 @@ tor_mmap_file(const char *filename)
   if (!res->data)
     goto win_err;
 
+  CloseHandle(file_handle);
   return res;
  win_err: {
     DWORD e = GetLastError();
@@ -234,6 +235,8 @@ tor_mmap_file(const char *filename)
  err:
   if (empty)
     errno = ERANGE;
+  if (file_handle != INVALID_HANDLE_VALUE)
+    CloseHandle(file_handle);
   tor_munmap_file(res);
   return NULL;
 }
@@ -247,8 +250,6 @@ tor_munmap_file(tor_mmap_t *handle)
 
   if (handle->mmap_handle != NULL)
     CloseHandle(handle->mmap_handle);
-  if (handle->file_handle != INVALID_HANDLE_VALUE)
-    CloseHandle(handle->file_handle);
   tor_free(handle);
 }
 #else
diff --git a/src/common/compat.h b/src/common/compat.h
index d2f1fd1..06aeeb1 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -249,7 +249,6 @@ typedef struct tor_mmap_t {
   size_t mapping_size; /**< Size of the actual mapping. (This is this file
                         * size, rounded up to the nearest page.) */
 #elif defined MS_WINDOWS
-  HANDLE file_handle;
   HANDLE mmap_handle;
 #endif
 





More information about the tor-commits mailing list