Re: A systematic way of handling Xapian lock errors?
[notmuch-archives.git] / 6d / 3b78e08bd18abc1034b5a51b07f8fd68f4d7f6
1 Return-Path: <jani@nikula.org>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id AE4F4431FBC\r
6         for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:09 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id WVYs4bR+NIm4 for <notmuch@notmuchmail.org>;\r
16         Fri, 17 Jan 2014 08:39:02 -0800 (PST)\r
17 Received: from mail-ea0-f169.google.com (mail-ea0-f169.google.com\r
18         [209.85.215.169]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
19         (No client certificate requested)\r
20         by olra.theworths.org (Postfix) with ESMTPS id 69A14431FB6\r
21         for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:02 -0800 (PST)\r
22 Received: by mail-ea0-f169.google.com with SMTP id l9so1625117eaj.28\r
23         for <notmuch@notmuchmail.org>; Fri, 17 Jan 2014 08:39:01 -0800 (PST)\r
24 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
25         d=1e100.net; s=20130820;\r
26         h=x-gm-message-state:from:to:cc:subject:date:message-id;\r
27         bh=h8UkzEHbSu9ewJzylDmJmr9Mt/2Tx1AfLcu18xbblaQ=;\r
28         b=HruogG3qRP7HKgBU4OhhtT8454dDoVmGDHRQCxfbCL70nGEXFPv04y8trYk4mo3uZp\r
29         GCnsHgf0kojQdz+gmPA0sAiRfNm8iH13QhCGAsWLkcS3t0grCZu1TGdq/JP6ZnATIRYk\r
30         z5KH6Sk+cHvGCXo+ACKYVSHxfWL/dzrFG3RBJzb1XVSbYC2Gc6bH1v+ntHiiKdg9D5Fk\r
31         v7jj5q9LnM70RkqEsEJbBaZ8hTABm1vHPfRRFkzKoGBGFLAd45Lu48Qanx2RfsE/eFKL\r
32         HgoB6GXjuOMXpA1K0l99XntRts9czZsBZnlgdADVoQlyjlEXKW5lLyq6IiGLHmm8WdA7\r
33         cVrg==\r
34 X-Gm-Message-State:\r
35  ALoCoQkWELhL+gPZ9kgX5OrbOuLLi0t3gO6PPpS8Y+79ggCHtGRTymbeaczSl/51ayOCzuOPJEmU\r
36 X-Received: by 10.14.32.132 with SMTP id o4mr3713545eea.14.1389976741107;\r
37         Fri, 17 Jan 2014 08:39:01 -0800 (PST)\r
38 Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
39         [88.195.111.91])\r
40         by mx.google.com with ESMTPSA id v1sm28217872eef.9.2014.01.17.08.38.59\r
41         for <multiple recipients>\r
42         (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
43         Fri, 17 Jan 2014 08:39:00 -0800 (PST)\r
44 From: Jani Nikula <jani@nikula.org>\r
45 To: notmuch@notmuchmail.org\r
46 Subject: [PATCH] cli: abstract common config get/set code\r
47 Date: Fri, 17 Jan 2014 18:38:58 +0200\r
48 Message-Id: <1389976738-25056-1-git-send-email-jani@nikula.org>\r
49 X-Mailer: git-send-email 1.8.5.2\r
50 X-BeenThere: notmuch@notmuchmail.org\r
51 X-Mailman-Version: 2.1.13\r
52 Precedence: list\r
53 List-Id: "Use and development of the notmuch mail system."\r
54         <notmuch.notmuchmail.org>\r
55 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
56         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
57 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
58 List-Post: <mailto:notmuch@notmuchmail.org>\r
59 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
60 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
61         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
62 X-List-Received-Date: Fri, 17 Jan 2014 16:39:09 -0000\r
63 \r
64 Pretty straightforward abstraction similar to get/set list.\r
65 \r
66 ---\r
67 \r
68 v2 of id:1376839205-5115-1-git-send-email-jani@nikula.org adding a few\r
69 comments about config value caching per David's request. Dropped the\r
70 2nd patch as too tricky.\r
71 ---\r
72  notmuch-config.c | 86 +++++++++++++++++++++++---------------------------------\r
73  1 file changed, 35 insertions(+), 51 deletions(-)\r
74 \r
75 diff --git a/notmuch-config.c b/notmuch-config.c\r
76 index 6845e3c..4aad9eb 100644\r
77 --- a/notmuch-config.c\r
78 +++ b/notmuch-config.c\r
79 @@ -496,6 +496,32 @@ notmuch_config_is_new (notmuch_config_t *config)\r
80      return config->is_new;\r
81  }\r
82  \r
83 +static const char *\r
84 +_config_get (notmuch_config_t *config, char **field,\r
85 +            const char *group, const char *key)\r
86 +{\r
87 +    /* read from config file and cache value, if not cached already */\r
88 +    if (*field == NULL) {\r
89 +       char *value;\r
90 +       value = g_key_file_get_string (config->key_file, group, key, NULL);\r
91 +       if (value) {\r
92 +           *field = talloc_strdup (config, value);\r
93 +           free (value);\r
94 +       }\r
95 +    }\r
96 +    return *field;\r
97 +}\r
98 +\r
99 +static void\r
100 +_config_set (notmuch_config_t *config, char **field,\r
101 +            const char *group, const char *key, const char *value)\r
102 +{\r
103 +    g_key_file_set_string (config->key_file, group, key, value);\r
104 +\r
105 +    /* drop the cached value */\r
106 +    talloc_free (*field);\r
107 +    *field = NULL;\r
108 +}\r
109  \r
110  static const char **\r
111  _config_get_list (notmuch_config_t *config,\r
112 @@ -504,6 +530,7 @@ _config_get_list (notmuch_config_t *config,\r
113  {\r
114      assert(outlist);\r
115  \r
116 +    /* read from config file and cache value, if not cached already */\r
117      if (*outlist == NULL) {\r
118  \r
119         char **inlist = g_key_file_get_string_list (config->key_file,\r
120 @@ -535,6 +562,8 @@ _config_set_list (notmuch_config_t *config,\r
121                   size_t length, const char ***config_var )\r
122  {\r
123      g_key_file_set_string_list (config->key_file, group, name, list, length);\r
124 +\r
125 +    /* drop the cached value */\r
126      talloc_free (*config_var);\r
127      *config_var = NULL;\r
128  }\r
129 @@ -542,85 +571,40 @@ _config_set_list (notmuch_config_t *config,\r
130  const char *\r
131  notmuch_config_get_database_path (notmuch_config_t *config)\r
132  {\r
133 -    char *path;\r
134 -\r
135 -    if (config->database_path == NULL) {\r
136 -       path = g_key_file_get_string (config->key_file,\r
137 -                                     "database", "path", NULL);\r
138 -       if (path) {\r
139 -           config->database_path = talloc_strdup (config, path);\r
140 -           free (path);\r
141 -       }\r
142 -    }\r
143 -\r
144 -    return config->database_path;\r
145 +    return _config_get (config, &config->database_path, "database", "path");\r
146  }\r
147  \r
148  void\r
149  notmuch_config_set_database_path (notmuch_config_t *config,\r
150                                   const char *database_path)\r
151  {\r
152 -    g_key_file_set_string (config->key_file,\r
153 -                          "database", "path", database_path);\r
154 -\r
155 -    talloc_free (config->database_path);\r
156 -    config->database_path = NULL;\r
157 +    _config_set (config, &config->database_path, "database", "path", database_path);\r
158  }\r
159  \r
160  const char *\r
161  notmuch_config_get_user_name (notmuch_config_t *config)\r
162  {\r
163 -    char *name;\r
164 -\r
165 -    if (config->user_name == NULL) {\r
166 -       name = g_key_file_get_string (config->key_file,\r
167 -                                     "user", "name", NULL);\r
168 -       if (name) {\r
169 -           config->user_name = talloc_strdup (config, name);\r
170 -           free (name);\r
171 -       }\r
172 -    }\r
173 -\r
174 -    return config->user_name;\r
175 +    return _config_get (config, &config->user_name, "user", "name");\r
176  }\r
177  \r
178  void\r
179  notmuch_config_set_user_name (notmuch_config_t *config,\r
180                               const char *user_name)\r
181  {\r
182 -    g_key_file_set_string (config->key_file,\r
183 -                          "user", "name", user_name);\r
184 -\r
185 -    talloc_free (config->user_name);\r
186 -    config->user_name = NULL;\r
187 +    _config_set (config, &config->user_name, "user", "name", user_name);\r
188  }\r
189  \r
190  const char *\r
191  notmuch_config_get_user_primary_email (notmuch_config_t *config)\r
192  {\r
193 -    char *email;\r
194 -\r
195 -    if (config->user_primary_email == NULL) {\r
196 -       email = g_key_file_get_string (config->key_file,\r
197 -                                      "user", "primary_email", NULL);\r
198 -       if (email) {\r
199 -           config->user_primary_email = talloc_strdup (config, email);\r
200 -           free (email);\r
201 -       }\r
202 -    }\r
203 -\r
204 -    return config->user_primary_email;\r
205 +    return _config_get (config, &config->user_primary_email, "user", "primary_email");\r
206  }\r
207  \r
208  void\r
209  notmuch_config_set_user_primary_email (notmuch_config_t *config,\r
210                                        const char *primary_email)\r
211  {\r
212 -    g_key_file_set_string (config->key_file,\r
213 -                          "user", "primary_email", primary_email);\r
214 -\r
215 -    talloc_free (config->user_primary_email);\r
216 -    config->user_primary_email = NULL;\r
217 +    _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email);\r
218  }\r
219  \r
220  const char **\r
221 -- \r
222 1.8.5.2\r
223 \r