gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r8582 - in gnunet/src: include peerinfo statistics topology


From: gnunet
Subject: [GNUnet-SVN] r8582 - in gnunet/src: include peerinfo statistics topology util
Date: Mon, 15 Jun 2009 14:11:51 -0600

Author: durner
Date: 2009-06-15 14:11:51 -0600 (Mon, 15 Jun 2009)
New Revision: 8582

Added:
   gnunet/src/include/gnunet_io_lib.h
   gnunet/src/util/io_handle.c
   gnunet/src/util/io_handle.h
Modified:
   gnunet/src/include/Makefile.am
   gnunet/src/include/gnunet_disk_lib.h
   gnunet/src/include/gnunet_util_lib.h
   gnunet/src/peerinfo/gnunet-service-peerinfo.c
   gnunet/src/statistics/gnunet-service-statistics.c
   gnunet/src/topology/gnunet-daemon-topology.c
   gnunet/src/util/Makefile.am
   gnunet/src/util/container_bloomfilter.c
   gnunet/src/util/crypto_hash.c
   gnunet/src/util/disk.c
   gnunet/src/util/pseudonym.c
   gnunet/src/util/service.c
   gnunet/src/util/test_disk.c
   gnunet/src/util/test_os_load.c
Log:
more complete DISK API

Modified: gnunet/src/include/Makefile.am
===================================================================
--- gnunet/src/include/Makefile.am      2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/include/Makefile.am      2009-06-15 20:11:51 UTC (rev 8582)
@@ -24,6 +24,7 @@
   gnunet_fragmentation_lib.h \
   gnunet_getopt_lib.h \
   gnunet_hello_lib.h \
+  gnunet_io_lib.h \
   gnunet_network_lib.h \
   gnunet_peerinfo_service.h \
   gnunet_program_lib.h \

Modified: gnunet/src/include/gnunet_disk_lib.h
===================================================================
--- gnunet/src/include/gnunet_disk_lib.h        2009-06-15 17:42:43 UTC (rev 
8581)
+++ gnunet/src/include/gnunet_disk_lib.h        2009-06-15 20:11:51 UTC (rev 
8582)
@@ -28,6 +28,7 @@
 
 #include "gnunet_configuration_lib.h"
 #include "gnunet_scheduler_lib.h"
+#include "gnunet_io_lib.h"
 
 /* we need size_t, and since it can be both unsigned int
    or unsigned long long, this IS platform dependent;
@@ -43,6 +44,37 @@
 #endif
 #endif
 
+/* Open the file for reading */
+#define GNUNET_DISK_OPEN_READ           1
+/* Open the file for writing */
+#define GNUNET_DISK_OPEN_WRITE          2
+/* Open the file for both reading and writing */
+#define GNUNET_DISK_OPEN_READWRITE      3
+/* Fail if file already exists */
+#define GNUNET_DISK_OPEN_FAILIFEXISTS   4
+/* Truncate file if it exists */
+#define GNUNET_DISK_OPEN_TRUNCATE       8
+/* Create file if it doesn't exist */
+#define GNUNET_DISK_OPEN_CREATE         16
+/* Append to the file */
+#define GNUNET_DISK_OPEN_APPEND         32
+
+#define GNUNET_DISK_MAP_READ    1
+#define GNUNET_DISK_MAP_WRITE   2
+#define GNUNET_DISK_MAP_READWRITE 3
+
+#define GNUNET_DISK_PERM_USER_READ      1
+#define GNUNET_DISK_PERM_USER_WRITE     2
+#define GNUNET_DISK_PERM_USER_EXEC      4
+#define GNUNET_DISK_PERM_GROUP_READ     8
+#define GNUNET_DISK_PERM_GROUP_WRITE    16
+#define GNUNET_DISK_PERM_GROUP_EXEC     32
+#define GNUNET_DISK_PERM_OTHER_READ     64
+#define GNUNET_DISK_PERM_OTHER_WRITE    128
+#define GNUNET_DISK_PERM_OTHER_EXEC     256
+
+enum GNUNET_DISK_Seek {GNUNET_SEEK_SET, GNUNET_SEEK_CUR, GNUNET_SEEK_END};
+
 /**
  * Get the number of blocks that are left on the partition that
  * contains the given file (for normal users).
@@ -64,6 +96,18 @@
 
 
 /**
+ * Move the read/write pointer in a file
+ * @param h handle of an open file
+ * @param offset position to move to
+ * @param whence specification to which position the offset parameter relates 
to
+ * @return the new position on success, GNUNET_SYSERR otherwise
+ */
+off_t
+GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset,
+    enum GNUNET_DISK_Seek whence);
+
+
+/**
  * Get the size of the file (or directory)
  * of the given file (in bytes).
  *
@@ -77,44 +121,66 @@
 
 
 /**
- * Wrapper around "open()".  Opens a file.
- *
- * @return file handle, -1 on error
+ * Open a file
+ * @param fn file name to be opened
+ * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
+ * @param perm permissions for the newly created file
+ * @return IO handle on success, NULL on error
  */
-int GNUNET_DISK_file_open (const char *filename, int oflag, ...);
+struct GNUNET_IO_Handle *GNUNET_DISK_file_open (const char *fn, int flags, 
...);
 
 
 /**
- * Wrapper around "close()".  Closes a file.
+ * Close an open file
+ * @param h file handle
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
  */
-void GNUNET_DISK_file_close (const char *filename, int fd);
+int GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h);
 
 
 /**
  * Read the contents of a binary file into a buffer.
- * @param fileName the name of the file, not freed,
- *        must already be expanded!
+ * @param h handle to an open file
+ * @param result the buffer to write the result to
  * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
+ */
+int GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int 
len);
+
+
+/**
+ * Read the contents of a binary file into a buffer.
+ * @param fn file name
  * @param result the buffer to write the result to
- * @return the number of bytes read on success, -1 on failure
+ * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
  */
-int GNUNET_DISK_file_read (const char *fileName, int len, void *result);
+int GNUNET_DISK_fn_read (const char * const fn, void *result, int len);
 
 
 /**
  * Write a buffer to a file.
- * @param fileName the name of the file, NOT freed!
+ * @param h handle to open file
  * @param buffer the data to write
  * @param n number of bytes to write
- * @param mode the mode for file permissions
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return number of bytes written on success, GNUNET_SYSERR on error
  */
-int GNUNET_DISK_file_write (const char *fileName,
-                            const void *buffer, unsigned int n,
-                            const char *mode);
+int GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void 
*buffer,
+    unsigned int n);
 
 
 /**
+ * Write a buffer to a file.
+ * @param fn file name
+ * @param buffer the data to write
+ * @param n number of bytes to write
+ * @return number of bytes written on success, GNUNET_SYSERR on error
+ */
+int GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
+    unsigned int n, int mode);
+
+
+/**
  * Copy a file.
  * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
@@ -235,6 +301,18 @@
 
 
 /**
+ * Lock a part of a file
+ * @param fh file handle
+ * @lockStart absolute position from where to lock
+ * @lockEnd absolute position until where to lock
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
+    off_t lockEnd);
+
+
+/**
  * @brief Removes special characters as ':' from a filename.
  * @param fn the filename to canonicalize
  */
@@ -266,6 +344,33 @@
 char *GNUNET_DISK_get_home_filename (struct GNUNET_CONFIGURATION_Handle *cfg,
                                      const char *serviceName, ...);
 
+/**
+ * Map a file into memory
+ * @param h open file handle
+ * @param m handle to the new mapping
+ * @param access access specification, GNUNET_DISK_MAP_xxx
+ * @param len size of the mapping
+ * @return pointer to the mapped memory region, NULL on failure
+ */
+void *GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct 
GNUNET_IO_Handle **m,
+    int access, size_t len);
+
+/**
+ * Unmap a file
+ * @param h mapping handle
+ * @param addr pointer to the mapped memory region
+ * @param len size of the mapping
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t 
len);
+
+/**
+ * Write file changes to disk
+ * @param h handle to an open file
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h);
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Added: gnunet/src/include/gnunet_io_lib.h
===================================================================
--- gnunet/src/include/gnunet_io_lib.h                          (rev 0)
+++ gnunet/src/include/gnunet_io_lib.h  2009-06-15 20:11:51 UTC (rev 8582)
@@ -0,0 +1,46 @@
+/*
+     This file is part of GNUnet.
+     (C) 2009 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file include/gnunet_io_lib.h
+ * @brief Helper functions for abstract IO handles
+ * @author Nils Durner
+ */
+
+#ifndef IO_HANDLE_H_
+#define IO_HANDLE_H_
+
+struct GNUNET_IO_Handle;
+
+/**
+ * Checks whether a handle is invalid
+ * @param h handle to check
+ * @return GNUNET_YES if invalid, GNUNET_NO if valid
+ */
+int GNUNET_IO_handle_invalid (const struct GNUNET_IO_Handle *h);
+
+/**
+ * Mark a handle as invalid
+ * @param h file handle
+ */
+void GNUNET_IO_handle_invalidate (struct GNUNET_IO_Handle *h);
+
+
+#endif /* IO_HANDLE_H_ */

