[PATCH 5 of 7] (issue36) Add encoding aware port_fopen function and use it

Wald Commits scm-commit at wald.intevation.org
Thu Sep 18 15:47:19 CEST 2014


# HG changeset patch
# User Andre Heinecke <andre.heinecke at intevation.de>
# Date 1411047802 -7200
# Node ID fd7d04bb37cb7af2f42815c8db965849bdef7ecb
# Parent  e986d3d4705f25ba75a74e9b99ecb43f8d1d5582
(issue36) Add encoding aware port_fopen function and use it

diff -r e986d3d4705f -r fd7d04bb37cb cinst/main.c
--- a/cinst/main.c	Thu Sep 18 12:56:31 2014 +0200
+++ b/cinst/main.c	Thu Sep 18 15:43:22 2014 +0200
@@ -50,6 +50,7 @@
 #include "errorcodes.h"
 #include "windowsstore.h"
 #include "nssstore.h"
+#include "portpath.h"
 
 /* The certificate list + choices may only be so long as
  * twice the accepted certificatelist size */
@@ -90,7 +91,7 @@
       return -1;
     }
 
-  f = fopen (file_name, "rb");
+  f = port_fopen_rb(file_name, false);
   if (f == NULL)
     {
       ERRORPRINTF ("Failed to open file: %s\n", file_name);
diff -r e986d3d4705f -r fd7d04bb37cb common/listutil.c
--- a/common/listutil.c	Thu Sep 18 12:56:31 2014 +0200
+++ b/common/listutil.c	Thu Sep 18 15:43:22 2014 +0200
@@ -22,6 +22,7 @@
 
 #include "strhelp.h"
 #include "logging.h"
+#include "portpath.h"
 
 #ifdef RELEASE_BUILD
 #include "pubkey-release.h"
@@ -54,23 +55,8 @@
     {
       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
+
+  f = port_fopen_rb(file_name, true);
   if (f == NULL)
     return READ_FILE_UNREADABLE;
 
diff -r e986d3d4705f -r fd7d04bb37cb common/portpath.c
--- a/common/portpath.c	Thu Sep 18 12:56:31 2014 +0200
+++ b/common/portpath.c	Thu Sep 18 15:43:22 2014 +0200
@@ -19,6 +19,10 @@
 #include <unistd.h>
 #include <string.h>
 
+#ifdef WIN32
+#include <share.h>
+#endif
+
 char *
 port_dirname(char *path)
 {
@@ -140,3 +144,53 @@
   else
     return false;
 }
+
+FILE*
+port_fopen_rb(const char *path, bool exclusive)
+{
+  FILE *f = NULL;
+  if (!path)
+    {
+      return NULL;
+    }
+#ifdef WIN32
+    {
+      wchar_t *wFilename = utf8_to_wchar(path, strlen(path));
+      if (!wFilename)
+        {
+          ERRORPRINTF ("Invalid encoding\n");
+          return NULL;
+        }
+      /* 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. */
+      OutputDebugStringW(wFilename);
+      if (exclusive)
+        {
+          f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
+        }
+      else
+        {
+          f = _wfopen(wFilename, L"rb");
+        }
+      xfree(wFilename);
+      if (f == NULL)
+        {
+          /* Fall back to local8 bit encoding */
+          if (exclusive)
+            {
+              f = _fsopen(path, "rb", _SH_DENYWR);
+            }
+          else
+            {
+              f = fopen(path, "rb");
+            }
+        }
+    }
+#else
+  (void)(exclusive);
+  f = fopen(path, "rb");
+#endif
+  return f;
+}
diff -r e986d3d4705f -r fd7d04bb37cb common/portpath.h
--- a/common/portpath.h	Thu Sep 18 12:56:31 2014 +0200
+++ b/common/portpath.h	Thu Sep 18 15:43:22 2014 +0200
@@ -9,6 +9,7 @@
 #define PORTPATH_H
 
 #include <stdbool.h>
+#include <stdio.h>
 
 /**
  * @file  portpath.h
@@ -82,4 +83,15 @@
  */
 bool port_mkdir_p(const char *path, bool propagate_acl);
 
+/**
+ * @brief Open a file in read binary mode
+ *
+ * @param[in] path UTF-8 (or local 8 bit encoding)
+ *                 encoded filename
+ * @param[in] exclusive weather or not to open the file with
+ *            a denywr lock. Ignored under linux.
+ * @returns the same as fopen.
+ */
+FILE* port_fopen_rb(const char *path, bool exclusive);
+
 #endif


More information about the Trustbridge-commits mailing list