diff common/listutil.c @ 1081:edbf5e5e88f4

(issue118) Extend verify_binary to carry an open file * binverify.c: Change result to a structure containing an open fptr Use in Memory data for windows verification. * mainwindow.cpp, selftest.c: Handle the returend structure * binverifytest.cpp: Test for the exclusive read and update signature. * listutil.c: Add optional fptr parameter to read_file
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 11 Sep 2014 12:05:24 +0200
parents 698b6a9bd75e
children fd7d04bb37cb
line wrap: on
line diff
--- a/common/listutil.c	Thu Sep 11 12:00:10 2014 +0200
+++ b/common/listutil.c	Thu Sep 11 12:05:24 2014 +0200
@@ -16,6 +16,10 @@
 #include <sys/stat.h>
 #include <string.h>
 
+#ifdef WIN32
+#include <share.h>
+#endif
+
 #include "strhelp.h"
 #include "logging.h"
 
@@ -41,7 +45,7 @@
 #define READ_FILE_INVALID_CALL -5
 int
 read_file(const char *file_name, char **data, size_t *size,
-          const size_t max_size)
+          const size_t max_size, FILE **fptr)
 {
   FILE *f;
   long file_size;
@@ -50,8 +54,23 @@
     {
       return READ_FILE_INVALID_CALL;
     }
-
+#ifdef WIN32
+    {
+      wchar_t *wFilename = utf8_to_wchar(file_name, strlen(file_name));
+      if (!wFilename)
+        {
+          return READ_FILE_UNREADABLE;
+        }
+      /* We open and write protect the file here so that
+         as long as the file is open we can be sure that
+         it was not modified and can use it in subsequent
+         calls based on the filename. */
+      f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
+      xfree(wFilename);
+    }
+#else
   f = fopen(file_name, "rb");
+#endif
   if (f == NULL)
     return READ_FILE_UNREADABLE;
 
@@ -92,7 +111,14 @@
       return READ_FILE_READ_FAILED;
     }
 
-  fclose(f);
+  if (fptr)
+    {
+      *fptr = f;
+    }
+  else
+    {
+      fclose(f);
+    }
 
   (*data)[*size] = '\0';
 
@@ -180,7 +206,7 @@
   *size = 0;
   int ret = 0;
 
-  ret = read_file(file_name, data, size, MAX_FILESIZE);
+  ret = read_file(file_name, data, size, MAX_FILESIZE, NULL);
 
   /* printf ("Ret: %i \n", ret); */
   if (ret != 0)

http://wald.intevation.org/projects/trustbridge/