aboutsummaryrefslogtreecommitdiffstats
path: root/kbx/keybox-update.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-update.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-update.c')
-rw-r--r--kbx/keybox-update.c85
1 files changed, 12 insertions, 73 deletions
diff --git a/kbx/keybox-update.c b/kbx/keybox-update.c
index aa8086550..eebcfcad4 100644
--- a/kbx/keybox-update.c
+++ b/kbx/keybox-update.c
@@ -68,88 +68,27 @@ fseeko (FILE * stream, off_t newpos, int whence)
#endif /* !defined(HAVE_FSEEKO) && !defined(fseeko) */
-
static int
create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, FILE **r_fp)
{
- char *bakfname, *tmpfname;
-
- *r_bakfname = NULL;
- *r_tmpfname = 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 and would lead to a sharing violation or data corruption.
- */
- if (strlen (template) > 4
- && !strcmp (template+strlen(template)-4, EXTSEP_S "kbx") )
- {
- bakfname = xtrymalloc (strlen (template) + 1);
- if (!bakfname)
- return gpg_error_from_syserror ();
- strcpy (bakfname, template);
- strcpy (bakfname+strlen(template)-4, EXTSEP_S "kb_");
-
- tmpfname = xtrymalloc (strlen (template) + 1);
- if (!tmpfname)
- {
- gpg_error_t tmperr = gpg_error_from_syserror ();
- xfree (bakfname);
- return tmperr;
- }
- strcpy (tmpfname,template);
- strcpy (tmpfname + strlen (template)-4, EXTSEP_S "k__");
- }
- else
- { /* File does not end with kbx, thus we hope we are working on a
- modern file system and appending a suffix works. */
- bakfname = xtrymalloc ( strlen (template) + 5);
- if (!bakfname)
- return gpg_error_from_syserror ();
- strcpy (stpcpy (bakfname, template), EXTSEP_S "kb_");
+ gpg_error_t err;
- tmpfname = xtrymalloc ( strlen (template) + 5);
- if (!tmpfname)
+ err = keybox_tmp_names (template, 0, r_bakfname, r_tmpfname);
+ if (!err)
+ {
+ *r_fp = fopen (*r_tmpfname, "wb");
+ if (!*r_fp)
{
- gpg_error_t tmperr = gpg_error_from_syserror ();
- xfree (bakfname);
- return tmperr;
+ err = gpg_error_from_syserror ();
+ xfree (*r_tmpfname);
+ *r_tmpfname = NULL;
+ xfree (*r_bakfname);
+ *r_bakfname = NULL;
}
- strcpy (stpcpy (tmpfname, template), EXTSEP_S "k__");
- }
-# else /* Posix file names */
- bakfname = xtrymalloc (strlen (template) + 2);
- if (!bakfname)
- return gpg_error_from_syserror ();
- strcpy (stpcpy (bakfname,template),"~");
-
- tmpfname = xtrymalloc ( strlen (template) + 5);
- if (!tmpfname)
- {
- gpg_error_t tmperr = gpg_error_from_syserror ();
- xfree (bakfname);
- return tmperr;
}
- strcpy (stpcpy (tmpfname,template), EXTSEP_S "tmp");
-# endif /* Posix filename */
- *r_fp = fopen (tmpfname, "wb");
- if (!*r_fp)
- {
- gpg_error_t tmperr = gpg_error_from_syserror ();
- xfree (tmpfname);
- xfree (bakfname);
- return tmperr;
- }
-
- *r_bakfname = bakfname;
- *r_tmpfname = tmpfname;
- return 0;
+ return err;
}