chiark / gitweb /
cgi-fcgi-interp: Constify stage2
[chiark-utils.git] / cprogs / wrbufcore.c
1 /*
2  * wrbufcore.c
3  *
4  * Core of algorithm for writing output to devices which don't like
5  * constant stopping and starting, such as tape drives.  This is:
6  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
7  *
8  * writebuffer is part of chiark backup, a system for backing up GNU/Linux
9  * and other UN*X-compatible machines, as used on chiark.greenend.org.uk.
10  * chiark backup is:
11  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
12  *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
13  *
14  * This is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 3,
17  * or (at your option) any later version.
18  *
19  * This is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public
25  * License along with this file; if not, consult the Free Software
26  * Foundation's website at www.fsf.org, or the GNU Project website at
27  * www.gnu.org.
28  *
29  */
30
31 #include "rwbuffer.h"
32
33 static size_t waitfill;
34
35 int writing;
36
37 void wrbufcore_startup(void) {
38   waitfill= (buffersize*3)/4;
39   writing=0;
40   maxselfd=0;
41 }
42
43 void fdsetset(int fd, fd_set *set) {
44   FD_SET(fd,set);
45   if (fd >= maxselfd) maxselfd= fd+1;
46 }
47
48 void wrbufcore_prepselect(int rdfd, int wrfd) {
49   FD_ZERO(&readfds);
50   if (rdfd>=0 && !seeneof && used+1<buffersize) fdsetset(rdfd,&readfds);
51   
52   FD_ZERO(&writefds);
53   if (writing) fdsetset(wrfd,&writefds);
54 }
55
56 void wrbufcore_afterselect(int rdfd, int wrfd) {
57   int r;
58     
59   if (FD_ISSET(wrfd,&writefds) &&
60       !(rdfd>=0 && FD_ISSET(rdfd,&readfds)) &&
61       !used) {
62     wrbuf_report("stopping");
63     writing= 0;
64     FD_CLR(wrfd,&writefds);
65   }
66
67   if (rdfd>=0 && FD_ISSET(rdfd,&readfds)) {
68     r= read(rdfd,rp,min(buffersize-1-used,buf+buffersize-rp));
69     if (!r) {
70       seeneof=1; writing=1;
71       wrbuf_report("seeneof");
72     } else if (r<0) {
73       if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
74     } else {
75       used+= r;
76       rp+= r;
77       if (rp == buf+buffersize) rp=buf;
78     }
79     if (used > waitfill) {
80       if (!writing) wrbuf_report("starting");
81       writing=1;
82     }
83   }
84
85   if (FD_ISSET(wrfd,&writefds) && used) {
86     r= write(wrfd,wp,min(used,buf+buffersize-wp));
87     if (r<=0) {
88       if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
89     } else {
90       used-= r;
91       wp+= r;
92       if (wp == buf+buffersize) wp=buf;
93     }
94   }
95 }