Modified: gnunet/src/include/gnunet_util_lib.h
===================================================================
--- gnunet/src/include/gnunet_util_lib.h        2009-06-15 17:42:43 UTC (rev 
8581)
+++ gnunet/src/include/gnunet_util_lib.h        2009-06-15 20:11:51 UTC (rev 
8582)
@@ -43,6 +43,7 @@
 #include "gnunet_crypto_lib.h"
 #include "gnunet_disk_lib.h"
 #include "gnunet_getopt_lib.h"
+#include "gnunet_io_lib.h"
 #include "gnunet_network_lib.h"
 #include "gnunet_plugin_lib.h"
 #include "gnunet_program_lib.h"

Modified: gnunet/src/peerinfo/gnunet-service-peerinfo.c
===================================================================
--- gnunet/src/peerinfo/gnunet-service-peerinfo.c       2009-06-15 17:42:43 UTC 
(rev 8581)
+++ gnunet/src/peerinfo/gnunet-service-peerinfo.c       2009-06-15 20:11:51 UTC 
(rev 8582)
@@ -197,14 +197,14 @@
   entry->identity = *identity;
   fn = get_trust_filename (identity);
   if ((GNUNET_DISK_file_test (fn) == GNUNET_YES) &&
-      (sizeof (trust) == GNUNET_DISK_file_read (fn, sizeof (trust), &trust)))
+      (sizeof (trust) == GNUNET_DISK_fn_read (fn, &trust, sizeof (trust))))
     entry->disk_trust = entry->trust = ntohl (trust);
   GNUNET_free (fn);
 
   fn = get_host_filename (identity);
   if (GNUNET_DISK_file_test (fn) == GNUNET_YES)
     {
-      size = GNUNET_DISK_file_read (fn, sizeof (buffer), buffer);
+      size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
       hello = (const struct GNUNET_HELLO_Message *) buffer;
       now = GNUNET_TIME_absolute_get ();
       hello_clean = GNUNET_HELLO_iterate_addresses (hello,
@@ -371,7 +371,9 @@
       host->hello = mrg;
     }
   fn = get_host_filename (peer);
-  GNUNET_DISK_file_write (fn, host->hello, GNUNET_HELLO_size (hello), "644");
+  GNUNET_DISK_fn_write (fn, host->hello, GNUNET_HELLO_size (hello),
+      GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
+          | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
   GNUNET_free (fn);
 }
 
@@ -455,8 +457,9 @@
   else
     {
       trust = htonl (host->trust);
-      if (GNUNET_OK ==
-          GNUNET_DISK_file_write (fn, &trust, sizeof (uint32_t), "644"))
+      if (GNUNET_OK == GNUNET_DISK_fn_write (fn, &trust, sizeof(uint32_t),
+          GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
+              | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ))
         host->disk_trust = host->trust;
     }
   GNUNET_free (fn);
@@ -497,7 +500,7 @@
   struct GNUNET_HELLO_Message *new_hello;
   int size;
 
-  size = GNUNET_DISK_file_read (fn, sizeof (buffer), buffer);
+  size = GNUNET_DISK_fn_read (fn, buffer, sizeof (buffer));
   if ((size < sizeof (struct GNUNET_MessageHeader)) && (0 != UNLINK (fn)))
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING |
@@ -513,9 +516,9 @@
                               GNUNET_ERROR_TYPE_BULK, "unlink", fn);
   if (new_hello != NULL)
     {
-      GNUNET_DISK_file_write (fn,
-                              new_hello,
-                              GNUNET_HELLO_size (new_hello), "644");
+      GNUNET_DISK_fn_write (fn, new_hello, GNUNET_HELLO_size (new_hello),
+          GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
+              | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_OTHER_READ);
       GNUNET_free (new_hello);
     }
   return GNUNET_OK;

Modified: gnunet/src/statistics/gnunet-service-statistics.c
===================================================================
--- gnunet/src/statistics/gnunet-service-statistics.c   2009-06-15 17:42:43 UTC 
(rev 8581)
+++ gnunet/src/statistics/gnunet-service-statistics.c   2009-06-15 20:11:51 UTC 
(rev 8582)
@@ -99,7 +99,7 @@
       struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   char *fn;
-  int fd;
+  struct GNUNET_IO_Handle *fh, *mh;
   struct stat sb;
   char *buf;
   size_t off;
@@ -114,17 +114,17 @@
       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);
+  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_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 +143,8 @@
         }
       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, buf, sb.st_size));
+  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh));
   GNUNET_free (fn);
 }
 
@@ -160,39 +160,37 @@
 {
   struct StatsEntry *pos;
   char *fn;
-  int fd;
+  struct GNUNET_IO_Handle *fh;
   uint16_t size;
   unsigned long long total;
 
-  fd = -1;
   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) && 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);
             }
           else
             total += size;
         }
       GNUNET_free (pos);
     }
-  if (fd != -1)
+  if (fh)
     {
-      GNUNET_DISK_file_close (fn, fd);
+      GNUNET_DISK_file_close (&fh);
       if (total == 0)
         GNUNET_break (0 == UNLINK (fn));
       else

Modified: gnunet/src/topology/gnunet-daemon-topology.c
===================================================================
--- gnunet/src/topology/gnunet-daemon-topology.c        2009-06-15 17:42:43 UTC 
(rev 8581)
+++ gnunet/src/topology/gnunet-daemon-topology.c        2009-06-15 20:11:51 UTC 
(rev 8582)
@@ -38,26 +38,26 @@
 /**
  * For how long do we blacklist a peer after a failed
  * connection attempt?
- */ 
+ */
 #define BLACKLIST_AFTER_ATTEMPT GNUNET_TIME_UNIT_HOURS
 
 /**
  * For how long do we blacklist a friend after a failed
  * connection attempt?
- */ 
+ */
 #define BLACKLIST_AFTER_ATTEMPT_FRIEND GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_MINUTES, 15)
 
 /**
  * How frequently are we allowed to ask PEERINFO for more
  * HELLO's to advertise (at most)?
- */ 
+ */
 #define MIN_HELLO_GATHER_DELAY GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_MINUTES, 27)
 
 /**
  * How often do we at most advertise the same HELLO to the same peer?
  * Also used to remove HELLOs of peers that PEERINFO no longer lists
  * from our cache.
- */ 
+ */
 #define HELLO_ADVERTISEMENT_MIN_FREQUENCY GNUNET_TIME_relative_multiply 
(GNUNET_TIME_UNIT_HOURS, 12)
 
 
@@ -97,7 +97,7 @@
    * ID of the peer.
    */
   struct GNUNET_PeerIdentity id;
-  
+
 };
 
 
@@ -122,12 +122,12 @@
    * this HELLO.
    */
   struct GNUNET_CONTAINER_BloomFilter *filter;
-  
+
   /**
    * What peer is this HELLO for?
    */
   struct GNUNET_PeerIdentity id;
- 
+
   /**
    * When should we remove this entry from the linked list (either
    * resetting the filter or possibly eliminating it for good because
@@ -152,12 +152,12 @@
  * Our configuration.
  */
 static struct GNUNET_CONFIGURATION_Handle * cfg;
-   
+
 /**
  * Handle to the core API.
  */
 static struct GNUNET_CORE_Handle *handle;
-   
+
 /**
  * Handle to the transport API.
  */
@@ -167,7 +167,7 @@
  * Identity of this peer.
  */
 static struct GNUNET_PeerIdentity my_identity;
-        
+
 /**
  * Linked list of all of our friends and all of our current
  * neighbours.
@@ -194,17 +194,17 @@
  * Number of peers (friends and others) that we are currently connected to.
  */
 static unsigned int connection_count;
- 
+
 /**
  * Target number of connections.
  */
 static unsigned int target_connection_count;
- 
+
 /**
  * Number of friends that we are currently connected to.
  */
 static unsigned int friend_count;
- 
+
 /**
  * Should the topology daemon try to establish connections?
  */
@@ -239,7 +239,7 @@
  * Function called by core when our attempt to connect
  * succeeded.  Does nothing.
  */
