gnunet-svn
[Top][All Lists]
Advanced

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

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


From: gnunet
Subject: [GNUnet-SVN] r13764 - in gnunet/src: include util
Date: Mon, 22 Nov 2010 21:15:29 +0100

Author: durner
Date: 2010-11-22 21:15:29 +0100 (Mon, 22 Nov 2010)
New Revision: 13764

Modified:
   gnunet/src/include/gnunet_disk_lib.h
   gnunet/src/util/disk.c
   gnunet/src/util/disk.h
   gnunet/src/util/network.c
Log:
named pipes

Modified: gnunet/src/include/gnunet_disk_lib.h
===================================================================
--- gnunet/src/include/gnunet_disk_lib.h        2010-11-22 16:59:21 UTC (rev 
13763)
+++ gnunet/src/include/gnunet_disk_lib.h        2010-11-22 20:15:29 UTC (rev 
13764)
@@ -642,6 +642,23 @@
  */
 int GNUNET_DISK_file_sync (const struct GNUNET_DISK_FileHandle *h);
 
+/**
+ * Creates a named pipe/FIFO
+ * @param fn name of the named pipe
+ * @param flags open flags
+ * @param perm access permissions
+ * @return pipe handle on success, NULL on error
+ */
+struct GNUNET_DISK_FileHandle *GNUNET_DISK_npipe_open (const char *fn,
+    enum GNUNET_DISK_OpenFlags flags, enum GNUNET_DISK_AccessPermissions perm);
+
+/**
+ * Closes a named pipe/FIFO
+ * @param pipe named pipe
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe);
+
 #if 0                           /* keep Emacsens' auto-indent happy */
 {
 #endif

Modified: gnunet/src/util/disk.c
===================================================================
--- gnunet/src/util/disk.c      2010-11-22 16:59:21 UTC (rev 13763)
+++ gnunet/src/util/disk.c      2010-11-22 20:15:29 UTC (rev 13764)
@@ -101,6 +101,34 @@
 };
 
 
+int translate_unix_perms(enum GNUNET_DISK_AccessPermissions perm)
+{
+  int mode;
+
+  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;
+
+  return mode;
+}
+
+
 /**
  * Iterate over all files in the given directory and 
  * accumulate their size.
@@ -1259,24 +1287,7 @@
     {
       (void) GNUNET_DISK_directory_create_for_file (expfn);
       oflags |= O_CREAT;
-      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;
+      mode = translate_unix_perms(perm);
     }
 
   fd = open (expfn, oflags | O_LARGEFILE, mode);
@@ -1347,6 +1358,7 @@
   ret = GNUNET_malloc (sizeof (struct GNUNET_DISK_FileHandle));
 #ifdef MINGW
   ret->h = h;
+  ret->type = GNUNET_DISK_FILE;
 #else
   ret->fd = fd;
 #endif
@@ -1748,6 +1760,8 @@
       SetNamedPipeHandleState (p->fd[1]->h, &mode, NULL, NULL);
       /* this always fails on Windows 95, so we don't care about error 
handling */
     }
+  p->fd[0]->type = GNUNET_PIPE;
+  p->fd[1]->type = GNUNET_PIPE;
 #endif
   return p;
 }
