chiark / gitweb /
Working on debianisation and packaging.
[chiark-utils.git] / backup / writebuffer.c
1 /*
2  * writebuffer.c
3  *
4  * A program for writing output to devices which don't like constant
5  * stopping and starting, such as tape drives.  writebuffer 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 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <unistd.h>
36
37 #ifndef RWBUFFER_SIZE_MB
38 #define RWBUFFER_SIZE_MB 16
39 #endif
40
41 #define BUFFER (RWBUFFER_SIZE_MB*1024*1024)
42 #define WAITFILL ((BUFFER*3)/4)
43
44 static inline int min(int a, int b) { return a<=b ? a : b; }
45
46 static void nonblock(int fd) {
47   int r;
48   r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
49   r |= O_NDELAY;
50   if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
51 }
52
53 int main(int argc, const char *const *argv) {
54   static unsigned char buf[BUFFER];
55   
56   unsigned char *wp, *rp;
57   int used,r,writing,seeneof;
58   fd_set readfds;
59   fd_set writefds;
60
61   if (argv[1]) {
62     fputs("readbuffer: no arguments allowed\n", stderr);
63     exit(-1);
64   }
65
66   used=0; wp=rp=buf; writing=0; seeneof=0;
67   nonblock(0); nonblock(1);
68   while (!seeneof || used) {
69     FD_ZERO(&readfds); if (!seeneof && used+1<BUFFER) FD_SET(0,&readfds);
70     FD_ZERO(&writefds); if (writing) FD_SET(1,&writefds);
71 /*fprintf(stderr,"used %6d writing %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
72         used,writing,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
73         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
74     r= select(2,&readfds,&writefds,0,0);
75 /*fprintf(stderr,"\t readable %d writeable %d\n",
76         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
77     if (r == -1) {
78       if (errno == EINTR) continue;
79       perror("select"); exit(1);
80     }
81     if (FD_ISSET(1,&writefds) && !FD_ISSET(0,&readfds) && !used) {
82       writing= 0;
83       FD_CLR(1,&writefds);
84 /*fprintf(stderr,"\t buffers empty - stopping\n");*/
85     }
86     if (FD_ISSET(0,&readfds)) {
87       r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
88       if (!r) {
89 /*fprintf(stderr,"\t eof detected\n");*/
90         seeneof=1; writing=1;
91       } else if (r<0) {
92         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
93 fprintf(stderr,"\t read transient error\n");
94       } else {
95 /*fprintf(stderr,"\t read %d\n",r);*/
96         used+= r;
97         rp+= r;
98         if (rp == buf+BUFFER) rp=buf;
99 /*fprintf(stderr,"\t starting writes\n");*/
100       }
101       if (used > WAITFILL) writing=1;
102     }
103     if (FD_ISSET(1,&writefds) && used) {
104       r= write(1,wp,min(used,buf+BUFFER-wp));
105       if (r<=0) {
106         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
107 /*fprintf(stderr,"\t write transient error\n");*/
108       } else {
109 /*fprintf(stderr,"\t wrote %d\n",r);*/
110         used-= r;
111         wp+= r;
112         if (wp == buf+BUFFER) wp=buf;
113       }
114     }
115   }
116   exit(0);
117 }