-static size_t 
+static size_t
 ready_callback (void *cls,
                size_t size, void *buf)
 {
@@ -346,7 +346,7 @@
          GNUNET_assert (GNUNET_NO == pos->is_connected);
          pos->is_connected = GNUNET_YES;
          pos->blacklisted_until.value = 0; /* remove blacklisting */
-         friend_count++;         
+         friend_count++;
          return;
        }
       pos = pos->next;
@@ -364,8 +364,8 @@
 /**
  * Disconnect from all non-friends (we're below quota).
  */
-static void 
-drop_non_friends () 
+static void
+drop_non_friends ()
 {
   struct PeerList *pos;
 
@@ -427,7 +427,7 @@
        }
       prev = pos;
       pos = pos->next;
-    } 
+    }
   GNUNET_break (0);
 }
 
@@ -444,12 +444,12 @@
 /**
  * Determine when we should try again to find more peers and
  * schedule the task.
- */ 
+ */
 static void
 schedule_peer_search ()
 {
   struct GNUNET_TIME_Relative delay;
-  
+
   /* Typically, we try again every 15 minutes; the minimum period is
      15s; if we are above the connection target, we reduce re-trying
      by the square of how much we are above; so for example, with 200%
@@ -473,8 +473,8 @@
 
 
 /**
- * Iterator called on each address. 
- * 
+ * Iterator called on each address.
+ *
  * @param cls flag that we will set if we see any addresses.
  */
 static int
@@ -526,18 +526,18 @@
   memcpy (&pos->msg, hello, size);
   pos->id = pid;
   pos->expiration = GNUNET_TIME_relative_to_absolute 
(HELLO_ADVERTISEMENT_MIN_FREQUENCY);
-  /* 2^{-5} chance of not sending a HELLO to a peer is 
-     acceptably small (if the filter is 50% full); 
+  /* 2^{-5} chance of not sending a HELLO to a peer is
+     acceptably small (if the filter is 50% full);
      64 bytes of memory are small compared to the rest
      of the data structure and would only really become
      "useless" once a HELLO has been passed on to ~100
-     other peers, which is likely more than enough in 
+     other peers, which is likely more than enough in
      any case; hence 64, 5 as bloomfilter parameters. */
   pos->filter = GNUNET_CONTAINER_bloomfilter_load (NULL, 64, 5);
   /* never send a peer its own HELLO */
   GNUNET_CONTAINER_bloomfilter_add (pos->filter, &pos->id.hashPubKey);
   pos->next = hellos;
-  hellos = pos;  
+  hellos = pos;
 }
 
 
@@ -585,7 +585,7 @@
            }
        }
       pos = pos->next;
-    }  
+    }
   if (GNUNET_YES == friends_only)
     return;
   if (friend_count < minimum_friend_count)
@@ -700,7 +700,7 @@
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Failed to connect to core service, can not manage 
topology!\n"));
-      return; 
+      return;
     }
   handle = server;
   my_identity = *my_id;
@@ -744,7 +744,7 @@
                                           "FRIENDS",
                                           &fn);
   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
-    GNUNET_DISK_file_write (fn, NULL, 0, "600");
+    GNUNET_DISK_fn_write (fn, NULL, 0, "600");
   if (0 != STAT (fn, &frstat))
     {
       if ((friends_only) || (minimum_friend_count > 0))
@@ -761,11 +761,11 @@
                  _("Friends file `%s' is empty.\n"),
                  fn);
       GNUNET_free (fn);
-      return; 
+      return;
     }
   data = GNUNET_malloc_large (frstat.st_size);
   if (frstat.st_size !=
-      GNUNET_DISK_file_read (fn, frstat.st_size, data))
+      GNUNET_DISK_fn_read (fn, data, frstat.st_size))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  _("Failed to read friends list from `%s'\n"), fn);
@@ -914,10 +914,10 @@
   while (NULL != (pos = next))
     {
       next = pos->next;
-      if (GNUNET_NO == 
+      if (GNUNET_NO ==
          GNUNET_CONTAINER_bloomfilter_test (pos->filter,
                                             &receiver->hashPubKey))
-       break;  
+       break;
       if (0 == GNUNET_TIME_absolute_get_remaining (pos->expiration).value)
        {
          /* time to discard... */
@@ -947,11 +947,11 @@
          size = 0;
        }
       return size;
-    }  
+    }
   if ( (GNUNET_NO == hello_gathering_active) &&
        (GNUNET_TIME_absolute_get_duration (last_hello_gather_time).value >
        MIN_HELLO_GATHER_DELAY.value) )
-    {     
+    {
       hello_gathering_active = GNUNET_YES;
       last_hello_gather_time = GNUNET_TIME_absolute_get();
       GNUNET_PEERINFO_for_all (cfg,
@@ -994,14 +994,14 @@
  * @param cfgfile name of the configuration file used (for saving, can be 
NULL!)
  * @param c configuration
  */
-static void 
+static void
 run (void *cls,
      struct GNUNET_SCHEDULER_Handle * s,
      char *const *args,
      const char *cfgfile,
      struct GNUNET_CONFIGURATION_Handle * c)
 {
-  struct GNUNET_CORE_MessageHandler handlers[] = 
+  struct GNUNET_CORE_MessageHandler handlers[] =
     {
       { &handle_encrypted_hello, GNUNET_MESSAGE_TYPE_HELLO, 0},
       { NULL, 0, 0 }
@@ -1012,7 +1012,7 @@
   cfg = c;
   autoconnect = GNUNET_CONFIGURATION_get_value_yesno (cfg,
                                                      "TOPOLOGY",
-                                                     "AUTOCONNECT"); 
+                                                     "AUTOCONNECT");
   friends_only = GNUNET_CONFIGURATION_get_value_yesno (cfg,
                                                       "TOPOLOGY",
                                                       "FRIENDS-ONLY");
@@ -1050,7 +1050,7 @@
                       NULL, GNUNET_NO,
                       NULL, GNUNET_NO,
                       handlers);
-  
+
   GNUNET_SCHEDULER_add_delayed (sched,
                                 GNUNET_YES,
                                 GNUNET_SCHEDULER_PRIORITY_IDLE,
@@ -1075,7 +1075,7 @@
   ret = (GNUNET_OK ==
          GNUNET_PROGRAM_run (argc,
                              argv,
-                             "topology", 
+                             "topology",
                             _("GNUnet topology control (maintaining P2P mesh 
and F2F constraints)"),
                             options,
                             &run, NULL)) ? 0 : 1;

Modified: gnunet/src/util/Makefile.am
===================================================================
--- gnunet/src/util/Makefile.am 2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/Makefile.am 2009-06-15 20:11:51 UTC (rev 8582)
@@ -33,6 +33,8 @@
   disk.c \
   getopt.c \
   getopt_helpers.c \
+  io_handle.c \
+  io_handle.h \
   network.c \
   os_installation.c \
   os_load.c \

Modified: gnunet/src/util/container_bloomfilter.c
===================================================================
--- gnunet/src/util/container_bloomfilter.c     2009-06-15 17:42:43 UTC (rev 
8581)
+++ gnunet/src/util/container_bloomfilter.c     2009-06-15 20:11:51 UTC (rev 
8582)
@@ -60,7 +60,7 @@
   /**
    * The bit counter file on disk
    */
-  int fd;
+  struct GNUNET_IO_Handle *fh;
 
   /**
    * How many bits we set for each stored element
@@ -139,10 +139,10 @@
  *
  * @param bitArray memory area to set the bit in
  * @param bitIdx which bit to test
- * @param fd A file to keep the 4 bit address usage counters in
+ * @param fh A file to keep the 4 bit address usage counters in
  */
 static void
-incrementBit (char *bitArray, unsigned int bitIdx, int fd)
+incrementBit (char *bitArray, unsigned int bitIdx, const struct 
GNUNET_IO_Handle *fh)
 {
   unsigned int fileSlot;
   unsigned char value;
@@ -151,14 +151,14 @@
   unsigned int targetLoc;
 
   setBit (bitArray, bitIdx);
-  if (fd == -1)
+  if (GNUNET_IO_handle_invalid (fh))
     return;
   /* Update the counter file on disk */
   fileSlot = bitIdx / 2;
   targetLoc = bitIdx % 2;
 
-  GNUNET_assert (fileSlot == (unsigned int) LSEEK (fd, fileSlot, SEEK_SET));
-  if (1 != READ (fd, &value, 1))
+  GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, 
fileSlot, GNUNET_SEEK_SET));
+  if (1 != GNUNET_DISK_file_read (fh, &value, 1))
     value = 0;
   low = value & 0xF;
   high = (value & (~0xF)) >> 4;
@@ -174,8 +174,8 @@
         high++;
     }
   value = ((high << 4) | low);
-  GNUNET_assert (fileSlot == (unsigned int) LSEEK (fd, fileSlot, SEEK_SET));
-  GNUNET_assert (1 == WRITE (fd, &value, 1));
+  GNUNET_assert (fileSlot == (unsigned int) GNUNET_DISK_file_seek (fh, 
fileSlot, GNUNET_SEEK_SET));
+  GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
 }
 
 /**
@@ -184,10 +184,10 @@
  *
  * @param bitArray memory area to set the bit in
  * @param bitIdx which bit to test
- * @param fd A file to keep the 4bit address usage counters in
+ * @param fh A file to keep the 4bit address usage counters in
  */
 static void
-decrementBit (char *bitArray, unsigned int bitIdx, int fd)
+decrementBit (char *bitArray, unsigned int bitIdx, const struct 
GNUNET_IO_Handle *fh)
 {
   unsigned int fileSlot;
   unsigned char value;
@@ -195,13 +195,13 @@
   unsigned int low;
   unsigned int targetLoc;
 
-  if (fd == -1)
+  if (GNUNET_IO_handle_invalid (fh))
     return;                     /* cannot decrement! */
   /* Each char slot in the counter file holds two 4 bit counters */
   fileSlot = bitIdx / 2;
   targetLoc = bitIdx % 2;
-  LSEEK (fd, fileSlot, SEEK_SET);
-  if (1 != READ (fd, &value, 1))
+  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET);
+  if (1 != GNUNET_DISK_file_read (fh, &value, 1))
     value = 0;
   low = value & 0xF;
   high = (value & 0xF0) >> 4;
@@ -226,8 +226,8 @@
         }
     }
   value = ((high << 4) | low);
