Re: [Patch v8 01/18] parse_tag_line: use enum for return value.
authorJani Nikula <jani@nikula.org>
Sat, 22 Dec 2012 23:47:02 +0000 (01:47 +0200)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:52:38 +0000 (09:52 -0800)
c3/9b63f98171560a298413c03e2bf4c31d8d376f [new file with mode: 0644]

diff --git a/c3/9b63f98171560a298413c03e2bf4c31d8d376f b/c3/9b63f98171560a298413c03e2bf4c31d8d376f
new file mode 100644 (file)
index 0000000..3db7fa7
--- /dev/null
@@ -0,0 +1,363 @@
+Return-Path: <jani@nikula.org>\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 6A666431FAF\r
+       for <notmuch@notmuchmail.org>; Sat, 22 Dec 2012 15:47:10 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] 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 hhiMKGPtjK99 for <notmuch@notmuchmail.org>;\r
+       Sat, 22 Dec 2012 15:47:08 -0800 (PST)\r
+Received: from mail-la0-f52.google.com (mail-la0-f52.google.com\r
+       [209.85.215.52]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id 36940431FAE\r
+       for <notmuch@notmuchmail.org>; Sat, 22 Dec 2012 15:47:08 -0800 (PST)\r
+Received: by mail-la0-f52.google.com with SMTP id l5so6912760lah.25\r
+       for <notmuch@notmuchmail.org>; Sat, 22 Dec 2012 15:47:06 -0800 (PST)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=x-received:from:to:subject:in-reply-to:references:user-agent:date\r
+       :message-id:mime-version:content-type:x-gm-message-state;\r
+       bh=8rUmMmr9Yiv5kYeF7klA3R5enCBtJW7ShuxcvkIzi9g=;\r
+       b=YrkCYzZFAMp8rgPPQAJEiQJZmiA+tvcd42x9CumREnpKWBnFR1NMYAOFYLo4DXCOok\r
+       F1Jhf5G4Vlc5yiRu036YMWMfIDzkdNjPO+XZ5Y4PGv1/dWkMbQ+uy2K2y9CBNB5WN6ak\r
+       aLo//E0UOsWQLSqYSgkG0xRIM5HyPLH6+BgYxzRpXGrUGhcj6otPppIfgqO3nQM/V2/U\r
+       mEnoMJsA/5luQOFCjiRzSyWqTKcEQG1iWq2Ke3BS3C9W0zlfmjFROdxJs+pHBIPV5Ha7\r
+       4fF02vIOC2IsRixzJgwSGBV8JEESxDRAmmYp4DfkRLLzSpSct8nc4O1wknHGVLyzJkBC\r
+       aUqQ==\r
+X-Received: by 10.112.26.70 with SMTP id j6mr5387483lbg.55.1356220026593;\r
+       Sat, 22 Dec 2012 15:47:06 -0800 (PST)\r
+Received: from localhost (dsl-hkibrasgw4-50df51-27.dhcp.inet.fi.\r
+       [80.223.81.27])\r
+       by mx.google.com with ESMTPS id ml1sm6161060lab.15.2012.12.22.15.47.04\r
+       (version=SSLv3 cipher=OTHER); Sat, 22 Dec 2012 15:47:05 -0800 (PST)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: Re: [Patch v8 01/18] parse_tag_line: use enum for return value.\r
+In-Reply-To: <87sj6z7ctm.fsf@zancas.localnet>\r
+References: <1356095307-22895-1-git-send-email-david@tethera.net>\r
+       <87sj6z7ctm.fsf@zancas.localnet>\r
+User-Agent: Notmuch/0.14+211~gc8d6546 (http://notmuchmail.org) Emacs/24.2.1\r
+       (x86_64-pc-linux-gnu)\r
+Date: Sun, 23 Dec 2012 01:47:02 +0200\r
+Message-ID: <87r4mhhcp5.fsf@oiva.home.nikula.org>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-Gm-Message-State:\r
+ ALoCoQkYOiTLus7UXYfZv7WCZOlxuvG/jlEXHmMakFmQOFa/6YC2pnvwChijYYuqjQms3KY0osw6\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: Sat, 22 Dec 2012 23:47:10 -0000\r
+\r
+On Fri, 21 Dec 2012, David Bremner <david@tethera.net> wrote:\r
+> david@tethera.net writes:\r
+>\r
+>> From: David Bremner <bremner@debian.org>\r
+>>\r
+>> This is essentially cosmetic, since success=0 is promised by\r
+>> the comments in tag-utils.h.\r
+>\r
+> In an amazing display of skill and style, in attempting to abort a git\r
+> send-email run so that I could rebase that last fixup away, I managed to\r
+> send the series without the cover letter. So, no point sending the whole\r
+> thing again just for that.\r
+>\r
+> This ever-growing series obsoletes \r
+>\r
+>      id:1355492062-7546-1-git-send-email-david@tethera.net\r
+>\r
+> I think I decided to ignore \r
+>\r
+>       id:871uettxln.fsf@qmul.ac.uk\r
+>\r
+> Perhaps that should be documented, I'm not sure. Since the batch tagging\r
+> interface also allows you to remove strangely named tags, I think it is\r
+> ok.\r
+>\r
+> Hopefully the new man page clears up what is and isn't allowed for\r
+> unencoded characters. Since spaces are the only thing used as\r
+> (top-level) delimiters now, they are the only thing "compressed" by\r
+> strtok_len.\r
+>\r
+> For id:87vcc2q5n2.fsf@nikula.org, there is a seperate series\r
+> id:1355716788-2940-1-git-send-email-david@tethera.net to fix the memory\r
+> leak/allocation confusion. This series needs to be rebased onto the\r
+> final version of that one. I decided _not_ to relax the requirement that\r
+> the ':' marking a prefix be un-encoded, but rather update the\r
+> documentation.\r
+\r
+I think the series looks good, apart from the clarifications I asked for\r
+in [1], and the rebase on the other series. Thanks for your persistence\r
+in following through with this. It has proven to be much more work than\r
+I envisioned when I sent the first patches.\r
+\r
+BR,\r
+Jani.\r
+\r
+\r
+[1] id:87txrdhd7g.fsf@oiva.home.nikula.org\r
+\r
+\r
+>\r
+> A summary of changes to the series follows; I left out the interdiff for\r
+> the notmuch-tag man page, and things that got their own new commit.\r
+>\r
+> ----------------------------------------------------------------------\r
+> commit 30adcab7678296b22d86da06d472c3920c336747\r
+> Author: David Bremner <bremner@debian.org>\r
+> Date:   Sat Dec 15 15:17:40 2012 -0400\r
+>\r
+>     fixup: clarify TAG_FLAG_ID_ONLY comments and name\r
+>\r
+> diff --git a/notmuch-restore.c b/notmuch-restore.c\r
+> index 112f2f3..1b66e76 100644\r
+> --- a/notmuch-restore.c\r
+> +++ b/notmuch-restore.c\r
+> @@ -208,7 +208,7 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
+>      if (input_format == DUMP_FORMAT_SUP) {\r
+>          ret = parse_sup_line (ctx, line, &query_string, tag_ops);\r
+>      } else {\r
+> -        ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS | TAG_FLAG_ID_ONLY,\r
+> +        ret = parse_tag_line (ctx, line, TAG_FLAG_BE_GENEROUS | TAG_FLAG_ID_DIRECT,\r
+>                                &query_string, tag_ops);\r
+>      }\r
+>  \r
+> diff --git a/tag-util.c b/tag-util.c\r
+> index 8fea76c..37bffd5 100644\r
+> --- a/tag-util.c\r
+> +++ b/tag-util.c\r
+> @@ -201,7 +201,7 @@ parse_tag_line (void *ctx, char *line,\r
+>      }\r
+>  \r
+>      /* tok now points to the query string */\r
+> -    if (flags & TAG_FLAG_ID_ONLY) {\r
+> +    if (flags & TAG_FLAG_ID_DIRECT) {\r
+>      /* this is under the assumption that any whitespace in the\r
+>       * message-id must be hex-encoded. The check is probably not\r
+>       * perfect for exotic unicode whitespace; as fallback the\r
+> diff --git a/tag-util.h b/tag-util.h\r
+> index 7674051..eec00cf 100644\r
+> --- a/tag-util.h\r
+> +++ b/tag-util.h\r
+> @@ -28,8 +28,10 @@ typedef enum {\r
+>       */\r
+>      TAG_FLAG_BE_GENEROUS = (1 << 3),\r
+>  \r
+> -    /* Query consists of a single id:$message-id */\r
+> -    TAG_FLAG_ID_ONLY = (1 << 4)\r
+> +    /* Directly look up messages by hex-decoded message-id, rather\r
+> +     * than parsing a general query. The query MUST be of the form\r
+> +     * id:$message-id. */\r
+> +    TAG_FLAG_ID_DIRECT = (1 << 4)\r
+>  \r
+>  } tag_op_flag_t;\r
+>  \r
+>\r
+> commit 256ec05a347b949e6b2bda0d2b6902ed8ab3fab3\r
+> Author: David Bremner <bremner@debian.org>\r
+> Date:   Sat Dec 15 19:54:05 2012 -0400\r
+>\r
+>     fixup for id:87sj778ajb.fsf@qmul.ac.uk and id:87zk1fot39.fsf@nikula.org\r
+>\r
+> diff --git a/notmuch-tag.c b/notmuch-tag.c\r
+> index 44b5bb4..5c8bad4 100644\r
+> --- a/notmuch-tag.c\r
+> +++ b/notmuch-tag.c\r
+> @@ -65,14 +65,16 @@ _optimize_tag_query (void *ctx, const char *orig_query_string,\r
+>      else\r
+>      query_string = talloc_asprintf (ctx, "( %s ) and (", orig_query_string);\r
+>  \r
+> -\r
+>      /* Boolean terms surrounded by double quotes can contain any\r
+>       * character.  Double quotes are quoted by doubling them. */\r
+>  \r
+>      for (i = 0; i < tag_op_list_size (list) && query_string; i++) {\r
+> -    double_quote_str (ctx,\r
+> -                      tag_op_list_tag (list, i),\r
+> -                      &escaped, &escaped_len);\r
+> +    /* XXX in case of OOM, query_string will be deallocated when\r
+> +     * ctx is, which might be at shutdown */\r
+> +    if (double_quote_str (ctx,\r
+> +                          tag_op_list_tag (list, i),\r
+> +                          &escaped, &escaped_len))\r
+> +        return NULL;\r
+>  \r
+>      query_string = talloc_asprintf_append_buffer (\r
+>          query_string, "%s%stag:%s", join,\r
+> diff --git a/util/string-util.c b/util/string-util.c\r
+> index ea7c25b..b9039f4 100644\r
+> --- a/util/string-util.c\r
+> +++ b/util/string-util.c\r
+> @@ -46,8 +46,11 @@ double_quote_str (void *ctx, const char *str,\r
+>      for (in = str; *in; in++)\r
+>      needed += (*in == '"') ? 2 : 1;\r
+>  \r
+> -    if (needed > *len)\r
+> -    *buf = talloc_realloc (ctx, *buf, char, 2*needed);\r
+> +    if ((*buf == NULL) || (needed > *len)) {\r
+> +    *len = 2 * needed;\r
+> +    *buf = talloc_realloc (ctx, *buf, char, *len);\r
+> +    }\r
+> +\r
+>  \r
+>      if (! *buf)\r
+>      return 1;\r
+> @@ -62,7 +65,7 @@ double_quote_str (void *ctx, const char *str,\r
+>      *out++ = *in++;\r
+>      }\r
+>      *out++ = '"';\r
+> -    *out = 0;\r
+> +    *out = '\0';\r
+>  \r
+>      return 0;\r
+>  }\r
+> diff --git a/util/string-util.h b/util/string-util.h\r
+> index b593bc7..4fc7942 100644\r
+> --- a/util/string-util.h\r
+> +++ b/util/string-util.h\r
+> @@ -23,7 +23,7 @@ char *strtok_len (char *s, const char *delim, size_t *len);\r
+>   * Any internal double-quotes are doubled, i.e. a"b -> "a""b"\r
+>   *\r
+>   * Output is into buf; it may be talloc_realloced\r
+> - * return 0 on success, non-zero on failure.\r
+> + * Return: 0 on success, non-zero on memory allocation failure.\r
+>   */\r
+>  int double_quote_str (void *talloc_ctx, const char *str,\r
+>                    char **buf, size_t *len);\r
+>\r
+> commit 4b26ac6f99c74cced64cfae317e6ac4b6a8f706f\r
+> Author: David Bremner <bremner@debian.org>\r
+> Date:   Mon Dec 17 23:36:30 2012 -0400\r
+>\r
+>     fixup: rewrite decode+quote function for queries.\r
+>     \r
+>     Rather than splitting on ':' at the top level, we examine each space\r
+>     delimited token for a prefix.\r
+>\r
+> diff --git a/tag-util.c b/tag-util.c\r
+> index 37bffd5..b0a846b 100644\r
+> --- a/tag-util.c\r
+> +++ b/tag-util.c\r
+> @@ -56,9 +56,14 @@ illegal_tag (const char *tag, notmuch_bool_t remove)\r
+>      return NULL;\r
+>  }\r
+>  \r
+> +/* Input is a hex encoded string, presumed to be a query for Xapian.\r
+> + *\r
+> + * Space delimited tokens are decoded and quoted, with '*' and prefixes\r
+> + * of the form "foo:" passed through unquoted.\r
+> + */\r
+>  static tag_parse_status_t\r
+> -quote_and_decode_query (void *ctx, char *encoded, const char *line_for_error,\r
+> -                    char **query_string)\r
+> +unhex_and_quote (void *ctx, char *encoded, const char *line_for_error,\r
+> +             char **query_string)\r
+>  {\r
+>      char *tok = encoded;\r
+>      size_t tok_len = 0;\r
+> @@ -68,14 +73,43 @@ quote_and_decode_query (void *ctx, char *encoded, const char *line_for_error,\r
+>  \r
+>      *query_string = talloc_strdup (ctx, "");\r
+>  \r
+> -    while (*query_string &&\r
+> -       (tok = strtok_len (tok + tok_len, ": ", &tok_len)) != NULL) {\r
+> -    char delim = tok[tok_len];\r
+> +    while ((tok = strtok_len (tok + tok_len, " ", &tok_len)) != NULL) {\r
+> +\r
+> +    size_t prefix_len;\r
+> +    char delim = *(tok + tok_len);\r
+>  \r
+>      *(tok + tok_len++) = '\0';\r
+>  \r
+> -    if (strcspn (tok, "%") < tok_len - 1) {\r
+> -        /* something to decode */\r
+> +    prefix_len = hex_invariant (tok, tok_len);\r
+> +\r
+> +    if ((strcmp (tok, "*") == 0) || prefix_len >= tok_len - 1) {\r
+> +\r
+> +        /* pass some things through without quoting or decoding.\r
+> +         * Note for '*' this is mandatory.\r
+> +         */\r
+> +\r
+> +        if (! (*query_string = talloc_asprintf_append_buffer (\r
+> +                   *query_string, "%s%c", tok, delim))) {\r
+> +\r
+> +            ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
+> +                              line_for_error, "aborting");\r
+> +            goto DONE;\r
+> +        }\r
+> +\r
+> +    } else {\r
+> +        /* potential prefix: one for ':', then something after */\r
+> +        if ((tok_len - prefix_len > 2) && *(tok + prefix_len) == ':') {\r
+> +            if (! (*query_string = talloc_strndup_append (*query_string,\r
+> +                                                          tok,\r
+> +                                                          prefix_len + 1))) {\r
+> +                ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
+> +                                  line_for_error, "aborting");\r
+> +                goto DONE;\r
+> +            }\r
+> +            tok += prefix_len + 1;\r
+> +            tok_len -= prefix_len + 1;\r
+> +        }\r
+> +\r
+>          if (hex_decode_inplace (tok) != HEX_SUCCESS) {\r
+>              ret = line_error (TAG_PARSE_INVALID, line_for_error,\r
+>                                "hex decoding of token '%s' failed", tok);\r
+> @@ -87,17 +121,14 @@ quote_and_decode_query (void *ctx, char *encoded, const char *line_for_error,\r
+>                                line_for_error, "aborting");\r
+>              goto DONE;\r
+>          }\r
+> -        *query_string = talloc_asprintf_append_buffer (\r
+> -            *query_string, "%s%c", buf, delim);\r
+>  \r
+> -    } else {\r
+> -        /* This is not just an optimization, but used to preserve\r
+> -         * prefixes like id:, which cannot be quoted.\r
+> -         */\r
+> -        *query_string = talloc_asprintf_append_buffer (\r
+> -            *query_string, "%s%c", tok, delim);\r
+> +        if (! (*query_string = talloc_asprintf_append_buffer (\r
+> +                   *query_string, "%s%c", buf, delim))) {\r
+> +            ret = line_error (TAG_PARSE_OUT_OF_MEMORY,\r
+> +                              line_for_error, "aborting");\r
+> +            goto DONE;\r
+> +        }\r
+>      }\r
+> -\r
+>      }\r
+>  \r
+>    DONE:\r
+> @@ -220,7 +251,7 @@ parse_tag_line (void *ctx, char *line,\r
+>      /* skip 'id:' */\r
+>      *query_string = tok + 3;\r
+>      } else {\r
+> -    ret = quote_and_decode_query (ctx, tok, line_for_error, query_string);\r
+> +    ret = unhex_and_quote (ctx, tok, line_for_error, query_string);\r
+>      }\r
+>  \r
+>    DONE:\r
+>\r
+>\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r