unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 1ff5840a8343e16d4d7b8b4013283c9deac42bf3 9104 bytes (raw)
name: notmuch-new.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
 
/* notmuch - Not much of an email program, (just index and search)
 *
 * Copyright © 2009 Carl Worth
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see http://www.gnu.org/licenses/ .
 *
 * Author: Carl Worth <cworth@cworth.org>
 */

#include "notmuch-client.h"

#include <unistd.h>

notmuch_indexing_context_t *indexing_ctx;

static void
handle_sigalrm (unused (int signal))
{
    indexing_ctx->print_progress = 1;
}

static volatile sig_atomic_t interrupted;

static void
handle_sigint (unused (int sig))
{
    ssize_t ignored;
    static char msg[] = "Stopping...         \n";

    ignored = write(2, msg, sizeof(msg)-1);
    indexing_ctx->interrupted = 1;
}

struct priv_ctx {
	struct timeval tv_start;
	int output_is_a_tty;
};

static void
add_files_print_progress (notmuch_indexing_context_t *state)
{
    struct priv_ctx *state2 = state->priv;
    struct timeval tv_now;
    double elapsed_overall, rate_overall;

    gettimeofday (&tv_now, NULL);

    elapsed_overall = notmuch_time_elapsed (state2->tv_start, tv_now);
    rate_overall = (state->processed_files) / elapsed_overall;

    printf ("Processed %d", state->processed_files);

    if (state->total_files) {
	double time_remaining;

	time_remaining = ((state->total_files - state->processed_files) /
			  rate_overall);
	printf (" of %d files (", state->total_files);
	notmuch_time_print_formatted_seconds (time_remaining);
	printf (" remaining).      \r");
    } else {
	printf (" files (%d files/sec.)    \r", (int) rate_overall);
    }

    fflush (stdout);
}

static void
add_files_print_verbose (notmuch_indexing_context_t *state, const char *filename)
{
    struct priv_ctx *state2 = state->priv;
    if (state2->output_is_a_tty)
	printf("\r\033[K");

    printf ("%i/%i: %s",
	    state->processed_files,
	    state->total_files,
	    filename);

    putchar((state2->output_is_a_tty) ? '\r' : '\n');
    fflush (stdout);
}

/* This is the top-level entry point for add_files. It does a couple
 * of error checks, sets up the progress-printing timer and then calls
 * into the recursive function. */
static notmuch_status_t
add_files (notmuch_mailstore_t *mailstore,
	   const char *path,
	   notmuch_indexing_context_t *state) /* FIXME: rename */
{
    struct priv_ctx *state2 = state->priv;
    notmuch_status_t status;
    struct sigaction action;
    struct itimerval timerval;
    notmuch_bool_t timer_is_active = FALSE;
    struct stat st;

    state->print_progress = 0;
    state->print_progress_cb = add_files_print_progress;
    state->print_verbose_cb = add_files_print_verbose;

    if (state2->output_is_a_tty && ! debugger_is_active () && ! state->verbose) {
	/* Setup our handler for SIGALRM */
	memset (&action, 0, sizeof (struct sigaction));
	action.sa_handler = handle_sigalrm;
	sigemptyset (&action.sa_mask);
	action.sa_flags = SA_RESTART;
	sigaction (SIGALRM, &action, NULL);

	/* Then start a timer to send SIGALRM once per second. */
	timerval.it_interval.tv_sec = 1;
	timerval.it_interval.tv_usec = 0;
	timerval.it_value.tv_sec = 1;
	timerval.it_value.tv_usec = 0;
	setitimer (ITIMER_REAL, &timerval, NULL);

	timer_is_active = TRUE;
    }

    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;
    }

    status = notmuch_mailstore_index_new (mailstore, path, state);

    if (timer_is_active) {
	/* Now stop the timer. */
	timerval.it_interval.tv_sec = 0;
	timerval.it_interval.tv_usec = 0;
	timerval.it_value.tv_sec = 0;
	timerval.it_value.tv_usec = 0;
	setitimer (ITIMER_REAL, &timerval, NULL);

	/* And disable the signal handler. */
	action.sa_handler = SIG_IGN;
	sigaction (SIGALRM, &action, NULL);
    }

    return status;
}

static void
upgrade_print_progress (void *closure,
			double progress)
{
    notmuch_indexing_context_t *state = closure;
    struct priv_ctx *state2 = state->priv;

    printf ("Upgrading database: %.2f%% complete", progress * 100.0);

    if (progress > 0) {
	struct timeval tv_now;
	double elapsed, time_remaining;

	gettimeofday (&tv_now, NULL);

	elapsed = notmuch_time_elapsed (state2->tv_start, tv_now);
	time_remaining = (elapsed / progress) * (1.0 - progress);
	printf (" (");
	notmuch_time_print_formatted_seconds (time_remaining);
	printf (" remaining)");
    }

    printf (".      \r");

    fflush (stdout);
}