-  LSEEK (fd, fileSlot, SEEK_SET);
-  GNUNET_assert (1 == WRITE (fd, &value, 1));
+  GNUNET_DISK_file_seek (fh, fileSlot, GNUNET_SEEK_SET);
+  GNUNET_assert (1 == GNUNET_DISK_file_write (fh, &value, 1));
 }
 
 #define BUFFSIZE 65536
@@ -235,36 +235,36 @@
 /**
  * Creates a file filled with zeroes
  *
- * @param fd the file handle
+ * @param fh the file handle
  * @param size the size of the file
  * @return GNUNET_OK if created ok, GNUNET_SYSERR otherwise
  */
 static int
-makeEmptyFile (int fd, unsigned int size)
+makeEmptyFile (const struct GNUNET_IO_Handle *fh, unsigned int size)
 {
   char *buffer;
   unsigned int bytesleft = size;
   int res = 0;
 
-  if (fd == -1)
+  if (GNUNET_IO_handle_invalid (fh))
     return GNUNET_SYSERR;
   buffer = GNUNET_malloc (BUFFSIZE);
   memset (buffer, 0, BUFFSIZE);
-  LSEEK (fd, 0, SEEK_SET);
+  GNUNET_DISK_file_seek (fh, 0, GNUNET_SEEK_SET);
 
   while (bytesleft > 0)
     {
       if (bytesleft > BUFFSIZE)
         {
-          res = WRITE (fd, buffer, BUFFSIZE);
+          res = GNUNET_DISK_file_write (fh, buffer, BUFFSIZE);
           bytesleft -= BUFFSIZE;
         }
       else
         {
-          res = WRITE (fd, buffer, bytesleft);
+          res = GNUNET_DISK_file_write (fh, buffer, bytesleft);
           bytesleft = 0;
         }
-      GNUNET_assert (res != -1);
+      GNUNET_assert (res != GNUNET_SYSERR);
     }
   GNUNET_free (buffer);
   return GNUNET_OK;
@@ -338,7 +338,7 @@
 incrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit, void *arg)
 {
-  incrementBit (bf->bitArray, bit, bf->fd);
+  incrementBit (bf->bitArray, bit, bf->fh);
 }
 
 /**
@@ -352,7 +352,7 @@
 decrementBitCallback (struct GNUNET_CONTAINER_BloomFilter *bf,
                       unsigned int bit, void *arg)
 {
-  decrementBit (bf->bitArray, bit, bf->fd);
+  decrementBit (bf->bitArray, bit, bf->fh);
 }
 
 /**
@@ -406,14 +406,10 @@
   /* Try to open a bloomfilter file */
   if (filename != NULL)
     {
-#ifndef _MSC_VER
-      bf->fd = GNUNET_DISK_file_open (filename, O_RDWR | O_CREAT,
-                                      S_IRUSR | S_IWUSR);
-#else
-      bf->fd = GNUNET_DISK_file_open (filename,
-                                      O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
-#endif
-      if (-1 == bf->fd)
+      bf->fh = GNUNET_DISK_file_open (filename,
+          GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
+          GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
+      if (!bf->fh)
         {
           GNUNET_free (bf);
           return NULL;
@@ -422,8 +418,8 @@
     }
   else
     {
-      bf->fd = -1;
       bf->filename = NULL;
+      bf->fh = NULL;
     }
   /* Alloc block */
   bf->bitArray = GNUNET_malloc_large (size);
@@ -431,7 +427,7 @@
   bf->addressesPerElement = k;
   memset (bf->bitArray, 0, bf->bitArraySize);
 
-  if (bf->fd != -1)
+  if (bf->filename != NULL)
     {
       /* Read from the file what bits we can */
       rbuff = GNUNET_malloc (BUFFSIZE);
@@ -440,7 +436,7 @@
         {
           int res;
 
-          res = READ (bf->fd, rbuff, BUFFSIZE);
+          res = GNUNET_DISK_file_read (bf->fh, rbuff, BUFFSIZE);
           if (res == 0)
             break;              /* is ok! we just did not use that many bits 
yet */
           for (i = 0; i < res; i++)
@@ -491,8 +487,8 @@
       return NULL;
     }
   bf = GNUNET_malloc (sizeof (struct GNUNET_CONTAINER_BloomFilter));
-  bf->fd = -1;
   bf->filename = NULL;
+  bf->fh = NULL;
   bf->bitArray = GNUNET_malloc_large (size);
   bf->bitArraySize = size;
   bf->addressesPerElement = k;
@@ -537,9 +533,11 @@
 {
   if (NULL == bf)
     return;
-  if (bf->fd != -1)
-    GNUNET_DISK_file_close (bf->filename, bf->fd);
-  GNUNET_free_non_null (bf->filename);
+  if (bf->filename != NULL)
+    {
+      GNUNET_DISK_file_close (&bf->fh);
+      GNUNET_free (bf->filename);
+    }
   GNUNET_free (bf->bitArray);
   GNUNET_free (bf);
 }
@@ -556,8 +554,8 @@
     return;
 
   memset (bf->bitArray, 0, bf->bitArraySize);
-  if (bf->fd != -1)
-    makeEmptyFile (bf->fd, bf->bitArraySize * 4);
+  if (bf->filename != NULL)
+    makeEmptyFile (bf->fh, bf->bitArraySize * 4);
 }
 
 
@@ -634,7 +632,7 @@
 {
   if (NULL == bf)
     return;
-  if (bf->fd == -1)
+  if (bf->filename == NULL)
     return;
   iterateBits (bf, &decrementBitCallback, NULL, e);
 }
@@ -668,8 +666,8 @@
   bf->bitArraySize = size;
   bf->bitArray = GNUNET_malloc (size);
   memset (bf->bitArray, 0, bf->bitArraySize);
-  if (bf->fd != -1)
-    makeEmptyFile (bf->fd, bf->bitArraySize * 4);
+  if (bf->filename != NULL)
+    makeEmptyFile (bf->fh, bf->bitArraySize * 4);
   while (GNUNET_YES == iterator (&hc, iterator_arg))
     GNUNET_CONTAINER_bloomfilter_add (bf, &hc);
 }

Modified: gnunet/src/util/crypto_hash.c
===================================================================
--- gnunet/src/util/crypto_hash.c       2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/crypto_hash.c       2009-06-15 20:11:51 UTC (rev 8582)
@@ -429,7 +429,7 @@
   /**
    * File descriptor.
    */
-  int fd;
+  struct GNUNET_IO_Handle *fh;
 
 };
 
@@ -443,8 +443,8 @@
 {
   fhc->callback (fhc->callback_cls, res);
   GNUNET_free (fhc->filename);
-  if (fhc->fd != -1)
-    GNUNET_break (0 == CLOSE (fhc->fd));
+  if (!GNUNET_IO_handle_invalid (fhc->fh))
+    GNUNET_break (0 == GNUNET_DISK_file_close (&fhc->fh));
   GNUNET_free (fhc);            /* also frees fhc->buffer */
 }
 
@@ -466,7 +466,7 @@
   delta = fhc->bsize;
   if (fhc->fsize - fhc->offset < delta)
     delta = fhc->fsize - fhc->offset;
-  if (delta != READ (fhc->fd, fhc->buffer, delta))
+  if (delta != GNUNET_DISK_file_read (fhc->fh, fhc->buffer, delta))
     {
       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
                                 "read", fhc->filename);
@@ -517,7 +517,7 @@
   fhc->callback_cls = callback_cls;
   fhc->buffer = (unsigned char *) &fhc[1];
   fhc->filename = GNUNET_strdup (filename);
-  fhc->fd = -1;
+  fhc->fh = NULL;
   sha512_init (&fhc->hctx);
   fhc->bsize = blocksize;
   if (GNUNET_OK != GNUNET_DISK_file_size (filename, &fhc->fsize, GNUNET_NO))
@@ -526,8 +526,9 @@
       return;
     }
   fhc->run_on_shutdown = run_on_shutdown;
