[Pkg-gnupg-commit] [gnupg2] 57/292: gpg: Make import filter data object more flexible.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Mon Nov 21 06:31:25 UTC 2016


This is an automated email from the git hooks/post-receive script.

dkg pushed a commit to branch master
in repository gnupg2.

commit c9237bf2ba2c49588576dcece756ebf5fe89aada
Author: Werner Koch <wk at gnupg.org>
Date:   Wed Sep 28 13:39:09 2016 +0200

    gpg: Make import filter data object more flexible.
    
    * g10/main.h (import_filter_t): New.
    * g10/import.c (struct import_filter_s): Declare struct.
    (import_keep_uid, import_drop_sig): Replace by ...
    (import_filter): new.  Adjust all users.
    (cleanup_import_globals): Move code to ...
    (release_import_filter): new.
    (save_and_clear_import_filter): New.
    (restore_import_filter): New.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>
---
 g10/import.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++-------------
 g10/main.h   |  6 +++++
 2 files changed, 63 insertions(+), 15 deletions(-)

diff --git a/g10/import.c b/g10/import.c
index 6a0dcde..25ce74e 100644
--- a/g10/import.c
+++ b/g10/import.c
@@ -76,16 +76,22 @@ struct import_stats_s
 #define NODE_FLAG_A  8
 
 
-/* Global variables to store selector created from
+/* A an object and a global instance to store selectors created from
  * --import-filter keep-uid=EXPR.
  * --import-filter drop-sig=EXPR.
  *
  * FIXME: We should put this into the CTRL object but that requires a
- * lot more changes right now.
+ * lot more changes right now.  For now we use save and restore
+ * fucntion to temporary change them.
  */
-static recsel_expr_t import_keep_uid;
-static recsel_expr_t import_drop_sig;
-
+/* Definition of the import filters.  */
+struct import_filter_s
+{
+  recsel_expr_t keep_uid;
+  recsel_expr_t drop_sig;
+};
+/* The current instance.  */
+struct import_filter_s import_filter;
 
 
 static int import (ctrl_t ctrl,
@@ -120,12 +126,18 @@ static int merge_keysigs (kbnode_t dst, kbnode_t src, int *n_sigs);
 
 

 static void
+release_import_filter (import_filter_t filt)
+{
+  recsel_release (filt->keep_uid);
+  filt->keep_uid = NULL;
+  recsel_release (filt->drop_sig);
+  filt->drop_sig = NULL;
+}
+
+static void
 cleanup_import_globals (void)
 {
-  recsel_release (import_keep_uid);
-  import_keep_uid = NULL;
-  recsel_release (import_drop_sig);
-  import_drop_sig = NULL;
+  release_import_filter (&import_filter);
 }
 
 
@@ -201,9 +213,9 @@ parse_and_set_import_filter (const char *string)
   register_mem_cleanup_func (cleanup_import_globals);
 
   if (!strncmp (string, "keep-uid=", 9))
-    err = recsel_parse_expr (&import_keep_uid, string+9);
+    err = recsel_parse_expr (&import_filter.keep_uid, string+9);
   else if (!strncmp (string, "drop-sig=", 9))
-    err = recsel_parse_expr (&import_drop_sig, string+9);
+    err = recsel_parse_expr (&import_filter.drop_sig, string+9);
   else
     err = gpg_error (GPG_ERR_INV_NAME);
 
@@ -211,6 +223,36 @@ parse_and_set_import_filter (const char *string)
 }
 
 
+/* Save the current import filters, return them, and clear the current
+ * filters.  Returns NULL on error and sets ERRNO.  */
+import_filter_t
+save_and_clear_import_filter (void)
+{
+  import_filter_t filt;
+
+  filt = xtrycalloc (1, sizeof *filt);
+  if (!filt)
+    return NULL;
+  *filt = import_filter;
+  memset (&import_filter, 0, sizeof import_filter);
+
+  return filt;
+}
+
+
+/* Release the current import filters and restore them from NEWFILT.
+ * Ownership of NEWFILT is moved to this function.  */
+void
+restore_import_filter (import_filter_t filt)
+{
+  if (filt)
+    {
+      release_import_filter (&import_filter);
+      import_filter = *filt;
+      xfree (filt);
+    }
+}
+
 
 import_stats_t
 import_new_stats_handle (void)
@@ -1409,14 +1451,14 @@ import_one (ctrl_t ctrl,
   commit_kbnode (&keyblock);
 
   /* Apply import filter.  */
-  if (import_keep_uid)
+  if (import_filter.keep_uid)
     {
-      apply_keep_uid_filter (keyblock, import_keep_uid);
+      apply_keep_uid_filter (keyblock, import_filter.keep_uid);
       commit_kbnode (&keyblock);
     }
-  if (import_drop_sig)
+  if (import_filter.drop_sig)
     {
-      apply_drop_sig_filter (keyblock, import_drop_sig);
+      apply_drop_sig_filter (keyblock, import_filter.drop_sig);
       commit_kbnode (&keyblock);
     }
 
diff --git a/g10/main.h b/g10/main.h
index b1563d2..c2c92d0 100644
--- a/g10/main.h
+++ b/g10/main.h
@@ -348,10 +348,16 @@ gcry_mpi_t encode_md_value (PKT_public_key *pk,
 /*-- import.c --*/
 struct import_stats_s;
 typedef struct import_stats_s *import_stats_t;
+struct import_filter_s;
+typedef struct import_filter_s *import_filter_t;
 typedef gpg_error_t (*import_screener_t)(kbnode_t keyblock, void *arg);
 
 int parse_import_options(char *str,unsigned int *options,int noisy);
+
 gpg_error_t parse_and_set_import_filter (const char *string);
+import_filter_t save_and_clear_import_filter (void);
+void            restore_import_filter (import_filter_t filt);
+
 gpg_error_t read_key_from_file (ctrl_t ctrl, const char *fname,
                                 kbnode_t *r_keyblock);
 void import_keys (ctrl_t ctrl, char **fnames, int nnames,

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-gnupg/gnupg2.git



More information about the Pkg-gnupg-commit mailing list