Commit 3dea5890 3dea5890547c7ec7f941660d011fb82fed1b174a by Alain Magloire

In order to provide inheritance, it is necessary to make

	the base functions/methods visible so the extended object
	could reuse some base methods if necessary.  Inheritance
	is possible because we always add to the end of the vtable
	structure new functions.
	For example, mime_t object will be also a message_t.
	and it will be possible to do
	{
	  mime_t mime;
	  header_t header;
	  ...
	  message_get_header ((message_t)mime, header);
	}
	Another example is tcpstream(socket stream) extends
	fdstream(file description stream);
	Also the name of the functions have change to match better
	there purpose.

	* mailbox2/Makefile.am: lockfile.c added.
	* mailbox2/bstream.c: Reoganisation to make the
	functions visible.
	* mailbox2/dattribute.c: Likewise.
	* mailbox2/dotlock.c: Likewise.
	* mailbox2/fdstream.c: Likewise.
	* mailbox2/fstream.c: Likewise.
	* mailbox2/mapstream.c: Likewise.
	* mailbox2/memstream.c: Likewise.
	* mailbox2/pticket.c: Likewise.
	* mailbox2/sdebug.c: Likewise.
	* mailbox2/tcpstream.c: Likewise.

	* mailbox2/include/mailutils/Makefile.am: lockfile.h added.
	* mailbox2/include/mailutils/attribute.h: Add the new prototypes.
	* mailbox2/include/mailutils/mbox.h: Add the new prototypes.
	* mailbox2/include/mailutils/stream.h: Add the new prototypes.

	* mailbox2/include/mailutils/sys/Makefile.am: dattribute.h lockfile.h
	mapstream.h pticket.h added.
	* mailbox2/include/mailutils/sys/bstream.h: Add new prototypes.
	* mailbox2/include/mailutils/sys/fdstream.h: Likewise.
	* mailbox2/include/mailutils/sys/fstream.h: Likewise.
	* mailbox2/include/mailutils/sys/mbox.h: Likewise.
	* mailbox2/include/mailutils/sys/memstream.h: Likewise.
	* mailbox2/include/mailutils/sys/sdebug.h: Likewise.
	* mailbox2/include/mailutils/sys/tcpstream.h: Likewise.