-  fhc->fd = GNUNET_DISK_file_open (filename, O_RDONLY | O_LARGEFILE);
-  if (fhc->fd == -1)
+  fhc->fh = GNUNET_DISK_file_open (filename,
+      GNUNET_DISK_OPEN_READ);
+  if (!fhc->fh)
     {
       file_hash_finish (fhc, NULL);
       return;

Modified: gnunet/src/util/disk.c
===================================================================
--- gnunet/src/util/disk.c      2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/disk.c      2009-06-15 20:11:51 UTC (rev 8582)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2001, 2002, 2005, 2006 Christian Grothoff (and other contributing 
authors)
+     (C) 2001, 2002, 2005, 2006, 2009 Christian Grothoff (and other 
contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -22,11 +22,14 @@
  * @file util/disk.c
  * @brief disk IO convenience methods
  * @author Christian Grothoff
+ * @author Nils Durner
  */
 
 #include "platform.h"
+#include "io_handle.h"
 #include "gnunet_common.h"
 #include "gnunet_directories.h"
+#include "gnunet_io_lib.h"
 #include "gnunet_disk_lib.h"
 #include "gnunet_scheduler_lib.h"
 #include "gnunet_strings_lib.h"
@@ -108,7 +111,45 @@
   return GNUNET_OK;
 }
 
+
 /**
+ * Move the read/write pointer in a file
+ * @param h handle of an open file
+ * @param offset position to move to
+ * @param whence specification to which position the offset parameter relates 
to
+ * @return the new position on success, GNUNET_SYSERR otherwise
+ */
+off_t
+GNUNET_DISK_file_seek (const struct GNUNET_IO_Handle *h, off_t offset,
+    enum GNUNET_DISK_Seek whence)
+{
+  if (h == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+#ifdef MINGW
+  DWORD ret;
+  static DWORD t[] = { [GNUNET_SEEK_SET] = FILE_BEGIN,
+      [GNUNET_SEEK_CUR] = FILE_CURRENT, [GNUNET_SEEK_END] = FILE_END };
+
+  ret = SetFilePointer (h->h, offset, NULL, t[whence]);
+  if (ret == INVALID_SET_FILE_POINTER)
+    {
+      SetErrnoFromWinError (GetLastError ());
+      return GNUNET_SYSERR;
+    }
+  return ret;
+#else
+  static int t[] = { [GNUNET_SEEK_SET] = SEEK_SET,
+      [GNUNET_SEEK_CUR] = SEEK_CUR, [GNUNET_SEEK_END] = SEEK_END };
+
+  return lseek (h->fd, offset, t[whence]);
+#endif
+}
+
+/**
  * Get the size of the file (or directory)
  * of the given file (in bytes).
  *
@@ -129,6 +170,7 @@
   return ret;
 }
 
+
 /**
  * Get the number of blocks that are left on the partition that
  * contains the given file (for normal users).
@@ -359,30 +401,55 @@
 
 /**
  * Read the contents of a binary file into a buffer.
- * @param fileName the name of the file, not freed,
- *        must already be expanded!
+ * @param h handle to an open file
+ * @param result the buffer to write the result to
  * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
+ */
+int
+GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len)
+{
+  if (h == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+#ifdef MINGW
+  DWORD bytesRead;
+
+  if (!ReadFile (h->h, result, len, &bytesRead, NULL))
+    {
+      SetErrnoFromWinError (GetLastError ());
+      return GNUNET_SYSERR;
+    }
+  return bytesRead;
+#else
+  return read (h->fd, result, len);
+#endif
+}
+
+
+/**
+ * Read the contents of a binary file into a buffer.
+ * @param fn file name
  * @param result the buffer to write the result to
- * @return the number of bytes read on success, -1 on failure
+ * @param len the maximum number of bytes to read
+ * @return the number of bytes read on success, GNUNET_SYSERR on failure
  */
 int
-GNUNET_DISK_file_read (const char *fileName, int len, void *result)
+GNUNET_DISK_fn_read (const char * const fn, void *result, int len)
 {
-  /* open file, must exist, open read only */
-  int handle;
-  int size;
+  struct GNUNET_IO_Handle *fh;
+  int ret;
 
-  GNUNET_assert (fileName != NULL);
-  GNUNET_assert (len > 0);
-  if (len == 0)
-    return 0;
-  GNUNET_assert (result != NULL);
-  handle = GNUNET_DISK_file_open (fileName, O_RDONLY, S_IRUSR);
-  if (handle < 0)
-    return -1;
-  size = READ (handle, result, len);
-  GNUNET_DISK_file_close (fileName, handle);
-  return size;
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_READ);
+  if (!fh)
+    return GNUNET_SYSERR;
+  ret = GNUNET_DISK_file_read (fh, result, len);
+  GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
+
+  return ret;
 }
 
 
@@ -404,48 +471,60 @@
 
 /**
  * Write a buffer to a file.
- * @param fileName the name of the file, NOT freed!
+ * @param h handle to open file
  * @param buffer the data to write
  * @param n number of bytes to write
- * @param mode permissions to set on the file
- * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ * @return number of bytes written on success, GNUNET_SYSERR on error
  */
 int
-GNUNET_DISK_file_write (const char *fileName,
-                        const void *buffer, unsigned int n, const char *mode)
+GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
+    unsigned int n)
 {
-  int handle;
-  char *fn;
-
-  /* open file, open with 600, create if not
-     present, otherwise overwrite */
-  GNUNET_assert (fileName != NULL);
-  fn = GNUNET_STRINGS_filename_expand (fileName);
-  handle = GNUNET_DISK_file_open (fn, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
-  if (handle == -1)
+  if (h == NULL)
     {
-      GNUNET_free (fn);
+      errno = EINVAL;
       return GNUNET_SYSERR;
     }
-  GNUNET_assert ((n == 0) || (buffer != NULL));
-  /* write the buffer take length from the beginning */
-  if (n != WRITE (handle, buffer, n))
+
+#ifdef MINGW
+  DWORD bytesWritten;
+
+  if (!WriteFile (h->h, buffer, n, &bytesWritten, NULL))
     {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
-      GNUNET_DISK_file_close (fn, handle);
-      GNUNET_free (fn);
+      SetErrnoFromWinError (GetLastError ());
       return GNUNET_SYSERR;
     }
-  GNUNET_DISK_file_close (fn, handle);
-  if (0 != CHMOD (fn, atoo (mode)))
-    {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "chmod", fn);
-    }
-  GNUNET_free (fn);
-  return GNUNET_OK;
+  return bytesWritten;
+#else
+  return write (h->fd, buffer, n);
+#endif
 }
 
 /**
+ * Write a buffer to a file.
+ * @param fn file name
+ * @param buffer the data to write
+ * @param n number of bytes to write
+ * @return number of bytes written on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
+    unsigned int n, int mode)
+{
+  struct GNUNET_IO_Handle *fh;
+  int ret;
+
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_WRITE
+      | GNUNET_DISK_OPEN_CREATE, mode);
+  if (!fh)
+    return GNUNET_SYSERR;
+  ret = GNUNET_DISK_file_write (fh, buffer, n);
+  GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
+
+  return ret;
+}
+
+/**
  * Scan a directory for files. The name of the directory
  * must be expanded first (!).
  * @param dirName the name of the directory
@@ -724,57 +803,6 @@
   return GNUNET_OK;
 }
 
-void
-GNUNET_DISK_file_close (const char *filename, int fd)
-{
-  if (0 != CLOSE (fd))
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "close", filename);
-}
-
-int
-GNUNET_DISK_file_open (const char *filename, int oflag, ...)
-{
-  char *fn;
-  int mode;
-  int ret;
-#ifdef MINGW
-  char szFile[_MAX_PATH + 1];
-  long lRet;
-
-  if ((lRet = plibc_conv_to_win_path (filename, szFile)) != ERROR_SUCCESS)
-    {
-      errno = ENOENT;
-      SetLastError (lRet);
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                "plibc_conv_to_win_path", filename);
-      return -1;
-    }
-  fn = GNUNET_strdup (szFile);
-#else
-  fn = GNUNET_STRINGS_filename_expand (filename);
-#endif
-  if (oflag & O_CREAT)
-    {
-      va_list arg;
-      va_start (arg, oflag);
-      mode = va_arg (arg, int);
-      va_end (arg);
-    }
-  else
-    {
-      mode = 0;
-    }
-#ifdef MINGW
-  /* set binary mode */
-  oflag |= O_BINARY;
-#endif
-  ret = OPEN (fn, oflag, mode);
-  if (ret == -1)
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
-  GNUNET_free (fn);
-  return ret;
-}
-
 #define COPY_BLK_SIZE 65536
 
 /**
@@ -788,21 +816,21 @@
   unsigned long long pos;
   unsigned long long size;
   unsigned long long len;
-  int in;
-  int out;
+  struct GNUNET_IO_Handle *in, *out;
 
   if (GNUNET_OK != GNUNET_DISK_file_size (src, &size, GNUNET_YES))
     return GNUNET_SYSERR;
   pos = 0;
-  in = GNUNET_DISK_file_open (src, O_RDONLY | O_LARGEFILE);
-  if (in == -1)
+  in = GNUNET_DISK_file_open (src, GNUNET_DISK_OPEN_READ);
+  if (!in)
     return GNUNET_SYSERR;
-  out = GNUNET_DISK_file_open (dst,
-                               O_LARGEFILE | O_WRONLY | O_CREAT | O_EXCL,
-                               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
-  if (out == -1)
+  out = GNUNET_DISK_file_open (dst, GNUNET_DISK_OPEN_WRITE
+      | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS,
+      GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE
+          | GNUNET_DISK_PERM_GROUP_READ | GNUNET_DISK_PERM_GROUP_WRITE);
+  if (!out)
     {
-      GNUNET_DISK_file_close (src, in);
+      GNUNET_DISK_file_close (&in);
       return GNUNET_SYSERR;
     }
   buf = GNUNET_malloc (COPY_BLK_SIZE);
@@ -811,20 +839,20 @@
       len = COPY_BLK_SIZE;
       if (len > size - pos)
         len = size - pos;
-      if (len != READ (in, buf, len))
+      if (len != GNUNET_DISK_file_read (in, buf, len))
         goto FAIL;
-      if (len != WRITE (out, buf, len))
+      if (len != GNUNET_DISK_file_write (out, buf, len))
         goto FAIL;
       pos += len;
     }
   GNUNET_free (buf);
-  GNUNET_DISK_file_close (src, in);
-  GNUNET_DISK_file_close (dst, out);
+  GNUNET_DISK_file_close (&in);
+  GNUNET_DISK_file_close (&out);
   return GNUNET_OK;
 FAIL:
   GNUNET_free (buf);
-  GNUNET_DISK_file_close (src, in);
-  GNUNET_DISK_file_close (dst, out);
+  GNUNET_DISK_file_close (&in);
+  GNUNET_DISK_file_close (&out);
   return GNUNET_SYSERR;
 }
 
@@ -881,6 +909,177 @@
 
 
 /**
+ * Lock a part of a file
+ * @param fh file handle
+ * @lockStart absolute position from where to lock
+ * @lockEnd absolute position until where to lock
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_DISK_file_lock(struct GNUNET_IO_Handle *fh, off_t lockStart,
+    off_t lockEnd)
+{
+  if (fh == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+#ifndef MINGW
+  struct flock fl;
+
+  memset(&fl, 0, sizeof(struct flock));
+  fl.l_type = F_WRLCK;
+  fl.l_whence = SEEK_SET;
+  fl.l_start = lockStart;
+  fl.l_len = lockEnd;
+
+  return fcntl(fh->fd, F_SETLK, &fl) != 0 ? GNUNET_SYSERR : GNUNET_OK;
+#else
+  if (!LockFile(fh->h, 0, lockStart, 0, lockEnd))
+  {
+    SetErrnoFromWinError(GetLastError());
+    return GNUNET_SYSERR;
+  }
+
+  return GNUNET_OK;
+#endif
+}
+
+
+/**
+ * Open a file
+ * @param fn file name to be opened
+ * @param flags opening flags, a combination of GNUNET_DISK_OPEN_xxx bit flags
+ * @param perm permissions for the newly created file
+ * @return IO handle on success, NULL on error
+ */
+struct GNUNET_IO_Handle *
+GNUNET_DISK_file_open (const char *fn, int flags, ...)
+{
+  char *expfn;
+  struct GNUNET_IO_Handle *ret;
+#ifdef MINGW
+  DWORD access, disp;
+  HANDLE h;
+#else
+  int oflags, mode;
+  int fd;
+#endif
+
+  expfn = GNUNET_STRINGS_filename_expand (fn);
+
+#ifndef MINGW
+  oflags = 0;
+  if (flags & GNUNET_DISK_OPEN_READ)
+    oflags = O_RDONLY;
+  if (flags & GNUNET_DISK_OPEN_WRITE)
+    oflags |= O_WRONLY;
+  if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
+    oflags |= (O_CREAT & O_EXCL);
+  if (flags & GNUNET_DISK_OPEN_TRUNCATE)
+    oflags |= O_TRUNC;
+  if (flags & GNUNET_DISK_OPEN_CREATE)
+    {
+      oflags |= O_CREAT;
+
+      va_list arg;
+      va_start (arg, oflag);
+      mode = va_arg (arg, int);
+      va_end (arg);
+    }
+  if (flags & GNUNET_DISK_OPEN_APPEND)
+    oflags = O_APPEND;
+
+  fd = open (expfn, oflag | O_LARGEFILE, perm, mode);
+  if (fd == -1)
+  {
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
+    return NULL;
+  }
+#else
+  access = 0;
+  disp = OPEN_ALWAYS;
+
+  if (flags & GNUNET_DISK_OPEN_READ)
+    access = FILE_READ_DATA;
+  if (flags & GNUNET_DISK_OPEN_WRITE)
+    access = FILE_WRITE_DATA;
+
+  if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
+    disp = CREATE_NEW;
+  if (flags & GNUNET_DISK_OPEN_TRUNCATE)
+    disp = TRUNCATE_EXISTING;
+  if (flags & GNUNET_DISK_OPEN_CREATE)
+    disp |= OPEN_ALWAYS;
+
+  /* TODO: access priviledges? */
+  h = CreateFile (expfn, access, FILE_SHARE_DELETE | FILE_SHARE_READ
+      | FILE_SHARE_WRITE, NULL, disp, FILE_ATTRIBUTE_NORMAL, NULL);
+  if (h == INVALID_HANDLE_VALUE)
+  {
+    SetErrnoFromWinError (GetLastError ());
+    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
+    return NULL;
+  }
+
+  if (flags & GNUNET_DISK_OPEN_APPEND)
+    if (SetFilePointer (h, 0, 0, FILE_END) == INVALID_SET_FILE_POINTER)
+    {
+      SetErrnoFromWinError (GetLastError ());
+      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "SetFilePointer", 
fn);
+      CloseHandle (h);
+      return NULL;
+    }
+#endif
+
+  ret = (struct GNUNET_IO_Handle *) GNUNET_malloc(sizeof(struct 
GNUNET_IO_Handle));
+#ifdef MINGW
+  ret->h = h;
+#else
+  ret->fd = fd;
+#endif
+
+  return ret;
+}
+
+/**
+ * Close an open file
+ * @param h file handle
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h)
+{
+  if (*h == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+#if MINGW
+  if (!CloseHandle ((*h)->h))
+  {
+    SetErrnoFromWinError (GetLastError ());
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
+    return GNUNET_SYSERR;
+  }
+#else
+  if (close ((*h)->fd) != 0)
+  {
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "close");
+    return GNUNET_SYSERR;
+  }
+#endif
+
+  GNUNET_IO_handle_invalidate (*h);
+  free(*h);
+  *h = NULL;
+
+  return GNUNET_OK;
+}
+
+/**
  * Construct full path to a file inside of the private
  * directory used by GNUnet.  Also creates the corresponding
  * directory.  If the resulting name is supposed to be
@@ -949,6 +1148,145 @@
   return ret;
 }
 
+/**
+ * Map a file into memory
+ * @param h open file handle
+ * @param m handle to the new mapping
+ * @param access access specification, GNUNET_DISK_MAP_xxx
+ * @param len size of the mapping
+ * @return pointer to the mapped memory region, NULL on failure
+ */
+void *
+GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct 
GNUNET_IO_Handle **m,
+    int access, size_t len)
+{
+  if (h == NULL)
+    {
+      errno = EINVAL;
+      return NULL;
+    }
 
+#ifdef MINGW
+  DWORD mapAccess, protect;
+  void *ret;
 
+  if (access & GNUNET_DISK_MAP_READ && access & GNUNET_DISK_MAP_WRITE)
+    {
+      protect = PAGE_READWRITE;
+      mapAccess = FILE_MAP_ALL_ACCESS;
+    }
+  else if (access & GNUNET_DISK_MAP_READ)
+    {
+      protect = PAGE_READONLY;
+      mapAccess = FILE_MAP_READ;
+    }
+  else if (access & GNUNET_DISK_MAP_WRITE)
+    {
+      protect = PAGE_READWRITE;
+      mapAccess = FILE_MAP_WRITE;
+    }
+  else
+    {
+      GNUNET_break (0);
+      return NULL;
+    }
+
+  *m = (struct GNUNET_IO_Handle *) GNUNET_malloc (sizeof (struct 
GNUNET_IO_Handle));
+  (*m)->h = CreateFileMapping (h->h, NULL, protect, 0, 0, NULL);
+  if ((*m)->h == INVALID_HANDLE_VALUE)
+    {
+      SetErrnoFromWinError (GetLastError ());
+      GNUNET_free (*m);
+      return NULL;
+    }
+
+  ret = MapViewOfFile ((*m)->h, mapAccess, 0, 0, len);
+  if (!ret)
+    {
+      SetErrnoFromWinError (GetLastError ());
+      CloseHandle ((*m)->h);
+      GNUNET_free (*m);
+    }
+
+  return ret;
+#else
+  int prot;
+
+  prot = flags = 0;
+  if (access & GNUNET_DISK_MAP_READ)
+    prot = PROT_READ;
+  if (access & GNUNET_DISK_MAP_WRITE)
+    prot |= PROT_WRITE;
+
+  return mmap (NULL, len, prot, MAP_SHARED, h->h, 0);
+#endif
+}
+
+/**
+ * Unmap a file
+ * @param h mapping handle
+ * @param addr pointer to the mapped memory region
+ * @param len size of the mapping
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len)
+{
+#ifdef MINGW
+  int ret;
+
+  if (h == NULL || *h == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+  ret = UnmapViewOfFile (addr) ? GNUNET_OK : GNUNET_SYSERR;
+  if (ret != GNUNET_OK)
+    SetErrnoFromWinError (GetLastError ());
+  if (!CloseHandle ((*h)->h) && ret == GNUNET_OK)
+    {
+      ret = GNUNET_SYSERR;
+      SetErrnoFromWinError (GetLastError ());
+    }
+
+  GNUNET_IO_handle_invalidate (*h);
+  GNUNET_free (*h);
+  h = NULL;
+
+  return ret;
+#else
+  int ret;
+  ret = munmap (addr, len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
+  GNUNET_IO_handle_invalidate (h);
+  return ret;
+#endif
+}
+
+/**
+ * Write file changes to disk
+ * @param h handle to an open file
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_DISK_file_sync (const struct GNUNET_IO_Handle *h)
+{
+  if (h == NULL)
+    {
+      errno = EINVAL;
+      return GNUNET_SYSERR;
+    }
+
+#ifdef MINGW
+  int ret;
+
+  ret = FlushFileBuffers (h->h) ? GNUNET_OK : GNUNET_SYSERR;
+  if (ret != GNUNET_OK)
+    SetErrnoFromWinError (GetLastError ());
+  return ret;
+#else
+  return fsync (h->fd) == -1 ? GNUNET_SYSERR : GNUNET_OK;
+#endif
+}
+
 /* end of disk.c */

