Merge from vendor branch BZIP:
[dragonfly.git] / contrib / cvs-1.12 / src / buffer.c
1 /*
2  * Copyright (C) 1996-2005 The 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 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 General Public License for more details.
13  */
14
15 /* Code for the buffer data structure.  */
16
17 #include "cvs.h"
18 #include "buffer.h"
19 #include "pagealign_alloc.h"
20
21 #if defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT)
22
23 # include <sys/socket.h>
24
25 /* OS/2 doesn't have EIO.  FIXME: this whole notion of turning
26    a different error into EIO strikes me as pretty dubious.  */
27 # if !defined( EIO )
28 #   define EIO EBADPOS
29 # endif
30
31 /* Local functions.  */
32 static void buf_default_memory_error (struct buffer *);
33 static struct buffer_data *get_buffer_data (void);
34
35
36
37 /* Initialize a buffer structure.  */
38 struct buffer *
39 buf_initialize (type_buf_input input,
40                 type_buf_output output,
41                 type_buf_flush flush,
42                 type_buf_block block,
43                 type_buf_get_fd get_fd,
44                 type_buf_shutdown shutdown,
45                 type_buf_memory_error memory_error,
46                 void *closure)
47 {
48     struct buffer *buf;
49
50     buf = xmalloc (sizeof (struct buffer));
51     buf->data = NULL;
52     buf->last = NULL;
53     buf->nonblocking = false;
54     buf->input = input;
55     buf->output = output;
56     buf->flush = flush;
57     buf->block = block;
58     buf->get_fd = get_fd;
59     buf->shutdown = shutdown;
60     buf->memory_error = memory_error ? memory_error : buf_default_memory_error;
61     buf->closure = closure;
62     return buf;
63 }
64
65
66
67 /* Free a buffer structure.  */
68 void
69 buf_free (struct buffer *buf)
70 {
71     if (buf->closure != NULL)
72     {
73         free (buf->closure);
74         buf->closure = NULL;
75     }
76     buf_free_data (buf);
77     free (buf);
78 }
79
80
81
82 /* Initialize a buffer structure which is not to be used for I/O.  */
83 struct buffer *
84 buf_nonio_initialize( void (*memory) (struct buffer *) )
85 {
86     return buf_initialize (NULL, NULL, NULL, NULL, NULL, NULL, memory, NULL);
87 }
88
89
90
91 /* Default memory error handler.  */
92 static void
93 buf_default_memory_error (struct buffer *buf)
94 {
95     error (1, 0, "out of memory");
96 }
97
98
99
100 /* Allocate more buffer_data structures.  */
101 /* Get a new buffer_data structure.  */
102 static struct buffer_data *
103 get_buffer_data (void)
104 {
105     struct buffer_data *ret;
106
107     ret = xmalloc (sizeof (struct buffer_data));
108     ret->text = pagealign_xalloc (BUFFER_DATA_SIZE);
109
110     return ret;
111 }
112
113
114
115 /* See whether a buffer and its file descriptor is empty.  */
116 int
117 buf_empty (buf)
118     struct buffer *buf;
119 {
120         /* Try and read any data on the file descriptor first.
121          * We already know the descriptor is non-blocking.
122          */
123         buf_input_data (buf, NULL);
124         return buf_empty_p (buf);
125 }
126
127
128
129 /* See whether a buffer is empty.  */
130 int
131 buf_empty_p (struct buffer *buf)
132 {
133     struct buffer_data *data;
134
135     for (data = buf->data; data != NULL; data = data->next)
136         if (data->size > 0)
137             return 0;
138     return 1;
139 }
140
141
142
143 # if defined (SERVER_FLOWCONTROL) || defined (PROXY_SUPPORT)
144 /*
145  * Count how much data is stored in the buffer..
146  * Note that each buffer is a xmalloc'ed chunk BUFFER_DATA_SIZE.
147  */
148 int
149 buf_count_mem (struct buffer *buf)
150 {
151     struct buffer_data *data;
152     int mem = 0;
153
154     for (data = buf->data; data != NULL; data = data->next)
155         mem += BUFFER_DATA_SIZE;
156
157     return mem;
158 }
159 # endif /* SERVER_FLOWCONTROL || PROXY_SUPPORT */
160
161
162
163 /* Add data DATA of length LEN to BUF.  */
164 void
165 buf_output (struct buffer *buf, const char *data, size_t len)
166 {
167     if (buf->data != NULL
168         && (((buf->last->text + BUFFER_DATA_SIZE)
169              - (buf->last->bufp + buf->last->size))
170             >= len))
171     {
172         memcpy (buf->last->bufp + buf->last->size, data, len);
173         buf->last->size += len;
174         return;
175     }
176
177     while (1)
178     {
179         struct buffer_data *newdata;
180
181         newdata = get_buffer_data ();
182         if (newdata == NULL)
183         {
184             (*buf->memory_error) (buf);
185             return;
186         }
187
188         if (buf->data == NULL)
189             buf->data = newdata;
190         else
191             buf->last->next = newdata;
192         newdata->next = NULL;
193         buf->last = newdata;
194
195         newdata->bufp = newdata->text;
196
197         if (len <= BUFFER_DATA_SIZE)
198         {
199             newdata->size = len;
200             memcpy (newdata->text, data, len);
201             return;
202         }
203
204         newdata->size = BUFFER_DATA_SIZE;
205         memcpy (newdata->text, data, BUFFER_DATA_SIZE);
206
207         data += BUFFER_DATA_SIZE;
208         len -= BUFFER_DATA_SIZE;
209     }
210
211     /*NOTREACHED*/
212 }
213
214
215
216 /* Add a '\0' terminated string to BUF.  */
217 void
218 buf_output0 (struct buffer *buf, const char *string)
219 {
220     buf_output (buf, string, strlen (string));
221 }
222
223
224
225 /* Add a single character to BUF.  */
226 void
227 buf_append_char (struct buffer *buf, int ch)
228 {
229     if (buf->data != NULL
230         && (buf->last->text + BUFFER_DATA_SIZE
231             != buf->last->bufp + buf->last->size))
232     {
233         *(buf->last->bufp + buf->last->size) = ch;
234         ++buf->last->size;
235     }
236     else
237     {
238         char b;
239
240         b = ch;
241         buf_output (buf, &b, 1);
242     }
243 }
244
245
246
247 /* Free struct buffer_data's from the list starting with FIRST and ending at
248  * LAST, inclusive.
249  */
250 static inline void
251 buf_free_datas (struct buffer_data *first, struct buffer_data *last)
252 {
253     struct buffer_data *b, *n, *p;
254     b = first;
255     do
256     {
257         p = b;
258         n = b->next;
259         pagealign_free (b->text);
260         free (b);
261         b = n;
262     } while (p != last);
263 }
264
265
266
267 /*
268  * Send all the output we've been saving up.  Returns 0 for success or
269  * errno code.  If the buffer has been set to be nonblocking, this
270  * will just write until the write would block.
271  */
272 int
273 buf_send_output (struct buffer *buf)
274 {
275     assert (buf->output != NULL);
276
277     while (buf->data != NULL)
278     {
279         struct buffer_data *data;
280
281         data = buf->data;
282
283         if (data->size > 0)
284         {
285             int status;
286             size_t nbytes;
287
288             status = (*buf->output) (buf->closure, data->bufp, data->size,
289                                      &nbytes);
290             if (status != 0)
291             {
292                 /* Some sort of error.  Discard the data, and return.  */
293                 buf_free_data (buf);
294                 return status;
295             }
296
297             if (nbytes != data->size)
298             {
299                 /* Not all the data was written out.  This is only
300                    permitted in nonblocking mode.  Adjust the buffer,
301                    and return.  */
302
303                 assert (buf->nonblocking);
304
305                 data->size -= nbytes;
306                 data->bufp += nbytes;
307
308                 return 0;
309             }
310         }
311
312         buf->data = data->next;
313         buf_free_datas (data, data);
314     }
315
316     buf->last = NULL;
317
318     return 0;
319 }
320
321
322
323 /*
324  * Flush any data queued up in the buffer.  If BLOCK is nonzero, then
325  * if the buffer is in nonblocking mode, put it into blocking mode for
326  * the duration of the flush.  This returns 0 on success, or an error
327  * code.
328  */
329 int
330 buf_flush (struct buffer *buf, bool block)
331 {
332     int nonblocking;
333     int status;
334
335     assert (buf->flush != NULL);
336
337     nonblocking = buf->nonblocking;
338     if (nonblocking && block)
339     {
340         status = set_block (buf);
341         if (status != 0)
342             return status;
343     }
344
345     status = buf_send_output (buf);
346     if (status == 0)
347         status = (*buf->flush) (buf->closure);
348
349     if (nonblocking && block)
350     {
351         int blockstat;
352
353         blockstat = set_nonblock (buf);
354         if (status == 0)
355             status = blockstat;
356     }
357
358     return status;
359 }
360
361
362
363 /*
364  * Set buffer BUF to nonblocking I/O.  Returns 0 for success or errno
365  * code.
366  */
367 int
368 set_nonblock (struct buffer *buf)
369 {
370     int status;
371
372     if (buf->nonblocking)
373         return 0;
374     assert (buf->block != NULL);
375     status = (*buf->block) (buf->closure, 0);
376     if (status != 0)
377         return status;
378     buf->nonblocking = true;
379     return 0;
380 }
381
382
383
384 /*
385  * Set buffer BUF to blocking I/O.  Returns 0 for success or errno
386  * code.
387  */
388 int
389 set_block (struct buffer *buf)
390 {
391     int status;
392
393     if (! buf->nonblocking)
394         return 0;
395     assert (buf->block != NULL);
396     status = (*buf->block) (buf->closure, 1);
397     if (status != 0)
398         return status;
399     buf->nonblocking = false;
400     return 0;
401 }
402
403
404
405 /*
406  * Send a character count and some output.  Returns errno code or 0 for
407  * success.
408  *
409  * Sending the count in binary is OK since this is only used on a pipe
410  * within the same system.
411  */
412 int
413 buf_send_counted (struct buffer *buf)
414 {
415     int size;
416     struct buffer_data *data;
417
418     size = 0;
419     for (data = buf->data; data != NULL; data = data->next)
420         size += data->size;
421
422     data = get_buffer_data ();
423     if (data == NULL)
424     {
425         (*buf->memory_error) (buf);
426         return ENOMEM;
427     }
428
429     data->next = buf->data;
430     buf->data = data;
431     if (buf->last == NULL)
432         buf->last = data;
433
434     data->bufp = data->text;
435     data->size = sizeof (int);
436
437     *((int *) data->text) = size;
438
439     return buf_send_output (buf);
440 }
441
442
443
444 /*
445  * Send a special count.  COUNT should be negative.  It will be
446  * handled specially by buf_copy_counted.  This function returns 0 or
447  * an errno code.
448  *
449  * Sending the count in binary is OK since this is only used on a pipe
450  * within the same system.
451  */
452 int
453 buf_send_special_count (struct buffer *buf, int count)
454 {
455     struct buffer_data *data;
456
457     data = get_buffer_data ();
458     if (data == NULL)
459     {
460         (*buf->memory_error) (buf);
461         return ENOMEM;
462     }
463
464     data->next = buf->data;
465     buf->data = data;
466     if (buf->last == NULL)
467         buf->last = data;
468
469     data->bufp = data->text;
470     data->size = sizeof (int);
471
472     *((int *) data->text) = count;
473
474     return buf_send_output (buf);
475 }
476
477
478
479 /* Append a list of buffer_data structures to an buffer.  */
480 void
481 buf_append_data (struct buffer *buf, struct buffer_data *data,
482                  struct buffer_data *last)
483 {
484     if (data != NULL)
485     {
486         if (buf->data == NULL)
487             buf->data = data;
488         else
489             buf->last->next = data;
490         buf->last = last;
491     }
492 }
493
494
495
496 # ifdef PROXY_SUPPORT
497 /* Copy data structures and append them to a buffer.
498  *
499  * ERRORS
500  *   Failure to allocate memory here is fatal.
501  */
502 void
503 buf_copy_data (struct buffer *buf, struct buffer_data *data,
504                struct buffer_data *last)
505 {
506     struct buffer_data *first, *new, *cur, *prev;
507
508     assert (buf);
509     assert (data);
510
511     prev = first = NULL;
512     cur = data;
513     while (1)
514     {
515         new = get_buffer_data ();
516         if (!new) error (1, errno, "Failed to allocate buffer data.");
517
518         if (!first) first = new;
519         memcpy (new->text, cur->bufp, cur->size);
520         new->bufp = new->text;
521         new->size = cur->size;
522         new->next = NULL;
523         if (prev) prev->next = new;
524         if (cur == last) break;
525         prev = new;
526         cur = cur->next;
527     }
528
529     buf_append_data (buf, first, new);
530 }
531 # endif /* PROXY_SUPPORT */
532
533
534
535 /* Dispose of any remaining data in the buffer.  */
536 void
537 buf_free_data (struct buffer *buffer)
538 {
539     if (buf_empty_p (buffer)) return;
540     buf_free_datas (buffer->data, buffer->last);
541     buffer->data = buffer->last = NULL;
542 }
543
544
545
546 /* Append the data in one buffer to another.  This removes the data
547  * from the source buffer.
548  */
549 void
550 buf_append_buffer (struct buffer *to, struct buffer *from)
551 {
552     struct buffer_data *n;
553
554     /* Copy the data pointer to the new buf.  */
555     buf_append_data (to, from->data, from->last);
556
557     n = from->data;
558     while (n)
559     {
560         if (n == from->last) break;
561         n = n->next;
562     }
563
564     /* Remove from the original location.  */
565     from->data = NULL;
566     from->last = NULL;
567 }
568
569
570
571 /*
572  * Copy the contents of file F into buffer_data structures.  We can't
573  * copy directly into an buffer, because we want to handle failure and
574  * success differently.  Returns 0 on success, or -2 if out of
575  * memory, or a status code on error.  Since the caller happens to
576  * know the size of the file, it is passed in as SIZE.  On success,
577  * this function sets *RETP and *LASTP, which may be passed to
578  * buf_append_data.
579  */
580 int
581 buf_read_file (FILE *f, long int size, struct buffer_data **retp,
582                struct buffer_data **lastp)
583 {
584     int status;
585
586     *retp = NULL;
587     *lastp = NULL;
588
589     while (size > 0)
590     {
591         struct buffer_data *data;
592         int get;
593
594         data = get_buffer_data ();
595         if (data == NULL)
596         {
597             status = -2;
598             goto error_return;
599         }
600
601         if (*retp == NULL)
602             *retp = data;
603         else
604             (*lastp)->next = data;
605         data->next = NULL;
606         *lastp = data;
607
608         data->bufp = data->text;
609         data->size = 0;
610
611         if (size > BUFFER_DATA_SIZE)
612             get = BUFFER_DATA_SIZE;
613         else
614             get = size;
615
616         errno = EIO;
617         if (fread (data->text, get, 1, f) != 1)
618         {
619             status = errno;
620             goto error_return;
621         }
622
623         data->size += get;
624         size -= get;
625     }
626
627     return 0;
628
629   error_return:
630     if (*retp != NULL)
631         buf_free_datas (*retp, (*lastp)->next);
632     return status;
633 }
634
635
636
637 /*
638  * Copy the contents of file F into buffer_data structures.  We can't
639  * copy directly into an buffer, because we want to handle failure and
640  * success differently.  Returns 0 on success, or -2 if out of
641  * memory, or a status code on error.  On success, this function sets
642  * *RETP and *LASTP, which may be passed to buf_append_data.
643  */
644 int
645 buf_read_file_to_eof (FILE *f, struct buffer_data **retp,
646                       struct buffer_data **lastp)
647 {
648     int status;
649
650     *retp = NULL;
651     *lastp = NULL;
652
653     while (!feof (f))
654     {
655         struct buffer_data *data;
656         int get, nread;
657
658         data = get_buffer_data ();
659         if (data == NULL)
660         {
661             status = -2;
662             goto error_return;
663         }
664
665         if (*retp == NULL)
666             *retp = data;
667         else
668             (*lastp)->next = data;
669         data->next = NULL;
670         *lastp = data;
671
672         data->bufp = data->text;
673         data->size = 0;
674
675         get = BUFFER_DATA_SIZE;
676
677         errno = EIO;
678         nread = fread (data->text, 1, get, f);
679         if (nread == 0 && !feof (f))
680         {
681             status = errno;
682             goto error_return;
683         }
684
685         data->size = nread;
686     }
687
688     return 0;
689
690   error_return:
691     if (*retp != NULL)
692         buf_free_datas (*retp, (*lastp)->next);
693     return status;
694 }
695
696
697
698 /* Return the number of bytes in a chain of buffer_data structures.  */
699 int
700 buf_chain_length (struct buffer_data *buf)
701 {
702     int size = 0;
703     while (buf)
704     {
705         size += buf->size;
706         buf = buf->next;
707     }
708     return size;
709 }
710
711
712
713 /* Return the number of bytes in a buffer.  */
714 int
715 buf_length (struct buffer *buf)
716 {
717     return buf_chain_length (buf->data);
718 }
719
720
721
722 /*
723  * Read an arbitrary amount of data into an input buffer.  The buffer
724  * will be in nonblocking mode, and we just grab what we can.  Return
725  * 0 on success, or -1 on end of file, or -2 if out of memory, or an
726  * error code.  If COUNTP is not NULL, *COUNTP is set to the number of
727  * bytes read.
728  */
729 int
730 buf_input_data (struct buffer *buf, size_t *countp)
731 {
732     assert (buf->input != NULL);
733
734     if (countp != NULL)
735         *countp = 0;
736
737     while (1)
738     {
739         int status;
740         size_t get, nbytes;
741
742         if (buf->data == NULL
743             || (buf->last->bufp + buf->last->size
744                 == buf->last->text + BUFFER_DATA_SIZE))
745         {
746             struct buffer_data *data;
747
748             data = get_buffer_data ();
749             if (data == NULL)
750             {
751                 (*buf->memory_error) (buf);
752                 return -2;
753             }
754
755             if (buf->data == NULL)
756                 buf->data = data;
757             else
758                 buf->last->next = data;
759             data->next = NULL;
760             buf->last = data;
761
762             data->bufp = data->text;
763             data->size = 0;
764         }
765
766         get = ((buf->last->text + BUFFER_DATA_SIZE)
767                - (buf->last->bufp + buf->last->size));
768
769         status = (*buf->input) (buf->closure,
770                                 buf->last->bufp + buf->last->size,
771                                 0, get, &nbytes);
772         if (status != 0)
773             return status;
774
775         buf->last->size += nbytes;
776         if (countp != NULL)
777             *countp += nbytes;
778
779         if (nbytes < get)
780         {
781             /* If we did not fill the buffer, then presumably we read
782                all the available data.  */
783             return 0;
784         }
785     }
786
787     /*NOTREACHED*/
788 }
789
790
791
792 /*
793  * Read a line (characters up to a \012) from an input buffer.  (We
794  * use \012 rather than \n for the benefit of non Unix clients for
795  * which \n means something else).  This returns 0 on success, or -1
796  * on end of file, or -2 if out of memory, or an error code.  If it
797  * succeeds, it sets *LINE to an allocated buffer holding the contents
798  * of the line.  The trailing \012 is not included in the buffer.  If
799  * LENP is not NULL, then *LENP is set to the number of bytes read;
800  * strlen may not work, because there may be embedded null bytes.
801  */
802 int
803 buf_read_line (struct buffer *buf, char **line, size_t *lenp)
804 {
805     return buf_read_short_line (buf, line, lenp, SIZE_MAX);
806 }
807
808
809
810 /* Like buf_read_line, but return -2 if no newline is found in MAX characters.
811  */
812 int
813 buf_read_short_line (struct buffer *buf, char **line, size_t *lenp,
814                      size_t max)
815 {
816     assert (buf->input != NULL);
817
818     *line = NULL;
819
820     while (1)
821     {
822         size_t len, finallen, predicted_len;
823         struct buffer_data *data;
824         char *nl;
825
826         /* See if there is a newline in BUF.  */
827         len = 0;
828         for (data = buf->data; data != NULL; data = data->next)
829         {
830             nl = memchr (data->bufp, '\012', data->size);
831             if (nl != NULL)
832             {
833                 finallen = nl - data->bufp;
834                 if (xsum (len, finallen) >= max) return -2;
835                 len += finallen;
836                 break;
837             }
838             else if (xsum (len, data->size) >= max) return -2;
839             len += data->size;
840         }
841
842         /* If we found a newline, copy the line into a memory buffer,
843            and remove it from BUF.  */
844         if (data != NULL)
845         {
846             char *p;
847             struct buffer_data *nldata;
848
849             p = xmalloc (len + 1);
850             if (p == NULL)
851                 return -2;
852             *line = p;
853
854             nldata = data;
855             data = buf->data;
856             while (data != nldata)
857             {
858                 struct buffer_data *next;
859
860                 memcpy (p, data->bufp, data->size);
861                 p += data->size;
862                 next = data->next;
863                 buf_free_datas (data, data);
864                 data = next;
865             }
866
867             memcpy (p, data->bufp, finallen);
868             p[finallen] = '\0';
869
870             data->size -= finallen + 1;
871             data->bufp = nl + 1;
872             buf->data = data;
873
874             if (lenp != NULL)
875                 *lenp = len;
876
877             return 0;
878         }
879
880         /* Read more data until we get a newline or MAX characters.  */
881         predicted_len = 0;
882         while (1)
883         {
884             int status;
885             size_t size, nbytes;
886             char *mem;
887
888             if (buf->data == NULL
889                 || (buf->last->bufp + buf->last->size
890                     == buf->last->text + BUFFER_DATA_SIZE))
891             {
892                 data = get_buffer_data ();
893                 if (data == NULL)
894                 {
895                     (*buf->memory_error) (buf);
896                     return -2;
897                 }
898
899                 if (buf->data == NULL)
900                     buf->data = data;
901                 else
902                     buf->last->next = data;
903                 data->next = NULL;
904                 buf->last = data;
905
906                 data->bufp = data->text;
907                 data->size = 0;
908             }
909
910             mem = buf->last->bufp + buf->last->size;
911             size = (buf->last->text + BUFFER_DATA_SIZE) - mem;
912
913             /* We need to read at least 1 byte.  We can handle up to
914                SIZE bytes.  This will only be efficient if the
915                underlying communication stream does its own buffering,
916                or is clever about getting more than 1 byte at a time.  */
917             status = (*buf->input) (buf->closure, mem, 1, size, &nbytes);
918             if (status != 0)
919                 return status;
920
921             predicted_len += nbytes;
922             buf->last->size += nbytes;
923
924             /* Optimize slightly to avoid an unnecessary call to memchr.  */
925             if (nbytes == 1)
926             {
927                 if (*mem == '\012')
928                     break;
929             }
930             else
931             {
932                 if (memchr (mem, '\012', nbytes) != NULL)
933                     break;
934             }
935             if (xsum (len, predicted_len) >= max) return -2;
936         }
937     }
938 }
939
940
941
942 /*
943  * Extract data from the input buffer BUF.  This will read up to WANT
944  * bytes from the buffer.  It will set *RETDATA to point at the bytes,
945  * and set *GOT to the number of bytes to be found there.  Any buffer
946  * call which uses BUF may change the contents of the buffer at *DATA,
947  * so the data should be fully processed before any further calls are
948  * made.  This returns 0 on success, or -1 on end of file, or -2 if
949  * out of memory, or an error code.
950  */
951 int
952 buf_read_data (struct buffer *buf, size_t want, char **retdata, size_t *got)
953 {
954     assert (buf->input != NULL);
955
956     while (buf->data != NULL && buf->data->size == 0)
957     {
958         struct buffer_data *next;
959
960         next = buf->data->next;
961         buf_free_datas (buf->data, buf->data);
962         buf->data = next;
963         if (next == NULL)
964             buf->last = NULL;
965     }
966
967     if (buf->data == NULL)
968     {
969         struct buffer_data *data;
970         int status;
971         size_t get, nbytes;
972
973         data = get_buffer_data ();
974         if (data == NULL)
975         {
976             (*buf->memory_error) (buf);
977             return -2;
978         }
979
980         buf->data = data;
981         buf->last = data;
982         data->next = NULL;
983         data->bufp = data->text;
984         data->size = 0;
985
986         if (want < BUFFER_DATA_SIZE)
987             get = want;
988         else
989             get = BUFFER_DATA_SIZE;
990         status = (*buf->input) (buf->closure, data->bufp, get,
991                                 BUFFER_DATA_SIZE, &nbytes);
992         if (status != 0)
993             return status;
994
995         data->size = nbytes;
996     }
997
998     *retdata = buf->data->bufp;
999     if (want < buf->data->size)
1000     {
1001         *got = want;
1002         buf->data->size -= want;
1003         buf->data->bufp += want;
1004     }
1005     else
1006     {
1007         *got = buf->data->size;
1008         buf->data->size = 0;
1009     }
1010
1011     return 0;
1012 }
1013
1014
1015
1016 /*
1017  * Copy lines from an input buffer to an output buffer.
1018  * This copies all complete lines (characters up to a
1019  * newline) from INBUF to OUTBUF.  Each line in OUTBUF is preceded by the
1020  * character COMMAND and a space.
1021  */
1022 void
1023 buf_copy_lines (struct buffer *outbuf, struct buffer *inbuf, int command)
1024 {
1025     while (1)
1026     {
1027         struct buffer_data *data;
1028         struct buffer_data *nldata;
1029         char *nl;
1030         int len;
1031
1032         /* See if there is a newline in INBUF.  */
1033         nldata = NULL;
1034         nl = NULL;
1035         for (data = inbuf->data; data != NULL; data = data->next)
1036         {
1037             nl = memchr (data->bufp, '\n', data->size);
1038             if (nl != NULL)
1039             {
1040                 nldata = data;
1041                 break;
1042             }
1043         }
1044
1045         if (nldata == NULL)
1046         {
1047             /* There are no more lines in INBUF.  */
1048             return;
1049         }
1050
1051         /* Put in the command.  */
1052         buf_append_char (outbuf, command);
1053         buf_append_char (outbuf, ' ');
1054
1055         if (inbuf->data != nldata)
1056         {
1057             /*
1058              * Simply move over all the buffers up to the one containing
1059              * the newline.
1060              */
1061             for (data = inbuf->data; data->next != nldata; data = data->next);
1062             data->next = NULL;
1063             buf_append_data (outbuf, inbuf->data, data);
1064             inbuf->data = nldata;
1065         }
1066
1067         /*
1068          * If the newline is at the very end of the buffer, just move
1069          * the buffer onto OUTBUF.  Otherwise we must copy the data.
1070          */
1071         len = nl + 1 - nldata->bufp;
1072         if (len == nldata->size)
1073         {
1074             inbuf->data = nldata->next;
1075             if (inbuf->data == NULL)
1076                 inbuf->last = NULL;
1077
1078             nldata->next = NULL;
1079             buf_append_data (outbuf, nldata, nldata);
1080         }
1081         else
1082         {
1083             buf_output (outbuf, nldata->bufp, len);
1084             nldata->bufp += len;
1085             nldata->size -= len;
1086         }
1087     }
1088 }
1089
1090
1091
1092 /*
1093  * Copy counted data from one buffer to another.  The count is an
1094  * integer, host size, host byte order (it is only used across a
1095  * pipe).  If there is enough data, it should be moved over.  If there
1096  * is not enough data, it should remain on the original buffer.  A
1097  * negative count is a special case.  if one is seen, *SPECIAL is set
1098  * to the (negative) count value and no additional data is gathered
1099  * from the buffer; normally *SPECIAL is set to 0.  This function
1100  * returns the number of bytes it needs to see in order to actually
1101  * copy something over.
1102  */
1103 int
1104 buf_copy_counted (struct buffer *outbuf, struct buffer *inbuf, int *special)
1105 {
1106     *special = 0;
1107
1108     while (1)
1109     {
1110         struct buffer_data *data;
1111         int need;
1112         union
1113         {
1114             char intbuf[sizeof (int)];
1115             int i;
1116         } u;
1117         char *intp;
1118         int count;
1119         struct buffer_data *start;
1120         int startoff;
1121         struct buffer_data *stop;
1122         int stopwant;
1123
1124         /* See if we have enough bytes to figure out the count.  */
1125         need = sizeof (int);
1126         intp = u.intbuf;
1127         for (data = inbuf->data; data != NULL; data = data->next)
1128         {
1129             if (data->size >= need)
1130             {
1131                 memcpy (intp, data->bufp, need);
1132                 break;
1133             }
1134             memcpy (intp, data->bufp, data->size);
1135             intp += data->size;
1136             need -= data->size;
1137         }
1138         if (data == NULL)
1139         {
1140             /* We don't have enough bytes to form an integer.  */
1141             return need;
1142         }
1143
1144         count = u.i;
1145         start = data;
1146         startoff = need;
1147
1148         if (count < 0)
1149         {
1150             /* A negative COUNT is a special case meaning that we
1151                don't need any further information.  */
1152             stop = start;
1153             stopwant = 0;
1154         }
1155         else
1156         {
1157             /*
1158              * We have an integer in COUNT.  We have gotten all the
1159              * data from INBUF in all buffers before START, and we
1160              * have gotten STARTOFF bytes from START.  See if we have
1161              * enough bytes remaining in INBUF.
1162              */
1163             need = count - (start->size - startoff);
1164             if (need <= 0)
1165             {
1166                 stop = start;
1167                 stopwant = count;
1168             }
1169             else
1170             {
1171                 for (data = start->next; data != NULL; data = data->next)
1172                 {
1173                     if (need <= data->size)
1174                         break;
1175                     need -= data->size;
1176                 }
1177                 if (data == NULL)
1178                 {
1179                     /* We don't have enough bytes.  */
1180                     return need;
1181                 }
1182                 stop = data;
1183                 stopwant = need;
1184             }
1185         }
1186
1187         /*
1188          * We have enough bytes.  Free any buffers in INBUF before
1189          * START, and remove STARTOFF bytes from START, so that we can
1190          * forget about STARTOFF.
1191          */
1192         start->bufp += startoff;
1193         start->size -= startoff;
1194
1195         if (start->size == 0)
1196             start = start->next;
1197
1198         if (stop->size == stopwant)
1199         {
1200             stop = stop->next;
1201             stopwant = 0;
1202         }
1203
1204         while (inbuf->data != start)
1205         {
1206             data = inbuf->data;
1207             inbuf->data = data->next;
1208             buf_free_datas (data, data);
1209         }
1210
1211         /* If COUNT is negative, set *SPECIAL and get out now.  */
1212         if (count < 0)
1213         {
1214             *special = count;
1215             return 0;
1216         }
1217
1218         /*
1219          * We want to copy over the bytes from START through STOP.  We
1220          * only want STOPWANT bytes from STOP.
1221          */
1222
1223         if (start != stop)
1224         {
1225             /* Attach the buffers from START through STOP to OUTBUF.  */
1226             for (data = start; data->next != stop; data = data->next);
1227             inbuf->data = stop;
1228             data->next = NULL;
1229             buf_append_data (outbuf, start, data);
1230         }
1231
1232         if (stopwant > 0)
1233         {
1234             buf_output (outbuf, stop->bufp, stopwant);
1235             stop->bufp += stopwant;
1236             stop->size -= stopwant;
1237         }
1238     }
1239
1240     /*NOTREACHED*/
1241 }
1242
1243
1244
1245 int
1246 buf_get_fd (struct buffer *buf)
1247 {
1248     if (buf->get_fd)
1249         return (*buf->get_fd) (buf->closure);
1250     return -1;
1251 }
1252
1253
1254
1255 /* Shut down a buffer.  This returns 0 on success, or an errno code.  */
1256 int
1257 buf_shutdown (struct buffer *buf)
1258 {
1259     if (buf->shutdown) return (*buf->shutdown) (buf);
1260     return 0;
1261 }
1262
1263
1264
1265 /* Certain types of communication input and output data in packets,
1266    where each packet is translated in some fashion.  The packetizing
1267    buffer type supports that, given a buffer which handles lower level
1268    I/O and a routine to translate the data in a packet.
1269
1270    This code uses two bytes for the size of a packet, so packets are
1271    restricted to 65536 bytes in total.
1272
1273    The translation functions should just translate; they may not
1274    significantly increase or decrease the amount of data.  The actual
1275    size of the initial data is part of the translated data.  The
1276    output translation routine may add up to PACKET_SLOP additional
1277    bytes, and the input translation routine should shrink the data
1278    correspondingly.  */
1279
1280 # define PACKET_SLOP (100)
1281
1282 /* This structure is the closure field of a packetizing buffer.  */
1283
1284 struct packetizing_buffer
1285 {
1286     /* The underlying buffer.  */
1287     struct buffer *buf;
1288     /* The input translation function.  Exactly one of inpfn and outfn
1289        will be NULL.  The input translation function should
1290        untranslate the data in INPUT, storing the result in OUTPUT.
1291        SIZE is the amount of data in INPUT, and is also the size of
1292        OUTPUT.  This should return 0 on success, or an errno code.  */
1293     int (*inpfn) (void *fnclosure, const char *input, char *output,
1294                         size_t size);
1295     /* The output translation function.  This should translate the
1296        data in INPUT, storing the result in OUTPUT.  The first two
1297        bytes in INPUT will be the size of the data, and so will SIZE.
1298        This should set *TRANSLATED to the amount of translated data in
1299        OUTPUT.  OUTPUT is large enough to hold SIZE + PACKET_SLOP
1300        bytes.  This should return 0 on success, or an errno code.  */
1301     int (*outfn) (void *fnclosure, const char *input, char *output,
1302                         size_t size, size_t *translated);
1303     /* A closure for the translation function.  */
1304     void *fnclosure;
1305     /* For an input buffer, we may have to buffer up data here.  */
1306     /* This is non-zero if the buffered data has been translated.
1307        Otherwise, the buffered data has not been translated, and starts
1308        with the two byte packet size.  */
1309     bool translated;
1310     /* The amount of buffered data.  */
1311     size_t holdsize;
1312     /* The buffer allocated to hold the data.  */
1313     char *holdbuf;
1314     /* The size of holdbuf.  */
1315     size_t holdbufsize;
1316     /* If translated is set, we need another data pointer to track
1317        where we are in holdbuf.  If translated is clear, then this
1318        pointer is not used.  */
1319     char *holddata;
1320 };
1321
1322
1323
1324 static int packetizing_buffer_input (void *, char *, size_t, size_t, size_t *);
1325 static int packetizing_buffer_output (void *, const char *, size_t, size_t *);
1326 static int packetizing_buffer_flush (void *);
1327 static int packetizing_buffer_block (void *, bool);
1328 static int packetizing_buffer_get_fd (void *);
1329 static int packetizing_buffer_shutdown (struct buffer *);
1330
1331
1332
1333 /* Create a packetizing buffer.  */
1334 struct buffer *
1335 packetizing_buffer_initialize (struct buffer *buf,
1336                                int (*inpfn) (void *, const char *, char *,
1337                                              size_t),
1338                                int (*outfn) (void *, const char *, char *,
1339                                              size_t, size_t *),
1340                                void *fnclosure,
1341                                void (*memory) (struct buffer *))
1342 {
1343     struct packetizing_buffer *pb;
1344
1345     pb = xmalloc (sizeof *pb);
1346     memset (pb, 0, sizeof *pb);
1347
1348     pb->buf = buf;
1349     pb->inpfn = inpfn;
1350     pb->outfn = outfn;
1351     pb->fnclosure = fnclosure;
1352
1353     if (inpfn != NULL)
1354     {
1355         /* Add PACKET_SLOP to handle larger translated packets, and
1356            add 2 for the count.  This buffer is increased if
1357            necessary.  */
1358         pb->holdbufsize = BUFFER_DATA_SIZE + PACKET_SLOP + 2;
1359         pb->holdbuf = xmalloc (pb->holdbufsize);
1360     }
1361
1362     return buf_initialize (inpfn != NULL ? packetizing_buffer_input : NULL,
1363                            inpfn != NULL ? NULL : packetizing_buffer_output,
1364                            inpfn != NULL ? NULL : packetizing_buffer_flush,
1365                            packetizing_buffer_block,
1366                            packetizing_buffer_get_fd,
1367                            packetizing_buffer_shutdown,
1368                            memory,
1369                            pb);
1370 }
1371
1372
1373
1374 /* Input data from a packetizing buffer.  */
1375 static int
1376 packetizing_buffer_input (void *closure, char *data, size_t need, size_t size,
1377                           size_t *got)
1378 {
1379     struct packetizing_buffer *pb = closure;
1380
1381     *got = 0;
1382
1383     if (pb->holdsize > 0 && pb->translated)
1384     {
1385         size_t copy;
1386
1387         copy = pb->holdsize;
1388
1389         if (copy > size)
1390         {
1391             memcpy (data, pb->holddata, size);
1392             pb->holdsize -= size;
1393             pb->holddata += size;
1394             *got = size;
1395             return 0;
1396         }
1397
1398         memcpy (data, pb->holddata, copy);
1399         pb->holdsize = 0;
1400         pb->translated = false;
1401
1402         data += copy;
1403         need -= copy;
1404         size -= copy;
1405         *got = copy;
1406     }
1407
1408     while (need > 0 || *got == 0)
1409     {
1410         int status;
1411         size_t get, nread, count, tcount;
1412         char *bytes;
1413         static char *stackoutbuf = NULL;
1414         char *inbuf, *outbuf;
1415
1416         if (!stackoutbuf)
1417             stackoutbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP);
1418
1419         /* If we don't already have the two byte count, get it.  */
1420         if (pb->holdsize < 2)
1421         {
1422             get = 2 - pb->holdsize;
1423             status = buf_read_data (pb->buf, get, &bytes, &nread);
1424             if (status != 0)
1425             {
1426                 /* buf_read_data can return -2, but a buffer input
1427                    function is only supposed to return -1, 0, or an
1428                    error code.  */
1429                 if (status == -2)
1430                     status = ENOMEM;
1431                 return status;
1432             }
1433
1434             if (nread == 0)
1435             {
1436                 /* The buffer is in nonblocking mode, and we didn't
1437                    manage to read anything.  */
1438                 return 0;
1439             }
1440
1441             if (get == 1)
1442                 pb->holdbuf[1] = bytes[0];
1443             else
1444             {
1445                 pb->holdbuf[0] = bytes[0];
1446                 if (nread < 2)
1447                 {
1448                     /* We only got one byte, but we needed two.  Stash
1449                        the byte we got, and try again.  */
1450                     pb->holdsize = 1;
1451                     continue;
1452                 }
1453                 pb->holdbuf[1] = bytes[1];
1454             }
1455             pb->holdsize = 2;
1456         }
1457
1458         /* Read the packet.  */
1459
1460         count = (((pb->holdbuf[0] & 0xff) << 8)
1461                  + (pb->holdbuf[1] & 0xff));
1462
1463         if (count + 2 > pb->holdbufsize)
1464         {
1465             char *n;
1466
1467             /* We didn't allocate enough space in the initialize
1468                function.  */
1469
1470             n = xrealloc (pb->holdbuf, count + 2);
1471             if (n == NULL)
1472             {
1473                 (*pb->buf->memory_error) (pb->buf);
1474                 return ENOMEM;
1475             }
1476             pb->holdbuf = n;
1477             pb->holdbufsize = count + 2;
1478         }
1479
1480         get = count - (pb->holdsize - 2);
1481
1482         status = buf_read_data (pb->buf, get, &bytes, &nread);
1483         if (status != 0)
1484         {
1485             /* buf_read_data can return -2, but a buffer input
1486                function is only supposed to return -1, 0, or an error
1487                code.  */
1488             if (status == -2)
1489                 status = ENOMEM;
1490             return status;
1491         }
1492
1493         if (nread == 0)
1494         {
1495             /* We did not get any data.  Presumably the buffer is in
1496                nonblocking mode.  */
1497             return 0;
1498         }
1499
1500         if (nread < get)
1501         {
1502             /* We did not get all the data we need to fill the packet.
1503                buf_read_data does not promise to return all the bytes
1504                requested, so we must try again.  */
1505             memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1506             pb->holdsize += nread;
1507             continue;
1508         }
1509
1510         /* We have a complete untranslated packet of COUNT bytes.  */
1511
1512         if (pb->holdsize == 2)
1513         {
1514             /* We just read the entire packet (the 2 bytes in
1515                PB->HOLDBUF are the size).  Save a memcpy by
1516                translating directly from BYTES.  */
1517             inbuf = bytes;
1518         }
1519         else
1520         {
1521             /* We already had a partial packet in PB->HOLDBUF.  We
1522                need to copy the new data over to make the input
1523                contiguous.  */
1524             memcpy (pb->holdbuf + pb->holdsize, bytes, nread);
1525             inbuf = pb->holdbuf + 2;
1526         }
1527
1528         if (count <= BUFFER_DATA_SIZE + PACKET_SLOP)
1529             outbuf = stackoutbuf;
1530         else
1531         {
1532             outbuf = xmalloc (count);
1533             if (outbuf == NULL)
1534             {
1535                 (*pb->buf->memory_error) (pb->buf);
1536                 return ENOMEM;
1537             }
1538         }
1539
1540         status = (*pb->inpfn) (pb->fnclosure, inbuf, outbuf, count);
1541         if (status != 0)
1542             return status;
1543
1544         /* The first two bytes in the translated buffer are the real
1545            length of the translated data.  */
1546         tcount = ((outbuf[0] & 0xff) << 8) + (outbuf[1] & 0xff);
1547
1548         if (tcount > count)
1549             error (1, 0, "Input translation failure");
1550
1551         if (tcount > size)
1552         {
1553             /* We have more data than the caller has provided space
1554                for.  We need to save some of it for the next call.  */
1555
1556             memcpy (data, outbuf + 2, size);
1557             *got += size;
1558
1559             pb->holdsize = tcount - size;
1560             memcpy (pb->holdbuf, outbuf + 2 + size, tcount - size);
1561             pb->holddata = pb->holdbuf;
1562             pb->translated = true;
1563
1564             if (outbuf != stackoutbuf)
1565                 free (outbuf);
1566
1567             return 0;
1568         }
1569
1570         memcpy (data, outbuf + 2, tcount);
1571
1572         if (outbuf != stackoutbuf)
1573             free (outbuf);
1574
1575         pb->holdsize = 0;
1576
1577         data += tcount;
1578         need -= tcount;
1579         size -= tcount;
1580         *got += tcount;
1581     }
1582
1583     return 0;
1584 }
1585
1586
1587
1588 /* Output data to a packetizing buffer.  */
1589 static int
1590 packetizing_buffer_output (void *closure, const char *data, size_t have,
1591                            size_t *wrote)
1592 {
1593     struct packetizing_buffer *pb = closure;
1594     static char *inbuf = NULL;  /* These two buffers are static so that they
1595                                  * depend on the size of BUFFER_DATA_SIZE yet
1596                                  * still only be allocated once per run.
1597                                  */
1598     static char *stack_outbuf = NULL;
1599     struct buffer_data *outdata = NULL; /* Initialize to silence -Wall.  Dumb.
1600                                          */
1601     char *outbuf;
1602     size_t size, translated;
1603     int status;
1604
1605     /* It would be easy to xmalloc a buffer, but I don't think this
1606        case can ever arise.  */
1607     assert (have <= BUFFER_DATA_SIZE);
1608
1609     if (!inbuf)
1610     {
1611         inbuf = xmalloc (BUFFER_DATA_SIZE + 2);
1612         stack_outbuf = xmalloc (BUFFER_DATA_SIZE + PACKET_SLOP + 4);
1613     }
1614
1615     inbuf[0] = (have >> 8) & 0xff;
1616     inbuf[1] = have & 0xff;
1617     memcpy (inbuf + 2, data, have);
1618
1619     size = have + 2;
1620
1621     /* The output function is permitted to add up to PACKET_SLOP
1622        bytes, and we need 2 bytes for the size of the translated data.
1623        If we can guarantee that the result will fit in a buffer_data,
1624        we translate directly into one to avoid a memcpy in buf_output.  */
1625     if (size + PACKET_SLOP + 2 > BUFFER_DATA_SIZE)
1626         outbuf = stack_outbuf;
1627     else
1628     {
1629         outdata = get_buffer_data ();
1630         if (outdata == NULL)
1631         {
1632             (*pb->buf->memory_error) (pb->buf);
1633             return ENOMEM;
1634         }
1635
1636         outdata->next = NULL;
1637         outdata->bufp = outdata->text;
1638
1639         outbuf = outdata->text;
1640     }
1641
1642     status = (*pb->outfn) (pb->fnclosure, inbuf, outbuf + 2, size,
1643                            &translated);
1644     if (status != 0)
1645         return status;
1646
1647     /* The output function is permitted to add up to PACKET_SLOP
1648        bytes.  */
1649     assert (translated <= size + PACKET_SLOP);
1650
1651     outbuf[0] = (translated >> 8) & 0xff;
1652     outbuf[1] = translated & 0xff;
1653
1654     if (outbuf == stack_outbuf)
1655         buf_output (pb->buf, outbuf, translated + 2);
1656     else
1657     {
1658         outdata->size = translated + 2;
1659         buf_append_data (pb->buf, outdata, outdata);
1660     }
1661
1662     *wrote = have;
1663
1664     /* We will only be here because buf_send_output was called on the
1665        packetizing buffer.  That means that we should now call
1666        buf_send_output on the underlying buffer.  */
1667     return buf_send_output (pb->buf);
1668 }
1669
1670
1671
1672 /* Flush data to a packetizing buffer.  */
1673 static int
1674 packetizing_buffer_flush (void *closure)
1675 {
1676     struct packetizing_buffer *pb = closure;
1677
1678     /* Flush the underlying buffer.  Note that if the original call to
1679        buf_flush passed 1 for the BLOCK argument, then the buffer will
1680        already have been set into blocking mode, so we should always
1681        pass 0 here.  */
1682     return buf_flush (pb->buf, 0);
1683 }
1684
1685
1686
1687 /* The block routine for a packetizing buffer.  */
1688 static int
1689 packetizing_buffer_block (void *closure, bool block)
1690 {
1691     struct packetizing_buffer *pb = closure;
1692
1693     if (block)
1694         return set_block (pb->buf);
1695     else
1696         return set_nonblock (pb->buf);
1697 }
1698
1699
1700
1701 /* Return the file descriptor underlying any child buffers.  */
1702 static int
1703 packetizing_buffer_get_fd (void *closure)
1704 {
1705     struct packetizing_buffer *cb = closure;
1706     return buf_get_fd (cb->buf);
1707 }
1708
1709
1710
1711 /* Shut down a packetizing buffer.  */
1712 static int
1713 packetizing_buffer_shutdown (struct buffer *buf)
1714 {
1715     struct packetizing_buffer *pb = buf->closure;
1716
1717     return buf_shutdown (pb->buf);
1718 }
1719
1720
1721
1722 /* All server communication goes through buffer structures.  Most of
1723    the buffers are built on top of a file descriptor.  This structure
1724    is used as the closure field in a buffer.  */
1725
1726 struct fd_buffer
1727 {
1728     /* The file descriptor.  */
1729     int fd;
1730     /* Nonzero if the file descriptor is in blocking mode.  */
1731     int blocking;
1732     /* The child process id when fd is a pipe.  */
1733     pid_t child_pid;
1734     /* The connection info, when fd is a pipe to a server.  */
1735     cvsroot_t *root;
1736 };
1737
1738 static int fd_buffer_input (void *, char *, size_t, size_t, size_t *);
1739 static int fd_buffer_output (void *, const char *, size_t, size_t *);
1740 static int fd_buffer_flush (void *);
1741 static int fd_buffer_block (void *, bool);
1742 static int fd_buffer_get_fd (void *);
1743 static int fd_buffer_shutdown (struct buffer *);
1744
1745 /* Initialize a buffer built on a file descriptor.  FD is the file
1746    descriptor.  INPUT is nonzero if this is for input, zero if this is
1747    for output.  MEMORY is the function to call when a memory error
1748    occurs.  */
1749
1750 struct buffer *
1751 fd_buffer_initialize (int fd, pid_t child_pid, cvsroot_t *root, bool input,
1752                       void (*memory) (struct buffer *))
1753 {
1754     struct fd_buffer *n;
1755
1756     n = xmalloc (sizeof *n);
1757     n->fd = fd;
1758     n->child_pid = child_pid;
1759     n->root = root;
1760     fd_buffer_block (n, true);
1761     return buf_initialize (input ? fd_buffer_input : NULL,
1762                            input ? NULL : fd_buffer_output,
1763                            input ? NULL : fd_buffer_flush,
1764                            fd_buffer_block, fd_buffer_get_fd,
1765                            fd_buffer_shutdown,
1766                            memory,
1767                            n);
1768 }
1769
1770
1771
1772 /* The buffer input function for a buffer built on a file descriptor.
1773  *
1774  * In non-blocking mode, this function will read as many bytes as it can in a
1775  * single try, up to SIZE bytes, and return.
1776  *
1777  * In blocking mode with NEED > 0, this function will read as many bytes as it
1778  * can but will not return until it has read at least NEED bytes.
1779  *
1780  * In blocking mode with NEED == 0, this function will block until it can read
1781  * either at least one byte or EOF, then read as many bytes as are available
1782  * and return.  At the very least, compress_buffer_shutdown depends on this
1783  * behavior to read EOF and can loop indefinitely without it.
1784  *
1785  * ASSUMPTIONS
1786  *   NEED <= SIZE.
1787  *
1788  * INPUTS
1789  *   closure    Our FD_BUFFER struct.
1790  *   data       The start of our input buffer.
1791  *   need       How many bytes our caller needs.
1792  *   size       How many bytes are available in DATA.
1793  *   got        Where to store the number of bytes read.
1794  *
1795  * OUTPUTS
1796  *   data       Filled with bytes read.
1797  *   *got       Number of bytes actually read into DATA.
1798  *
1799  * RETURNS
1800  *   errno      On error.
1801  *   -1         On EOF.
1802  *   0          Otherwise.
1803  *
1804  * ERRORS
1805  *   This function can return an error if fd_buffer_block(), or the system
1806  *   read() or select() calls do.
1807  */
1808 static int
1809 fd_buffer_input (void *closure, char *data, size_t need, size_t size,
1810                  size_t *got)
1811 {
1812     struct fd_buffer *fb = closure;
1813     int nbytes;
1814
1815     assert (need <= size);
1816
1817     *got = 0;
1818
1819     if (fb->blocking)
1820     {
1821         int status;
1822         fd_set readfds;
1823
1824         /* Set non-block.  */
1825         status = fd_buffer_block (fb, false);
1826         if (status != 0) return status;
1827
1828         FD_ZERO (&readfds);
1829         FD_SET (fb->fd, &readfds);
1830         do
1831         {
1832             int numfds;
1833
1834             do {
1835                 /* This used to select on exceptions too, but as far
1836                    as I know there was never any reason to do that and
1837                    SCO doesn't let you select on exceptions on pipes.  */
1838                 numfds = fd_select (fb->fd + 1, &readfds, NULL, NULL, NULL);
1839                 if (numfds < 0 && errno != EINTR)
1840                 {
1841                     status = errno;
1842                     goto block_done;
1843                 }
1844             } while (numfds < 0);
1845
1846             nbytes = read (fb->fd, data + *got, size - *got);
1847
1848             if (nbytes == 0)
1849             {
1850                 /* End of file.  This assumes that we are using POSIX or BSD
1851                    style nonblocking I/O.  On System V we will get a zero
1852                    return if there is no data, even when not at EOF.  */
1853                 if (*got)
1854                 {
1855                     /* We already read some data, so return no error, counting
1856                      * on the fact that we will read EOF again next time.
1857                      */
1858                     status = 0;
1859                     break;
1860                 }
1861                 else
1862                 {
1863                     /* Return EOF.  */
1864                     status = -1;
1865                     break;
1866                 }
1867             }
1868
1869             if (nbytes < 0)
1870             {
1871                 /* Some error occurred.  */
1872                 if (!blocking_error (errno))
1873                 {
1874                     status = errno;
1875                     break;
1876                 }
1877                 /* else Everything's fine, we just didn't get any data.  */
1878             }
1879
1880             *got += nbytes;
1881         } while (*got < need);
1882
1883 block_done:
1884         if (status == 0 || status == -1)
1885         {
1886             int newstatus;
1887
1888             /* OK or EOF - Reset block.  */
1889             newstatus = fd_buffer_block (fb, true);
1890             if (newstatus) status = newstatus;
1891         }
1892         return status;
1893     }
1894
1895     /* The above will always return.  Handle non-blocking read.  */
1896     nbytes = read (fb->fd, data, size);
1897
1898     if (nbytes > 0)
1899     {
1900         *got = nbytes;
1901         return 0;
1902     }
1903
1904     if (nbytes == 0)
1905         /* End of file.  This assumes that we are using POSIX or BSD
1906            style nonblocking I/O.  On System V we will get a zero
1907            return if there is no data, even when not at EOF.  */
1908         return -1;
1909
1910     /* Some error occurred.  */
1911     if (blocking_error (errno))
1912         /* Everything's fine, we just didn't get any data.  */
1913         return 0;
1914
1915     return errno;
1916 }
1917
1918
1919
1920 /* The buffer output function for a buffer built on a file descriptor.  */
1921
1922 static int
1923 fd_buffer_output (void *closure, const char *data, size_t have, size_t *wrote)
1924 {
1925     struct fd_buffer *fd = closure;
1926
1927     *wrote = 0;
1928
1929     while (have > 0)
1930     {
1931         int nbytes;
1932
1933         nbytes = write (fd->fd, data, have);
1934
1935         if (nbytes <= 0)
1936         {
1937             if (! fd->blocking
1938                 && (nbytes == 0 || blocking_error (errno)))
1939             {
1940                 /* A nonblocking write failed to write any data.  Just
1941                    return.  */
1942                 return 0;
1943             }
1944
1945             /* Some sort of error occurred.  */
1946
1947             if (nbytes == 0)
1948                 return EIO;
1949
1950             return errno;
1951         }
1952
1953         *wrote += nbytes;
1954         data += nbytes;
1955         have -= nbytes;
1956     }
1957
1958     return 0;
1959 }
1960
1961
1962
1963 /* The buffer flush function for a buffer built on a file descriptor.  */
1964 static int
1965 fd_buffer_flush (void *closure)
1966 {
1967     /* We don't need to do anything here.  Our fd doesn't have its own buffer
1968      * and syncing won't do anything but slow us down.
1969      *
1970      * struct fd_buffer *fb = closure;
1971      *
1972      * if (fsync (fb->fd) < 0 && errno != EROFS && errno != EINVAL)
1973      *     return errno;
1974      */
1975     return 0;
1976 }
1977
1978
1979
1980 static struct stat devnull;
1981 static int devnull_set = -1;
1982
1983 /* The buffer block function for a buffer built on a file descriptor.  */
1984 static int
1985 fd_buffer_block (void *closure, bool block)
1986 {
1987     struct fd_buffer *fb = closure;
1988 # if defined (F_GETFL) && defined (O_NONBLOCK) && defined (F_SETFL)
1989     int flags;
1990
1991     flags = fcntl (fb->fd, F_GETFL, 0);
1992     if (flags < 0)
1993         return errno;
1994
1995     if (block)
1996         flags &= ~O_NONBLOCK;
1997     else
1998         flags |= O_NONBLOCK;
1999
2000     if (fcntl (fb->fd, F_SETFL, flags) < 0)
2001     {
2002         /*
2003          * BSD returns ENODEV when we try to set block/nonblock on /dev/null.
2004          * BSDI returns ENOTTY when we try to set block/nonblock on /dev/null.
2005          */
2006         struct stat sb;
2007         int save_errno = errno;
2008         bool isdevnull = false;
2009
2010         if (devnull_set == -1)
2011             devnull_set = stat ("/dev/null", &devnull);
2012
2013         if (devnull_set >= 0)
2014             /* Equivalent to /dev/null ? */
2015             isdevnull = (fstat (fb->fd, &sb) >= 0
2016                          && sb.st_dev == devnull.st_dev
2017                          && sb.st_ino == devnull.st_ino
2018                          && sb.st_mode == devnull.st_mode
2019                          && sb.st_uid == devnull.st_uid
2020                          && sb.st_gid == devnull.st_gid
2021                          && sb.st_size == devnull.st_size
2022                          && sb.st_blocks == devnull.st_blocks
2023                          && sb.st_blksize == devnull.st_blksize);
2024         if (isdevnull)
2025             errno = 0;
2026         else
2027         {
2028             errno = save_errno;
2029             return errno;
2030         }
2031     }
2032 # endif /* F_GETFL && O_NONBLOCK && F_SETFL */
2033
2034     fb->blocking = block;
2035
2036     return 0;
2037 }
2038
2039
2040
2041 static int
2042 fd_buffer_get_fd (void *closure)
2043 {
2044     struct fd_buffer *fb = closure;
2045     return fb->fd;
2046 }
2047
2048
2049
2050 /* The buffer shutdown function for a buffer built on a file descriptor.
2051  *
2052  * This function disposes of memory allocated for this buffer.
2053  */
2054 static int
2055 fd_buffer_shutdown (struct buffer *buf)
2056 {
2057     struct fd_buffer *fb = buf->closure;
2058     struct stat s;
2059     bool closefd, statted;
2060
2061     /* Must be an open pipe, socket, or file.  What could go wrong? */
2062     if (fstat (fb->fd, &s) == -1) statted = false;
2063     else statted = true;
2064     /* Don't bother to try closing the FD if we couldn't stat it.  This
2065      * probably won't work.
2066      *
2067      * (buf_shutdown() on some of the server/child communication pipes is
2068      * getting EBADF on both the fstat and the close.  I'm not sure why -
2069      * perhaps they were alredy closed somehow?
2070      */
2071     closefd = statted;
2072
2073     /* Flush the buffer if possible.  */
2074     if (buf->flush)
2075     {
2076         buf_flush (buf, 1);
2077         buf->flush = NULL;
2078     }
2079
2080     if (buf->input)
2081     {
2082         /* There used to be a check here for unread data in the buffer of
2083          * the pipe, but it was deemed unnecessary and possibly dangerous.  In
2084          * some sense it could be second-guessing the caller who requested it
2085          * closed, as well.
2086          */
2087
2088 /* FIXME:
2089  *
2090  * This mess of #ifdefs is hard to read.  There must be some relation between
2091  * the macros being checked which at least deserves comments - if
2092  * SHUTDOWN_SERVER, NO_SOCKET_TO_FD, & START_RSH_WITH_POPEN_RW were completely
2093  * independant, then the next few lines could easily refuse to compile.
2094  *
2095  * The note below about START_RSH_WITH_POPEN_RW never being set when
2096  * SHUTDOWN_SERVER is defined means that this code would always break on
2097  * systems with SHUTDOWN_SERVER defined and thus the comment must now be
2098  * incorrect or the code was broken since the comment was written.
2099  */
2100 # ifdef SHUTDOWN_SERVER
2101         if (fb->root && fb->root->method != server_method)
2102 # endif
2103 # ifndef NO_SOCKET_TO_FD
2104         {
2105             /* shutdown() sockets */
2106             if (statted && S_ISSOCK (s.st_mode))
2107                 shutdown (fb->fd, 0);
2108         }
2109 # endif /* NO_SOCKET_TO_FD */
2110 # ifdef START_RSH_WITH_POPEN_RW
2111 /* Can't be set with SHUTDOWN_SERVER defined */
2112         /* FIXME: This is now certainly broken since pclose is defined by ANSI
2113          * C to accept a FILE * argument.  The switch will need to happen at a
2114          * higher abstraction level to switch between initializing stdio & fd
2115          * buffers on systems that need this (or maybe an fd buffer that keeps
2116          * track of the FILE * could be used - I think flushing the stream
2117          * before beginning exclusive access via the FD is OK.
2118          */
2119         else if (fb->root && pclose (fb->fd) == EOF)
2120         {
2121             error (1, errno, "closing connection to %s",
2122                    fb->root->hostname);
2123             closefd = false;
2124         }
2125 # endif /* START_RSH_WITH_POPEN_RW */
2126
2127         buf->input = NULL;
2128     }
2129     else if (buf->output)
2130     {
2131 # ifdef SHUTDOWN_SERVER
2132         /* FIXME:  Should have a SHUTDOWN_SERVER_INPUT &
2133          * SHUTDOWN_SERVER_OUTPUT
2134          */
2135         if (fb->root && fb->root->method == server_method)
2136             SHUTDOWN_SERVER (fb->fd);
2137         else
2138 # endif
2139 # ifndef NO_SOCKET_TO_FD
2140         /* shutdown() sockets */
2141         if (statted && S_ISSOCK (s.st_mode))
2142             shutdown (fb->fd, 1);
2143 # else
2144         {
2145         /* I'm not sure I like this empty block, but the alternative
2146          * is another nested NO_SOCKET_TO_FD switch as above.
2147          */
2148         }
2149 # endif /* NO_SOCKET_TO_FD */
2150
2151         buf->output = NULL;
2152     }
2153
2154     if (statted && closefd && close (fb->fd) == -1)
2155     {
2156         if (server_active)
2157         {
2158             /* Syslog this? */
2159         }
2160 # ifdef CLIENT_SUPPORT
2161         else if (fb->root)
2162             error (1, errno, "closing down connection to %s",
2163                    fb->root->hostname);
2164             /* EXITS */
2165 # endif /* CLIENT_SUPPORT */
2166
2167         error (0, errno, "closing down buffer");
2168     }
2169
2170     /* If we were talking to a process, make sure it exited */
2171     if (fb->child_pid)
2172     {
2173         int w;
2174
2175         do
2176             w = waitpid (fb->child_pid, NULL, 0);
2177         while (w == -1 && errno == EINTR);
2178         if (w == -1)
2179             error (1, errno, "waiting for process %d", fb->child_pid);
2180     }
2181
2182     free (buf->closure);
2183     buf->closure = NULL;
2184
2185     return 0;
2186 }
2187 #endif /* defined (SERVER_SUPPORT) || defined (CLIENT_SUPPORT) */