1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-stream-vfs.c : file system based stream */
5 * Authors: Srinivasa Ragavan <sragavan@novell.com>
7 * Copyright 2006 Novell, Inc. (www.novell.com)
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU Lesser General Public
11 * License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
32 #include <sys/types.h>
35 #include <glib/gstdio.h>
37 #include "camel-file-utils.h"
38 #include "camel-operation.h"
39 #include "camel-private.h"
40 #include "camel-stream-vfs.h"
42 static CamelSeekableStreamClass *parent_class = NULL;
44 /* Returns the class for a CamelStreamVFS */
45 #define CSVFS_CLASS(so) CAMEL_STREAM_VFS_CLASS (CAMEL_OBJECT_GET_CLASS(so))
47 static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n);
48 static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n);
49 /* static int stream_flush (CamelStream *stream); */
50 static int stream_close (CamelStream *stream);
51 static off_t stream_seek (CamelSeekableStream *stream, off_t offset,
52 CamelStreamSeekPolicy policy);
55 camel_stream_vfs_class_init (CamelStreamVFSClass *camel_stream_vfs_class)
57 CamelSeekableStreamClass *camel_seekable_stream_class =
58 CAMEL_SEEKABLE_STREAM_CLASS (camel_stream_vfs_class);
59 CamelStreamClass *camel_stream_class =
60 CAMEL_STREAM_CLASS (camel_stream_vfs_class);
62 parent_class = CAMEL_SEEKABLE_STREAM_CLASS (camel_type_get_global_classfuncs (camel_seekable_stream_get_type ()));
64 /* virtual method overload */
65 camel_stream_class->read = stream_read;
66 camel_stream_class->write = stream_write;
67 /* camel_stream_class->flush = stream_flush; */
68 camel_stream_class->close = stream_close;
70 camel_seekable_stream_class->seek = stream_seek;
74 camel_stream_vfs_init (gpointer object, gpointer klass)
76 CamelStreamVFS *stream = CAMEL_STREAM_VFS (object);
78 stream->handle = GINT_TO_POINTER (-1);
79 ((CamelSeekableStream *)stream)->bound_end = CAMEL_STREAM_UNBOUND;
83 camel_stream_vfs_finalize (CamelObject *object)
85 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (object);
87 if (stream_vfs->handle != GINT_TO_POINTER (-1))
88 gnome_vfs_close (stream_vfs->handle);
93 camel_stream_vfs_get_type (void)
95 static CamelType camel_stream_vfs_type = CAMEL_INVALID_TYPE;
97 if (camel_stream_vfs_type == CAMEL_INVALID_TYPE) {
98 camel_stream_vfs_type = camel_type_register (camel_seekable_stream_get_type (), "CamelStreamVFS",
99 sizeof (CamelStreamVFS),
100 sizeof (CamelStreamVFSClass),
101 (CamelObjectClassInitFunc) camel_stream_vfs_class_init,
103 (CamelObjectInitFunc) camel_stream_vfs_init,
104 (CamelObjectFinalizeFunc) camel_stream_vfs_finalize);
107 return camel_stream_vfs_type;
111 * camel_stream_vfs_new_with_handle:
112 * @handle: a GnomeVFS handle
114 * Creates a new fs stream using the given GnomeVFS handle @handle as the
115 * backing store. When the stream is destroyed, the file descriptor
118 * Returns a new #CamelStreamVFS
121 camel_stream_vfs_new_with_handle (GnomeVFSHandle *handle)
123 CamelStreamVFS *stream_vfs;
129 stream_vfs = CAMEL_STREAM_VFS (camel_object_new (camel_stream_vfs_get_type ()));
130 stream_vfs->handle = handle;
131 gnome_vfs_seek (handle, GNOME_VFS_SEEK_CURRENT, 0);
133 CAMEL_SEEKABLE_STREAM (stream_vfs)->position = offset;
135 return CAMEL_STREAM (stream_vfs);
139 * camel_stream_vfs_new_with_uri:
141 * @flags: flags as in open(2)
144 * Creates a new #CamelStreamVFS corresponding to the named file, flags,
147 * Returns the new stream, or %NULL on error.
150 camel_stream_vfs_new_with_uri (const char *name, int flags, mode_t mode)
152 GnomeVFSResult result;
153 GnomeVFSHandle *handle;
156 if (flags & O_WRONLY)
157 vfs_flag = vfs_flag | GNOME_VFS_OPEN_WRITE;
158 if (flags & O_RDONLY)
159 vfs_flag = vfs_flag | GNOME_VFS_OPEN_READ;
161 vfs_flag = vfs_flag | GNOME_VFS_OPEN_READ |GNOME_VFS_OPEN_WRITE;
164 result = gnome_vfs_create (&handle, name, vfs_flag, FALSE, mode);
166 result = gnome_vfs_open (&handle, name, vfs_flag);
168 if (result != GNOME_VFS_OK) {
172 return camel_stream_vfs_new_with_handle (handle);
176 stream_read (CamelStream *stream, char *buffer, size_t n)
178 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
179 CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
180 GnomeVFSFileSize nread = 0;
181 GnomeVFSResult result;
183 if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
184 n = MIN (seekable->bound_end - seekable->position, n);
186 result = gnome_vfs_read (stream_vfs->handle, buffer, n, &nread);
188 if (nread > 0 && result == GNOME_VFS_OK)
189 seekable->position += nread;
197 stream_write (CamelStream *stream, const char *buffer, size_t n)
199 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
200 CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
201 GnomeVFSFileSize nwritten = 0;
202 GnomeVFSResult result;
204 if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
205 n = MIN (seekable->bound_end - seekable->position, n);
207 result = gnome_vfs_write (stream_vfs->handle, buffer, n, &nwritten);
209 if (nwritten > 0 && result == GNOME_VFS_OK)
210 seekable->position += nwritten;
216 /* stream_flush (CamelStream *stream) */
218 /* return fsync(((CamelStreamVFS *)stream)->handle); */
222 stream_close (CamelStream *stream)
224 GnomeVFSResult result;
226 result = gnome_vfs_close(((CamelStreamVFS *)stream)->handle);
228 if (result != GNOME_VFS_OK)
231 ((CamelStreamVFS *)stream)->handle = NULL;
236 stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy policy)
238 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
239 GnomeVFSFileSize real = 0;
240 GnomeVFSResult result;
241 GnomeVFSHandle *handle = stream_vfs->handle;
244 case CAMEL_STREAM_SET:
247 case CAMEL_STREAM_CUR:
248 real = stream->position + offset;
250 case CAMEL_STREAM_END:
251 if (stream->bound_end == CAMEL_STREAM_UNBOUND) {
252 result = gnome_vfs_seek (handle, GNOME_VFS_SEEK_END, offset);
253 if (result != GNOME_VFS_OK)
255 gnome_vfs_tell (handle, &real);
257 if (real<stream->bound_start)
258 real = stream->bound_start;
259 stream->position = real;
263 real = stream->bound_end + offset;
267 if (stream->bound_end != CAMEL_STREAM_UNBOUND)
268 real = MIN (real, stream->bound_end);
269 real = MAX (real, stream->bound_start);
271 result = gnome_vfs_seek (handle, GNOME_VFS_SEEK_START, real);
272 if (result != GNOME_VFS_OK)
275 if (real != stream->position && ((CamelStream *)stream)->eos)
276 ((CamelStream *)stream)->eos = FALSE;
278 stream->position = real;