[dovecot-cvs] dovecot/src/lib nfs-workarounds.c, 1.1,
1.2 nfs-workarounds.h, 1.1, 1.2
tss-movial at dovecot.org
tss-movial at dovecot.org
Tue Apr 25 16:40:20 EEST 2006
Update of /var/lib/cvs/dovecot/src/lib
In directory talvi:/tmp/cvs-serv5366/src/lib
Modified Files:
nfs-workarounds.c nfs-workarounds.h
Log Message:
Added nfs_safe_stat() to transparently work around ESTALE with stat().
Index: nfs-workarounds.c
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/nfs-workarounds.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- nfs-workarounds.c 26 Feb 2006 10:15:39 -0000 1.1
+++ nfs-workarounds.c 25 Apr 2006 13:40:18 -0000 1.2
@@ -7,19 +7,19 @@
#include <unistd.h>
#include <sys/stat.h>
-int nfs_safe_open(const char *path, int flags)
+static int
+nfs_safe_do(const char *path, int (*callback)(const char *path, void *context),
+ void *context)
{
const char *dir = NULL;
struct stat st;
unsigned int i;
- int fd;
-
- i_assert((flags & O_CREAT) == 0);
+ int ret;
t_push();
for (i = 1;; i++) {
- fd = open(path, flags);
- if (fd != -1 || errno != ESTALE || i == NFS_ESTALE_RETRY_COUNT)
+ ret = callback(path, context);
+ if (ret == 0 || errno != ESTALE || i == NFS_ESTALE_RETRY_COUNT)
break;
/* ESTALE: Some operating systems may fail with this if they
@@ -44,5 +44,43 @@
/* directory still exists, try reopening */
}
t_pop();
- return fd;
+ return ret;
+}
+
+struct nfs_safe_open_context {
+ int flags;
+ int fd;
+};
+
+static int nfs_safe_open_callback(const char *path, void *context)
+{
+ struct nfs_safe_open_context *ctx = context;
+
+ ctx->fd = open(path, ctx->flags);
+ return ctx->fd == -1 ? -1 : 0;
+}
+
+int nfs_safe_open(const char *path, int flags)
+{
+ struct nfs_safe_open_context ctx;
+
+ i_assert((flags & O_CREAT) == 0);
+
+ ctx.flags = flags;
+ if (nfs_safe_do(path, nfs_safe_open_callback, &ctx) < 0)
+ return -1;
+
+ return ctx.fd;
+}
+
+static int nfs_safe_stat_callback(const char *path, void *context)
+{
+ struct stat *buf = context;
+
+ return stat(path, buf);
+}
+
+int nfs_safe_stat(const char *path, struct stat *buf)
+{
+ return nfs_safe_do(path, nfs_safe_stat_callback, buf);
}
Index: nfs-workarounds.h
===================================================================
RCS file: /var/lib/cvs/dovecot/src/lib/nfs-workarounds.h,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- nfs-workarounds.h 26 Feb 2006 10:15:39 -0000 1.1
+++ nfs-workarounds.h 25 Apr 2006 13:40:18 -0000 1.2
@@ -1,11 +1,15 @@
#ifndef __NFS_WORKAROUNDS_H
#define __NFS_WORKAROUNDS_H
+struct stat;
+
/* 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);
+/* stat() with some NFS workarounds */
+int nfs_safe_stat(const char *path, struct stat *buf);
#endif
More information about the dovecot-cvs
mailing list