1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/cdbackup.c Sat Dec 29 15:22:32 2007 +0100
1.3 @@ -0,0 +1,298 @@
1.4 +/* cdbackup.c
1.5 +Copyright (c) 2000-2002 Craig Condit, Stefan Hülswitt.
1.6 +
1.7 +Redistribution and use in source and binary forms, with or without
1.8 +modification, are permitted provided that the following conditions are met:
1.9 +
1.10 +1. Redistributions of source code must retain the above copyright notice,
1.11 + this list of conditions and the following disclaimer.
1.12 +2. Redistributions in binary form must reproduce the above copyright notice,
1.13 + this list of conditions and the following disclaimer in the documentation
1.14 + and/or other materials provided with the distribution.
1.15 +
1.16 +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
1.17 +OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.18 +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1.19 +DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
1.20 +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.21 +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1.22 +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
1.23 +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.24 +LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.25 +OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.26 +SUCH DAMAGE.
1.27 +*/
1.28 +
1.29 +#define _LARGEFILE64_SOURCE
1.30 +
1.31 +#include <stdlib.h>
1.32 +#include <stdio.h>
1.33 +#include <unistd.h>
1.34 +#include <fcntl.h>
1.35 +#include <string.h>
1.36 +#include <time.h>
1.37 +#include <errno.h>
1.38 +#include <getopt.h>
1.39 +#include <sys/wait.h>
1.40 +#include <sys/ioctl.h>
1.41 +#include <netinet/in.h>
1.42 +#include <linux/cdrom.h>
1.43 +
1.44 +#include "cdbackup.h"
1.45 +#include "cdrom.h"
1.46 +#include "misc.h"
1.47 +#include "version.h"
1.48 +
1.49 +/* #define DEBUGOUT */
1.50 +
1.51 +/* defaults */
1.52 +char * prg_name ="cdbackup";
1.53 +char * cd_dev ="/dev/burner";
1.54 +char * cdr_dev =0; /* no default here, too dangerous */
1.55 +char * cd_label ="CDBackup Track";
1.56 +int cd_speed =4;
1.57 +long cd_len =333000; /* blocks */
1.58 +int padsize =15; /* blocks */
1.59 +int multidisk=0;
1.60 +char * multicmd =0;
1.61 +int verbose =0;
1.62 +int xamode2 =0;
1.63 +
1.64 +char **cdrec_opt=0;
1.65 +int cdrec_opt_count=0;
1.66 +
1.67 +long long totalSize;
1.68 +struct tm curtime; /* global, so multi-disks get all the same time */
1.69 +
1.70 +/****************************************************************************/
1.71 +
1.72 +void usage()
1.73 +{
1.74 + fprintf(stderr,
1.75 + "Usage: %s [options ...] [-- cdrecord-options ...]\n"
1.76 + "Reads from standard input, block formats and writes to CD-R(W).\n\n"
1.77 + " -d DEVICE DEVICE for CD queries (default /dev/burner)\n"
1.78 + " -l N CD-R has a size of N MB (default 650)\n"
1.79 + " -r DEVICE DEVICE for CD recording (e.g. 0,4,0)\n"
1.80 + " -s N record CD at speed N (default 4)\n"
1.81 + " -X enable CDROM XA2 mode in cdrecord\n"
1.82 + " -a LABEL use LABEL as CD session title\n"
1.83 + " -p N use a padsize of N sectors for the session (default 15)\n"
1.84 + " -m enable multi-disk mode\n"
1.85 + " -c COMMAND call COMMAND on disk change in multi-disk mode\n"
1.86 + " -v be verbose\n"
1.87 + " -V prints version & exits\n"
1.88 + " -- pass rest of commandline to cdrecord\n"
1.89 + "\n", prg_name);
1.90 +}
1.91 +
1.92 +/****************************************************************************/
1.93 +
1.94 +void parse_cmdline(char argc, char *argv[])
1.95 +{
1.96 + int i;
1.97 +
1.98 + while ((i=getopt(argc,argv,"d:r:l:s:p:a:c:mvVX"))>0) {
1.99 + switch (i) {
1.100 + case 'V': fprintf(stderr,"cdbackup "VERSION" (compiled "__DATE__")\n"
1.101 + "Copyright (C) 2000-2002\n"
1.102 + "This is free software; see the source for copying conditions.\n"
1.103 + "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
1.104 + "PARTICULAR PURPOSE.\n");
1.105 + exit(0);
1.106 + case 'v': verbose=1; break;
1.107 + case 'm': multidisk=1; break;
1.108 + case 'X': xamode2=1; break;
1.109 + case 'c': multicmd=optarg; break;
1.110 + case 'd': cd_dev=optarg; break;
1.111 + case 'r': cdr_dev=optarg; break;
1.112 + case 'a': cd_label=optarg; break;
1.113 + case 'l': errno=0; cd_len=strtol(optarg,NULL,10);
1.114 + if(errno==ERANGE || cd_len<1) serror("Option -l: length out of range (must be >=1)\n");
1.115 + cd_len = (long long)cd_len * (1024*1024) / CD_FRAMESIZE; /* convert to blocks */
1.116 + break;
1.117 + case 's': errno=0; cd_speed=strtol(optarg,NULL,10);
1.118 + if(errno==ERANGE || cd_speed<1) serror("Option -s: speed out of range (must be >=1)\n");
1.119 + break;
1.120 + case 'p': errno=0; padsize=strtol(optarg,NULL,10);
1.121 + if(errno==ERANGE || padsize<15) serror("Option -p: padsize out of range (must be >=15)\n");
1.122 + break;
1.123 + default: usage(); exit(0);
1.124 + }
1.125 + }
1.126 +
1.127 + if(optind<argc) { /* save position/count of cdrecord options */
1.128 + cdrec_opt_count=argc-optind;
1.129 + cdrec_opt=&argv[optind];
1.130 + }
1.131 +
1.132 + if(!cdr_dev) serror("You must specify a device for cdrecord with -r\n");
1.133 +}
1.134 +
1.135 +/****************************************************************************/
1.136 +
1.137 +#define MARG(ptr,len,form,arg) { int l=(len);\
1.138 + if(!(*ptr=(char *)malloc(l+1))) serror("No memory for cdrecord args\n");\
1.139 + snprintf(*ptr++,l,form,arg);\
1.140 + }
1.141 +
1.142 +void start_cdrecord()
1.143 +{
1.144 + char **args, **p;
1.145 + int l;
1.146 +
1.147 + if(!(p=args=calloc(cdrec_opt_count+8,sizeof(char *))))
1.148 + serror("No memory for cdrecord args\n");
1.149 +
1.150 + *p++="cdrecord";
1.151 + *p++="-multi";
1.152 + MARG(p,16,"speed=%d",cd_speed);
1.153 + MARG(p,6+strlen(cdr_dev),"dev=%s",cdr_dev);
1.154 +
1.155 + for(l=0 ; l<cdrec_opt_count ; l++) *p++=cdrec_opt[l];
1.156 +
1.157 + MARG(p,20,"padsize=%ds",padsize);
1.158 + if(xamode2) *p++="-xa2"; else *p++="-data";
1.159 + *p++="-";
1.160 + *p++=0;
1.161 +
1.162 + if(verbose) {
1.163 + fprintf(stderr,"%s: cdrecord command:",prg_name);
1.164 + for(p=args ; *p ; p++) fprintf(stderr," %s",*p);
1.165 + fprintf(stderr,"\n");
1.166 + }
1.167 +
1.168 + execvp("cdrecord",args);
1.169 + error("Unable to launch cdrecord");
1.170 +}
1.171 +
1.172 +/****************************************************************************/
1.173 +
1.174 +int backup(char disk_set)
1.175 +{
1.176 + pid_t childpid;
1.177 + int fd[2];
1.178 + int outpipe, bytes;
1.179 + long long grandTotal=0;
1.180 + struct header_block header;
1.181 +
1.182 + char buffer[CD_FRAMESIZE];
1.183 + struct data_block *db=(struct data_block *)&buffer[0];
1.184 +
1.185 + sprintf(buffer, "%04d%02d%02d%02d%02d", curtime.tm_year + 1900,
1.186 + curtime.tm_mon + 1, curtime.tm_mday, curtime.tm_hour, curtime.tm_min);
1.187 +
1.188 + strncpy(header.id_str,HDR_STRING,32); header.id_str[32] = 0;
1.189 + strncpy(header.vol_id,cd_label,32); header.vol_id[32] = 0;
1.190 + strncpy(header.t_stamp,buffer,12); header.t_stamp[12] = 0;
1.191 + header.disk_set = disk_set;
1.192 +
1.193 + if(verbose)
1.194 + fprintf(stderr,"%s: Recording to device %s, multidisk %s, disk %d\n",prg_name,cdr_dev,multidisk?"enabled":"disabled",disk_set);
1.195 +
1.196 +#ifndef DEBUGOUT /* the "real" code */
1.197 + /* launch cdrecord */
1.198 + if(pipe(fd) == -1) error("Unable to create pipe handles");
1.199 + if((childpid=fork()) == -1) error("Fork failed");
1.200 + if(childpid == 0) { /* child */
1.201 + close(fd[1]);
1.202 + close(0); /* stdin */
1.203 + dup2(fd[0], 0);
1.204 + start_cdrecord(); /* doesn't return */
1.205 + }
1.206 +
1.207 + close(fd[0]); outpipe=fd[1];
1.208 +
1.209 + /* output the header block */
1.210 + memset(buffer,0,CD_FRAMESIZE);
1.211 + memcpy(buffer,&header,sizeof(struct header_block));
1.212 + if((bytes=write(outpipe, buffer, CD_FRAMESIZE)) != CD_FRAMESIZE) error("Error writing header block");
1.213 +
1.214 + cd_avail-=bytes; grandTotal+=bytes;
1.215 + /* account for the padsize */
1.216 + cd_avail-=padsize*CD_FRAMESIZE;
1.217 +#else
1.218 + /* debug code; send data to /dev/null. Don't need the pipe. */
1.219 + fprintf(stderr, "DEBUG CODE: sending data to /dev/null!\n");
1.220 + outpipe = open("/dev/null", O_WRONLY);
1.221 + if (outpipe < 0) { perror("/dev/null"); exit(1); }
1.222 +#endif
1.223 +
1.224 + db->reserved = 0;
1.225 +
1.226 + do {
1.227 + /* read a block */
1.228 + db->status = 0; /* this isn't the last block (for now) */
1.229 + bytes=full_read(0,&buffer[DBSIZE],DATASIZE);
1.230 + if (bytes < 0) error("Error reading input");
1.231 + if (bytes != DATASIZE) db->status=1; /* EOF, this is the last block */
1.232 + db->datasize = htons(bytes);
1.233 +
1.234 + /* check for free space */
1.235 + if(cd_avail < (CD_FRAMESIZE*2)) { /* less than 2 block free */
1.236 + if(db->status==0) db->status=2; /* if not last block, mark disk as full */
1.237 + }
1.238 +
1.239 + /* write a block */
1.240 + bytes = write(outpipe, buffer, CD_FRAMESIZE);
1.241 + if(bytes != CD_FRAMESIZE) error("Error writing data block");
1.242 +
1.243 + grandTotal+=bytes; cd_avail-=bytes;
1.244 + } while(db->status==0);
1.245 +
1.246 + /* close pipe and wait for child termination */
1.247 + close(outpipe);
1.248 + while (wait(0) != childpid);
1.249 +
1.250 + totalSize+=grandTotal;
1.251 + if(verbose) fprintf(stderr,"%s: Recording finished. %lld kB written (%lld kB on this disk)\n",
1.252 + prg_name,totalSize/1024,grandTotal/1024);
1.253 +
1.254 + if(db->status==2) return 1; /* disk was full */
1.255 + return 0;
1.256 +}
1.257 +
1.258 +/****************************************************************************/
1.259 +
1.260 +int main(int argc, char *argv[])
1.261 +{
1.262 + int cdr;
1.263 + int disknum, result, loop;
1.264 + time_t curtime_t;
1.265 +
1.266 + disknum=1; totalSize=0;
1.267 + curtime_t=time(0); curtime=*localtime(&curtime_t);
1.268 +
1.269 + parse_cmdline(argc,argv);
1.270 +
1.271 + do {
1.272 + do {
1.273 + cdr=open_cdr(cd_dev); result=read_toc(cdr,0); close_cdr(cdr);
1.274 + print_space();
1.275 + loop=1;
1.276 +
1.277 + if(disknum>1 && result!=0) {
1.278 + fprintf(stderr,"%s: Can't do multidisk continuation on non-empty disk! Try another disk\n", prg_name);
1.279 + if(start_diskchange(multicmd,cd_dev)) serror("Diskchange command failed");
1.280 + }
1.281 + else if(cd_avail<(padsize+MIN_BLOCKS)*CD_FRAMESIZE) {
1.282 + if(multidisk) {
1.283 + fprintf(stderr,"%s: Not enough free space on disk! Try another disk\n", prg_name);
1.284 + if(start_diskchange(multicmd,cd_dev)) serror("Diskchange command failed");
1.285 + }
1.286 + else serror("Not enough free space on disk");
1.287 + }
1.288 + else loop=0;
1.289 + } while(loop);
1.290 +
1.291 + result = backup(disknum);
1.292 + if(result == 1) {
1.293 + if(multidisk == 0) serror("Disk full, multi-disk not enabled. Aborting");
1.294 +
1.295 + disknum++;
1.296 + if(start_diskchange(multicmd,cd_dev)) serror("Diskchange command failed");
1.297 + }
1.298 + } while (result != 0);
1.299 +
1.300 + return 0;
1.301 +}