1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* camel-stream-fs.c : file system based stream */
5 * Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
6 * Michael Zucchi <notzed@ximian.com>
8 * Copyright 1999-2003 Ximian, Inc. (www.ximian.com)
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU Lesser General Public
12 * License as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
33 #include <sys/types.h>
36 #include <glib/gstdio.h>
38 #include "camel-file-utils.h"
39 #include "camel-operation.h"
40 #include "camel-private.h"
41 #include "camel-stream-fs.h"
43 static CamelSeekableStreamClass *parent_class = NULL;
45 /* Returns the class for a CamelStreamFS */
46 #define CSFS_CLASS(so) CAMEL_STREAM_FS_CLASS (CAMEL_OBJECT_GET_CLASS(so))
48 static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n);
49 static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n);
50 static int stream_flush (CamelStream *stream);
51 static int stream_close (CamelStream *stream);
52 static off_t stream_seek (CamelSeekableStream *stream, off_t offset,
53 CamelStreamSeekPolicy policy);
56 camel_stream_fs_class_init (CamelStreamFsClass *camel_stream_fs_class)
58 CamelSeekableStreamClass *camel_seekable_stream_class =
59 CAMEL_SEEKABLE_STREAM_CLASS (camel_stream_fs_class);
60 CamelStreamClass *camel_stream_class =
61 CAMEL_STREAM_CLASS (camel_stream_fs_class);
63 parent_class = CAMEL_SEEKABLE_STREAM_CLASS (camel_type_get_global_classfuncs (camel_seekable_stream_get_type ()));
65 /* virtual method overload */
66 camel_stream_class->read = stream_read;
67 camel_stream_class->write = stream_write;
68 camel_stream_class->flush = stream_flush;
69 camel_stream_class->close = stream_close;
71 camel_seekable_stream_class->seek = stream_seek;
75 camel_stream_fs_init (gpointer object, gpointer klass)
77 CamelStreamFs *stream = CAMEL_STREAM_FS (object);
80 ((CamelSeekableStream *)stream)->bound_end = CAMEL_STREAM_UNBOUND;
84 camel_stream_fs_finalize (CamelObject *object)
86 CamelStreamFs *stream_fs = CAMEL_STREAM_FS (object);
88 if (stream_fs->fd != -1)
89 close (stream_fs->fd);
94 camel_stream_fs_get_type (void)
96 static CamelType camel_stream_fs_type = CAMEL_INVALID_TYPE;
98 if (camel_stream_fs_type == CAMEL_INVALID_TYPE) {
99 camel_stream_fs_type = camel_type_register (camel_seekable_stream_get_type (), "CamelStreamFs",
100 sizeof (CamelStreamFs),
101 sizeof (CamelStreamFsClass),
102 (CamelObjectClassInitFunc) camel_stream_fs_class_init,
104 (CamelObjectInitFunc) camel_stream_fs_init,
105 (CamelObjectFinalizeFunc) camel_stream_fs_finalize);
108 return camel_stream_fs_type;
112 * camel_stream_fs_new_with_fd:
113 * @fd: a file descriptor
115 * Creates a new fs stream using the given file descriptor @fd as the
116 * backing store. When the stream is destroyed, the file descriptor
119 * Returns a new #CamelStreamFs
122 camel_stream_fs_new_with_fd (int fd)
124 CamelStreamFs *stream_fs;
130 stream_fs = CAMEL_STREAM_FS (camel_object_new (camel_stream_fs_get_type ()));
132 offset = lseek (fd, 0, SEEK_CUR);
135 CAMEL_SEEKABLE_STREAM (stream_fs)->position = offset;
137 return CAMEL_STREAM (stream_fs);
141 * camel_stream_fs_new_with_fd_and_bounds:
142 * @fd: a file descriptor
143 * @start: the first valid position in the file
144 * @end: the first invalid position in the file, or #CAMEL_STREAM_UNBOUND
146 * Gets a stream associated with the given file descriptor and bounds.
147 * When the stream is destroyed, the file descriptor will be closed.
149 * Returns the bound stream
152 camel_stream_fs_new_with_fd_and_bounds (int fd, off_t start, off_t end)
156 stream = camel_stream_fs_new_with_fd (fd);
157 camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream), start, end);
163 * camel_stream_fs_new_with_name:
164 * @name: a local filename
165 * @flags: flags as in open(2)
168 * Creates a new #CamelStreamFs corresponding to the named file, flags,
171 * Returns the new stream, or %NULL on error.
174 camel_stream_fs_new_with_name (const char *name, int flags, mode_t mode)
178 fd = g_open (name, flags|O_BINARY, mode);
183 return camel_stream_fs_new_with_fd (fd);
187 * camel_stream_fs_new_with_name_and_bounds:
188 * @name: a local filename
189 * @flags: flags as in open(2)
191 * @start: the first valid position in the file
192 * @end: the first invalid position in the file, or #CAMEL_STREAM_UNBOUND
194 * Creates a new CamelStream corresponding to the given arguments.
196 * Returns the stream, or %NULL on error.
199 camel_stream_fs_new_with_name_and_bounds (const char *name, int flags,
200 mode_t mode, off_t start, off_t end)
204 stream = camel_stream_fs_new_with_name (name, flags, mode);
208 camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream),
216 stream_read (CamelStream *stream, char *buffer, size_t n)
218 CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
219 CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
222 if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
223 n = MIN (seekable->bound_end - seekable->position, n);
225 if ((nread = camel_read (stream_fs->fd, buffer, n)) > 0)
226 seekable->position += nread;
234 stream_write (CamelStream *stream, const char *buffer, size_t n)
236 CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
237 CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
240 if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
241 n = MIN (seekable->bound_end - seekable->position, n);
243 if ((nwritten = camel_write (stream_fs->fd, buffer, n)) > 0)
244 seekable->position += nwritten;
250 stream_flush (CamelStream *stream)
252 return fsync(((CamelStreamFs *)stream)->fd);
256 stream_close (CamelStream *stream)
258 if (close (((CamelStreamFs *)stream)->fd) == -1)
261 ((CamelStreamFs *)stream)->fd = -1;
266 stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy policy)
268 CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
272 case CAMEL_STREAM_SET:
275 case CAMEL_STREAM_CUR:
276 real = stream->position + offset;
278 case CAMEL_STREAM_END:
279 if (stream->bound_end == CAMEL_STREAM_UNBOUND) {
280 real = lseek(stream_fs->fd, offset, SEEK_END);
282 if (real<stream->bound_start)
283 real = stream->bound_start;
284 stream->position = real;
288 real = stream->bound_end + offset;
292 if (stream->bound_end != CAMEL_STREAM_UNBOUND)
293 real = MIN (real, stream->bound_end);
294 real = MAX (real, stream->bound_start);
296 real = lseek(stream_fs->fd, real, SEEK_SET);
300 if (real != stream->position && ((CamelStream *)stream)->eos)
301 ((CamelStream *)stream)->eos = FALSE;
303 stream->position = real;