chiark / gitweb /
ec03c6a390a08022529e12d3727b287e3327718b
[chiark-utils.git] / backup / readbuffer.c
1 /*
2  * readbuffer.c
3  *
4  * A program for reading input from devices which don't like constant
5  * stopping and starting, such as tape drives.  readbuffer is:
6  *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
7  *
8  * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
9  * 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 <assert.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <unistd.h>
37
38 #define BUFFER 16*1024*1024
39 #define WAITEMPTY ((BUFFER*1)/4)
40
41 static inline int min(int a, int b) { return a<=b ? a : b; }
42
43 static void nonblock(int fd) {
44   int r;
45   r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
46   r |= O_NDELAY;
47   if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
48 }
49
50 int main(int argc, const char *const *argv) {
51   static unsigned char buf[BUFFER];
52   
53   unsigned char *wp, *rp;
54   int used,r,reading,seeneof;
55   fd_set readfds;
56   fd_set writefds;
57
58   used=0; wp=rp=buf; reading=1; seeneof=0;
59   nonblock(0); nonblock(1);
60
61   if (argv[1] && !strcmp(argv[1],"--mlock")) {
62     if (mlock(buf,sizeof(buf))) { perror("mlock"); exit(1); }
63     argv++; argc--;
64   }
65   if (argv[1]) { fputs("usage: readbuffer [--mlock]\n",stderr); exit(1); }
66
67   while (!seeneof || used) {
68     FD_ZERO(&readfds);
69     if (reading) {
70       if (used<BUFFER-1) {
71         FD_SET(0,&readfds);
72       } else {
73 /*fprintf(stderr,"\t buffers full - stopping\n");*/
74         reading=0;
75       }
76     }
77     FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
78 /*fprintf(stderr,"used %6d reading %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
79         used,reading,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
80         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
81     r= select(2,&readfds,&writefds,0,0);
82 /*fprintf(stderr,"\t readable %d writeable %d\n",
83         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
84     if (r == -1) {
85       if (errno == EINTR) continue;
86       perror("select"); exit(1);
87     }
88     if (FD_ISSET(0,&readfds)) {
89       r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
90       if (!r) {
91 /*fprintf(stderr,"\t eof detected\n");*/
92         seeneof=1; reading=0;
93       } else if (r<0) {
94         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
95 /*fprintf(stderr,"\t read transient error\n");*/
96       } else {
97 /*fprintf(stderr,"\t read %d\n",r);*/
98         used+= r;
99         rp+= r;
100         if (rp == buf+BUFFER) rp=buf;
101       }
102     }
103     if (FD_ISSET(1,&writefds)) {
104       assert(used);
105       r= write(1,wp,min(used,buf+BUFFER-wp));
106       if (r<=0) {
107         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
108 /*fprintf(stderr,"\t write transient error\n");*/
109       } else {
110 /*fprintf(stderr,"\t wrote %d\n",r);*/
111         used-= r;
112         wp+= r;
113         if (wp == buf+BUFFER) wp=buf;
114       }
115       if (used < WAITEMPTY && !seeneof) {
116 /*fprintf(stderr,"\t starting writes\n");*/
117         reading=1;
118       }
119     }
120   }
121   exit(0);
122 }