]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Replace get_homedir() by get_confdir().
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 20 Mar 2024 16:24:28 +0000 (17:24 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 20 Mar 2024 23:56:34 +0000 (00:56 +0100)
Every single caller of get_homedir() uses the returned string to
create a path below ~/.paraslash, so we might as well return this path,
simplifying the callers.

afs.c
audioc.c
client.c
client_common.c
file_write.c
lsu.c
play.c
server.c
string.c
string.h
upgrade_db.c

diff --git a/afs.c b/afs.c
index 8bc7cd84bd407700b1e85727aabefea01316396d..b41d00b6b5b2e1cb290ab45af74a6cf46923d5a6 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -568,13 +568,9 @@ static void close_afs_tables(void)
 static void get_database_dir(void)
 {
        if (!database_dir) {
-               if (OPT_GIVEN(AFS_DATABASE_DIR))
-                       database_dir = para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR));
-               else {
-                       const char *home = get_homedir();
-                       database_dir = make_message(
-                               "%s/.paraslash/afs_database-0.7", home);
-               }
+               database_dir = OPT_GIVEN(AFS_DATABASE_DIR)?
+                       para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR)) :
+                       make_message("%s/afs_database-0.7", get_confdir());
        }
        PARA_INFO_LOG("afs_database dir %s\n", database_dir);
 }
index 16155d4c02a191e776a678f1ddd402935d7922b8..83b1930c732ef1fbecfefb6441672eee5fafad80 100644 (file)
--- a/audioc.c
+++ b/audioc.c
@@ -231,7 +231,6 @@ close:
 __noreturn static void interactive_session(void)
 {
        int ret;
-       char *history_file;
        struct sigaction act;
        struct i9e_client_info ici = {
                .fds = {0, 1, 2},
@@ -242,15 +241,9 @@ __noreturn static void interactive_session(void)
        };
 
        PARA_NOTICE_LOG("\n%s\n", version_text("audioc"));
-       if (OPT_GIVEN(HISTORY_FILE))
-               history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
-       else {
-               const char *home = get_homedir();
-               history_file = make_message("%s/.paraslash/audioc.history",
-                       home);
-       }
-       ici.history_file = history_file;
-
+       ici.history_file = OPT_GIVEN(HISTORY_FILE)?
+               para_strdup(OPT_STRING_VAL(HISTORY_FILE)) :
+               make_message("%s/audioc.history", get_confdir());
        act.sa_handler = i9e_signal_dispatch;
        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
@@ -267,7 +260,7 @@ __noreturn static void interactive_session(void)
        i9e_close();
        para_log = stderr_log;
 out:
-       free(history_file);
+       free(ici.history_file);
        free(socket_name);
        if (ret < 0)
                PARA_ERROR_LOG("%s\n", para_strerror(-ret));
index 916adfb0ca9172c397dea28fe91bb09819089b59..351259e31ca7186e258f4f8e9f93160771139a04 100644 (file)
--- a/client.c
+++ b/client.c
@@ -525,14 +525,9 @@ __noreturn static void interactive_session(void)
        };
 
        PARA_NOTICE_LOG("\n%s\n", version_text("client"));
-       if (CLIENT_OPT_GIVEN(HISTORY_FILE, ct->lpr))
-               ici.history_file = para_strdup(CLIENT_OPT_STRING_VAL(
-                       HISTORY_FILE, ct->lpr));
-       else {
-               const char *home = get_homedir();
-               ici.history_file = make_message("%s/.paraslash/client.history",
-                       home);
-       }
+       ici.history_file = CLIENT_OPT_GIVEN(HISTORY_FILE, ct->lpr)?
+               para_strdup(CLIENT_OPT_STRING_VAL(HISTORY_FILE, ct->lpr)) :
+               make_message("%s/client.history", get_confdir());
        act.sa_handler = i9e_signal_dispatch;
        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;
