--- /dev/null
+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 arlo.cworth.org (Postfix) with ESMTP id 3207B6DE02C6\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:31:03 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.557\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.557 tagged_above=-999 required=5 tests=[AWL=0.163,\r
+ DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_LOW=-0.7,\r
+ RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01] autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 19pRkScxQ1N8 for <notmuch@notmuchmail.org>;\r
+ Fri, 15 Apr 2016 12:30:55 -0700 (PDT)\r
+Received: from mail-wm0-f67.google.com (mail-wm0-f67.google.com\r
+ [74.125.82.67]) by arlo.cworth.org (Postfix) with ESMTPS id 15DFF6DE02C9 for\r
+ <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:44 -0700 (PDT)\r
+Received: by mail-wm0-f67.google.com with SMTP id a140so8599757wma.2\r
+ for <notmuch@notmuchmail.org>; Fri, 15 Apr 2016 12:30:44 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=nikula-org.20150623.gappssmtp.com; s=20150623;\r
+ h=from:to:cc:subject:date:message-id:in-reply-to:references\r
+ :in-reply-to:references;\r
+ bh=IjAFeeLPjOVUzS/KPfOPJ+kDhtHgyR+uCtBOblUYM7A=;\r
+ b=ni9Kv1zzdoOTsYaIIXmIs7NZE9DIxC7D1W5jfX8N8Wz4Nn4kG9qQPDA+pg7t7P9TXc\r
+ rEbRhw9o5mOcE0mzLfATlz9dM96uHTvzQNc32KLCUr5NTXg3PwtWgPt2gs/8RmxO7sfs\r
+ NNF9XOAXk6hIhLlSrr+8ZkObbHlB4z9i9M9UES1CvS9unsMcCb6sMYzinpPCuGZjjVZ4\r
+ wf4eWAwa7hAMXO0SBt56cZe3vODxk038uFvRKK6EtsE3c27NNtsOFFB6B/7B2y8HenPk\r
+ GU2iGtZOZS5RMbb1sw0/eWu070nrDxpnInjZ+aAddLd+ollmv4E4Fx5NT3e8+MSSOJvo\r
+ Ehpg==\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+ d=1e100.net; s=20130820;\r
+ h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\r
+ :references:in-reply-to:references;\r
+ bh=IjAFeeLPjOVUzS/KPfOPJ+kDhtHgyR+uCtBOblUYM7A=;\r
+ b=i3ND83O5lWSb2a+Fa0mR564kXkoBf4IRNGTXSywIWQt7f/HJKvNir7NzVW2iZ4bUdU\r
+ Iev0dDAyeEicjXqPlS76gELCBqMdh3HU7zhZy++GcSZZCAPFOTI/9SW9gVKwkXxJoyos\r
+ 9hanvG5T2TqS7UeL7ZFGqYEuQqwGEA+WnW0U6t41q1rKxyZpLa7ejouN5rqfPv1d1EPe\r
+ Malu32havTn6frDnpw+RtqeIw+OxngvcFS6h99iMS9PDWiJIYBp+ArsSa0ih7Z7njTkV\r
+ tyxRdQG+hjz8jl5iJ+WXfW9cyKk3f1YAFl9OPxRc+pPrURazE8z867sw5kQr98z3o5VZ\r
+ w7AQ==\r
+X-Gm-Message-State:\r
+ AOPr4FVk90nbjxP3QhO+hNUtoWzJrJnsmetLCGwuk3fK6ossjEwCC08NKy17GHcX0RVQCw==\r
+X-Received: by 10.28.101.213 with SMTP id z204mr6376130wmb.22.1460748642753;\r
+ Fri, 15 Apr 2016 12:30:42 -0700 (PDT)\r
+Received: from localhost (mobile-access-bcee7f-102.dhcp.inet.fi.\r
+ [188.238.127.102])\r
+ by smtp.gmail.com with ESMTPSA id w75sm39823395wmw.4.2016.04.15.12.30.41\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Fri, 15 Apr 2016 12:30:42 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [RFC PATCH 5/5] cli: convert count_files to new scandir\r
+Date: Fri, 15 Apr 2016 22:29:19 +0300\r
+Message-Id:\r
+ <6aa9bcd2ce29f185f68b06c4684c65d900fdb85d.1460748142.git.jani@nikula.org>\r
+X-Mailer: git-send-email 2.1.4\r
+In-Reply-To: <cover.1460748142.git.jani@nikula.org>\r
+References: <cover.1460748142.git.jani@nikula.org>\r
+In-Reply-To: <cover.1460748142.git.jani@nikula.org>\r
+References: <cover.1460748142.git.jani@nikula.org>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://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: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Fri, 15 Apr 2016 19:31:03 -0000\r
+\r
+Makes sense to use similar code in both places.\r
+---\r
+ notmuch-new.c | 97 +++++++++++++++++++++++++++++++++++------------------------\r
+ 1 file changed, 58 insertions(+), 39 deletions(-)\r
+\r
+diff --git a/notmuch-new.c b/notmuch-new.c\r
+index 262895d466ae..966b89ca39a0 100644\r
+--- a/notmuch-new.c\r
++++ b/notmuch-new.c\r
+@@ -733,64 +733,83 @@ stop_progress_printing_timer (void)\r
+ static void\r
+ count_files (const char *path, int *count, add_files_state_t *state)\r
+ {\r
+- struct dirent *entry = NULL;\r
+- char *next;\r
+- struct dirent **fs_entries = NULL;\r
+- int num_fs_entries = scandir (path, &fs_entries, 0, NULL);\r
+- int entry_type, i;\r
++ char **fs_files = NULL, **fs_subdirs = NULL;\r
++ int num_fs_files = 0, num_fs_subdirs = 0;\r
++ int i;\r
++ notmuch_bool_t is_maildir;\r
++ struct filter filter = {\r
++ .path = path,\r
++ .state = state,\r
++ };\r
+ \r
+- if (num_fs_entries == -1) {\r
++ filter.entry_type = S_IFDIR;\r
++ num_fs_subdirs = scandirx (path, &fs_subdirs, filter_fn, NULL, &filter);\r
++ if (num_fs_subdirs == -1) {\r
+ fprintf (stderr, "Warning: failed to open directory %s: %s\n",\r
+ path, strerror (errno));\r
+ goto DONE;\r
+ }\r
+ \r
+- for (i = 0; i < num_fs_entries && ! interrupted; i++) {\r
+- entry = fs_entries[i];\r
++ is_maildir = _entries_resemble_maildir (fs_subdirs, num_fs_subdirs);\r
++\r
++ /* Recurse to subdirs. */\r
++ for (i = 0; i < num_fs_subdirs && !interrupted; i++) {\r
++ const char *name = fs_subdirs[i];\r
++ char *next;\r
+ \r
+- /* Ignore special directories to avoid infinite recursion.\r
+- * Also ignore the .notmuch directory and files/directories\r
+- * the user has configured to be ignored.\r
++ /*\r
++ * Ignore the .notmuch directory and any "tmp" directory that\r
++ * appears within a maildir.\r
+ */\r
+- if (strcmp (entry->d_name, ".") == 0 ||\r
+- strcmp (entry->d_name, "..") == 0 ||\r
+- strcmp (entry->d_name, ".notmuch") == 0 ||\r
+- _entry_in_ignore_list (entry->d_name, state))\r
+- {\r
+- if (state->debug && _entry_in_ignore_list (entry->d_name, state))\r
+- printf ("(D) count_files: explicitly ignoring %s/%s\n",\r
+- path,\r
+- entry->d_name);\r
++ if ((is_maildir && strcmp (name, "tmp") == 0) ||\r
++ strcmp (name, ".notmuch") == 0)\r
+ continue;\r
+- }\r
+ \r
+- if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) {\r
+- next = NULL;\r
++ next = talloc_asprintf (NULL, "%s/%s", path, name);\r
++ if (!next) {\r
+ fprintf (stderr, "Error descending from %s to %s: Out of memory\n",\r
+- path, entry->d_name);\r
++ path, name);\r
+ continue;\r
+ }\r
++ count_files (next, count, state);\r
++ talloc_free (next);\r
++ next = NULL;\r
++ }\r
+ \r
+- entry_type = dirent_type (path, entry);\r
+- if (entry_type == S_IFREG) {\r
+- *count = *count + 1;\r
+- if (*count % 1000 == 0 && state->verbosity >= VERBOSITY_NORMAL) {\r
+- printf ("Found %d files so far.\r", *count);\r
+- fflush (stdout);\r
+- }\r
+- } else if (entry_type == S_IFDIR) {\r
+- count_files (next, count, state);\r
+- }\r
++ if (interrupted)\r
++ goto DONE;\r
++\r
++ filter.entry_type = S_IFREG;\r
++ num_fs_files = scandirx (path, &fs_files, filter_fn, NULL, &filter);\r
++ if (num_fs_files == -1) {\r
++ fprintf (stderr, "Warning: failed to open directory %s: %s\n",\r
++ path, strerror (errno));\r
++ goto DONE;\r
++ }\r
++\r
++ if (state->verbosity >= VERBOSITY_NORMAL) {\r
++ int new_count = *count + num_fs_files;\r
+ \r
+- free (next);\r
++ if (new_count / 1000 > *count / 1000) {\r
++ printf ("Found %d files so far.\r", new_count / 1000 * 1000);\r
++ fflush (stdout);\r
++ }\r
+ }\r
+ \r
++ *count += num_fs_files;\r
++\r
+ DONE:\r
+- if (fs_entries) {\r
+- for (i = 0; i < num_fs_entries; i++)\r
+- free (fs_entries[i]);\r
++ if (fs_subdirs) {\r
++ for (i = 0; i < num_fs_subdirs; i++)\r
++ free (fs_subdirs[i]);\r
++\r
++ free (fs_subdirs);\r
++ }\r
++ if (fs_files) {\r
++ for (i = 0; i < num_fs_files; i++)\r
++ free (fs_files[i]);\r
+ \r
+- free (fs_entries);\r
++ free (fs_files);\r
+ }\r
+ }\r
+ \r
+-- \r
+2.1.4\r
+\r