--- /dev/null
+Return-Path: <too@guru-group.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 12EA0431FBD\r
+ for <notmuch@notmuchmail.org>; Thu, 14 Nov 2013 14:03:51 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
+ autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id wZVNZu7GG1zu for <notmuch@notmuchmail.org>;\r
+ Thu, 14 Nov 2013 14:03:43 -0800 (PST)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 8C604431FDD\r
+ for <notmuch@notmuchmail.org>; Thu, 14 Nov 2013 14:03:36 -0800 (PST)\r
+Received: by guru.guru-group.fi (Postfix, from userid 501)\r
+ id 17D9310009D; Fri, 15 Nov 2013 00:03:29 +0200 (EET)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 3/5] compact: preserve backup database until compacted\r
+ database is in place\r
+Date: Fri, 15 Nov 2013 00:03:25 +0200\r
+Message-Id: <1384466607-28298-1-git-send-email-tomi.ollila@iki.fi>\r
+X-Mailer: git-send-email 1.8.0\r
+In-Reply-To: <id:1384362167-12740-1-git-send-email-tomi.ollila@iki.fi>\r
+References: <id:1384362167-12740-1-git-send-email-tomi.ollila@iki.fi>\r
+Cc: tomi.ollila@iki.fi\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Thu, 14 Nov 2013 22:03:51 -0000\r
+\r
+It is less error prone and window of failure opportunity is smaller\r
+if the old (backup) database is always renamed (instead of sometimes\r
+rmtree'd) before new (compacted) database is put into its place.\r
+Finally rmtree() old database in case old database backup is not kept.\r
+---\r
+\r
+The patches 3-5 in this series are updated based on Jani's comments.\r
+\r
+I've kept the fprintf(stderr,...)'s due to reasons explained in\r
+\r
+id:m28uwquccd.fsf@guru.guru-group.fi\r
+\r
+Diff v2-v3 for first (3/5) patch. Second (4/5) has just // -> /* .. */ change.\r
+\r
+|--- a/lib/database.cc\r
+|+++ b/lib/database.cc\r
+|@@ -906,17 +906,19 @@ notmuch_database_compact (const char *path,\r
+| }\r
+| keep_backup = FALSE;\r
+| }\r
+|- else\r
+|+ else {\r
+| keep_backup = TRUE;\r
+|+ }\r
+|\r
+| if (stat (backup_path, &statbuf) != -1) {\r
+|- fprintf (stderr, "Backup path already exists: %s\n", backup_path);\r
+|+ fprintf (stderr, "Path already exists: %s\n", backup_path);\r
+| ret = NOTMUCH_STATUS_FILE_ERROR;\r
+| goto DONE;\r
+| }\r
+| if (errno != ENOENT) {\r
+|- fprintf (stderr, "Unknown error while stat()ing backup path: %s\n",\r
+|+ fprintf (stderr, "Unknown error while stat()ing path: %s\n",\r
+| strerror (errno));\r
+|+ ret = NOTMUCH_STATUS_FILE_ERROR;\r
+| goto DONE;\r
+| }\r
+\r
+\r
+\r
+\r
+ lib/database.cc | 44 +++++++++++++++++++++++++++-----------------\r
+ 1 file changed, 27 insertions(+), 17 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3530cb6..d79cc30 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -873,6 +873,7 @@ notmuch_database_compact (const char *path,\r
+ notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;\r
+ notmuch_database_t *notmuch = NULL;\r
+ struct stat statbuf;\r
++ notmuch_bool_t keep_backup;\r
+ \r
+ local = talloc_new (NULL);\r
+ if (! local)\r
+@@ -898,17 +899,27 @@ notmuch_database_compact (const char *path,\r
+ goto DONE;\r
+ }\r
+ \r
+- if (backup_path != NULL) {\r
+- if (stat (backup_path, &statbuf) != -1) {\r
+- fprintf (stderr, "Backup path already exists: %s\n", backup_path);\r
+- ret = NOTMUCH_STATUS_FILE_ERROR;\r
+- goto DONE;\r
+- }\r
+- if (errno != ENOENT) {\r
+- fprintf (stderr, "Unknown error while stat()ing backup path: %s\n",\r
+- strerror (errno));\r
++ if (backup_path == NULL) {\r
++ if (! (backup_path = talloc_asprintf (local, "%s.old", xapian_path))) {\r
++ ret = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+ goto DONE;\r
+ }\r
++ keep_backup = FALSE;\r
++ }\r
++ else {\r
++ keep_backup = TRUE;\r
++ }\r
++\r
++ if (stat (backup_path, &statbuf) != -1) {\r
++ fprintf (stderr, "Path already exists: %s\n", backup_path);\r
++ ret = NOTMUCH_STATUS_FILE_ERROR;\r
++ goto DONE;\r
++ }\r
++ if (errno != ENOENT) {\r
++ fprintf (stderr, "Unknown error while stat()ing path: %s\n",\r
++ strerror (errno));\r
++ ret = NOTMUCH_STATUS_FILE_ERROR;\r
++ goto DONE;\r
+ }\r
+ \r
+ try {\r
+@@ -924,14 +935,10 @@ notmuch_database_compact (const char *path,\r
+ goto DONE;\r
+ }\r
+ \r
+- if (backup_path) {\r
+- if (rename (xapian_path, backup_path)) {\r
+- fprintf (stderr, "Error moving old database out of the way\n");\r
+- ret = NOTMUCH_STATUS_FILE_ERROR;\r
+- goto DONE;\r
+- }\r
+- } else {\r
+- rmtree (xapian_path);\r
++ if (rename (xapian_path, backup_path)) {\r
++ fprintf (stderr, "Error moving old database out of the way\n");\r
++ ret = NOTMUCH_STATUS_FILE_ERROR;\r
++ goto DONE;\r
+ }\r
+ \r
+ if (rename (compact_xapian_path, xapian_path)) {\r
+@@ -940,6 +947,9 @@ notmuch_database_compact (const char *path,\r
+ goto DONE;\r
+ }\r
+ \r
++ if (! keep_backup)\r
++ rmtree (backup_path);\r
++\r
+ DONE:\r
+ if (notmuch)\r
+ notmuch_database_destroy (notmuch);\r
+-- \r
+1.8.0\r
+\r