Blame view

mailbox/stream.c 14.6 KB
Alain Magloire authored
1
/* GNU mailutils - a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
Alain Magloire authored
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

18 19 20 21 22 23 24 25 26 27
/* Credits.  Some of the Readline an buffering scheme was taken
   from 4.4BSDLite2.

   Copyright (c) 1990, 1993
   The Regents of the University of California.  All rights reserved.

   This code is derived from software contributed to Berkeley by
   Chris Torek.
 */

28 29 30 31
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

Alain Magloire authored
32 33 34
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
35
#include <string.h>
Alain Magloire authored
36

37 38
#include <stream0.h>

39 40
static int refill (stream_t, off_t);

41 42 43 44 45 46 47
/* A stream provides a way for an object to do I/O. It overloads
   stream read/write functions. Only a minimal buffering is done
   and that if stream's bufsiz member is set. If the requested
   offset does not equal the one maintained internally the buffer
   is flushed and refilled. This buffering scheme is more convenient
   for networking streams (POP/IMAP).
   Writes are always unbuffered. */
Alain Magloire authored
48
int
49
stream_create (stream_t *pstream, int flags, void *owner)
Alain Magloire authored
50
{
51 52
  stream_t stream;
  if (pstream == NULL || owner == NULL)
Alain Magloire authored
53
    return EINVAL;
54 55
  stream = calloc (1, sizeof (*stream));
  if (stream == NULL)
Alain Magloire authored
56
    return ENOMEM;
57
  stream->owner = owner;
58
  stream->flags = flags;
59 60
  /* By default unbuffered, the buffering scheme is not for all models, it
     really makes sense for network streams, where there is no offset.  */
61
  /* stream->rbuffer.bufsiz = BUFSIZ; */
62
  *pstream = stream;
Alain Magloire authored
63 64 65
  return 0;
}

66 67 68
void
stream_destroy (stream_t *pstream, void *owner)
{
69
   if (pstream && *pstream)
70 71
    {
      stream_t stream = *pstream;
72 73 74 75
      if ((stream->flags & MU_STREAM_NO_CHECK) || stream->owner == owner)
	{
	  if (stream->_destroy)
	    stream->_destroy (stream);
76 77
	  if (stream->rbuffer.base)
	    free (stream->rbuffer.base);
78 79
	  free (stream);
	}
80 81 82 83
      *pstream = NULL;
    }
}

84 85 86 87 88 89
void *
stream_get_owner (stream_t stream)
{
  return (stream) ? stream->owner : NULL;
}

90
int
91 92 93 94
stream_open (stream_t stream, const char *name, int port, int flags)
{
  if (stream == NULL)
    return EINVAL;
95
  stream->state = MU_STREAM_STATE_OPEN;
96
  stream->flags |= flags;
97 98 99 100 101 102 103 104 105 106
  if (stream->_open)
    return stream->_open (stream, name, port, flags);
  return  0;
}

int
stream_close (stream_t stream)
{
  if (stream == NULL)
    return EINVAL;
107
  stream->state = MU_STREAM_STATE_CLOSE;
108 109 110 111 112 113 114
  /* Clear the buffer of any residue left.  */
  if (stream->rbuffer.base)
    {
      stream->rbuffer.ptr = stream->rbuffer.base;
      stream->rbuffer.count = 0;
      memset (stream->rbuffer.base, '\0', stream->rbuffer.bufsiz);
    }
115 116 117 118 119 120
  if (stream->_close)
    return stream->_close (stream);
  return  0;
}

int
121 122 123 124 125 126
stream_is_seekable (stream_t stream)
{
  return (stream) ? stream->flags & MU_STREAM_SEEKABLE : 0;
}

int
127
stream_setbufsiz (stream_t stream, size_t size)
128 129 130
{
  if (stream == NULL)
    return EINVAL;
131 132
  stream->rbuffer.bufsiz = size;
  return 0;
133 134
}

135
/* We have to be clear about the buffering scheme, it is not designed to be
136 137 138 139 140 141
   used as a full-fledged buffer mechanism.  It is a simple mechanism for
   networking. Lots of code between POP and IMAP can be shared this way.
   - First caveat; the code maintains its own offset (rbuffer.offset member)
   and if it does not match the requested one, the data is flushed
   and the underlying _read is called. It is up to the latter to return
   EISPIPE when appropriate.
142 143
   - Again, this is targeting networking stream to make readline()
   a little bit more efficient, instead of reading a char at a time.  */
144

145
int
146 147
stream_read (stream_t is, char *buf, size_t count,
	     off_t offset, size_t *pnread)
