/* 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 #include #include #include #include "log-private.h" #include "notmuch.h" /* * Return a file descriptor to the open log file, or -1 if an error * occurs. * */ notmuch_log_t * notmuch_log_open (void *ctx,const char *path, notmuch_log_buffering_t buffering) { int fd; notmuch_log_t *log; log=talloc (ctx, notmuch_log_t); if (log==NULL) return NULL; fd = open (path, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if (fd < 0) { fprintf (stderr, "Failed to open %s: %s\n", path, strerror (errno)); return NULL; } log->file_desc = fd; log->buffer = NULL; log->txn_id = NULL; log->txn_depth = 0; log->buffering = buffering; return log; } static notmuch_status_t _log_write (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; } void notmuch_log_sync (notmuch_log_t *log) { if (log->buffer) { _log_write(log->file_desc, log->buffer, strlen (log->buffer)); talloc_free(log->buffer); log->buffer=NULL; } }; /* This function was derived from the print_string_ptr function of * cJSON (http://cjson.sourceforge.net/) and is used by permission of * the following license: * * Copyright (c) 2009 Dave Gamble * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ static char * _log_escape_chararray(const void *ctx, const char *str, const size_t len) { const char *ptr; char *ptr2; char *out; size_t loop; size_t required; for (loop = 0, required = 0, ptr = str; loop < len; loop++, required++, ptr++) { if ((unsigned char)(*ptr) <= ' ') required++; } /* * + 1 for trailing NULL. */ out = talloc_array (ctx, char, required + 1); ptr = str; ptr2 = out; for (loop = 0; loop < len; loop++) { if ((unsigned char)(*ptr) > 32 && *ptr != '\"' && *ptr != '\\') { *ptr2++ = *ptr++; } else { *ptr2++ = '\\'; switch (*ptr++) { case ' ': *ptr2++ = ' '; break; case '\\': *ptr2++ = '\\'; break; case '\b': *ptr2++ = 'b'; break; case '\f': *ptr2++ = 'f'; break; case '\n': *ptr2++ = 'n'; break; case '\r': *ptr2++ = 'r'; break; case '\t': *ptr2++ = 't'; break; default: ptr2--; break; } } } *ptr2++ = '\0'; return out; } static char* _log_escape_string (void *ctx, const char *str) { return _log_escape_chararray (ctx, str, strlen(str)); } /* * Log a series of strings as a space delimited line. Spaces and * backslashes will be escaped by adding a backslash in front. */ static notmuch_status_t _log_append_string (notmuch_log_t *log, const char *str) { if (log->buffer == NULL) { log->buffer = talloc_strdup (log, str); } else { log->buffer = talloc_strdup_append (log->buffer, str); } if (log->buffer == NULL){ return NOTMUCH_STATUS_OUT_OF_MEMORY; } if (log->buffering == NOTMUCH_LOG_BUFFER_NONE) notmuch_log_sync(log); return NOTMUCH_STATUS_SUCCESS; } /* * Log a null terminated list of strings as a single space delimited log line. * spaces and newlines are escaped. */ void notmuch_log_words (notmuch_log_t *log, const char *word, ...) { va_list args; const char *str; char *timestamp; timestamp = talloc_asprintf (log, "%ld", (long)time(NULL)); _log_append_string(log,timestamp); talloc_free(timestamp); va_start (args, word); for (str = word; str != NULL; str = va_arg (args, const char *)) { _log_append_string (log, " "); _log_append_string(log, _log_escape_string (log, str)); } va_end(args); _log_append_string(log, "\n"); if (log->buffering <= NOTMUCH_LOG_BUFFER_LINE) notmuch_log_sync(log); }; void notmuch_log_start_transaction (notmuch_log_t *log) { /* XXX This should probably be something like a uuid */ if (log->txn_depth == 0) { log->txn_id = talloc_asprintf (log, "%ld", random()); } log->txn_depth++; notmuch_log_words(log,"TX_START", talloc_asprintf (log, "%s.%d", log->txn_id, log->txn_depth)); }; void notmuch_log_finish_transaction (notmuch_log_t *log) { notmuch_log_words(log,"TX_END", talloc_asprintf (log, "%s.%d", log->txn_id, log->txn_depth)); log->txn_depth--; if (log->txn_depth == 0){ talloc_free (log->txn_id); log->txn_id = NULL; } };