From: ianmdlvl Date: Wed, 21 May 2003 20:42:36 +0000 (+0000) Subject: preparing for trivsoundd X-Git-Tag: branchpoint-trivsoundd~2 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=commitdiff_plain;h=2bac409b3db30f17b3144837394db7222f2a9cdd preparing for trivsoundd --- diff --git a/backup/rwbuffer.c b/backup/rwbuffer.c index e38d7db..d91a96e 100644 --- a/backup/rwbuffer.c +++ b/backup/rwbuffer.c @@ -100,8 +100,7 @@ void startup(const char *const *argv) { } buffersize= opt_buffersize*1024*1024; - buf= malloc(buffersize); - if (!buf) { perror("malloc buffer"); exit(6); } + buf= xmalloc(buffersize); if (opt_mlock) { if (mlock(buf,buffersize)) { perror("mlock"); exit(2); } @@ -112,6 +111,10 @@ void startup(const char *const *argv) { nonblock(0,1); nonblock(1,1); } +void *xmalloc(size_t sz) { + void *r= malloc(sz); if (!r) { perror("malloc"); exit(6); }; return r; +} + void callselect(void) { int r; diff --git a/backup/wrbufcore.c b/backup/wrbufcore.c new file mode 100644 index 0000000..260f6d2 --- /dev/null +++ b/backup/wrbufcore.c @@ -0,0 +1,78 @@ +/* + * wrbufcore.c + * + * Core of algorithm for writing output to devices which don't like + * constant stopping and starting, such as tape drives. This is: + * Copyright (C) 1997-1998,2000-2001 Ian Jackson + * + * writebuffer 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. + * + */ + +static size_t waitfill; +static int writing; + +void wrbufcore_startup(void) { + waitfill= (buffersize*3)/4; + writing=0; +} + +void wrbufcore_mainloop(void) { + int r; + + while (!seeneof || used) { + + FD_ZERO(&readfds); if (!seeneof && used+1 waitfill) writing=1; + } + + if (FD_ISSET(1,&writefds) && used) { + r= write(1,wp,min(used,buf+buffersize-wp)); + if (r<=0) { + if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); } + } else { + used-= r; + wp+= r; + if (wp == buf+buffersize) wp=buf; + } + } + } +} diff --git a/backup/writebuffer.c b/backup/writebuffer.c index 68e2940..9b1a368 100644 --- a/backup/writebuffer.c +++ b/backup/writebuffer.c @@ -38,51 +38,11 @@ const char *progname= "writebuffer"; -static size_t waitfill; - int main(int argc, const char *const *argv) { int r, writing; startup(argv); - waitfill= (buffersize*3)/4; - writing=0; - - while (!seeneof || used) { - - FD_ZERO(&readfds); if (!seeneof && used+1 waitfill) writing=1; - } - - if (FD_ISSET(1,&writefds) && used) { - r= write(1,wp,min(used,buf+buffersize-wp)); - if (r<=0) { - if (!(errno == EAGAIN || errno == EINTR)) { perror("write"); exit(1); } - } else { - used-= r; - wp+= r; - if (wp == buf+buffersize) wp=buf; - } - } - } + writebuf_startup(); + writebuf_mainloop(); exit(0); }