Alain Magloire authored
148
{
149 150
  int status = 0;
  if (is == NULL || is->_read == NULL)
Alain Magloire authored
151 152
    return EINVAL;

153
  is->state = MU_STREAM_STATE_READ;
Alain Magloire authored
154

155 156
  /* Sanity check; noop.  */
  if (count == 0)
157
    {
158 159
      if (pnread)
	*pnread = 0;
160 161 162
      return 0;
    }

163 164 165 166 167
  /* If rbuffer.bufsiz == 0.  It means they did not want the buffer
     mechanism.  Good for them.  */
  if (is->rbuffer.bufsiz == 0)
    status = is->_read (is, buf, count, offset, pnread);
  else
168
    {
169 170
      size_t residue = count;
      int r;
Alain Magloire authored
171

172
      /* If the amount requested is bigger than the buffer cache size,
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	 bypass it.  Do no waste time and let it through.  */
      if (count > is->rbuffer.bufsiz)
	{
	  r = 0;
	  /* Drain the buffer first.  */
	  if (is->rbuffer.count > 0 && offset == is->rbuffer.offset)
	    {
	      (void)memcpy(buf, is->rbuffer.ptr, is->rbuffer.count);
	      is->rbuffer.offset += is->rbuffer.count;
	      residue -= is->rbuffer.count;
	      buf += is->rbuffer.count;
	      offset += is->rbuffer.count;
	    }
	  is->rbuffer.count = 0;
	  status = is->_read (is, buf, residue, offset, &r);
	  is->rbuffer.offset += r;
	  residue -= r;
	  if (pnread)
	    *pnread = count - residue;
	  return status;
	}

195
      /* Fill the buffer, do not want to start empty hand.  */
196
      if (is->rbuffer.count <= 0 || offset != is->rbuffer.offset)
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	{
	  status = refill (is, offset);
	  if (status != 0)
	    return status;
	  /* Reached the end ??  */
	  if (is->rbuffer.count == 0)
	    {
	      if (pnread)
		*pnread = 0;
	      return status;
	    }
	}

      /* Drain the buffer, if we have less then requested.  */
      while (residue > (size_t)(r = is->rbuffer.count))
	{
	  (void)memcpy (buf, is->rbuffer.ptr, (size_t)r);
	  /* stream->rbuffer.count = 0 ... done in refill */
	  is->rbuffer.ptr += r;
	  is->rbuffer.offset += r;
	  buf += r;
	  residue -= r;
219
	  status = refill (is, is->rbuffer.offset);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	  if (status != 0)
	    {
	      /* We have something in the buffer return the error on the
		 next call .  */
	      if (count != residue)
		{
		  if (pnread)
		    *pnread = count - residue;
		  status = 0;
		}
	      return status;
	    }
	  /* Did we reach the end.  */
	  if (is->rbuffer.count == 0)
	    {
	      if (pnread)
		*pnread = count - residue;
	      return status;
	    }
	}
      (void)memcpy(buf, is->rbuffer.ptr, residue);
      is->rbuffer.count -= residue;
      is->rbuffer.ptr += residue;
      is->rbuffer.offset += residue;
      if (pnread)
	*pnread = count;
    }
  return status;
Alain Magloire authored
248 249
}

250 251 252 253
/*
 * Read at most n-1 characters.
 * Stop when a newline has been read, or the count runs out.
 */
