report diagnostics
[oweals/gnunet.git] / src / statistics / gnunet-service-statistics.c
index 676db1b0244f46571bb1b9e17061a960068736a1..3221fe2ae03465231ff2cf8f197fc0459269f100 100644 (file)
@@ -22,6 +22,9 @@
  * @file statistics/gnunet-service-statistics.c
  * @brief program that tracks statistics
  * @author Christian Grothoff
+ * 
+ * TODO:
+ * - use BIO for IO operations
  */
 #include "platform.h"
 #include "gnunet_disk_lib.h"
@@ -96,10 +99,11 @@ static uint32_t uidgen;
  */
 static void
 load (struct GNUNET_SERVER_Handle *server,
-      struct GNUNET_CONFIGURATION_Handle *cfg)
+      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   char *fn;
-  int fd;
+  struct GNUNET_DISK_FileHandle *fh;
+  struct GNUNET_DISK_MapHandle *mh;
   struct stat sb;
   char *buf;
   size_t off;
@@ -114,17 +118,18 @@ load (struct GNUNET_SERVER_Handle *server,
       GNUNET_free (fn);
       return;
     }
-  fd = GNUNET_DISK_file_open (fn, O_RDONLY);
-  if (fd == -1)
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ,
+                             GNUNET_DISK_PERM_NONE);
+  if (!fh)
     {
       GNUNET_free (fn);
       return;
     }
-  buf = mmap (NULL, sb.st_size, PROT_READ, MAP_SHARED, fd, 0);
-  if (MAP_FAILED == buf)
+  buf = GNUNET_DISK_file_map (fh, &mh, GNUNET_DISK_MAP_TYPE_READ, sb.st_size);
+  if (NULL == buf)
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mmap", fn);
-      GNUNET_break (0 == CLOSE (fd));
+      GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
       GNUNET_free (fn);
       return;
     }
@@ -143,8 +148,8 @@ load (struct GNUNET_SERVER_Handle *server,
         }
       off += ntohs (msg->size);
     }
-  GNUNET_break (0 == munmap (buf, sb.st_size));
-  GNUNET_break (0 == CLOSE (fd));
+  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
+  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fh));
   GNUNET_free (fn);
 }
 
@@ -156,43 +161,44 @@ load (struct GNUNET_SERVER_Handle *server,
  * @param cfg configuration to use
  */
 static void
-save (void *cls, struct GNUNET_CONFIGURATION_Handle *cfg)
+save (void *cls, 
+      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct StatsEntry *pos;
   char *fn;
-  int fd;
+  struct GNUNET_DISK_FileHandle *fh;
   uint16_t size;
   unsigned long long total;
 
-  fd = -1;
+  fh = NULL;
   fn = GNUNET_DISK_get_home_filename (cfg,
                                       "statistics", "statistics.data", NULL);
   if (fn != NULL)
-    fd =
-      GNUNET_DISK_file_open (fn, O_WRONLY | O_CREAT | O_TRUNC,
-                             S_IRUSR | S_IWUSR);
+    fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
+        | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_TRUNCATE,
+        GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
   total = 0;
   while (NULL != (pos = start))
     {
       start = pos->next;
-      if ((pos->persistent) && (fd != -1))
+      if ((pos->persistent) && (NULL != fh))
         {
           size = htons (pos->msg->header.size);
-          if (size != WRITE (fd, pos->msg, size))
+          if (size != GNUNET_DISK_file_write (fh, pos->msg, size))
             {
               GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                         "write", fn);
-              GNUNET_DISK_file_close (fn, fd);
-              fd = -1;
+              GNUNET_DISK_file_close (fh);
+             fh = NULL;
             }
           else
             total += size;
         }
       GNUNET_free (pos);
     }
-  if (fd != -1)
+  if (NULL != fh)
     {
-      GNUNET_DISK_file_close (fn, fd);
+      GNUNET_DISK_file_close (fh);
       if (total == 0)
         GNUNET_break (0 == UNLINK (fn));
       else
@@ -440,7 +446,7 @@ static void
 run (void *cls,
      struct GNUNET_SCHEDULER_Handle *sched,
      struct GNUNET_SERVER_Handle *server,
-     struct GNUNET_CONFIGURATION_Handle *cfg)
+     const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   GNUNET_SERVER_add_handlers (server, handlers);
   load (server, cfg);