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"
30 #include "utilproto.h"
33 static void *(*alloc_func)(size_t n) = malloc;
34 static void *(*realloc_func)(void *p, size_t n) = realloc;
35 static void (*free_func)(void*) = free;
40 keybox_set_malloc_hooks ( void *(*new_alloc_func)(size_t n),
41 void *(*new_realloc_func)(void *p, size_t n),
42 void (*new_free_func)(void*) )
44 alloc_func = new_alloc_func;
45 realloc_func = new_realloc_func;
46 free_func = new_free_func;
50 _keybox_malloc (size_t n)
52 return alloc_func (n);
56 _keybox_realloc (void *a, size_t n)
58 return realloc_func (a, n);
62 _keybox_calloc (size_t n, size_t m)
64 void *p = _keybox_malloc (n*m);
71 _keybox_free (void *p)
78 /* Store the two malloced temporary file names used for keybox updates
79 of file FILENAME at R_BAKNAME and R_TMPNAME. On error an error
80 code is returned and NULL stored at R_BAKNAME and R_TMPNAME. If
81 FOR_KEYRING is true the returned names match those used by GnuPG's
84 keybox_tmp_names (const char *filename, int for_keyring,
85 char **r_bakname, char **r_tmpname)
88 char *bak_name, *tmp_name;
93 # ifdef USE_ONLY_8DOT3
94 /* Here is another Windoze bug?:
95 * you can't rename("pubring.kbx.tmp", "pubring.kbx");
96 * but rename("pubring.kbx.tmp", "pubring.aaa");
97 * works. So we replace ".kbx" by ".kb_" or ".k__". Note that we
98 * can't use ".bak" and ".tmp", because these suffixes are used by
99 * gpg's keyrings and would lead to a sharing violation or data
100 * corruption. If the name does not end in ".kbx" we assume working
101 * on a modern file system and append the suffix. */
103 const char *ext = for_keyring? EXTSEP_S GPGEXT_GPG : EXTSEP_S "kbx";
104 const char *b_ext = for_keyring? EXTSEP_S "bak" : EXTSEP_S "kb_";
105 const char *t_ext = for_keyring? EXTSEP_S "tmp" : EXTSEP_S "k__";
108 if (strlen (ext) != 4 || strlen (b_ext) != 4)
110 repl = (strlen (filename) > 4
111 && !strcmp (filename + strlen (filename) - 4, ext));
112 bak_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
114 return gpg_error_from_syserror ();
115 strcpy (bak_name, filename);
116 strcpy (bak_name + strlen (filename) - (repl?4:0), b_ext);
118 tmp_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
121 err = gpg_error_from_syserror ();
125 strcpy (tmp_name, filename);
126 strcpy (tmp_name + strlen (filename) - (repl?4:0), t_ext);
128 # else /* Posix file names */
130 bak_name = xtrymalloc (strlen (filename) + 2);
132 return gpg_error_from_syserror ();
133 strcpy (stpcpy (bak_name, filename), "~");
135 tmp_name = xtrymalloc (strlen (filename) + 5);
138 err = gpg_error_from_syserror ();
142 strcpy (stpcpy (tmp_name,filename), EXTSEP_S "tmp");
143 # endif /* Posix filename */
145 *r_bakname = bak_name;
146 *r_tmpname = tmp_name;
151 keybox_file_rename (const char *oldname, const char *newname,
154 return gnupg_rename_file (oldname, newname, block_signals);