GNUNET_DISK_OPEN_READ
[oweals/gnunet.git] / src / util / disk.c
index b0d3b18a4765e11016a8e1c5adb8dd993f180e68..2722dda71df81465b96fa937a3e321fa57d1a332 100644 (file)
  */
 
 #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"
@@ -76,6 +74,15 @@ typedef struct
   int include_sym_links;
 } GetFileSizeData;
 
+struct GNUNET_IO_Handle
+{
+#if MINGW
+  HANDLE h;
+#else
+  int fd;
+#endif
+};
+
 static int
 getSizeRec (void *ptr, const char *fn)
 {
@@ -111,6 +118,34 @@ getSizeRec (void *ptr, const char *fn)
   return GNUNET_OK;
 }
 
+/**
+ * Checks whether a handle is invalid
+ * @param h handle to check
+ * @return GNUNET_YES if invalid, GNUNET_NO if valid
+ */
+int
+GNUNET_DISK_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
+ */
+static void
+GNUNET_DISK_handle_invalidate (struct GNUNET_IO_Handle *h)
+{
+#ifdef MINGW
+  h->h = INVALID_HANDLE_VALUE;
+#else
+  h->fd = -1;
+#endif
+}
 
 /**
  * Move the read/write pointer in a file
@@ -435,7 +470,7 @@ GNUNET_DISK_file_read (const struct GNUNET_IO_Handle *h, void *result, int len)
  * @param fn file name
  * @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
+ * @return number of bytes read, GNUNET_SYSERR on failure
  */
 int
 GNUNET_DISK_fn_read (const char * const fn, void *result, int len)
@@ -505,7 +540,7 @@ GNUNET_DISK_file_write (const struct GNUNET_IO_Handle *h, const void *buffer,
  * @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
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
  */
 int
 GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
@@ -518,7 +553,7 @@ GNUNET_DISK_fn_write (const char * const fn, const void *buffer,
       | GNUNET_DISK_OPEN_CREATE, mode);
   if (!fh)
     return GNUNET_SYSERR;
-  ret = GNUNET_DISK_file_write (fh, buffer, n);
+  ret = (n == GNUNET_DISK_file_write (fh, buffer, n)) ? GNUNET_OK : GNUNET_SYSERR;
   GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(&fh));
 
   return ret;
@@ -981,17 +1016,39 @@ GNUNET_DISK_file_open (const char *fn, int flags, ...)
     oflags |= O_TRUNC;
   if (flags & GNUNET_DISK_OPEN_CREATE)
     {
+      int perm;
+
       oflags |= O_CREAT;
 
       va_list arg;
-      va_start (arg, oflag);
-      mode = va_arg (arg, int);
+      va_start (arg, flags);
+      perm = va_arg (arg, int);
       va_end (arg);
+
+      mode = 0;
+      if (perm & GNUNET_DISK_PERM_USER_READ)
+        mode = S_IRUSR;
+      if (perm & GNUNET_DISK_PERM_USER_WRITE)
+        mode |= S_IWUSR;
+      if (perm & GNUNET_DISK_PERM_USER_EXEC)
+        mode |= S_IXUSR;
+      if (perm & GNUNET_DISK_PERM_GROUP_READ)
+        mode = S_IRGRP;
+      if (perm & GNUNET_DISK_PERM_GROUP_WRITE)
+        mode |= S_IWGRP;
+      if (perm & GNUNET_DISK_PERM_GROUP_EXEC)
+        mode |= S_IXGRP;
+      if (perm & GNUNET_DISK_PERM_OTHER_READ)
+        mode = S_IROTH;
+      if (perm & GNUNET_DISK_PERM_OTHER_WRITE)
+        mode |= S_IWOTH;
+      if (perm & GNUNET_DISK_PERM_OTHER_EXEC)
+        mode |= S_IXOTH;
     }
   if (flags & GNUNET_DISK_OPEN_APPEND)
     oflags = O_APPEND;
 
-  fd = open (expfn, oflag | O_LARGEFILE, perm, mode);
+  fd = open (expfn, oflags | O_LARGEFILE, mode);
   if (fd == -1)
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", fn);
@@ -1072,7 +1129,7 @@ GNUNET_DISK_file_close (struct GNUNET_IO_Handle **h)
   }
 #endif
 
-  GNUNET_IO_handle_invalidate (*h);
+  GNUNET_DISK_handle_invalidate (*h);
   free(*h);
   *h = NULL;
 
@@ -1212,13 +1269,13 @@ GNUNET_DISK_file_map (const struct GNUNET_IO_Handle *h, struct GNUNET_IO_Handle
 #else
   int prot;
 
-  prot = flags = 0;
+  prot = 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);
+  return mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
 #endif
 }
 
@@ -1250,7 +1307,7 @@ GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len)
       SetErrnoFromWinError (GetLastError ());
     }
 
-  GNUNET_IO_handle_invalidate (*h);
+  GNUNET_DISK_handle_invalidate (*h);
   GNUNET_free (*h);
   h = NULL;
 
@@ -1258,7 +1315,7 @@ GNUNET_DISK_file_unmap (struct GNUNET_IO_Handle **h, void *addr, size_t len)
 #else
   int ret;
   ret = munmap (addr, len) != -1 ? GNUNET_OK : GNUNET_SYSERR;
-  GNUNET_IO_handle_invalidate (h);
+  GNUNET_DISK_handle_invalidate (*h);
   return ret;
 #endif
 }