[PATCH libgpg-error 3/4] estream: Track the kind of backend used.

Justus Winter justus at g10code.com
Wed Oct 19 12:20:43 CEST 2016


* src/estream.c (struct _gpgrt_stream_internal): Add 'kind'.
(init_stream_obj): New parameter 'kind', initialize field.
(es_create): New parameter 'kind'.  Update all callers.
* src/gpgrt-int.h (gpgrt_stream_backend_kind_t): New type.

Signed-off-by: Justus Winter <justus at g10code.com>
---
 src/estream.c   | 39 ++++++++++++++++++++++-----------------
 src/gpgrt-int.h | 11 +++++++++++
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/src/estream.c b/src/estream.c
index 1cc4001..7ef687a 100644
--- a/src/estream.c
+++ b/src/estream.c
@@ -202,6 +202,7 @@ struct _gpgrt_stream_internal
 
   gpgrt_lock_t lock;		 /* Lock.  Used by *_stream_lock(). */
 
+  gpgrt_stream_backend_kind_t kind;
   void *cookie;			 /* Cookie.                */
   void *opaque;			 /* Opaque data.           */
   unsigned int modeflags;	 /* Flags for the backend. */
@@ -1991,9 +1992,11 @@ es_empty (estream_t stream)
 static void
 init_stream_obj (estream_t stream,
                  void *cookie, es_syshd_t *syshd,
+                 gpgrt_stream_backend_kind_t kind,
                  struct cookie_io_functions_s functions,
                  unsigned int modeflags, unsigned int xmode)
 {
+  stream->intern->kind = kind;
   stream->intern->cookie = cookie;
   stream->intern->opaque = NULL;
   stream->intern->offset = 0;
@@ -2076,6 +2079,7 @@ es_deinitialize (estream_t stream)
  */
 static int
 es_create (estream_t *stream, void *cookie, es_syshd_t *syshd,
+           gpgrt_stream_backend_kind_t kind,
 	   struct cookie_io_functions_s functions, unsigned int modeflags,
            unsigned int xmode, int with_locked_list)
 {
@@ -2106,7 +2110,8 @@ es_create (estream_t *stream, void *cookie, es_syshd_t *syshd,
   stream_new->unread_buffer_size = sizeof (stream_internal_new->unread_buffer);
   stream_new->intern = stream_internal_new;
 
-  init_stream_obj (stream_new, cookie, syshd, functions, modeflags, xmode);
+  init_stream_obj (stream_new, cookie, syshd, kind, functions, modeflags,
+                   xmode);
   init_stream_lock (stream_new);
 
   err = do_list_add (stream_new, with_locked_list);
@@ -2813,7 +2818,7 @@ doreadline (estream_t _GPGRT__RESTRICT stream, size_t max_length,
     goto out;
 
   memset (&syshd, 0, sizeof syshd);
-  err = es_create (&line_stream, line_stream_cookie, &syshd,
+  err = es_create (&line_stream, line_stream_cookie, &syshd, BACKEND_MEM,
 		   estream_functions_mem, O_RDWR, 1, 0);
   if (err)
     goto out;
@@ -3064,8 +3069,8 @@ _gpgrt_fopen (const char *_GPGRT__RESTRICT path,
   syshd.type = ES_SYSHD_FD;
   syshd.u.fd = fd;
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd, estream_functions_fd, modeflags,
-                   xmode, 0);
+  err = es_create (&stream, cookie, &syshd, BACKEND_FD,
+                   estream_functions_fd, modeflags, xmode, 0);
   if (err)
     goto out;
 
@@ -3121,7 +3126,7 @@ _gpgrt_mopen (void *_GPGRT__RESTRICT data, size_t data_n, size_t data_len,
 
   memset (&syshd, 0, sizeof syshd);
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd,
+  err = es_create (&stream, cookie, &syshd, BACKEND_MEM,
                    estream_functions_mem, modeflags, xmode, 0);
 
  out:
@@ -3155,8 +3160,8 @@ _gpgrt_fopenmem (size_t memlimit, const char *_GPGRT__RESTRICT mode)
     return NULL;
 
   memset (&syshd, 0, sizeof syshd);
-  if (es_create (&stream, cookie, &syshd, estream_functions_mem, modeflags,
-                 xmode, 0))
+  if (es_create (&stream, cookie, &syshd, BACKEND_MEM,
+                 estream_functions_mem, modeflags, xmode, 0))
     (*estream_functions_mem.public.func_close) (cookie);
 
   return stream;
@@ -3217,8 +3222,8 @@ _gpgrt_fopencookie (void *_GPGRT__RESTRICT cookie,
     goto out;
 
   memset (&syshd, 0, sizeof syshd);
-  err = es_create (&stream, cookie, &syshd, io_functions, modeflags,
-                   xmode, 0);
+  err = es_create (&stream, cookie, &syshd, BACKEND_USER, io_functions,
+                   modeflags, xmode, 0);
   if (err)
     goto out;
 
@@ -3260,7 +3265,7 @@ do_fdopen (int filedes, const char *mode, int no_close, int with_locked_list)
   syshd.type = ES_SYSHD_FD;
   syshd.u.fd = filedes;
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd, estream_functions_fd,
+  err = es_create (&stream, cookie, &syshd, BACKEND_FD, estream_functions_fd,
                    modeflags, xmode, with_locked_list);
 
   if (!err && stream)
@@ -3326,7 +3331,7 @@ do_fpopen (FILE *fp, const char *mode, int no_close, int with_locked_list)
   syshd.type = ES_SYSHD_FD;
   syshd.u.fd = fp? fileno (fp): -1;
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd, estream_functions_fp,
+  err = es_create (&stream, cookie, &syshd, BACKEND_FP, estream_functions_fp,
                    modeflags, xmode, with_locked_list);
 
  out:
@@ -3386,8 +3391,8 @@ do_w32open (HANDLE hd, const char *mode,
   syshd.type = ES_SYSHD_HANDLE;
   syshd.u.handle = hd;
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd, estream_functions_w32,
-                   modeflags, xmode, with_locked_list);
+  err = es_create (&stream, cookie, &syshd, BACKEND_W32,
+                   estream_functions_w32, modeflags, xmode, with_locked_list);
 
  leave:
   if (err && create_called)
@@ -3565,8 +3570,8 @@ _gpgrt_freopen (const char *_GPGRT__RESTRICT path,
       syshd.type = ES_SYSHD_FD;
       syshd.u.fd = fd;
       create_called = 1;
-      init_stream_obj (stream, cookie, &syshd, estream_functions_fd,
-                       modeflags, xmode);
+      init_stream_obj (stream, cookie, &syshd, BACKEND_FD,
+                       estream_functions_fd, modeflags, xmode);
 
     leave:
 
@@ -4559,8 +4564,8 @@ _gpgrt_tmpfile (void)
   syshd.type = ES_SYSHD_FD;
   syshd.u.fd = fd;
   create_called = 1;
-  err = es_create (&stream, cookie, &syshd, estream_functions_fd, modeflags,
-                   0, 0);
+  err = es_create (&stream, cookie, &syshd, BACKEND_FD, estream_functions_fd,
+                   modeflags, 0, 0);
 
  out:
   if (err)
diff --git a/src/gpgrt-int.h b/src/gpgrt-int.h
index f74dc5d..2b13350 100644
--- a/src/gpgrt-int.h
+++ b/src/gpgrt-int.h
@@ -69,6 +69,17 @@ struct cookie_io_functions_s
   cookie_ioctl_function_t func_ioctl;
 };
 
+typedef enum
+  {
+    BACKEND_MEM,
+    BACKEND_FD,
+    BACKEND_W32,
+    BACKEND_FP,
+    BACKEND_USER,
+    BACKEND_W32_POLLABLE,
+  } gpgrt_stream_backend_kind_t;
+
+
 
 /* Local prototypes for estream.  */
 int _gpgrt_es_init (void);
-- 
2.9.3




More information about the Gnupg-devel mailing list