Added: gnunet/src/util/io_handle.c
===================================================================
--- gnunet/src/util/io_handle.c                         (rev 0)
+++ gnunet/src/util/io_handle.c 2009-06-15 20:11:51 UTC (rev 8582)
@@ -0,0 +1,58 @@
+/*
+     This file is part of GNUnet.
+     (C) 2009 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+/**
+ * @file util/io_handle.c
+ * @brief common functions for IO handles
+ * @author Nils Durner
+ */
+#include "platform.h"
+#include "io_handle.h"
+#include "gnunet_common.h"
+
+/**
+ * Checks whether a handle is invalid
+ * @param h handle to check
+ * @return GNUNET_YES if invalid, GNUNET_NO if valid
+ */
+int
+GNUNET_IO_handle_invalid (const struct GNUNET_IO_Handle *h)
+{
+#ifdef MINGW
+  return !h || h->h == INVALID_HANDLE_VALUE ? GNUNET_YES : GNUNET_NO;
+#else
+  return !h || h->fd == -1 ? GNUNET_YES : GNUNET_NO;
+#endif
+}
+
+/**
+ * Mark a handle as invalid
+ * @param h file handle
+ */
+void
+GNUNET_IO_handle_invalidate (struct GNUNET_IO_Handle *h)
+{
+#ifdef MINGW
+  h->h = INVALID_HANDLE_VALUE;
+#else
+  h->fd = -1;
+#endif
+}
+
+/* end of io_handle.c */

