--- /dev/null
+Return-Path: <tomi.ollila@iki.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id EF2116DE01F7\r
+ for <notmuch@notmuchmail.org>; Wed, 25 May 2016 08:55:54 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.589\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.589 tagged_above=-999 required=5 tests=[AWL=-0.063,\r
+ SPF_NEUTRAL=0.652] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 9CsfeJYB6O32 for <notmuch@notmuchmail.org>;\r
+ Wed, 25 May 2016 08:55:46 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by arlo.cworth.org (Postfix) with ESMTP id 46E4F6DE00EB\r
+ for <notmuch@notmuchmail.org>; Wed, 25 May 2016 08:55:45 -0700 (PDT)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+ by guru.guru-group.fi (Postfix) with ESMTP id ADDFA100090;\r
+ Wed, 25 May 2016 18:55:43 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: Bijan Chokoufe Nejad <bijan@chokoufe.com>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH v3] config: Expand ~ to $HOME\r
+In-Reply-To: <1463238949-22612-1-git-send-email-bijan@chokoufe.com>\r
+References: <1463238949-22612-1-git-send-email-bijan@chokoufe.com>\r
+User-Agent: Notmuch/0.22+26~g2adc7d1 (http://notmuchmail.org) Emacs/24.5.1\r
+ (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+ $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+ !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Wed, 25 May 2016 18:55:43 +0300\r
+Message-ID: <m2fut6cetc.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 25 May 2016 15:55:55 -0000\r
+\r
+On Sat, May 14 2016, Bijan Chokoufe Nejad <bijan@chokoufe.com> wrote:\r
+\r
+> Very useful in case you want to keep your .notmuch-config synchronized across\r
+> machines where you have different user names.\r
+\r
+\r
+Good progress! There are quite a few issues still to resolve...\r
+\r
+First, commit message (1st line got a bit outdated). More comments inline\r
+\r
+>\r
+> ---\r
+> v2: Check for '~/' instead of checking for '~' and fix memory issue.\r
+> Unit test is still pending\r
+> v3: Extended implementation handling also '~foo/' and '~foo'\r
+> ---\r
+> notmuch-config.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++\r
+> 1 file changed, 46 insertions(+)\r
+>\r
+> diff --git a/notmuch-config.c b/notmuch-config.c\r
+> index 01bb185..25668a6 100644\r
+> --- a/notmuch-config.c\r
+> +++ b/notmuch-config.c\r
+> @@ -602,9 +602,55 @@ _config_set_list (notmuch_config_t *config,\r
+> *config_var = NULL;\r
+> }\r
+> \r
+> +static char *\r
+> +_config_get_user_folder (notmuch_config_t *config, const char *path) {\r
+\r
+Naming, but that will go away via other future changes.\r
+\r
+> + char* tmp = talloc_strdup (config, path + 1);\r
+> + const char* user;\r
+> + if (strpbrk(tmp, "/") != NULL) {\r
+> + user = strtok (tmp, "/");\r
+\r
+Above (sh|c)ould be done like:\r
+\r
+ char *user = talloc_strdup (config, path + 1); // XXX path + 1\r
+ char *tmp = strchr(user, '/');\r
+ if (tmp) *tmp = '\0';\r
+\r
+> + }\r
+> + else {\r
+> + user = tmp;\r
+> + }\r
+> + struct passwd * pwd = getpwnam (user);\r
+> +\r
+> + if (pwd == NULL) {\r
+> + printf("Config: could not expand %s.\n", path);\r
+> + printf("'%s' doesn't seem to be a valid user on this system.\n", user);\r
+> + exit(EXIT_FAILURE);\r
+> + }\r
+\r
+Unable to expand ~ expression is not failure. it is just used unexpanded.\r
+This is the way e.g. shell and emacs does it.\r
+\r
+> +\r
+> + tmp = talloc_asprintf (config, "%s/%s", pwd->pw_dir, path + strlen (user) + 2);\r
+> + return tmp;\r
+> +}\r
+> +\r
+> +static void\r
+> +_config_expand_path (notmuch_config_t *config) {\r
+> + char* path = (char*) _config_get (config, &config->database_path,\r
+> + "database", "path");\r
+\r
+_config_get done twice in all cases, which doesn't sound like a good idea.\r
+\r
+> + if (path != NULL && path[0] == '~') {\r
+> + char *new_path;\r
+> + if (strlen (path) == 1 || path[1] == '/') {\r
+\r
+strlen(path) scans the whole string and then the return value is just\r
+compared with 1 \r
+better do path[1] == '\0' (after it was checked that path[0] != '\0' above)\r
+\r
+> + const char *home_path = getenv("HOME");\r
+> + if (strlen (path) == 1)\r
+\r
+ditto\r
+\r
+> + new_path = talloc_asprintf (config, "%s%s", home_path, path + 1);\r
+> + else if (path[1] == '/')\r
+> + new_path = talloc_asprintf (config, "%s/%s", home_path, path + 2);\r
+> + }\r
+> + else {\r
+> + new_path = _config_get_user_folder (config, path);\r
+> + }\r
+> + notmuch_config_set_database_path (config, new_path);\r
+\r
+I think this was my suggestion (or something similar) .. but that is not a\r
+good idea -- some (unrelated) config setting will (or may) expand ~ and\r
+write it to the config file.\r
+\r
+> + talloc_free (new_path);\r
+> + }\r
+> +}\r
+> +\r
+> const char *\r
+> notmuch_config_get_database_path (notmuch_config_t *config)\r
+> {\r
+> + // ideally a get_database_path should only get the database path and this\r
+> + // call to _config_expand_path would be done in a setup phase\r
+> + _config_expand_path (config);\r
+> return _config_get (config, &config->database_path, "database", "path");\r
+> }\r
+> \r
+> -- \r
+> 2.7.4\r
+\r
+I think there are at least these 2 ways that might get this done...\r
+\r
+... both uses the expand_tilde WIP i did post earlier -- there is still\r
+something to be fixed there (in addition to header file)\r
+\r
+;; This buffer is for notes you don't want to save, and for Lisp evaluation.\r
+;; If you want to create a file, visit that file with C-x C-f,\r
+;; then enter the text in that file's own buffer.\r
+\r
+--------------- either just with changes in notmuch_config_get_database_path\r
+\r
+const char *\r
+notmuch_config_get_database_path (notmuch_config_t *config)\r
+{\r
+ if (config->database_path != NULL)\r
+ return config->database_path;\r
+\r
+ (void)_config_get (config, &config->database_path, "database", "path");\r
+\r
+ if (!!config->database_path && config->database_path[0] == '~') {\r
+ const char * rest;\r
+ const char * path = expand_tilde (config->database_path, &rest);\r
+ if (path != NULL) {\r
+ talloc_free (config->database_path);\r
+ //config->database_path = [path stringByAppendingString rest];\r
+ config->database_path = talloc_asprintf ("%s%s", path, rest);\r
+ }\r
+ }\r
+ return config->database_path;\r
+}\r
+\r
+--------------- or with new helper _config_get_expand_tilde and using that...\r
+\r
+const char *\r
+notmuch_config_get_database_path (notmuch_config_t *config)\r
+{\r
+ return _config_get_expand_tilde (config, &config->database_path,\r
+ "database", "path");\r
+}\r
+\r
+\r
+--- this is to be next to _config_get and the changes of these two needs to\r
+--- be tracked carefully (if there is ever need to change); alternatively\r
+--- support function and flag variable to decice when to expand and when not\r
+\r
+static const char *\r
+_config_get_expand_tilde (notmuch_config_t *config, char **field,\r
+ const char *group, const char *key)\r
+{\r
+ /* read from config file and cache value, if not cached already */\r
+ if (*field == NULL) {\r
+ char *value;\r
+ value = g_key_file_get_string (config->key_file, group, key, NULL);\r
+ if (value) {\r
+ const char *path, *rest;\r
+ if (value[0] == '~' && (path = expand_tilde (value, &rest)) != NULL)\r
+ *field = talloc_asprintf ("%s%s", path, rest);\r
+ else\r
+ *field = talloc_strdup (config, value);\r
+ free (value);\r
+ }\r
+ }\r
+ return *field;\r
+}\r
+\r
+\r
+\r
+Tomi\r
+\r