From: Carl Worth Date: Wed, 6 Jan 2010 00:06:46 +0000 (-0800) Subject: notmuch new: Remove an unnecessary stat of every regular file in the mail store. X-Git-Tag: 0.1~175 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=2c4555f1a56602ff1dd55a63699810522ba4d91e;p=notmuch.git notmuch new: Remove an unnecessary stat of every regular file in the mail store. We were previousl using the stat for two reasons. One was to obtain the mtime of the file. This usage was removed in the previous commit, (since the mtime is unreliable in the case of a file being moved into the mail store). The second reason was to identify regular and directory file types. But this information is already available in the result we get from scandir. What's left is simply a stat for each directory in the mailstore, (which we are still using to compare filesystem mtime with the mtime stored in the database). --- diff --git a/notmuch-new.c b/notmuch-new.c index fe280d84..490101d0 100644 --- a/notmuch-new.c +++ b/notmuch-new.c @@ -141,7 +141,6 @@ is_maildir (struct dirent **entries, int count) static notmuch_status_t add_files_recursive (notmuch_database_t *notmuch, const char *path, - struct stat *st, add_files_state_t *state) { DIR *dir = NULL; @@ -153,8 +152,20 @@ add_files_recursive (notmuch_database_t *notmuch, struct dirent **namelist = NULL; int num_entries; notmuch_directory_t *directory; + struct stat st; - fs_mtime = st->st_mtime; + if (stat (path, &st)) { + fprintf (stderr, "Error reading directory %s: %s\n", + path, strerror (errno)); + return NOTMUCH_STATUS_FILE_ERROR; + } + + if (! S_ISDIR (st.st_mode)) { + fprintf (stderr, "Error: %s is not a directory.\n", path); + return NOTMUCH_STATUS_FILE_ERROR; + } + + fs_mtime = st.st_mtime; directory = notmuch_database_get_directory (notmuch, path); db_mtime = notmuch_directory_get_mtime (directory); @@ -199,25 +210,7 @@ add_files_recursive (notmuch_database_t *notmuch, next = talloc_asprintf (notmuch, "%s/%s", path, entry->d_name); - if (stat (next, st)) { - int err = errno; - - switch (err) { - case ENOENT: - /* The file was removed between scandir and now... */ - case EPERM: - case EACCES: - /* We can't read this file so don't add it to the cache. */ - continue; - } - - fprintf (stderr, "Error reading %s: %s\n", - next, strerror (errno)); - ret = NOTMUCH_STATUS_FILE_ERROR; - goto DONE; - } - - if (S_ISREG (st->st_mode)) { + if (entry->d_type == DT_REG) { state->processed_files++; if (state->verbose) { @@ -275,8 +268,8 @@ add_files_recursive (notmuch_database_t *notmuch, do_add_files_print_progress = 0; add_files_print_progress (state); } - } else if (S_ISDIR (st->st_mode)) { - status = add_files_recursive (notmuch, next, st, state); + } else if (entry->d_type == DT_DIR) { + status = add_files_recursive (notmuch, next, state); if (status && ret == NOTMUCH_STATUS_SUCCESS) ret = status; } @@ -312,23 +305,11 @@ add_files (notmuch_database_t *notmuch, const char *path, add_files_state_t *state) { - struct stat st; notmuch_status_t status; struct sigaction action; struct itimerval timerval; notmuch_bool_t timer_is_active = FALSE; - if (stat (path, &st)) { - fprintf (stderr, "Error reading directory %s: %s\n", - path, strerror (errno)); - return NOTMUCH_STATUS_FILE_ERROR; - } - - if (! S_ISDIR (st.st_mode)) { - fprintf (stderr, "Error: %s is not a directory.\n", path); - return NOTMUCH_STATUS_FILE_ERROR; - } - if (state->output_is_a_tty && ! debugger_is_active () && ! state->verbose) { /* Setup our handler for SIGALRM */ memset (&action, 0, sizeof (struct sigaction)); @@ -347,7 +328,7 @@ add_files (notmuch_database_t *notmuch, timer_is_active = TRUE; } - status = add_files_recursive (notmuch, path, &st, state); + status = add_files_recursive (notmuch, path, state); if (timer_is_active) { /* Now stop the timer. */