Added: gnunet/src/util/io_handle.h
===================================================================
--- gnunet/src/util/io_handle.h                         (rev 0)
+++ gnunet/src/util/io_handle.h 2009-06-15 20:11:51 UTC (rev 8582)
@@ -0,0 +1,38 @@
+/*
+     This file is part of GNUnet.
+     (C) 2009 Christian Grothoff (and other contributing authors)
+
+     GNUnet is free software; you can redistribute it and/or modify
+     it under the terms of the GNU General Public License as published
+     by the Free Software Foundation; either version 2, or (at your
+     option) any later version.
+
+     GNUnet is distributed in the hope that it will be useful, but
+     WITHOUT ANY WARRANTY; without even the implied warranty of
+     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+     General Public License for more details.
+
+     You should have received a copy of the GNU General Public License
+     along with GNUnet; see the file COPYING.  If not, write to the
+     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+     Boston, MA 02111-1307, USA.
+*/
+
+/**
+ * @file util/io_handle.c
+ * @brief wrapper structure for file handles, sockets, ...
+ */
+
+#ifndef IO_HANDLE_H_
+#define IO_HANDLE_H_
+
+struct GNUNET_IO_Handle
+{
+#if MINGW
+  HANDLE h;
+#else
+  int fd;
+#endif
+};
+
+#endif /* IO_HANDLE_H_ */

Modified: gnunet/src/util/pseudonym.c
===================================================================
--- gnunet/src/util/pseudonym.c 2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/pseudonym.c 2009-06-15 20:11:51 UTC (rev 8582)
@@ -170,7 +170,8 @@
                                                         off + 1],
                                                    size,
                                                    
GNUNET_CONTAINER_META_DATA_SERIALIZE_FULL));
-  GNUNET_DISK_file_write (fn, buf, tag, "660");
+  GNUNET_DISK_fn_write (fn, buf, tag, GNUNET_DISK_PERM_USER_READ
+      | GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ);
   GNUNET_free (fn);
   GNUNET_free (buf);
   /* create entry for pseudonym name in names */
@@ -218,7 +219,7 @@
       return GNUNET_SYSERR;
     }
   buf = GNUNET_malloc (len);
-  if (len != GNUNET_DISK_file_read (fn, len, buf))
+  if (len != GNUNET_DISK_fn_read (fn, buf, len))
     {
       GNUNET_free (buf);
       GNUNET_free (fn);
@@ -280,7 +281,7 @@
   GNUNET_HashCode nh;
   char *fn;
   unsigned long long len;
-  int fd;
+  struct GNUNET_IO_Handle *fh;
   unsigned int i;
   unsigned int idx;
   char *ret;
@@ -317,12 +318,14 @@
   len = 0;
   if (0 == STAT (fn, &sbuf))
     GNUNET_DISK_file_size (fn, &len, GNUNET_YES);
-  fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
+      | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
+      | GNUNET_DISK_PERM_USER_WRITE);
   i = 0;
   idx = -1;
   while ((len >= sizeof (GNUNET_HashCode)) &&
          (sizeof (GNUNET_HashCode)
-          == READ (fd, &nh, sizeof (GNUNET_HashCode))))
+          == GNUNET_DISK_file_read (fh, &nh, sizeof (GNUNET_HashCode))))
     {
       if (0 == memcmp (&nh, nsid, sizeof (GNUNET_HashCode)))
         {
@@ -336,10 +339,10 @@
     {
       idx = i;
       if (sizeof (GNUNET_HashCode) !=
-          WRITE (fd, nsid, sizeof (GNUNET_HashCode)))
+          GNUNET_DISK_file_write (fh, nsid, sizeof (GNUNET_HashCode)))
         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "write", fn);
     }
