Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / disk.c
index 88f11be51993a8413926b7cfb320a7c3478c243c..307c4535bf2478576c257d7234816ec374bf0873 100644 (file)
@@ -810,10 +810,6 @@ GNUNET_DISK_directory_scan (const char *dirName,
  */
 struct GNUNET_DISK_DirectoryIterator
 {
-  /**
-   * Our scheduler.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
 
   /**
    * Function to call on directory entries.
@@ -906,8 +902,7 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
       GNUNET_DISK_directory_iterator_next (iter, GNUNET_YES);
       return GNUNET_NO;
     }
-  GNUNET_SCHEDULER_add_with_priority (iter->sched,
-                                      iter->priority,
+  GNUNET_SCHEDULER_add_with_priority (iter->priority,
                                       &directory_iterator_task, iter);
   return GNUNET_YES;
 }
@@ -919,15 +914,13 @@ GNUNET_DISK_directory_iterator_next (struct GNUNET_DISK_DirectoryIterator
  * If a scheduler does not need to be used, GNUNET_DISK_directory_scan
  * may provide a simpler API.
  *
- * @param sched scheduler to use
  * @param prio priority to use
  * @param dirName the name of the directory
  * @param callback the method to call for each file
  * @param callback_cls closure for callback
  */
 void
-GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
-                                      enum GNUNET_SCHEDULER_Priority prio,
+GNUNET_DISK_directory_iterator_start (enum GNUNET_SCHEDULER_Priority prio,
                                       const char *dirName,
                                       GNUNET_DISK_DirectoryIteratorCallback
                                       callback, void *callback_cls)
@@ -935,7 +928,6 @@ GNUNET_DISK_directory_iterator_start (struct GNUNET_SCHEDULER_Handle *sched,
   struct GNUNET_DISK_DirectoryIterator *di;
 
   di = GNUNET_malloc (sizeof (struct GNUNET_DISK_DirectoryIterator));
-  di->sched = sched;
   di->callback = callback;
   di->callback_cls = callback_cls;
   di->directory = OPENDIR (dirName);
@@ -1642,14 +1634,18 @@ GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h)
 #endif
 }
 
+
 /**
  * Creates an interprocess channel
  *
  * @param blocking creates an asynchronous pipe if set to GNUNET_NO
+ * @param inherit_read inherit the parent processes stdin (only for windows)
+ * @param inherit_write inherit the parent processes stdout (only for windows)
+ *
  * @return handle to the new pipe, NULL on error
  */
 struct GNUNET_DISK_PipeHandle *
-GNUNET_DISK_pipe (int blocking)
+GNUNET_DISK_pipe (int blocking, int inherit_read, int inherit_write)
 {
   struct GNUNET_DISK_PipeHandle *p;
   struct GNUNET_DISK_FileHandle *fds;
@@ -1669,24 +1665,34 @@ GNUNET_DISK_pipe (int blocking)
   ret = pipe (fd);
   if (ret == -1)
     {
+      eno = errno;
+      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "pipe");
       GNUNET_free (p);
+      errno = eno;
       return NULL;
     }
   p->fd[0]->fd = fd[0];
   p->fd[1]->fd = fd[1];
   ret = 0;
   flags = fcntl (fd[0], F_GETFL);
-  flags |= FD_CLOEXEC;
   if (!blocking)
     flags |= O_NONBLOCK;
   if (0 > fcntl (fd[0], F_SETFL, flags))
     ret = -1;
-  flags = fcntl (fd[1], F_GETFL);
+  flags = fcntl (fd[0], F_GETFD);
   flags |= FD_CLOEXEC;
+  if (0 > fcntl (fd[0], F_SETFD, flags))
+    ret = -1;
+
+  flags = fcntl (fd[1], F_GETFL);
   if (!blocking)
     flags |= O_NONBLOCK;
   if (0 > fcntl (fd[1], F_SETFL, flags))
     ret = -1;
+  flags = fcntl (fd[1], F_GETFD);
+  flags |= FD_CLOEXEC;
+  if (0 > fcntl (fd[1], F_SETFD, flags))
+    ret = -1;
   if (ret == -1)
     {
       eno = errno;
@@ -1699,6 +1705,7 @@ GNUNET_DISK_pipe (int blocking)
     }
 #else
   BOOL ret;
+  HANDLE tmp_handle;
 
   ret = CreatePipe (&p->fd[0]->h, &p->fd[1]->h, NULL, 0);
   if (!ret)
@@ -1707,6 +1714,31 @@ GNUNET_DISK_pipe (int blocking)
       SetErrnoFromWinError (GetLastError ());
       return NULL;
     }
+  if (!DuplicateHandle (GetCurrentProcess (), p->fd[0]->h,
+               GetCurrentProcess (), &tmp_handle, 0, inherit_read == GNUNET_YES ? TRUE : FALSE,
+                       DUPLICATE_SAME_ACCESS))
+       {
+         SetErrnoFromWinError (GetLastError ());
+         CloseHandle (p->fd[0]->h);
+         CloseHandle (p->fd[1]->h);
+         GNUNET_free (p);
+         return NULL;
+       }
+       CloseHandle (p->fd[0]->h);
+       p->fd[0]->h = tmp_handle;
+
+       if (!DuplicateHandle (GetCurrentProcess (), p->fd[1]->h,
+                       GetCurrentProcess (), &tmp_handle, 0, inherit_write == GNUNET_YES ? TRUE : FALSE,
+                       DUPLICATE_SAME_ACCESS))
+       {
+         SetErrnoFromWinError (GetLastError ());
+         CloseHandle (p->fd[0]->h);
+         CloseHandle (p->fd[1]->h);
+         GNUNET_free (p);
+         return NULL;
+       }
+  CloseHandle (p->fd[1]->h);
+  p->fd[1]->h = tmp_handle;
   if (!blocking)
     {
       DWORD mode;