unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob a147206e8b6d5f7c68b13c2576541fc1969825fb 8306 bytes (raw)
name: notmuch-link.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
329
330
331
332
333
334
335
336
337
338
339
 
/* notmuch - Not much of an email program, (just index and search)
 *
 * Copyright © 2011 Ali Polatel
 *
 * 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: Ali Polatel <alip@exherbo.org>
 */

#include "notmuch-client.h"

typedef struct {
    const char *maildir;

    notmuch_bool_t cleandir;
    notmuch_bool_t makedir;
    notmuch_bool_t entire_thread;
    mode_t mode;

    clean_method_t clean_method;
    rename_method_t rename_method;
} link_options_t;

static inline void
init_link_options(link_options_t *options)
{
    options->maildir = NULL;

    options->makedir = FALSE;
    options->entire_thread = FALSE;
    options->mode = 0700;

    options->clean_method = CLEAN_NONE;
    options->rename_method = RENAME_SYMLINK;
}

static inline const char *
rename_method_abbr(rename_method_t method)
{
    switch (method) {
    case RENAME_SYMLINK:
	return "sym";
    case RENAME_HARDLINK:
	return "hard";
    default:
	return "love";
    }
}

static inline const char *
rename_method_verb(rename_method_t method)
{
    switch (method) {
    case RENAME_SYMLINK:
	return "symlinked";
    case RENAME_HARDLINK:
	return "hardlinked";
    default:
	return "made love all day long!";
    }
}

static notmuch_bool_t
prepare_maildir (const char *maildir,
		 notmuch_bool_t makedir,
		 mode_t mode,
		 clean_method_t methc)
{
    int ret;

    if (!maildir_check(maildir, makedir, mode))
	return FALSE;

    ret = maildir_clean_recursive(maildir, methc);
    if (ret == -1)
	return FALSE;
    else if (ret > 0)
	printf("Unlinked %d entries under %s\n",
	       ret, maildir);

    return TRUE;
}

static int
link_messages (notmuch_messages_t *messages,
	       rename_method_t rename_method,
	       const char *maildir)
{
    int ret = 0;
    const char *path;
    notmuch_message_t *message;
    notmuch_filenames_t *filenames;

    for (;
	 notmuch_messages_valid (messages);
	 notmuch_messages_move_to_next (messages))
    {
	message = notmuch_messages_get (messages);

	filenames = notmuch_message_get_filenames (message);
	for (;
	     notmuch_filenames_valid (filenames);
	     notmuch_filenames_move_to_next (filenames))
	{
	    path = notmuch_filenames_get (filenames);
	    if (maildir_rename (path, maildir, rename_method)) {
		ret++;
	    }
	}
	notmuch_filenames_destroy( filenames );

	notmuch_message_destroy (message);
    }

    return ret;
}

static int
do_link_threads (notmuch_query_t *query,
		 const link_options_t *lopts)
{
    int message_count, thread_count;
    notmuch_threads_t *threads;
    notmuch_thread_t *thread;
    notmuch_messages_t *messages;

    threads = notmuch_query_search_threads (query);
    if (threads == NULL)
	return 1;

    if (!prepare_maildir(lopts->maildir,
			 lopts->makedir, lopts->mode,
			 lopts->clean_method))
	return 1;

    message_count = thread_count = 0;
    for (;
	 notmuch_threads_valid (threads);
	 notmuch_threads_move_to_next (threads))
    {
	thread = notmuch_threads_get (threads);

	messages = notmuch_thread_get_toplevel_messages (thread);

	if (messages == NULL)
	    INTERNAL_ERROR ("Thread %s has no toplevel messages.\n",
			    notmuch_thread_get_thread_id (thread));
	else
	    thread_count++;

	message_count += link_messages (messages,
					lopts->rename_method,
					lopts->maildir);

	notmuch_messages_destroy (messages);
	notmuch_thread_destroy (thread);
    }

    if (message_count > 0) {
	printf("%d messages in %d threads %s under %s\n",
	       message_count, thread_count,
	       rename_method_verb(lopts->rename_method),
	       lopts->maildir);
    }

    notmuch_threads_destroy (threads);

    return 0;
}

static int
do_link_messages (notmuch_query_t *query,
		  const link_options_t *lopts)
{
    int message_count;
    notmuch_messages_t *messages;

    messages = notmuch_query_search_messages (query);
    if (messages == NULL)
	return 1;

    if (!prepare_maildir(lopts->maildir,
			 lopts->makedir,
			 lopts->mode,
			 lopts->clean_method))
	return 1;

    message_count = link_messages (messages,
				   lopts->rename_method,
				   lopts->maildir);
    if (message_count > 0) {
	printf("%d messages %s under %s\n",
	       message_count,
	       rename_method_verb(lopts->rename_method),
	       lopts->maildir);
    }

    notmuch_messages_destroy (messages);

    return 0;
}

