From: Wolfgang Stöggl Date: Wed, 24 Apr 2019 13:29:54 +0000 (+0200) Subject: Consider flags for CreateFileA(), Windows X-Git-Tag: v1.7.2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0d6e25e08b85eeffa38896f211af4825070714a9;p=thirdparty%2Frrdtool-1.x.git Consider flags for CreateFileA(), Windows - Implement dwDesiredAccess and dwCreationDisposition based on the flags O_RDONLY, O_RDWR, O_CREAT | O_TRUNC and O_EXCL - This updates how a file handle is created using CreateFileA() under Windows, considering the flags. Use e.g. DesiredAccess = GENERIC_READ | GENERIC_WRITE dwCreationDisposition = OPEN_EXISTING - CreateFileA(): Check for INVALID_HANDLE_VALUE and add output of error messages using GetLastError() and FormatMessage() - The error message provides correct output now, which error occurs. e.g.: rrdtool.exe info not_existing_file.rrd ERROR: opening 'not_existing_file.rrd': The system cannot find the file specified. Previously, the following error occurred later in the code: ERROR: short read while reading header rrd->stat_head And also empty files were left behind: e.g. rrdtool.exe resize not_existing_file.rrd 0 GROW 5200 - Use MultiByte consistently in .vcxproj files. There were 4 inadvertent occurrences of Unicode in librrd-4.vcxproj. Using MultiByte or NotSet instead of Unicode is required for printing "(LPTSTR) lpMsgBuf" from FormatMessage() using %s, to avoid unnecessary wide characters. - This commit is an update to a9671a7 --- diff --git a/src/rrd_open.c b/src/rrd_open.c index 9815e3cf..d29853f3 100644 --- a/src/rrd_open.c +++ b/src/rrd_open.c @@ -47,6 +47,11 @@ #define LK_NBLCK _LK_NBLCK #define LK_RLCK _LK_RLCK #define LK_NBRLCK _LK_NBRLCK + +/* Variables for CreateFileA(). Names of variables are according to + * https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea */ +DWORD dwDesiredAccess = 0; +DWORD dwCreationDisposition = 0; #endif /* DEBUG 2 prints information obtained via mincore(2) */ @@ -237,6 +242,10 @@ rrd_file_t *rrd_open( if (rdwr & RRD_READONLY) { flags |= O_RDONLY; +#ifdef _WIN32 + dwDesiredAccess = GENERIC_READ; + dwCreationDisposition = OPEN_EXISTING; +#endif #ifdef HAVE_MMAP # if !defined(AIX) rrd_simple_file->mm_flags = MAP_PRIVATE; @@ -248,6 +257,10 @@ rrd_file_t *rrd_open( } else { if (rdwr & RRD_READWRITE) { flags |= O_RDWR; +#ifdef _WIN32 + dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; + dwCreationDisposition = OPEN_EXISTING; +#endif #ifdef HAVE_MMAP rrd_simple_file->mm_flags = MAP_SHARED; rrd_simple_file->mm_prot |= PROT_WRITE; @@ -255,9 +268,17 @@ rrd_file_t *rrd_open( } if (rdwr & RRD_CREAT) { flags |= (O_CREAT | O_TRUNC); +#ifdef _WIN32 + dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; + dwCreationDisposition = CREATE_ALWAYS; +#endif } if (rdwr & RRD_EXCL) { flags |= O_EXCL; +#ifdef _WIN32 + dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; + dwCreationDisposition = CREATE_NEW; +#endif } } if (rdwr & RRD_READAHEAD) { @@ -279,9 +300,20 @@ rrd_file_t *rrd_open( HANDLE handle; handle = - CreateFileA(file_name, GENERIC_READ | GENERIC_WRITE, + CreateFileA(file_name, dwDesiredAccess, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, - NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + NULL, dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL); + if (handle == INVALID_HANDLE_VALUE) { + LPVOID lpMsgBuf = NULL; + + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | + FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, + (LPTSTR) & lpMsgBuf, 0, NULL); + rrd_set_error("opening '%s': %s", file_name, (LPTSTR) lpMsgBuf); + LocalFree(lpMsgBuf); + goto out_free; + } if ((rrd_simple_file->fd = _open_osfhandle((intptr_t) handle, flags)) < 0) { rrd_set_error("opening '%s': %s", file_name, rrd_strerror(errno)); goto out_free; diff --git a/win32/librrd-4.vcxproj b/win32/librrd-4.vcxproj index 0cae4611..949d130b 100644 --- a/win32/librrd-4.vcxproj +++ b/win32/librrd-4.vcxproj @@ -37,23 +37,23 @@ StaticLibrary v140 - Unicode + MultiByte DynamicLibrary v140 - Unicode + MultiByte StaticLibrary v140 - Unicode + MultiByte true StaticLibrary v140 - Unicode + MultiByte