unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Yuri Volchkov <yuri.volchkov@gmail.com>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/4] insert: strip trailing / in folder path
Date: Sat, 12 Aug 2017 18:47:31 +0200	[thread overview]
Message-ID: <1502556453-11609-3-git-send-email-yuri.volchkov@gmail.com> (raw)
In-Reply-To: <1502556453-11609-1-git-send-email-yuri.volchkov@gmail.com>

I have faced a problem, that messages sent by emacs could not be shown
or found later. The "notmuch show id:<msg_id>" says "no such file or
directory".

The reason of this behavior is the following chain of events:
1) While sending a message, emacs calls
      notmuch insert --folder=maildir/Sent/ < test.msg

   From database's point of view, "Sent" and "Sent/" are different
   folders, and the separate record is created for the "Sent/".

   For the sake of simplicity let's assume inserted message will be
   stored as "maildir/Sent//new/msg_file_orig" (note the double slash)

2) During next sync, offlineimap uploads this message to the server,
   renames the source file according to his or servers naming
   convention. Let's say it is renamed to
   "maildir/Sent/new/msg_file_renamed"

3) The "notmuch new" command, composes a list of folders which has
   been modified. Obviously, the "Sent" dir is going to be in this
   list, but not the "Sent/".

   When notmuch scans the "Sent" folder, it finds the new file
   "maildir/Sent/new/msg_file_renamed", and adds it to the database as
   a duplicated of the message inserted in the step 1.

   But, notmuch does not notice that the file
   "maildir/Sent//new/msg_file_orig" has been deleted. So it
   erroneously stays in the database.

4) Next time, the user wants to see this email, the command "notmuch
   show id:<inserted_msg_id>" picks the first message file, stored in
   the database record, which happens to be the non-existing
   "maildir/Sent//new/msg_file_orig".

The solution is simple, we have to strip trailing '/' from the insert
path.

Signed-off-by: Yuri Volchkov <yuri.volchkov@gmail.com>
---
 lib/database.cc    |  3 +--
 notmuch-insert.c   |  4 +++-
 util/string-util.c | 13 +++++++++++++
 util/string-util.h |  2 ++
 4 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index 8f0e22a..79eb3d6 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -858,8 +858,7 @@ notmuch_database_open_verbose (const char *path,
     notmuch->status_string = NULL;
     notmuch->path = talloc_strdup (notmuch, path);
 
-    if (notmuch->path[strlen (notmuch->path) - 1] == '/')
-	notmuch->path[strlen (notmuch->path) - 1] = '\0';
+    strip_trailing(notmuch->path, '/');
 
     notmuch->mode = mode;
     notmuch->atomic_nesting = 0;
diff --git a/notmuch-insert.c b/notmuch-insert.c
index bc96af0..2590e83 100644
--- a/notmuch-insert.c
+++ b/notmuch-insert.c
@@ -27,6 +27,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include "string-util.h"
 
 static volatile sig_atomic_t interrupted;
 
@@ -451,7 +452,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     size_t new_tags_length;
     tag_op_list_t *tag_ops;
     char *query_string = NULL;
-    const char *folder = NULL;
+    char *folder = NULL;
     notmuch_bool_t create_folder = FALSE;
     notmuch_bool_t keep = FALSE;
     notmuch_bool_t no_hooks = FALSE;
@@ -511,6 +512,7 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
     if (folder == NULL) {
 	maildir = db_path;
     } else {
+	strip_trailing (folder, '/');
 	if (! is_valid_folder_name (folder)) {
 	    fprintf (stderr, "Error: invalid folder name: '%s'\n", folder);
 	    return EXIT_FAILURE;
diff --git a/util/string-util.c b/util/string-util.c
index 1812530..b010881 100644
--- a/util/string-util.c
+++ b/util/string-util.c
@@ -255,3 +255,16 @@ strcase_hash (const void *ptr)
 
     return hash;
 }
+
+void
+strip_trailing (char *str, char ch)
+{
+    int i;
+
+    for (i = strlen (str) - 1; i >= 0; i--) {
+	if (str[i] == ch)
+	    str[i] = '\0';
+	else
+	    break;
+    }
+}
diff --git a/util/string-util.h b/util/string-util.h
index 87917b8..9777061 100644
--- a/util/string-util.h
+++ b/util/string-util.h
@@ -75,6 +75,8 @@ int strcase_equal (const void *a, const void *b);
 /* GLib GHashFunc compatible case insensitive hash function */
 unsigned int strcase_hash (const void *ptr);
 
+void strip_trailing (char *str, char ch);
+
 #ifdef __cplusplus
 }
 #endif
-- 
2.7.4

  parent reply	other threads:[~2017-08-12 16:47 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-12 16:47 [PATCH 0/4] fix "no such file" problem for emails send by emacs Yuri Volchkov
2017-08-12 16:47 ` [PATCH 1/4] test: insert into the folder with trailing / Yuri Volchkov
2017-08-17  1:21   ` David Bremner
2017-08-20 12:16   ` David Bremner
2017-08-12 16:47 ` Yuri Volchkov [this message]
2017-08-19  0:17   ` [PATCH 2/4] insert: strip trailing / in folder path David Bremner
2017-08-19  8:27     ` Yuri Volchkov
2017-08-19 12:55       ` David Bremner
2017-08-12 16:47 ` [PATCH 3/4] test: show id:<> works even if the first duplicated is deleted Yuri Volchkov
2017-08-12 16:47 ` [PATCH 4/4] show: workaround for the missing file problem Yuri Volchkov
2017-08-13 12:41 ` [PATCH 0/4] fix "no such file" problem for emails send by emacs David Bremner
2017-08-13 21:56   ` Yuri Volchkov
2017-08-16 11:32     ` David Bremner
2017-08-21 15:44 ` [PATCH v2 0/4] fix insufficient path canonization Yuri Volchkov
2017-08-21 15:44   ` [PATCH v2 1/4] database: move striping of trailing '/' into helper function Yuri Volchkov
2017-08-21 15:44   ` [PATCH v2 2/4] insert: strip trailing / in folder path Yuri Volchkov
2017-08-21 15:44   ` [PATCH v2 3/4] test: show id:<> works even if the first duplicate is deleted Yuri Volchkov
2017-08-21 15:44   ` [PATCH v2 4/4] show: workaround for the missing file problem Yuri Volchkov
2017-08-23  0:19   ` [PATCH v2 0/4] fix insufficient path canonization David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1502556453-11609-3-git-send-email-yuri.volchkov@gmail.com \
    --to=yuri.volchkov@gmail.com \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).