dovecot-2.2: lib-fs: posix fs backend now closes the fd after re...

dovecot at dovecot.org dovecot at dovecot.org
Sun Apr 7 23:26:58 EEST 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/73bef641620d
changeset: 16237:73bef641620d
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Apr 07 23:26:52 2013 +0300
description:
lib-fs: posix fs backend now closes the fd after reads are finished.
This allows keeping more fs_file structs open than there are available fds.

diffstat:

 src/lib-fs/fs-posix.c |  32 +++++++++++++++++++-------------
 1 files changed, 19 insertions(+), 13 deletions(-)

diffs (64 lines):

diff -r 81d87e43e167 -r 73bef641620d src/lib-fs/fs-posix.c
--- a/src/lib-fs/fs-posix.c	Sun Apr 07 23:17:37 2013 +0300
+++ b/src/lib-fs/fs-posix.c	Sun Apr 07 23:26:52 2013 +0300
@@ -252,6 +252,16 @@
 	return &file->file;
 }
 
+static void fs_posix_file_close(struct posix_fs_file *file)
+{
+	if (file->fd != -1 && file->file.output == NULL) {
+		if (close(file->fd) < 0) {
+			fs_set_critical(file->file.fs, "close(%s) failed: %m",
+					file->file.path);
+		}
+	}
+}
+
 static void fs_posix_file_deinit(struct fs_file *_file)
 {
 	struct posix_fs_file *file = (struct posix_fs_file *)_file;
@@ -275,12 +285,7 @@
 		break;
 	}
 
-	if (file->fd != -1) {
-		if (close(file->fd) < 0) {
-			fs_set_critical(_file->fs, "close(%s) failed: %m",
-					_file->path);
-		}
-	}
+	fs_posix_file_close(file);
 	i_free(file->temp_path);
 	i_free(file->file.path);
 	i_free(file);
@@ -327,6 +332,7 @@
 	ret = read(file->fd, buf, size);
 	if (ret < 0)
 		fs_set_error(_file->fs, "read(%s) failed: %m", _file->path);
+	fs_posix_file_close(file);
 	return ret;
 }
 
@@ -336,14 +342,14 @@
 	struct posix_fs_file *file = (struct posix_fs_file *)_file;
 	struct istream *input;
 
-	if (file->fd == -1) {
-		if (fs_posix_open(file) < 0) {
-			input = i_stream_create_error(errno);
-			i_stream_set_name(input, _file->path);
-			return input;
-		}
+	if (file->fd == -1 && fs_posix_open(file) < 0) {
+		input = i_stream_create_error(errno);
+		i_stream_set_name(input, _file->path);
+	} else {
+		input = i_stream_create_fd(file->fd, max_buffer_size, FALSE);
 	}
-	return i_stream_create_fd(file->fd, max_buffer_size, FALSE);
+	i_stream_add_destroy_callback(input, fs_posix_file_close, file);
+	return input;
 }
 
 static int fs_posix_write_finish(struct posix_fs_file *file)


More information about the dovecot-cvs mailing list