gnunet-svn
[Top][All Lists]
Advanced

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

[GNUnet-SVN] r26115 - in gnunet/src: include util


From: gnunet
Subject: [GNUnet-SVN] r26115 - in gnunet/src: include util
Date: Thu, 14 Feb 2013 17:15:15 +0100

Author: LRN
Date: 2013-02-14 17:15:15 +0100 (Thu, 14 Feb 2013)
New Revision: 26115

Modified:
   gnunet/src/include/gnunet_disk_lib.h
   gnunet/src/util/disk.c
Log:
More flexible native->GNUnet_handle file opening

Can now open not just FILEs, but also integer fds and
native Windows handles.
The last two will be used later by the new pipe control code.

Modified: gnunet/src/include/gnunet_disk_lib.h
===================================================================
--- gnunet/src/include/gnunet_disk_lib.h        2013-02-14 16:15:11 UTC (rev 
26114)
+++ gnunet/src/include/gnunet_disk_lib.h        2013-02-14 16:15:15 UTC (rev 
26115)
@@ -504,7 +504,28 @@
                          enum GNUNET_DISK_PipeEnd n);
 
 
+#if WINDOWS
 /**
+ * Get a GNUnet file handle from a W32 handle (W32-only).
+ * Do not call on non-W32 platforms (returns NULL).
+ *
+ * @param handle native handle
+ * @return GNUnet file handle corresponding to the W32 handle
+ */
+struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh);
+#endif
+
+/**
+ * Get a handle from a native integer FD.
+ *
+ * @param fd native integer file descriptor
+ * @return file handle corresponding to the descriptor
+ */
+struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_get_handle_from_int_fd (int fno);
+
+/**
  * Get a handle from a native FD.
  *
  * @param fd native file descriptor

Modified: gnunet/src/util/disk.c
===================================================================
--- gnunet/src/util/disk.c      2013-02-14 16:15:11 UTC (rev 26114)
+++ gnunet/src/util/disk.c      2013-02-14 16:15:15 UTC (rev 26115)
@@ -1866,52 +1866,81 @@
   return ret;
 }
 
+#ifndef WINDOWS
+/**
+ * Get a GNUnet file handle from a W32 handle.
+ *
+ * @param handle native handle
+ * @return GNUnet file handle corresponding to the W32 handle
+ */
+struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_get_handle_from_w32_handle (HANDLE osfh)
+{
+  struct GNUNET_DISK_FileHandle *fh;
 
+  DWORD dwret;
+  enum GNUNET_FILE_Type ftype;
+
+  dwret = GetFileType (osfh);
+  switch (dwret)
+  {
+  case FILE_TYPE_DISK:
+    ftype = GNUNET_DISK_HANLDE_TYPE_FILE;
+    break;
+  case FILE_TYPE_PIPE:
+    ftype = GNUNET_DISK_HANLDE_TYPE_PIPE;
+    break;
+  default:
+    return NULL;
+  }
+
+  fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
+
+  fh->h = osfh;
+  fh->type = ftype;
+  if (ftype == GNUNET_DISK_HANLDE_TYPE_PIPE)
+  {
+    /**
+     * Note that we can't make it overlapped if it isn't already.
+     * (ReOpenFile() is only available in 2003/Vista).
+     * The process that opened this file in the first place (usually a parent
+     * process, if this is stdin/stdout/stderr) must make it overlapped,
+     * otherwise we're screwed, as selecting on non-overlapped handle
+     * will block.
+     */
+    fh->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
+    fh->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
+    fh->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
+    fh->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
+  }
+
+  return fh;
+}
+#endif
+
 /**
- * Get a handle from a native FD.
+ * Get a handle from a native integer FD.
  *
- * @param fd native file descriptor
+ * @param fd native integer file descriptor
  * @return file handle corresponding to the descriptor
  */
 struct GNUNET_DISK_FileHandle *
-GNUNET_DISK_get_handle_from_native (FILE *fd)
+GNUNET_DISK_get_handle_from_int_fd (int fno)
 {
   struct GNUNET_DISK_FileHandle *fh;
-  int fno;
-#if MINGW
+
+#ifndef WINDOWS
+  fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
+
+  fh->fd = fno;
+#else
   intptr_t osfh;
-#endif
 
-  fno = fileno (fd);
-  if (-1 == fno)
-    return NULL;
-
-#if MINGW
   osfh = _get_osfhandle (fno);
   if (INVALID_HANDLE_VALUE == (HANDLE) osfh)
     return NULL;
-#endif
 
-  fh = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
-
-#if MINGW
-  fh->h = (HANDLE) osfh;
-  /* Assume it to be a pipe. TODO: use some kind of detection
-   * function to figure out handle type.
-   * Note that we can't make it overlapped if it isn't already.
-   * (ReOpenFile() is only available in 2003/Vista).
-   * The process that opened this file in the first place (usually a parent
-   * process, if this is stdin/stdout/stderr) must make it overlapped,
-   * otherwise we're screwed, as selecting on non-overlapped handle
-   * will block.
-   */
-  fh->type = GNUNET_DISK_HANLDE_TYPE_PIPE;
-  fh->oOverlapRead = GNUNET_malloc (sizeof (OVERLAPPED));
-  fh->oOverlapWrite = GNUNET_malloc (sizeof (OVERLAPPED));
-  fh->oOverlapRead->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
-  fh->oOverlapWrite->hEvent = CreateEvent (NULL, FALSE, FALSE, NULL);
-#else
-  fh->fd = fno;
+  fh = GNUNET_DISK_get_handle_from_w32_handle ((HANDLE) osfh);
 #endif
 
   return fh;
@@ -1919,6 +1948,25 @@
 
 
 /**
+ * Get a handle from a native streaming FD.
+ *
+ * @param fd native streaming file descriptor
+ * @return file handle corresponding to the descriptor
+ */
+struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_get_handle_from_native (FILE *fd)
+{
+  int fno;
+
+  fno = fileno (fd);
+  if (-1 == fno)
+    return NULL;
+
+  return GNUNET_DISK_get_handle_from_int_fd (fno);
+}
+
+
+/**
  * 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




reply via email to

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