Code

prepare for the release of rrdtool-1.3.8
[rrdtool.git] / src / rrd_open.c
index 81986472250c52317c968fcb1300b8884bb5d1b2..a1b2670ef8a249187ea54ed86b5e9371725e7cd1 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.3  Copyright by Tobi Oetiker, 1997-2008
+ * RRDtool 1.3.8  Copyright by Tobi Oetiker, 1997-2009
  *****************************************************************************
  * rrd_open.c  Open an RRD File
  *****************************************************************************
@@ -8,6 +8,14 @@
 
 #include "rrd_tool.h"
 #include "unused.h"
+
+#ifdef WIN32
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <utime.h>
+#endif
+
 #define MEMBLK 8192
 
 /* DEBUG 2 prints information obtained via mincore(2) */
@@ -34,7 +42,7 @@
 #define __rrd_read(dst, dst_t, cnt) { \
        size_t wanted = sizeof(dst_t)*(cnt); \
         size_t got; \
-       if ((dst = malloc(wanted)) == NULL) { \
+       if ((dst = (dst_t*)malloc(wanted)) == NULL) { \
                rrd_set_error(#dst " malloc"); \
                goto out_nullify_head; \
        } \
@@ -66,7 +74,13 @@ rrd_file_t *rrd_open(
     unsigned rdwr)
 {
     int       flags = 0;
+
+/* Win32 can't use S_IRUSR flag */
+#ifndef WIN32
     mode_t    mode = S_IRUSR;
+#else
+    int mode = 0;
+#endif
     int       version;
 
 #ifdef HAVE_MMAP
@@ -86,7 +100,7 @@ rrd_file_t *rrd_open(
         free(rrd->stat_head);
     }
     rrd_init(rrd);
-    rrd_file = malloc(sizeof(rrd_file_t));
+    rrd_file = (rrd_file_t*)malloc(sizeof(rrd_file_t));
     if (rrd_file == NULL) {
         rrd_set_error("allocating rrd_file descriptor for '%s'", file_name);
         return NULL;
@@ -111,7 +125,9 @@ rrd_file_t *rrd_open(
 #endif
     } else {
         if (rdwr & RRD_READWRITE) {
+#ifndef WIN32 // Win32 can't use this mode
             mode |= S_IWUSR;
+#endif
             flags |= O_RDWR;
 #ifdef HAVE_MMAP
             mm_flags = MAP_SHARED;
@@ -139,6 +155,21 @@ rrd_file_t *rrd_open(
         goto out_free;
     }
 
+#ifdef HAVE_MMAP
+#ifdef HAVE_BROKEN_MS_ASYNC
+    if (rdwr & RRD_READWRITE) {
+        /* some unices, the files mtime does not get update
+           on msync MS_ASYNC, in order to help them,
+           we update the the timestamp at this point.
+           The thing happens pretty 'close' to the open
+           call so the chances of a race should be minimal.
+              
+           Maybe ask your vendor to fix your OS ... */
+           utime(file_name,NULL);
+    }
+#endif    
+#endif
+
     /* Better try to avoid seeks as much as possible. stat may be heavy but
      * many concurrent seeks are even worse.  */
     if (newfile_size == 0 && ((fstat(rrd_file->fd, &statb)) < 0)) {
@@ -364,6 +395,13 @@ void rrd_dontneed(
     unsigned long i;
     ssize_t   _page_size = sysconf(_SC_PAGESIZE);
 
+    if (rrd_file == NULL) {
+#if defined DEBUG && DEBUG
+           fprintf (stderr, "rrd_dontneed: Argument 'rrd_file' is NULL.\n");
+#endif
+           return;
+    }
+
 #if defined DEBUG && DEBUG > 1
     mincore_print(rrd_file, "before");
 #endif
@@ -550,10 +588,16 @@ ssize_t rrd_write(
 void rrd_flush(
     rrd_file_t *rrd_file)
 {
+/*
+ * Win32 can only flush files by FlushFileBuffers function, 
+ * but it works with HANDLE hFile, not FILE. So skipping
+ */
+#ifndef WIN32
     if (fdatasync(rrd_file->fd) != 0) {
         rrd_set_error("flushing fd %d: %s", rrd_file->fd,
                       rrd_strerror(errno));
     }
+#endif
 }