]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Consider flags for CreateFileA(), Windows
authorWolfgang Stöggl <c72578@yahoo.de>
Wed, 24 Apr 2019 13:29:54 +0000 (15:29 +0200)
committerTobias Oetiker <tobi@oetiker.ch>
Wed, 24 Apr 2019 15:06:52 +0000 (17:06 +0200)
- 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 <CharacterSet>MultiByte</CharacterSet> consistently
  in .vcxproj files. There were 4 inadvertent occurrences of
  <CharacterSet>Unicode</CharacterSet> 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

src/rrd_open.c
win32/librrd-4.vcxproj

index 9815e3cf74d3d20a643d2eaea4c944ddc4561e94..d29853f3ef7b3f144859a0794f374416a8619672 100644 (file)
 #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;
index 0cae4611b7f808b6a07261ae56dac5f9ab26ed3c..949d130b35d9c9551a6b0ed0fa3dd600bdbf8647 100644 (file)
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Static Debug|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <PlatformToolset>v140</PlatformToolset>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='DebugDLL|Win32'" Label="Configuration">
     <ConfigurationType>DynamicLibrary</ConfigurationType>
     <PlatformToolset>v140</PlatformToolset>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <PlatformToolset>v140</PlatformToolset>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
     <WholeProgramOptimization>true</WholeProgramOptimization>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <PlatformToolset>v140</PlatformToolset>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
   <ImportGroup Label="ExtensionSettings">