Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id D87E040D141 for ; Mon, 11 Oct 2010 06:27:35 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.6 X-Spam-Level: X-Spam-Status: No, score=-2.6 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, RCVD_IN_DNSWL_LOW=-0.7] autolearn=ham Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id haDA-XLbZceo for ; Mon, 11 Oct 2010 06:27:17 -0700 (PDT) Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21]) by olra.theworths.org (Postfix) with ESMTP id 84B0C40BFD3 for ; Mon, 11 Oct 2010 06:27:16 -0700 (PDT) Received: from rocinante.cs.unb.ca (fctnnbsc30w-142167176217.pppoe-dynamic.High-Speed.nb.bellaliant.net [142.167.176.217]) (authenticated bits=0) by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id o9BDRG7B003861 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO); Mon, 11 Oct 2010 10:27:16 -0300 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.72) (envelope-from ) id 1P5IPL-0004ca-Ra; Mon, 11 Oct 2010 10:27:15 -0300 From: david@tethera.net To: notmuch@notmuchmail.org Subject: [PATCH 1/3] Initial implementation of low level logging routines. Date: Mon, 11 Oct 2010 10:26:55 -0300 Message-Id: <1286803617-17328-2-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1286803617-17328-1-git-send-email-david@tethera.net> References: <1286803617-17328-1-git-send-email-david@tethera.net> Cc: David Bremner X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 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: Mon, 11 Oct 2010 13:27:36 -0000 From: David Bremner 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 + */ + +#include +#include +#include +#include + +#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