int
notmuch_new_command (void *ctx, int argc, char *argv[])
{
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    struct priv_ctx state2;
    notmuch_indexing_context_t add_files_state; /* FIXME: Rename */
    double elapsed;
    struct timeval tv_now;
    int ret = 0;
    struct stat st;
    const char *db_path;
    char *dot_notmuch_path;
    struct sigaction action;
    int i;
    notmuch_mailstore_t *mailstore;

    indexing_ctx = &add_files_state;

    add_files_state.verbose = 0;
    add_files_state.priv = &state2;
    state2.output_is_a_tty = isatty (fileno (stdout));

    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
	if (STRNCMP_LITERAL (argv[i], "--verbose") == 0) {
	    add_files_state.verbose = 1;
	} else {
	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
	    return 1;
	}
    }

    config = notmuch_config_open (ctx, NULL, NULL);
    if (config == NULL)
	return 1;

    db_path = notmuch_config_get_database_path (config);
    mailstore = notmuch_config_get_mailstore (config);

    dot_notmuch_path = talloc_asprintf (ctx, "%s/%s", db_path, ".notmuch");

    if (stat (dot_notmuch_path, &st)) {
	int count;

	count = 0;
	notmuch_mailstore_count_files (mailstore, db_path, &count, &interrupted);
	if (interrupted)
	    return 1;

	printf ("Found %d total files (that's not much mail).\n", count);
	notmuch = notmuch_database_create (db_path, mailstore);
	add_files_state.total_files = count;
    } else {
	notmuch = notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
					 mailstore);
	if (notmuch == NULL)
	    return 1;

	if (notmuch_database_needs_upgrade (notmuch)) {
	    printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n");
	    gettimeofday (&state2.tv_start, NULL);
	    notmuch_database_upgrade (notmuch, upgrade_print_progress,
				      &add_files_state);
	    printf ("Your notmuch database has now been upgraded to database format version %u.\n",
		    notmuch_database_get_version (notmuch));
	}

	add_files_state.total_files = 0;
    }

    if (notmuch == NULL)
	return 1;

    /* Setup our handler for SIGINT. We do this after having
     * potentially done a database upgrade we this interrupt handler
     * won't support. */
    add_files_state.interrupted = 0;
    memset (&action, 0, sizeof (struct sigaction));
    action.sa_handler = handle_sigint;
    sigemptyset (&action.sa_mask);
    action.sa_flags = SA_RESTART;
    sigaction (SIGINT, &action, NULL);

    talloc_free (dot_notmuch_path);
    dot_notmuch_path = NULL;

    add_files_state.processed_files = 0;
    add_files_state.added_messages = 0;
    gettimeofday (&state2.tv_start, NULL);

    ret = add_files (mailstore, db_path, &add_files_state);

    gettimeofday (&tv_now, NULL);
    elapsed = notmuch_time_elapsed (state2.tv_start,
				    tv_now);

    if (add_files_state.processed_files) {
	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 new %s to the database.",
		add_files_state.added_messages,
		add_files_state.added_messages == 1 ?
		"message" : "messages");
    } else {
	printf ("No new mail.");
    }

    if (add_files_state.removed_files) {
	printf (" Removed %d %s.",
		add_files_state.removed_files,
		add_files_state.removed_files == 1 ? "message" : "messages");
    }

    if (add_files_state.renamed_files) {
	printf (" Detected %d file %s.",
		add_files_state.renamed_files,
		add_files_state.renamed_files == 1 ? "rename" : "renames");
    }

    printf ("\n");

    if (ret) {
	printf ("\nNote: At least one error was encountered: %s\n",
		notmuch_status_to_string (ret));
    }

    notmuch_database_close (notmuch);

    return ret || interrupted;
}

debug log:

solving 1ff5840 ...
found 1ff5840 in https://yhetil.org/notmuch/1269722661-6894-2-git-send-email-sojkam1@fel.cvut.cz/
found 2d0ba6c in https://yhetil.org/notmuch/1270737766-29237-2-git-send-email-sojkam1@fel.cvut.cz/ ||
	https://yhetil.org/notmuch/1269722661-6894-1-git-send-email-sojkam1@fel.cvut.cz/ ||
	https://yhetil.org/notmuch/1269639777-25765-1-git-send-email-sojkam1@fel.cvut.cz/ ||
	https://yhetil.org/notmuch/1268926780-20045-2-git-send-email-sojkam1@fel.cvut.cz/
found 44b50aa in https://yhetil.org/notmuch.git/
preparing index
index prepared:
100644 44b50aaa2c49ceea06825abea39256790f2c1a5d	notmuch-new.c

applying [1/2] https://yhetil.org/notmuch/1270737766-29237-2-git-send-email-sojkam1@fel.cvut.cz/
diff --git a/notmuch-new.c b/notmuch-new.c
index 44b50aa..2d0ba6c 100644

Checking patch notmuch-new.c...
Applied patch notmuch-new.c cleanly.

skipping https://yhetil.org/notmuch/1269722661-6894-1-git-send-email-sojkam1@fel.cvut.cz/ for 2d0ba6c
skipping https://yhetil.org/notmuch/1269639777-25765-1-git-send-email-sojkam1@fel.cvut.cz/ for 2d0ba6c
skipping https://yhetil.org/notmuch/1268926780-20045-2-git-send-email-sojkam1@fel.cvut.cz/ for 2d0ba6c
index at:
100644 2d0ba6cece7a41f78e940d1d9b4cdba04b33fdd0	notmuch-new.c

applying [2/2] https://yhetil.org/notmuch/1269722661-6894-2-git-send-email-sojkam1@fel.cvut.cz/
diff --git a/notmuch-new.c b/notmuch-new.c
index 2d0ba6c..1ff5840 100644

Checking patch notmuch-new.c...
Applied patch notmuch-new.c cleanly.

index at:
100644 1ff5840a8343e16d4d7b8b4013283c9deac42bf3	notmuch-new.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).