LCOV - code coverage report
Current view: top level - kbx - keybox-util.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 36 83.3 %
Date: 2016-11-29 15:00:56 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* keybox-util.c - Utility functions for Keybox
       2             :  *      Copyright (C) 2001 Free Software Foundation, Inc.
       3             :  *
       4             :  * This file is part of GnuPG.
       5             :  *
       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.
      10             :  *
      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.
      15             :  *
      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/>.
      18             :  */
      19             : 
      20             : #include <config.h>
      21             : #include <stdlib.h>
      22             : #include <stdio.h>
      23             : #include <string.h>
      24             : #ifdef  HAVE_DOSISH_SYSTEM
      25             : # define WIN32_LEAN_AND_MEAN  /* We only need the OS core stuff.  */
      26             : # include <windows.h>
      27             : #endif
      28             : 
      29             : #include "keybox-defs.h"
      30             : #include "utilproto.h"
      31             : 
      32             : 
      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;
      36             : 
      37             : 
      38             : 
      39             : void
      40           3 : 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*) )
      43             : {
      44           3 :   alloc_func        = new_alloc_func;
      45           3 :   realloc_func      = new_realloc_func;
      46           3 :   free_func         = new_free_func;
      47           3 : }
      48             : 
      49             : void *
      50      210566 : _keybox_malloc (size_t n)
      51             : {
      52      210566 :   return alloc_func (n);
      53             : }
      54             : 
      55             : void *
      56         128 : _keybox_realloc (void *a, size_t n)
      57             : {
      58         128 :   return realloc_func (a, n);
      59             : }
      60             : 
      61             : void *
      62      105836 : _keybox_calloc (size_t n, size_t m)
      63             : {
      64      105836 :   void *p = _keybox_malloc (n*m);
      65      105836 :   if (p)
      66      105836 :     memset (p, 0, n* m);
      67      105836 :   return p;
      68             : }
      69             : 
      70             : void
      71      611523 : _keybox_free (void *p)
      72             : {
      73      611523 :   if (p)
      74      205207 :     free_func (p);
      75      611523 : }
      76             : 
      77             : 
      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
      82             :    keyring code.  */
      83             : gpg_error_t
      84         177 : keybox_tmp_names (const char *filename, int for_keyring,
      85             :                   char **r_bakname, char **r_tmpname)
      86             : {
      87             :   gpg_error_t err;
      88             :   char *bak_name, *tmp_name;
      89             : 
      90         177 :   *r_bakname = NULL;
      91         177 :   *r_tmpname = NULL;
      92             : 
      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.  */
     102             :   {
     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__";
     106             :     int repl;
     107             : 
     108             :     if (strlen (ext) != 4 || strlen (b_ext) != 4)
     109             :       BUG ();
     110             :     repl = (strlen (filename) > 4
     111             :             && !strcmp (filename + strlen (filename) - 4, ext));
     112             :     bak_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
     113             :     if (!bak_name)
     114             :       return gpg_error_from_syserror ();
     115             :     strcpy (bak_name, filename);
     116             :     strcpy (bak_name + strlen (filename) - (repl?4:0), b_ext);
     117             : 
     118             :     tmp_name = xtrymalloc (strlen (filename) + (repl?0:4) + 1);
     119             :     if (!tmp_name)
     120             :       {
     121             :         err = gpg_error_from_syserror ();
     122             :         xfree (bak_name);
     123             :         return err;
     124             :       }
     125             :     strcpy (tmp_name, filename);
     126             :     strcpy (tmp_name + strlen (filename) - (repl?4:0), t_ext);
     127             :   }
     128             : # else /* Posix file names */
     129             :   (void)for_keyring;
     130         177 :   bak_name = xtrymalloc (strlen (filename) + 2);
     131         177 :   if (!bak_name)
     132           0 :     return gpg_error_from_syserror ();
     133         177 :   strcpy (stpcpy (bak_name, filename), "~");
     134             : 
     135         177 :   tmp_name = xtrymalloc (strlen (filename) + 5);
     136         177 :   if (!tmp_name)
     137             :     {
     138           0 :       err = gpg_error_from_syserror ();
     139           0 :       xfree (bak_name);
     140           0 :       return err;
     141             :     }
     142         177 :   strcpy (stpcpy (tmp_name,filename), EXTSEP_S "tmp");
     143             : # endif /* Posix filename */
     144             : 
     145         177 :   *r_bakname = bak_name;
     146         177 :   *r_tmpname = tmp_name;
     147         177 :   return 0;
     148             : }
     149             : 
     150             : gpg_error_t
     151           0 : keybox_file_rename (const char *oldname, const char *newname,
     152             :                     int *block_signals)
     153             : {
     154           0 :   return gnupg_rename_file (oldname, newname, block_signals);
     155             : }

Generated by: LCOV version 1.11