chiark / gitweb /
@@ -1,8 +1,12 @@
authorianmdlvl <ianmdlvl>
Sun, 7 Oct 2001 23:27:29 +0000 (23:27 +0000)
committerianmdlvl <ianmdlvl>
Sun, 7 Oct 2001 23:27:29 +0000 (23:27 +0000)
 chiark-utils (0.99.0-1) experimental; urgency=low
-
+
   * Initial prepackaged release of chiark backup.
+
+  Significant changes since merge chiark/anarres/mnemeth:
+  * readbuffer/writebuffer command line arg for buffer size.
+  * readbuffer/writebuffer share common code.

- -- Ian Jackson <ian@davenant.greenend.org.uk>  Sun,  7 Oct 2001 23:31:25 +0100
+ --

 # Local variables:
 # mode: debian-changelog

TODO
backup/Makefile
backup/readbuffer.c
backup/rwbuffer.c [new file with mode: 0644]
backup/rwbuffer.h [new file with mode: 0644]
backup/writebuffer.c
debian/changelog

diff --git a/TODO b/TODO
index a048004af3e8735785148f298dc9e165dd3f7bb6..07b3ce004bf5ae179dd62036dfd7e9e6f74c83cd 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,11 +1,8 @@
 backup:
 
-read/writebuffer cmdline for amount
-read/writebuffer no cloneandhack
-read/writebuffer security --mlock
+read/writebuffer setuid --mlock
 whatisthis no cloneandhack
 whatisthis listing for cpio and dump archives
-debian/rules binary-indep
 configuration files sensibleness
 configuration files autogenerator
 documentation
index 0d8e6346165e01dc51a0ce0e79d2d0303878fd63..ded6a8c2aef82207048a5a9452f888269b1edc23 100644 (file)
@@ -49,6 +49,10 @@ SHAREFILES=  backuplib.pl
 
 all:           $(CTARGETS)
 
+readbuffer:                            readbuffer.o rwbuffer.o
+writebuffer:                           writebuffer.o rwbuffer.o
+readbuffer.o writebuffer.o rwbuffer.o: rwbuffer.h
+
 install:               all
                $(INSTALL_DIRECTORY) $(etcdir) $(bindir) $(sharedir)
                $(INSTALL_PROGRAM) $(CTARGETS) $(bindir)
@@ -58,7 +62,7 @@ install:              all
                $(INSTALL_SCRIPT) $(SHARESCRIPTS) $(sharedir)
 
 clean:
-               rm -f *~ ./#*#
+               rm -f *~ ./#*# *.o
 
 distclean realclean:   clean
                rm -f $(CTARGETS)
index f389a77c3fce8bf6319bc4a2c6698dfe28e68dc9..e48e78c6ff9fd2819e4747ddd6565ec9c269e9b8 100644 (file)
 
 #include <sys/time.h>
 #include <sys/types.h>
-#include <sys/mman.h>
 #include <assert.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <errno.h>
 #include <unistd.h>
 
-#ifndef RWBUFFER_SIZE_MB
-#define RWBUFFER_SIZE_MB 16
-#endif
+#include "rwbuffer.h"
 
-#define BUFFER (RWBUFFER_SIZE_MB*1024*1024)
-#define WAITEMPTY ((BUFFER*1)/4)
+const char *progname= "readbuffer";
 
