Code

only include utime.h when necessary
[rrdtool-all.git] / program / src / rrd_open.c
index 06afabe12540400a1635e190f0b3b04e73f64665..f7ccca87a7d643c19d78ab8dce8c9ffc12560e81 100644 (file)
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * RRDtool 1.3.4  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,18 @@
 
 #include "rrd_tool.h"
 #include "unused.h"
+
+#ifdef WIN32
+#include <stdlib.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#endif
+
+#ifdef HAVE_BROKEN_MS_ASYNC
+#include <sys/types.h>       
+#include <utime.h>
+#endif
+
 #define MEMBLK 8192
 
 /* DEBUG 2 prints information obtained via mincore(2) */
@@ -34,7 +46,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 +78,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 +104,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 +129,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 +159,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)) {
@@ -279,7 +314,7 @@ rrd_file_t *rrd_open(
       for (i=0; i<rrd->stat_head->rra_cnt; i++)
         row_cnt += rrd->rra_def[i].row_cnt;
 
-      off_t correct_len = rrd_file->header_len +
+      size_t correct_len = rrd_file->header_len +
         sizeof(rrd_value_t) * row_cnt * rrd->stat_head->ds_cnt;
 
       if (correct_len > rrd_file->file_len)
@@ -358,10 +393,10 @@ void rrd_dontneed(
     rrd_t *rrd)
 {
 #if defined USE_MADVISE || defined HAVE_POSIX_FADVISE
-    unsigned long dontneed_start;
-    unsigned long rra_start;
-    unsigned long active_block;
-    unsigned long i;
+    size_t dontneed_start;
+    size_t rra_start;
+    size_t active_block;
+    size_t i;
     ssize_t   _page_size = sysconf(_SC_PAGESIZE);
 
     if (rrd_file == NULL) {
@@ -557,10 +592,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
 }