nathan@0
|
1 |
/* cdrestore.c
|
nathan@0
|
2 |
Copyright (c) 2000-2002 Craig Condit, Stefan Hülswitt.
|
nathan@0
|
3 |
|
nathan@0
|
4 |
Redistribution and use in source and binary forms, with or without
|
nathan@0
|
5 |
modification, are permitted provided that the following conditions are met:
|
nathan@0
|
6 |
|
nathan@0
|
7 |
1. Redistributions of source code must retain the above copyright notice,
|
nathan@0
|
8 |
this list of conditions and the following disclaimer.
|
nathan@0
|
9 |
2. Redistributions in binary form must reproduce the above copyright notice,
|
nathan@0
|
10 |
this list of conditions and the following disclaimer in the documentation
|
nathan@0
|
11 |
and/or other materials provided with the distribution.
|
nathan@0
|
12 |
|
nathan@0
|
13 |
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
nathan@0
|
14 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
nathan@0
|
15 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
nathan@0
|
16 |
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
|
nathan@0
|
17 |
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
nathan@0
|
18 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
nathan@0
|
19 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
nathan@0
|
20 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
nathan@0
|
21 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
nathan@0
|
22 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
nathan@0
|
23 |
SUCH DAMAGE.
|
nathan@0
|
24 |
*/
|
nathan@0
|
25 |
|
nathan@0
|
26 |
#define _LARGEFILE64_SOURCE
|
nathan@0
|
27 |
|
nathan@0
|
28 |
#include <stdio.h>
|
nathan@0
|
29 |
#include <stdlib.h>
|
nathan@0
|
30 |
#include <string.h>
|
nathan@0
|
31 |
#include <unistd.h>
|
nathan@0
|
32 |
#include <fcntl.h>
|
nathan@0
|
33 |
#include <time.h>
|
nathan@0
|
34 |
#include <errno.h>
|
nathan@0
|
35 |
#include <getopt.h>
|
nathan@0
|
36 |
#include <sys/ioctl.h>
|
nathan@0
|
37 |
#include <sys/wait.h>
|
nathan@0
|
38 |
#include <netinet/in.h>
|
nathan@0
|
39 |
#include <linux/cdrom.h>
|
nathan@0
|
40 |
|
nathan@0
|
41 |
#include "cdbackup.h"
|
nathan@0
|
42 |
#include "cdrom.h"
|
nathan@0
|
43 |
#include "misc.h"
|
nathan@0
|
44 |
#include "version.h"
|
nathan@0
|
45 |
|
nathan@0
|
46 |
/* defaults */
|
nathan@0
|
47 |
char * prg_name ="cdrestore";
|
nathan@0
|
48 |
int cd_mode =0;
|
nathan@0
|
49 |
int cd_track =-1;
|
nathan@0
|
50 |
char * cd_dev ="/dev/cdrom";
|
nathan@0
|
51 |
long cd_len =333000; /* blocks */
|
nathan@0
|
52 |
char * multicmd =0;
|
nathan@0
|
53 |
int verbose =1;
|
nathan@0
|
54 |
|
nathan@0
|
55 |
int tracks;
|
nathan@0
|
56 |
long long totalSize;
|
nathan@0
|
57 |
struct header_block headersave;
|
nathan@0
|
58 |
|
nathan@0
|
59 |
/****************************************************************************/
|
nathan@0
|
60 |
|
nathan@0
|
61 |
void usage()
|
nathan@0
|
62 |
{
|
nathan@0
|
63 |
fprintf(stderr,
|
nathan@0
|
64 |
"Usage: %s [OPTION]...\n"
|
nathan@0
|
65 |
"Reads block input from CD-R(W) and writes it to standard output.\n\n"
|
nathan@0
|
66 |
" -d DEVICE DEVICE for CD queries (e.g. /dev/sr0)\n"
|
nathan@0
|
67 |
" -q query disk and print TOC only\n"
|
nathan@0
|
68 |
" -t N restore from track N\n"
|
nathan@0
|
69 |
" -l N CD-R has a size of N MB\n"
|
nathan@0
|
70 |
" -c COMMAND call COMMAND on disk change in multi-disk mode\n"
|
nathan@0
|
71 |
" -V prints version & exits\n"
|
nathan@0
|
72 |
"\n", prg_name);
|
nathan@0
|
73 |
}
|
nathan@0
|
74 |
|
nathan@0
|
75 |
/****************************************************************************/
|
nathan@0
|
76 |
|
nathan@0
|
77 |
void parse_cmdline(char argc, char *argv[])
|
nathan@0
|
78 |
{
|
nathan@0
|
79 |
int i;
|
nathan@0
|
80 |
|
nathan@0
|
81 |
while ((i=getopt(argc,argv,"d:l:c:t:qV"))>0) {
|
nathan@0
|
82 |
switch (i) {
|
nathan@0
|
83 |
case 'V': fprintf(stderr,"cdrestore "VERSION" (compiled "__DATE__")\n"
|
nathan@0
|
84 |
"Copyright (C) 2000-2002\n"
|
nathan@0
|
85 |
"This is free software; see the source for copying conditions.\n"
|
nathan@0
|
86 |
"There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
|
nathan@0
|
87 |
"PARTICULAR PURPOSE.\n");
|
nathan@0
|
88 |
exit(0);
|
nathan@0
|
89 |
case 'c': multicmd=optarg; break;
|
nathan@0
|
90 |
case 'd': cd_dev=optarg; break;
|
nathan@0
|
91 |
case 'q': cd_mode=1; break;
|
nathan@0
|
92 |
case 't': errno=0; cd_track=strtol(optarg,NULL,10);
|
nathan@0
|
93 |
if(errno==ERANGE || cd_track<1) serror("Option -t: invalid track (must be >=1)\n");
|
nathan@0
|
94 |
break;
|
nathan@0
|
95 |
case 'l': errno=0; cd_len=strtol(optarg,NULL,10);
|
nathan@0
|
96 |
if(errno==ERANGE || cd_len<1) serror("Option -l: length out of range (must be >=1)\n");
|
nathan@0
|
97 |
cd_len = (long long)cd_len * (1024*1024) / CD_FRAMESIZE; /* convert to blocks */
|
nathan@0
|
98 |
break;
|
nathan@0
|
99 |
default: usage(); exit(0);
|
nathan@0
|
100 |
}
|
nathan@0
|
101 |
}
|
nathan@0
|
102 |
|
nathan@0
|
103 |
if(!cd_mode && cd_track<0) /* need track number */
|
nathan@0
|
104 |
serror("A track number is required.\n");
|
nathan@0
|
105 |
}
|
nathan@0
|
106 |
|
nathan@0
|
107 |
/****************************************************************************/
|
nathan@0
|
108 |
|
nathan@0
|
109 |
void print_track(int track, char *stamp, char *id, int disk, int startsec, int endsec)
|
nathan@0
|
110 |
{
|
nathan@0
|
111 |
char timestr[32], size[32];
|
nathan@0
|
112 |
|
nathan@0
|
113 |
snprintf(timestr,sizeof(timestr)-1,"%02d/%02d/%04d %02d:%02d",
|
nathan@0
|
114 |
(stamp[4]-'0')*10 + (stamp[5]-'0'),
|
nathan@0
|
115 |
(stamp[6]-'0')*10 + (stamp[7]-'0'),
|
nathan@0
|
116 |
(stamp[0]-'0')*1000 + (stamp[1]-'0')*100 + (stamp[2]-'0')*10 + (stamp[3]-'0'),
|
nathan@0
|
117 |
(stamp[8]-'0')*10 + (stamp[9]-'0'),
|
nathan@0
|
118 |
(stamp[10]-'0')*10 + (stamp[11]-'0'));
|
nathan@0
|
119 |
|
nathan@0
|
120 |
if(startsec>=0) snprintf(size,sizeof(size)-1," %3ld MB:",(long)((long long)(endsec-startsec)*CD_FRAMESIZE/(1024*1024)));
|
nathan@0
|
121 |
else size[0]=0;
|
nathan@0
|
122 |
|
nathan@0
|
123 |
fprintf(stderr,"Track %02d:%s %s Part %d: %s\n", track, size, timestr, disk, id);
|
nathan@0
|
124 |
}
|
nathan@0
|
125 |
|
nathan@0
|
126 |
/****************************************************************************/
|
nathan@0
|
127 |
|
nathan@0
|
128 |
int restore(int disknum, int disktrack)
|
nathan@0
|
129 |
{
|
nathan@0
|
130 |
int infile;
|
nathan@0
|
131 |
int result=0, i, bytes;
|
nathan@0
|
132 |
long long totalRead;
|
nathan@0
|
133 |
struct header_block header;
|
nathan@0
|
134 |
char buffer[CD_FRAMESIZE];
|
nathan@0
|
135 |
struct data_block *db=(struct data_block *)&buffer[0];
|
nathan@0
|
136 |
|
nathan@0
|
137 |
if ((infile = open(cd_dev, O_RDONLY)) < 0) error("Error opening device");
|
nathan@0
|
138 |
|
nathan@0
|
139 |
/* seek to proper CD-R(W) track */
|
nathan@0
|
140 |
for(i=tracks;i>0;i--) if(toc[i].track_no==disktrack) break;
|
nathan@0
|
141 |
if(!i) { fprintf(stderr, "%s: Can't find track %d\n", prg_name, disktrack); exit(1); }
|
nathan@0
|
142 |
|
nathan@0
|
143 |
totalRead=(long long)toc[i].sec_start*CD_FRAMESIZE;
|
nathan@0
|
144 |
if(lseek64(infile,totalRead,SEEK_SET) != totalRead) error("Error seeking to track");
|
nathan@0
|
145 |
|
nathan@0
|
146 |
/* read header block */
|
nathan@0
|
147 |
bytes=full_read(infile,buffer,CD_FRAMESIZE);
|
nathan@0
|
148 |
if (bytes < 0) error("Error reading header block");
|
nathan@0
|
149 |
if (bytes != CD_FRAMESIZE) error("Unexpected EOF reading header block");
|
nathan@0
|
150 |
totalRead = bytes;
|
nathan@0
|
151 |
|
nathan@0
|
152 |
memcpy(&header,buffer,sizeof(header));
|
nathan@0
|
153 |
|
nathan@0
|
154 |
if(!strncmp(SHORT_HDR,header.id_str,strlen(SHORT_HDR))) {
|
nathan@0
|
155 |
fprintf(stderr,"%s: ", prg_name);
|
nathan@0
|
156 |
print_track(disktrack, header.t_stamp, header.vol_id, header.disk_set, -1, -1);
|
nathan@0
|
157 |
|
nathan@0
|
158 |
if(disknum==1) {
|
nathan@0
|
159 |
if(header.disk_set!=1) {
|
nathan@0
|
160 |
fprintf(stderr,"%s: This is disk %d of a multi-disk set. Restore can only be started with disk 1!\n",prg_name,header.disk_set);
|
nathan@0
|
161 |
exit(1);
|
nathan@0
|
162 |
}
|
nathan@0
|
163 |
headersave=header; /* save header for use with disk 2-n */
|
nathan@0
|
164 |
}
|
nathan@0
|
165 |
else {
|
nathan@0
|
166 |
if(strcmp(header.t_stamp,headersave.t_stamp) || strcmp(header.vol_id,headersave.vol_id)) {
|
nathan@0
|
167 |
fprintf(stderr,"%s: This disk doesn't belong to the current set!\n",prg_name);
|
nathan@0
|
168 |
result=2;
|
nathan@0
|
169 |
}
|
nathan@0
|
170 |
else if(header.disk_set!=disknum) {
|
nathan@0
|
171 |
fprintf(stderr,"%s: Wrong sequence. You need disk %d now!\n",prg_name,disknum);
|
nathan@0
|
172 |
result=2;
|
nathan@0
|
173 |
}
|
nathan@0
|
174 |
else fprintf(stderr, "%s: Beginning restore (Disk %d)\n", prg_name,disknum);
|
nathan@0
|
175 |
}
|
nathan@0
|
176 |
}
|
nathan@0
|
177 |
else {
|
nathan@0
|
178 |
fprintf(stderr, "%s: Track %02d was not created with 'cdbackup'\n", prg_name,disktrack);
|
nathan@0
|
179 |
if(disknum==1) exit(1);
|
nathan@0
|
180 |
result=2;
|
nathan@0
|
181 |
}
|
nathan@0
|
182 |
|
nathan@0
|
183 |
while(!result) {
|
nathan@0
|
184 |
int size;
|
nathan@0
|
185 |
|
nathan@0
|
186 |
/* read data block */
|
nathan@0
|
187 |
bytes = full_read(infile, buffer, CD_FRAMESIZE);
|
nathan@0
|
188 |
if (bytes < 0) error("Error reading data");
|
nathan@0
|
189 |
if (bytes != CD_FRAMESIZE) error("Unexpected EOF reading data");
|
nathan@0
|
190 |
totalRead += bytes;
|
nathan@0
|
191 |
|
nathan@0
|
192 |
/* sanity check */
|
nathan@0
|
193 |
size=ntohs(db->datasize);
|
nathan@0
|
194 |
if(size>DATASIZE) {
|
nathan@0
|
195 |
fprintf(stderr,"%s: Warning! Bad datasize at %lld\n",prg_name,totalRead);
|
nathan@0
|
196 |
size=DATASIZE;
|
nathan@0
|
197 |
}
|
nathan@0
|
198 |
|
nathan@0
|
199 |
/* write the data block */
|
nathan@0
|
200 |
bytes=write(1,&buffer[DBSIZE], size);
|
nathan@0
|
201 |
if(bytes!=size) error("Error writing data");
|
nathan@0
|
202 |
|
nathan@0
|
203 |
if(db->status == 1) break; /* end of backup*/
|
nathan@0
|
204 |
if(db->status == 2) result=1; /* disk full */
|
nathan@0
|
205 |
}
|
nathan@0
|
206 |
|
nathan@0
|
207 |
/* print status */
|
nathan@0
|
208 |
totalSize+=totalRead;
|
nathan@0
|
209 |
if(result!=2) fprintf(stderr, "%s: Restore complete. %lld kB read (%lld kB from this disk)\n",prg_name, totalSize/1024, totalRead/1024);
|
nathan@0
|
210 |
|
nathan@0
|
211 |
close(infile);
|
nathan@0
|
212 |
return(result);
|
nathan@0
|
213 |
}
|
nathan@0
|
214 |
|
nathan@0
|
215 |
/****************************************************************************/
|
nathan@0
|
216 |
|
nathan@0
|
217 |
void print_toc()
|
nathan@0
|
218 |
{
|
nathan@0
|
219 |
int i;
|
nathan@0
|
220 |
|
nathan@0
|
221 |
fprintf(stderr,"Tracks: %d\n",tracks);
|
nathan@0
|
222 |
print_space();
|
nathan@0
|
223 |
fprintf(stderr,"\n");
|
nathan@0
|
224 |
|
nathan@0
|
225 |
for (i = 1; i <= tracks; i++) {
|
nathan@0
|
226 |
if(toc[i].is_data==0) fprintf(stderr,"Track %02d: Non-data\n", toc[i].track_no);
|
nathan@0
|
227 |
else if (toc[i].is_cdbackup == 1)
|
nathan@0
|
228 |
print_track(i, toc[i].t_stamp, toc[i].vol_id, toc[i].disk_set, toc[i].sec_start, i==tracks?toc[0].sec_start:toc[i+1].sec_start);
|
nathan@0
|
229 |
else fprintf(stderr,"Track %02d: Data\n", toc[i].track_no);
|
nathan@0
|
230 |
}
|
nathan@0
|
231 |
}
|
nathan@0
|
232 |
|
nathan@0
|
233 |
/****************************************************************************/
|
nathan@0
|
234 |
|
nathan@0
|
235 |
int main(int argc, char *argv[])
|
nathan@0
|
236 |
{
|
nathan@0
|
237 |
int cdr;
|
nathan@0
|
238 |
|
nathan@0
|
239 |
parse_cmdline(argc, argv);
|
nathan@0
|
240 |
|
nathan@0
|
241 |
if(!cd_mode) {
|
nathan@0
|
242 |
int disknum=1, result;
|
nathan@0
|
243 |
totalSize=0;
|
nathan@0
|
244 |
do {
|
nathan@0
|
245 |
cdr=open_cdr(cd_dev); tracks=read_toc(cdr,0); close_cdr(cdr);
|
nathan@0
|
246 |
|
nathan@0
|
247 |
result=restore(disknum,cd_track);
|
nathan@0
|
248 |
if(result) {
|
nathan@0
|
249 |
if(result==1) { disknum++; cd_track=1; }
|
nathan@0
|
250 |
fprintf(stderr,"%s: Next disk needed: disk %d from %s\n",prg_name,disknum,headersave.vol_id);
|
nathan@0
|
251 |
if(start_diskchange(multicmd,cd_dev)) serror("Diskchange command failed");
|
nathan@0
|
252 |
}
|
nathan@0
|
253 |
} while(result);
|
nathan@0
|
254 |
}
|
nathan@0
|
255 |
else {
|
nathan@0
|
256 |
cdr=open_cdr(cd_dev); tracks=read_toc(cdr,1); close_cdr(cdr);
|
nathan@0
|
257 |
print_toc();
|
nathan@0
|
258 |
}
|
nathan@0
|
259 |
|
nathan@0
|
260 |
return 0;
|
nathan@0
|
261 |
}
|