1 /* keybox-util.c - Utility functions for Keybox
2 * Copyright (C) 2001 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
24 #ifdef HAVE_DOSISH_SYSTEM
25 # define WIN32_LEAN_AND_MEAN /* We only need the OS core stuff. */
29 #include "keybox-defs.h"
32 /* Store the two malloced temporary file names used for keybox updates
33 of file FILENAME at R_BAKNAME and R_TMPNAME. On error an error
34 code is returned and NULL stored at R_BAKNAME and R_TMPNAME. If
35 FOR_KEYRING is true the returned names match those used by GnuPG's
38 keybox_tmp_names (const char *filename, int for_keyring,
39 char **r_bakname, char **r_tmpname)
42 char *bak_name, *tmp_name;
47 # ifdef USE_ONLY_8DOT3
48 /* Here is another Windoze bug?:
49 * you can't rename("pubring.kbx.tmp", "pubring.kbx");
50 * but rename("pubring.kbx.tmp", "pubring.aaa");
51 * works. So we replace ".kbx" by ".kb_" or ".k__". Note that we
52 * can't use ".bak" and ".tmp", because these suffixes are used by
53 * gpg's keyrings and would lead to a sharing violation or data
54 * corruption. If the name does not end in ".kbx" we assume working
55 * on a modern file system and append the suffix. */
57 const char *ext = for_keyring? EXTSEP_S GPGEXT_GPG : EXTSEP_S "kbx";
58 const char *b_ext = for_keyring? EXTSEP_S "bak" : EXTSEP_S "kb_";
59 const char *t_ext = for_keyring? EXTSEP_S "tmp" : EXTSEP_S "k__";
62 if (strlen (ext) != 4 || strlen (b_ext) != 4)
64 repl = (strlen (filename) > 4
65 && !strcmp (filename + strlen (filename) - 4, ext));
66 bak_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
68 return gpg_error_from_syserror ();
69 strcpy (bak_name, filename);
70 strcpy (bak_name + strlen (filename) - (repl?4:0), b_ext);
72 tmp_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
75 err = gpg_error_from_syserror ();
79 strcpy (tmp_name, filename);
80 strcpy (tmp_name + strlen (filename) - (repl?4:0), t_ext);
82 # else /* Posix file names */
84 bak_name = xtrymalloc (strlen (filename) + 2);
86 return gpg_error_from_syserror ();
87 strcpy (stpcpy (bak_name, filename), "~");
89 tmp_name = xtrymalloc (strlen (filename) + 5);
92 err = gpg_error_from_syserror ();
96 strcpy (stpcpy (tmp_name,filename), EXTSEP_S "tmp");
97 # endif /* Posix filename */
99 *r_bakname = bak_name;
100 *r_tmpname = tmp_name;