notmuch_message_tags_to_maildir_flags: Do nothing outside of "new" and "cur"

Some people use notmuch with non-maildir files, (for example, email
messages in MH format, or else cool things like using sluk[*] to suck
down feeds into a format that notmuch can index).

To better support uses like that, don't do any renaming for files that
are not in a directory named either "new" or "cur".

[*] https://github.com/krl/sluk/
This commit is contained in:
Carl Worth 2010-11-11 14:32:17 -08:00
parent 1ea0b8bfe1
commit 95dd5fe5d7
4 changed files with 54 additions and 19 deletions

View file

@ -945,20 +945,45 @@ maildir_get_new_flags(notmuch_message_t *message, char *flags)
*p = '\0'; *p = '\0';
} }
static char * /* Is the given filename within a maildir directory?
maildir_get_subdir (char *filename) *
* Specifically, is the final directory component of 'filename' either
* "cur" or "new". If so, return a pointer to that final directory
* component within 'filename'. If not, return NULL.
*
* A non-NULL return value is guaranteed to be a valid string pointer
* pointing to the characters "new/" or "cur/", (but not
* NUL-terminated).
*/
static const char *
_filename_is_in_maildir (const char *filename)
{ {
char *p, *subdir = NULL; const char *slash, *dir = NULL;
p = filename + strlen (filename) - 1; /* Find the last '/' separating directory from filename. */
while (p > filename + 3 && *p != '/') slash = strrchr (filename, '/');
p--; if (slash == NULL)
if (*p == '/') { return NULL;
subdir = p - 3;
if (subdir > filename && *(subdir - 1) != '/') /* Jump back 4 characters to where the previous '/' will be if the
subdir = NULL; * directory is named "cur" or "new". */
if (slash - filename < 4)
return NULL;
slash -= 4;
if (*slash != '/')
return NULL;
dir = slash + 1;
if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
STRNCMP_LITERAL (dir, "new/") == 0)
{
return dir;
} }
return subdir;
return NULL;
} }
/* XXX: Needs to ensure that existing, unsupported flags in the /* XXX: Needs to ensure that existing, unsupported flags in the
@ -971,7 +996,7 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
notmuch_filenames_t *filenames; notmuch_filenames_t *filenames;
char flags[ARRAY_SIZE(flag2tag)+1]; char flags[ARRAY_SIZE(flag2tag)+1];
const char *filename, *p; const char *filename, *p;
char *filename_new, *subdir = NULL; char *filename_new, *dir;
int ret; int ret;
maildir_get_new_flags (message, flags); maildir_get_new_flags (message, flags);
@ -982,6 +1007,9 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
{ {
filename = notmuch_filenames_get (filenames); filename = notmuch_filenames_get (filenames);
if (! _filename_is_in_maildir (filename))
continue;
p = strstr(filename, ":2,"); p = strstr(filename, ":2,");
if ((p && strcmp (p+3, flags) == 0) || if ((p && strcmp (p+3, flags) == 0) ||
(!p && flags[0] == '\0')) (!p && flags[0] == '\0'))
@ -1001,9 +1029,9 @@ notmuch_message_tags_to_maildir_flags (notmuch_message_t *message)
filename_new[p-filename] = '\0'; filename_new[p-filename] = '\0';
/* If message is in new/ move it under cur/. */ /* If message is in new/ move it under cur/. */
subdir = maildir_get_subdir (filename_new); dir = (char *) _filename_is_in_maildir (filename_new);
if (subdir && memcmp (subdir, "new/", 4) == 0) if (dir && STRNCMP_LITERAL (dir, "new/") == 0)
memcpy (subdir, "cur/", 4); memcpy (dir, "cur/", 4);
strcpy (filename_new+(p-filename), ":2,"); strcpy (filename_new+(p-filename), ":2,");
strcpy (filename_new+(p-filename)+3, flags); strcpy (filename_new+(p-filename)+3, flags);

View file

@ -57,6 +57,9 @@ NOTMUCH_BEGIN_DECLS
#define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)])) #define COMPILE_TIME_ASSERT(pred) ((void)sizeof(char[1 - 2*!(pred)]))
#define STRNCMP_LITERAL(var, literal) \
strncmp ((var), (literal), sizeof (literal) - 1)
/* There's no point in continuing when we've detected that we've done /* There's no point in continuing when we've detected that we've done
* something wrong internally (as opposed to the user passing in a * something wrong internally (as opposed to the user passing in a
* bogus value). * bogus value).

View file

@ -947,9 +947,13 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message);
* *
* Specifically, for each filename corresponding to this message: * Specifically, for each filename corresponding to this message:
* *
* Rename the file so that its filename ends with the sequence ":2," * If the filename is not in a maildir directory, do nothing.
* followed by zero or more of the following single-character flags * (A maildir directory is determined as a directory named "new" or
* (in ASCII order): * "cur".)
*
* If the filename is in a maildir directory, rename the file so that
* its filename ends with the sequence ":2," followed by zero or more
* of the following single-character flags (in ASCII order):
* *
* 'D' iff the message has the "draft" tag * 'D' iff the message has the "draft" tag
* 'F' iff the message has the "flagged" tag * 'F' iff the message has the "flagged" tag

View file

@ -118,7 +118,7 @@ output+=$(notmuch search subject:"Message to lose maildir info" | notmuch_search
test_expect_equal "$output" "No new mail. Detected 1 file rename. test_expect_equal "$output" "No new mail. Detected 1 file rename.
thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Message to lose maildir info (inbox)" thread:XXX 2001-01-05 [1/1] Notmuch Test Suite; Message to lose maildir info (inbox)"
add_message [subject]='"Non-maildir message"' [dir]=notmaildir/new [filename]='non-maildir-message' add_message [subject]='"Non-maildir message"' [dir]=notmaildir [filename]='non-maildir-message'
expected=$(notmuch search --output=files subject:"Non-maildir message") expected=$(notmuch search --output=files subject:"Non-maildir message")
test_expect_success "Can remove unread tag from message in non-maildir directory" 'notmuch tag -unread subject:"Non-maildir message"' test_expect_success "Can remove unread tag from message in non-maildir directory" 'notmuch tag -unread subject:"Non-maildir message"'