From: Bijan Chokoufe Nejad <bijan@chokoufe.com>
To: notmuch@notmuchmail.org
Subject: [PATCH v3] config: Expand ~ to $HOME
Date: Sat, 14 May 2016 17:15:49 +0200 [thread overview]
Message-ID: <1463238949-22612-1-git-send-email-bijan@chokoufe.com> (raw)
Very useful in case you want to keep your .notmuch-config synchronized across
machines where you have different user names.
---
v2: Check for '~/' instead of checking for '~' and fix memory issue.
Unit test is still pending
v3: Extended implementation handling also '~foo/' and '~foo'
---
notmuch-config.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/notmuch-config.c b/notmuch-config.c
index 01bb185..25668a6 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -602,9 +602,55 @@ _config_set_list (notmuch_config_t *config,
*config_var = NULL;
}
+static char *
+_config_get_user_folder (notmuch_config_t *config, const char *path) {
+ char* tmp = talloc_strdup (config, path + 1);
+ const char* user;
+ if (strpbrk(tmp, "/") != NULL) {
+ user = strtok (tmp, "/");
+ }
+ else {
+ user = tmp;
+ }
+ struct passwd * pwd = getpwnam (user);
+
+ if (pwd == NULL) {
+ printf("Config: could not expand %s.\n", path);
+ printf("'%s' doesn't seem to be a valid user on this system.\n", user);
+ exit(EXIT_FAILURE);
+ }
+
+ tmp = talloc_asprintf (config, "%s/%s", pwd->pw_dir, path + strlen (user) + 2);
+ return tmp;
+}
+
+static void
+_config_expand_path (notmuch_config_t *config) {
+ char* path = (char*) _config_get (config, &config->database_path,
+ "database", "path");
+ if (path != NULL && path[0] == '~') {
+ char *new_path;
+ if (strlen (path) == 1 || path[1] == '/') {
+ const char *home_path = getenv("HOME");
+ if (strlen (path) == 1)
+ new_path = talloc_asprintf (config, "%s%s", home_path, path + 1);
+ else if (path[1] == '/')
+ new_path = talloc_asprintf (config, "%s/%s", home_path, path + 2);
+ }
+ else {
+ new_path = _config_get_user_folder (config, path);
+ }
+ notmuch_config_set_database_path (config, new_path);
+ talloc_free (new_path);
+ }
+}
+
const char *
notmuch_config_get_database_path (notmuch_config_t *config)
{
+ // ideally a get_database_path should only get the database path and this
+ // call to _config_expand_path would be done in a setup phase
+ _config_expand_path (config);
return _config_get (config, &config->database_path, "database", "path");
}
--
2.7.4
next reply other threads:[~2016-05-14 15:16 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-14 15:15 Bijan Chokoufe Nejad [this message]
2016-05-25 15:55 ` [PATCH v3] config: Expand ~ to $HOME Tomi Ollila
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1463238949-22612-1-git-send-email-bijan@chokoufe.com \
--to=bijan@chokoufe.com \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).