@@ -1863,6 +1877,89 @@
 
 
 /**
+ * Creates a named pipe/FIFO
+ * @param fn name of the named pipe
+ * @param flags open flags
+ * @param perm access permissions
+ * @return pipe handle on success, NULL on error
+ */
+struct GNUNET_DISK_FileHandle *
+GNUNET_DISK_npipe_open (const char *fn,
+                       enum GNUNET_DISK_OpenFlags flags,
+                       enum GNUNET_DISK_AccessPermissions perm)
+{
+  struct GNUNET_DISK_FileHandle *ret;
+
+#ifdef MINGW
+  HANDLE h;
+  DWORD openMode;
+  char *name;
+
+  openMode = 0;
+  if (flags & GNUNET_DISK_OPEN_READWRITE)
+    openMode = PIPE_ACCESS_DUPLEX;
+  else if (flags & GNUNET_DISK_OPEN_READ)
+    openMode = PIPE_ACCESS_INBOUND;
+  else if (flags & GNUNET_DISK_OPEN_WRITE)
+    openMode = PIPE_ACCESS_OUTBOUND;
+
+  if (flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
+    openMode |= FILE_FLAG_FIRST_PIPE_INSTANCE;
+
+  GNUNET_asprintf(&name, "\\\\.\\pipe\\pipename\\%s", fn);
+  h = CreateNamedPipe (fn, openMode | FILE_FLAG_OVERLAPPED,
+      PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 2, 1, 1, 0, NULL);
+  GNUNET_free(name);
+  if (h == NULL)
+    {
+      SetErrnoFromWinError(GetLastError());
+      return NULL;
+    }
+
+  ret = GNUNET_malloc(sizeof(*ret));
+  ret->h = h;
+
+  return ret;
+#else
+  int fd;
+
+  if (mkfifo(fn, translate_unix_perms(perm)) == -1)
+    {
+      if (errno == EEXISTS && flags & GNUNET_DISK_OPEN_FAILIFEXISTS)
+        return NULL;
+    }
+
+  flags ~= GNUNET_DISK_OPEN_FAILIFEXISTS;
+  return GNUNET_DISK_file_open(fn, flags, perm);
+#endif
+}
+
+/**
+ * Closes a named pipe/FIFO
+ * @param pipe named pipe
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise
+ */
+int
+GNUNET_DISK_npipe_close (struct GNUNET_DISK_FileHandle *pipe)
+{
+#ifndef MINGW
+  return close(pipe->fd) == 0 ? GNUNET_OK : GNUNET_SYSERR;
+#else
+  BOOL ret;
+
+  ret = CloseHandle(pipe->h);
+  if (!ret)
+    {
+      SetErrnoFromWinError(GetLastError());
+      return GNUNET_SYSERR;
+    }
+  else
+    return GNUNET_OK;
+#endif
+}
+
+
+/**
  * Get the handle to a particular pipe end
  *
  * @param p pipe

Modified: gnunet/src/util/disk.h
===================================================================
--- gnunet/src/util/disk.h      2010-11-22 16:59:21 UTC (rev 13763)
+++ gnunet/src/util/disk.h      2010-11-22 20:15:29 UTC (rev 13764)
@@ -42,10 +42,14 @@
    * File handle under W32.
    */ 
   HANDLE h;
-  
-#else                           /* 
- */
+
   /**
+   * Type
+   */
+  enum {GNUNET_DISK_FILE, GNUNET_PIPE} type;
+#else
+
+  /**
    * File handle on other OSes.
    */ 
   int fd;

Modified: gnunet/src/util/network.c
===================================================================
--- gnunet/src/util/network.c   2010-11-22 16:59:21 UTC (rev 13763)
+++ gnunet/src/util/network.c   2010-11-22 20:15:29 UTC (rev 13764)
@@ -1137,28 +1137,38 @@
                GNUNET_CONTAINER_slist_next (i))
 
             {
-              HANDLE h;
+              struct GNUNET_DISK_FileHandle *fh;
               DWORD dwBytes;
-              h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
-              if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
+              fh = (struct GNUNET_DISK_FileHandle *) 
GNUNET_CONTAINER_slist_get (i, NULL);
+              if (fh->type == GNUNET_PIPE)
                 {
-                  retcode = -1;
-                  SetErrnoFromWinError (GetLastError ());
+                  if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
+                    {
+                      retcode = -1;
+                      SetErrnoFromWinError (GetLastError ());
 
-#if DEBUG_NETWORK
-                  GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
-                                       "PeekNamedPipe");
+    #if DEBUG_NETWORK
+                      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                                           "PeekNamedPipe");
 
-#endif
-                  goto select_loop_end;
+    #endif
+                      goto select_loop_end;
+                    }
+                  else if (dwBytes)
+
+                    {
+                      GNUNET_CONTAINER_slist_add (handles_read,
+                                                  
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+                                                  fh, sizeof (struct 
GNUNET_DISK_FileHandle));
+                      retcode++;
+                    }
                 }
-              else if (dwBytes)
-
+              else
                 {
+                  /* Should we wait for more bytes to read here (in case of 
previous EOF)? */
                   GNUNET_CONTAINER_slist_add (handles_read,
                                               
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
-                                              &h, sizeof (HANDLE));
-                  retcode++;
+                                              fh, sizeof (struct 
GNUNET_DISK_FileHandle));
                 }
             }
           GNUNET_CONTAINER_slist_iter_destroy (i);
@@ -1174,16 +1184,20 @@
                GNUNET_CONTAINER_slist_next (i))
 
             {
-              HANDLE h;
+              struct GNUNET_DISK_FileHandle *fh;
               DWORD dwBytes;
-              h = *(HANDLE *) GNUNET_CONTAINER_slist_get (i, NULL);
-              if (!PeekNamedPipe (h, NULL, 0, NULL, &dwBytes, NULL))
 
+              fh = (struct GNUNET_DISK_FileHandle *) 
GNUNET_CONTAINER_slist_get (i, NULL);
+              if (fh->type == GNUNET_PIPE)
                 {
-                  GNUNET_CONTAINER_slist_add (handles_except,
-                                              
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
-                                              &h, sizeof (HANDLE));
-                  retcode++;
+                  if (!PeekNamedPipe (fh->h, NULL, 0, NULL, &dwBytes, NULL))
+
+                    {
+                      GNUNET_CONTAINER_slist_add (handles_except,
+                                                  
GNUNET_CONTAINER_SLIST_DISPOSITION_TRANSIENT,
+                                                  fh, sizeof (struct 
GNUNET_DISK_FileHandle));
+                      retcode++;
+                    }
                 }
             }
           GNUNET_CONTAINER_slist_iter_destroy (i);




reply via email to

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