aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-util.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2016-01-14 15:29:45 +0000
committerWerner Koch <[email protected]>2016-01-14 15:47:42 +0000
commitf5cceef115f0307664956d01c48b1b397fdad4b3 (patch)
treed0a46e2336192d942c03576341d650b421c5057d /kbx/keybox-util.c
parentgpg: Make --list-options show-usage the default. (diff)
downloadgnupg-f5cceef115f0307664956d01c48b1b397fdad4b3.tar.gz
gnupg-f5cceef115f0307664956d01c48b1b397fdad4b3.zip
kbx: Add function keybox_tmp_names to avoid code duplication.
* kbx/keybox-update.c (create_tmp_file): Move some code to... * kbx/keybox-util.c (keybox_tmp_names): new. * g10/keyring.c: Include keybox.h. (create_tmp_file): Replace parts by keybox_tmp_names. -- Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'kbx/keybox-util.c')
-rw-r--r--kbx/keybox-util.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/kbx/keybox-util.c b/kbx/keybox-util.c
index 9fe9290ac..f7efd1a29 100644
--- a/kbx/keybox-util.c
+++ b/kbx/keybox-util.c
@@ -68,3 +68,76 @@ _keybox_free (void *p)
if (p)
free_func (p);
}
+
+
+/* Store the two malloced temporary file names used for keybox updates
+ of file FILENAME at R_BAKNAME and R_TMPNAME. On error an error
+ code is returned and NULL stored at R_BAKNAME and R_TMPNAME. If
+ FOR_KEYRING is true the returned names match those used by GnuPG's
+ keyring code. */
+gpg_error_t
+keybox_tmp_names (const char *filename, int for_keyring,
+ char **r_bakname, char **r_tmpname)
+{
+ gpg_error_t err;
+ char *bak_name, *tmp_name;
+
+ *r_bakname = NULL;
+ *r_tmpname = NULL;
+
+# ifdef USE_ONLY_8DOT3
+ /* Here is another Windoze bug?:
+ * you can't rename("pubring.kbx.tmp", "pubring.kbx");
+ * but rename("pubring.kbx.tmp", "pubring.aaa");
+ * works. So we replace ".kbx" by ".kb_" or ".k__". Note that we
+ * can't use ".bak" and ".tmp", because these suffixes are used by
+ * gpg's keyrings and would lead to a sharing violation or data
+ * corruption. If the name does not end in ".kbx" we assume working
+ * on a modern file system and append the suffix. */
+ {
+ const char *ext = for_keyring? EXTSEP_S GPGEXT_GPG : EXTSEP_S "kbx";
+ const char *b_ext = for_keyring? EXTSEP_S "bak" : EXTSEP_S "kb_";
+ const char *t_ext = for_keyring? EXTSEP_S "tmp" : EXTSEP_S "k__";
+ int repl;
+
+ if (strlen (ext) != 4 || strlen (b_ext) != 4)
+ BUG ();
+ repl = (strlen (filename) > 4
+ && !strcmp (filename + strlen (filename) - 4, ext));
+ bak_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
+ if (!bak_name)
+ return gpg_error_from_syserror ();
+ strcpy (bak_name, filename);
+ strcpy (bak_name + strlen (filename) - (repl?4:0), b_ext);
+
+ tmp_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
+ if (!tmp_name)
+ {
+ err = gpg_error_from_syserror ();
+ xfree (bak_name);
+ return err;
+ }
+ strcpy (tmp_name, filename);
+ strcpy (tmp_name + strlen (filename) - (repl?4:0), t_ext);
+ }
+# else /* Posix file names */
+ (void)for_keyring;
+ bak_name = xtrymalloc (strlen (filename) + 2);
+ if (!bak_name)
+ return gpg_error_from_syserror ();
+ strcpy (stpcpy (bak_name, filename), "~");
+
+ tmp_name = xtrymalloc (strlen (filename) + 5);
+ if (!tmp_name)
+ {
+ err = gpg_error_from_syserror ();
+ xfree (bak_name);
+ return err;
+ }
+ strcpy (stpcpy (tmp_name,filename), EXTSEP_S "tmp");
+# endif /* Posix filename */
+
+ *r_bakname = bak_name;
+ *r_tmpname = tmp_name;
+ return 0;
+}