dovecot-2.2: lib-fs: Added a default fs_read() implementation us...

dovecot at dovecot.org dovecot at dovecot.org
Tue Dec 4 07:18:14 EET 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/674e45904706
changeset: 15448:674e45904706
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Dec 04 07:18:03 2012 +0200
description:
lib-fs: Added a default fs_read() implementation using the fs_read_stream().

diffstat:

 src/lib-fs/fs-api-private.h |   2 ++
 src/lib-fs/fs-api.c         |  28 +++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletions(-)

diffs (57 lines):

diff -r 4aabd11948d1 -r 674e45904706 src/lib-fs/fs-api-private.h
--- a/src/lib-fs/fs-api-private.h	Tue Dec 04 07:04:54 2012 +0200
+++ b/src/lib-fs/fs-api-private.h	Tue Dec 04 07:18:03 2012 +0200
@@ -64,6 +64,8 @@
 	struct fs *fs;
 	struct ostream *output;
 	char *path;
+
+	struct istream *pending_read_input;
 	bool write_pending;
 };
 
diff -r 4aabd11948d1 -r 674e45904706 src/lib-fs/fs-api.c
--- a/src/lib-fs/fs-api.c	Tue Dec 04 07:04:54 2012 +0200
+++ b/src/lib-fs/fs-api.c	Tue Dec 04 07:18:03 2012 +0200
@@ -4,6 +4,7 @@
 #include "array.h"
 #include "module-dir.h"
 #include "str.h"
+#include "istream.h"
 #include "ostream.h"
 #include "fs-api-private.h"
 
@@ -199,7 +200,32 @@
 
 ssize_t fs_read(struct fs_file *file, void *buf, size_t size)
 {
-	return file->fs->v.read(file, buf, size);
+	const unsigned char *data;
+	size_t data_size;
+	ssize_t ret;
+
+	if (file->fs->v.read != NULL)
+		return file->fs->v.read(file, buf, size);
+
+	/* backend didn't bother to implement read(), but we can do it with
+	   streams. */
+	if (file->pending_read_input == NULL)
+		file->pending_read_input = fs_read_stream(file, size+1);
+	ret = i_stream_read_data(file->pending_read_input,
+				 &data, &data_size, size-1);
+	if (ret == 0) {
+		fs_set_error_async(file->fs);
+		return -1;
+	}
+	if (ret < 0 && file->pending_read_input->stream_errno != 0) {
+		fs_set_error(file->fs, "read(%s) failed: %m",
+			     i_stream_get_name(file->pending_read_input));
+	} else {
+		ret = I_MIN(size, data_size);
+		memcpy(buf, data, ret);
+	}
+	i_stream_unref(&file->pending_read_input);
+	return ret;
 }
 
 struct istream *fs_read_stream(struct fs_file *file, size_t max_buffer_size)


More information about the dovecot-cvs mailing list