unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob b8c48b39509d4b21acffe4bc239b6cf8d06228fa 6127 bytes (raw)
name: maildir.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
 
/* Maildir utilities for the notmuch mail library
 *
 * Copyright © 2011 Ali Polatel
 * Based in part upon mu which is:
 *   Copyright © 2008-2011 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
 *
 * 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 <polatel@gmail.com>
 */

#include "maildir.h"

static const char *const maildir_subdirs_array[] = {"new", "cur", "tmp"};

/* FIXME: The two functions below, dirent_sort_inode and get_dtype duplicate
 * code from notmuch-new.c
 */
static int
dirent_sort_inode (const struct dirent **a, const struct dirent **b)
{
    return ((*a)->d_ino < (*b)->d_ino) ? -1 : 1;
}

static unsigned char
get_dtype(const char *fullpath, struct dirent *entry)
{
    struct stat buf;

    if (entry->d_type != DT_UNKNOWN)
	return entry->d_type;

    if (lstat(fullpath, &buf) == -1) {
	fprintf (stderr, "Warning: stat failed on `%s': %s\n",
		 fullpath, strerror(errno));
	return DT_UNKNOWN;
    }

    if (S_ISREG (buf.st_mode)) {
	return DT_REG;
    } else if (S_ISDIR (buf.st_mode)) {
	return DT_DIR;
    } else if (S_ISLNK (buf.st_mode)) {
	return DT_LNK;
    }

    return DT_UNKNOWN;
}

static notmuch_bool_t
maildir_access (const char *path)
{
    struct stat buf;

    if (access (path, R_OK | W_OK | X_OK) == -1) {
	fprintf (stderr, "Failed to access path `%s': %s\n",
		 path, strerror(errno));
	return FALSE;
    }

    if (lstat(path, &buf) == -1) {
	fprintf (stderr, "Failed to access path `%s': %s\n",
		 path, strerror(errno));
	return FALSE;
    }

    if (!S_ISDIR(buf.st_mode)) {
	fprintf (stderr, "Path `%s' is not a directory\n", path);
	return FALSE;
    }

    return TRUE;
}

/* Determine whether the source message is in 'new' or in 'cur';
 * we ignore messages in 'tmp' for obvious reasons
 */
static notmuch_bool_t
maildir_subdir (const char *src, notmuch_bool_t *in_cur)
{
    char *srcpath;

    srcpath = g_path_get_dirname (src);

    if (g_str_has_suffix (srcpath, "new"))
	*in_cur = FALSE;
    else if (g_str_has_suffix (srcpath, "cur"))
	*in_cur = TRUE;
    else {
	g_free (srcpath);
	errno = EINVAL;
	return FALSE;
    }

    g_free(srcpath);
    return TRUE;
}

static char *
maildir_transform_path (const char *src, const char *targetpath)
{
    char *targetfullpath, *srcfile;
    notmuch_bool_t in_cur;

    if (!maildir_subdir (src, &in_cur)) {
	fprintf (stderr, "Invalid maildir subdirectory `%s': %s\n",
		 src, strerror(errno));
	return NULL;
    }

    srcfile = g_path_get_basename (src);
    targetfullpath = g_strdup_printf ("%s%c%s%c%s",
				      targetpath,
				      G_DIR_SEPARATOR,
				      in_cur ? "cur" : "new",
				      G_DIR_SEPARATOR,
				      srcfile);
    g_free (srcfile);

    return targetfullpath;
}

notmuch_bool_t
maildir_check (const char *path, notmuch_bool_t makedir, mode_t mode)
{
    int i;
    char *fullpath = NULL;

    for (i = 0; i != G_N_ELEMENTS (maildir_subdirs_array); i++) {
	    fullpath = g_build_filename (path, maildir_subdirs_array[i], NULL);
	    if (!makedir && !maildir_access(fullpath))
		goto FAIL;
	    if (makedir && g_mkdir_with_parents (fullpath, (int)mode) != 0) {
		fprintf (stderr, "Error creating %s: %s\n",
			 fullpath, strerror(errno));
		goto FAIL;
	    }
	    g_free (fullpath);
    }

    return TRUE;

  FAIL:
    g_free (fullpath);
    return FALSE;
}

notmuch_bool_t
maildir_rename (const char *src, const char *targetpath, rename_method_t methr)
{
    int ret;
    char *targetfullpath;

    targetfullpath = maildir_transform_path (src, targetpath);
    if (!targetfullpath)
	return FALSE;

    switch (methr) {
    case RENAME_SYMLINK:
	ret = symlink (src, targetfullpath);
	break;
    case RENAME_HARDLINK:
	ret = link (src, targetfullpath);
	break;
    default:
	return FALSE;
    }

    if (ret == -1) {
	if (errno != EEXIST) {
	    fprintf (stderr, "Failed to link %s to %s: %s\n",
		     src, targetfullpath, strerror(errno));
	}
	g_free (targetfullpath);
	return FALSE;
    }

    g_free (targetfullpath);
    return TRUE;
}

int
maildir_clean_recursive (const char *path, clean_method_t methc)
{
    int i, count, ret, num_fs_entries;
    notmuch_bool_t delete;
    char *fullpath = NULL;
    struct dirent *entry = NULL;
    struct dirent **fs_entries = NULL;
    struct stat buf;

    if (methc == CLEAN_NONE)
	return 0;

    num_fs_entries = scandir(path, &fs_entries, NULL, dirent_sort_inode);
    if (num_fs_entries == -1) {
	fprintf (stderr, "Error opening directory %s: %s\n",
		 path, strerror(errno));
	return -1;
    }

    count = 0;
    for (i = 0; i < num_fs_entries; i++) {
	entry = fs_entries[i];

	if (!entry->d_name ||
	    strcmp (entry->d_name, ".") == 0 ||
	    strcmp (entry->d_name, "..") == 0 ||
	    strcmp (entry->d_name, "tmp") == 0)
	{
	    continue;
	}

	delete = FALSE;
	fullpath = g_build_filename (path, entry->d_name, NULL);
	switch (get_dtype(fullpath, entry)) {
	case DT_REG:
	    if (methc == CLEAN_ALL)
		delete = TRUE;
	    break;
	case DT_LNK:
	    if (methc == CLEAN_ALL ||
		methc == CLEAN_SYMLINK ||
		(methc == CLEAN_DANGLING &&
		 (stat(fullpath, &buf) == -1 &&
		  (errno == ENOENT || errno == ELOOP))))
		delete = TRUE;
	    break;
	case DT_DIR:
	    ret = maildir_clean_recursive (fullpath, methc);
	    if (ret != -1)
		count += ret;
	    break;
	default:
	    break; /* skip the rest */
	}

	if (delete) {
	    if (unlink (fullpath) == -1) {
		fprintf (stderr, "Warning: error unlinking `%s': %s",
			 fullpath, strerror(errno));
	    } else {
		count++;
	    }
	}

	g_free (fullpath);
    }

    return count;
}

debug log:

solving b8c48b3 ...
found b8c48b3 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/maildir.c b/maildir.c
new file mode 100644
index 0000000..b8c48b3

Checking patch maildir.c...
Applied patch maildir.c cleanly.

index at:
100644 b8c48b39509d4b21acffe4bc239b6cf8d06228fa	maildir.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).