chiark / gitweb /
preparing for trivsoundd
[chiark-utils.git] / backup / 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 2,
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, write to the Free Software
26  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  */
29
30 static size_t waitfill;
31 static int writing;
32
33 void wrbufcore_startup(void) {
34   waitfill= (buffersize*3)/4;
35   writing=0;
36 }
37
38 void wrbufcore_mainloop(void) {
39   int r;
40
41   while (!seeneof || used) {
42     
43     FD_ZERO(&readfds); if (!seeneof && used+1<buffersize) FD_SET(0,&readfds);
44     FD_ZERO(&writefds); if (writing) FD_SET(1,&writefds);
45
46     callselect();
47     
48     if (FD_ISSET(1,&writefds) && !FD_ISSET(0,&readfds) && !used) {
49       writing= 0;
50       FD_CLR(1,&writefds);
51     }
52
53     if (FD_ISSET(0,&readfds)) {
54       r= read(0,rp,min(buffersize-1-used,buf+buffersize-rp));
55       if (!r) {
56         seeneof=1; writing=1;
57       } else if (r<0) {
58         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
59       } else {
60         used+= r;
61         rp+= r;
62         if (rp == buf+buffersize) rp=buf;
63       }
64       if (used > waitfill) writing=1;
65     }
66
67     if (FD_ISSET(1,&writefds) && used) {
68       r= write(1,wp,min(used,buf+buffersize-wp));
69       if (r<=0) {
70         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
71       } else {
72         used-= r;
73         wp+= r;
74         if (wp == buf+buffersize) wp=buf;
75       }
76     }
77   }
78 }