int
notmuch_link_command (void *ctx, int argc, char *argv[])
{
    int i, ret;
    char *query_str;
    char *opt;
    notmuch_config_t *config;
    notmuch_database_t *notmuch;
    notmuch_query_t *query;
    link_options_t options;

    init_link_options(&options);

    for (i = 0; i < argc && argv[i][0] == '-'; i++) {
	if (strcmp (argv[i], "--") == 0) {
	    i++;
	    break;
	}
	if (STRNCMP_LITERAL (argv[i], "--rename=") == 0) {
	    opt = argv[i] + sizeof ("--rename=") - 1;
	    if (strcmp (opt, "symlink") == 0) {
		options.rename_method = RENAME_SYMLINK;
	    } else if (strcmp (opt, "hardlink") == 0) {
		options.rename_method = RENAME_HARDLINK;
#if 0
#error TODO
	    } else if (strcmp (opt, "copy") == 0) {
		options.rename_method = RENAME_COPY;
	    } else if (strcmp (opt, "move") == 0) {
		options.rename_method = RENAME_MOVE;
#endif
	    } else {
		fprintf (stderr, "Invalid value for --rename: %s\n", opt);
		return 1;
	    }
	} else if (STRNCMP_LITERAL (argv[i], "--maildir=") == 0) {
	    opt = argv[i] + sizeof ("--maildir=") - 1;
	    options.maildir = talloc_strdup (ctx, opt);
	} else if (STRNCMP_LITERAL (argv[i], "--clean=") == 0) {
	    opt = argv[i] + sizeof ("--clean=") - 1;
	    if (strcmp (opt, "dangling") == 0) {
		options.clean_method = CLEAN_DANGLING;
	    } else if (strcmp (opt, "symlink") == 0) {
		options.clean_method = CLEAN_SYMLINK;
	    } else if (strcmp (opt, "all") == 0) {
		options.clean_method = CLEAN_ALL;
	    } else if (strcmp (opt, "none") == 0) {
		options.clean_method = CLEAN_NONE;
	    } else {
		fprintf (stderr, "Invalid value for --clean: %s\n", opt);
		return 1;
	    }
	} else if (STRNCMP_LITERAL (argv[i], "--mkdir") == 0) {
	    options.makedir = TRUE;

	    opt = argv[i] + sizeof ("--mkdir") - 1;

	    if (opt[0] == '\0') {
		continue;
	    } else if (opt[0] != '=') {
		fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
		return 1;
	    } else {
		opt++; /* skip '=' */
	    }

	    options.mode = 0;
	    while (*opt >= '0' && *opt <= '7')
		options.mode = options.mode * 8 + (*opt++ - '0');
	    if (*opt) {
		fprintf (stderr, "Invalid value for --mkdir: %s\n", opt);
		return 1;
	    }
	} else if (STRNCMP_LITERAL (argv[i], "--entire-thread") == 0) {
	    options.entire_thread = TRUE;
	} else {
	    fprintf (stderr, "Unrecognized option: %s\n", argv[i]);
	    return 1;
	}
    }

    if (!options.maildir) {
	fprintf (stderr, "Target directory must be specified "
			 "using --maildir option\n");
	return 1;
    }

    argc -= i;
    argv += i;

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

    notmuch = notmuch_database_open (notmuch_config_get_database_path (config),
				     NOTMUCH_DATABASE_MODE_READ_ONLY);
    if (notmuch == NULL)
	return 1;

    query_str = query_string_from_args (notmuch, argc, argv);
    if (query_str == NULL) {
	fprintf (stderr, "Out of memory.\n");
	return 1;
    }
    if (*query_str == '\0') {
	fprintf (stderr, "Error: notmuch link requires at least one search term.\n");
	return 1;
    }

    query = notmuch_query_create (notmuch, query_str);
    if (query == NULL) {
	fprintf (stderr, "Out of memory\n");
	return 1;
    }

    if (options.entire_thread) {
	ret = do_link_threads (query, &options);
    } else {
	ret = do_link_messages (query, &options);
    }

    notmuch_query_destroy (query);
    notmuch_database_close (notmuch);

    return ret;
}

debug log:

solving a147206 ...
found a147206 in https://yhetil.org/notmuch/18678bd1b39417e4a806210c88a4a28c9a36884a.1317458072.git.alip@exherbo.org/

applying [1/1] https://yhetil.org/notmuch/18678bd1b39417e4a806210c88a4a28c9a36884a.1317458072.git.alip@exherbo.org/
diff --git a/notmuch-link.c b/notmuch-link.c
new file mode 100644
index 0000000..a147206

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

index at:
100644 a147206e8b6d5f7c68b13c2576541fc1969825fb	notmuch-link.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).