1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; fill-column: 160 -*- */
/* camel-stream-fs.c : file system based stream */
/*
* Authors: Bertrand Guiheneuf <bertrand@helixcode.com>
* Michael Zucchi <notzed@helixcode.com>
*
* Copyright 1999, 2000 Helix Code, Inc. (http://www.helixcode.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
#include <config.h>
#include "camel-stream-fs.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include "camel-session.h" /* for camel_cancel_* */
static CamelSeekableStreamClass *parent_class = NULL;
/* Returns the class for a CamelStreamFS */
#define CSFS_CLASS(so) CAMEL_STREAM_FS_CLASS (CAMEL_OBJECT_GET_CLASS(so))
static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n);
static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n);
static int stream_flush (CamelStream *stream);
static int stream_close (CamelStream *stream);
static off_t stream_seek (CamelSeekableStream *stream, off_t offset,
CamelStreamSeekPolicy policy);
static void
camel_stream_fs_class_init (CamelStreamFsClass *camel_stream_fs_class)
{
CamelSeekableStreamClass *camel_seekable_stream_class =
CAMEL_SEEKABLE_STREAM_CLASS (camel_stream_fs_class);
CamelStreamClass *camel_stream_class =
CAMEL_STREAM_CLASS (camel_stream_fs_class);
parent_class = CAMEL_SEEKABLE_STREAM_CLASS (camel_type_get_global_classfuncs (camel_seekable_stream_get_type ()));
/* virtual method overload */
camel_stream_class->read = stream_read;
camel_stream_class->write = stream_write;
camel_stream_class->flush = stream_flush;
camel_stream_class->close = stream_close;
camel_seekable_stream_class->seek = stream_seek;
}
static void
camel_stream_fs_init (gpointer object, gpointer klass)
{
CamelStreamFs *stream = CAMEL_STREAM_FS (object);
stream->fd = -1;
((CamelSeekableStream *)stream)->bound_end = CAMEL_STREAM_UNBOUND;
}
static void
camel_stream_fs_finalize (CamelObject *object)
{
CamelStreamFs *stream_fs = CAMEL_STREAM_FS (object);
if (stream_fs->fd != -1)
close (stream_fs->fd);
}
CamelType
camel_stream_fs_get_type (void)
{
static CamelType camel_stream_fs_type = CAMEL_INVALID_TYPE;
if (camel_stream_fs_type == CAMEL_INVALID_TYPE) {
camel_stream_fs_type = camel_type_register (camel_seekable_stream_get_type (), "CamelStreamFs",
sizeof (CamelStreamFs),
sizeof (CamelStreamFsClass),
(CamelObjectClassInitFunc) camel_stream_fs_class_init,
NULL,
(CamelObjectInitFunc) camel_stream_fs_init,
(CamelObjectFinalizeFunc) camel_stream_fs_finalize);
}
return camel_stream_fs_type;
}
/**
* camel_stream_fs_new_with_fd:
* @fd: a file descriptor
*
* Returns a stream associated with the given file descriptor.
* When the stream is destroyed, the file descriptor will be closed.
*
* Return value: the stream
**/
CamelStream *
camel_stream_fs_new_with_fd (int fd)
{
CamelStreamFs *stream_fs;
off_t offset;
if (fd == -1)
return NULL;
stream_fs = CAMEL_STREAM_FS (camel_object_new (camel_stream_fs_get_type ()));
stream_fs->fd = fd;
offset = lseek (fd, 0, SEEK_CUR);
if (offset == -1)
offset = 0;
CAMEL_SEEKABLE_STREAM (stream_fs)->position = offset;
return CAMEL_STREAM (stream_fs);
}
/**
* camel_stream_fs_new_with_fd_and_bounds:
* @fd: a file descriptor
* @start: the first valid position in the file
* @end: the first invalid position in the file, or CAMEL_STREAM_UNBOUND
*
* Returns a stream associated with the given file descriptor and bounds.
* When the stream is destroyed, the file descriptor will be closed.
*
* Return value: the stream
**/
CamelStream *
camel_stream_fs_new_with_fd_and_bounds (int fd, off_t start, off_t end)
{
CamelStream *stream;
stream = camel_stream_fs_new_with_fd (fd);
camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream), start, end);
return stream;
}
/**
* camel_stream_fs_new_with_name:
* @name: a local filename
* @flags: flags as in open(2)
* @mode: a file mode
*
* Creates a new CamelStream corresponding to the named file, flags,
* and mode.
*
* Return value: the stream, or #NULL on error.
**/
CamelStream *
camel_stream_fs_new_with_name (const char *name, int flags, mode_t mode)
{
int fd;
fd = open (name, flags, mode);
if (fd == -1) {
return NULL;
}
return camel_stream_fs_new_with_fd (fd);
}
/**
* camel_stream_fs_new_with_name_and_bounds:
* @name: a local filename
* @flags: flags as in open(2)
* @mode: a file mode
* @start: the first valid position in the file
* @end: the first invalid position in the file, or CAMEL_STREAM_UNBOUND
*
* Creates a new CamelStream corresponding to the given arguments.
*
* Return value: the stream, or NULL on error.
**/
CamelStream *
camel_stream_fs_new_with_name_and_bounds (const char *name, int flags,
mode_t mode, off_t start, off_t end)
{
CamelStream *stream;
stream = camel_stream_fs_new_with_name (name, flags, mode);
if (stream == NULL)
return NULL;
camel_seekable_stream_set_bounds (CAMEL_SEEKABLE_STREAM (stream),
start, end);
return stream;
}
static ssize_t
stream_read (CamelStream *stream, char *buffer, size_t n)
{
CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
ssize_t nread;
int cancel_fd;
if (camel_cancel_check(NULL)) {
errno = EINTR;
return -1;
}
if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
n = MIN (seekable->bound_end - seekable->position, n);
cancel_fd = camel_cancel_fd(NULL);
if (cancel_fd == -1) {
do {
nread = read (stream_fs->fd, buffer, n);
} while (nread == -1 && errno == EINTR);
} else {
fd_set rdset;
int flags, fdmax;
flags = fcntl(stream_fs->fd, F_GETFL);
fcntl(stream_fs->fd, F_SETFL, flags | O_NONBLOCK);
FD_ZERO(&rdset);
FD_SET(stream_fs->fd, &rdset);
FD_SET(cancel_fd, &rdset);
fdmax = MAX(stream_fs->fd, cancel_fd)+1;
select(fdmax, &rdset, 0, 0, NULL);
if (FD_ISSET(cancel_fd, &rdset)) {
fcntl(stream_fs->fd, F_SETFL, flags);
errno = EINTR;
return -1;
}
nread = read(stream_fs->fd, buffer, n);
fcntl(stream_fs->fd, F_SETFL, flags);
}
if (nread > 0)
seekable->position += nread;
else if (nread == 0)
stream->eos = TRUE;
return nread;
}
static ssize_t
stream_write (CamelStream *stream, const char *buffer, size_t n)
{
CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
CamelSeekableStream *seekable = CAMEL_SEEKABLE_STREAM (stream);
ssize_t v, written = 0;
int cancel_fd;
if (camel_cancel_check(NULL)) {
errno = EINTR;
return -1;
}
if (seekable->bound_end != CAMEL_STREAM_UNBOUND)
n = MIN (seekable->bound_end - seekable->position, n);
cancel_fd = camel_cancel_fd(NULL);
if (cancel_fd == -1) {
do {
v = write (stream_fs->fd, buffer+written, n-written);
if (v > 0)
written += v;
} while (v == -1 && errno == EINTR);
} else {
fd_set rdset, wrset;
int flags, fdmax;
flags = fcntl(stream_fs->fd, F_GETFL);
fcntl(stream_fs->fd, F_SETFL, flags | O_NONBLOCK);
fdmax = MAX(stream_fs->fd, cancel_fd)+1;
do {
FD_ZERO(&rdset);
FD_ZERO(&wrset);
FD_SET(stream_fs->fd, &wrset);
FD_SET(cancel_fd, &rdset);
select(fdmax, &rdset, &wrset, 0, NULL);
if (FD_ISSET(cancel_fd, &rdset)) {
fcntl(stream_fs->fd, F_SETFL, flags);
errno = EINTR;
return -1;
}
v = write(stream_fs->fd, buffer+written, n-written);
if (v>0)
written += v;
} while (v != -1 && written < n);
fcntl(stream_fs->fd, F_SETFL, flags);
}
if (written > 0)
seekable->position += written;
else if (v == -1)
return -1;
return written;
}
static int
stream_flush (CamelStream *stream)
{
return fsync(((CamelStreamFs *)stream)->fd);
}
static int
stream_close (CamelStream *stream)
{
if (close (((CamelStreamFs *)stream)->fd) == -1)
return -1;
((CamelStreamFs *)stream)->fd = -1;
return 0;
}
static off_t
stream_seek (CamelSeekableStream *stream, off_t offset, CamelStreamSeekPolicy policy)
{
CamelStreamFs *stream_fs = CAMEL_STREAM_FS (stream);
off_t real = 0;
switch (policy) {
case CAMEL_STREAM_SET:
real = offset;
break;
case CAMEL_STREAM_CUR:
real = stream->position + offset;
break;
case CAMEL_STREAM_END:
if (stream->bound_end == CAMEL_STREAM_UNBOUND) {
real = lseek(stream_fs->fd, offset, SEEK_END);
if (real != -1) {
if (real<stream->bound_start)
real = stream->bound_start;
stream->position = real;
}
return real;
}
real = stream->bound_end + offset;
break;
}
if (stream->bound_end != CAMEL_STREAM_UNBOUND)
real = MIN (real, stream->bound_end);
real = MAX (real, stream->bound_start);
real = lseek(stream_fs->fd, real, SEEK_SET);
if (real == -1)
return -1;
if (real != stream->position && ((CamelStream *)stream)->eos)
((CamelStream *)stream)->eos = FALSE;
stream->position = real;
return real;
}
|