index e207684e61121b18333ce3a99b476b8f92511d6c..e67a938b4df8aa69c2536505ce86c5017ab469b2 100644 (file)
@@ -582,11 +582,11 @@ int client_parse_config(int argc, char *argv[], struct client_task **ct_ptr,
                kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
        else {
                struct stat statbuf;
-               const char *home = get_homedir();
-               kf = make_message("%s/.paraslash/key.%s", home, user);
+               const char *confdir = get_confdir();
+               kf = make_message("%s/key.%s", confdir, user);
                if (stat(kf, &statbuf) != 0) { /* assume file does not exist */
                        free(kf);
-                       kf = make_message("%s/.ssh/id_rsa", home);
+                       kf = make_message("%s/.ssh/id_rsa", confdir);
                }
        }
        PARA_INFO_LOG("user: %s\n", user);
index 5435db444c013c9bea8a4b00f204261d5303571d..0a5d6bbaf2505618edecde49063036d08a876894 100644 (file)
@@ -31,13 +31,8 @@ struct private_file_write_data {
  */
 __must_check __malloc static char *random_filename(void)
 {
-       char *result;
-       const char *home = get_homedir();
-
        srandom(clock_get_realtime(NULL)->tv_usec);
-       result = make_message("%s/.paraslash/%08ld", home,
-               para_random(99999999));
-       return result;
+       return make_message("%s/%08ld", get_confdir(), para_random(99999999));
 }
 
 static int prepare_output_file(struct writer_node *wn)
diff --git a/lsu.c b/lsu.c
index 3cd6ba04e4f41a26cd8e235b0ff8899aea11ab9a..75114e68a5fa443ca675987b05bb9eb452ee9270 100644 (file)
--- a/lsu.c
+++ b/lsu.c
@@ -179,12 +179,8 @@ int lsu_merge_config_file_options(const char *path, const char *dflt,
        struct lls_parse_result *old_lpr = *lpr, *cf_lpr, *merged_lpr;
        const char *subcmd_name;
 
-       if (path)
-               cf = para_strdup(path);
-       else {
-               const char *home = get_homedir();
-               cf = make_message("%s/.paraslash/%s", home, dflt);
-       }
+       cf = path? para_strdup(path) : make_message("%s/%s",
+               get_confdir(), dflt);
        ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
        if (ret < 0) {
                if (ret == -E_EMPTY)
diff --git a/play.c b/play.c
index efac36a1a9c9c1096edbec27ac429df397364941..cc87286518469c53e8c2c3d006a842e3772e12f4 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1044,16 +1044,14 @@ static void session_open(void)
        if (OPT_GIVEN(HISTORY_FILE))
                history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
        else {
-               const char *home = get_homedir();
-               char *dot_para = make_message("%s/.paraslash", home);
+               const char *confdir = get_confdir();
 
-               ret = para_mkdir(dot_para);
+               ret = para_mkdir(confdir);
                /* warn, but otherwise ignore mkdir error */
                if (ret < 0)
-                       PARA_WARNING_LOG("Can not create %s: %s\n", dot_para,
+                       PARA_WARNING_LOG("Can not create %s: %s\n", confdir,
                                para_strerror(-ret));
-               history_file = make_message("%s/play.history", dot_para);
-               free(dot_para);
+               history_file = make_message("%s/play.history", confdir);
        }
        ici.history_file = history_file;
        ici.loglevel = loglevel;
index 0df82e000bc491d6de9e1333fb3241446674e49d..141cd6de5de41944fc1b2d9570ab6263b5f1d8cc 100644 (file)
--- a/server.c
+++ b/server.c
@@ -228,17 +228,12 @@ void parse_config_or_die(bool reload)
                daemon_set_flag(DF_LOG_TIMING);
        daemon_set_priority(OPT_UINT32_VAL(PRIORITY));
        if (!reload || getpid() != afs_pid) {
-               char *user_list_file;
-               if (OPT_GIVEN(USER_LIST))
-                       user_list_file = para_strdup(OPT_STRING_VAL(USER_LIST));
-               else {
-                       const char *home = get_homedir();
-                       user_list_file = make_message("%s/.paraslash/server.users", home);
-               }
+               char *user_list_file = OPT_GIVEN(USER_LIST)?
+                       para_strdup(OPT_STRING_VAL(USER_LIST)) :
+                       make_message("%s/server.users", get_confdir());
                user_list_init(user_list_file);
                free(user_list_file);
        }
-       return;
 }
 
 /*
index 1cc59eeaa127f37e34940683b7376c5ad9690743..9fffad13f3586796c2f66d66b884375f71c6570e 100644 (file)
--- a/string.c
+++ b/string.c
@@ -308,21 +308,28 @@ __must_check __malloc char *para_logname(void)
 }
 
 /**
- * Investigate $HOME to get the home directory of the calling user.
+ * Return the expansion of $HOME/.paraslash.
  *
- * \return A pointer to read-only memory that must not be freed by the caller.
- * If the environment variable HOME is unset or empty, the function prints an
- * error message and aborts.
+ * \return A pointer to memory that must not be freed by the caller. If the
+ * environment variable HOME is unset or empty, the function prints an error
+ * message and aborts.
  *
  * \sa getenv(3), getuid(2).
  */
-const char *get_homedir(void)
+const char *get_confdir(void)
 {
-       const char *home = getenv("HOME");
-       if (home && *home)
-               return home;
-       PARA_EMERG_LOG("fatal: HOME is unset or empty");
-       exit(EXIT_FAILURE);
+       static const char *dot_para;
+       const char *home;
+
+       if (dot_para)
+               return dot_para;
+       home = getenv("HOME");
+       if (!home || !*home) {
+               PARA_EMERG_LOG("fatal: HOME is unset or empty");
+               exit(EXIT_FAILURE);
+       }
+       dot_para = make_message("%s/.paraslash", home);
+       return dot_para;
 }
 
 /**
index 7cb4ed13ff2d7bf17107dc05b292b288507967ab..c499b7c41680f35971b345f44fd2161f2c56f1fe 100644 (file)
--- a/string.h
+++ b/string.h
@@ -80,7 +80,7 @@ __printf_2_3 unsigned xasprintf(char **result, const char *fmt, ...);
 __must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
 __must_check __malloc char *para_strcat(char *a, const char *b);
 __must_check __malloc char *para_logname(void);
-const char *get_homedir(void);
+const char *get_confdir(void);
 __malloc char *para_hostname(void);
 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...);
 int para_atoi64(const char *str, int64_t *result);
index 9b75ebef4cf039559b5eaf39a3f68d5e8ccc408d..39bd6686a3c09949fb127b37380c441223aab224 100644 (file)
@@ -76,20 +76,18 @@ static char *src_db_dir, *dst_db_dir, *src_aft_dir, *dst_aft_dir;
 
 static void set_paths(const struct lls_parse_result *lpr)
 {
-       const char *home = get_homedir();
+       const char *confdir = get_confdir();
 
        if (OPT_GIVEN(SRC_DATABASE_DIR, lpr))
                src_db_dir = para_strdup(OPT_STRING_VAL(SRC_DATABASE_DIR,
                        lpr));
        else
-               src_db_dir = make_message(
-                       "%s/.paraslash/afs_database-0.4", home);
+               src_db_dir = make_message("%s/afs_database-0.4", confdir);
        if (OPT_GIVEN(DST_DATABASE_DIR, lpr))
                dst_db_dir = para_strdup(OPT_STRING_VAL(DST_DATABASE_DIR,
                        lpr));
        else
-               dst_db_dir = make_message(
-                       "%s/.paraslash/afs_database-0.7", home);
+               dst_db_dir = make_message("%s/afs_database-0.7", confdir);
        src_aft_dir = make_message("%s/audio_files", src_db_dir);
        dst_aft_dir = make_message("%s/audio-files", src_db_dir);
        PARA_NOTICE_LOG("source aft dir: %s\n", src_aft_dir);