1 parent 1e6107e6
1 2001-09-22 Alain Magloire
2
3 In order to provide inheritance, it is necessary to make
4 the base functions/methods visible so the extended object
5 could reuse some base methods if necessary. Inheritance
6 is possible because we always add to the end of the vtable
7 structure new functions.
8 For example, mime_t object will be also a message_t.
9 and it will be possible to do
10 {
11 mime_t mime;
12 header_t header;
13 ...
14 message_get_header ((message_t)mime, header);
15 }
16 Another example is tcpstream(socket stream) extends
17 fdstream(file description stream);
18 Also the name of the functions have change to match better
19 there purpose.
20
21 * mailbox2/Makefile.am: lockfile.c added.
22 * mailbox2/bstream.c: Reoganisation to make the
23 functions visible.
24 * mailbox2/dattribute.c: Likewise.
25 * mailbox2/dotlock.c: Likewise.
26 * mailbox2/fdstream.c: Likewise.
27 * mailbox2/fstream.c: Likewise.
28 * mailbox2/mapstream.c: Likewise.
29 * mailbox2/memstream.c: Likewise.
30 * mailbox2/pticket.c: Likewise.
31 * mailbox2/sdebug.c: Likewise.
32 * mailbox2/tcpstream.c: Likewise.
33
34 * mailbox2/include/mailutils/Makefile.am: lockfile.h added.
35 * mailbox2/include/mailutils/attribute.h: Add the new prototypes.
36 * mailbox2/include/mailutils/mbox.h: Add the new prototypes.
37 * mailbox2/include/mailutils/stream.h: Add the new prototypes.
38
39 * mailbox2/include/mailutils/sys/Makefile.am: dattribute.h lockfile.h
40 mapstream.h pticket.h added.
41 * mailbox2/include/mailutils/sys/bstream.h: Add new prototypes.
42 * mailbox2/include/mailutils/sys/fdstream.h: Likewise.
43 * mailbox2/include/mailutils/sys/fstream.h: Likewise.
44 * mailbox2/include/mailutils/sys/mbox.h: Likewise.
45 * mailbox2/include/mailutils/sys/memstream.h: Likewise.
46 * mailbox2/include/mailutils/sys/sdebug.h: Likewise.
47 * mailbox2/include/mailutils/sys/tcpstream.h: Likewise.
48
1 2001-09-20 Sergey Poznyakoff 49 2001-09-20 Sergey Poznyakoff
2 50
3 * frm/frm.c: Print newline not depending on the presence 51 * frm/frm.c: Print newline not depending on the presence
...@@ -21,22 +69,21 @@ ...@@ -21,22 +69,21 @@
21 * mailbox2/sdebug.c (mu_debug_stream_create): New function 69 * mailbox2/sdebug.c (mu_debug_stream_create): New function
22 replace mu_debug_stdio_create(). 70 replace mu_debug_stdio_create().
23 * mailbox2/fdstream.c (_fds_desroy): Do not close the stream 71 * mailbox2/fdstream.c (_fds_desroy): Do not close the stream
24 when distroying, it should have been done explicitely with 72 when destroying, it should have been done explicitely with
25 stream_close (); 73 stream_close ();
26 * mailbox2/fstream.c (_fs_desroy): Do not close the stream 74 * mailbox2/fstream.c (_fs_desroy): Do not close the stream
27 when distroying, it should have been done explicitely with 75 when destroying, it should have been done explicitely with
28 stream_close (); 76 stream_close ();
29 * mailbox2/mapstream.c (_map_desroy): Do not close the stream 77 * mailbox2/mapstream.c (_map_desroy): Do not close the stream
30 when distroying, it should have been done explicitely with 78 when destroying, it should have been done explicitely with
31 stream_close (); 79 stream_close ();
32 * mailbox2/memstream.c (_memory_desroy): Do not close the stream 80 * mailbox2/memstream.c (_memory_desroy): Do not close the stream
33 when distroying, it should have been done explicitely with 81 when destroying, it should have been done explicitely with
34 stream_close (); 82 stream_close ();
35 * mailbox2/tcpstream.c (_tcp_desroy): Do not close the stream 83 * mailbox2/tcpstream.c (_tcp_desroy): Do not close the stream
36 when distroying, it should have been done explicitely with 84 when destroying, it should have been done explicitely with
37 stream_close (); 85 stream_close ();
38 86
39
40 2001-09-18 Sam Roberts 87 2001-09-18 Sam Roberts
41 88
42 * mailbox/mbx_pop.c: Check url authentication type at 89 * mailbox/mbx_pop.c: Check url authentication type at
...@@ -2144,7 +2191,7 @@ ...@@ -2144,7 +2191,7 @@
2144 2001-04-23 Alain Magloire 2191 2001-04-23 Alain Magloire
2145 2192
2146 Sergey Poznyakoff noted: When the user's mailbox has zero size, mmap 2193 Sergey Poznyakoff noted: When the user's mailbox has zero size, mmap
2147 fails on Solaris. On GNU/Linux it reuturn NULL buf subsequent munmap 2194 fails on Solaris. On GNU/Linux it return NULL buf subsequent munmap
2148 fails. 2195 fails.
2149 2196
2150 * mailbox/mapfile_stream.c: To take care of this, mfs->ptr is set 2197 * mailbox/mapfile_stream.c: To take care of this, mfs->ptr is set
......
...@@ -25,7 +25,7 @@ libmailbox_la_SOURCES = \ ...@@ -25,7 +25,7 @@ libmailbox_la_SOURCES = \
25 header.c \ 25 header.c \
26 iterator.c \ 26 iterator.c \
27 list.c \ 27 list.c \
28 locker.c \ 28 lockfile.c \
29 mailbox.c \ 29 mailbox.c \
30 md5-rsa.c \ 30 md5-rsa.c \
31 memstream.c \ 31 memstream.c \
......
...@@ -37,14 +37,14 @@ ...@@ -37,14 +37,14 @@
37 #include <mailutils/sys/bstream.h> 37 #include <mailutils/sys/bstream.h>
38 38
39 static void 39 static void
40 _bs_cleanup (void *arg) 40 _stream_buffer_cleanup (void *arg)
41 { 41 {
42 struct _bs *bs = arg; 42 struct _stream_buffer *bs = arg;
43 mu_refcount_unlock (bs->refcount); 43 mu_refcount_unlock (bs->refcount);
44 } 44 }
45 45
46 static int 46 static int
47 refill (struct _bs *bs) 47 refill (struct _stream_buffer *bs)
48 { 48 {
49 int status; 49 int status;
50 if (bs->rbuffer.base == NULL) 50 if (bs->rbuffer.base == NULL)
...@@ -60,17 +60,17 @@ refill (struct _bs *bs) ...@@ -60,17 +60,17 @@ refill (struct _bs *bs)
60 return status; 60 return status;
61 } 61 }
62 62
63 static int 63 int
64 _bs_ref (stream_t stream) 64 _stream_buffer_ref (stream_t stream)
65 { 65 {
66 struct _bs *bs = (struct _bs *)stream; 66 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
67 return mu_refcount_inc (bs->refcount); 67 return mu_refcount_inc (bs->refcount);
68 } 68 }
69 69
70 static void 70 void
71 _bs_destroy (stream_t *pstream) 71 _stream_buffer_destroy (stream_t *pstream)
72 { 72 {
73 struct _bs *bs = (struct _bs *)*pstream; 73 struct _stream_buffer *bs = (struct _stream_buffer *)*pstream;
74 if (mu_refcount_dec (bs->refcount) == 0) 74 if (mu_refcount_dec (bs->refcount) == 0)
75 { 75 {
76 stream_destroy (&bs->stream); 76 stream_destroy (&bs->stream);
...@@ -79,17 +79,17 @@ _bs_destroy (stream_t *pstream) ...@@ -79,17 +79,17 @@ _bs_destroy (stream_t *pstream)
79 } 79 }
80 } 80 }
81 81
82 static int 82 int
83 _bs_open (stream_t stream, const char *name, int port, int flags) 83 _stream_buffer_open (stream_t stream, const char *name, int port, int flags)
84 { 84 {
85 struct _bs *bs = (struct _bs *)stream; 85 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
86 return stream_open (bs->stream, name, port, flags); 86 return stream_open (bs->stream, name, port, flags);
87 } 87 }
88 88
89 static int 89 int
90 _bs_close (stream_t stream) 90 _stream_buffer_close (stream_t stream)
91 { 91 {
92 struct _bs *bs = (struct _bs *)stream; 92 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
93 mu_refcount_lock (bs->refcount); 93 mu_refcount_lock (bs->refcount);
94 /* Clear the buffer of any residue left. */ 94 /* Clear the buffer of any residue left. */
95 if (bs->rbuffer.base && bs->rbuffer.bufsize) 95 if (bs->rbuffer.base && bs->rbuffer.bufsize)
...@@ -106,11 +106,11 @@ _bs_close (stream_t stream) ...@@ -106,11 +106,11 @@ _bs_close (stream_t stream)
106 use as a fully fledge buffer mechanism. It is a simple mechanims for 106 use as a fully fledge buffer mechanism. It is a simple mechanims for
107 networking. Lots of code between POP and IMAP can be share this way. 107 networking. Lots of code between POP and IMAP can be share this way.
108 The buffering is on the read only, the writes fall through. */ 108 The buffering is on the read only, the writes fall through. */
109 static int 109 int
110 _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread) 110 _stream_buffer_read (stream_t stream, void *buf, size_t count, size_t *pnread)
111 { 111 {
112 int status = 0; 112 int status = 0;
113 struct _bs *bs = (struct _bs *)stream; 113 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
114 114
115 /* Noop. */ 115 /* Noop. */
116 if (count == 0) 116 if (count == 0)
...@@ -131,7 +131,7 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread) ...@@ -131,7 +131,7 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread)
131 int r; 131 int r;
132 132
133 mu_refcount_lock (bs->refcount); 133 mu_refcount_lock (bs->refcount);
134 monitor_cleanup_push (_bs_cleanup, bs); 134 monitor_cleanup_push (_stream_buffer_cleanup, bs);
135 135
136 /* If the amount requested is bigger then the buffer cache size 136 /* If the amount requested is bigger then the buffer cache size
137 bypass it. Do no waste time and let it through. */ 137 bypass it. Do no waste time and let it through. */
...@@ -199,11 +199,11 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread) ...@@ -199,11 +199,11 @@ _bs_read (stream_t stream, void *buf, size_t count, size_t *pnread)
199 * Read at most n-1 characters. 199 * Read at most n-1 characters.
200 * Stop when a newline has been read, or the count runs out. 200 * Stop when a newline has been read, or the count runs out.
201 */ 201 */
202 static int 202 int
203 _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread) 203 _stream_buffer_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
204 { 204 {
205 int status = 0; 205 int status = 0;
206 struct _bs *bs = (struct _bs *)stream; 206 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
207 207
208 /* Noop. */ 208 /* Noop. */
209 if (count == 0) 209 if (count == 0)
...@@ -224,7 +224,7 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread) ...@@ -224,7 +224,7 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
224 size_t total = 0; 224 size_t total = 0;
225 225
226 mu_refcount_lock (bs->refcount); 226 mu_refcount_lock (bs->refcount);
227 monitor_cleanup_push (_bs_cleanup, bs); 227 monitor_cleanup_push (_stream_buffer_cleanup, bs);
228 228
229 count--; /* Leave space for the null. */ 229 count--; /* Leave space for the null. */
230 230
...@@ -277,91 +277,74 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread) ...@@ -277,91 +277,74 @@ _bs_readline (stream_t stream, char *buf, size_t count, size_t *pnread)
277 return status; 277 return status;
278 } 278 }
279 279
280 static int 280 int
281 _bs_write (stream_t stream, const void *buf, size_t count, size_t *pnwrite) 281 _stream_buffer_write (stream_t stream, const void *buf, size_t count,
282 size_t *pnwrite)
282 { 283 {
283 struct _bs *bs = (struct _bs *)stream; 284 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
284 int err = 0; 285 return stream_write (bs->stream, buf, count, pnwrite);
285 size_t nwriten = 0;
286 size_t total = 0;
287 int nleft = count;
288 const char *p = buf;
289
290 /* First try to send it all. */
291 while (nleft > 0)
292 {
293 err = stream_write (bs->stream, p, nleft, &nwriten);
294 if (err != 0 || nwriten == 0)
295 break;
296 nleft -= nwriten;
297 total += nwriten;
298 p += nwriten;
299 }
300 if (pnwrite)
301 *pnwrite = total;
302 return err;
303 } 286 }
304 287
305 static int 288 int
306 _bs_get_fd (stream_t stream, int *pfd) 289 _stream_buffer_get_fd (stream_t stream, int *pfd)
307 { 290 {
308 struct _bs *bs = (struct _bs *)stream; 291 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
309 return stream_get_fd (bs->stream, pfd); 292 return stream_get_fd (bs->stream, pfd);
310 } 293 }
311 294
312 static int 295 int
313 _bs_get_flags (stream_t stream, int *pfl) 296 _stream_buffer_get_flags (stream_t stream, int *pfl)
314 { 297 {
315 struct _bs *bs = (struct _bs *)stream; 298 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
316 return stream_get_flags (bs->stream, pfl); 299 return stream_get_flags (bs->stream, pfl);
317 } 300 }
318 301
319 static int 302 int
320 _bs_get_size (stream_t stream, off_t *psize) 303 _stream_buffer_get_size (stream_t stream, off_t *psize)
321 { 304 {
322 struct _bs *bs = (struct _bs *)stream; 305 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
323 return stream_get_size (bs->stream, psize); 306 return stream_get_size (bs->stream, psize);
324 } 307 }
325 308
326 static int 309 int
327 _bs_truncate (stream_t stream, off_t len) 310 _stream_buffer_truncate (stream_t stream, off_t len)
328 { 311 {
329 struct _bs *bs = (struct _bs *)stream; 312 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
330 return stream_truncate (bs->stream, len); 313 return stream_truncate (bs->stream, len);
331 } 314 }
332 315
333 static int 316 int
334 _bs_flush (stream_t stream) 317 _stream_buffer_flush (stream_t stream)
335 { 318 {
336 struct _bs *bs = (struct _bs *)stream; 319 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
337 return stream_flush (bs->stream); 320 return stream_flush (bs->stream);
338 } 321 }
339 322
340 static int 323 int
341 _bs_get_state (stream_t stream, enum stream_state *pstate) 324 _stream_buffer_get_state (stream_t stream, enum stream_state *pstate)
342 { 325 {
343 struct _bs *bs = (struct _bs *)stream; 326 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
344 return stream_get_state (bs->stream, pstate); 327 return stream_get_state (bs->stream, pstate);
345 } 328 }
346 329
347 static int 330 int
348 _bs_seek (stream_t stream, off_t off, enum stream_whence whence) 331 _stream_buffer_seek (stream_t stream, off_t off, enum stream_whence whence)
349 { 332 {
350 struct _bs *bs = (struct _bs *)stream; 333 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
351 return stream_seek (bs->stream, off, whence); 334 return stream_seek (bs->stream, off, whence);
352 } 335 }
353 336
354 static int 337 int
355 _bs_tell (stream_t stream, off_t *off) 338 _stream_buffer_tell (stream_t stream, off_t *off)
356 { 339 {
357 struct _bs *bs = (struct _bs *)stream; 340 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
358 return stream_tell (bs->stream, off); 341 return stream_tell (bs->stream, off);
359 } 342 }
360 343
361 static int 344 int
362 _bs_is_readready (stream_t stream, int timeout) 345 _stream_buffer_is_readready (stream_t stream, int timeout)
363 { 346 {
364 struct _bs *bs = (struct _bs *)stream; 347 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
365 /* Drain our buffer first. */ 348 /* Drain our buffer first. */
366 mu_refcount_lock (bs->refcount); 349 mu_refcount_lock (bs->refcount);
367 if (bs->rbuffer.count > 0) 350 if (bs->rbuffer.count > 0)
...@@ -373,62 +356,84 @@ _bs_is_readready (stream_t stream, int timeout) ...@@ -373,62 +356,84 @@ _bs_is_readready (stream_t stream, int timeout)
373 return stream_is_readready (bs->stream, timeout); 356 return stream_is_readready (bs->stream, timeout);
374 } 357 }
375 358
376 static int 359 int
377 _bs_is_writeready (stream_t stream, int timeout) 360 _stream_buffer_is_writeready (stream_t stream, int timeout)
378 { 361 {
379 struct _bs *bs = (struct _bs *)stream; 362 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
380 return stream_is_writeready (bs->stream, timeout); 363 return stream_is_writeready (bs->stream, timeout);
381 } 364 }
382 365
383 static int 366 int
384 _bs_is_exceptionpending (stream_t stream, int timeout) 367 _stream_buffer_is_exceptionpending (stream_t stream, int timeout)
385 { 368 {
386 struct _bs *bs = (struct _bs *)stream; 369 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
387 return stream_is_exceptionpending (bs->stream, timeout); 370 return stream_is_exceptionpending (bs->stream, timeout);
388 } 371 }
389 372
390 static int 373 int
391 _bs_is_open (stream_t stream) 374 _stream_buffer_is_open (stream_t stream)
392 { 375 {
393 struct _bs *bs = (struct _bs *)stream; 376 struct _stream_buffer *bs = (struct _stream_buffer *)stream;
394 return stream_is_open (bs->stream); 377 return stream_is_open (bs->stream);
395 } 378 }
396 379
397 380
398 static struct _stream_vtable _bs_vtable = 381 static struct _stream_vtable _stream_buffer_vtable =
399 { 382 {
400 _bs_ref, 383 _stream_buffer_ref,
401 _bs_destroy, 384 _stream_buffer_destroy,
402 385
403 _bs_open, 386 _stream_buffer_open,
404 _bs_close, 387 _stream_buffer_close,
405 388
406 _bs_read, 389 _stream_buffer_read,
407 _bs_readline, 390 _stream_buffer_readline,
408 _bs_write, 391 _stream_buffer_write,
409 392
410 _bs_seek, 393 _stream_buffer_seek,
411 _bs_tell, 394 _stream_buffer_tell,
412 395
413 _bs_get_size, 396 _stream_buffer_get_size,
414 _bs_truncate, 397 _stream_buffer_truncate,
415 _bs_flush, 398 _stream_buffer_flush,
416 399
417 _bs_get_fd, 400 _stream_buffer_get_fd,
418 _bs_get_flags, 401 _stream_buffer_get_flags,
419 _bs_get_state, 402 _stream_buffer_get_state,
420 403
421 _bs_is_readready, 404 _stream_buffer_is_readready,
422 _bs_is_writeready, 405 _stream_buffer_is_writeready,
423 _bs_is_exceptionpending, 406 _stream_buffer_is_exceptionpending,
424 407
425 _bs_is_open 408 _stream_buffer_is_open
426 }; 409 };
427 410
428 int 411 int
412 _stream_buffer_ctor (struct _stream_buffer *bs, stream_t stream,
413 size_t bufsize)
414 {
415 mu_refcount_create (&(bs->refcount));
416 if (bs->refcount == NULL)
417 return MU_ERROR_NO_MEMORY;
418
419 bs->stream = stream;
420 bs->rbuffer.bufsize = bufsize;
421 bs->base.vtable = &_stream_buffer_vtable;
422 return 0;
423 }
424
425 void
426 _stream_buffer_dtor (struct _stream_buffer *bs)
427 {
428 stream_destroy (&bs->stream);
429 mu_refcount_destroy (&bs->refcount);
430 }
431
432 int
429 stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize) 433 stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize)
430 { 434 {
431 struct _bs *bs; 435 struct _stream_buffer *bs;
436 int status;
432 437
433 if (pstream == NULL || stream == NULL || bufsize == 0) 438 if (pstream == NULL || stream == NULL || bufsize == 0)
434 return MU_ERROR_INVALID_PARAMETER; 439 return MU_ERROR_INVALID_PARAMETER;
...@@ -437,16 +442,9 @@ stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize) ...@@ -437,16 +442,9 @@ stream_buffer_create (stream_t *pstream, stream_t stream, size_t bufsize)
437 if (bs == NULL) 442 if (bs == NULL)
438 return MU_ERROR_NO_MEMORY; 443 return MU_ERROR_NO_MEMORY;
439 444
440 mu_refcount_create (&(bs->refcount)); 445 status = _stream_buffer_ctor (bs, stream, bufsize);
441 if (bs->refcount == NULL) 446 if (status != 0)
442 { 447 return status;
443 free (bs);
444 return MU_ERROR_NO_MEMORY;
445 }
446
447 bs->stream = stream;
448 bs->rbuffer.bufsize = bufsize;
449 bs->base.vtable = &_bs_vtable;
450 *pstream = &bs->base; 448 *pstream = &bs->base;
451 return 0; 449 return 0;
452 } 450 }
......
...@@ -28,19 +28,19 @@ ...@@ -28,19 +28,19 @@
28 #endif 28 #endif
29 29
30 #include <mailutils/error.h> 30 #include <mailutils/error.h>
31 #include <mailutils/sys/attribute.h> 31 #include <mailutils/sys/dattribute.h>
32 32
33 static int 33 int
34 _da_ref (attribute_t attribute) 34 _attribute_default_ref (attribute_t attribute)
35 { 35 {
36 struct _da *da = (struct _da *)attribute; 36 struct _attribute_default *da = (struct _attribute_default *)attribute;
37 return mu_refcount_inc (da->refcount); 37 return mu_refcount_inc (da->refcount);
38 } 38 }
39 39
40 static void 40 void
41 _da_destroy (attribute_t *pattribute) 41 _attribute_default_destroy (attribute_t *pattribute)
42 { 42 {
43 struct _da *da = (struct _da *)*pattribute; 43 struct _attribute_default *da = (struct _attribute_default *)*pattribute;
44 if (mu_refcount_dec (da->refcount) == 0) 44 if (mu_refcount_dec (da->refcount) == 0)
45 { 45 {
46 mu_refcount_destroy (&da->refcount); 46 mu_refcount_destroy (&da->refcount);
...@@ -48,10 +48,10 @@ _da_destroy (attribute_t *pattribute) ...@@ -48,10 +48,10 @@ _da_destroy (attribute_t *pattribute)
48 } 48 }
49 } 49 }
50 50
51 static int 51 int
52 _da_get_flags (attribute_t attribute, int *pflags) 52 _attribute_default_get_flags (attribute_t attribute, int *pflags)
53 { 53 {
54 struct _da *da = (struct _da *)attribute; 54 struct _attribute_default *da = (struct _attribute_default *)attribute;
55 mu_refcount_lock (da->refcount); 55 mu_refcount_lock (da->refcount);
56 if (pflags) 56 if (pflags)
57 *pflags = da->flags; 57 *pflags = da->flags;
...@@ -59,20 +59,20 @@ _da_get_flags (attribute_t attribute, int *pflags) ...@@ -59,20 +59,20 @@ _da_get_flags (attribute_t attribute, int *pflags)
59 return 0; 59 return 0;
60 } 60 }
61 61
62 static int 62 int
63 _da_set_flags (attribute_t attribute, int flags) 63 _attribute_default_set_flags (attribute_t attribute, int flags)
64 { 64 {
65 struct _da *da = (struct _da *)attribute; 65 struct _attribute_default *da = (struct _attribute_default *)attribute;
66 mu_refcount_lock (da->refcount); 66 mu_refcount_lock (da->refcount);
67 da->flags |= (flags | MU_ATTRIBUTE_MODIFIED); 67 da->flags |= (flags | MU_ATTRIBUTE_MODIFIED);
68 mu_refcount_unlock (da->refcount); 68 mu_refcount_unlock (da->refcount);
69 return 0; 69 return 0;
70 } 70 }
71 71
72 static int 72 int
73 _da_unset_flags (attribute_t attribute, int flags) 73 _attribute_default_unset_flags (attribute_t attribute, int flags)
74 { 74 {
75 struct _da *da = (struct _da *)attribute; 75 struct _attribute_default *da = (struct _attribute_default *)attribute;
76 mu_refcount_lock (da->refcount); 76 mu_refcount_lock (da->refcount);
77 da->flags &= ~flags; 77 da->flags &= ~flags;
78 /* If Modified was being unset do not reset it. */ 78 /* If Modified was being unset do not reset it. */
...@@ -82,45 +82,115 @@ _da_unset_flags (attribute_t attribute, int flags) ...@@ -82,45 +82,115 @@ _da_unset_flags (attribute_t attribute, int flags)
82 return 0; 82 return 0;
83 } 83 }
84 84
85 static int 85 int
86 _da_clear_flags (attribute_t attribute) 86 _attribute_default_clear_flags (attribute_t attribute)
87 { 87 {
88 struct _da *da = (struct _da *)attribute; 88 struct _attribute_default *da = (struct _attribute_default *)attribute;
89 mu_refcount_lock (da->refcount); 89 mu_refcount_lock (da->refcount);
90 da->flags = 0; 90 da->flags = 0;
91 mu_refcount_unlock (da->refcount); 91 mu_refcount_unlock (da->refcount);
92 return 0; 92 return 0;
93 } 93 }
94 94
95 static struct _attribute_vtable _da_vtable = 95 static struct _attribute_vtable _attribute_default_vtable =
96 { 96 {
97 _da_ref, 97 _attribute_default_ref,
98 _da_destroy, 98 _attribute_default_destroy,
99 99
100 _da_get_flags, 100 _attribute_default_get_flags,
101 _da_set_flags, 101 _attribute_default_set_flags,
102 _da_unset_flags, 102 _attribute_default_unset_flags,
103 _da_clear_flags 103 _attribute_default_clear_flags
104 }; 104 };
105 105
106 int 106 int
107 attribute_create (attribute_t *pattribute) 107 _attribute_default_ctor (struct _attribute_default *da)
108 {
109 mu_refcount_create (&(da->refcount));
110 if (da->refcount == NULL)
111 return MU_ERROR_NO_MEMORY;
112 da->flags = 0;
113 da->base.vtable = &_attribute_default_vtable;
114 return 0;
115 }
116
117 void
118 _attribute_default_dtor (struct _attribute_default *da)
119 {
120 mu_refcount_destroy (&da->refcount);
121 }
122
123 int
124 attribute_default_create (attribute_t *pattribute)
108 { 125 {
109 struct _da *da; 126 struct _attribute_default *da;
127 int status;
128
110 if (pattribute == NULL) 129 if (pattribute == NULL)
111 return MU_ERROR_INVALID_PARAMETER; 130 return MU_ERROR_INVALID_PARAMETER;
131
112 da = calloc (1, sizeof *da); 132 da = calloc (1, sizeof *da);
113 if (da == NULL) 133 if (da == NULL)
114 return MU_ERROR_NO_MEMORY; 134 return MU_ERROR_NO_MEMORY;
115 135
116 mu_refcount_create (&(da->refcount)); 136 status = _attribute_default_ctor (da);
117 if (da->refcount == NULL) 137 if (status != 0)
118 { 138 {
119 free (da); 139 free (da);
140 return status;
141 }
142 *pattribute = &da->base;
143 return 0;
144 }
145
146 int
147 attribute_status_create (attribute_t *pattribute, const char *field)
148 {
149 struct _attribute_default *da;
150 int status;
151 char *colon;
152
153 if (pattribute == NULL || field == NULL)
154 return MU_ERROR_INVALID_PARAMETER;
155
156 da = calloc (1, sizeof *da);
157 if (da == NULL)
120 return MU_ERROR_NO_MEMORY; 158 return MU_ERROR_NO_MEMORY;
159
160 status = _attribute_default_ctor (da);
161 if (status != 0)
162 {
163 free (da);
164 return status;
121 } 165 }
122 da->flags = 0;
123 da->base.vtable = &_da_vtable;
124 *pattribute = &da->base; 166 *pattribute = &da->base;
167
168 colon = strchr (field, ':');
169 if (colon)
170 field = ++colon;
171
172 for (; *field; field++)
173 {
174 switch (*field)
175 {
176 case 'r':
177 case 'R':
178 attribute_set_read (*pattribute);
179 break;
180 case 'O':
181 case 'o':
182 attribute_set_seen (*pattribute);
183 break;
184 case 'a':
185 case 'A':
186 attribute_set_answered (*pattribute);
187 break;
188 case 'd':
189 case 'D':
190 attribute_set_deleted (*pattribute);
191 break;
192 }
193 }
194 attribute_unset_modified (*pattribute);
125 return 0; 195 return 0;
126 } 196 }
......
...@@ -33,53 +33,35 @@ ...@@ -33,53 +33,35 @@
33 #include <signal.h> 33 #include <signal.h>
34 34
35 #include <mailutils/error.h> 35 #include <mailutils/error.h>
36 #include <mailutils/sys/locker.h> 36 #include <mailutils/sys/dotlock.h>
37 #include <mailutils/refcount.h> 37 #include <mailutils/refcount.h>
38 38
39 /* locking flags */
40 #define MU_DOTLOCK_PID 1
41 #define MU_DOTLOCK_FCNTL 2
42 #define MU_DOTLOCK_TIME 4
43
44 #define MU_DOTLOCK_EXPIRE_TIME (5 * 60)
45
46 #define LOCKFILE_ATTR 0444
47
48 /* First draft by Brian Edmond. */ 39 /* First draft by Brian Edmond. */
49 40
50 struct _dotlock 41 int
51 { 42 _lockfile_dotlock_ref (lockfile_t lockfile)
52 struct _locker base;
53 mu_refcount_t refcount;
54 int fd;
55 int refcnt;
56 char *fname;
57 int flags;
58 };
59
60 static int
61 _dotlock_ref (locker_t locker)
62 { 43 {
63 struct _dotlock *dotlock = (struct _dotlock *)locker; 44 struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
64 return mu_refcount_inc (dotlock->refcount); 45 return mu_refcount_inc (dotlock->refcount);
65 } 46 }
66 47
67 static void 48 void
68 _dotlock_destroy (locker_t *plocker) 49 _lockfile_dotlock_destroy (lockfile_t *plockfile)
69 { 50 {
70 struct _dotlock *dotlock = (struct _dotlock *)*plocker; 51 struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)*plockfile;
71 if (mu_refcount_dec (dotlock->refcount) == 0) 52 if (mu_refcount_dec (dotlock->refcount) == 0)
72 { 53 {
73 mu_refcount_destroy (&dotlock->refcount); 54 mu_refcount_destroy (&dotlock->refcount);
55 if (dotlock->fname)
74 free (dotlock->fname); 56 free (dotlock->fname);
75 free (dotlock); 57 free (dotlock);
76 } 58 }
77 } 59 }
78 60
79 static int 61 int
80 _dotlock_lock (locker_t locker) 62 _lockfile_dotlock_lock (lockfile_t lockfile)
81 { 63 {
82 struct _dotlock *dotlock = (struct _dotlock *)locker; 64 struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
83 int fd = -1; 65 int fd = -1;
84 char buf[16]; 66 char buf[16];
85 pid_t pid; 67 pid_t pid;
...@@ -104,31 +86,34 @@ _dotlock_lock (locker_t locker) ...@@ -104,31 +86,34 @@ _dotlock_lock (locker_t locker)
104 if (fd != -1) 86 if (fd != -1)
105 { 87 {
106 /* Check to see if this process is still running. */ 88 /* Check to see if this process is still running. */
107 if (dotlock->flags & MU_DOTLOCK_PID) 89 if (dotlock->flags & MU_LOCKFILE_DOTLOCK_PID)
108 { 90 {
109 int nread = read (fd, buf, sizeof (buf) - 1); 91 int nread = read (fd, buf, sizeof (buf) - 1);
110 if (nread > 0) 92 if (nread > 0)
111 { 93 {
112 buf[nread] = '\0'; 94 buf[nread] = '\0';
113 pid = strtol (buf, NULL, 10); 95 switch (pid = strtol (buf, NULL, 10))
114 if (pid > 0)
115 { 96 {
97 case LONG_MIN:
98 case LONG_MAX:
99 if (errno == ERANGE)
100 removed = 1;
101 break;
102 default:
116 /* Process is gone so we try to remove the lock. */ 103 /* Process is gone so we try to remove the lock. */
117 if (kill (pid, 0) == -1) 104 if (kill (pid, 0) == -1)
118 removed = 1; 105 removed = 1;
119 } 106 }
120 else
121 removed = 1; /* Corrupted file, remove the lock. */
122 } 107 }
123 } 108 }
124 /* Check to see if the lock expired. */ 109 /* Check to see if the lock expired. */
125 if (dotlock->flags & MU_DOTLOCK_TIME) 110 if (dotlock->flags & MU_LOCKFILE_DOTLOCK_TIME)
126 { 111 {
127 struct stat stbuf; 112 struct stat stbuf;
128 113
129 fstat (fd, &stbuf); 114 fstat (fd, &stbuf);
130 /* The lock has expired. */ 115 /* The lock has expired. */
131 if ((time (NULL) - stbuf.st_mtime) > MU_DOTLOCK_EXPIRE_TIME) 116 if ((time (NULL) - stbuf.st_mtime) > MU_LOCKFILE_DOTLOCK_EXPIRE_TIME)
132 removed = 1; 117 removed = 1;
133 } 118 }
134 119
...@@ -138,7 +123,7 @@ _dotlock_lock (locker_t locker) ...@@ -138,7 +123,7 @@ _dotlock_lock (locker_t locker)
138 } 123 }
139 124
140 /* Try to create the lockfile. */ 125 /* Try to create the lockfile. */
141 fd = open (dotlock->fname, O_WRONLY | O_CREAT | O_EXCL, LOCKFILE_ATTR); 126 fd = open (dotlock->fname, O_WRONLY | O_CREAT | O_EXCL, MU_LOCKFILE_DOTLOCK_ATTR);
142 if (fd == -1) 127 if (fd == -1)
143 return errno; 128 return errno;
144 else 129 else
...@@ -165,7 +150,7 @@ _dotlock_lock (locker_t locker) ...@@ -165,7 +150,7 @@ _dotlock_lock (locker_t locker)
165 write (fd, buf, strlen (buf)); 150 write (fd, buf, strlen (buf));
166 151
167 /* Try to get a file lock. */ 152 /* Try to get a file lock. */
168 if (dotlock->flags & MU_DOTLOCK_FCNTL) 153 if (dotlock->flags & MU_LOCKFILE_DOTLOCK_FCNTL)
169 { 154 {
170 struct flock fl; 155 struct flock fl;
171 156
...@@ -186,26 +171,26 @@ _dotlock_lock (locker_t locker) ...@@ -186,26 +171,26 @@ _dotlock_lock (locker_t locker)
186 return 0; 171 return 0;
187 } 172 }
188 173
189 static int 174 int
190 _dotlock_touchlock (locker_t locker) 175 _lockfile_dotlock_touchlock (lockfile_t lockfile)
191 { 176 {
192 struct _dotlock *dotlock = (struct _dotlock *)locker; 177 struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
193 if (!dotlock || !dotlock->fname || dotlock->fd == -1) 178 if (!dotlock || !dotlock->fname || dotlock->fd == -1)
194 return MU_ERROR_INVALID_PARAMETER; 179 return MU_ERROR_INVALID_PARAMETER;
195 return utime (dotlock->fname, NULL); 180 return utime (dotlock->fname, NULL);
196 } 181 }
197 182
198 static int 183 int
199 _dotlock_unlock (locker_t locker) 184 _lockfile_dotlock_unlock (lockfile_t lockfile)
200 { 185 {
201 struct _dotlock *dotlock = (struct _dotlock *)locker; 186 struct _lockfile_dotlock *dotlock = (struct _lockfile_dotlock *)lockfile;
202 if (!dotlock || !dotlock->fname || dotlock->fd == -1 || dotlock->refcnt <= 0) 187 if (!dotlock || !dotlock->fname || dotlock->fd == -1 || dotlock->refcnt <= 0)
203 return EINVAL; 188 return EINVAL;
204 189
205 if (--dotlock->refcnt > 0) 190 if (--dotlock->refcnt > 0)
206 return 0; 191 return 0;
207 192
208 if (dotlock->flags & MU_DOTLOCK_FCNTL) 193 if (dotlock->flags & MU_LOCKFILE_DOTLOCK_FCNTL)
209 { 194 {
210 struct flock fl; 195 struct flock fl;
211 196
...@@ -221,48 +206,68 @@ _dotlock_unlock (locker_t locker) ...@@ -221,48 +206,68 @@ _dotlock_unlock (locker_t locker)
221 return 0; 206 return 0;
222 } 207 }
223 208
224 static struct _locker_vtable _dotlock_vtable = 209 static struct _lockfile_vtable _lockfile_dotlock_vtable =
225 { 210 {
226 _dotlock_ref, 211 _lockfile_dotlock_ref,
227 _dotlock_destroy, 212 _lockfile_dotlock_destroy,
228 213
229 _dotlock_lock, 214 _lockfile_dotlock_lock,
230 _dotlock_touchlock, 215 _lockfile_dotlock_touchlock,
231 _dotlock_unlock, 216 _lockfile_dotlock_unlock,
232 }; 217 };
233 218
234 int 219 int
235 locker_dotlock_create (locker_t *plocker, const char *filename) 220 _lockfile_dotlock_ctor (struct _lockfile_dotlock *dotlock,
221 const char *filename)
236 { 222 {
237 struct _dotlock *dotlock;
238
239 if (plocker == NULL || filename == NULL)
240 return MU_ERROR_INVALID_PARAMETER;
241
242 dotlock = calloc (1, sizeof *dotlock);
243 if (dotlock == NULL)
244 return MU_ERROR_NO_MEMORY;
245
246 mu_refcount_create (&dotlock->refcount); 223 mu_refcount_create (&dotlock->refcount);
247 if (dotlock->refcount) 224 if (dotlock->refcount)
248 {
249 free (dotlock);
250 return MU_ERROR_NO_MEMORY; 225 return MU_ERROR_NO_MEMORY;
251 }
252 226
253 dotlock->fname = calloc (strlen (filename) + 5 /*strlen(".lock")*/ + 1, 1); 227 dotlock->fname = calloc (strlen (filename) + 5 /*strlen(".lock")*/ + 1, 1);
254 if (dotlock->fname == NULL) 228 if (dotlock->fname == NULL)
255 { 229 {
256 free (dotlock); 230 mu_refcount_destroy (&dotlock->refcount);
257 return MU_ERROR_NO_MEMORY; 231 return MU_ERROR_NO_MEMORY;
258 } 232 }
259 strcpy (dotlock->fname, filename); 233 strcpy (dotlock->fname, filename);
260 strcat (dotlock->fname, ".lock"); 234 strcat (dotlock->fname, ".lock");
261 235
262 dotlock->flags = MU_DOTLOCK_PID | MU_DOTLOCK_TIME | MU_DOTLOCK_FCNTL; 236 dotlock->flags = MU_LOCKFILE_DOTLOCK_PID | MU_LOCKFILE_DOTLOCK_TIME
237 | MU_LOCKFILE_DOTLOCK_FCNTL;
263 dotlock->fd = -1; 238 dotlock->fd = -1;
264 dotlock->refcnt = 0; 239 dotlock->refcnt = 0;
265 dotlock->base.vtable = &_dotlock_vtable; 240 dotlock->base.vtable = &_lockfile_dotlock_vtable;
266 *plocker = &dotlock->base; 241 return 0;
242 }
243
244 void
245 _lockfile_dotlock_dtor (struct _lockfile_dotlock *dotlock)
246 {
247 mu_refcount_destroy (&dotlock->refcount);
248 if (dotlock->fname)
249 free (dotlock->fname);
250 }
251
252 int
253 lockfile_dotlock_create (lockfile_t *plockfile, const char *filename)
254 {
255 struct _lockfile_dotlock *dotlock;
256 int status;
257
258 if (plockfile == NULL || filename == NULL)
259 return MU_ERROR_INVALID_PARAMETER;
260
261 dotlock = calloc (1, sizeof *dotlock);
262 if (dotlock == NULL)
263 return MU_ERROR_NO_MEMORY;
264
265 status = _lockfile_dotlock_ctor (dotlock, filename);
266 if (status != 0)
267 {
268 free (dotlock);
269 return status;
270 }
271 *plockfile = &dotlock->base;
267 return 0; 272 return 0;
268 } 273 }
......
...@@ -33,23 +33,23 @@ ...@@ -33,23 +33,23 @@
33 #include <mailutils/error.h> 33 #include <mailutils/error.h>
34 34
35 static void 35 static void
36 _fds_cleanup (void *arg) 36 _stream_fd_cleanup (void *arg)
37 { 37 {
38 struct _fds *fds = arg; 38 struct _stream_fd *fds = arg;
39 mu_refcount_unlock (fds->refcount); 39 mu_refcount_unlock (fds->refcount);
40 } 40 }
41 41
42 static int 42 int
43 _fds_ref (stream_t stream) 43 _stream_fd_ref (stream_t stream)
44 { 44 {
45 struct _fds *fds = (struct _fds *)stream; 45 struct _stream_fd *fds = (struct _stream_fd *)stream;
46 return mu_refcount_inc (fds->refcount); 46 return mu_refcount_inc (fds->refcount);
47 } 47 }
48 48
49 static void 49 void
50 _fds_destroy (stream_t *pstream) 50 _stream_fd_destroy (stream_t *pstream)
51 { 51 {
52 struct _fds *fds = (struct _fds *)*pstream; 52 struct _stream_fd *fds = (struct _stream_fd *)*pstream;
53 if (mu_refcount_dec (fds->refcount) == 0) 53 if (mu_refcount_dec (fds->refcount) == 0)
54 { 54 {
55 mu_refcount_destroy (&fds->refcount); 55 mu_refcount_destroy (&fds->refcount);
...@@ -58,39 +58,40 @@ _fds_destroy (stream_t *pstream) ...@@ -58,39 +58,40 @@ _fds_destroy (stream_t *pstream)
58 } 58 }
59 59
60 static int 60 static int
61 _fds_close0 (stream_t stream) 61 _stream_fd_close0 (stream_t stream)
62 { 62 {
63 struct _fds *fds = (struct _fds *)stream; 63 struct _stream_fd *fds = (struct _stream_fd *)stream;
64 fds->state = MU_STREAM_STATE_CLOSE;
64 if (fds->fd != -1) 65 if (fds->fd != -1)
65 close (fds->fd); 66 close (fds->fd);
66 fds->fd = -1; 67 fds->fd = -1;
67 return 0; 68 return 0;
68 } 69 }
69 70
70 static int 71 int
71 _fds_close (stream_t stream) 72 _stream_fd_close (stream_t stream)
72 { 73 {
73 struct _fds *fds = (struct _fds *)stream; 74 struct _stream_fd *fds = (struct _stream_fd *)stream;
74 75
75 mu_refcount_lock (fds->refcount); 76 mu_refcount_lock (fds->refcount);
76 monitor_cleanup_push (_fds_cleanup, fds); 77 monitor_cleanup_push (_stream_fd_cleanup, fds);
77 _fds_close0 (stream); 78 _stream_fd_close0 (stream);
78 mu_refcount_unlock (fds->refcount); 79 mu_refcount_unlock (fds->refcount);
79 monitor_cleanup_pop (0); 80 monitor_cleanup_pop (0);
80 return 0; 81 return 0;
81 } 82 }
82 83
83 static int 84 int
84 _fds_open (stream_t stream, const char *name, int port, int flags) 85 _stream_fd_open (stream_t stream, const char *name, int port, int flags)
85 { 86 {
86 (void)stream; (void)name; (void)port; (void)flags; 87 (void)stream; (void)name; (void)port; (void)flags;
87 return MU_ERROR_NOT_SUPPORTED; 88 return MU_ERROR_NOT_SUPPORTED;
88 } 89 }
89 90
90 static int 91 int
91 _fds_get_fd (stream_t stream, int *fd) 92 _stream_fd_get_fd (stream_t stream, int *fd)
92 { 93 {
93 struct _fds *fds = (struct _fds *)stream; 94 struct _stream_fd *fds = (struct _stream_fd *)stream;
94 95
95 if (fd == NULL || fds->fd == -1) 96 if (fd == NULL || fds->fd == -1)
96 return MU_ERROR_INVALID_PARAMETER; 97 return MU_ERROR_INVALID_PARAMETER;
...@@ -99,13 +100,14 @@ _fds_get_fd (stream_t stream, int *fd) ...@@ -99,13 +100,14 @@ _fds_get_fd (stream_t stream, int *fd)
99 return 0; 100 return 0;
100 } 101 }
101 102
102 static int 103 int
103 _fds_read (stream_t stream, void *buf, size_t buf_size, size_t *br) 104 _stream_fd_read (stream_t stream, void *buf, size_t buf_size, size_t *br)
104 { 105 {
105 struct _fds *fds = (struct _fds *)stream; 106 struct _stream_fd *fds = (struct _stream_fd *)stream;
106 int bytes = 0; 107 int bytes = 0;
107 int status = 0; 108 int status = 0;
108 109
110 fds->state = MU_STREAM_STATE_READ;
109 bytes = read (fds->fd, buf, buf_size); 111 bytes = read (fds->fd, buf, buf_size);
110 if (bytes == -1) 112 if (bytes == -1)
111 { 113 {
...@@ -117,15 +119,16 @@ _fds_read (stream_t stream, void *buf, size_t buf_size, size_t *br) ...@@ -117,15 +119,16 @@ _fds_read (stream_t stream, void *buf, size_t buf_size, size_t *br)
117 return status; 119 return status;
118 } 120 }
119 121
120 static int 122 int
121 _fds_readline (stream_t stream, char *buf, size_t buf_size, size_t *br) 123 _stream_fd_readline (stream_t stream, char *buf, size_t buf_size, size_t *br)
122 { 124 {
123 struct _fds *fds = (struct _fds *)stream; 125 struct _stream_fd *fds = (struct _stream_fd *)stream;
124 int status = 0; 126 int status = 0;
125 size_t n; 127 size_t n;
126 int nr = 0; 128 int nr = 0;
127 char c; 129 char c;
128 130
131 fds->state = MU_STREAM_STATE_READ;
129 /* Grossly inefficient hopefully they override this */ 132 /* Grossly inefficient hopefully they override this */
130 for (n = 1; n < buf_size; n++) 133 for (n = 1; n < buf_size; n++)
131 { 134 {
...@@ -154,13 +157,14 @@ _fds_readline (stream_t stream, char *buf, size_t buf_size, size_t *br) ...@@ -154,13 +157,14 @@ _fds_readline (stream_t stream, char *buf, size_t buf_size, size_t *br)
154 return status; 157 return status;
155 } 158 }
156 159
157 static int 160 int
158 _fds_write (stream_t stream, const void *buf, size_t buf_size, size_t *bw) 161 _stream_fd_write (stream_t stream, const void *buf, size_t buf_size, size_t *bw)
159 { 162 {
160 struct _fds *fds = (struct _fds *)stream; 163 struct _stream_fd *fds = (struct _stream_fd *)stream;
161 int bytes = 0; 164 int bytes = 0;
162 int status = 0; 165 int status = 0;
163 166
167 fds->state = MU_STREAM_STATE_WRITE;
164 bytes = write (fds->fd, buf, buf_size); 168 bytes = write (fds->fd, buf, buf_size);
165 if (bytes == -1) 169 if (bytes == -1)
166 { 170 {
...@@ -172,10 +176,10 @@ _fds_write (stream_t stream, const void *buf, size_t buf_size, size_t *bw) ...@@ -172,10 +176,10 @@ _fds_write (stream_t stream, const void *buf, size_t buf_size, size_t *bw)
172 return status; 176 return status;
173 } 177 }
174 178
175 static int 179 int
176 _fds_seek (stream_t stream, off_t off, enum stream_whence whence) 180 _stream_fd_seek (stream_t stream, off_t off, enum stream_whence whence)
177 { 181 {
178 struct _fds *fds = (struct _fds *)stream; 182 struct _stream_fd *fds = (struct _stream_fd *)stream;
179 int err = 0; 183 int err = 0;
180 if (fds->fd) 184 if (fds->fd)
181 { 185 {
...@@ -193,10 +197,10 @@ _fds_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -193,10 +197,10 @@ _fds_seek (stream_t stream, off_t off, enum stream_whence whence)
193 return err; 197 return err;
194 } 198 }
195 199
196 static int 200 int
197 _fds_tell (stream_t stream, off_t *off) 201 _stream_fd_tell (stream_t stream, off_t *off)
198 { 202 {
199 struct _fds *fds = (struct _fds *)stream; 203 struct _stream_fd *fds = (struct _stream_fd *)stream;
200 int err = 0; 204 int err = 0;
201 if (off) 205 if (off)
202 { 206 {
...@@ -207,13 +211,13 @@ _fds_tell (stream_t stream, off_t *off) ...@@ -207,13 +211,13 @@ _fds_tell (stream_t stream, off_t *off)
207 *off = 0; 211 *off = 0;
208 } 212 }
209 } 213 }
210 return err;; 214 return err;
211 } 215 }
212 216
213 static int 217 int
214 _fds_get_size (stream_t stream, off_t *psize) 218 _stream_fd_get_size (stream_t stream, off_t *psize)
215 { 219 {
216 struct _fds *fds = (struct _fds *)stream; 220 struct _stream_fd *fds = (struct _stream_fd *)stream;
217 struct stat stbuf; 221 struct stat stbuf;
218 int err = 0; 222 int err = 0;
219 stbuf.st_size = 0; 223 stbuf.st_size = 0;
...@@ -224,47 +228,47 @@ _fds_get_size (stream_t stream, off_t *psize) ...@@ -224,47 +228,47 @@ _fds_get_size (stream_t stream, off_t *psize)
224 return err; 228 return err;
225 } 229 }
226 230
227 static int 231 int
228 _fds_truncate (stream_t stream, off_t len) 232 _stream_fd_truncate (stream_t stream, off_t len)
229 { 233 {
230 struct _fds *fds = (struct _fds *)stream; 234 struct _stream_fd *fds = (struct _stream_fd *)stream;
231 int err = 0; 235 int err = 0;
232 if (ftruncate (fds->fd, len) == -1) 236 if (ftruncate (fds->fd, len) == -1)
233 err = errno ; 237 err = errno;
234 return err; 238 return err;
235 } 239 }
236 240
237 static int 241 int
238 _fds_flush (stream_t stream) 242 _stream_fd_flush (stream_t stream)
239 { 243 {
240 (void)stream; 244 (void)stream;
241 return 0; 245 return 0;
242 } 246 }
243 247
244 static int 248 int
245 _fds_get_flags (stream_t stream, int *flags) 249 _stream_fd_get_flags (stream_t stream, int *flags)
246 { 250 {
247 struct _fds *fds = (struct _fds *)stream; 251 struct _stream_fd *fds = (struct _stream_fd *)stream;
248 if (flags == NULL) 252 if (flags == NULL)
249 return MU_ERROR_INVALID_PARAMETER; 253 return MU_ERROR_INVALID_PARAMETER;
250 *flags = fds->flags; 254 *flags = fds->flags;
251 return 0; 255 return 0;
252 } 256 }
253 257
254 static int 258 int
255 _fds_get_state (stream_t stream, enum stream_state *state) 259 _stream_fd_get_state (stream_t stream, enum stream_state *state)
256 { 260 {
257 (void)stream; 261 struct _stream_fd *fds = (struct _stream_fd *)stream;
258 if (state == NULL) 262 if (state == NULL)
259 return MU_ERROR_INVALID_PARAMETER; 263 return MU_ERROR_INVALID_PARAMETER;
260 *state = MU_STREAM_NO_STATE; 264 *state = fds->state;
261 return 0; 265 return 0;
262 } 266 }
263 267
264 static int 268 int
265 _fds_is_readready (stream_t stream, int timeout) 269 _stream_fd_is_readready (stream_t stream, int timeout)
266 { 270 {
267 struct _fds *fds = (struct _fds *)stream; 271 struct _stream_fd *fds = (struct _stream_fd *)stream;
268 int ready = 0; 272 int ready = 0;
269 273
270 if (fds->fd >= 0) 274 if (fds->fd >= 0)
...@@ -285,13 +289,13 @@ _fds_is_readready (stream_t stream, int timeout) ...@@ -285,13 +289,13 @@ _fds_is_readready (stream_t stream, int timeout)
285 return ready; 289 return ready;
286 } 290 }
287 291
288 static int 292 int
289 _fds_is_writeready (stream_t stream, int timeout) 293 _stream_fd_is_writeready (stream_t stream, int timeout)
290 { 294 {
291 struct _fds *fds = (struct _fds *)stream; 295 struct _stream_fd *fds = (struct _stream_fd *)stream;
292 int ready = 0; 296 int ready = 0;
293 297
294 if (fds->fd) 298 if (fds->fd >= 0)
295 { 299 {
296 struct timeval tv; 300 struct timeval tv;
297 fd_set fset; 301 fd_set fset;
...@@ -309,13 +313,13 @@ _fds_is_writeready (stream_t stream, int timeout) ...@@ -309,13 +313,13 @@ _fds_is_writeready (stream_t stream, int timeout)
309 return ready; 313 return ready;
310 } 314 }
311 315
312 static int 316 int
313 _fds_is_exceptionpending (stream_t stream, int timeout) 317 _stream_fd_is_exceptionpending (stream_t stream, int timeout)
314 { 318 {
315 struct _fds *fds = (struct _fds *)stream; 319 struct _stream_fd *fds = (struct _stream_fd *)stream;
316 int ready = 0; 320 int ready = 0;
317 321
318 if (fds->fd) 322 if (fds->fd >= 0)
319 { 323 {
320 struct timeval tv; 324 struct timeval tv;
321 fd_set fset; 325 fd_set fset;
...@@ -333,47 +337,66 @@ _fds_is_exceptionpending (stream_t stream, int timeout) ...@@ -333,47 +337,66 @@ _fds_is_exceptionpending (stream_t stream, int timeout)
333 return 0; 337 return 0;
334 } 338 }
335 339
336 static int 340 int
337 _fds_is_open (stream_t stream) 341 _stream_fd_is_open (stream_t stream)
338 { 342 {
339 struct _fds *fds = (struct _fds *)stream; 343 struct _stream_fd *fds = (struct _stream_fd *)stream;
340 return fds->fd >= 0; 344 return fds->fd >= 0;
341 } 345 }
342 346
343 static struct _stream_vtable _fds_vtable = 347 static struct _stream_vtable _stream_fd_vtable =
344 { 348 {
345 _fds_ref, 349 _stream_fd_ref,
346 _fds_destroy, 350 _stream_fd_destroy,
347 351
348 _fds_open, 352 _stream_fd_open,
349 _fds_close, 353 _stream_fd_close,
350 354
351 _fds_read, 355 _stream_fd_read,
352 _fds_readline, 356 _stream_fd_readline,
353 _fds_write, 357 _stream_fd_write,
354 358
355 _fds_seek, 359 _stream_fd_seek,
356 _fds_tell, 360 _stream_fd_tell,
357 361
358 _fds_get_size, 362 _stream_fd_get_size,
359 _fds_truncate, 363 _stream_fd_truncate,
360 _fds_flush, 364 _stream_fd_flush,
361 365
362 _fds_get_fd, 366 _stream_fd_get_fd,
363 _fds_get_flags, 367 _stream_fd_get_flags,
364 _fds_get_state, 368 _stream_fd_get_state,
365 369
366 _fds_is_readready, 370 _stream_fd_is_readready,
367 _fds_is_writeready, 371 _stream_fd_is_writeready,
368 _fds_is_exceptionpending, 372 _stream_fd_is_exceptionpending,
369 373
370 _fds_is_open 374 _stream_fd_is_open
371 }; 375 };
372 376
377
378 int
379 _stream_fd_ctor (struct _stream_fd *fds, int fd)
380 {
381 mu_refcount_create (&fds->refcount);
382 if (fds->refcount == NULL)
383 return MU_ERROR_NO_MEMORY;
384 fds->fd = fd;
385 fds->base.vtable = &_stream_fd_vtable;
386 return 0;
387 }
388
389 void
390 _stream_fd_dtor (struct _stream_fd *fds)
391 {
392 mu_refcount_destroy (&fds->refcount);
393 }
394
373 int 395 int
374 stream_fd_create (stream_t *pstream, int fd) 396 stream_fd_create (stream_t *pstream, int fd)
375 { 397 {
376 struct _fds *fds; 398 struct _stream_fd *fds;
399 int status;
377 400
378 if (pstream == NULL || fd < 0) 401 if (pstream == NULL || fd < 0)
379 return MU_ERROR_INVALID_PARAMETER; 402 return MU_ERROR_INVALID_PARAMETER;
...@@ -382,14 +405,12 @@ stream_fd_create (stream_t *pstream, int fd) ...@@ -382,14 +405,12 @@ stream_fd_create (stream_t *pstream, int fd)
382 if (fds == NULL) 405 if (fds == NULL)
383 return MU_ERROR_NO_MEMORY; 406 return MU_ERROR_NO_MEMORY;
384 407
385 mu_refcount_create (&fds->refcount); 408 status = _stream_fd_ctor (fds, fd);
386 if (fds->refcount == NULL) 409 if (status != 0)
387 { 410 {
388 free (fds); 411 free (fds);
389 return MU_ERROR_NO_MEMORY; 412 return status;
390 } 413 }
391 fds->fd = fd;
392 fds->base.vtable = &_fds_vtable;
393 *pstream = &fds->base; 414 *pstream = &fds->base;
394 return 0; 415 return 0;
395 } 416 }
......
...@@ -34,17 +34,17 @@ ...@@ -34,17 +34,17 @@
34 #include <mailutils/error.h> 34 #include <mailutils/error.h>
35 35
36 36
37 static int 37 int
38 _fs_ref (stream_t stream) 38 _stream_stdio_ref (stream_t stream)
39 { 39 {
40 struct _fs *fs = (struct _fs *)stream; 40 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
41 return mu_refcount_inc (fs->refcount); 41 return mu_refcount_inc (fs->refcount);
42 } 42 }
43 43
44 static void 44 void
45 _fs_destroy (stream_t *pstream) 45 _stream_stdio_destroy (stream_t *pstream)
46 { 46 {
47 struct _fs *fs = (struct _fs *)*pstream; 47 struct _stream_stdio *fs = (struct _stream_stdio *)*pstream;
48 if (mu_refcount_dec (fs->refcount) == 0) 48 if (mu_refcount_dec (fs->refcount) == 0)
49 { 49 {
50 mu_refcount_destroy (&fs->refcount); 50 mu_refcount_destroy (&fs->refcount);
...@@ -52,10 +52,10 @@ _fs_destroy (stream_t *pstream) ...@@ -52,10 +52,10 @@ _fs_destroy (stream_t *pstream)
52 } 52 }
53 } 53 }
54 54
55 static int 55 int
56 _fs_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) 56 _stream_stdio_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
57 { 57 {
58 struct _fs *fs = (struct _fs *)stream; 58 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
59 size_t n = 0; 59 size_t n = 0;
60 int err = 0; 60 int err = 0;
61 61
...@@ -74,10 +74,10 @@ _fs_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) ...@@ -74,10 +74,10 @@ _fs_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
74 return err; 74 return err;
75 } 75 }
76 76
77 static int 77 int
78 _fs_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) 78 _stream_stdio_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
79 { 79 {
80 struct _fs *fs = (struct _fs *)stream; 80 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
81 size_t n = 0; 81 size_t n = 0;
82 int err = 0; 82 int err = 0;
83 83
...@@ -99,10 +99,10 @@ _fs_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) ...@@ -99,10 +99,10 @@ _fs_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
99 return err; 99 return err;
100 } 100 }
101 101
102 static int 102 int
103 _fs_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) 103 _stream_stdio_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes)
104 { 104 {
105 struct _fs *fs = (struct _fs *)stream; 105 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
106 size_t n = 0; 106 size_t n = 0;
107 int err = 0; 107 int err = 0;
108 108
...@@ -123,19 +123,19 @@ _fs_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) ...@@ -123,19 +123,19 @@ _fs_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes)
123 return err; 123 return err;
124 } 124 }
125 125
126 static int 126 int
127 _fs_truncate (stream_t stream, off_t len) 127 _stream_stdio_truncate (stream_t stream, off_t len)
128 { 128 {
129 struct _fs *fs = (struct _fs *)stream; 129 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
130 if (fs->file && ftruncate (fileno (fs->file), len) != 0) 130 if (fs->file && ftruncate (fileno (fs->file), len) != 0)
131 return MU_ERROR_IO; 131 return MU_ERROR_IO;
132 return 0; 132 return 0;
133 } 133 }
134 134
135 static int 135 int
136 _fs_get_size (stream_t stream, off_t *psize) 136 _stream_stdio_get_size (stream_t stream, off_t *psize)
137 { 137 {
138 struct _fs *fs = (struct _fs *)stream; 138 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
139 struct stat stbuf; 139 struct stat stbuf;
140 stbuf.st_size = 0; 140 stbuf.st_size = 0;
141 if (fs->file) 141 if (fs->file)
...@@ -149,19 +149,19 @@ _fs_get_size (stream_t stream, off_t *psize) ...@@ -149,19 +149,19 @@ _fs_get_size (stream_t stream, off_t *psize)
149 return 0; 149 return 0;
150 } 150 }
151 151
152 static int 152 int
153 _fs_flush (stream_t stream) 153 _stream_stdio_flush (stream_t stream)
154 { 154 {
155 struct _fs *fs = (struct _fs *)stream; 155 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
156 if (fs->file) 156 if (fs->file)
157 return fflush (fs->file); 157 return fflush (fs->file);
158 return 0; 158 return 0;
159 } 159 }
160 160
161 static int 161 int
162 _fs_get_fd (stream_t stream, int *pfd) 162 _stream_stdio_get_fd (stream_t stream, int *pfd)
163 { 163 {
164 struct _fs *fs = (struct _fs *)stream; 164 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
165 int status = 0; 165 int status = 0;
166 if (pfd) 166 if (pfd)
167 { 167 {
...@@ -173,10 +173,10 @@ _fs_get_fd (stream_t stream, int *pfd) ...@@ -173,10 +173,10 @@ _fs_get_fd (stream_t stream, int *pfd)
173 return status; 173 return status;
174 } 174 }
175 175
176 static int 176 int
177 _fs_seek (stream_t stream, off_t off, enum stream_whence whence) 177 _stream_stdio_seek (stream_t stream, off_t off, enum stream_whence whence)
178 { 178 {
179 struct _fs *fs = (struct _fs *)stream; 179 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
180 int err = 0; 180 int err = 0;
181 if (fs->file) 181 if (fs->file)
182 { 182 {
...@@ -194,28 +194,28 @@ _fs_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -194,28 +194,28 @@ _fs_seek (stream_t stream, off_t off, enum stream_whence whence)
194 return err; 194 return err;
195 } 195 }
196 196
197 static int 197 int
198 _fs_tell (stream_t stream, off_t *off) 198 _stream_stdio_tell (stream_t stream, off_t *off)
199 { 199 {
200 struct _fs *fs = (struct _fs *)stream; 200 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
201 if (off == NULL) 201 if (off == NULL)
202 return MU_ERROR_INVALID_PARAMETER; 202 return MU_ERROR_INVALID_PARAMETER;
203 *off = (fs->file) ? ftell (fs->file) : 0; 203 *off = (fs->file) ? ftell (fs->file) : 0;
204 return 0; 204 return 0;
205 } 205 }
206 206
207 static int 207 int
208 _fs_get_flags (stream_t stream, int *flags) 208 _stream_stdio_get_flags (stream_t stream, int *flags)
209 { 209 {
210 struct _fs *fs = (struct _fs *)stream; 210 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
211 if (flags == NULL) 211 if (flags == NULL)
212 return MU_ERROR_INVALID_PARAMETER; 212 return MU_ERROR_INVALID_PARAMETER;
213 *flags = fs->flags; 213 *flags = fs->flags;
214 return 0; 214 return 0;
215 } 215 }
216 216
217 static int 217 int
218 _fs_get_state (stream_t stream, enum stream_state *state) 218 _stream_stdio_get_state (stream_t stream, enum stream_state *state)
219 { 219 {
220 (void)stream; 220 (void)stream;
221 if (state == NULL) 221 if (state == NULL)
...@@ -224,40 +224,39 @@ _fs_get_state (stream_t stream, enum stream_state *state) ...@@ -224,40 +224,39 @@ _fs_get_state (stream_t stream, enum stream_state *state)
224 return 0; 224 return 0;
225 } 225 }
226 226
227 static int 227 int
228 _fs_is_open (stream_t stream) 228 _stream_stdio_is_open (stream_t stream)
229 { 229 {
230 struct _fs *fs = (struct _fs *)stream; 230 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
231 return (fs->file) ? 1 : 0; 231 return (fs->file) ? 1 : 0;
232 } 232 }
233 233
234 static int 234 int
235 _fs_is_readready (stream_t stream, int timeout) 235 _stream_stdio_is_readready (stream_t stream, int timeout)
236 { 236 {
237 (void)timeout; 237 (void)timeout;
238 return _fs_is_open (stream); 238 return _stream_stdio_is_open (stream);
239 } 239 }
240 240
241 static int 241 int
242 _fs_is_writeready (stream_t stream, int timeout) 242 _stream_stdio_is_writeready (stream_t stream, int timeout)
243 { 243 {
244 (void)timeout; 244 (void)timeout;
245 return _fs_is_open (stream); 245 return _stream_stdio_is_open (stream);
246 } 246 }
247 247
248 static int 248 int
249 _fs_is_exceptionpending (stream_t stream, int timeout) 249 _stream_stdio_is_exceptionpending (stream_t stream, int timeout)
250 { 250 {
251 (void)stream; 251 (void)stream;
252 (void)timeout; 252 (void)timeout;
253 return 0; 253 return 0;
254 } 254 }
255 255
256 256 int
257 static int 257 _stream_stdio_close (stream_t stream)
258 _fs_close (stream_t stream)
259 { 258 {
260 struct _fs *fs = (struct _fs *)stream; 259 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
261 int err = 0; 260 int err = 0;
262 if (fs->file) 261 if (fs->file)
263 { 262 {
...@@ -268,10 +267,10 @@ _fs_close (stream_t stream) ...@@ -268,10 +267,10 @@ _fs_close (stream_t stream)
268 return err; 267 return err;
269 } 268 }
270 269
271 static int 270 int
272 _fs_open (stream_t stream, const char *filename, int port, int flags) 271 _stream_stdio_open (stream_t stream, const char *filename, int port, int flags)
273 { 272 {
274 struct _fs *fs = (struct _fs *)stream; 273 struct _stream_stdio *fs = (struct _stream_stdio *)stream;
275 int flg; 274 int flg;
276 int fd; 275 int fd;
277 const char *mode; 276 const char *mode;
...@@ -371,40 +370,60 @@ _fs_open (stream_t stream, const char *filename, int port, int flags) ...@@ -371,40 +370,60 @@ _fs_open (stream_t stream, const char *filename, int port, int flags)
371 return 0; 370 return 0;
372 } 371 }
373 372
374 static struct _stream_vtable _fs_vtable = 373 static struct _stream_vtable _stream_stdio_vtable =
375 { 374 {
376 _fs_ref, 375 _stream_stdio_ref,
377 _fs_destroy, 376 _stream_stdio_destroy,
378 377
379 _fs_open, 378 _stream_stdio_open,
380 _fs_close, 379 _stream_stdio_close,
381 380
382 _fs_read, 381 _stream_stdio_read,
383 _fs_readline, 382 _stream_stdio_readline,
384 _fs_write, 383 _stream_stdio_write,
385 384
386 _fs_seek, 385 _stream_stdio_seek,
387 _fs_tell, 386 _stream_stdio_tell,
388 387
389 _fs_get_size, 388 _stream_stdio_get_size,
390 _fs_truncate, 389 _stream_stdio_truncate,
391 _fs_flush, 390 _stream_stdio_flush,
392 391
393 _fs_get_fd, 392 _stream_stdio_get_fd,
394 _fs_get_flags, 393 _stream_stdio_get_flags,
395 _fs_get_state, 394 _stream_stdio_get_state,
396 395
397 _fs_is_readready, 396 _stream_stdio_is_readready,
398 _fs_is_writeready, 397 _stream_stdio_is_writeready,
399 _fs_is_exceptionpending, 398 _stream_stdio_is_exceptionpending,
400 399
401 _fs_is_open 400 _stream_stdio_is_open
402 }; 401 };
403 402
404 int 403 int
404 _stream_stdio_ctor (struct _stream_stdio *fs, FILE *fp)
405 {
406 mu_refcount_create (&fs->refcount);
407 if (fs->refcount == NULL)
408 return MU_ERROR_NO_MEMORY ;
409 fs->file = fp;
410 fs->flags = 0;
411 fs->base.vtable = &_stream_stdio_vtable;
412 return 0;
413 }
414
415 void
416 _stream_stdio_dtor (struct _stream_stdio *fs)
417 {
418 mu_refcount_destroy (&fs->refcount);
419 /* We may leak if they did not close the FILE * */
420 fs->file = NULL;
421 }
422
423 int
405 stream_file_create (stream_t *pstream) 424 stream_file_create (stream_t *pstream)
406 { 425 {
407 struct _fs *fs; 426 struct _stream_stdio *fs;
408 427
409 if (pstream == NULL) 428 if (pstream == NULL)
410 return MU_ERROR_INVALID_PARAMETER; 429 return MU_ERROR_INVALID_PARAMETER;
...@@ -421,7 +440,7 @@ stream_file_create (stream_t *pstream) ...@@ -421,7 +440,7 @@ stream_file_create (stream_t *pstream)
421 } 440 }
422 fs->file = NULL; 441 fs->file = NULL;
423 fs->flags = 0; 442 fs->flags = 0;
424 fs->base.vtable = &_fs_vtable; 443 fs->base.vtable = &_stream_stdio_vtable;
425 *pstream = &fs->base; 444 *pstream = &fs->base;
426 return 0; 445 return 0;
427 } 446 }
...@@ -429,7 +448,7 @@ stream_file_create (stream_t *pstream) ...@@ -429,7 +448,7 @@ stream_file_create (stream_t *pstream)
429 int 448 int
430 stream_stdio_create (stream_t *pstream, FILE *fp) 449 stream_stdio_create (stream_t *pstream, FILE *fp)
431 { 450 {
432 struct _fs *fs; 451 struct _stream_stdio *fs;
433 452
434 if (pstream == NULL) 453 if (pstream == NULL)
435 return MU_ERROR_INVALID_PARAMETER; 454 return MU_ERROR_INVALID_PARAMETER;
...@@ -446,7 +465,7 @@ stream_stdio_create (stream_t *pstream, FILE *fp) ...@@ -446,7 +465,7 @@ stream_stdio_create (stream_t *pstream, FILE *fp)
446 } 465 }
447 fs->file = fp; 466 fs->file = fp;
448 fs->flags = 0; 467 fs->flags = 0;
449 fs->base.vtable = &_fs_vtable; 468 fs->base.vtable = &_stream_stdio_vtable;
450 *pstream = &fs->base; 469 *pstream = &fs->base;
451 return 0; 470 return 0;
452 } 471 }
......
...@@ -15,7 +15,7 @@ pkginclude_HEADERS = \ ...@@ -15,7 +15,7 @@ pkginclude_HEADERS = \
15 header.h \ 15 header.h \
16 iterator.h \ 16 iterator.h \
17 list.h \ 17 list.h \
18 locker.h \ 18 lockfile.h \
19 mailbox.h \ 19 mailbox.h \
20 mbox.h \ 20 mbox.h \
21 md5-rsa.h \ 21 md5-rsa.h \
......
...@@ -42,8 +42,6 @@ typedef struct _attribute * attribute_t; ...@@ -42,8 +42,6 @@ typedef struct _attribute * attribute_t;
42 #define MU_ATTRIBUTE_MODIFIED 0x1000 42 #define MU_ATTRIBUTE_MODIFIED 0x1000
43 #define MU_ATTRIBUTE_RECENT 0x0000 43 #define MU_ATTRIBUTE_RECENT 0x0000
44 44
45 extern int attribute_create __P ((attribute_t *));
46
47 extern int attribute_ref __P ((attribute_t)); 45 extern int attribute_ref __P ((attribute_t));
48 extern void attribute_destroy __P ((attribute_t *)); 46 extern void attribute_destroy __P ((attribute_t *));
49 47
...@@ -83,6 +81,8 @@ extern int attribute_clear_flags __P ((attribute_t)); ...@@ -83,6 +81,8 @@ extern int attribute_clear_flags __P ((attribute_t));
83 extern int attribute_is_equal __P ((attribute_t, attribute_t)); 81 extern int attribute_is_equal __P ((attribute_t, attribute_t));
84 extern int attribute_copy __P ((attribute_t dst, attribute_t src)); 82 extern int attribute_copy __P ((attribute_t dst, attribute_t src));
85 83
84 extern int attribute_default_create __P ((attribute_t *));
85 extern int attribute_status_create __P ((attribute_t *, const char *));
86 86
87 #ifdef __cplusplus 87 #ifdef __cplusplus
88 } 88 }
......
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
15 along with this program; if not, write to the Free Software 15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #ifndef _MAILUTILS_LOCKER_H 18 #ifndef _MAILUTILS_LOCKFILE_H
19 #define _MAILUTILS_LOCKER_H 19 #define _MAILUTILS_LOCKFILE_H
20 20
21 #include <sys/types.h> 21 #include <sys/types.h>
22 22
...@@ -32,21 +32,21 @@ extern "C" { ...@@ -32,21 +32,21 @@ extern "C" {
32 # endif 32 # endif
33 #endif /*__P */ 33 #endif /*__P */
34 34
35 struct _locker; 35 struct _lockfile;
36 typedef struct _locker *locker_t; 36 typedef struct _lockfile *lockfile_t;
37 37
38 extern int locker_ref __P ((locker_t)); 38 extern int lockfile_ref __P ((lockfile_t));
39 extern void locker_destroy __P ((locker_t *)); 39 extern void lockfile_destroy __P ((lockfile_t *));
40 40
41 extern int locker_lock __P ((locker_t)); 41 extern int lockfile_lock __P ((lockfile_t));
42 extern int locker_touchlock __P ((locker_t)); 42 extern int lockfile_touchlock __P ((lockfile_t));
43 extern int locker_unlock __P ((locker_t)); 43 extern int lockfile_unlock __P ((lockfile_t));
44 44
45 extern int locker_dotlock_create __P ((locker_t *, const char *filename)); 45 extern int lockfile_dotlock_create __P ((lockfile_t *, const char *filename));
46 extern int locker_nfslock_create __P ((locker_t *, const char *filename)); 46 extern int lockfile_nfslock_create __P ((lockfile_t *, const char *filename));
47 47
48 #ifdef __cplusplus 48 #ifdef __cplusplus
49 } 49 }
50 #endif 50 #endif
51 51
52 #endif /* _MAILUTILS_MAILBOX_H */ 52 #endif /* _MAILUTILS_LOCKFILE_H */
......
...@@ -41,7 +41,7 @@ extern int mbox_create __P ((mbox_t *)); ...@@ -41,7 +41,7 @@ extern int mbox_create __P ((mbox_t *));
41 extern void mbox_destroy __P ((mbox_t *)); 41 extern void mbox_destroy __P ((mbox_t *));
42 42
43 extern int mbox_get_uidvalidity __P ((mbox_t, unsigned long *)); 43 extern int mbox_get_uidvalidity __P ((mbox_t, unsigned long *));
44 extern int mbox_get_uidnext __P ((mbox_t, unsigned long)); 44 extern int mbox_get_uidnext __P ((mbox_t, unsigned long *));
45 45
46 extern int mbox_open __P ((mbox_t, const char *, int)); 46 extern int mbox_open __P ((mbox_t, const char *, int));
47 extern int mbox_close __P ((mbox_t)); 47 extern int mbox_close __P ((mbox_t));
...@@ -57,7 +57,7 @@ extern int mbox_set_hstream __P ((mbox_t, unsigned int, stream_t)); ...@@ -57,7 +57,7 @@ extern int mbox_set_hstream __P ((mbox_t, unsigned int, stream_t));
57 extern int mbox_get_hsize __P ((mbox_t, unsigned int, unsigned int *)); 57 extern int mbox_get_hsize __P ((mbox_t, unsigned int, unsigned int *));
58 extern int mbox_get_hlines __P ((mbox_t, unsigned int, unsigned int *)); 58 extern int mbox_get_hlines __P ((mbox_t, unsigned int, unsigned int *));
59 59
60 extern int mbox_get_bstream __P ((mbox_t, unsigned int, stream_t int *)); 60 extern int mbox_get_bstream __P ((mbox_t, unsigned int, stream_t *));
61 extern int mbox_set_bstream __P ((mbox_t, unsigned int, stream_t)); 61 extern int mbox_set_bstream __P ((mbox_t, unsigned int, stream_t));
62 extern int mbox_get_bsize __P ((mbox_t, unsigned int, unsigned int *)); 62 extern int mbox_get_bsize __P ((mbox_t, unsigned int, unsigned int *));
63 extern int mbox_get_blines __P ((mbox_t, unsigned int, unsigned int *)); 63 extern int mbox_get_blines __P ((mbox_t, unsigned int, unsigned int *));
...@@ -75,12 +75,18 @@ extern int mbox_set_newmsg_cb __P ((mbox_t, int (*) __P ((int, void *)), void ...@@ -75,12 +75,18 @@ extern int mbox_set_newmsg_cb __P ((mbox_t, int (*) __P ((int, void *)), void
75 extern int mbox_newmsg_cb __P ((mbox_t, int)); 75 extern int mbox_newmsg_cb __P ((mbox_t, int));
76 extern int mbox_progress_cb __P ((mbox_t, int)); 76 extern int mbox_progress_cb __P ((mbox_t, int));
77 77
78 extern int mbox_scan __P ((mbox_t, unsigned int, unsigned int *)); 78 extern int mbox_scan __P ((mbox_t, unsigned int, unsigned int *, int));
79 extern int mbox_messages_count __P ((mbox_t, unsigned int *)); 79 extern int mbox_messages_count __P ((mbox_t, unsigned int *));
80 80
81 extern int mbox_append __P ((mbox_t, const char *, stream_t)); 81 extern int mbox_append __P ((mbox_t, const char *, stream_t));
82 extern int mbox_append_hb __P ((mbox_t, const char *, stream_t, stream_t)); 82 extern int mbox_append_hb __P ((mbox_t, const char *, stream_t, stream_t));
83 83
84 extern int mbox_get_carrier __P ((mbox_t, stream_t *));
85 extern int mbox_set_carrier __P ((mbox_t, stream_t));
86
87 extern int mbox_set_hcache __P ((mbox_t, char **, unsigned int));
88 extern int mbox_hcache_get_value __P ((mbox_t, const char *, char *, size_t,
89 size_t *));
84 90
85 #ifdef __cplusplus 91 #ifdef __cplusplus
86 } 92 }
......
...@@ -81,8 +81,8 @@ extern int stream_is_open __P ((stream_t)); ...@@ -81,8 +81,8 @@ extern int stream_is_open __P ((stream_t));
81 /* Misc. */ 81 /* Misc. */
82 extern int stream_file_create __P ((stream_t *)); 82 extern int stream_file_create __P ((stream_t *));
83 extern int stream_stdio_create __P ((stream_t *, FILE *)); 83 extern int stream_stdio_create __P ((stream_t *, FILE *));
84 extern int stream_mapfile_create __P ((stream_t *)); 84 extern int stream_mmap_create __P ((stream_t *));
85 extern int stream_memory_create __P ((stream_t *)); 85 extern int stream_memory_create __P ((stream_t *, size_t));
86 extern int stream_tcp_create __P ((stream_t *)); 86 extern int stream_tcp_create __P ((stream_t *));
87 extern int stream_fd_create __P ((stream_t *, int)); 87 extern int stream_fd_create __P ((stream_t *, int));
88 extern int stream_buffer_create __P ((stream_t *, stream_t, size_t)); 88 extern int stream_buffer_create __P ((stream_t *, stream_t, size_t));
......
...@@ -5,6 +5,7 @@ pkginclude_HEADERS = \ ...@@ -5,6 +5,7 @@ pkginclude_HEADERS = \
5 attribute.h \ 5 attribute.h \
6 authority.h \ 6 authority.h \
7 bstream.h \ 7 bstream.h \
8 dattribute.h \
8 debug.h \ 9 debug.h \
9 envelope.h \ 10 envelope.h \
10 folder.h \ 11 folder.h \
...@@ -12,15 +13,16 @@ pkginclude_HEADERS = \ ...@@ -12,15 +13,16 @@ pkginclude_HEADERS = \
12 header.h \ 13 header.h \
13 iterator.h \ 14 iterator.h \
14 list.h \ 15 list.h \
15 locker.h \ 16 lockfile.h \
16 mailbox.h \ 17 mailbox.h \
17 mbox.h \ 18 mbox.h \
19 mapstream.h \
18 memstream.h \ 20 memstream.h \
19 message.h \ 21 message.h \
20 mstream.h \
21 observable.h \ 22 observable.h \
22 observer.h \ 23 observer.h \
23 pop3.h \ 24 pop3.h \
25 pticket.h \
24 refcount.h \ 26 refcount.h \
25 sdebug.h \ 27 sdebug.h \
26 stream.h \ 28 stream.h \
......
...@@ -21,6 +21,10 @@ ...@@ -21,6 +21,10 @@
21 #include <mailutils/sys/stream.h> 21 #include <mailutils/sys/stream.h>
22 #include <mailutils/refcount.h> 22 #include <mailutils/refcount.h>
23 23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
24 /* Read buffer */ 28 /* Read buffer */
25 struct _rbuffer 29 struct _rbuffer
26 { 30 {
...@@ -30,7 +34,7 @@ struct _rbuffer ...@@ -30,7 +34,7 @@ struct _rbuffer
30 size_t bufsize; 34 size_t bufsize;
31 }; 35 };
32 36
33 struct _bs 37 struct _stream_buffer
34 { 38 {
35 struct _stream base; 39 struct _stream base;
36 mu_refcount_t refcount; 40 mu_refcount_t refcount;
...@@ -38,4 +42,30 @@ struct _bs ...@@ -38,4 +42,30 @@ struct _bs
38 struct _rbuffer rbuffer; 42 struct _rbuffer rbuffer;
39 }; 43 };
40 44
45 extern int _stream_buffer_ctor __P ((struct _stream_buffer *, stream_t, size_t));
46 extern void _stream_buffer_dtor __P ((struct _stream_buffer *));
47 extern int _stream_buffer_ref __P ((stream_t));
48 extern void _stream_buffer_destroy __P ((stream_t *));
49 extern int _stream_buffer_open __P ((stream_t, const char *, int, int));
50 extern int _stream_buffer_close __P ((stream_t));
51 extern int _stream_buffer_read __P ((stream_t, void *, size_t, size_t *));
52 extern int _stream_buffer_readline __P ((stream_t, char *, size_t, size_t *));
53 extern int _stream_buffer_write __P ((stream_t, const void *, size_t, size_t *));
54 extern int _stream_buffer_get_fd __P ((stream_t, int *));
55 extern int _stream_buffer_get_flags __P ((stream_t, int *));
56 extern int _stream_buffer_get_size __P ((stream_t, off_t *));
57 extern int _stream_buffer_truncate __P ((stream_t, off_t));
58 extern int _stream_buffer_flush __P ((stream_t));
59 extern int _stream_buffer_get_state __P ((stream_t, enum stream_state *));
60 extern int _stream_buffer_seek __P ((stream_t, off_t, enum stream_whence));
61 extern int _stream_buffer_tell __P ((stream_t, off_t *));
62 extern int _stream_buffer_is_readready __P ((stream_t, int ));
63 extern int _stream_buffer_is_writeready __P ((stream_t, int));
64 extern int _stream_buffer_is_exceptionpending __P ((stream_t, int));
65 extern int _stream_buffer_is_open __P ((stream_t));
66
67 #ifdef __cplusplus
68 }
69 #endif
70
41 #endif /* _MAILUTILS_SYS_BSTREAM_H */ 71 #endif /* _MAILUTILS_SYS_BSTREAM_H */
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MAILUTILS_SYS_DATTRIBUTE_H
19 # define _MAILUTILS_SYS_DATTRIBUTE_H
20
21 #ifdef DMALLOC
22 # include <dmalloc.h>
23 #endif
24
25 #include <mailutils/sys/attribute.h>
26 #include <mailutils/refcount.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /* A simple default attribute implementation. */
33 struct _attribute_default
34 {
35 struct _attribute base;
36 mu_refcount_t refcount;
37 int flags;
38 };
39
40 extern int _attribute_default_ctor __P ((struct _attribute_default *));
41 extern void _attribute_default_dtor __P ((struct _attribute_default *));
42 extern int _attribute_default_ref __P ((attribute_t));
43 extern void _attribute_default_destroy __P ((attribute_t *));
44
45 extern int _attribute_default_get_flags __P ((attribute_t, int *));
46 extern int _attribute_default_set_flags __P ((attribute_t, int));
47 extern int _attribute_default_unset_flags __P ((attribute_t, int));
48 extern int _attribute_default_clear_flags __P ((attribute_t));
49
50 #ifdef __cplusplus
51 }
52 #endif
53
54 #endif /* _MAILUTILS_SYS_ATTRIBUTE_H */
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MAILUTILS_SYS_DOTLOCK_H
19 #define _MAILUTILS_SYS_DOTLOCK_H
20
21 #ifdef DMALLOC
22 #include <dmalloc.h>
23 #endif
24
25 #include <mailutils/refcount.h>
26 #include <mailutils/sys/lockfile.h>
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 /* locking flags */
33 #define MU_LOCKFILE_DOTLOCK_PID 1
34 #define MU_LOCKFILE_DOTLOCK_FCNTL 2
35 #define MU_LOCKFILE_DOTLOCK_TIME 4
36
37 #define MU_LOCKFILE_DOTLOCK_EXPIRE_TIME (5 * 60)
38
39 #define MU_LOCKFILE_DOTLOCK_ATTR 0444
40
41 struct _lockfile_dotlock
42 {
43 struct _lockfile base;
44 mu_refcount_t refcount;
45 int fd;
46 int refcnt; /* For recursive file locking. */
47 char *fname;
48 int flags;
49 };
50
51 extern int _lockfile_dotlock_ctor __P ((struct _lockfile_dotlock *, const char *));
52 extern void _lockfile_dotlock_dtor __P ((struct _lockfile_dotlock *));
53 extern int _lockfile_dotlock_ref __P ((lockfile_t));
54 extern void _lockfile_dotlock_destroy __P ((lockfile_t *));
55
56 extern int _lockfile_dotlock_lock __P ((lockfile_t));
57 extern int _lockfile_dotlock_touchlock __P ((lockfile_t));
58 extern int _lockfile_dotlock_unlock __P ((lockfile_t));
59
60 #ifdef __cplusplus
61 }
62 #endif
63
64 #endif /* _MAILUTILS_SYS_DOTLOCK_H */
...@@ -21,7 +21,11 @@ ...@@ -21,7 +21,11 @@
21 #include <mailutils/refcount.h> 21 #include <mailutils/refcount.h>
22 #include <mailutils/sys/stream.h> 22 #include <mailutils/sys/stream.h>
23 23
24 struct _fds 24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 struct _stream_fd
25 { 29 {
26 struct _stream base; 30 struct _stream base;
27 mu_refcount_t refcount; 31 mu_refcount_t refcount;
...@@ -30,4 +34,31 @@ struct _fds ...@@ -30,4 +34,31 @@ struct _fds
30 int flags; 34 int flags;
31 }; 35 };
32 36
37 int _stream_fd_ctor __P ((struct _stream_fd *, int));
38 void _stream_fd_dtor __P ((struct _stream_fd *));
39
40 int _stream_fd_ref __P ((stream_t));
41 void _stream_fd_destroy __P ((stream_t *));
42 int _stream_fd_open __P ((stream_t, const char *, int, int));
43 int _stream_fd_close __P ((stream_t));
44 int _stream_fd_read __P ((stream_t, void *, size_t, size_t *));
45 int _stream_fd_readline __P ((stream_t, char *, size_t, size_t *));
46 int _stream_fd_write __P ((stream_t, const void *, size_t, size_t *));
47 int _stream_fd_get_fd __P ((stream_t, int *));
48 int _stream_fd_get_flags __P ((stream_t, int *));
49 int _stream_fd_get_size __P ((stream_t, off_t *));
50 int _stream_fd_truncate __P ((stream_t, off_t));
51 int _stream_fd_flush __P ((stream_t));
52 int _stream_fd_get_state __P ((stream_t, enum stream_state *));
53 int _stream_fd_seek __P ((stream_t, off_t, enum stream_whence));
54 int _stream_fd_tell __P ((stream_t, off_t *));
55 int _stream_fd_is_readready __P ((stream_t, int ));
56 int _stream_fd_is_writeready __P ((stream_t, int));
57 int _stream_fd_is_exceptionpending __P ((stream_t, int));
58 int _stream_fd_is_open __P ((stream_t));
59
60 #ifdef __cplusplus
61 }
62 #endif
63
33 #endif /* _MAILUTILS_SYS_FDSTREAM_H */ 64 #endif /* _MAILUTILS_SYS_FDSTREAM_H */
......
...@@ -21,7 +21,11 @@ ...@@ -21,7 +21,11 @@
21 #include <mailutils/sys/stream.h> 21 #include <mailutils/sys/stream.h>
22 #include <mailutils/refcount.h> 22 #include <mailutils/refcount.h>
23 23
24 struct _fs 24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 struct _stream_stdio
25 { 29 {
26 struct _stream base; 30 struct _stream base;
27 mu_refcount_t refcount; 31 mu_refcount_t refcount;
...@@ -29,4 +33,30 @@ struct _fs ...@@ -29,4 +33,30 @@ struct _fs
29 FILE *file; 33 FILE *file;
30 }; 34 };
31 35
36 extern int _stream_stdio_ctor __P ((struct _stream_stdio *, FILE *));
37 extern void _stream_stdio_dtor __P ((struct _stream_stdio *));
38 extern int _stream_stdio_ref __P ((stream_t));
39 extern void _stream_stdio_destroy __P ((stream_t *));
40 extern int _stream_stdio_open __P ((stream_t, const char *, int, int));
41 extern int _stream_stdio_close __P ((stream_t));
42 extern int _stream_stdio_read __P ((stream_t, void *, size_t, size_t *));
43 extern int _stream_stdio_readline __P ((stream_t, char *, size_t, size_t *));
44 extern int _stream_stdio_write __P ((stream_t, const void *, size_t, size_t *));
45 extern int _stream_stdio_get_fd __P ((stream_t, int *));
46 extern int _stream_stdio_get_flags __P ((stream_t, int *));
47 extern int _stream_stdio_get_size __P ((stream_t, off_t *));
48 extern int _stream_stdio_truncate __P ((stream_t, off_t));
49 extern int _stream_stdio_flush __P ((stream_t));
50 extern int _stream_stdio_get_state __P ((stream_t, enum stream_state *));
51 extern int _stream_stdio_seek __P ((stream_t, off_t, enum stream_whence));
52 extern int _stream_stdio_tell __P ((stream_t, off_t *));
53 extern int _stream_stdio_is_readready __P ((stream_t, int ));
54 extern int _stream_stdio_is_writeready __P ((stream_t, int));
55 extern int _stream_stdio_is_exceptionpending __P ((stream_t, int));
56 extern int _stream_stdio_is_open __P ((stream_t));
57
58 #ifdef __cplusplus
59 }
60 #endif
61
32 #endif /* _MAILUTILS_SYS_FSTREAM_H */ 62 #endif /* _MAILUTILS_SYS_FSTREAM_H */
......
...@@ -15,14 +15,14 @@ ...@@ -15,14 +15,14 @@
15 along with this program; if not, write to the Free Software 15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ 16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17 17
18 #ifndef _MAILUTILS_SYS_LOCKER_H 18 #ifndef _MAILUTILS_SYS_LOCKFILE_H
19 #define _MAILUTILS_SYS_LOCKER_H 19 #define _MAILUTILS_SYS_LOCKFILE_H
20 20
21 #ifdef DMALLOC 21 #ifdef DMALLOC
22 #include <dmalloc.h> 22 #include <dmalloc.h>
23 #endif 23 #endif
24 24
25 #include <mailutils/locker.h> 25 #include <mailutils/lockfile.h>
26 26
27 #ifdef __cplusplus 27 #ifdef __cplusplus
28 extern "C" { 28 extern "C" {
...@@ -36,23 +36,23 @@ extern "C" { ...@@ -36,23 +36,23 @@ extern "C" {
36 # endif 36 # endif
37 #endif /*__P */ 37 #endif /*__P */
38 38
39 struct _locker_vtable 39 struct _lockfile_vtable
40 { 40 {
41 int (*ref) __P ((locker_t)); 41 int (*ref) __P ((lockfile_t));
42 void (*destroy) __P ((locker_t *)); 42 void (*destroy) __P ((lockfile_t *));
43 43
44 int (*lock) __P ((locker_t)); 44 int (*lock) __P ((lockfile_t));
45 int (*touchlock) __P ((locker_t)); 45 int (*touchlock) __P ((lockfile_t));
46 int (*unlock) __P ((locker_t)); 46 int (*unlock) __P ((lockfile_t));
47 }; 47 };
48 48
49 struct _locker 49 struct _lockfile
50 { 50 {
51 struct _locker_vtable *vtable; 51 struct _lockfile_vtable *vtable;
52 }; 52 };
53 53
54 #ifdef __cplusplus 54 #ifdef __cplusplus
55 } 55 }
56 #endif 56 #endif
57 57
58 #endif /* _MAILUTILS_SYS_ENVELOPE_H */ 58 #endif /* _MAILUTILS_SYS_LOCKFILE_H */
......
...@@ -18,10 +18,18 @@ ...@@ -18,10 +18,18 @@
18 #ifndef MAILUTILS_SYS_MSTREAM_H 18 #ifndef MAILUTILS_SYS_MSTREAM_H
19 #define MAILUTILS_SYS_MSTREAM_H 19 #define MAILUTILS_SYS_MSTREAM_H
20 20
21 #include <unistd.h>
22
21 #include <mailutils/sys/stream.h> 23 #include <mailutils/sys/stream.h>
22 #include <mailutils/refcount.h> 24 #include <mailutils/refcount.h>
23 25
24 struct _ms 26 #ifdef _POSIX_MAPPED_FILES
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 struct _stream_mmap
25 { 33 {
26 struct _stream base; 34 struct _stream base;
27 mu_refcount_t refcount; 35 mu_refcount_t refcount;
...@@ -33,4 +41,31 @@ struct _ms ...@@ -33,4 +41,31 @@ struct _ms
33 off_t offset; 41 off_t offset;
34 }; 42 };
35 43
44 extern int _stream_mmap_ctor __P ((struct _stream_mmap *));
45 extern void _stream_mmap_dtor __P ((struct _stream_mmap *));
46 extern int _stream_mmap_ref __P ((stream_t));
47 extern void _stream_mmap_destroy __P ((stream_t *));
48 extern int _stream_mmap_open __P ((stream_t, const char *, int, int));
49 extern int _stream_mmap_close __P ((stream_t));
50 extern int _stream_mmap_read __P ((stream_t, void *, size_t, size_t *));
51 extern int _stream_mmap_readline __P ((stream_t, char *, size_t, size_t *));
52 extern int _stream_mmap_write __P ((stream_t, const void *, size_t, size_t *));
53 extern int _stream_mmap_get_fd __P ((stream_t, int *));
54 extern int _stream_mmap_get_flags __P ((stream_t, int *));
55 extern int _stream_mmap_get_size __P ((stream_t, off_t *));
56 extern int _stream_mmap_truncate __P ((stream_t, off_t));
57 extern int _stream_mmap_flush __P ((stream_t));
58 extern int _stream_mmap_get_state __P ((stream_t, enum stream_state *));
59 extern int _stream_mmap_seek __P ((stream_t, off_t, enum stream_whence));
60 extern int _stream_mmap_tell __P ((stream_t, off_t *));
61 extern int _stream_mmap_is_readready __P ((stream_t, int ));
62 extern int _stream_mmap_is_writeready __P ((stream_t, int));
63 extern int _stream_mmap_is_exceptionpending __P ((stream_t, int));
64 extern int _stream_mmap_is_open __P ((stream_t));
65 #endif
66
67 #ifdef __cplusplus
68 }
69 #endif
70
36 #endif /* _MAILUTILS_SYS_MSTREAM_H */ 71 #endif /* _MAILUTILS_SYS_MSTREAM_H */
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
19 #define _MAILUTILS_SYS_MBOX_H 19 #define _MAILUTILS_SYS_MBOX_H
20 20
21 #include <time.h> 21 #include <time.h>
22 #include <mailutils/locker.h> 22 #include <mailutils/lockfile.h>
23 #include <mailutils/mbox.h> 23 #include <mailutils/mbox.h>
24 24
25 #ifdef __cplusplus 25 #ifdef __cplusplus
...@@ -28,35 +28,6 @@ extern "C" { ...@@ -28,35 +28,6 @@ extern "C" {
28 28
29 typedef struct _mbox_message* mbox_message_t; 29 typedef struct _mbox_message* mbox_message_t;
30 30
31
32 /* Keep the file positions of where the headers and bodies start and end.
33 attribute is the "Status:" message. */
34 struct _mbox_message
35 {
36 /* Offset of the messages in the mailbox. */
37 off_t from_;
38
39 /* Fast header retrieve, we save here the most common headers. This will
40 speed the header search. The entire headers are copied, when modified,
41 by the header_t object, we do not have to worry about updating them. */
42 char **fhdr;
43
44 struct
45 {
46 stream_t stream;
47 unsigned int lines;
48 unsigned int size;
49 off_t start;
50 off_t end;
51 } header, body;
52
53 /* UID i.e. see IMAP */
54 unsigned long uid;
55 attribute_t attribute; /* The attr_flags contains the "Status:" attribute */
56
57 mbox_t mbox; /* Back pointer. */
58 };
59
60 /* The umessages is an array of pointers that contains umessages_count of 31 /* The umessages is an array of pointers that contains umessages_count of
61 mbox_message_t*; umessages[umessages_count]. We do it this way because 32 mbox_message_t*; umessages[umessages_count]. We do it this way because
62 realloc() can move everything to a new memory region and invalidate all 33 realloc() can move everything to a new memory region and invalidate all
...@@ -85,8 +56,7 @@ struct _mbox ...@@ -85,8 +56,7 @@ struct _mbox
85 MBOX_STATE_APPEND_BODY 56 MBOX_STATE_APPEND_BODY
86 } state ; 57 } state ;
87 58
88 locker_t locker; 59 lockfile_t lockfile;
89
90 struct 60 struct
91 { 61 {
92 int (*cb) __P ((int, void *)); 62 int (*cb) __P ((int, void *));
...@@ -94,6 +64,35 @@ struct _mbox ...@@ -94,6 +64,35 @@ struct _mbox
94 } newmsg, progress, corrupt; 64 } newmsg, progress, corrupt;
95 }; 65 };
96 66
67 /* Keep the file positions of where the headers and bodies start and end.
68 attribute is the "Status:" message. */
69 struct _mbox_message
70 {
71 /* Offset of the messages in the mailbox. */
72 off_t from_;
73
74 /* Fast header retrieve, we save here the most common headers. This will
75 speed the header search. The entire headers are copied, when modified,
76 by the header_t object, we do not have to worry about updating them. */
77 char **fhdr;
78
79 struct
80 {
81 stream_t stream;
82 unsigned int lines;
83 unsigned int size;
84 off_t start;
85 off_t end;
86 } header, body;
87
88 /* UID i.e. see IMAP */
89 unsigned long uid;
90 unsigned int attr_flags;
91 attribute_t attribute; /* The attr_flags contains the "Status:" attribute */
92
93 mbox_t mbox; /* Back pointer. */
94 };
95
97 #ifdef __cplusplus 96 #ifdef __cplusplus
98 } 97 }
99 #endif 98 #endif
......
...@@ -23,14 +23,49 @@ ...@@ -23,14 +23,49 @@
23 #include <mailutils/refcount.h> 23 #include <mailutils/refcount.h>
24 #include <mailutils/sys/stream.h> 24 #include <mailutils/sys/stream.h>
25 25
26 struct _memory_stream 26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #ifndef MU_STREAM_MEMORY_BLOCKSIZE
31 #define MU_STREAM_MEMORY_BLOCKSIZE 512
32 #endif
33
34 struct _stream_memory
27 { 35 {
28 struct _stream base; 36 struct _stream base;
29 mu_refcount_t refcount; 37 mu_refcount_t refcount;
30 char *ptr; 38 char *ptr;
39 size_t capacity;
31 size_t size; 40 size_t size;
32 off_t offset; 41 off_t offset;
33 int flags; 42 int flags;
34 }; 43 };
35 44
45 extern int _stream_memory_ctor __P ((struct _stream_memory *, size_t));
46 extern void _stream_memory_dtor __P ((struct _stream_memory *));
47 extern int _stream_memory_ref __P ((stream_t));
48 extern void _stream_memory_destroy __P ((stream_t *));
49 extern int _stream_memory_open __P ((stream_t, const char *, int, int));
50 extern int _stream_memory_close __P ((stream_t));
51 extern int _stream_memory_read __P ((stream_t, void *, size_t, size_t *));
52 extern int _stream_memory_readline __P ((stream_t, char *, size_t, size_t *));
53 extern int _stream_memory_write __P ((stream_t, const void *, size_t, size_t *));
54 extern int _stream_memory_get_fd __P ((stream_t, int *));
55 extern int _stream_memory_get_flags __P ((stream_t, int *));
56 extern int _stream_memory_get_size __P ((stream_t, off_t *));
57 extern int _stream_memory_truncate __P ((stream_t, off_t));
58 extern int _stream_memory_flush __P ((stream_t));
59 extern int _stream_memory_get_state __P ((stream_t, enum stream_state *));
60 extern int _stream_memory_seek __P ((stream_t, off_t, enum stream_whence));
61 extern int _stream_memory_tell __P ((stream_t, off_t *));
62 extern int _stream_memory_is_readready __P ((stream_t, int ));
63 extern int _stream_memory_is_writeready __P ((stream_t, int));
64 extern int _stream_memory_is_exceptionpending __P ((stream_t, int));
65 extern int _stream_memory_is_open __P ((stream_t));
66
67 #ifdef __cplusplus
68 }
69 #endif
70
36 #endif /* _MAILUTILS_SYS_MEMSTREAM_H */ 71 #endif /* _MAILUTILS_SYS_MEMSTREAM_H */
......
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Library Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 #ifndef _MAILUTILS_SYS_PTICKET_H
19 #define _MAILUTILS_SYS_PTICKET_H
20
21 #include <mailutils/sys/ticket.h>
22
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26
27 struct _ticket_prompt
28 {
29 struct _ticket base;
30 mu_refcount_t refcount;
31 };
32
33 extern int _ticket_prompt_ctor __P ((struct _ticket_prompt *));
34 extern void _ticket_prompt_dtor __P ((struct _ticket_prompt *));
35 extern int _ticket_prompt_ref __P ((ticket_t));
36 extern void _ticket_prompt_destroy __P ((ticket_t *));
37 extern int _ticket_prompt_pop __P ((ticket_t, const char *, char **));
38
39 #ifdef __cplusplus
40 }
41 #endif
42
43 #endif /* _MAILUTILS_SYS_PTICKET_H */
...@@ -29,15 +29,7 @@ ...@@ -29,15 +29,7 @@
29 extern "C" { 29 extern "C" {
30 #endif 30 #endif
31 31
32 #ifndef __P 32 struct _mu_debug_stream
33 # ifdef __STDC__
34 # define __P(args) args
35 # else
36 # define __P(args) ()
37 # endif
38 #endif /*__P */
39
40 struct _sdebug
41 { 33 {
42 struct _mu_debug base; 34 struct _mu_debug base;
43 mu_refcount_t refcount; 35 mu_refcount_t refcount;
...@@ -46,6 +38,14 @@ struct _sdebug ...@@ -46,6 +38,14 @@ struct _sdebug
46 int close_on_destroy; 38 int close_on_destroy;
47 }; 39 };
48 40
41 int _mu_debug_stream_ctor __P ((struct _mu_debug_stream *));
42 int _mu_debug_stream_dtor __P ((struct _mu_debug_stream *));
43 int _mu_debug_stream_ref __P ((mu_debug_t));
44 void _mu_debug_stream_destroy __P ((mu_debug_t *));
45 int _mu_debug_stream_set_level __P ((mu_debug_t, size_t));
46 int _mu_debug_stream_get_level __P ((mu_debug_t, size_t *));
47 int _mu_debug_stream_print __P ((mu_debug_t, size_t, const char *));
48
49 #ifdef __cplusplus 49 #ifdef __cplusplus
50 } 50 }
51 #endif 51 #endif
......
...@@ -19,9 +19,13 @@ ...@@ -19,9 +19,13 @@
19 #define MAILUTILS_SYS_TCP_H 19 #define MAILUTILS_SYS_TCP_H
20 20
21 #include <mailutils/refcount.h> 21 #include <mailutils/refcount.h>
22 #include <mailutils/sys/stream.h> 22 #include <mailutils/sys/fdstream.h>
23 23
24 enum _tcp_state 24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 enum _stream_tcp_state
25 { 29 {
26 TCP_STATE_INIT, 30 TCP_STATE_INIT,
27 TCP_STATE_RESOLVE, 31 TCP_STATE_RESOLVE,
...@@ -30,16 +34,21 @@ enum _tcp_state ...@@ -30,16 +34,21 @@ enum _tcp_state
30 TCP_STATE_CONNECTED 34 TCP_STATE_CONNECTED
31 }; 35 };
32 36
33 struct _tcp_instance 37 struct _stream_tcp
34 { 38 {
35 struct _stream base; 39 struct _stream_fd base;
36 mu_refcount_t refcount;
37 int fd;
38 char *host; 40 char *host;
39 int port; 41 int port;
40 int state; 42 int state;
41 int flags;
42 unsigned long address; 43 unsigned long address;
43 }; 44 };
44 45
46 void _stream_tcp_destroy __P ((stream_t *));
47 int _stream_tcp_close __P ((stream_t));
48 int _stream_tcp_open __P ((stream_t, const char *, int, int));
49
50 #ifdef __cplusplus
51 }
52 #endif
53
45 #endif /* _MAILUTILS_SYS_TCP_H */ 54 #endif /* _MAILUTILS_SYS_TCP_H */
......
...@@ -21,52 +21,52 @@ ...@@ -21,52 +21,52 @@
21 21
22 #include <stdlib.h> 22 #include <stdlib.h>
23 #include <mailutils/error.h> 23 #include <mailutils/error.h>
24 #include <mailutils/sys/locker.h> 24 #include <mailutils/sys/lockfile.h>
25 25
26 int 26 int
27 locker_ref (locker_t locker) 27 lockfile_ref (lockfile_t lockfile)
28 { 28 {
29 if (locker == NULL || locker->vtable == NULL 29 if (lockfile == NULL || lockfile->vtable == NULL
30 || locker->vtable->ref == NULL) 30 || lockfile->vtable->ref == NULL)
31 return MU_ERROR_NOT_SUPPORTED; 31 return MU_ERROR_NOT_SUPPORTED;
32 return locker->vtable->ref (locker); 32 return lockfile->vtable->ref (lockfile);
33 } 33 }
34 34
35 void 35 void
36 locker_destroy (locker_t *plocker) 36 lockfile_destroy (lockfile_t *plockfile)
37 { 37 {
38 if (plocker && *plocker) 38 if (plockfile && *plockfile)
39 { 39 {
40 locker_t locker = *plocker; 40 lockfile_t lockfile = *plockfile;
41 if (locker->vtable && locker->vtable->destroy) 41 if (lockfile->vtable && lockfile->vtable->destroy)
42 locker->vtable->destroy (plocker); 42 lockfile->vtable->destroy (plockfile);
43 *plocker = NULL; 43 *plockfile = NULL;
44 } 44 }
45 } 45 }
46 46
47 int 47 int
48 locker_lock (locker_t locker) 48 lockfile_lock (lockfile_t lockfile)
49 { 49 {
50 if (locker == NULL || locker->vtable == NULL 50 if (lockfile == NULL || lockfile->vtable == NULL
51 || locker->vtable->lock == NULL) 51 || lockfile->vtable->lock == NULL)
52 return MU_ERROR_NOT_SUPPORTED; 52 return MU_ERROR_NOT_SUPPORTED;
53 return locker->vtable->lock (locker); 53 return lockfile->vtable->lock (lockfile);
54 } 54 }
55 55
56 int 56 int
57 locker_touchlock (locker_t locker) 57 lockfile_touchlock (lockfile_t lockfile)
58 { 58 {
59 if (locker == NULL || locker->vtable == NULL 59 if (lockfile == NULL || lockfile->vtable == NULL
60 || locker->vtable->lock == NULL) 60 || lockfile->vtable->lock == NULL)
61 return MU_ERROR_NOT_SUPPORTED; 61 return MU_ERROR_NOT_SUPPORTED;
62 return locker->vtable->lock (locker); 62 return lockfile->vtable->lock (lockfile);
63 } 63 }
64 64
65 int 65 int
66 locker_unlock (locker_t locker) 66 lockfile_unlock (lockfile_t lockfile)
67 { 67 {
68 if (locker == NULL || locker->vtable == NULL 68 if (lockfile == NULL || lockfile->vtable == NULL
69 || locker->vtable->lock == NULL) 69 || lockfile->vtable->lock == NULL)
70 return MU_ERROR_NOT_SUPPORTED; 70 return MU_ERROR_NOT_SUPPORTED;
71 return locker->vtable->lock (locker); 71 return lockfile->vtable->lock (lockfile);
72 } 72 }
......
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
29 #include <fcntl.h> 29 #include <fcntl.h>
30 #include <unistd.h> 30 #include <unistd.h>
31 31
32 #include <mailutils/sys/mstream.h> 32 #include <mailutils/sys/mapstream.h>
33 #include <mailutils/error.h> 33 #include <mailutils/error.h>
34 34
35 #ifdef _POSIX_MAPPED_FILES 35 #ifdef _POSIX_MAPPED_FILES
...@@ -39,17 +39,17 @@ ...@@ -39,17 +39,17 @@
39 # define MAP_FAILED (void*)-1 39 # define MAP_FAILED (void*)-1
40 #endif 40 #endif
41 41
42 static int 42 int
43 _map_ref (stream_t stream) 43 _stream_mmap_ref (stream_t stream)
44 { 44 {
45 struct _ms *ms = (struct _ms *)stream; 45 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
46 return mu_refcount_inc (ms->refcount); 46 return mu_refcount_inc (ms->refcount);
47 } 47 }
48 48
49 static void 49 void
50 _map_destroy (stream_t *pstream) 50 _stream_mmap_destroy (stream_t *pstream)
51 { 51 {
52 struct _ms *ms = (struct _ms *)*pstream; 52 struct _stream_mmap *ms = (struct _stream_mmap *)*pstream;
53 53
54 if (mu_refcount_dec (ms->refcount) == 0) 54 if (mu_refcount_dec (ms->refcount) == 0)
55 { 55 {
...@@ -58,10 +58,10 @@ _map_destroy (stream_t *pstream) ...@@ -58,10 +58,10 @@ _map_destroy (stream_t *pstream)
58 } 58 }
59 } 59 }
60 60
61 static int 61 int
62 _map_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) 62 _stream_mmap_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
63 { 63 {
64 struct _ms *ms = (struct _ms *)stream; 64 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
65 size_t n = 0; 65 size_t n = 0;
66 66
67 mu_refcount_lock (ms->refcount); 67 mu_refcount_lock (ms->refcount);
...@@ -82,10 +82,10 @@ _map_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) ...@@ -82,10 +82,10 @@ _map_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
82 return 0; 82 return 0;
83 } 83 }
84 84
85 static int 85 int
86 _map_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) 86 _stream_mmap_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
87 { 87 {
88 struct _ms *ms = (struct _ms *)stream; 88 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
89 size_t n = 0; 89 size_t n = 0;
90 90
91 mu_refcount_lock (ms->refcount); 91 mu_refcount_lock (ms->refcount);
...@@ -111,10 +111,10 @@ _map_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) ...@@ -111,10 +111,10 @@ _map_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
111 return 0; 111 return 0;
112 } 112 }
113 113
114 static int 114 int
115 _map_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) 115 _stream_mmap_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes)
116 { 116 {
117 struct _ms *ms = (struct _ms *)stream; 117 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
118 int err = 0; 118 int err = 0;
119 size_t n = 0; 119 size_t n = 0;
120 120
...@@ -157,10 +157,10 @@ _map_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) ...@@ -157,10 +157,10 @@ _map_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes)
157 return err; 157 return err;
158 } 158 }
159 159
160 static int 160 int
161 _map_truncate (stream_t stream, off_t len) 161 _stream_mmap_truncate (stream_t stream, off_t len)
162 { 162 {
163 struct _ms *ms = (struct _ms *)stream; 163 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
164 int err = 0; 164 int err = 0;
165 165
166 mu_refcount_lock (ms->refcount); 166 mu_refcount_lock (ms->refcount);
...@@ -188,10 +188,10 @@ _map_truncate (stream_t stream, off_t len) ...@@ -188,10 +188,10 @@ _map_truncate (stream_t stream, off_t len)
188 return err; 188 return err;
189 } 189 }
190 190
191 static int 191 int
192 _map_get_size (stream_t stream, off_t *psize) 192 _stream_mmap_get_size (stream_t stream, off_t *psize)
193 { 193 {
194 struct _ms *ms = (struct _ms *)stream; 194 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
195 struct stat stbuf; 195 struct stat stbuf;
196 int err = 0; 196 int err = 0;
197 197
...@@ -232,11 +232,11 @@ _map_get_size (stream_t stream, off_t *psize) ...@@ -232,11 +232,11 @@ _map_get_size (stream_t stream, off_t *psize)
232 return err; 232 return err;
233 } 233 }
234 234
235 static int 235 int
236 _map_flush (stream_t stream) 236 _stream_mmap_flush (stream_t stream)
237 { 237 {
238 int err = 0; 238 int err = 0;
239 struct _ms *ms = (struct _ms *)stream; 239 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
240 mu_refcount_lock (ms->refcount); 240 mu_refcount_lock (ms->refcount);
241 if (ms->ptr != MAP_FAILED && ms->ptr != NULL) 241 if (ms->ptr != MAP_FAILED && ms->ptr != NULL)
242 err = msync (ms->ptr, ms->size, MS_SYNC); 242 err = msync (ms->ptr, ms->size, MS_SYNC);
...@@ -244,27 +244,27 @@ _map_flush (stream_t stream) ...@@ -244,27 +244,27 @@ _map_flush (stream_t stream)
244 return 0; 244 return 0;
245 } 245 }
246 246
247 static int 247 int
248 _map_get_fd (stream_t stream, int *pfd) 248 _stream_mmap_get_fd (stream_t stream, int *pfd)
249 { 249 {
250 struct _ms *ms = (struct _ms *)stream; 250 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
251 if (pfd) 251 if (pfd)
252 *pfd = ms->fd; 252 *pfd = ms->fd;
253 return 0; 253 return 0;
254 } 254 }
255 255
256 static int 256 int
257 _map_get_flags (stream_t stream, int *flags) 257 _stream_mmap_get_flags (stream_t stream, int *flags)
258 { 258 {
259 struct _ms *ms = (struct _ms *)stream; 259 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
260 if (flags == NULL) 260 if (flags == NULL)
261 return MU_ERROR_INVALID_PARAMETER; 261 return MU_ERROR_INVALID_PARAMETER;
262 *flags = ms->flags; 262 *flags = ms->flags;
263 return 0; 263 return 0;
264 } 264 }
265 265
266 static int 266 int
267 _map_get_state (stream_t stream, enum stream_state *state) 267 _stream_mmap_get_state (stream_t stream, enum stream_state *state)
268 { 268 {
269 (void)stream; 269 (void)stream;
270 if (state == NULL) 270 if (state == NULL)
...@@ -273,10 +273,10 @@ _map_get_state (stream_t stream, enum stream_state *state) ...@@ -273,10 +273,10 @@ _map_get_state (stream_t stream, enum stream_state *state)
273 return 0; 273 return 0;
274 } 274 }
275 275
276 static int 276 int
277 _map_seek (stream_t stream, off_t off, enum stream_whence whence) 277 _stream_mmap_seek (stream_t stream, off_t off, enum stream_whence whence)
278 { 278 {
279 struct _ms *ms = (struct _ms *)stream; 279 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
280 off_t noff = ms->offset; 280 off_t noff = ms->offset;
281 int err = 0; 281 int err = 0;
282 if (whence == MU_STREAM_WHENCE_SET) 282 if (whence == MU_STREAM_WHENCE_SET)
...@@ -290,7 +290,7 @@ _map_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -290,7 +290,7 @@ _map_seek (stream_t stream, off_t off, enum stream_whence whence)
290 if (noff >= 0) 290 if (noff >= 0)
291 { 291 {
292 if (noff > ms->offset) 292 if (noff > ms->offset)
293 _map_truncate (stream, noff); 293 _stream_mmap_truncate (stream, noff);
294 ms->offset = noff; 294 ms->offset = noff;
295 } 295 }
296 else 296 else
...@@ -298,20 +298,20 @@ _map_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -298,20 +298,20 @@ _map_seek (stream_t stream, off_t off, enum stream_whence whence)
298 return err; 298 return err;
299 } 299 }
300 300
301 static int 301 int
302 _map_tell (stream_t stream, off_t *off) 302 _stream_mmap_tell (stream_t stream, off_t *off)
303 { 303 {
304 struct _ms *ms = (struct _ms *)stream; 304 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
305 if (off == NULL) 305 if (off == NULL)
306 return MU_ERROR_INVALID_PARAMETER; 306 return MU_ERROR_INVALID_PARAMETER;
307 *off = ms->offset; 307 *off = ms->offset;
308 return 0; 308 return 0;
309 } 309 }
310 310
311 static int 311 int
312 _map_close (stream_t stream) 312 _stream_mmap_close (stream_t stream)
313 { 313 {
314 struct _ms *ms = (struct _ms *)stream; 314 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
315 int err = 0; 315 int err = 0;
316 mu_refcount_lock (ms->refcount); 316 mu_refcount_lock (ms->refcount);
317 if (ms->ptr != MAP_FAILED) 317 if (ms->ptr != MAP_FAILED)
...@@ -328,40 +328,39 @@ _map_close (stream_t stream) ...@@ -328,40 +328,39 @@ _map_close (stream_t stream)
328 return err; 328 return err;
329 } 329 }
330 330
331 static int 331 int
332 _map_is_open (stream_t stream) 332 _stream_mmap_is_open (stream_t stream)
333 { 333 {
334 struct _ms *ms = (struct _ms *)stream; 334 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
335 return (ms->ptr == MAP_FAILED) ? 0 : 1; 335 return (ms->ptr == MAP_FAILED) ? 0 : 1;
336 } 336 }
337 337
338 static int 338 int
339 _map_is_readready (stream_t stream, int timeout) 339 _stream_mmap_is_readready (stream_t stream, int timeout)
340 { 340 {
341 (void)timeout; 341 (void)timeout;
342 return _map_is_open (stream); 342 return _stream_mmap_is_open (stream);
343 } 343 }
344 344
345 static int 345 int
346 _map_is_writeready (stream_t stream, int timeout) 346 _stream_mmap_is_writeready (stream_t stream, int timeout)
347 { 347 {
348 (void)timeout; 348 (void)timeout;
349 return _map_is_open (stream); 349 return _stream_mmap_is_open (stream);
350 } 350 }
351 351
352 static int 352 int
353 _map_is_exceptionpending (stream_t stream, int timeout) 353 _stream_mmap_is_exceptionpending (stream_t stream, int timeout)
354 { 354 {
355 (void)stream; 355 (void)stream;
356 (void)timeout; 356 (void)timeout;
357 return 0; 357 return 0;
358 } 358 }
359 359
360 360 int
361 static int 361 _stream_mmap_open (stream_t stream, const char *filename, int port, int flags)
362 _map_open (stream_t stream, const char *filename, int port, int flags)
363 { 362 {
364 struct _ms *ms = (struct _ms *)stream; 363 struct _stream_mmap *ms = (struct _stream_mmap *)stream;
365 int mflag, flg; 364 int mflag, flg;
366 struct stat st; 365 struct stat st;
367 366
...@@ -400,7 +399,19 @@ _map_open (stream_t stream, const char *filename, int port, int flags) ...@@ -400,7 +399,19 @@ _map_open (stream_t stream, const char *filename, int port, int flags)
400 flg = O_RDONLY; 399 flg = O_RDONLY;
401 } 400 }
402 401
402 /* Anonymous Mapping. */
403 if (filename == NULL)
404 {
405 #ifdef MAP_ANON
406 mflag |= MAP_ANON;
407 #else
408 filename = "/dev/zero";
409 ms->fd = open (filename, flg);
410 #endif
411 }
412 else
403 ms->fd = open (filename, flg); 413 ms->fd = open (filename, flg);
414
404 if (ms->fd < 0) 415 if (ms->fd < 0)
405 return errno; 416 return errno;
406 if (fstat (ms->fd, &st) != 0) 417 if (fstat (ms->fd, &st) != 0)
...@@ -428,45 +439,70 @@ _map_open (stream_t stream, const char *filename, int port, int flags) ...@@ -428,45 +439,70 @@ _map_open (stream_t stream, const char *filename, int port, int flags)
428 return 0; 439 return 0;
429 } 440 }
430 441
431 static struct _stream_vtable _map_vtable = 442 static struct _stream_vtable _stream_mmap_vtable =
432 { 443 {
433 _map_ref, 444 _stream_mmap_ref,
434 _map_destroy, 445 _stream_mmap_destroy,
435 446
436 _map_open, 447 _stream_mmap_open,
437 _map_close, 448 _stream_mmap_close,
438 449
439 _map_read, 450 _stream_mmap_read,
440 _map_readline, 451 _stream_mmap_readline,
441 _map_write, 452 _stream_mmap_write,
442 453
443 _map_seek, 454 _stream_mmap_seek,
444 _map_tell, 455 _stream_mmap_tell,
445 456
446 _map_get_size, 457 _stream_mmap_get_size,
447 _map_truncate, 458 _stream_mmap_truncate,
448 _map_flush, 459 _stream_mmap_flush,
449 460
450 _map_get_fd, 461 _stream_mmap_get_fd,
451 _map_get_flags, 462 _stream_mmap_get_flags,
452 _map_get_state, 463 _stream_mmap_get_state,
453 464
454 _map_is_readready, 465 _stream_mmap_is_readready,
455 _map_is_writeready, 466 _stream_mmap_is_writeready,
456 _map_is_exceptionpending, 467 _stream_mmap_is_exceptionpending,
457 468
458 _map_is_open 469 _stream_mmap_is_open
459 }; 470 };
460 471
472 int
473 _stream_mmap_ctor (struct _stream_mmap *ms)
474 {
475 mu_refcount_create (&ms->refcount);
476 if (ms->refcount == NULL)
477 return MU_ERROR_NO_MEMORY;
478 ms->fd = -1;
479 ms->offset = -1;
480 ms->flags = 0;
481 ms->mflags = 0;
482 ms->base.vtable = &_stream_mmap_vtable;
483 return 0;
484 }
485
486 void
487 _stream_mmap_dtor (struct _stream_mmap *ms)
488 {
489 mu_refcount_destroy (&ms->refcount);
490 ms->fd = -1;
491 ms->offset = -1;
492 ms->mflags = 0;
493 ms->ptr = MAP_FAILED;
494 }
495
461 #endif /* _POSIX_MAPPED_FILES */ 496 #endif /* _POSIX_MAPPED_FILES */
462 497
463 int 498 int
464 stream_mapfile_create (stream_t *pstream) 499 stream_mmap_create (stream_t *pstream)
465 { 500 {
466 #ifndef _POSIX_MAPPED_FILES 501 #ifndef _POSIX_MAPPED_FILES
467 return ENOSYS; 502 return ENOSYS;
468 #else 503 #else
469 struct _ms *ms; 504 struct _stream_mmap *ms;
505 int status;
470 506
471 if (pstream == NULL) 507 if (pstream == NULL)
472 return MU_ERROR_INVALID_PARAMETER; 508 return MU_ERROR_INVALID_PARAMETER;
...@@ -475,19 +511,10 @@ stream_mapfile_create (stream_t *pstream) ...@@ -475,19 +511,10 @@ stream_mapfile_create (stream_t *pstream)
475 if (ms == NULL) 511 if (ms == NULL)
476 return MU_ERROR_NO_MEMORY; 512 return MU_ERROR_NO_MEMORY;
477 513
478 mu_refcount_create (&ms->refcount); 514 status = _stream_mmap_ctor (ms);
479 if (ms->refcount == NULL) 515 if (status != 0)
480 { 516 return status;
481 free (ms);
482 return MU_ERROR_NO_MEMORY;
483 }
484 ms->fd = -1;
485 ms->offset = -1;
486 ms->flags = 0;
487 ms->mflags = 0;
488 ms->base.vtable = &_map_vtable;
489 *pstream = &ms->base; 517 *pstream = &ms->base;
490
491 return 0; 518 return 0;
492 #endif /* _POSIX_MAPPED_FILES */ 519 #endif /* _POSIX_MAPPED_FILES */
493 } 520 }
......
...@@ -21,22 +21,25 @@ ...@@ -21,22 +21,25 @@
21 21
22 #include <errno.h> 22 #include <errno.h>
23 #include <stdlib.h> 23 #include <stdlib.h>
24 #include <stdio.h>
24 #include <string.h> 25 #include <string.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
25 28
26 #include <mailutils/error.h> 29 #include <mailutils/error.h>
27 #include <mailutils/sys/memstream.h> 30 #include <mailutils/sys/memstream.h>
28 31
29 static int 32 int
30 _memory_ref (stream_t stream) 33 _stream_memory_ref (stream_t stream)
31 { 34 {
32 struct _memory_stream *mem = (struct _memory_stream *)stream; 35 struct _stream_memory *mem = (struct _stream_memory *)stream;
33 return mu_refcount_inc (mem->refcount); 36 return mu_refcount_inc (mem->refcount);
34 } 37 }
35 38
36 static void 39 void
37 _memory_destroy (stream_t *pstream) 40 _stream_memory_destroy (stream_t *pstream)
38 { 41 {
39 struct _memory_stream *mem = (struct _memory_stream *)*pstream; 42 struct _stream_memory *mem = (struct _stream_memory *)*pstream;
40 if (mu_refcount_dec (mem->refcount) == 0) 43 if (mu_refcount_dec (mem->refcount) == 0)
41 { 44 {
42 mu_refcount_destroy (&mem->refcount); 45 mu_refcount_destroy (&mem->refcount);
...@@ -44,10 +47,10 @@ _memory_destroy (stream_t *pstream) ...@@ -44,10 +47,10 @@ _memory_destroy (stream_t *pstream)
44 } 47 }
45 } 48 }
46 49
47 static int 50 int
48 _memory_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) 51 _stream_memory_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
49 { 52 {
50 struct _memory_stream *mem = (struct _memory_stream *)stream; 53 struct _stream_memory *mem = (struct _stream_memory *)stream;
51 size_t n = 0; 54 size_t n = 0;
52 55
53 mu_refcount_lock (mem->refcount); 56 mu_refcount_lock (mem->refcount);
...@@ -64,10 +67,10 @@ _memory_read (stream_t stream, void *optr, size_t osize, size_t *nbytes) ...@@ -64,10 +67,10 @@ _memory_read (stream_t stream, void *optr, size_t osize, size_t *nbytes)
64 return 0; 67 return 0;
65 } 68 }
66 69
67 static int 70 int
68 _memory_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) 71 _stream_memory_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
69 { 72 {
70 struct _memory_stream *mem = (struct _memory_stream *)stream; 73 struct _stream_memory *mem = (struct _stream_memory *)stream;
71 char *nl; 74 char *nl;
72 size_t n = 0; 75 size_t n = 0;
73 mu_refcount_lock (mem->refcount); 76 mu_refcount_lock (mem->refcount);
...@@ -88,20 +91,25 @@ _memory_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes) ...@@ -88,20 +91,25 @@ _memory_readline (stream_t stream, char *optr, size_t osize, size_t *nbytes)
88 return 0; 91 return 0;
89 } 92 }
90 93
91 static int 94 int
92 _memory_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) 95 _stream_memory_write (stream_t stream, const void *iptr, size_t isize,
96 size_t *nbytes)
93 { 97 {
94 struct _memory_stream *mem = (struct _memory_stream *)stream; 98 struct _stream_memory *mem = (struct _stream_memory *)stream;
95 99
96 mu_refcount_lock (mem->refcount); 100 mu_refcount_lock (mem->refcount);
97 /* Bigger we have to realloc. */ 101 /* Bigger we have to realloc. */
98 if (mem->size < (mem->offset + isize)) 102 if (mem->capacity < (mem->offset + isize))
99 { 103 {
100 char *tmp = realloc (mem->ptr, mem->offset + isize); 104 /* Realloc by blocks of 512. */
105 int newsize = MU_STREAM_MEMORY_BLOCKSIZE *
106 (((mem->offset + isize)/MU_STREAM_MEMORY_BLOCKSIZE) + 1);
107 char *tmp = realloc (mem->ptr, newsize);
101 if (tmp == NULL) 108 if (tmp == NULL)
102 return ENOMEM; 109 return ENOMEM;
103 mem->ptr = tmp; 110 mem->ptr = tmp;
104 mem->size = mem->offset + isize; 111 mem->size = mem->offset + isize;
112 mem->capacity = newsize;
105 } 113 }
106 114
107 memcpy (mem->ptr + mem->offset, iptr, isize); 115 memcpy (mem->ptr + mem->offset, iptr, isize);
...@@ -112,10 +120,10 @@ _memory_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes) ...@@ -112,10 +120,10 @@ _memory_write (stream_t stream, const void *iptr, size_t isize, size_t *nbytes)
112 return 0; 120 return 0;
113 } 121 }
114 122
115 static int 123 int
116 _memory_truncate (stream_t stream, off_t len) 124 _stream_memory_truncate (stream_t stream, off_t len)
117 { 125 {
118 struct _memory_stream *mem = (struct _memory_stream *)stream; 126 struct _stream_memory *mem = (struct _stream_memory *)stream;
119 127
120 mu_refcount_lock (mem->refcount); 128 mu_refcount_lock (mem->refcount);
121 if (len == 0) 129 if (len == 0)
...@@ -130,16 +138,17 @@ _memory_truncate (stream_t stream, off_t len) ...@@ -130,16 +138,17 @@ _memory_truncate (stream_t stream, off_t len)
130 return ENOMEM; 138 return ENOMEM;
131 mem->ptr = tmp; 139 mem->ptr = tmp;
132 } 140 }
141 mem->capacity = len;
133 mem->size = len; 142 mem->size = len;
134 mem->offset = len; 143 mem->offset = len;
135 mu_refcount_unlock (mem->refcount); 144 mu_refcount_unlock (mem->refcount);
136 return 0; 145 return 0;
137 } 146 }
138 147
139 static int 148 int
140 _memory_get_size (stream_t stream, off_t *psize) 149 _stream_memory_get_size (stream_t stream, off_t *psize)
141 { 150 {
142 struct _memory_stream *mem = (struct _memory_stream *)stream; 151 struct _stream_memory *mem = (struct _stream_memory *)stream;
143 mu_refcount_lock (mem->refcount); 152 mu_refcount_lock (mem->refcount);
144 if (psize) 153 if (psize)
145 *psize = mem->size; 154 *psize = mem->size;
...@@ -147,46 +156,47 @@ _memory_get_size (stream_t stream, off_t *psize) ...@@ -147,46 +156,47 @@ _memory_get_size (stream_t stream, off_t *psize)
147 return 0; 156 return 0;
148 } 157 }
149 158
150 static int 159 int
151 _memory_close (stream_t stream) 160 _stream_memory_close (stream_t stream)
152 { 161 {
153 struct _memory_stream *mem = (struct _memory_stream *)stream; 162 struct _stream_memory *mem = (struct _stream_memory *)stream;
154 mu_refcount_lock (mem->refcount); 163 mu_refcount_lock (mem->refcount);
155 if (mem->ptr) 164 if (mem->ptr)
156 free (mem->ptr); 165 free (mem->ptr);
157 mem->ptr = NULL; 166 mem->ptr = NULL;
167 mem->capacity = 0;
158 mem->size = 0; 168 mem->size = 0;
159 mem->offset = 0; 169 mem->offset = 0;
160 mu_refcount_unlock (mem->refcount); 170 mu_refcount_unlock (mem->refcount);
161 return 0; 171 return 0;
162 } 172 }
163 173
164 static int 174 int
165 _memory_flush (stream_t stream) 175 _stream_memory_flush (stream_t stream)
166 { 176 {
167 (void)stream; 177 (void)stream;
168 return 0; 178 return 0;
169 } 179 }
170 180
171 static int 181 int
172 _memory_get_fd (stream_t stream, int *pfd) 182 _stream_memory_get_fd (stream_t stream, int *pfd)
173 { 183 {
174 (void)stream; (void)pfd; 184 (void)stream; (void)pfd;
175 return MU_ERROR_NOT_SUPPORTED; 185 return MU_ERROR_NOT_SUPPORTED;
176 } 186 }
177 187
178 static int 188 int
179 _memory_get_flags (stream_t stream, int *flags) 189 _stream_memory_get_flags (stream_t stream, int *flags)
180 { 190 {
181 struct _memory_stream *mem = (struct _memory_stream *)stream; 191 struct _stream_memory *mem = (struct _stream_memory *)stream;
182 if (flags == NULL) 192 if (flags == NULL)
183 return MU_ERROR_INVALID_PARAMETER; 193 return MU_ERROR_INVALID_PARAMETER;
184 *flags = mem->flags; 194 *flags = mem->flags;
185 return 0; 195 return 0;
186 } 196 }
187 197
188 static int 198 int
189 _memory_get_state (stream_t stream, enum stream_state *state) 199 _stream_memory_get_state (stream_t stream, enum stream_state *state)
190 { 200 {
191 (void)stream; 201 (void)stream;
192 if (state == NULL) 202 if (state == NULL)
...@@ -195,10 +205,10 @@ _memory_get_state (stream_t stream, enum stream_state *state) ...@@ -195,10 +205,10 @@ _memory_get_state (stream_t stream, enum stream_state *state)
195 return 0; 205 return 0;
196 } 206 }
197 207
198 static int 208 int
199 _memory_seek (stream_t stream, off_t off, enum stream_whence whence) 209 _stream_memory_seek (stream_t stream, off_t off, enum stream_whence whence)
200 { 210 {
201 struct _memory_stream *mem = (struct _memory_stream *)stream; 211 struct _stream_memory *mem = (struct _stream_memory *)stream;
202 off_t noff = mem->offset; 212 off_t noff = mem->offset;
203 int err = 0; 213 int err = 0;
204 if (whence == MU_STREAM_WHENCE_SET) 214 if (whence == MU_STREAM_WHENCE_SET)
...@@ -212,7 +222,7 @@ _memory_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -212,7 +222,7 @@ _memory_seek (stream_t stream, off_t off, enum stream_whence whence)
212 if (noff >= 0) 222 if (noff >= 0)
213 { 223 {
214 if (noff > mem->offset) 224 if (noff > mem->offset)
215 _memory_truncate (stream, noff); 225 _stream_memory_truncate (stream, noff);
216 mem->offset = noff; 226 mem->offset = noff;
217 } 227 }
218 else 228 else
...@@ -220,41 +230,41 @@ _memory_seek (stream_t stream, off_t off, enum stream_whence whence) ...@@ -220,41 +230,41 @@ _memory_seek (stream_t stream, off_t off, enum stream_whence whence)
220 return err; 230 return err;
221 } 231 }
222 232
223 static int 233 int
224 _memory_tell (stream_t stream, off_t *off) 234 _stream_memory_tell (stream_t stream, off_t *off)
225 { 235 {
226 struct _memory_stream *mem = (struct _memory_stream *)stream; 236 struct _stream_memory *mem = (struct _stream_memory *)stream;
227 if (off == NULL) 237 if (off == NULL)
228 return MU_ERROR_INVALID_PARAMETER; 238 return MU_ERROR_INVALID_PARAMETER;
229 *off = mem->offset; 239 *off = mem->offset;
230 return 0; 240 return 0;
231 } 241 }
232 242
233 static int 243 int
234 _memory_is_open (stream_t stream) 244 _stream_memory_is_open (stream_t stream)
235 { 245 {
236 (void)stream; 246 (void)stream;
237 return 1; 247 return 1;
238 } 248 }
239 249
240 static int 250 int
241 _memory_is_readready (stream_t stream, int timeout) 251 _stream_memory_is_readready (stream_t stream, int timeout)
242 { 252 {
243 (void)stream; 253 (void)stream;
244 (void)timeout; 254 (void)timeout;
245 return 1; 255 return 1;
246 } 256 }
247 257
248 static int 258 int
249 _memory_is_writeready (stream_t stream, int timeout) 259 _stream_memory_is_writeready (stream_t stream, int timeout)
250 { 260 {
251 (void)stream; 261 (void)stream;
252 (void)timeout; 262 (void)timeout;
253 return 1; 263 return 1;
254 } 264 }
255 265
256 static int 266 int
257 _memory_is_exceptionpending (stream_t stream, int timeout) 267 _stream_memory_is_exceptionpending (stream_t stream, int timeout)
258 { 268 {
259 (void)stream; 269 (void)stream;
260 (void)timeout; 270 (void)timeout;
...@@ -262,61 +272,134 @@ _memory_is_exceptionpending (stream_t stream, int timeout) ...@@ -262,61 +272,134 @@ _memory_is_exceptionpending (stream_t stream, int timeout)
262 } 272 }
263 273
264 274
265 static int 275 int
266 _memory_open (stream_t stream, const char *filename, int port, int flags) 276 _stream_memory_open (stream_t stream, const char *filename, int port,
277 int flags)
267 { 278 {
268 struct _memory_stream *mem = (struct _memory_stream *)stream; 279 struct _stream_memory *mem = (struct _stream_memory *)stream;
280 int status = 0;
269 281
270 (void)port; /* Ignored. */ 282 (void)port; /* Ignored. */
271 (void)filename; /* Ignored. */
272 (void)flags; /* Ignored. */
273 283
274 mu_refcount_lock (mem->refcount); 284 mu_refcount_lock (mem->refcount);
275 /* Close any previous file. */ 285 /* Free any previous memory. */
276 if (mem->ptr) 286 if (mem->ptr)
277 free (mem->ptr); 287 free (mem->ptr);
278 mem->ptr = NULL; 288 mem->ptr = NULL;
289 mem->capacity = 0;
279 mem->size = 0; 290 mem->size = 0;
280 mem->offset = 0; 291 mem->offset = 0;
281 mem->flags = flags; 292 mem->flags = flags;
293 if (filename)
294 {
295 struct stat statbuf;
296 if (stat (filename, &statbuf) == 0)
297 {
298 mem->ptr = calloc (1, statbuf.st_size);
299 if (mem->ptr)
300 {
301 FILE *fp;
302 mem->capacity = statbuf.st_size;
303 mem->size = statbuf.st_size;
304 fp = fopen (filename, "r");
305 if (fp)
306 {
307 size_t r = fread (mem->ptr, mem->size, 1, fp);
308 if (r != mem->size)
309 status = MU_ERROR_IO;
310 fclose (fp);
311 }
312 else
313 status = errno;
314 if (status != 0)
315 {
316 free (mem->ptr);
317 mem->ptr = NULL;
318 mem->capacity = 0;
319 mem->size = 0;
320 }
321 }
322 else
323 status = MU_ERROR_NO_MEMORY;
324 }
325 else
326 status = MU_ERROR_IO;
327 }
282 mu_refcount_unlock (mem->refcount); 328 mu_refcount_unlock (mem->refcount);
283 return 0; 329 return status;
284 } 330 }
285 331
286 static struct _stream_vtable _mem_vtable = 332 static struct _stream_vtable _stream_memory_vtable =
287 { 333 {
288 _memory_ref, 334 _stream_memory_ref,
289 _memory_destroy, 335 _stream_memory_destroy,
290 336
291 _memory_open, 337 _stream_memory_open,
292 _memory_close, 338 _stream_memory_close,
293 339
294 _memory_read, 340 _stream_memory_read,
295 _memory_readline, 341 _stream_memory_readline,
296 _memory_write, 342 _stream_memory_write,
297 343
298 _memory_seek, 344 _stream_memory_seek,
299 _memory_tell, 345 _stream_memory_tell,
300 346
301 _memory_get_size, 347 _stream_memory_get_size,
302 _memory_truncate, 348 _stream_memory_truncate,
303 _memory_flush, 349 _stream_memory_flush,
304 350
305 _memory_get_fd, 351 _stream_memory_get_fd,
306 _memory_get_flags, 352 _stream_memory_get_flags,
307 _memory_get_state, 353 _stream_memory_get_state,
308 354
309 _memory_is_readready, 355 _stream_memory_is_readready,
310 _memory_is_writeready, 356 _stream_memory_is_writeready,
311 _memory_is_exceptionpending, 357 _stream_memory_is_exceptionpending,
312 358
313 _memory_is_open 359 _stream_memory_is_open
314 }; 360 };
315 361
316 int 362 int
317 stream_memory_create (stream_t *pstream) 363 _stream_memory_ctor (struct _stream_memory *mem, size_t capacity)
364 {
365 mu_refcount_create (&mem->refcount);
366 if (mem->refcount == NULL)
367 return MU_ERROR_NO_MEMORY;
368 if (capacity)
369 {
370 mem->ptr = calloc (1, capacity);
371 if (mem->ptr == NULL)
372 {
373 mu_refcount_destroy (&mem->refcount);
374 return MU_ERROR_NO_MEMORY;
375 }
376 mem->capacity = capacity;
377 }
378 else
379 mem->capacity = 0;
380 mem->size = 0;
381 mem->offset = 0;
382 mem->flags = 0;
383 mem->base.vtable = &_stream_memory_vtable;
384 return 0;
385 }
386
387 void
388 _stream_memory_dtor (struct _stream_memory *mem)
389 {
390 mu_refcount_destroy (&mem->refcount);
391 mem->ptr = NULL;
392 mem->capacity = 0;
393 mem->size = 0;
394 mem->offset = 0;
395 mem->flags = 0;
396 }
397
398 int
399 stream_memory_create (stream_t *pstream, size_t capacity)
318 { 400 {
319 struct _memory_stream *mem; 401 struct _stream_memory *mem;
402 int status;
320 403
321 if (pstream == NULL) 404 if (pstream == NULL)
322 return MU_ERROR_INVALID_PARAMETER; 405 return MU_ERROR_INVALID_PARAMETER;
...@@ -325,17 +408,13 @@ stream_memory_create (stream_t *pstream) ...@@ -325,17 +408,13 @@ stream_memory_create (stream_t *pstream)
325 if (mem == NULL) 408 if (mem == NULL)
326 return MU_ERROR_NO_MEMORY; 409 return MU_ERROR_NO_MEMORY;
327 410
328 mu_refcount_create (&mem->refcount); 411 status = _stream_memory_ctor (mem, capacity);
329 if (mem->refcount == NULL) 412 if (status != 0)
330 { 413 {
331 free (mem); 414 free (mem);
332 return MU_ERROR_NO_MEMORY; 415 return status;
333 } 416 }
334 mem->ptr = NULL; 417 mem->base.vtable = &_stream_memory_vtable;
335 mem->size = 0;
336 mem->offset = 0;
337 mem->flags = 0;
338 mem->base.vtable = &_mem_vtable;
339 *pstream = &mem->base; 418 *pstream = &mem->base;
340 return 0; 419 return 0;
341 } 420 }
......
...@@ -27,14 +27,7 @@ ...@@ -27,14 +27,7 @@
27 27
28 #include <mailutils/error.h> 28 #include <mailutils/error.h>
29 #include <mailutils/refcount.h> 29 #include <mailutils/refcount.h>
30 #include <mailutils/sys/ticket.h> 30 #include <mailutils/sys/pticket.h>
31
32 struct _prompt_ticket
33 {
34 struct _ticket base;
35 int ref;
36 mu_refcount_t refcount;
37 };
38 31
39 static void 32 static void
40 echo_off(struct termios *stored_settings) 33 echo_off(struct termios *stored_settings)
...@@ -52,17 +45,17 @@ echo_on(struct termios *stored_settings) ...@@ -52,17 +45,17 @@ echo_on(struct termios *stored_settings)
52 tcsetattr (0, TCSANOW, stored_settings); 45 tcsetattr (0, TCSANOW, stored_settings);
53 } 46 }
54 47
55 static int 48 int
56 _prompt_ref (ticket_t ticket) 49 _ticket_prompt_ref (ticket_t ticket)
57 { 50 {
58 struct _prompt_ticket *prompt = (struct _prompt_ticket *)ticket; 51 struct _ticket_prompt *prompt = (struct _ticket_prompt *)ticket;
59 return mu_refcount_inc (prompt->refcount); 52 return mu_refcount_inc (prompt->refcount);
60 } 53 }
61 54
62 static void 55 void
63 _prompt_destroy (ticket_t *pticket) 56 _ticket_prompt_destroy (ticket_t *pticket)
64 { 57 {
65 struct _prompt_ticket *prompt = (struct _prompt_ticket *)*pticket; 58 struct _ticket_prompt *prompt = (struct _ticket_prompt *)*pticket;
66 if (mu_refcount_dec (prompt->refcount) == 0) 59 if (mu_refcount_dec (prompt->refcount) == 0)
67 { 60 {
68 mu_refcount_destroy (&prompt->refcount); 61 mu_refcount_destroy (&prompt->refcount);
...@@ -70,8 +63,8 @@ _prompt_destroy (ticket_t *pticket) ...@@ -70,8 +63,8 @@ _prompt_destroy (ticket_t *pticket)
70 } 63 }
71 } 64 }
72 65
73 static int 66 int
74 _prompt_pop (ticket_t ticket, const char *challenge, char **parg) 67 _ticket_prompt_pop (ticket_t ticket, const char *challenge, char **parg)
75 { 68 {
76 char arg[256]; 69 char arg[256];
77 struct termios stored_settings; 70 struct termios stored_settings;
...@@ -98,18 +91,35 @@ _prompt_pop (ticket_t ticket, const char *challenge, char **parg) ...@@ -98,18 +91,35 @@ _prompt_pop (ticket_t ticket, const char *challenge, char **parg)
98 return 0; 91 return 0;
99 } 92 }
100 93
101 static struct _ticket_vtable _prompt_vtable = 94 static struct _ticket_vtable _ticket_prompt_vtable =
102 { 95 {
103 _prompt_ref, 96 _ticket_prompt_ref,
104 _prompt_destroy, 97 _ticket_prompt_destroy,
105 98
106 _prompt_pop, 99 _ticket_prompt_pop,
107 }; 100 };
108 101
109 int 102 int
103 _ticket_prompt_ctor (struct _ticket_prompt *prompt)
104 {
105 mu_refcount_create (&prompt->refcount);
106 if (prompt->refcount == NULL)
107 return MU_ERROR_NO_MEMORY;
108 prompt->base.vtable = &_ticket_prompt_vtable;
109 return 0;
110 }
111
112 void
113 _ticket_prompt_dtor (struct _ticket_prompt *prompt)
114 {
115 mu_refcount_destroy (&prompt->refcount);
116 }
117
118 int
110 ticket_prompt_create (ticket_t *pticket) 119 ticket_prompt_create (ticket_t *pticket)
111 { 120 {
112 struct _prompt_ticket *prompt; 121 struct _ticket_prompt *prompt;
122 int status;
113 123
114 if (pticket == NULL) 124 if (pticket == NULL)
115 return MU_ERROR_INVALID_PARAMETER; 125 return MU_ERROR_INVALID_PARAMETER;
...@@ -118,14 +128,12 @@ ticket_prompt_create (ticket_t *pticket) ...@@ -118,14 +128,12 @@ ticket_prompt_create (ticket_t *pticket)
118 if (prompt == NULL) 128 if (prompt == NULL)
119 return MU_ERROR_NO_MEMORY; 129 return MU_ERROR_NO_MEMORY;
120 130
121 mu_refcount_create (&prompt->refcount); 131 status = _ticket_prompt_ctor (prompt);
122 if (prompt->refcount == NULL) 132 if (status != 0)
123 { 133 {
124 free (prompt); 134 free (prompt);
125 return MU_ERROR_NO_MEMORY; 135 return status;
126 } 136 }
127 *pticket = &prompt->base; 137 *pticket = &prompt->base;
128 prompt->base.vtable = &_prompt_vtable;
129 return 0; 138 return 0;
130 } 139 }
131
......
...@@ -27,19 +27,19 @@ ...@@ -27,19 +27,19 @@
27 #include <mailutils/sys/sdebug.h> 27 #include <mailutils/sys/sdebug.h>
28 #include <mailutils/error.h> 28 #include <mailutils/error.h>
29 29
30 static int 30 int
31 _sdebug_ref (mu_debug_t debug) 31 _mu_debug_stream_ref (mu_debug_t debug)
32 { 32 {
33 struct _sdebug *sdebug = (struct _sdebug *)debug; 33 struct _mu_debug_stream *sdebug = (struct _mu_debug_stream *)debug;
34 return mu_refcount_inc (sdebug->refcount); 34 return mu_refcount_inc (sdebug->refcount);
35 } 35 }
36 36
37 static void 37 void
38 _sdebug_destroy (mu_debug_t *pdebug) 38 _mu_debug_stream_destroy (mu_debug_t *pdebug)
39 { 39 {
40 if (pdebug && *pdebug) 40 if (pdebug && *pdebug)
41 { 41 {
42 struct _sdebug *sdebug = (struct _sdebug *)*pdebug; 42 struct _mu_debug_stream *sdebug = (struct _mu_debug_stream *)*pdebug;
43 if (mu_refcount_dec (sdebug->refcount) == 0) 43 if (mu_refcount_dec (sdebug->refcount) == 0)
44 { 44 {
45 if (sdebug->stream) 45 if (sdebug->stream)
...@@ -55,27 +55,27 @@ _sdebug_destroy (mu_debug_t *pdebug) ...@@ -55,27 +55,27 @@ _sdebug_destroy (mu_debug_t *pdebug)
55 } 55 }
56 } 56 }
57 57
58 static int 58 int
59 _sdebug_set_level (mu_debug_t debug, size_t level) 59 _mu_debug_stream_set_level (mu_debug_t debug, size_t level)
60 { 60 {
61 struct _sdebug *sdebug = (struct _sdebug *)debug; 61 struct _mu_debug_stream *sdebug = (struct _mu_debug_stream *)debug;
62 sdebug->level = level; 62 sdebug->level = level;
63 return 0; 63 return 0;
64 } 64 }
65 65
66 static int 66 int
67 _sdebug_get_level (mu_debug_t debug, size_t *plevel) 67 _mu_debug_stream_get_level (mu_debug_t debug, size_t *plevel)
68 { 68 {
69 struct _sdebug *sdebug = (struct _sdebug *)debug; 69 struct _mu_debug_stream *sdebug = (struct _mu_debug_stream *)debug;
70 if (plevel) 70 if (plevel)
71 *plevel = sdebug->level; 71 *plevel = sdebug->level;
72 return 0; 72 return 0;
73 } 73 }
74 74
75 static int 75 int
76 _sdebug_print (mu_debug_t debug, size_t level, const char *mesg) 76 _mu_debug_stream_print (mu_debug_t debug, size_t level, const char *mesg)
77 { 77 {
78 struct _sdebug *sdebug = (struct _sdebug *)debug; 78 struct _mu_debug_stream *sdebug = (struct _mu_debug_stream *)debug;
79 79
80 if (mesg == NULL) 80 if (mesg == NULL)
81 return MU_ERROR_INVALID_PARAMETER; 81 return MU_ERROR_INVALID_PARAMETER;
...@@ -86,21 +86,21 @@ _sdebug_print (mu_debug_t debug, size_t level, const char *mesg) ...@@ -86,21 +86,21 @@ _sdebug_print (mu_debug_t debug, size_t level, const char *mesg)
86 return stream_write (sdebug->stream, mesg, strlen (mesg), NULL); 86 return stream_write (sdebug->stream, mesg, strlen (mesg), NULL);
87 } 87 }
88 88
89 struct _mu_debug_vtable _sdebug_vtable = 89 static struct _mu_debug_vtable _mu_debug_stream_vtable =
90 { 90 {
91 _sdebug_ref, 91 _mu_debug_stream_ref,
92 _sdebug_destroy, 92 _mu_debug_stream_destroy,
93 93
94 _sdebug_get_level, 94 _mu_debug_stream_get_level,
95 _sdebug_set_level, 95 _mu_debug_stream_set_level,
96 _sdebug_print 96 _mu_debug_stream_print
97 }; 97 };
98 98
99 int 99 int
100 mu_debug_stream_create (mu_debug_t *pdebug, stream_t stream, 100 mu_debug_stream_create (mu_debug_t *pdebug, stream_t stream,
101 int close_on_destroy) 101 int close_on_destroy)
102 { 102 {
103 struct _sdebug *sdebug; 103 struct _mu_debug_stream *sdebug;
104 104
105 if (pdebug == NULL || stream == NULL) 105 if (pdebug == NULL || stream == NULL)
106 return MU_ERROR_INVALID_PARAMETER; 106 return MU_ERROR_INVALID_PARAMETER;
...@@ -119,7 +119,7 @@ mu_debug_stream_create (mu_debug_t *pdebug, stream_t stream, ...@@ -119,7 +119,7 @@ mu_debug_stream_create (mu_debug_t *pdebug, stream_t stream,
119 sdebug->level = 0; 119 sdebug->level = 0;
120 sdebug->stream = stream; 120 sdebug->stream = stream;
121 sdebug->close_on_destroy = close_on_destroy; 121 sdebug->close_on_destroy = close_on_destroy;
122 sdebug->base.vtable = &_sdebug_vtable; 122 sdebug->base.vtable = &_mu_debug_stream_vtable;
123 *pdebug = &sdebug->base; 123 *pdebug = &sdebug->base;
124 return 0; 124 return 0;
125 } 125 }
......