X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=notmuch-setup.c;h=622bbaa68343c7633ade295d794e86e66c64d137;hp=2c3404ff67b6c0d36c511b997185400713d8dd09;hb=bb5211684654b7cf54f842990a733a64fe01d612;hpb=6bd01e1b340f6a209dde64471bc9d7137511dada;ds=sidebyside diff --git a/notmuch-setup.c b/notmuch-setup.c index 2c3404ff..622bbaa6 100644 --- a/notmuch-setup.c +++ b/notmuch-setup.c @@ -20,131 +20,6 @@ #include "notmuch-client.h" -static notmuch_status_t -add_all_files (notmuch_database_t *notmuch, - const char *mail_directory, - int num_files) -{ - add_files_state_t add_files_state; - double elapsed; - struct timeval tv_now; - notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS; - - add_files_state.ignore_read_only_directories = FALSE; - add_files_state.saw_read_only_directory = FALSE; - add_files_state.total_files = num_files; - add_files_state.processed_files = 0; - add_files_state.added_messages = 0; - add_files_state.callback = NULL; - gettimeofday (&add_files_state.tv_start, NULL); - - ret = add_files (notmuch, mail_directory, &add_files_state); - - gettimeofday (&tv_now, NULL); - elapsed = notmuch_time_elapsed (add_files_state.tv_start, - tv_now); - printf ("Processed %d %s in ", add_files_state.processed_files, - add_files_state.processed_files == 1 ? - "file" : "total files"); - notmuch_time_print_formatted_seconds (elapsed); - if (elapsed > 1) { - printf (" (%d files/sec.). \n", - (int) (add_files_state.processed_files / elapsed)); - } else { - printf (". \n"); - } - if (add_files_state.added_messages) { - printf ("Added %d %s to the database.\n\n", - add_files_state.added_messages, - add_files_state.added_messages == 1 ? - "message" : "unique messages"); - } - - return ret; -} - - -/* XXX: This should be merged with the existing add_files function in - * add-files.c. */ -/* Recursively count all regular files in path and all sub-direcotries - * of path. The result is added to *count (which should be - * initialized to zero by the top-level caller before calling - * count_files). */ -static void -count_files (const char *path, int *count) -{ - DIR *dir; - struct dirent *e, *entry = NULL; - int entry_length; - int err; - char *next; - struct stat st; - - dir = opendir (path); - - if (dir == NULL) { - fprintf (stderr, "Warning: failed to open directory %s: %s\n", - path, strerror (errno)); - 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)); - free (entry); - goto DONE; - } - - if (e == NULL) - break; - - /* 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; - } - - if (asprintf (&next, "%s/%s", path, entry->d_name) == -1) { - next = NULL; - fprintf (stderr, "Error descending from %s to %s: Out of memory\n", - path, entry->d_name); - continue; - } - - stat (next, &st); - - if (S_ISREG (st.st_mode)) { - *count = *count + 1; - if (*count % 1000 == 0) { - printf ("Found %d files so far.\r", *count); - fflush (stdout); - } - } else if (S_ISDIR (st.st_mode)) { - count_files (next, count); - } - - free (next); - } - - DONE: - if (entry) - free (entry); - - closedir (dir); -} - static const char * make_path_absolute (void *ctx, const char *path) { @@ -201,7 +76,8 @@ welcome_message_post_setup (void) "Notmuch is now configured, and the configuration settings are saved in\n" "a file in your home directory named .notmuch-config . If you'd like to\n" "change the configuration in the future, you can either edit that file\n" -"directly or run \"notmuch setup\".\n\n" +"directly or run \"notmuch setup\". To choose an alternate configuration\n" +"location, set ${NOTMUCH_CONFIG}.\n\n" "The next step is to run \"notmuch new\" which will create a database\n" "that indexes all of your mail. Depending on the amount of mail you have\n" @@ -224,12 +100,15 @@ notmuch_setup_command (unused (void *ctx), unsigned int i; int is_new; -#define prompt(format, ...) \ - do { \ - printf (format, ##__VA_ARGS__); \ - fflush (stdout); \ - getline (&response, &response_size, stdin); \ - chomp_newline (response); \ +#define prompt(format, ...) \ + do { \ + printf (format, ##__VA_ARGS__); \ + fflush (stdout); \ + if (getline (&response, &response_size, stdin) < 0) { \ + printf ("Exiting.\n"); \ + exit (1); \ + } \ + chomp_newline (response); \ } while (0) config = notmuch_config_open (ctx, NULL, &is_new); @@ -280,10 +159,11 @@ notmuch_setup_command (unused (void *ctx), notmuch_config_set_database_path (config, absolute_path); } - notmuch_config_save (config); - - if (is_new) - welcome_message_post_setup (); - - return 0; + if (! notmuch_config_save (config)) { + if (is_new) + welcome_message_post_setup (); + return 0; + } else { + return 1; + } }