From 0de4a1a60d326fe8226491c58f27947a71c0dba9 Mon Sep 17 00:00:00 2001 From: ianmdlvl Date: Sun, 7 Oct 2001 23:27:29 +0000 Subject: [PATCH] @@ -1,8 +1,12 @@ 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 Sun, 7 Oct 2001 23:31:25 +0100 + -- # Local variables: # mode: debian-changelog --- TODO | 5 +- backup/Makefile | 6 ++- backup/readbuffer.c | 71 +++++++----------------- backup/rwbuffer.c | 125 +++++++++++++++++++++++++++++++++++++++++++ backup/rwbuffer.h | 45 ++++++++++++++++ backup/writebuffer.c | 69 +++++++----------------- debian/changelog | 8 ++- 7 files changed, 221 insertions(+), 108 deletions(-) create mode 100644 backup/rwbuffer.c create mode 100644 backup/rwbuffer.h diff --git a/TODO b/TODO index a048004..07b3ce0 100644 --- 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 diff --git a/backup/Makefile b/backup/Makefile index 0d8e634..ded6a8c 100644 --- a/backup/Makefile +++ b/backup/Makefile @@ -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) diff --git a/backup/readbuffer.c b/backup/readbuffer.c index f389a77..e48e78c 100644 --- a/backup/readbuffer.c +++ b/backup/readbuffer.c @@ -29,96 +29,63 @@ #include #include -#include #include #include #include #include #include -#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 + * + * 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 + * Copyright (C) 1999 Peter Maydell + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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] []\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 index 0000000..3684c39 --- /dev/null +++ b/backup/rwbuffer.h @@ -0,0 +1,45 @@ +/* + * rwbuffer.h + * common declarations for readbuffer/writebuffer + * + * readbuffer and writebuffer are: + * Copyright (C) 1997-1998,2000-2001 Ian Jackson + * + * 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 + * Copyright (C) 1999 Peter Maydell + * + * 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*/ diff --git a/backup/writebuffer.c b/backup/writebuffer.c index aaff8d7..013fa8a 100644 --- a/backup/writebuffer.c +++ b/backup/writebuffer.c @@ -34,82 +34,53 @@ #include #include -#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 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; } } } diff --git a/debian/changelog b/debian/changelog index fea10a9..11df0c1 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,12 @@ chiark-utils (0.99.0-1) experimental; urgency=low - + * Initial prepackaged release of chiark backup. - -- Ian Jackson 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 -- 2.30.2