-  CLOSE (fd);
+  GNUNET_DISK_file_close (&fh);
   ret = GNUNET_malloc (strlen (name) + 32);
   GNUNET_snprintf (ret, strlen (name) + 32, "%s-%u", name, idx);
   GNUNET_free (name);
@@ -362,7 +365,7 @@
   char *name;
   GNUNET_HashCode nh;
   char *fn;
-  int fd;
+  struct GNUNET_IO_Handle *fh;
 
   idx = -1;
   slen = strlen (ns_uname);
@@ -384,15 +387,17 @@
       GNUNET_free (fn);
       return GNUNET_SYSERR;
     }
-  fd = GNUNET_DISK_file_open (fn, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
+  fh = GNUNET_DISK_file_open (fn, GNUNET_DISK_OPEN_CREATE
+      | GNUNET_DISK_OPEN_READWRITE, GNUNET_DISK_PERM_USER_READ
+      | GNUNET_DISK_PERM_USER_WRITE);
   GNUNET_free (fn);
-  LSEEK (fd, idx * sizeof (GNUNET_HashCode), SEEK_SET);
-  if (sizeof (GNUNET_HashCode) != READ (fd, nsid, sizeof (GNUNET_HashCode)))
+  GNUNET_DISK_file_seek (fh, idx * sizeof (GNUNET_HashCode), GNUNET_SEEK_SET);
+  if (sizeof (GNUNET_HashCode) != GNUNET_DISK_file_read (fh, nsid, sizeof 
(GNUNET_HashCode)))
     {
-      CLOSE (fd);
+      GNUNET_DISK_file_close (&fh);
       return GNUNET_SYSERR;
     }
-  CLOSE (fd);
+  GNUNET_DISK_file_close (&fh);
   return GNUNET_OK;
 }
 

Modified: gnunet/src/util/service.c
===================================================================
--- gnunet/src/util/service.c   2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/service.c   2009-06-15 20:11:51 UTC (rev 8582)
@@ -1154,7 +1154,7 @@
   GNUNET_break (0 == CLOSE (0));
   GNUNET_break (0 == CLOSE (1));
   GNUNET_break (0 == CLOSE (filedes[0]));
-  nullfd = GNUNET_DISK_file_open ("/dev/null", O_RDWR | O_APPEND);
+  nullfd = OPEN ("/dev/null", O_RDWR | O_APPEND);
   if (nullfd < 0)
     return GNUNET_SYSERR;
   /* set stdin/stdout to /dev/null */

Modified: gnunet/src/util/test_disk.c
===================================================================
--- gnunet/src/util/test_disk.c 2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/test_disk.c 2009-06-15 20:11:51 UTC (rev 8582)
@@ -36,13 +36,12 @@
   char tmp[100 + 1];
   int ret;
 
-  if (GNUNET_OK !=
-      GNUNET_DISK_file_write (".testfile", TESTSTRING, strlen (TESTSTRING),
-                              "644"))
+  if (GNUNET_OK != GNUNET_DISK_fn_write (".testfile", TESTSTRING, strlen (
+      TESTSTRING), GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE))
     return 1;
   if (GNUNET_OK != GNUNET_DISK_file_test (".testfile"))
     return 1;
-  ret = GNUNET_DISK_file_read (".testfile", sizeof (tmp) - 1, tmp);
+  ret = GNUNET_DISK_fn_read (".testfile", tmp, sizeof (tmp) - 1);
   if (ret < 0)
     {
       fprintf (stderr,
@@ -59,7 +58,7 @@
     }
   GNUNET_DISK_file_copy (".testfile", ".testfile2");
   memset (tmp, 0, sizeof (tmp));
-  ret = GNUNET_DISK_file_read (".testfile2", sizeof (tmp) - 1, tmp);
+  ret = GNUNET_DISK_fn_read (".testfile2", tmp, sizeof (tmp) - 1);
   if (ret < 0)
     {
       fprintf (stderr,
@@ -86,15 +85,16 @@
 static int
 testOpenClose ()
 {
-  int fd;
+  struct GNUNET_IO_Handle *fh;
   unsigned long long size;
   long avail;
 
-  fd = GNUNET_DISK_file_open (".testfile",
-                              O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
-  GNUNET_assert (-1 != fd);
-  GNUNET_break (5 == WRITE (fd, "Hello", 5));
-  GNUNET_DISK_file_close (".testfile", fd);
+  fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
+      | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_READ
+      | GNUNET_DISK_PERM_USER_WRITE);
+  GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid (fh));
+  GNUNET_break (5 == GNUNET_DISK_file_write (fh, "Hello", 5));
+  GNUNET_DISK_file_close (&fh);
   GNUNET_break (GNUNET_OK ==
                 GNUNET_DISK_file_size (".testfile", &size, GNUNET_NO));
   if (size != 5)
@@ -105,18 +105,19 @@
   GNUNET_log_skip (1);
   avail = GNUNET_DISK_get_blocks_available (".testfile");
   GNUNET_log_skip (0);
-  fd = GNUNET_DISK_file_open (".testfile",
-                              O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
-  GNUNET_assert (-1 != fd);
+  fh = GNUNET_DISK_file_open (".testfile", GNUNET_DISK_OPEN_READWRITE
+      | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_WRITE
+      | GNUNET_DISK_PERM_USER_READ);
+  GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid (fh));
   while ((avail == GNUNET_DISK_get_blocks_available (".testfile")) &&
          (avail != -1))
-    if (16 != WRITE (fd, "HelloWorld123456", 16))
+    if (16 != GNUNET_DISK_file_write (fh, "HelloWorld123456", 16))
       {
-        GNUNET_DISK_file_close (".testfile", fd);
+        GNUNET_DISK_file_close (&fh);
         GNUNET_break (0 == UNLINK (".testfile"));
         return 1;
       }
-  GNUNET_DISK_file_close (".testfile", fd);
+  GNUNET_DISK_file_close (&fh);
   GNUNET_break (0 == UNLINK (".testfile"));
 
   return 0;

Modified: gnunet/src/util/test_os_load.c
===================================================================
--- gnunet/src/util/test_os_load.c      2009-06-15 17:42:43 UTC (rev 8581)
+++ gnunet/src/util/test_os_load.c      2009-06-15 20:11:51 UTC (rev 8582)
@@ -99,7 +99,7 @@
 testdisk ()
 {
   int ret;
-  int fd;
+  struct GNUNET_IO_Handle *fh;
   char buf[65536];
   struct GNUNET_TIME_Absolute start;
   struct GNUNET_CONFIGURATION_Handle *cfg;
@@ -132,20 +132,20 @@
       return 0;
     }
   memset (buf, 42, sizeof (buf));
-  fd =
-    GNUNET_DISK_file_open (".loadfile", O_WRONLY | O_CREAT,
-                           S_IRUSR | S_IWUSR);
-  GNUNET_assert (fd != -1);
+  fh = GNUNET_DISK_file_open (".loadfile", GNUNET_DISK_OPEN_WRITE
+      | GNUNET_DISK_OPEN_CREATE, GNUNET_DISK_PERM_USER_READ
+      | GNUNET_DISK_PERM_USER_WRITE);
+  GNUNET_assert (GNUNET_NO == GNUNET_IO_handle_invalid(fh));
   while (GNUNET_TIME_absolute_get_duration (start).value < 60 * 1000)
     {
-      LSEEK (fd, GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
-                                           1024 * 1024 * 1024), SEEK_SET);
-      GNUNET_assert (sizeof (buf) == WRITE (fd, buf, sizeof (buf)));
-      fsync (fd);
+      GNUNET_DISK_file_seek (fh, GNUNET_CRYPTO_random_u64 
(GNUNET_CRYPTO_QUALITY_WEAK,
+                                           1024 * 1024 * 1024), 
GNUNET_SEEK_SET);
+      GNUNET_assert (sizeof (buf) == GNUNET_DISK_file_write (fh, buf, sizeof 
(buf)));
+      GNUNET_DISK_file_sync (fh);
       if (ret < GNUNET_OS_load_disk_get (cfg))
         break;
     }
-  GNUNET_break (0 == CLOSE (fd));
+  GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (&fh));
   GNUNET_break (0 == UNLINK (".loadfile"));
   if (ret >= GNUNET_OS_load_disk_get (cfg))
     {





reply via email to

[Prev in Thread] Current Thread [Next in Thread]