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