[Pkg-gnupg-commit] [gnupg2] 17/205: g13: Re-factor high level create code.

Daniel Kahn Gillmor dkg at fifthhorseman.net
Wed May 11 08:38:09 UTC 2016


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

dkg pushed a commit to branch experimental
in repository gnupg2.

commit dc1dbc43a6bfb2f3e6a1cc2ca089e0318b3af0ed
Author: Werner Koch <wk at gnupg.org>
Date:   Tue Feb 2 09:03:37 2016 +0100

    g13: Re-factor high level create code.
    
    * g13/create.c (g13_create_container): Factor some code out to ...
    * g13/backend.c (be_take_lock_for_create): new.
    
    Signed-off-by: Werner Koch <wk at gnupg.org>
---
 g13/backend.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 g13/backend.h |  2 ++
 g13/create.c  | 31 +++----------------------------
 3 files changed, 64 insertions(+), 28 deletions(-)

diff --git a/g13/backend.c b/g13/backend.c
index 52e1e0a..b35887b 100644
--- a/g13/backend.c
+++ b/g13/backend.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "g13.h"
 #include "i18n.h"
@@ -88,6 +89,64 @@ be_is_supported_conttype (int conttype)
 }
 
 
+/* Create a lock file for the container FNAME and store the lock at
+ * R_LOCK and return 0.  On error return an error code and store NULL
+ * at R_LOCK.  */
+gpg_error_t
+be_take_lock_for_create (ctrl_t ctrl, const char *fname, dotlock_t *r_lock)
+{
+  gpg_error_t err;
+  dotlock_t lock = NULL;
+  struct stat sb;
+
+  *r_lock = NULL;
+
+  /* A DM-crypt container requires special treatment by using the
+     syshelper fucntions.  */
+  if (ctrl->conttype == CONTTYPE_DM_CRYPT)
+    {
+      /*  */
+      err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
+      goto leave;
+    }
+
+
+  /* A quick check to see that no container with that name already
+     exists.  */
+  if (!access (fname, F_OK))
+    {
+      err = gpg_error (GPG_ERR_EEXIST);
+      goto leave;
+    }
+
+  /* Take a lock and proceed with the creation.  If there is a lock we
+     immediately return an error because for creation it does not make
+     sense to wait.  */
+  lock = dotlock_create (fname, 0);
+  if (!lock)
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+  if (dotlock_take (lock, 0))
+    {
+      err = gpg_error_from_syserror ();
+      goto leave;
+    }
+
+  /* Check again that the file does not exist.  */
+  err = stat (fname, &sb)? 0 : gpg_error (GPG_ERR_EEXIST);
+
+ leave:
+  if (!err)
+    {
+      *r_lock = lock;
+      lock = NULL;
+    }
+  dotlock_destroy (lock);
+  return err;
+}
+
 
 /* If the backend requires a separate file or directory for the
    container, return its name by computing it from FNAME which gives
diff --git a/g13/backend.h b/g13/backend.h
index e570fc5..be81a04 100644
--- a/g13/backend.h
+++ b/g13/backend.h
@@ -25,6 +25,8 @@
 
 int be_parse_conttype_name (const char *name);
 int be_is_supported_conttype (int conttype);
+gpg_error_t be_take_lock_for_create (ctrl_t ctrl, const char *fname,
+                                     dotlock_t *r_lock);
 gpg_error_t be_get_detached_name (int conttype, const char *fname,
                                   char **r_name, int *r_isdir);
 gpg_error_t be_create_new_keys (int conttype, membuf_t *mb);
diff --git a/g13/create.c b/g13/create.c
index 91b290c..3dac903 100644
--- a/g13/create.c
+++ b/g13/create.c
@@ -239,35 +239,10 @@ g13_create_container (ctrl_t ctrl, const char *filename, strlist_t keys)
   if (!keys)
     return gpg_error (GPG_ERR_NO_PUBKEY);
 
-  /* A quick check to see that no container with that name already
-     exists.  */
-  if (!access (filename, F_OK))
-    return gpg_error (GPG_ERR_EEXIST);
-
-  /* Take a lock and proceed with the creation.  If there is a lock we
-     immediately return an error because for creation it does not make
-     sense to wait.  */
-  lock = dotlock_create (filename, 0);
-  if (!lock)
-    return gpg_error_from_syserror ();
-  if (dotlock_take (lock, 0))
-    {
-      err = gpg_error_from_syserror ();
-      goto leave;
-    }
-  else
-    err = 0;
-
-  /* Check again that the file does not exist.  */
-  {
-      struct stat sb;
+  err = be_take_lock_for_create (ctrl, filename, &lock);
+  if (err)
+    goto leave;
 
-      if (!stat (filename, &sb))
-        {
-          err = gpg_error (GPG_ERR_EEXIST);
-          goto leave;
-        }
-  }
   /* And a possible detached file or directory may not exist either.  */
   err = be_get_detached_name (ctrl->conttype, filename,
                               &detachedname, &detachedisdir);

-- 
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