* [WIP 1/3] util: move chomp_newline to string-util.h
2014-09-28 18:28 ` David Bremner
@ 2014-09-28 18:28 ` David Bremner
2014-09-28 18:28 ` [WIP 2/3] cli/insert: move several file/directory manipulation routines into a new util file David Bremner
2014-09-28 18:28 ` [WIP 3/3] lib: add configuration framework David Bremner
2 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2014-09-28 18:28 UTC (permalink / raw)
To: notmuch
This allows it to be called from functions in the library, if needed.
Instead of making all of string-util available to everywhere
notmuch-client.h is included, just add an extra include to the three
places chomp_newline was needed (and string-util.h not yet included).
---
notmuch-client.h | 7 -------
notmuch-count.c | 1 +
notmuch-setup.c | 1 +
test/hex-xcode.c | 2 ++
util/string-util.h | 7 +++++++
5 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/notmuch-client.h b/notmuch-client.h
index e1efbe0..bd01005 100644
--- a/notmuch-client.h
+++ b/notmuch-client.h
@@ -111,13 +111,6 @@ typedef struct notmuch_show_params {
#define STRNCMP_LITERAL(var, literal) \
strncmp ((var), (literal), sizeof (literal) - 1)
-static inline void
-chomp_newline (char *str)
-{
- if (str && str[strlen(str)-1] == '\n')
- str[strlen(str)-1] = '\0';
-}
-
/* Exit status code indicating the requested format version is too old
* (support for that version has been dropped). CLI code should use
* notmuch_exit_if_unsupported_format rather than directly exiting
diff --git a/notmuch-count.c b/notmuch-count.c
index 6058f7c..c3f4b54 100644
--- a/notmuch-count.c
+++ b/notmuch-count.c
@@ -20,6 +20,7 @@
*/
#include "notmuch-client.h"
+#include "string-util.h"
enum {
OUTPUT_THREADS,
diff --git a/notmuch-setup.c b/notmuch-setup.c
index 36a6171..eb91179 100644
--- a/notmuch-setup.c
+++ b/notmuch-setup.c
@@ -19,6 +19,7 @@
*/
#include "notmuch-client.h"
+#include "string-util.h"
static const char *
make_path_absolute (void *ctx, const char *path)
diff --git a/test/hex-xcode.c b/test/hex-xcode.c
index 65d4956..20d0a68 100644
--- a/test/hex-xcode.c
+++ b/test/hex-xcode.c
@@ -9,6 +9,8 @@
#include "notmuch-client.h"
#include "hex-escape.h"
+#include "string-util.h"
+
#include <assert.h>
enum direction {
diff --git a/util/string-util.h b/util/string-util.h
index e409cb3..9337770 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -64,6 +64,13 @@ int
parse_boolean_term (void *ctx, const char *str,
char **prefix_out, char **term_out);
+static inline void
+chomp_newline (char *str)
+{
+ if (str && str[strlen(str)-1] == '\n')
+ str[strlen(str)-1] = '\0';
+}
+
#ifdef __cplusplus
}
#endif
--
2.1.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [WIP 2/3] cli/insert: move several file/directory manipulation routines into a new util file.
2014-09-28 18:28 ` David Bremner
2014-09-28 18:28 ` [WIP 1/3] util: move chomp_newline to string-util.h David Bremner
@ 2014-09-28 18:28 ` David Bremner
2014-09-28 18:28 ` [WIP 3/3] lib: add configuration framework David Bremner
2 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2014-09-28 18:28 UTC (permalink / raw)
To: notmuch
This will promote code sharing of low level file operations.
---
notmuch-insert.c | 79 ++----------------------------------------
util/Makefile.local | 1 +
util/file-util.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++
util/file-util.h | 20 +++++++++++
4 files changed, 121 insertions(+), 77 deletions(-)
create mode 100644 util/file-util.c
create mode 100644 util/file-util.h
diff --git a/notmuch-insert.c b/notmuch-insert.c
index 5ef6e66..48bdd28 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -23,6 +23,7 @@
#include "notmuch-client.h"
#include "tag-util.h"
+#include "file-util.h"
#include <sys/types.h>
#include <sys/stat.h>
@@ -63,26 +64,6 @@ safe_gethostname (char *hostname, size_t len)
}
}
-/* Call fsync() on a directory path. */
-static notmuch_bool_t
-sync_dir (const char *dir)
-{
- int fd, r;
-
- fd = open (dir, O_RDONLY);
- if (fd == -1) {
- fprintf (stderr, "Error: open %s: %s\n", dir, strerror (errno));
- return FALSE;
- }
-
- r = fsync (fd);
- if (r)
- fprintf (stderr, "Error: fsync %s: %s\n", dir, strerror (errno));
-
- close (fd);
-
- return r == 0;
-}
/*
* Check the specified folder name does not contain a directory
@@ -92,65 +73,9 @@ sync_dir (const char *dir)
static notmuch_bool_t
is_valid_folder_name (const char *folder)
{
- const char *p = folder;
-
- for (;;) {
- if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/'))
- return FALSE;
- p = strchr (p, '/');
- if (!p)
- return TRUE;
- p++;
- }
+ return ! has_double_dot_component (folder);
}
-/*
- * Make the given directory and its parents as necessary, using the
- * given mode. Return TRUE on success, FALSE otherwise. Partial
- * results are not cleaned up on errors.
- */
-static notmuch_bool_t
-mkdir_recursive (const void *ctx, const char *path, int mode)
-{
- struct stat st;
- int r;
- char *parent = NULL, *slash;
-
- /* First check the common case: directory already exists. */
- r = stat (path, &st);
- if (r == 0) {
- if (! S_ISDIR (st.st_mode)) {
- fprintf (stderr, "Error: '%s' is not a directory: %s\n",
- path, strerror (EEXIST));
- return FALSE;
- }
-
- return TRUE;
- } else if (errno != ENOENT) {
- fprintf (stderr, "Error: stat '%s': %s\n", path, strerror (errno));
- return FALSE;
- }
-
- /* mkdir parents, if any */
- slash = strrchr (path, '/');
- if (slash && slash != path) {
- parent = talloc_strndup (ctx, path, slash - path);
- if (! parent) {
- fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
- return FALSE;
- }
-
- if (! mkdir_recursive (ctx, parent, mode))
- return FALSE;
- }
-
- if (mkdir (path, mode)) {
- fprintf (stderr, "Error: mkdir '%s': %s\n", path, strerror (errno));
- return FALSE;
- }
-
- return parent ? sync_dir (parent) : TRUE;
-}
/*
* Create the given maildir folder, i.e. maildir and its
diff --git a/util/Makefile.local b/util/Makefile.local
index 905f237..8749cfb 100644
--- a/util/Makefile.local
+++ b/util/Makefile.local
@@ -5,6 +5,7 @@ extra_cflags += -I$(srcdir)/$(dir)
libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
$(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
+ $(dir)/file-util.c \
$(dir)/util.c
libutil_modules := $(libutil_c_srcs:.c=.o)
diff --git a/util/file-util.c b/util/file-util.c
new file mode 100644
index 0000000..66c4485
--- /dev/null
+++ b/util/file-util.c
@@ -0,0 +1,98 @@
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <talloc.h>
+#include <unistd.h>
+
+#include "file-util.h"
+
+/*
+ * Check whether specified path name contains a directory
+ * component "..". to prevent writes outside of the Maildir
+ */
+int
+has_double_dot_component (const char *path)
+{
+ const char *p = path;
+
+ for (;;) {
+ if ((p[0] == '.') && (p[1] == '.') && (p[2] == '\0' || p[2] == '/'))
+ return TRUE;
+ p = strchr (p, '/');
+ if (!p)
+ return FALSE;
+ p++;
+ }
+}
+
+/*
+ * Make the given directory and its parents as necessary, using the
+ * given mode. return TRUE on success, 0 otherwise. Partial
+ * results are not cleaned up on errors.
+ */
+int
+mkdir_recursive (const void *ctx, const char *path, int mode)
+{
+ struct stat st;
+ int r;
+ char *parent = NULL, *slash;
+
+ /* First check the common case: directory already exists. */
+ r = stat (path, &st);
+ if (r == 0) {
+ if (! S_ISDIR (st.st_mode)) {
+ fprintf (stderr, "Error: '%s' is not a directory: %s\n",
+ path, strerror (EEXIST));
+ return FALSE;
+ }
+
+ return TRUE;
+ } else if (errno != ENOENT) {
+ fprintf (stderr, "Error: stat '%s': %s\n", path, strerror (errno));
+ return FALSE;
+ }
+
+ /* mkdir parents, if any */
+ slash = strrchr (path, '/');
+ if (slash && slash != path) {
+ parent = talloc_strndup (ctx, path, slash - path);
+ if (! parent) {
+ fprintf (stderr, "Error: %s\n", strerror (ENOMEM));
+ return FALSE;
+ }
+
+ if (! mkdir_recursive (ctx, parent, mode))
+ return FALSE;
+ }
+
+ if (mkdir (path, mode)) {
+ fprintf (stderr, "Error: mkdir '%s': %s\n", path, strerror (errno));
+ return FALSE;
+ }
+
+ return parent ? sync_dir (parent) : TRUE;
+}
+
+/* Call fsync() on a directory path. */
+int
+sync_dir (const char *dir)
+{
+ int fd, r;
+
+ fd = open (dir, O_RDONLY);
+ if (fd == -1) {
+ fprintf (stderr, "Error: open %s: %s\n", dir, strerror (errno));
+ return FALSE;
+ }
+
+ r = fsync (fd);
+ if (r)
+ fprintf (stderr, "Error: fsync %s: %s\n", dir, strerror (errno));
+
+ close (fd);
+
+ return r == 0;
+}
diff --git a/util/file-util.h b/util/file-util.h
new file mode 100644
index 0000000..4f96957
--- /dev/null
+++ b/util/file-util.h
@@ -0,0 +1,20 @@
+#ifndef _FILE_UTIL_H
+#define _FILE_UTIL_H
+
+#ifndef FALSE
+#define FALSE 0
+#endif
+#ifndef TRUE
+#define TRUE 1
+#endif
+
+int
+has_double_dot_component (const char *path);
+
+int
+mkdir_recursive (const void *ctx, const char *path, int mode);
+
+int
+sync_dir (const char *path);
+
+#endif
--
2.1.0
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [WIP 3/3] lib: add configuration framework.
2014-09-28 18:28 ` David Bremner
2014-09-28 18:28 ` [WIP 1/3] util: move chomp_newline to string-util.h David Bremner
2014-09-28 18:28 ` [WIP 2/3] cli/insert: move several file/directory manipulation routines into a new util file David Bremner
@ 2014-09-28 18:28 ` David Bremner
2 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2014-09-28 18:28 UTC (permalink / raw)
To: notmuch
Allow clients to atomically get and set key value pairs.
---
lib/Makefile.local | 1 +
lib/config.c | 156 ++++++++++++++++++++++++++++++++++++++++++++++++
lib/notmuch.h | 7 +++
test/Makefile.local | 7 +++
test/T560-lib-config.sh | 15 +++++
test/config-test.c | 28 +++++++++
6 files changed, 214 insertions(+)
create mode 100644 lib/config.c
create mode 100755 test/T560-lib-config.sh
create mode 100644 test/config-test.c
diff --git a/lib/Makefile.local b/lib/Makefile.local
index 4120390..7ca2b3b 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -54,6 +54,7 @@ lib := $(dir)
libnotmuch_c_srcs = \
$(notmuch_compat_srcs) \
+ $(dir)/config.c \
$(dir)/filenames.c \
$(dir)/string-list.c \
$(dir)/libsha1.c \
diff --git a/lib/config.c b/lib/config.c
new file mode 100644
index 0000000..c3b8f39
--- /dev/null
+++ b/lib/config.c
@@ -0,0 +1,156 @@
+#include "notmuch-private.h"
+#include "string-util.h"
+#include "file-util.h"
+#include <talloc.h>
+
+static notmuch_status_t
+compute_paths (void *ctx, const char *notmuch_path, const char *key,
+ char **parent_out, char **dest_out) {
+
+ char *parent, *dest, *final_component, *last_slash;
+
+ parent = talloc_asprintf (ctx, "%s/config/%s", notmuch_path, key);
+ if (!parent)
+ return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+ last_slash = strrchr (parent, '/');
+ *last_slash = '\0';
+
+ final_component = talloc_strdup (ctx, last_slash + 1);
+
+ dest = talloc_asprintf(ctx, "%s/_%s", parent, final_component);
+ if (!dest)
+ return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
+ *parent_out = parent;
+ *dest_out = dest;
+
+ return NOTMUCH_STATUS_SUCCESS;
+
+}
+
+notmuch_status_t
+notmuch_config_get (const char *notmuch_path, const char *key, const char **val){
+
+ char *line = NULL;
+ size_t line_size;
+ ssize_t line_len;
+ char *buf = NULL;
+ char *file_name, *parent;
+ notmuch_status_t status;
+ void *local = NULL;
+ FILE *file_ptr = NULL;
+
+ if (notmuch_path == NULL || key == NULL || val == NULL)
+ return NOTMUCH_STATUS_NULL_POINTER;
+
+ local = talloc_new (NULL);
+
+ status = compute_paths (local, notmuch_path, key, &parent, &file_name);
+ if (status)
+ goto DONE;
+
+ file_ptr = fopen (file_name, "r");
+ if (file_ptr == NULL) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ while ((line_len = getline (&line, &line_size, file_ptr)) != -1) {
+
+ if (buf)
+ buf = talloc_asprintf (local, "%s%s", buf, line);
+ else
+ buf = talloc_strdup (local, line);
+
+ if (buf == NULL) {
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+ goto DONE;
+ }
+
+ }
+
+
+ /* remove the last newline. Convenient for the single line case. */
+ chomp_newline (buf);
+
+ *val = buf;
+ status = NOTMUCH_STATUS_SUCCESS;
+
+ DONE:
+ if (line)
+ free (line);
+
+ if (file_ptr)
+ fclose (file_ptr);
+
+ talloc_free (local);
+
+ return status;
+}
+
+notmuch_status_t
+notmuch_config_set (const char *notmuch_path, const char *key, const char *val){
+
+ char *parent, *path, *temp_path;
+ int out_fd = -1;
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+ void *local = NULL;
+ FILE *out_file;
+
+ if (notmuch_path == NULL || key == NULL || val == NULL)
+ return NOTMUCH_STATUS_NULL_POINTER;
+
+ if (has_double_dot_component (key))
+ return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
+
+ local = talloc_new (NULL);
+
+ status = compute_paths (local, notmuch_path, key, &parent, &path);
+ if (status)
+ goto DONE;
+
+ if (! mkdir_recursive (local, parent, 0700)) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ temp_path = talloc_asprintf (local, "%s/tmp.XXXXXX", parent);
+ if (temp_path == NULL) {
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+ goto DONE;
+ }
+
+ out_fd = mkstemp (temp_path);
+ if (out_fd == -1) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ out_file = fdopen (out_fd, "w");
+ if (out_file == NULL) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ if (fputs (val, out_file) == EOF) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ if (fclose (out_file)) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ if (rename (temp_path, path) < 0) {
+ status = NOTMUCH_STATUS_FILE_ERROR;
+ goto DONE;
+ }
+
+ DONE:
+
+ talloc_free(local);
+
+ return status;
+}
diff --git a/lib/notmuch.h b/lib/notmuch.h
index fe2340b..9a5f9df 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -192,6 +192,13 @@ typedef struct _notmuch_directory notmuch_directory_t;
typedef struct _notmuch_filenames notmuch_filenames_t;
#endif /* __DOXYGEN__ */
+
+notmuch_status_t
+notmuch_config_get (const char *notmuch_path, const char *key, const char **val);
+
+notmuch_status_t
+notmuch_config_set (const char *notmuch_path, const char *key, const char *val);
+
/**
* Create a new, empty notmuch database located at 'path'.
*
diff --git a/test/Makefile.local b/test/Makefile.local
index a2d58fc..8a203f0 100644
--- a/test/Makefile.local
+++ b/test/Makefile.local
@@ -23,6 +23,9 @@ random_corpus_deps = $(dir)/random-corpus.o $(dir)/database-test.o \
lib/libnotmuch.a util/libutil.a \
parse-time-string/libparse-time-string.a
+config_test_deps = $(dir)/config-test.o \
+ lib/libnotmuch.a util/libutil.a
+
$(dir)/random-corpus: $(random_corpus_deps)
$(call quiet,CXX) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS)
@@ -38,6 +41,9 @@ $(dir)/parse-time: $(dir)/parse-time.o parse-time-string/parse-time-string.o
$(dir)/make-db-version: $(dir)/make-db-version.o
$(call quiet,CXX) $^ -o $@ $(XAPIAN_LDFLAGS)
+$(dir)/config-test: $(config_test_deps)
+ $(call quiet,CXX) $(CFLAGS_FINAL) $^ -o $@ $(CONFIGURE_LDFLAGS)
+
.PHONY: test check
test_main_srcs=$(dir)/arg-test.c \
@@ -47,6 +53,7 @@ test_main_srcs=$(dir)/arg-test.c \
$(dir)/smtp-dummy.c \
$(dir)/symbol-test.cc \
$(dir)/make-db-version.cc \
+ $(dir)/config-test.c \
test_srcs=$(test_main_srcs) $(dir)/database-test.c
diff --git a/test/T560-lib-config.sh b/test/T560-lib-config.sh
new file mode 100755
index 0000000..ec8ddbe
--- /dev/null
+++ b/test/T560-lib-config.sh
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+test_description="library config handling"
+
+. ./test-lib.sh
+
+test_begin_subtest "getting and setting"
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch set a foo
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch set a/b bar
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch set b/a fub
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch get a >> OUTPUT
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch get a/b >> OUTPUT
+${TEST_DIRECTORY}/config-test ${MAIL_DIR}/.notmuch get b/a >> OUTPUT
+test_expect_equal "$(cat OUTPUT)" "foobarfub"
+
+test_done
diff --git a/test/config-test.c b/test/config-test.c
new file mode 100644
index 0000000..d9a1116
--- /dev/null
+++ b/test/config-test.c
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include <string.h>
+
+#include "notmuch.h"
+
+int
+main (int argc, char **argv) {
+ const char *val;
+ notmuch_status_t status;
+
+ if (argc == 4 && strcmp (argv[2], "get") == 0) {
+
+ status = notmuch_config_get (argv[1], argv[3], &val);
+ if (status)
+ return status;
+ fputs (val, stdout);
+ return 0;
+
+ } else if (argc == 5 && strcmp (argv[2], "set") == 0) {
+
+ status = notmuch_config_set (argv[1], argv[3], argv[4]);
+ if (status)
+ return status;
+ return 0;
+ }
+
+ return 1;
+}
--
2.1.0
^ permalink raw reply related [flat|nested] 8+ messages in thread