Re: [PATCH 08/16] tag-util.[ch]: New files for common tagging routines
authorMark Walters <markwalters1009@gmail.com>
Thu, 22 Nov 2012 12:22:50 +0000 (12:22 +0000)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:50:47 +0000 (09:50 -0800)
90/33cdcfdc06d350855b183063bd7d705e251af3 [new file with mode: 0644]

diff --git a/90/33cdcfdc06d350855b183063bd7d705e251af3 b/90/33cdcfdc06d350855b183063bd7d705e251af3
new file mode 100644 (file)
index 0000000..cff8215
--- /dev/null
@@ -0,0 +1,511 @@
+Return-Path: <m.walters@qmul.ac.uk>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 0E8EA431FB6\r
+       for <notmuch@notmuchmail.org>; Thu, 22 Nov 2012 04:23:05 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+       tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+       NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id eLgKnG8jceqo for <notmuch@notmuchmail.org>;\r
+       Thu, 22 Nov 2012 04:23:03 -0800 (PST)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+       (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id C2CE6431FAF\r
+       for <notmuch@notmuchmail.org>; Thu, 22 Nov 2012 04:23:02 -0800 (PST)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+       by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1TbVnu-0002GW-4k; Thu, 22 Nov 2012 12:22:52 +0000\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+       by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+       (envelope-from <m.walters@qmul.ac.uk>)\r
+       id 1TbVnt-0004DL-Dc; Thu, 22 Nov 2012 12:22:50 +0000\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: david@tethera.net, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 08/16] tag-util.[ch]: New files for common tagging\r
+ routines\r
+In-Reply-To: <1353265498-3839-9-git-send-email-david@tethera.net>\r
+References: <1353265498-3839-1-git-send-email-david@tethera.net>\r
+       <1353265498-3839-9-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.14+81~g9730584 (http://notmuchmail.org) Emacs/23.4.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Thu, 22 Nov 2012 12:22:50 +0000\r
+Message-ID: <87k3tdltd1.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record.  :)\r
+X-QM-Body-MD5: 00d652e871f3b7c3eb9a7da70df9034d (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+       determine if it is\r
+       spam. We require at least 5.0 points to mark a message as spam.\r
+       This message scored -1.8 points.\r
+       Summary of the scoring: \r
+       * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+       *      medium trust\r
+       *      [138.37.6.40 listed in list.dnswl.org]\r
+       * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+       provider *      (markwalters1009[at]gmail.com)\r
+       *  0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+Cc: David Bremner <bremner@debian.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 22 Nov 2012 12:23:05 -0000\r
+\r
+\r
+Hi\r
+\r
+I am slowly working my way through this series. I have some queries\r
+comments and things that confused me in this patch.\r
+\r
+One longer term concern (but I have not got far enough to see if it\r
+could be a problem): could someone use dump and then not be able to\r
+restore because some of the tags are now banned (eg starting with -\r
+etc)? Obviously anything which fails currently is not a concern, only if\r
+some cases stop some currently working cases.\r
+\r
+\r
+One particular comment is that most of the functions do not have a\r
+comment defining them in tag-util.h. So for example (at this point in\r
+reading the series) I am not sure of why we need both parse_tag_line and\r
+tag_op_list_from_string.\r
+\r
+\r
+On Sun, 18 Nov 2012, david@tethera.net wrote:\r
+> From: David Bremner <bremner@debian.org>\r
+>\r
+> These are meant to be shared between notmuch-tag and notmuch-restore.\r
+>\r
+> The bulk of the routines implement a "tag operation list" abstract\r
+> data type act as a structured representation of a set of tag\r
+> operations (typically coming from a single tag command or line of\r
+> input).\r
+> ---\r
+>  Makefile.local |    1 +\r
+>  tag-util.c     |  273 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+>  tag-util.h     |   71 +++++++++++++++\r
+>  3 files changed, 345 insertions(+)\r
+>  create mode 100644 tag-util.c\r
+>  create mode 100644 tag-util.h\r
+>\r
+> diff --git a/Makefile.local b/Makefile.local\r
+> index 2b91946..854867d 100644\r
+> --- a/Makefile.local\r
+> +++ b/Makefile.local\r
+> @@ -274,6 +274,7 @@ notmuch_client_srcs =            \\r
+>      query-string.c          \\r
+>      mime-node.c             \\r
+>      crypto.c                \\r
+> +    tag-util.c\r
+>  \r
+>  notmuch_client_modules = $(notmuch_client_srcs:.c=.o)\r
+>  \r
+> diff --git a/tag-util.c b/tag-util.c\r
+> new file mode 100644\r
+> index 0000000..5329b1f\r
+> --- /dev/null\r
+> +++ b/tag-util.c\r
+> @@ -0,0 +1,273 @@\r
+> +#include "tag-util.h"\r
+> +#include "hex-escape.h"\r
+> +\r
+> +static char *\r
+> +strtok_len (char *s, const char *delim, size_t *len)\r
+> +{\r
+> +    /* skip initial delims */\r
+> +    s += strspn (s, delim);\r
+> +\r
+> +    /* length of token */\r
+> +    *len = strcspn (s, delim);\r
+> +\r
+> +    return *len ? s : NULL;\r
+> +}\r
+> +\r
+> +/* Tag messages according to 'input', which must consist of lines of\r
+> + * the format:\r
+> + *\r
+> + * +<tag>|-<tag> [...] [--] <search-terms>\r
+> + *\r
+> + * Each line is interpreted similarly to "notmuch tag" command line\r
+> + * arguments. The delimiter is one or more spaces ' '. Any characters\r
+> + * in <tag> and <search-terms> MAY be hex encoded with %NN where NN is\r
+> + * the hexadecimal value of the character. Any ' ' and '%' characters\r
+> + * in <tag> and <search-terms> MUST be hex encoded (using %20 and %25,\r
+> + * respectively). Any characters that are not part of <tag> or\r
+> + * <search-terms> MUST NOT be hex encoded.\r
+> + *\r
+> + * Leading and trailing space ' ' is ignored. Empty lines and lines\r
+> + * beginning with '#' are ignored.\r
+> + */\r
+\r
+The comment says lines starting with '#' are ignored but I don't see\r
+anything actually ignoring them.\r
+\r
+> +int\r
+> +parse_tag_line (void *ctx, char *line,\r
+> +            char **query_string,\r
+> +            tag_op_list_t *tag_ops)\r
+> +{\r
+> +    char *tok = line;\r
+> +    size_t tok_len = 0;\r
+> +\r
+> +    chomp_newline (line);\r
+> +    tag_op_list_reset (tag_ops);\r
+> +\r
+> +    /* Parse tags. */\r
+> +    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
+> +    notmuch_bool_t remove;\r
+> +    char *tag;\r
+> +\r
+> +    /* Optional explicit end of tags marker. */\r
+> +    if (strncmp (tok, "--", tok_len) == 0) {\r
+> +        tok = strtok_len (tok + tok_len, " ", &tok_len);\r
+> +        break;\r
+> +    }\r
+> +\r
+> +    /* Implicit end of tags. */\r
+> +    if (*tok != '-' && *tok != '+')\r
+> +        break;\r
+> +\r
+> +    /* If tag is terminated by NUL, there's no query string. */\r
+> +    if (*(tok + tok_len) == '\0') {\r
+> +        tok = NULL;\r
+> +        break;\r
+> +    }\r
+> +\r
+> +    /* Terminate, and start next token after terminator. */\r
+> +    *(tok + tok_len++) = '\0';\r
+> +\r
+> +    remove = (*tok == '-');\r
+> +    tag = tok + 1;\r
+> +\r
+> +    /* Refuse empty tags. */\r
+> +    if (*tag == '\0') {\r
+> +        tok = NULL;\r
+> +        break;\r
+> +    }\r
+> +\r
+> +    /* Decode tag. */\r
+> +    if (hex_decode_inplace (tag) != HEX_SUCCESS) {\r
+> +        tok = NULL;\r
+> +        break;\r
+> +    }\r
+> +\r
+> +    if (tag_op_list_append (ctx, tag_ops, tag, remove))\r
+> +        return -1;\r
+> +    }\r
+> +\r
+> +    if (tok == NULL || tag_ops->count == 0) {\r
+> +    /* FIXME: line has been modified! */\r
+> +    fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
+> +             line);\r
+> +    return 1;\r
+> +    }\r
+> +\r
+> +    /* tok now points to the query string */\r
+> +    if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
+> +    /* FIXME: line has been modified! */\r
+> +    fprintf (stderr, "Warning: Ignoring invalid input line: %s\n",\r
+> +             line);\r
+> +    return 1;\r
+> +    }\r
+> +\r
+> +    *query_string = tok;\r
+> +\r
+> +    return 0;\r
+> +}\r
+> +\r
+> +static inline void\r
+> +message_error (notmuch_message_t *message,\r
+> +           notmuch_status_t status,\r
+> +           const char *format, ...)\r
+> +{\r
+> +    va_list va_args;\r
+> +\r
+> +    va_start (va_args, format);\r
+> +\r
+> +    vfprintf (stderr, format, va_args);\r
+> +    fprintf (stderr, "Message-ID: %s\n", notmuch_message_get_message_id (message));\r
+> +    fprintf (stderr, "Status: %s\n", notmuch_status_to_string (status));\r
+> +}\r
+> +\r
+> +notmuch_status_t\r
+> +tag_op_list_apply (notmuch_message_t *message,\r
+> +               tag_op_list_t *list,\r
+> +               tag_op_flag_t flags)\r
+> +{\r
+> +    int i;\r
+> +\r
+> +    notmuch_status_t status = 0;\r
+> +    tag_operation_t *tag_ops = list->ops;\r
+> +\r
+> +    status = notmuch_message_freeze (message);\r
+> +    if (status) {\r
+> +    message_error (message, status, "freezing message");\r
+> +    return status;\r
+> +    }\r
+> +\r
+> +    if (flags & TAG_FLAG_REMOVE_ALL) {\r
+> +    status = notmuch_message_remove_all_tags (message);\r
+> +    if (status) {\r
+> +        message_error (message, status, "removing all tags" );\r
+> +        return status;\r
+> +    }\r
+> +    }\r
+> +\r
+> +    for (i = 0; i < list->count; i++) {\r
+> +    if (tag_ops[i].remove) {\r
+> +        status = notmuch_message_remove_tag (message, tag_ops[i].tag);\r
+> +        if (status) {\r
+> +            message_error (message, status, "removing tag %s", tag_ops[i].tag);\r
+> +            return status;\r
+> +        }\r
+> +    } else {\r
+> +        status = notmuch_message_add_tag (message, tag_ops[i].tag);\r
+> +        if (status) {\r
+> +            message_error (message, status, "adding tag %s", tag_ops[i].tag);\r
+> +            return status;\r
+> +        }\r
+> +\r
+> +    }\r
+> +    }\r
+> +\r
+> +    status = notmuch_message_thaw (message);\r
+> +    if (status) {\r
+> +    message_error (message, status, "thawing message");\r
+> +    return status;\r
+> +    }\r
+> +\r
+> +\r
+> +    if (flags & TAG_FLAG_MAILDIR_SYNC) {\r
+> +    status = notmuch_message_tags_to_maildir_flags (message);\r
+> +    if (status) {\r
+> +        message_error (message, status, "synching tags to maildir");\r
+> +        return status;\r
+> +    }\r
+> +    }\r
+> +\r
+> +    return NOTMUCH_STATUS_SUCCESS;\r
+> +\r
+> +}\r
+> +\r
+> +\r
+> +/* Array of tagging operations (add or remove), terminated with an\r
+> + * empty element. Size will be increased as necessary. */\r
+\r
+Is the list termination used (above you used list->count) or is it just\r
+that you can always safely write to the last element?\r
+\r
+> +\r
+> +tag_op_list_t *\r
+> +tag_op_list_create (void *ctx)\r
+> +{\r
+> +    tag_op_list_t *list;\r
+> +\r
+> +    list = talloc (ctx, tag_op_list_t);\r
+> +    if (list == NULL)\r
+> +    return NULL;\r
+> +\r
+> +    list->size = TAG_OP_LIST_INITIAL_SIZE;\r
+> +    list->count = 0;\r
+> +\r
+> +    list->ops = talloc_array (ctx, tag_operation_t, list->size);\r
+> +    if (list->ops == NULL)\r
+> +    return NULL;\r
+> +\r
+> +    return list;\r
+> +}\r
+> +\r
+> +\r
+> +int\r
+> +tag_op_list_append (void *ctx,\r
+> +                tag_op_list_t *list,\r
+> +                const char *tag,\r
+> +                notmuch_bool_t remove)\r
+> +{\r
+> +    list->ops[list->count].tag = tag;\r
+> +    list->ops[list->count].remove = remove;\r
+> +    list->count++;\r
+> +\r
+> +    /* Make room for terminating empty element and potential\r
+> +     * new tags, if necessary. This should be a fairly rare\r
+> +     * case, considering the initial array size. */\r
+> +    if (list->count == list->size) {\r
+> +    list->size *= 2;\r
+> +    list->ops = talloc_realloc (ctx, list->ops, tag_operation_t,\r
+> +                                list->size);\r
+> +    if (list->ops == NULL) {\r
+> +        fprintf (stderr, "Out of memory.\n");\r
+> +        return 1;\r
+> +    }\r
+> +    }\r
+> +\r
+> +    list->ops[list->count].tag = NULL;\r
+> +\r
+> +    return 0;\r
+> +}\r
+> +\r
+> +\r
+> +/* WARNING: modifies it's string argument */\r
+> +\r
+> +int\r
+> +tag_op_list_from_string (void *ctx,\r
+> +                     char *tag_string,\r
+> +                     notmuch_bool_t remove,\r
+> +                     tag_op_list_t *tag_ops)\r
+\r
+As mentioned above I wasn't sure why both this and parse_tag_line are\r
+needed. Also should one use the other? Otherwise we could get different\r
+allowed tags in different places?\r
+\r
+> +{\r
+> +    char *tok = tag_string;\r
+> +    size_t tok_len = 0;\r
+> +\r
+> +    tag_op_list_reset (tag_ops);\r
+> +\r
+> +/* Parse tags. */\r
+> +    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
+> +\r
+> +    if (*(tok + tok_len) != '\0') {\r
+> +        *(tok + tok_len) = '\0';\r
+> +        tok_len++;\r
+> +    }\r
+> +\r
+> +    if (tag_op_list_append (ctx, tag_ops, tok, remove))\r
+> +        return -1;\r
+> +    }\r
+> +\r
+> +    return 0;\r
+> +\r
+> +}\r
+> +\r
+> +notmuch_bool_t\r
+> +tag_op_list_empty (tag_op_list_t *list)\r
+> +{\r
+> +    return (list->count == 0);\r
+> +}\r
+> +\r
+> +\r
+> +void\r
+> +tag_op_list_reset (tag_op_list_t *list)\r
+> +{\r
+> +    list->count = 0;\r
+> +}\r
+> diff --git a/tag-util.h b/tag-util.h\r
+> new file mode 100644\r
+> index 0000000..b381b8e\r
+> --- /dev/null\r
+> +++ b/tag-util.h\r
+> @@ -0,0 +1,71 @@\r
+> +#ifndef _TAG_UTIL_H\r
+> +#define _TAG_UTIL_H\r
+> +\r
+> +#include "notmuch-client.h"\r
+> +\r
+> +typedef struct {\r
+> +    const char *tag;\r
+> +    notmuch_bool_t remove;\r
+> +} tag_operation_t;\r
+> +\r
+> +#define TAG_OP_LIST_INITIAL_SIZE 10\r
+> +\r
+> +typedef struct {\r
+> +    tag_operation_t *ops;\r
+> +    int count;\r
+> +    int size;\r
+> +} tag_op_list_t;\r
+\r
+I thought it would be helpful to say something about *ops: e.g., "ops\r
+points to the first element of an array of count tag_operation_t's. This\r
+array should be empty terminated (if true)."\r
+\r
+One trivial comment for the next patch: the commit message says "notmuch\r
+new" rather than "notmuch tag".\r
+\r
+Best wishes\r
+\r
+Mark\r
\r
+> +\r
+> +/* Use powers of 2 */\r
+> +typedef enum  { TAG_FLAG_NONE = 0,\r
+> +            TAG_FLAG_MAILDIR_SYNC = 1,\r
+> +            TAG_FLAG_REMOVE_ALL = 2 } tag_op_flag_t;\r
+> +\r
+> +\r
+> +typedef int (*tag_callback_t)(void *ctx,\r
+> +                          notmuch_database_t *notmuch,\r
+> +                          const char *query_string,\r
+> +                          tag_op_list_t *tag_ops,\r
+> +                          tag_op_flag_t flags);\r
+> +\r
+> +int\r
+> +parse_tag_stream (void *ctx,\r
+> +              notmuch_database_t *notmuch,\r
+> +              FILE *input,\r
+> +              tag_callback_t callback,\r
+> +              tag_op_flag_t flags,\r
+> +              volatile sig_atomic_t *interrupted);\r
+> +\r
+> +/*\r
+> + * Returns: 0 for OK, 1 for skipped line, -1 for fatal(ish) error.\r
+> + */\r
+> +int\r
+> +parse_tag_line (void *ctx, char *line, char **query_str, tag_op_list_t *ops);\r
+> +\r
+> +tag_op_list_t\r
+> +*tag_op_list_create (void *ctx);\r
+> +\r
+> +int\r
+> +tag_op_list_append (void *ctx,\r
+> +                tag_op_list_t *list,\r
+> +                const char *tag,\r
+> +                notmuch_bool_t remove);\r
+> +\r
+> +notmuch_status_t\r
+> +tag_op_list_apply (notmuch_message_t *message,\r
+> +               tag_op_list_t *tag_ops,\r
+> +               tag_op_flag_t flags);\r
+> +\r
+> +int\r
+> +tag_op_list_from_string (void *ctx,\r
+> +                     char *tag_string,\r
+> +                     notmuch_bool_t remove,\r
+> +                     tag_op_list_t *tag_ops);\r
+> +\r
+> +notmuch_bool_t\r
+> +tag_op_list_empty (tag_op_list_t *list);\r
+> +\r
+> +void\r
+> +tag_op_list_reset (tag_op_list_t *list);\r
+> +\r
+> +#endif\r
+> -- \r
+> 1.7.10.4\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r