1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-04-17 15:44:34 +02:00

common: Fix duplicate implementation of try_make_homedir.

* g10/openfile.c (try_make_homedir): Move core of the code to ...
* common/homedir.c (gnupg_maybe_make_homedir): new.
* sm/keydb.c (try_make_homedir): Implement using new function.

* common/homedir.c: Include i18n.h.
* po/POTFILES.in: Add common/homedir.c.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2020-11-04 15:36:52 +01:00
parent b4cb91d5fb
commit 6fe5c8c06e
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
5 changed files with 42 additions and 48 deletions

View File

@ -61,6 +61,7 @@
#include "util.h" #include "util.h"
#include "sysutils.h" #include "sysutils.h"
#include "i18n.h"
#include "zb32.h" #include "zb32.h"
/* The GnuPG homedir. This is only accessed by the functions /* The GnuPG homedir. This is only accessed by the functions
@ -489,6 +490,38 @@ gnupg_set_homedir (const char *newdir)
} }
/* Create the homedir directory only if the supplied directory name is
* the same as the default one. This way we avoid to create arbitrary
* directories when a non-default home directory is used. To cope
* with HOME, we do compare only the suffix if we see that the default
* homedir does start with a tilde. If the mkdir fails the function
* terminates the process. If QUIET is set not diagnostic is printed
* on homedir creation. */
void
gnupg_maybe_make_homedir (const char *fname, int quiet)
{
const char *defhome = standard_homedir ();
if (
#ifdef HAVE_W32_SYSTEM
( !compare_filenames (fname, defhome) )
#else
( *defhome == '~'
&& (strlen(fname) >= strlen (defhome+1)
&& !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
|| (*defhome != '~' && !compare_filenames( fname, defhome ) )
#endif
)
{
if (gnupg_mkdir (fname, "-rwx"))
log_fatal ( _("can't create directory '%s': %s\n"),
fname, strerror(errno) );
else if (!quiet )
log_info ( _("directory '%s' created\n"), fname );
}
}
/* Return the homedir. The returned string is valid until another /* Return the homedir. The returned string is valid until another
* gnupg-set-homedir call. This is always an absolute directory name. * gnupg-set-homedir call. This is always an absolute directory name.
* The function replaces the former global var opt.homedir. */ * The function replaces the former global var opt.homedir. */

View File

@ -262,6 +262,7 @@ const char *openpgp_is_curve_supported (const char *name,
const char *standard_homedir (void); const char *standard_homedir (void);
const char *default_homedir (void); const char *default_homedir (void);
void gnupg_set_homedir (const char *newdir); void gnupg_set_homedir (const char *newdir);
void gnupg_maybe_make_homedir (const char *fname, int quiet);
const char *gnupg_homedir (void); const char *gnupg_homedir (void);
int gnupg_default_homedir_p (void); int gnupg_default_homedir_p (void);
const char *gnupg_daemon_rootdir (void); const char *gnupg_daemon_rootdir (void);

View File

@ -367,36 +367,18 @@ open_sigfile (const char *sigfilename, progress_filter_context_t *pfx)
} }
void
try_make_homedir (const char *fname)
{
const char *defhome = standard_homedir ();
/* Create the directory only if the supplied directory name is the /* Create the directory only if the supplied directory name is the
same as the default one. This way we avoid to create arbitrary same as the default one. This way we avoid to create arbitrary
directories when a non-default home directory is used. To cope directories when a non-default home directory is used. To cope
with HOME, we do compare only the suffix if we see that the with HOME, we do compare only the suffix if we see that the default
default homedir does start with a tilde. */ homedir does start with a tilde. */
void
try_make_homedir (const char *fname)
{
if ( opt.dry_run || opt.no_homedir_creation ) if ( opt.dry_run || opt.no_homedir_creation )
return; return;
if ( gnupg_maybe_make_homedir (fname, opt.quiet);
#ifdef HAVE_W32_SYSTEM
( !compare_filenames (fname, defhome) )
#else
( *defhome == '~'
&& (strlen(fname) >= strlen (defhome+1)
&& !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
|| (*defhome != '~' && !compare_filenames( fname, defhome ) )
#endif
)
{
if (gnupg_mkdir (fname, "-rwx"))
log_fatal ( _("can't create directory '%s': %s\n"),
fname, strerror(errno) );
else if (!opt.quiet )
log_info ( _("directory '%s' created\n"), fname );
}
} }

View File

@ -30,6 +30,7 @@ common/logging.c
common/utf8conv.c common/utf8conv.c
common/dotlock.c common/dotlock.c
common/init.c common/init.c
common/homedir.c
g10/armor.c g10/armor.c
g10/build-packet.c g10/build-packet.c

View File

@ -75,33 +75,10 @@ static void unlock_all (KEYDB_HANDLE hd);
static void static void
try_make_homedir (const char *fname) try_make_homedir (const char *fname)
{ {
const char *defhome = standard_homedir ();
/* Create the directory only if the supplied directory name is the
same as the default one. This way we avoid to create arbitrary
directories when a non-default home directory is used. To cope
with HOME, we do compare only the suffix if we see that the
default homedir does start with a tilde. */
if ( opt.dry_run || opt.no_homedir_creation ) if ( opt.dry_run || opt.no_homedir_creation )
return; return;
if ( gnupg_maybe_make_homedir (fname, opt.quiet);
#ifdef HAVE_W32_SYSTEM
( !compare_filenames (fname, defhome) )
#else
( *defhome == '~'
&& (strlen(fname) >= strlen (defhome+1)
&& !strcmp(fname+strlen(fname)-strlen(defhome+1), defhome+1 ) ))
|| (*defhome != '~' && !compare_filenames( fname, defhome ) )
#endif
)
{
if (gnupg_mkdir (fname, "-rwx"))
log_info (_("can't create directory '%s': %s\n"),
fname, strerror(errno) );
else if (!opt.quiet )
log_info (_("directory '%s' created\n"), fname);
}
} }