[PATCH 1/3] Initial implementation of low level logging routines.
david at tethera.net
david at tethera.net
Mon Oct 11 06:26:55 PDT 2010
From: David Bremner <bremner at unb.ca>
notmuch_log_open: open a log file; just a wrapper around open(2)
notmuch_log_append: atomically append a buffer (character array) to a log file
Based on per-write file locking, performance will have to be tested.
---
Makefile.local | 1 +
notmuch-client.h | 10 +++++
notmuch-config.c | 15 ++++++++
notmuch-log.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 128 insertions(+), 0 deletions(-)
create mode 100644 notmuch-log.c
diff --git a/Makefile.local b/Makefile.local
index ade8412..8dbda15 100644
--- a/Makefile.local
+++ b/Makefile.local
@@ -240,6 +240,7 @@ notmuch_client_srcs = \
notmuch-config.c \
notmuch-count.c \
notmuch-dump.c \
+ notmuch-log.c \
notmuch-new.c \
notmuch-reply.c \
notmuch-restore.c \
diff --git a/notmuch-client.h b/notmuch-client.h
index 20be43b..0422b1c 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -191,6 +191,16 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
const char *new_tags[],
size_t length);
+const char *
+notmuch_config_get_log_path (notmuch_config_t *config,
+ const char *name);
+
+int
+notmuch_log_open (const char *path);
+
+notmuch_status_t
+notmuch_log_append (int file_desc, const char *buffer, size_t len);
+
notmuch_bool_t
debugger_is_active (void);
diff --git a/notmuch-config.c b/notmuch-config.c
index cf30603..c01e8f4 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -562,3 +562,18 @@ notmuch_config_set_new_tags (notmuch_config_t *config,
config->new_tags = NULL;
}
+const char *
+notmuch_config_get_log_path (notmuch_config_t *config, const char *name)
+{
+ char *path, *rpath;
+
+ path= g_key_file_get_string (config->key_file,
+ "log", name, NULL);
+ if (path != NULL) {
+ rpath = talloc_strdup (config, path);
+ free (path);
+ return rpath;
+ } else {
+ return NULL;
+ }
+}
diff --git a/notmuch-log.c b/notmuch-log.c
new file mode 100644
index 0000000..c4ddcd3
--- /dev/null
+++ b/notmuch-log.c
@@ -0,0 +1,102 @@
+
+/* notmuch - Not much of an email program, (just index and search)
+ *
+ * Copyright © 2010 David Bremner
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see http://www.gnu.org/licenses/ .
+ *
+ * Author: David Bremner <david at tethera.net>
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "notmuch-client.h"
+/*
+ Look a key up in the config file; open the corresponding file as a
+ log.
+
+ Return a file descriptor to the open log file, or -1 if an error
+ occurs.
+ */
+
+int
+notmuch_log_open (const char *path)
+{
+ int fd;
+
+ fd = open (path, O_CREAT|O_WRONLY|O_APPEND);
+ if (fd < 0) {
+ fprintf (stderr, "Failed to open %s: %s\n",
+ path, strerror (errno));
+ }
+
+ return fd;
+}
+
+notmuch_status_t
+notmuch_log_append (int file_desc, const char *buffer, size_t len){
+
+ struct flock lock;
+
+ lock.l_type = F_WRLCK;
+ lock.l_whence = SEEK_SET;
+ lock.l_start = 0;
+ lock.l_len = 0;
+
+ if (fcntl (file_desc, F_SETLKW, &lock) != 0) {
+ fprintf (stderr, "Failed to lock %s\n",
+ strerror (errno));
+
+ return NOTMUCH_STATUS_FILE_ERROR;
+ }
+
+ while (len > 0)
+ {
+ int written;
+
+ written = write(file_desc, buffer, len);
+ if (written < 0 || (written == 0 && errno !=0))
+ {
+ fprintf (stderr, "Failed to write %zd characters: %s\n",
+ len, strerror (errno));
+
+ return NOTMUCH_STATUS_FILE_ERROR;
+ }
+
+ len -= written;
+ buffer += written;
+
+ }
+
+ if (fdatasync (file_desc) != 0) {
+ fprintf (stderr, "Failed to sync: %s\n",
+ strerror (errno));
+
+ return NOTMUCH_STATUS_FILE_ERROR;
+ }
+
+ lock.l_type=F_UNLCK;
+
+ if (fcntl (file_desc, F_SETLK, &lock) != 0) {
+ fprintf (stderr, "Failed to unlock: %s\n",
+ strerror (errno));
+
+ return NOTMUCH_STATUS_FILE_ERROR;
+ }
+
+ return NOTMUCH_STATUS_SUCCESS;
+}
--
1.7.1
More information about the notmuch
mailing list