changeset 4071:cd3d26cf124a HEAD

Renamed safe-open.* to nfs-workarounds.*, safe_open() to nfs_safe_open() and added a new global NFS_ESTALE_RETRY_COUNT which everyone uses instead of defining their own.
author Timo Sirainen <tss@iki.fi>
date Sun, 26 Feb 2006 12:15:39 +0200
parents 71b8faa84ec6
children 7e6acdd8d18d
files src/lib-index/mail-index-private.h src/lib-index/mail-index.c src/lib-index/mail-transaction-log.c src/lib-storage/index/maildir/maildir-uidlist.c src/lib-storage/subscription-file/subscription-file.c src/lib/Makefile.am src/lib/nfs-workarounds.c src/lib/nfs-workarounds.h src/lib/safe-open.c src/lib/safe-open.h
diffstat 10 files changed, 77 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-index/mail-index-private.h	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib-index/mail-index-private.h	Sun Feb 26 12:15:39 2006 +0200
@@ -18,7 +18,7 @@
 /* How many times to retry opening index files if read/fstat returns ESTALE.
    This happens with NFS when the file has been deleted (ie. index file was
    rewritten by another computer than us). */
-#define MAIL_INDEX_ESTALE_RETRY_COUNT 10
+#define MAIL_INDEX_ESTALE_RETRY_COUNT NFS_ESTALE_RETRY_COUNT
 
 #define MAIL_INDEX_IS_IN_MEMORY(index) \
 	((index)->dir == NULL)
--- a/src/lib-index/mail-index.c	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib-index/mail-index.c	Sun Feb 26 12:15:39 2006 +0200
@@ -5,7 +5,7 @@
 #include "buffer.h"
 #include "hash.h"
 #include "mmap-util.h"
-#include "safe-open.h"
+#include "nfs-workarounds.h"
 #include "read-full.h"
 #include "write-full.h"
 #include "mail-index-private.h"
@@ -1194,7 +1194,7 @@
         /* Note that our caller must close index->fd by itself.
            mail_index_reopen() for example wants to revert back to old
            index file if opening the new one fails. */
