From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 5011F429E31 for ; Sat, 19 Jan 2013 16:51:55 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id khgi01zL7bvl for ; Sat, 19 Jan 2013 16:51:54 -0800 (PST) Received: from mail-pa0-f47.google.com (mail-pa0-f47.google.com [209.85.220.47]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 5CECF431FCF for ; Sat, 19 Jan 2013 16:51:42 -0800 (PST) Received: by mail-pa0-f47.google.com with SMTP id fa10so2753365pad.20 for ; Sat, 19 Jan 2013 16:51:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:from:to:cc:subject:date:message-id:x-mailer:in-reply-to :references; bh=gUz+F2dlXciewub+lsh7NrpMmtsK7aYRFpop9lh8aNY=; b=GuaxkEsN1jqBQzifuAKtgGfcfqAa3FPWoBZVucb9iaogJyYvbOmoTxq9F3S6nq8iEk eiY8nkPMVG7r5wVpDKAsFWXEwg3JFLl1vUgozGpksMsllKrSmtjLJ540BZLbIHjuJV79 mFeMXrvs9pdL/66GPPnJ7ngrSUPLSHhl1TlJyxCj5Eid7wxZn7sLmDsARioq/NcesD1I yu1JZ9AX7XMJYR7WUY/tHF+oVocOWc/KrSAJ+3J/t+HWo3M4if+L939ctkpAaCDYbScA LmsVwMb0qXYEMfEe2WzXndLRHGxzgGsUuqhGjWBqVTg4rzkol6NMoeL0Dxs48QSelrG8 jxpA== X-Received: by 10.66.73.105 with SMTP id k9mr36422216pav.37.1358643101494; Sat, 19 Jan 2013 16:51:41 -0800 (PST) Received: from localhost (215.42.233.220.static.exetel.com.au. [220.233.42.215]) by mx.google.com with ESMTPS id ux4sm5805088pbc.25.2013.01.19.16.51.39 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Sat, 19 Jan 2013 16:51:40 -0800 (PST) From: Peter Wang To: notmuch@notmuchmail.org Subject: [PATCH v3 14/20] insert: fsync new directory after rename Date: Sun, 20 Jan 2013 11:49:58 +1100 Message-Id: <1358643004-14522-15-git-send-email-novalazy@gmail.com> X-Mailer: git-send-email 1.7.12.1 In-Reply-To: <1358643004-14522-1-git-send-email-novalazy@gmail.com> References: <1358643004-14522-1-git-send-email-novalazy@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 20 Jan 2013 00:51:55 -0000 After moving the file from the 'tmp' to the 'new' directory, fsync on the 'new' directory for durability. --- notmuch-insert.c | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/notmuch-insert.c b/notmuch-insert.c index 60855c1..c4d5e75 100644 --- a/notmuch-insert.c +++ b/notmuch-insert.c @@ -56,6 +56,28 @@ check_folder_name (const char *folder) } } +/* Call fsync() on a directory path. */ +static notmuch_bool_t +sync_dir (const char *dir) +{ + notmuch_bool_t ret; + int fd; + + ret = TRUE; + fd = open (dir, O_RDONLY); + if (fd == -1) { + fprintf (stderr, "Error: open() dir failed: %s\n", strerror (errno)); + ret = FALSE; + } + if (ret && fsync (fd) != 0) { + fprintf (stderr, "Error: fsync() dir failed: %s\n", strerror (errno)); + ret = FALSE; + } + if (fd != -1) + close (fd); + return ret; +} + /* Make the given directory, succeeding if it already exists. */ static notmuch_bool_t make_directory (char *path, int mode) @@ -140,10 +162,11 @@ maildir_create_folder (void *ctx, const char *dir) /* Open a unique file in the Maildir 'tmp' directory. * Returns the file descriptor on success, or -1 on failure. * On success, file paths for the message in the 'tmp' and 'new' - * directories are returned via tmppath and newpath. */ + * directories are returned via tmppath and newpath, + * and the path of the 'new' directory itself in newdir. */ static int maildir_open_tmp_file (void *ctx, const char *dir, - char **tmppath, char **newpath) + char **tmppath, char **newpath, char **newdir) { pid_t pid; char hostname[256]; @@ -180,8 +203,9 @@ maildir_open_tmp_file (void *ctx, const char *dir, return -1; } + *newdir = talloc_asprintf (ctx, "%s/new", dir); *newpath = talloc_asprintf (ctx, "%s/new/%s", dir, filename); - if (! *newpath) { + if (! *newdir || ! *newpath) { fprintf (stderr, "Out of memory\n"); close (fd); unlink (*tmppath); @@ -201,14 +225,16 @@ maildir_open_tmp_file (void *ctx, const char *dir, * http://wiki.dovecot.org/MailboxFormat/Maildir#Mail_delivery */ static notmuch_bool_t -maildir_move_tmp_to_new (const char *tmppath, const char *newpath) +maildir_move_tmp_to_new (const char *tmppath, const char *newpath, + const char *newdir) { if (rename (tmppath, newpath) != 0) { fprintf (stderr, "Error: rename() failed: %s\n", strerror (errno)); return FALSE; } - return TRUE; + /* Sync the 'new' directory after rename for durability. */ + return sync_dir (newdir); } /* Copy the contents of standard input (fdin) into fdout. */ @@ -297,10 +323,11 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, { char *tmppath; char *newpath; + char *newdir; int fdout; notmuch_bool_t ret; - fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath); + fdout = maildir_open_tmp_file (ctx, dir, &tmppath, &newpath, &newdir); if (fdout < 0) { return FALSE; } @@ -311,7 +338,7 @@ insert_message (void *ctx, notmuch_database_t *notmuch, int fdin, } close (fdout); if (ret) { - ret = maildir_move_tmp_to_new (tmppath, newpath); + ret = maildir_move_tmp_to_new (tmppath, newpath, newdir); } if (!ret) { unlink (tmppath); -- 1.7.12.1