-static inline int min(int a, int b) { return a<=b ? a : b; }
-
-static void nonblock(int fd) {
-  int r;
-  r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
-  r |= O_NDELAY;
-  if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
-}
+static size_t waitempty;
 
 int main(int argc, const char *const *argv) {
-  static unsigned char buf[BUFFER];
-  
-  unsigned char *wp, *rp;
-  int used,r,reading,seeneof;
-  fd_set readfds;
-  fd_set writefds;
-
-  used=0; wp=rp=buf; reading=1; seeneof=0;
-  nonblock(0); nonblock(1);
-
-  if (argv[1] && !strcmp(argv[1],"--mlock")) {
-    if (mlock(buf,sizeof(buf))) { perror("mlock"); exit(1); }
-    argv++; argc--;
-  }
-  if (argv[1]) { fputs("usage: readbuffer [--mlock]\n",stderr); exit(1); }
+  int r,reading;
 
+  startup(argv);
+  waitempty= (buffersize*1)/4;
+  reading=1;
+  
   while (!seeneof || used) {
+    
     FD_ZERO(&readfds);
     if (reading) {
-      if (used<BUFFER-1) {
+      if (used<buffersize-1) {
        FD_SET(0,&readfds);
       } else {
-/*fprintf(stderr,"\t buffers full - stopping\n");*/
        reading=0;
       }
     }
     FD_ZERO(&writefds); if (used) FD_SET(1,&writefds);
-/*fprintf(stderr,"used %6d reading %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
-        used,reading,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
-        FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
-    r= select(2,&readfds,&writefds,0,0);
-/*fprintf(stderr,"\t readable %d writeable %d\n",
-        FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
-    if (r == -1) {
-      if (errno == EINTR) continue;
-      perror("select"); exit(1);
-    }
+
+    callselect();
+
     if (FD_ISSET(0,&readfds)) {
-      r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
+      r= read(0,rp,min(buffersize-1-used,buf+buffersize-rp));
       if (!r) {
-/*fprintf(stderr,"\t eof detected\n");*/
         seeneof=1; reading=0;
       } else if (r<0) {
         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
-/*fprintf(stderr,"\t read transient error\n");*/
       } else {
-/*fprintf(stderr,"\t read %d\n",r);*/
         used+= r;
         rp+= r;
-        if (rp == buf+BUFFER) rp=buf;
+        if (rp == buf+buffersize) rp=buf;
       }
     }
+
     if (FD_ISSET(1,&writefds)) {
       assert(used);
-      r= write(1,wp,min(used,buf+BUFFER-wp));
+      r= write(1,wp,min(used,buf+buffersize-wp));
       if (r<=0) {
         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
-/*fprintf(stderr,"\t write transient error\n");*/
       } else {
-/*fprintf(stderr,"\t wrote %d\n",r);*/
         used-= r;
         wp+= r;
-        if (wp == buf+BUFFER) wp=buf;
+        if (wp == buf+buffersize) wp=buf;
       }
-      if (used < WAITEMPTY && !seeneof) {
-/*fprintf(stderr,"\t starting writes\n");*/
+      if (used < waitempty && !seeneof) {
        reading=1;
       }
     }
diff --git a/backup/rwbuffer.c b/backup/rwbuffer.c
new file mode 100644 (file)
index 0000000..e38d7db
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * rwbuffer.c
+ * common definitions for readbuffer/writebuffer
+ *
+ * readbuffer and writebuffer are:
+ *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
+ *
+ * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
+ * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
+ * chiark backup is:
+ *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
+ *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this file; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <assert.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <sys/mman.h>
+
+#include "rwbuffer.h"
+
+#ifndef RWBUFFER_SIZE_MB_DEF
+#define RWBUFFER_SIZE_MB_DEF 16
+#endif
+
+#ifndef RWBUFFER_SIZE_MB_MAX
+#define RWBUFFER_SIZE_MB_MAX 512
+#endif
+
+unsigned char *buf, *wp, *rp;
+int used, seeneof;
+size_t buffersize;
+fd_set readfds;
+fd_set writefds;
+
+int min(int a, int b) { return a<=b ? a : b; }
+
+static void usage(FILE *f) {
+  if (fprintf(f,"usage: %s [--mlock] [<megabytes>]\n",progname) < 0)
+    { perror("print usage"); exit(16); }
+}
+
+static void usageerr(const char *what) {
+  fprintf(stderr,"%s: bad usage: %s\n",progname,what);
+  usage(stderr);
+  exit(12);
+}
+
+static void nonblock(int fd, int yesno) {
+  int r;
+  r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(8); }
+  if (yesno) r |= O_NDELAY;
+  else r &= ~O_NDELAY;
+  if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(8); }
+}
+
+static void unnonblock(void) {
+  nonblock(0,0); nonblock(1,0);
+}
+
+void startup(const char *const *argv) {
+  const char *arg;
+  char *ep;
+  unsigned long opt_buffersize=RWBUFFER_SIZE_MB_DEF;
+  int opt_mlock=0;
+  
+  assert(argv[0]);
+  
+  while ((arg= *++argv)) {
+    if (!strcmp(arg,"--mlock")) {
+      opt_mlock= 1;
+    } else if (isdigit((unsigned char)arg[0])) {
+      opt_buffersize= strtoul(arg,&ep,0);
+      if (opt_buffersize > RWBUFFER_SIZE_MB_MAX)
+       usageerr("buffer size too big");
+    } else {
+      usageerr("invalid option");
+    }
+  }
+
+  buffersize= opt_buffersize*1024*1024;
+  buf= malloc(buffersize);
+  if (!buf) { perror("malloc buffer"); exit(6); }
+
+  if (opt_mlock) {
+    if (mlock(buf,buffersize)) { perror("mlock"); exit(2); }
+  }
+
+  used=0; wp=rp=buf; seeneof=0;
+  if (atexit(unnonblock)) { perror("atexit"); exit(16); }
+  nonblock(0,1); nonblock(1,1);
+}
+
+void callselect(void) {
+  int r;
+  
+  for (;;) {
+    r= select(2,&readfds,&writefds,0,0);
+    if (r != -1) return;
+    if (errno != EINTR) {
+      perror("select"); exit(4);
+    }
+  }
+}
diff --git a/backup/rwbuffer.h b/backup/rwbuffer.h
new file mode 100644 (file)
index 0000000..3684c39
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * rwbuffer.h
+ * common declarations for readbuffer/writebuffer
+ *
+ * readbuffer and writebuffer are:
+ *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
+ *
+ * readbuffer is part of chiark backup, a system for backing up GNU/Linux and
+ * other UN*X-compatible machines, as used on chiark.greenend.org.uk.
+ * chiark backup is:
+ *  Copyright (C) 1997-1998,2000-2001 Ian Jackson <ian@chiark.greenend.org.uk>
+ *  Copyright (C) 1999 Peter Maydell <pmaydell@chiark.greenend.org.uk>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this file; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef RWBUFFER_H
+#define RWBUFFER_H
+
+int min(int a, int b);
+void callselect(void);
+void startup(const char *const *argv);
+
+extern const char *progname; /* must be defined by main .c file */
+
+extern unsigned char *buf, *wp, *rp;
+extern int used, seeneof;
+extern size_t buffersize;
+extern fd_set readfds;
+extern fd_set writefds;
+
+#endif /*RWBUFFER_H*/
index aaff8d7859d48a30899fa93d21a3cf3158591597..013fa8a5909fa3586e63d49dee0908e709bf3dd2 100644 (file)
 #include <errno.h>
 #include <unistd.h>
 