Alain Magloire authored
254
int
255 256 257
stream_readline (stream_t is, char *buf, size_t count,
		 off_t offset, size_t *pnread)
{
258 259
  int status = 0;

260 261
  if (is == NULL)
    return EINVAL;
262 263 264

  is->state = MU_STREAM_STATE_READ;

265 266 267 268 269 270
  if (count == 0)
    {
      if (pnread)
	*pnread = 0;
      return 0;
    }
271

272 273
  /* Use the provided readline.  */
  if (is->rbuffer.bufsiz == 0 &&  is->_readline != NULL)
274 275
    status = is->_readline (is, buf, count, offset, pnread);
  else if (is->rbuffer.bufsiz == 0) /* No Buffering.  */
276
    {
277 278 279 280
      size_t n, nr = 0;
      char c;
      /* Grossly inefficient hopefully they override this */
      for (n = 1; n < count; n++)
281
	{
282
	  status = is->_read (is, &c, 1, offset, &nr);
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
	  if (status != 0) /* Error.  */
	    return status;
	  else if (nr == 1)
	    {
	      *buf++ = c;
	      offset++;
	      if (c == '\n') /* Newline is stored like fgets().  */
		break;
	    }
	  else if (nr == 0)
	    {
	      if (n == 1) /* EOF, no data read.  */
		n = 0;
	      break; /* EOF, some data was read.  */
	    }
298
	}
299 300 301 302 303 304 305 306 307 308
      *buf = '\0';
      if (pnread)
	*pnread = (n == count) ? n - 1: n;
    }
  else /* Buffered.  */
    {
      char *s = buf;
      char *p, *nl;
      size_t len;
      size_t total = 0;
309

310
      count--;  /* Leave space for the null.  */
311 312

      /* If out of range refill.  */
313 314
      /*      if ((offset < is->rbuffer.offset */
      /*	   || offset > (is->rbuffer.offset + is->rbuffer.count))) */
315
      if (offset != is->rbuffer.offset)
316 317 318 319 320 321 322 323 324 325 326 327
	{
	  status = refill (is, offset);
	  if (status != 0)
	    return status;
	  if (is->rbuffer.count == 0)
	    {
	      if (pnread)
		*pnread = 0;
	      return 0;
	    }
	}

328
      while (count != 0)
329
	{
330 331 332 333
	  /* If the buffer is empty refill it.  */
	  len = is->rbuffer.count;
	  if (len <= 0)
	    {
334
	      status = refill (is, is->rbuffer.offset);
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	      if (status != 0)
		{
		  if (s != buf)
		    break;
		}
	      len = is->rbuffer.count;
	      if (len == 0)
		break;
	    }
	  p = is->rbuffer.ptr;

	  /* Scan through at most n bytes of the current buffer,
	     looking for '\n'.  If found, copy up to and including
	     newline, and stop.  Otherwise, copy entire chunk
	     and loop.  */
	  if (len > count)
	    len = count;
	  nl = memchr ((void *)p, '\n', len);
	  if (nl != NULL)
	    {
	      len = ++nl - p;
	      is->rbuffer.count -= len;
	      is->rbuffer.ptr = nl;
358
	      is->rbuffer.offset += len;
359 360 361 362 363 364 365 366 367
	      (void)memcpy ((void *)s, (void *)p, len);
	      total += len;
	      s[len] = 0;
	      if (pnread)
		*pnread = total;
	      return 0;
	    }
	  is->rbuffer.count -= len;
	  is->rbuffer.ptr += len;
368
	  is->rbuffer.offset += len;
369 370 371 372 373 374 375 376
	  (void)memcpy((void *)s, (void *)p, len);
	  total += len;
	  s += len;
	  count -= len;
        }
      *s = 0;
      if (pnread)
	*pnread = s - buf;
377
    }
378
  return status;
379 380 381
}

int
382 383
stream_write (stream_t os, const char *buf, size_t count,
	      off_t offset, size_t *pnwrite)
Alain Magloire authored
384
{
385 386 387 388 389
  int nleft;
  int err = 0;
  size_t nwriten = 0;
  size_t total = 0;

Alain Magloire authored
390
  if (os == NULL || os->_write == NULL)
391
      return EINVAL;
392
  os->state = MU_STREAM_STATE_WRITE;
393 394 395 396 397 398

  nleft = count;
  /* First try to send it all.  */
  while (nleft > 0)
    {
      err = os->_write (os, buf, nleft, offset, &nwriten);
399
      if (err != 0 || nwriten == 0)
400 401 402 403 404 405 406
        break;
      nleft -= nwriten;
      total += nwriten;
      buf += nwriten;
    }
  if (pnwrite)
    *pnwrite = total;
407
  return err;
Alain Magloire authored
408
}
409 410 411 412 413 414 415 416

int
stream_get_fd (stream_t stream, int *pfd)
{
  if (stream == NULL || stream->_get_fd == NULL)
    return EINVAL;
  return stream->_get_fd (stream, pfd);
}
417 418 419 420 421 422 423 424 425

int
stream_get_flags (stream_t stream, int *pfl)
{
  if (stream == NULL && pfl == NULL )
    return EINVAL;
  *pfl = stream->flags;
  return 0;
}
426 427

int
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
stream_set_property (stream_t stream, property_t property, void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (stream->owner != owner)
    return EACCES;
  if (stream->property)
    property_destroy (&(stream->property), stream);
  stream->property = property;
  return 0;
}

int
stream_get_property (stream_t stream, property_t *pp)
{
  if (stream == NULL)
    return EINVAL;
  if (stream->property == NULL)
    {
      int status = property_create (&(stream->property), stream);
      if (status != 0)
	return status;
    }
  *pp = stream->property;
  return 0;
}

