Mercurial > trustbridge
changeset 1157:fd7d04bb37cb
(issue36) Add encoding aware port_fopen function and use it
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Thu, 18 Sep 2014 15:43:22 +0200 |
parents | e986d3d4705f |
children | ffdc8cba139a |
files | cinst/main.c common/listutil.c common/portpath.c common/portpath.h |
diffstat | 4 files changed, 71 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- 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);
--- 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;
--- 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; +}
--- 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