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 (C) 1999-2008 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
33 #include "camel-file-utils.h"
34 #include "camel-operation.h"
35 #include "camel-stream-vfs.h"
37 static CamelStreamClass *parent_class = NULL;
41 stream_vfs_finalize (CamelStreamVFS *stream_vfs)
43 if (stream_vfs->stream)
44 g_object_unref (stream_vfs->stream);
48 stream_vfs_read (CamelStream *stream,
52 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
56 nread = g_input_stream_read (
57 G_INPUT_STREAM (stream_vfs->stream),
58 buffer, n, NULL, &error);
60 if (nread == 0 || error != NULL)
64 g_warning ("%s", error->message);
72 stream_vfs_write (CamelStream *stream,
76 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
81 success = g_output_stream_write_all (
82 G_OUTPUT_STREAM (stream_vfs->stream),
83 buffer, n, &bytes_written, NULL, &error);
86 g_warning ("%s", error->message);
90 return success ? bytes_written : -1;
94 stream_vfs_flush (CamelStream *stream)
96 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
100 success = g_output_stream_flush (
101 G_OUTPUT_STREAM (stream_vfs->stream), NULL, &error);
104 g_warning ("%s", error->message);
105 g_error_free (error);
108 return success ? 0 : -1;
112 stream_vfs_close (CamelStream *stream)
114 CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
116 GError *error = NULL;
118 if (G_IS_OUTPUT_STREAM (stream_vfs->stream))
119 success = g_output_stream_close (
120 G_OUTPUT_STREAM (stream_vfs->stream), NULL, &error);
122 success = g_input_stream_close (
123 G_INPUT_STREAM (stream_vfs->stream), NULL, &error);
126 g_warning ("%s", error->message);
127 g_error_free (error);
131 g_object_unref (stream_vfs->stream);
132 stream_vfs->stream = NULL;
135 return success ? 0 : -1;
139 camel_stream_vfs_class_init (CamelStreamVFSClass *class)
141 CamelStreamClass *stream_class;
143 parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_stream_get_type ()));
145 stream_class = CAMEL_STREAM_CLASS (class);
146 stream_class->read = stream_vfs_read;
147 stream_class->write = stream_vfs_write;
148 stream_class->flush = stream_vfs_flush;
149 stream_class->close = stream_vfs_close;
153 camel_stream_vfs_init (CamelStreamVFS *stream)
155 stream->stream = NULL;
159 camel_stream_vfs_get_type (void)
161 static CamelType camel_stream_vfs_type = CAMEL_INVALID_TYPE;
163 if (camel_stream_vfs_type == CAMEL_INVALID_TYPE) {
164 camel_stream_vfs_type = camel_type_register (camel_stream_get_type (), "CamelStreamVFS",
165 sizeof (CamelStreamVFS),
166 sizeof (CamelStreamVFSClass),
167 (CamelObjectClassInitFunc) camel_stream_vfs_class_init,
169 (CamelObjectInitFunc) camel_stream_vfs_init,
170 (CamelObjectFinalizeFunc) stream_vfs_finalize);
173 return camel_stream_vfs_type;
177 * camel_stream_vfs_new_with_stream:
178 * @stream: a GInputStream or GOutputStream instance
180 * Creates a new fs stream using the given gio stream @stream as the
181 * backing store. When the stream is destroyed, the file descriptor
182 * will be closed. This will not increase reference counter on the stream.
184 * Returns: a new #CamelStreamVFS
189 camel_stream_vfs_new_with_stream (GObject *stream)
191 CamelStreamVFS *stream_vfs;
198 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream) || G_IS_INPUT_STREAM (stream), NULL);
201 stream_vfs = CAMEL_STREAM_VFS (camel_object_new (camel_stream_vfs_get_type ()));
202 stream_vfs->stream = stream;
204 return CAMEL_STREAM (stream_vfs);
208 * camel_stream_vfs_new_with_uri:
210 * @mode: opening mode for the uri file
212 * Creates a new #CamelStreamVFS corresponding to the named file and mode.
214 * Returns: the new stream, or %NULL on error.
217 camel_stream_vfs_new_with_uri (const gchar *uri, CamelStreamVFSOpenMethod mode)
221 GError *error = NULL;
223 file = g_file_new_for_uri (uri);
226 case CAMEL_STREAM_VFS_CREATE:
227 stream = G_OBJECT (g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error));
229 case CAMEL_STREAM_VFS_APPEND:
230 stream = G_OBJECT (g_file_append_to (file, G_FILE_CREATE_NONE, NULL, &error));
232 case CAMEL_STREAM_VFS_READ:
233 stream = G_OBJECT (g_file_read (file, NULL, &error));
237 g_return_val_if_reached (NULL);
240 g_object_unref (file);
244 g_warning ("%s", error->message);
245 g_error_free (error);
249 return camel_stream_vfs_new_with_stream (stream);
253 * camel_stream_vfs_is_writable:
254 * @stream_vfs: a #CamelStreamVFS instance
256 * Returns: whether is the underlying stream writable or not.
261 camel_stream_vfs_is_writable (CamelStreamVFS *stream_vfs)
263 g_return_val_if_fail (stream_vfs != NULL, FALSE);
264 g_return_val_if_fail (stream_vfs->stream != NULL, FALSE);
266 return G_IS_OUTPUT_STREAM (stream_vfs->stream);