chiark / gitweb /
f389a77c3fce8bf6319bc4a2c6698dfe28e68dc9
[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 <sys/mman.h>
33 #include <assert.h>
34 #include <fcntl.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <unistd.h>
38
39 #ifndef RWBUFFER_SIZE_MB
40 #define RWBUFFER_SIZE_MB 16
41 #endif
42
43 #define BUFFER (RWBUFFER_SIZE_MB*1024*1024)
44 #define WAITEMPTY ((BUFFER*1)/4)
45
46 static inline int min(int a, int b) { return a<=b ? a : b; }
47
48 static void nonblock(int fd) {
49   int r;
50   r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
51   r |= O_NDELAY;
52   if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
53 }
54
55 int main(int argc, const char *const *argv) {
56   static unsigned char buf[BUFFER];
57   
58   unsigned char *wp, *rp;
59   int used,r,reading,seeneof;
60   fd_set readfds;
61   fd_set writefds;
62
63   used=0; wp=rp=buf; reading=1; seeneof=0;
64   nonblock(0); nonblock(1);
65
66   if (argv[1] && !strcmp(argv[1],"--mlock")) {
67     if (mlock(buf,sizeof(buf))) { perror("mlock"); exit(1); }
68     argv++; argc--;
69   }
70   if (argv[1]) { fputs("usage: readbuffer [--mlock]\n",stderr); exit(1); }
71
72   while (!seeneof || used) {
73     FD_ZERO(&readfds);
74     if (reading) {
75       if (used<BUFFER-1) {
76         FD_SET(0,&readfds);
77       } else {
78 /*fprintf(stderr,"\t buffers full - stopping\n");*/
79         reading=0;
80       }
81     }
82     FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
83 /*fprintf(stderr,"used %6d reading %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
84         used,reading,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
85         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
86     r= select(2,&readfds,&writefds,0,0);
87 /*fprintf(stderr,"\t readable %d writeable %d\n",
88         FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
89     if (r == -1) {
90       if (errno == EINTR) continue;
91       perror("select"); exit(1);
92     }
93     if (FD_ISSET(0,&readfds)) {
94       r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
95       if (!r) {
96 /*fprintf(stderr,"\t eof detected\n");*/
97         seeneof=1; reading=0;
98       } else if (r<0) {
99         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
100 /*fprintf(stderr,"\t read transient error\n");*/
101       } else {
102 /*fprintf(stderr,"\t read %d\n",r);*/
103         used+= r;
104         rp+= r;
105         if (rp == buf+BUFFER) rp=buf;
106       }
107     }
108     if (FD_ISSET(1,&writefds)) {
109       assert(used);
110       r= write(1,wp,min(used,buf+BUFFER-wp));
111       if (r<=0) {
112         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
113 /*fprintf(stderr,"\t write transient error\n");*/
114       } else {
115 /*fprintf(stderr,"\t wrote %d\n",r);*/
116         used-= r;
117         wp+= r;
118         if (wp == buf+BUFFER) wp=buf;
119       }
120       if (used < WAITEMPTY && !seeneof) {
121 /*fprintf(stderr,"\t starting writes\n");*/
122         reading=1;
123       }
124     }
125   }
126   exit(0);
127 }