diff options
author | Werner Koch <[email protected]> | 2020-11-04 14:36:52 +0000 |
---|---|---|
committer | Werner Koch <[email protected]> | 2020-11-04 15:25:57 +0000 |
commit | 6fe5c8c06e8cd162913ee5b0eb741eb4beebf44a (patch) | |
tree | 53234f85813be5e23e7cad58b0284f05f52e4591 | |
parent | w32: Add another pinentry search path (diff) | |
download | gnupg-6fe5c8c06e8cd162913ee5b0eb741eb4beebf44a.tar.gz gnupg-6fe5c8c06e8cd162913ee5b0eb741eb4beebf44a.zip |
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 <[email protected]>
-rw-r--r-- | common/homedir.c | 33 | ||||
-rw-r--r-- | common/util.h | 1 | ||||
-rw-r--r-- | g10/openfile.c | 30 | ||||
-rw-r--r-- | po/POTFILES.in | 1 | ||||
-rw-r--r-- | sm/keydb.c | 25 |
5 files changed, 42 insertions, 48 deletions
diff --git a/common/homedir.c b/common/homedir.c index d64b74427..85e09c46a 100644 --- a/common/homedir.c +++ b/common/homedir.c @@ -61,6 +61,7 @@ #include "util.h" #include "sysutils.h" +#include "i18n.h" #include "zb32.h" /* 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 * gnupg-set-homedir call. This is always an absolute directory name. * The function replaces the former global var opt.homedir. */ diff --git a/common/util.h b/common/util.h index fd8a7dc81..70a0f4f56 100644 --- a/common/util.h +++ b/common/util.h @@ -262,6 +262,7 @@ const char *openpgp_is_curve_supported (const char *name, const char *standard_homedir (void); const char *default_homedir (void); void gnupg_set_homedir (const char *newdir); +void gnupg_maybe_make_homedir (const char *fname, int quiet); const char *gnupg_homedir (void); int gnupg_default_homedir_p (void); const char *gnupg_daemon_rootdir (void); diff --git a/g10/openfile.c b/g10/openfile.c index 86ed1dc79..d82858f5a 100644 --- a/g10/openfile.c +++ b/g10/openfile.c @@ -367,36 +367,18 @@ open_sigfile (const char *sigfilename, progress_filter_context_t *pfx) } +/* 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. */ void 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 ) return; - 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 (!opt.quiet ) - log_info ( _("directory '%s' created\n"), fname ); - } + gnupg_maybe_make_homedir (fname, opt.quiet); } diff --git a/po/POTFILES.in b/po/POTFILES.in index 70b5d45d8..a281946e0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -30,6 +30,7 @@ common/logging.c common/utf8conv.c common/dotlock.c common/init.c +common/homedir.c g10/armor.c g10/build-packet.c diff --git a/sm/keydb.c b/sm/keydb.c index 976e1674c..8c0537a9f 100644 --- a/sm/keydb.c +++ b/sm/keydb.c @@ -75,33 +75,10 @@ static void unlock_all (KEYDB_HANDLE hd); static void 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 ) return; - 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_info (_("can't create directory '%s': %s\n"), - fname, strerror(errno) ); - else if (!opt.quiet ) - log_info (_("directory '%s' created\n"), fname); - } + gnupg_maybe_make_homedir (fname, opt.quiet); } |