-	index->fd = safe_open(index->filepath, O_RDWR);
+	index->fd = nfs_safe_open(index->filepath, O_RDWR);
 	index->readonly = FALSE;
 
 	if (index->fd == -1 && errno == EACCES) {
--- a/src/lib-index/mail-transaction-log.c	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib-index/mail-transaction-log.c	Sun Feb 26 12:15:39 2006 +0200
@@ -4,7 +4,7 @@
 #include "ioloop.h"
 #include "buffer.h"
 #include "file-dotlock.h"
-#include "safe-open.h"
+#include "nfs-workarounds.h"
 #include "close-keep-errno.h"
 #include "read-full.h"
 #include "write-full.h"
@@ -497,7 +497,7 @@
 	bool found;
 
 	/* log creation is locked now - see if someone already created it */
-	old_fd = safe_open(path, O_RDWR);
+	old_fd = nfs_safe_open(path, O_RDWR);
 	if (old_fd != -1) {
 		if ((ret = fstat(old_fd, &st)) < 0) {
                         mail_index_file_set_syscall_error(index, path,
@@ -773,7 +773,7 @@
 		return mail_transaction_log_file_alloc_in_memory(log);
 
         for (i = 0; ; i++) {
-                fd = safe_open(path, O_RDWR);
+                fd = nfs_safe_open(path, O_RDWR);
                 if (fd == -1) {
                         if (errno != ENOENT) {
                                 mail_index_file_set_syscall_error(log->index,
@@ -808,7 +808,7 @@
         int fd, ret;
 
         for (i = 0;; i++) {
-                fd = safe_open(path, O_RDWR);
+                fd = nfs_safe_open(path, O_RDWR);
                 if (fd == -1) {
                         mail_index_file_set_syscall_error(log->index, path,
                                                           "open()");
@@ -978,7 +978,7 @@
 	/* see if we have it in log.2 file */
 	path = t_strconcat(log->index->filepath,
 			   MAIL_TRANSACTION_LOG_SUFFIX".2", NULL);
-	fd = safe_open(path, O_RDWR);
+	fd = nfs_safe_open(path, O_RDWR);
 	if (fd == -1) {
 		if (errno == ENOENT)
 			return 0;
--- a/src/lib-storage/index/maildir/maildir-uidlist.c	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib-storage/index/maildir/maildir-uidlist.c	Sun Feb 26 12:15:39 2006 +0200
@@ -8,7 +8,7 @@
 #include "str.h"
 #include "file-dotlock.h"
 #include "close-keep-errno.h"
-#include "safe-open.h"
+#include "nfs-workarounds.h"
 #include "write-full.h"
 #include "maildir-storage.h"
 #include "maildir-uidlist.h"
@@ -20,7 +20,7 @@
 
 /* NFS: How many times to retry reading dovecot-uidlist file if ESTALE
    error occurs in the middle of reading it */
-#define UIDLIST_ESTALE_RETRY_COUNT 10
+#define UIDLIST_ESTALE_RETRY_COUNT NFS_ESTALE_RETRY_COUNT
 
 /* how many seconds to wait before overriding uidlist.lock */
 #define UIDLIST_LOCK_STALE_TIMEOUT (60*2)
@@ -258,7 +258,7 @@
 
         *retry_r = FALSE;
 
-	fd = safe_open(uidlist->fname, O_RDONLY);
+	fd = nfs_safe_open(uidlist->fname, O_RDONLY);
 	if (fd == -1) {
 		if (errno != ENOENT) {
 			mail_storage_set_critical(storage,
--- a/src/lib-storage/subscription-file/subscription-file.c	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib-storage/subscription-file/subscription-file.c	Sun Feb 26 12:15:39 2006 +0200
@@ -3,7 +3,7 @@
 #include "lib.h"
 #include "istream.h"
 #include "ostream.h"
-#include "safe-open.h"
+#include "nfs-workarounds.h"
 #include "file-dotlock.h"
 #include "mail-storage-private.h"
 #include "subscription-file.h"
@@ -13,7 +13,7 @@
 
 #define MAX_MAILBOX_LENGTH PATH_MAX
 
-#define SUBSCRIPTION_FILE_ESTALE_RETRY_COUNT 10
+#define SUBSCRIPTION_FILE_ESTALE_RETRY_COUNT NFS_ESTALE_RETRY_COUNT
 #define SUBSCRIPTION_FILE_LOCK_TIMEOUT 120
 #define SUBSCRIPTION_FILE_CHANGE_TIMEOUT 30
 
@@ -105,7 +105,7 @@
 		return -1;
 	}
 
-	fd_in = safe_open(path, O_RDONLY);
+	fd_in = nfs_safe_open(path, O_RDONLY);
 	if (fd_in == -1 && errno != ENOENT) {
 		subsfile_set_syscall_error(storage, "open()", path);
 		(void)file_dotlock_delete(&dotlock);
@@ -172,7 +172,7 @@
 	pool_t pool;
 	int fd;
 
-	fd = safe_open(path, O_RDONLY);
+	fd = nfs_safe_open(path, O_RDONLY);
 	if (fd == -1 && errno != ENOENT) {
 		subsfile_set_syscall_error(storage, "open()", path);
 		return NULL;
@@ -223,7 +223,7 @@
                    memory or try to play any guessing games. */
                 i_stream_destroy(&ctx->input);
 
-                fd = safe_open(ctx->path, O_RDONLY);
+                fd = nfs_safe_open(ctx->path, O_RDONLY);
                 if (fd == -1) {
                         /* In case of ENOENT all the subscriptions got lost.
                            Just return end of subscriptions list in that
--- a/src/lib/Makefile.am	Sun Feb 26 12:04:59 2006 +0200
+++ b/src/lib/Makefile.am	Sun Feb 26 12:15:39 2006 +0200
@@ -53,6 +53,7 @@
 	mmap-util.c \
 	module-dir.c \
 	network.c \
+	nfs-workarounds.c \
 	ostream.c \
 	ostream-file.c \
 	ostream-crlf.c \
@@ -65,7 +66,6 @@
 	restrict-process-size.c \
 	safe-memset.c \
 	safe-mkdir.c \
-	safe-open.c \
 	sendfile-util.c \
 	seq-range-array.c \
 	sha1.c \
@@ -122,6 +122,7 @@
 	mmap-util.h \
 	module-dir.h \
 	network.h \
+	nfs-workarounds.h \
 	ostream.h \
 	ostream-crlf.h \
 	ostream-internal.h \
@@ -134,7 +135,6 @@
 	restrict-process-size.h \
 	safe-memset.h \
 	safe-mkdir.h \
-	safe-open.h \
 	sendfile-util.h \
 	seq-range-array.h \
 	sha1.h \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/nfs-workarounds.c	Sun Feb 26 12:15:39 2006 +0200
@@ -0,0 +1,48 @@
+/* Copyright (c) 2006 Timo Sirainen */
+
+#include "lib.h"
+#include "nfs-workarounds.h"
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+int nfs_safe_open(const char *path, int flags)
+{
+        const char *dir = NULL;
+        struct stat st;
+        unsigned int i;
+        int fd;
+
+        i_assert((flags & O_CREAT) == 0);
+
+        t_push();
+        for (i = 1;; i++) {
+                fd = open(path, flags);
+                if (fd != -1 || errno != ESTALE || i == NFS_ESTALE_RETRY_COUNT)
+                        break;
+
+                /* ESTALE: Some operating systems may fail with this if they
+                   can't internally revalidating the NFS handle. It may also
+                   happen if the parent directory has been deleted. If the
+                   directory still exists, try reopening the file. */
+                if (dir == NULL) {
+                        dir = strrchr(path, '/');
+                        if (dir == NULL)
+                                break;
+                        dir = t_strdup_until(path, dir);
+                }
+                if (stat(dir, &st) < 0) {
+                        /* maybe it's gone or something else bad happened to
+                           it. in any case we can't open the file, so fail
+                           with the original ESTALE error and let our caller
+                           handle it. */
+                        errno = ESTALE;
+                        break;
+                }
+
+                /* directory still exists, try reopening */
+        }
+        t_pop();
+        return fd;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib/nfs-workarounds.h	Sun Feb 26 12:15:39 2006 +0200
@@ -0,0 +1,11 @@
+#ifndef __NFS_WORKAROUNDS_H
+#define __NFS_WORKAROUNDS_H
+
+/* When syscall fails with ESTALE error, how many times to try reopening the
+   file and retrying the operation. */
+#define NFS_ESTALE_RETRY_COUNT 10
+
+/* open() with some NFS workarounds */
+int nfs_safe_open(const char *path, int flags);
+
+#endif
--- a/src/lib/safe-open.c	Sun Feb 26 12:04:59 2006 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,50 +0,0 @@
-/* Copyright (c) 2006 Timo Sirainen */
-
-#include "lib.h"
-#include "safe-open.h"
-
-#include <fcntl.h>
-#include <unistd.h>
-#include <sys/stat.h>
-
-#define NFS_OPEN_RETRY_COUNT 10
-
-int safe_open(const char *path, int flags)
-{
-        const char *dir = NULL;
-        struct stat st;
-        unsigned int i;
-        int fd;
-
-        i_assert((flags & O_CREAT) == 0);
-
-        t_push();
-        for (i = 1;; i++) {
-                fd = open(path, flags);
-                if (fd != -1 || errno != ESTALE || i == NFS_OPEN_RETRY_COUNT)
-                        break;
-
-                /* ESTALE: Some operating systems may fail with this if they
-                   can't internally revalidating the NFS handle. It may also
-                   happen if the parent directory has been deleted. If the
-                   directory still exists, try reopening the file. */
-                if (dir == NULL) {
-                        dir = strrchr(path, '/');
-                        if (dir == NULL)
-                                break;
-                        dir = t_strdup_until(path, dir);
-                }
-                if (stat(dir, &st) < 0) {
-                        /* maybe it's gone or something else bad happened to
-                           it. in any case we can't open the file, so fail
-                           with the original ESTALE error and let our caller
-                           handle it. */
-                        errno = ESTALE;
-                        break;
-                }
-
-                /* directory still exists, try reopening */
-        }
-        t_pop();
-        return fd;
-}
--- a/src/lib/safe-open.h	Sun Feb 26 12:04:59 2006 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,7 +0,0 @@
-#ifndef __SAFE_OPEN_H
-#define __SAFE_OPEN_H
-
-/* open() with some NFS workarounds */
-int safe_open(const char *path, int flags);
-
-#endif