int
456
stream_size (stream_t stream, off_t *psize)
457
{
458
  if (stream == NULL || stream->_size == NULL)
459
    return EINVAL;
460
  return stream->_size (stream, psize);
461
}
462 463

int
464
stream_truncate (stream_t stream, off_t len)
465
{
466
  if (stream == NULL || stream->_truncate == NULL )
467
    return EINVAL;
468 469

  return stream->_truncate (stream, len);
470 471
}

472

473
int
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
stream_flush (stream_t stream)
{
  if (stream == NULL || stream->_flush == NULL)
    return EINVAL;
  return stream->_flush (stream);
}


int
stream_get_state (stream_t stream, int *pstate)
{
  if (stream == NULL || pstate == NULL)
    return EINVAL;
  *pstate = stream->state;
  return 0;
}

int
stream_set_destroy (stream_t stream, void (*_destroy) (stream_t),  void *owner)
493 494 495
{
  if (stream == NULL)
    return EINVAL;
496

497 498
  if (stream->owner != owner)
    return EACCES;
499 500

  stream->_destroy = _destroy;
501 502 503
  return 0;
}

504 505 506 507 508 509 510 511 512 513 514 515 516
int
stream_set_open (stream_t stream,
	         int (*_open) (stream_t, const char *, int, int), void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_open = _open;
      return 0;
    }
  return EACCES;
}
517 518

int
519
stream_set_close (stream_t stream, int (*_close) (stream_t), void *owner)
520
{
521
  if (stream == NULL)
522
    return EINVAL;
523 524 525 526 527 528 529
  if (owner == stream->owner)
    {
      stream->_close = _close;
      return 0;
    }
  return EACCES;
}
530

531 532 533 534 535 536 537 538 539 540 541
int
stream_set_fd (stream_t stream, int (*_get_fd) (stream_t, int *), void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_get_fd = _get_fd;
      return 0;
    }
  return EACCES;
542 543 544
}

int
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
stream_set_read (stream_t stream, int (*_read)
		 (stream_t, char *, size_t, off_t, size_t *),
		 void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_read = _read;
      return 0;
    }
  return EACCES;
}

int
stream_set_readline (stream_t stream, int (*_readline)
		 (stream_t, char *, size_t, off_t, size_t *),
		 void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (owner == stream->owner)
    {
      stream->_readline = _readline;
      return 0;
    }
  return EACCES;
}

int
stream_set_write (stream_t stream, int (*_write)
		  __P ((stream_t, const char *, size_t, off_t, size_t *)),
		  void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (stream->owner == owner)
    {
      stream->_write = _write;
      return 0;
    }
  return EACCES;
}


int
stream_set_size (stream_t stream, int (*_size)(stream_t, off_t *), void *owner)
592 593 594 595 596
{
  if (stream == NULL)
    return EINVAL;
  if (stream->owner != owner)
    return EACCES;
597
  stream->_size = _size;
598 599 600 601
  return 0;
}

int
602 603
stream_set_truncate (stream_t stream, int (*_truncate) (stream_t, off_t),
		     void *owner)
604
{
605
  if (stream == NULL)
606
    return EINVAL;
607 608 609 610
  if (stream->owner != owner)
    return EACCES;
  stream->_truncate = _truncate;
  return 0;
611 612 613 614 615 616 617 618 619 620 621 622
}

int
stream_set_flush (stream_t stream, int (*_flush) (stream_t), void *owner)
{
  if (stream == NULL)
    return EINVAL;
  if (stream->owner != owner)
    return EACCES;
  stream->_flush = _flush;
  return 0;
}
623 624

int
625
stream_set_flags (stream_t stream, int fl)
626
{
627
  if (stream == NULL)
628
    return EINVAL;
629
  stream->flags |= fl;
630 631
  return 0;
}
632 633 634 635 636 637

static int
refill (stream_t stream, off_t offset)
{
  if (stream->_read)
    {
638
      int status;
639 640 641 642 643 644 645 646 647
      if (stream->rbuffer.base == NULL)
	{
	  stream->rbuffer.base = calloc (1, stream->rbuffer.bufsiz);
	  if (stream->rbuffer.base == NULL)
	    return ENOMEM;
	}
      stream->rbuffer.ptr = stream->rbuffer.base;
      stream->rbuffer.offset = offset;
      stream->rbuffer.count = 0;
648 649 650 651
      status = stream->_read (stream, stream->rbuffer.ptr,
			      stream->rbuffer.bufsiz, offset,
			      (size_t *)&(stream->rbuffer.count));
      return status;
652
    }
653
  return ENOSYS;
654
}