database error
[notmuch-archives.git] / 9b / b325b1b969d5672d68a2386795899db5a95352
1 Return-Path: <bremner@unb.ca>\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 D87E040D141\r
6         for <notmuch@notmuchmail.org>; Mon, 11 Oct 2010 06:27:35 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.6\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.6 tagged_above=-999 required=5\r
12         tests=[BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7] autolearn=ham\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 haDA-XLbZceo for <notmuch@notmuchmail.org>;\r
16         Mon, 11 Oct 2010 06:27:17 -0700 (PDT)\r
17 Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])\r
18         by olra.theworths.org (Postfix) with ESMTP id 84B0C40BFD3\r
19         for <notmuch@notmuchmail.org>; Mon, 11 Oct 2010 06:27:16 -0700 (PDT)\r
20 Received: from rocinante.cs.unb.ca\r
21         (fctnnbsc30w-142167176217.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
22         [142.167.176.217]) (authenticated bits=0)\r
23         by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id o9BDRG7B003861\r
24         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
25         Mon, 11 Oct 2010 10:27:16 -0300\r
26 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.72)\r
27         (envelope-from <bremner@unb.ca>)\r
28         id 1P5IPL-0004ca-Ra; Mon, 11 Oct 2010 10:27:15 -0300\r
29 From: david@tethera.net\r
30 To: notmuch@notmuchmail.org\r
31 Subject: [PATCH 1/3] Initial implementation of low level logging routines.\r
32 Date: Mon, 11 Oct 2010 10:26:55 -0300\r
33 Message-Id: <1286803617-17328-2-git-send-email-david@tethera.net>\r
34 X-Mailer: git-send-email 1.7.1\r
35 In-Reply-To: <1286803617-17328-1-git-send-email-david@tethera.net>\r
36 References: <1286803617-17328-1-git-send-email-david@tethera.net>\r
37 Cc: David Bremner <bremner@unb.ca>\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Mon, 11 Oct 2010 13:27:36 -0000\r
51 \r
52 From: David Bremner <bremner@unb.ca>\r
53 \r
54 notmuch_log_open: open a log file; just a wrapper around open(2)\r
55 notmuch_log_append: atomically append a buffer (character array) to a log file\r
56 \r
57 Based on per-write file locking, performance will have to be tested.\r
58 ---\r
59  Makefile.local   |    1 +\r
60  notmuch-client.h |   10 +++++\r
61  notmuch-config.c |   15 ++++++++\r
62  notmuch-log.c    |  102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
63  4 files changed, 128 insertions(+), 0 deletions(-)\r
64  create mode 100644 notmuch-log.c\r
65 \r
66 diff --git a/Makefile.local b/Makefile.local\r
67 index ade8412..8dbda15 100644\r
68 --- a/Makefile.local\r
69 +++ b/Makefile.local\r
70 @@ -240,6 +240,7 @@ notmuch_client_srcs =               \\r
71         notmuch-config.c        \\r
72         notmuch-count.c         \\r
73         notmuch-dump.c          \\r
74 +       notmuch-log.c           \\r
75         notmuch-new.c           \\r
76         notmuch-reply.c         \\r
77         notmuch-restore.c       \\r
78 diff --git a/notmuch-client.h b/notmuch-client.h\r
79 index 20be43b..0422b1c 100644\r
80 --- a/notmuch-client.h\r
81 +++ b/notmuch-client.h\r
82 @@ -191,6 +191,16 @@ notmuch_config_set_new_tags (notmuch_config_t *config,\r
83                              const char *new_tags[],\r
84                              size_t length);\r
85  \r
86 +const char *\r
87 +notmuch_config_get_log_path (notmuch_config_t *config,\r
88 +                            const char *name);\r
89 +\r
90 +int\r
91 +notmuch_log_open (const char *path);\r
92 +\r
93 +notmuch_status_t\r
94 +notmuch_log_append (int file_desc, const char *buffer, size_t len);\r
95 +\r
96  notmuch_bool_t\r
97  debugger_is_active (void);\r
98  \r
99 diff --git a/notmuch-config.c b/notmuch-config.c\r
100 index cf30603..c01e8f4 100644\r
101 --- a/notmuch-config.c\r
102 +++ b/notmuch-config.c\r
103 @@ -562,3 +562,18 @@ notmuch_config_set_new_tags (notmuch_config_t *config,\r
104      config->new_tags = NULL;\r
105  }\r
106  \r
107 +const char *\r
108 +notmuch_config_get_log_path (notmuch_config_t *config, const char *name)\r
109 +{\r
110 +    char *path, *rpath;\r
111 +\r
112 +    path= g_key_file_get_string (config->key_file,\r
113 +                                "log", name, NULL);\r
114 +    if (path != NULL) {\r
115 +       rpath = talloc_strdup (config, path);\r
116 +       free (path);\r
117 +       return rpath;\r
118 +    } else {\r
119 +       return NULL;\r
120 +    }\r
121 +}\r
122 diff --git a/notmuch-log.c b/notmuch-log.c\r
123 new file mode 100644\r
124 index 0000000..c4ddcd3\r
125 --- /dev/null\r
126 +++ b/notmuch-log.c\r
127 @@ -0,0 +1,102 @@\r
128 +\r
129 +/* notmuch - Not much of an email program, (just index and search)\r
130 + *\r
131 + * Copyright © 2010 David Bremner\r
132 + *\r
133 + * This program is free software: you can redistribute it and/or modify\r
134 + * it under the terms of the GNU General Public License as published by\r
135 + * the Free Software Foundation, either version 3 of the License, or\r
136 + * (at your option) any later version.\r
137 + *\r
138 + * This program is distributed in the hope that it will be useful,\r
139 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
140 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
141 + * GNU General Public License for more details.\r
142 + *\r
143 + * You should have received a copy of the GNU General Public License\r
144 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
145 + *\r
146 + * Author: David Bremner <david@tethera.net>\r
147 + */\r
148 +\r
149 +#include <sys/types.h>\r
150 +#include <sys/stat.h>\r
151 +#include <fcntl.h>\r
152 +#include <unistd.h>\r
153 +\r
154 +#include "notmuch-client.h"\r
155 +/*\r
156 +   Look a key up in the config file; open the corresponding file as a\r
157 +   log.\r
158 +\r
159 +   Return a file descriptor to the open log file, or -1 if an error\r
160 +   occurs.\r
161 +  */\r
162 +\r
163 +int\r
164 +notmuch_log_open (const char *path)\r
165 +{\r
166 +    int fd;\r
167 +\r
168 +    fd = open (path, O_CREAT|O_WRONLY|O_APPEND);\r
169 +    if (fd < 0) {\r
170 +       fprintf (stderr, "Failed to open %s: %s\n",\r
171 +                path, strerror (errno));\r
172 +    }\r
173 +\r
174 +    return fd;\r
175 +}\r
176 +\r
177 +notmuch_status_t\r
178 +notmuch_log_append (int file_desc, const char *buffer, size_t len){\r
179 +\r
180 +    struct flock lock;\r
181 +\r
182 +    lock.l_type = F_WRLCK;\r
183 +    lock.l_whence = SEEK_SET;\r
184 +    lock.l_start = 0;\r
185 +    lock.l_len = 0;\r
186 +\r
187 +    if (fcntl (file_desc, F_SETLKW, &lock) != 0) {\r
188 +       fprintf (stderr, "Failed to lock  %s\n",\r
189 +                strerror (errno));\r
190 +\r
191 +       return NOTMUCH_STATUS_FILE_ERROR;\r
192 +    }\r
193 +\r
194 +    while (len > 0)\r
195 +    {\r
196 +       int written;\r
197 +\r
198 +       written = write(file_desc, buffer, len);\r
199 +       if (written < 0 || (written == 0 && errno !=0))\r
200 +       {\r
201 +           fprintf (stderr, "Failed to write %zd characters: %s\n",\r
202 +                    len, strerror (errno));\r
203 +\r
204 +           return NOTMUCH_STATUS_FILE_ERROR;\r
205 +       }\r
206 +\r
207 +       len -= written;\r
208 +       buffer += written;\r
209 +\r
210 +    }\r
211 +\r
212 +    if (fdatasync (file_desc) != 0) {\r
213 +       fprintf (stderr, "Failed to sync:  %s\n",\r
214 +                strerror (errno));\r
215 +\r
216 +       return NOTMUCH_STATUS_FILE_ERROR;\r
217 +    }\r
218 +\r
219 +    lock.l_type=F_UNLCK;\r
220 +\r
221 +    if (fcntl (file_desc, F_SETLK, &lock) != 0) {\r
222 +       fprintf (stderr, "Failed to unlock:  %s\n",\r
223 +                strerror (errno));\r
224 +\r
225 +       return NOTMUCH_STATUS_FILE_ERROR;\r
226 +    }\r
227 +\r
228 +    return NOTMUCH_STATUS_SUCCESS;\r
229 +}\r
230 -- \r
231 1.7.1\r
232 \r