-#ifndef RWBUFFER_SIZE_MB
-#define RWBUFFER_SIZE_MB 16
-#endif
+#include "rwbuffer.h"
 
-#define BUFFER (RWBUFFER_SIZE_MB*1024*1024)
-#define WAITFILL ((BUFFER*3)/4)
+const char *progname= "readbuffer";
 
-static inline int min(int a, int b) { return a<=b ? a : b; }
-
-static void nonblock(int fd) {
-  int r;
-  r= fcntl(fd,F_GETFL,0); if (r == -1) { perror("fcntl getfl"); exit(1); }
-  r |= O_NDELAY;
-  if (fcntl(fd,F_SETFL,r) == -1) { perror("fcntl setfl"); exit(1); }
-}
+static size_t waitfill;
 
 int main(int argc, const char *const *argv) {
-  static unsigned char buf[BUFFER];
-  
-  unsigned char *wp, *rp;
-  int used,r,writing,seeneof;
-  fd_set readfds;
-  fd_set writefds;
-
-  if (argv[1]) {
-    fputs("readbuffer: no arguments allowed\n", stderr);
-    exit(-1);
-  }
+  int r, writing;
 
-  used=0; wp=rp=buf; writing=0; seeneof=0;
-  nonblock(0); nonblock(1);
+  startup(argv);
+  waitfill= (buffersize*3)/4;
+  writing=0;
+  
   while (!seeneof || used) {
-    FD_ZERO(&readfds); if (!seeneof && used+1<BUFFER) FD_SET(0,&readfds);
+    
+    FD_ZERO(&readfds); if (!seeneof && used+1<buffersize) FD_SET(0,&readfds);
     FD_ZERO(&writefds); if (writing) FD_SET(1,&writefds);
-/*fprintf(stderr,"used %6d writing %d seeneof %d wp %8lx rp %8lx read %d write %d\n",
-        used,writing,seeneof,(unsigned long)(wp-buf),(unsigned long)(rp-buf),
-        FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
-    r= select(2,&readfds,&writefds,0,0);
-/*fprintf(stderr,"\t readable %d writeable %d\n",
-        FD_ISSET(0,&readfds),FD_ISSET(1,&writefds));*/
-    if (r == -1) {
-      if (errno == EINTR) continue;
-      perror("select"); exit(1);
-    }
+
+    callselect();
+    
     if (FD_ISSET(1,&writefds) && !FD_ISSET(0,&readfds) && !used) {
       writing= 0;
       FD_CLR(1,&writefds);
-/*fprintf(stderr,"\t buffers empty - stopping\n");*/
     }
+
     if (FD_ISSET(0,&readfds)) {
-      r= read(0,rp,min(BUFFER-1-used,buf+BUFFER-rp));
+      r= read(0,rp,min(buffersize-1-used,buf+buffersize-rp));
       if (!r) {
-/*fprintf(stderr,"\t eof detected\n");*/
         seeneof=1; writing=1;
       } else if (r<0) {
         if (!(errno == EAGAIN || errno == EINTR)) { perror("read"); exit(1); }
-fprintf(stderr,"\t read transient error\n");
       } else {
-/*fprintf(stderr,"\t read %d\n",r);*/
         used+= r;
         rp+= r;
-        if (rp == buf+BUFFER) rp=buf;
-/*fprintf(stderr,"\t starting writes\n");*/
+        if (rp == buf+buffersize) rp=buf;
       }
-      if (used > WAITFILL) writing=1;
+      if (used > waitfill) writing=1;
     }
+
     if (FD_ISSET(1,&writefds) && used) {
-      r= write(1,wp,min(used,buf+BUFFER-wp));
+      r= write(1,wp,min(used,buf+buffersize-wp));
       if (r<=0) {
         if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); }
-/*fprintf(stderr,"\t write transient error\n");*/
       } else {
-/*fprintf(stderr,"\t wrote %d\n",r);*/
         used-= r;
         wp+= r;
-        if (wp == buf+BUFFER) wp=buf;
+        if (wp == buf+buffersize) wp=buf;
       }
     }
   }
index fea10a9a5c254c0d1b69e2f0a791b2b3f0b06cf5..11df0c15abadacb136c63dd09f6be7c8ca130349 100644 (file)
@@ -1,8 +1,12 @@
 chiark-utils (0.99.0-1) experimental; urgency=low
-
+  
   * Initial prepackaged release of chiark backup.
 
- -- Ian Jackson <ian@davenant.greenend.org.uk>  Sun,  7 Oct 2001 23:31:25 +0100
+  Significant changes since merge chiark/anarres/mnemeth:
+  * readbuffer/writebuffer command line arg for buffer size.
+  * readbuffer/writebuffer share common code.
+
+ --
 
 # Local variables:
 # mode: debian-changelog