MinGW
[oweals/gnunet.git] / src / util / disk.c
index 3b3cb9e652c8de07c3adc1e4cd21f7be2df84599..588ecae3cc5dd3b606013032f8ee06d8425cc28c 100644 (file)
@@ -284,10 +284,15 @@ GNUNET_DISK_mktemp (const char *t)
   tmpdir = getenv ("TMPDIR");
   tmpdir = tmpdir ? tmpdir : "/tmp";
 
-  GNUNET_asprintf (&tmpl, "%s%s%s%s", tmpdir, DIR_SEPARATOR_STR, t, "XXXXXX");
+  GNUNET_asprintf (&tmpl, "%s/%s%s", tmpdir, t, "XXXXXX");
 #ifdef MINGW
   fn = (char *) GNUNET_malloc (MAX_PATH + 1);
-  plibc_conv_to_win_path (tmpl, fn);
+  if (ERROR_SUCCESS != plibc_conv_to_win_path (tmpl, fn))
+    {
+      GNUNET_free (fn);
+      GNUNET_free (tmpl);
+      return NULL;
+    }
   GNUNET_free (tmpl);
 #else
   fn = tmpl;
@@ -857,8 +862,8 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
       return GNUNET_NO;
     }
   GNUNET_SCHEDULER_add_with_priority (iter->sched,
-                                     iter->priority,
-                                     &directory_iterator_task, iter);
+                                      iter->priority,
+                                      &directory_iterator_task, iter);
   return GNUNET_YES;
 }
 
@@ -906,7 +911,7 @@ GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
 static int
 remove_helper (void *unused, const char *fn)
 {
-  GNUNET_DISK_directory_remove (fn);
+  (void) GNUNET_DISK_directory_remove (fn);
   return GNUNET_OK;
 }
 
@@ -1185,6 +1190,8 @@ GNUNET_DISK_file_open (const char *fn,
 #endif
 
   expfn = GNUNET_STRINGS_filename_expand (fn);
+  if (NULL == expfn)
+    return NULL;
 
 #ifndef MINGW
   mode = 0;
@@ -1201,7 +1208,7 @@ GNUNET_DISK_file_open (const char *fn,
       return NULL;
     }
   if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
-    oflags |= (O_CREAT & O_EXCL);
+    oflags |= (O_CREAT | O_EXCL);
   if (flags & GNUNET_DISK_OPEN_TRUNCATE)
     oflags |= O_TRUNC;
   if (flags & GNUNET_DISK_OPEN_APPEND)
@@ -1232,7 +1239,10 @@ GNUNET_DISK_file_open (const char *fn,
   fd = open (expfn, oflags | O_LARGEFILE, mode);
   if (fd == -1)
     {
-      GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
+      if (0 == (flags & GNUNET_DISK_OPEN_FAILIFEXISTS))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "open", expfn);
+      else
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_DEBUG, "open", expfn);
       GNUNET_free (expfn);
       return NULL;
     }
@@ -1264,7 +1274,7 @@ GNUNET_DISK_file_open (const char *fn,
     }
   else
     {
-      disp = OPEN_ALWAYS;
+      disp = OPEN_EXISTING;
     }
 
   /* TODO: access priviledges? */
@@ -1432,6 +1442,10 @@ struct GNUNET_DISK_MapHandle
 };
 
 
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *) -1)
+#endif
+
 /**
  * Map a file into memory
  *
@@ -1498,6 +1512,7 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
   return ret;
 #else
   int prot;
+  int ec;
 
   prot = 0;
   if (access & GNUNET_DISK_MAP_TYPE_READ)
@@ -1506,6 +1521,14 @@ GNUNET_DISK_file_map (const struct GNUNET_DISK_FileHandle *h,
     prot |= PROT_WRITE;
   *m = GNUNET_malloc (sizeof (struct GNUNET_DISK_MapHandle));
   (*m)->addr = mmap (NULL, len, prot, MAP_SHARED, h->fd, 0);
+  GNUNET_assert (NULL != (*m)->addr);
+  if (MAP_FAILED == (*m)->addr)
+    {    
+      ec = errno;
+      GNUNET_free (*m);
+      errno = ec;
+      return NULL;
+    }
   (*m)->len = len;
   return (*m)->addr;
 #endif
@@ -1642,8 +1665,6 @@ GNUNET_DISK_pipe (int blocking)
       DWORD mode;
 
       mode = PIPE_NOWAIT;
-      p->fd[0] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
-      p->fd[1] = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
       SetNamedPipeHandleState (p->fd[0]->h, &mode, NULL, NULL);
       SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
       /* this always fails on Windows 95, so we don't care about error handling */
@@ -1653,6 +1674,64 @@ GNUNET_DISK_pipe (int blocking)
 }
 
 
+/**
+ * Closes an interprocess channel
+ *
+ * @param p pipe to close
+ * @param end which end of the pipe to close
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_DISK_pipe_close_end (struct GNUNET_DISK_PipeHandle *p,
+                           enum GNUNET_DISK_PipeEnd end)
+{
+  int ret = GNUNET_OK;
+  int save;
+
+  /* FIXME: What can we safely set HANDLE to once we've closed an end, NULL? */
+#ifdef MINGW
+  if (end == GNUNET_DISK_PIPE_END_READ)
+    {
+      if (!CloseHandle (p->fd[0]->h))
+        {
+          SetErrnoFromWinError (GetLastError ());
+          ret = GNUNET_SYSERR;
+        }
+    }
+  else if (end == GNUNET_DISK_PIPE_END_WRITE)
+    {
+      if (!CloseHandle (p->fd[1]->h))
+        {
+          SetErrnoFromWinError (GetLastError ());
+          ret = GNUNET_SYSERR;
+        }
+    }
+  save = errno;
+#else
+  save = 0;
+  if (end == GNUNET_DISK_PIPE_END_READ)
+    {
+      if (0 != close (p->fd[0]->fd))
+        {
+          ret = GNUNET_SYSERR;
+          save = errno;
+        }
+      p->fd[0]->fd = -1;
+    }
+  else if (end == GNUNET_DISK_PIPE_END_WRITE)
+    {
+      if (0 != close (p->fd[1]->fd))
+        {
+          ret = GNUNET_SYSERR;
+          save = errno;
+        }
+      p->fd[1]->fd = -1;
+    }
+#endif
+  errno = save;
+  return ret;
+}
+
 /**
  * Closes an interprocess channel
  *
@@ -1679,15 +1758,22 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
   save = errno;
 #else
   save = 0;
-  if (0 != close (p->fd[0]->fd))
+  if (p->fd[0]->fd != -1)
     {
-      ret = GNUNET_SYSERR;
-      save = errno;
+      if (0 != close (p->fd[0]->fd))
+        {
+          ret = GNUNET_SYSERR;
+          save = errno;
+        }
     }
-  if (0 != close (p->fd[1]->fd))
+
+  if (p->fd[1]->fd != -1)
     {
-      ret = GNUNET_SYSERR;
-      save = errno;
+      if (0 != close (p->fd[1]->fd))
+        {
+          ret = GNUNET_SYSERR;
+          save = errno;
+        }
     }
 #endif
   GNUNET_free (p);
@@ -1698,8 +1784,10 @@ GNUNET_DISK_pipe_close (struct GNUNET_DISK_PipeHandle *p)
 
 /**
  * Get the handle to a particular pipe end
+ *
  * @param p pipe
  * @param n end to access
+ * @return handle for the respective end
  */
 const struct GNUNET_DISK_FileHandle *
 GNUNET_DISK_pipe_handle (const struct GNUNET_DISK_PipeHandle *p,