-
- path_mtime = st.st_mtime;
-
- path_dbtime = notmuch_database_get_timestamp (notmuch, path);
-
- dir = opendir (path);
- if (dir == NULL) {
- fprintf (stderr, "Error opening directory %s: %s\n",
- path, strerror (errno));
- ret = NOTMUCH_STATUS_FILE_ERROR;
- goto DONE;
- }
-
- entry_length = offsetof (struct dirent, d_name) +
- pathconf (path, _PC_NAME_MAX) + 1;
- entry = malloc (entry_length);
-
- while (1) {
- err = readdir_r (dir, entry, &e);
- if (err) {
- fprintf (stderr, "Error reading directory: %s\n",
- strerror (errno));
- ret = NOTMUCH_STATUS_FILE_ERROR;
- goto DONE;
- }
-
- if (e == NULL)
- break;
-
- /* If this directory hasn't been modified since the last
- * add_files, then we only need to look further for
- * sub-directories. */
- if (path_mtime <= path_dbtime && entry->d_type != DT_DIR)
- continue;
-
- /* Ignore special directories to avoid infinite recursion.
- * Also ignore the .notmuch directory.
- */
- /* XXX: Eventually we'll want more sophistication to let the
- * user specify files to be ignored. */
- if (strcmp (entry->d_name, ".") == 0 ||
- strcmp (entry->d_name, "..") == 0 ||
- strcmp (entry->d_name, ".notmuch") ==0)
- {
- continue;
- }
-
- next = g_strdup_printf ("%s/%s", path, entry->d_name);
-
- if (stat (next, &st)) {
- fprintf (stderr, "Error reading %s: %s\n",
- next, strerror (errno));
- ret = NOTMUCH_STATUS_FILE_ERROR;
- continue;
- }
-
- if (S_ISREG (st.st_mode)) {
- /* If the file hasn't been modified since the last
- * add_files, then we need not look at it. */
- if (st.st_mtime > path_dbtime) {
- state->processed_files++;
-
- status = notmuch_database_add_message (notmuch, next);
- switch (status) {
- /* success */
- case NOTMUCH_STATUS_SUCCESS:
- state->added_messages++;
- break;
- /* Non-fatal issues (go on to next file) */
- case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
- /* Stay silent on this one. */
- break;
- case NOTMUCH_STATUS_FILE_NOT_EMAIL:
- fprintf (stderr, "Note: Ignoring non-mail file: %s\n",
- next);
- break;
- /* Fatal issues. Don't process anymore. */
- case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
- fprintf (stderr, "A Xapian error was encountered. Halting processing.\n");
- ret = status;
- goto DONE;
- default:
- fprintf (stderr, "Internal error: add_message returned unexpected value: %d\n", status);
- ret = status;
- goto DONE;
- }
- if (state->processed_files % 1000 == 0)
- add_files_print_progress (state);
- }
- } else if (S_ISDIR (st.st_mode)) {
- status = add_files (notmuch, next, state);
- if (status && ret == NOTMUCH_STATUS_SUCCESS)
- ret = status;
- }
-
- free (next);
- next = NULL;
- }
-
- notmuch_database_set_timestamp (notmuch, path, path_mtime);
-
- DONE:
- if (next)
- free (next);
- if (entry)
- free (entry);
- if (dir)
- closedir (dir);
-
- return ret;