Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by arlo.cworth.org (Postfix) with ESMTP id 989206DE01EA for ; Sat, 14 May 2016 08:16:29 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at cworth.org X-Spam-Flag: NO X-Spam-Score: -0.531 X-Spam-Level: X-Spam-Status: No, score=-0.531 tagged_above=-999 required=5 tests=[AWL=-0.061, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.249, FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, SPF_PASS=-0.001] autolearn=disabled Received: from arlo.cworth.org ([127.0.0.1]) by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oOavauwDgjg0 for ; Sat, 14 May 2016 08:16:21 -0700 (PDT) Received: from mail-wm0-f66.google.com (mail-wm0-f66.google.com [74.125.82.66]) by arlo.cworth.org (Postfix) with ESMTPS id A2B656DE01D0 for ; Sat, 14 May 2016 08:16:20 -0700 (PDT) Received: by mail-wm0-f66.google.com with SMTP id w143so9252784wmw.3 for ; Sat, 14 May 2016 08:16:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id; bh=0eUoOLuLsViVEB7I24ou8Os2MaZ6iLIM5B4ybvgUUxo=; b=QwB2fn85UZ8HemuBvHZEOQEx2Kym3l/rcsFXqba/lb9G8iptBhGpUSzSovJzEFzYzU tj0GigBXbTwBW4967qk4uCyS6jhixwgbNrmHa3PWWgGYqsEQCboJZLLnSdMD4YiokKHW KhTPjiNudtHL8QiuyFpld5E3muXI7GMPBC9OE1YpHe/H+KxOCQF9E5cM1L8s9wvX/UR2 sIsyP/e1TktkWKfCW6J+zSxwf6jJ9XSEkGip4dvBbZ8Vh82N+BDAvNW9mUeBvvD+fIHW ElQkh9IWaezaKfEeD6GYeBczc+luRNdT7ndVYt85HKk7NpHC4tzUj4/jpsRhMD1zUAfq nVtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id; bh=0eUoOLuLsViVEB7I24ou8Os2MaZ6iLIM5B4ybvgUUxo=; b=O3OJ++fLX4iFbvfqvzB89lbcovRjLcKV1Ujjz7A1bpE218EJF+9P4rvyPBhEgFpTGN OlNP6Zq1ZnNCrkMD1uEcRlNJFbvYGvdS7NTT9oe6IRcAADwRU5jUUSMLZydKSX23gA4w S2qWkgSy6tLjJ/wwOF8UnxaD6jG60GeC5BzMRnssWMhWH3TPjG4eHhZHSwYfKHI5f8Mm lfv9NM+fToTGOshoNHBaQJsXFSkqXKFYCv7ZP7b8/mFZ57KaQzRfKF43gOpkhEOl3RfA dgzfFjrTDcT9sPiMrRnh3VJNIadfKUJFW1yQw87U9ikXXSH8rdFn36soOD0o1w6G19yf rxsA== X-Gm-Message-State: AOPr4FV20IKDPQ3/4Y1JglVWbQhyi+4UQRMHVR/aAF1+Qy8GsIKvg+mk0fqjjNDnANZS8w== X-Received: by 10.194.59.108 with SMTP id y12mr20565048wjq.63.1463238978863; Sat, 14 May 2016 08:16:18 -0700 (PDT) Received: from localhost.localdomain (ip5f5be327.dynamic.kabel-deutschland.de. [95.91.227.39]) by smtp.gmail.com with ESMTPSA id u4sm24112394wjz.4.2016.05.14.08.16.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sat, 14 May 2016 08:16:17 -0700 (PDT) Sender: Bijan Chokoufe From: Bijan Chokoufe Nejad To: notmuch@notmuchmail.org Subject: [PATCH v3] config: Expand ~ to $HOME Date: Sat, 14 May 2016 17:15:49 +0200 Message-Id: <1463238949-22612-1-git-send-email-bijan@chokoufe.com> X-Mailer: git-send-email 2.7.4 X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 May 2016 15:16:29 -0000 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