1 | /* libmondo-stream.c
|
---|
2 | $Id: libmondo-stream.c 1969 2008-05-29 17:36:58Z bruno $
|
---|
3 |
|
---|
4 | ...tools for talking to tapes, Monitas streams, etc.
|
---|
5 |
|
---|
6 | */
|
---|
7 |
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * @file
|
---|
11 | * Functions for writing data to/reading data from streams (tape, CD stream, etc.)
|
---|
12 | */
|
---|
13 |
|
---|
14 | #include "my-stuff.h"
|
---|
15 | #include "mondostructures.h"
|
---|
16 | #include "libmondo-devices.h"
|
---|
17 | #include "lib-common-externs.h"
|
---|
18 | #include "libmondo-stream.h"
|
---|
19 | #include "libmondo-string-EXT.h"
|
---|
20 | #include "libmondo-files-EXT.h"
|
---|
21 | #include "libmondo-gui-EXT.h"
|
---|
22 | #include "libmondo-fork-EXT.h"
|
---|
23 | #include "libmondo-tools-EXT.h"
|
---|
24 | #include "libmondo-fifo-EXT.h"
|
---|
25 |
|
---|
26 | #define EXTRA_TAPE_CHECKSUMS
|
---|
27 | #define STR_HEADER "Mondolicious, baby"
|
---|
28 |
|
---|
29 | /*@unused@*/
|
---|
30 | //static char cvsid[] = "$Id: libmondo-stream.c 1969 2008-05-29 17:36:58Z bruno $";
|
---|
31 | extern bool g_sigpipe;
|
---|
32 | extern int g_tape_buffer_size_MB;
|
---|
33 |
|
---|
34 | extern char *g_getfacl;
|
---|
35 | extern char *g_getfattr;
|
---|
36 | extern char *MONDO_LOGFILE;
|
---|
37 |
|
---|
38 | /* Reference to global bkpinfo */
|
---|
39 | extern struct s_bkpinfo *bkpinfo;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * @addtogroup globalGroup
|
---|
43 | * @{
|
---|
44 | */
|
---|
45 | /**
|
---|
46 | * The file pointer for the opened tape/CD stream.
|
---|
47 | * Opened and closed by openin_tape(), openin_cdstream(),
|
---|
48 | * openout_tape(), openout_cdstream(), closein_tape(), closein_cdstream(),
|
---|
49 | * closeout_tape(), and closeout_cdstream().
|
---|
50 | */
|
---|
51 | FILE *g_tape_stream = NULL;
|
---|
52 |
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The position (in kilobytes) where we are on the tape.
|
---|
56 | */
|
---|
57 | long long g_tape_posK = 0;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The current media number we're using. This value is 1-based.
|
---|
61 | */
|
---|
62 | int g_current_media_number = -1;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The tape catalog that keeps track of files written to tape.
|
---|
66 | */
|
---|
67 | struct s_tapecatalog *g_tapecatalog;
|
---|
68 |
|
---|
69 | /* @} - end of globalGroup */
|
---|
70 |
|
---|
71 | int write_backcatalog_to_tape();
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * @addtogroup streamGroup
|
---|
79 | * @{
|
---|
80 | */
|
---|
81 | /**
|
---|
82 | * Close the global output file descriptor which Mondo has used to read
|
---|
83 | * from the CD stream.
|
---|
84 | * @param bkpinfo The backup information structure. Passed directly to closein_tape().
|
---|
85 | * @return 0 for success, nonzero for failure.
|
---|
86 | * @note This should be called by restore processes only.
|
---|
87 | */
|
---|
88 | int closein_cdstream()
|
---|
89 | {
|
---|
90 | return (closein_tape());
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * Close the global output file descriptor which Mondo has used to read
|
---|
96 | * from buffer or dd, which read from the tape.
|
---|
97 | * @param bkpinfo The backup information structure. Unused.
|
---|
98 | * @return 0 for success, nonzero for failure.
|
---|
99 | * @note This should be called by restore processes only.
|
---|
100 | * @note This function also works for cdstreams for now, but don't count on this behavior.
|
---|
101 | * @bug @p bkpinfo parameter is unused.
|
---|
102 | */
|
---|
103 | int closein_tape()
|
---|
104 | {
|
---|
105 | /*@ int's ******************************************************* */
|
---|
106 | int retval = 0;
|
---|
107 | int res = 0;
|
---|
108 | int ctrl_chr = '\0';
|
---|
109 |
|
---|
110 | /*@ buffers ***************************************************** */
|
---|
111 | char fname[MAX_STR_LEN];
|
---|
112 |
|
---|
113 | /*@ long long's ************************************************* */
|
---|
114 | long long size;
|
---|
115 | char *blk;
|
---|
116 | int i;
|
---|
117 |
|
---|
118 | blk = (char *) malloc(256 * 1024);
|
---|
119 |
|
---|
120 | log_it("closein_tape() -- entering");
|
---|
121 | res = read_header_block_from_stream(&size, fname, &ctrl_chr);
|
---|
122 | retval += res;
|
---|
123 | if (ctrl_chr != BLK_END_OF_BACKUP) {
|
---|
124 | wrong_marker(BLK_END_OF_BACKUP, ctrl_chr);
|
---|
125 | }
|
---|
126 | res = read_header_block_from_stream(&size, fname, &ctrl_chr);
|
---|
127 | retval += res;
|
---|
128 | if (ctrl_chr != BLK_END_OF_TAPE) {
|
---|
129 | wrong_marker(BLK_END_OF_TAPE, ctrl_chr);
|
---|
130 | }
|
---|
131 | for (i = 0; i < 8 && !feof(g_tape_stream); i++) {
|
---|
132 | (void) fread(blk, 1, 256 * 1024, g_tape_stream);
|
---|
133 | }
|
---|
134 | sleep(1);
|
---|
135 | paranoid_system("sync");
|
---|
136 | sleep(1);
|
---|
137 | paranoid_pclose(g_tape_stream);
|
---|
138 | log_it("closein_tape() -- leaving");
|
---|
139 | /*
|
---|
140 | for(i=0; i < g_tapecatalog->entries; i++)
|
---|
141 | {
|
---|
142 | log_it("i=%d type=%s num=%d aux=%ld", i, (g_tapecatalog->el[i].type==fileset)?"fileset":"bigslice", g_tapecatalog->el[i].number, g_tapecatalog->el[i].aux);
|
---|
143 | }
|
---|
144 | */
|
---|
145 | if (!bkpinfo->please_dont_eject) {
|
---|
146 | eject_device(bkpinfo->media_device);
|
---|
147 | }
|
---|
148 | paranoid_free(blk);
|
---|
149 | paranoid_free(g_tapecatalog);
|
---|
150 | return (retval);
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Close the global output file descriptor which Mondo has been using to write
|
---|
157 | * to the tape device (via buffer or dd).
|
---|
158 | * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
|
---|
159 | * @return 0 for success, nonzero for failure.
|
---|
160 | * @note This should be called by backup processes only.
|
---|
161 | */
|
---|
162 | int closeout_tape()
|
---|
163 | {
|
---|
164 | /*@ int's ******************************************************* */
|
---|
165 | int retval = 0;
|
---|
166 | // int res = 0;
|
---|
167 | // int ctrl_chr = '\0';
|
---|
168 |
|
---|
169 | /*@ buffers ***************************************************** */
|
---|
170 | // char fname[MAX_STR_LEN];
|
---|
171 |
|
---|
172 | /*@ long long's ************************************************* */
|
---|
173 | // long long size;
|
---|
174 | int i;
|
---|
175 | char *blk;
|
---|
176 |
|
---|
177 | blk = (char *) malloc(256 * 1024);
|
---|
178 |
|
---|
179 | sleep(1);
|
---|
180 | paranoid_system("sync");
|
---|
181 | sleep(1);
|
---|
182 | log_it("closeout_tape() -- entering");
|
---|
183 | retval +=
|
---|
184 | write_header_block_to_stream((off_t)0, "end-of-backup",
|
---|
185 | BLK_END_OF_BACKUP);
|
---|
186 | retval += write_header_block_to_stream((off_t)0, "end-of-tape", BLK_END_OF_TAPE); /* just in case */
|
---|
187 | /* write 1MB of crap */
|
---|
188 | for (i = 0; i < 256 * 1024; i++) {
|
---|
189 | blk[i] = (int) (random() & 0xFF);
|
---|
190 | }
|
---|
191 | for (i = 0; i < 4 * 8; i++) {
|
---|
192 | (void) fwrite(blk, 1, 256 * 1024, g_tape_stream);
|
---|
193 | if (should_we_write_to_next_tape
|
---|
194 | (bkpinfo->media_size[g_current_media_number], (off_t)256 * 1024)) {
|
---|
195 | start_to_write_to_next_tape();
|
---|
196 | }
|
---|
197 | }
|
---|
198 | /* write 1MB of zeroes */
|
---|
199 | /*
|
---|
200 | for (i = 0; i < 256*1024; i++)
|
---|
201 | {
|
---|
202 | blk[i] = 0;
|
---|
203 | }
|
---|
204 | for (i = 0; i < 4; i++)
|
---|
205 | {
|
---|
206 | fwrite (blk, 1, 256*1024, g_tape_stream);
|
---|
207 | if (should_we_write_to_next_tape (bkpinfo->media_size[g_current_media_number], 256*1024))
|
---|
208 | {
|
---|
209 | start_to_write_to_next_tape ();
|
---|
210 | }
|
---|
211 | }
|
---|
212 | */
|
---|
213 | sleep(2);
|
---|
214 | paranoid_pclose(g_tape_stream);
|
---|
215 | log_it("closeout_tape() -- leaving");
|
---|
216 | for (i = 0; i < g_tapecatalog->entries; i++) {
|
---|
217 | log_it("i=%d type=%s num=%d aux=%ld posK=%lld", i,
|
---|
218 | (g_tapecatalog->el[i].type ==
|
---|
219 | fileset) ? "fileset" : "bigslice",
|
---|
220 | g_tapecatalog->el[i].number, g_tapecatalog->el[i].aux,
|
---|
221 | g_tapecatalog->el[i].tape_posK);
|
---|
222 | }
|
---|
223 | // if (!bkpinfo->please_dont_eject)
|
---|
224 | // { eject_device(bkpinfo->media_device); }
|
---|
225 | paranoid_free(blk);
|
---|
226 | paranoid_free(g_tapecatalog);
|
---|
227 | return (retval);
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 |
|
---|
232 | bool mt_says_tape_exists(char *dev)
|
---|
233 | {
|
---|
234 | char *command;
|
---|
235 | int res;
|
---|
236 |
|
---|
237 | malloc_string(command);
|
---|
238 | sprintf(command, "mt -f %s status", dev);
|
---|
239 | res = run_program_and_log_output(command, 1);
|
---|
240 | paranoid_free(command);
|
---|
241 | if (res) {
|
---|
242 | return (FALSE);
|
---|
243 | } else {
|
---|
244 | return (TRUE);
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Determine the name and size of the tape device. Tries the SCSI tape for
|
---|
252 | * this platform, then the IDE tape, then "/dev/st0", then "/dev/osst0".
|
---|
253 | * @param dev Where to put the found tape device.
|
---|
254 | * @param siz Where to put the tape size (a string like "4GB")
|
---|
255 | * @return 0 if success, nonzero if failure (in which @p dev and @p siz are undefined).
|
---|
256 | */
|
---|
257 | int find_tape_device_and_size(char *dev, char *siz)
|
---|
258 | {
|
---|
259 | char tmp[MAX_STR_LEN];
|
---|
260 | char command[MAX_STR_LEN * 2];
|
---|
261 | char cdr_exe[MAX_STR_LEN];
|
---|
262 | int res;
|
---|
263 |
|
---|
264 | log_to_screen("I am looking for your tape streamer. Please wait.");
|
---|
265 | dev[0] = siz[0] = '\0';
|
---|
266 | if (find_home_of_exe("cdrecord")) {
|
---|
267 | strcpy(cdr_exe, "cdrecord");
|
---|
268 | } else {
|
---|
269 | strcpy(cdr_exe, "dvdrecord");
|
---|
270 | }
|
---|
271 | sprintf(command, "%s -scanbus 2> /dev/null | grep -i tape | wc -l",
|
---|
272 | cdr_exe);
|
---|
273 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
274 | if (atoi(tmp) != 1) {
|
---|
275 | log_it
|
---|
276 | ("Either too few or too many tape streamers for me to detect...");
|
---|
277 | strcpy(dev, VANILLA_SCSI_TAPE);
|
---|
278 | return 1;
|
---|
279 | }
|
---|
280 | sprintf(command,
|
---|
281 | "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f2 | head -n1",
|
---|
282 | cdr_exe);
|
---|
283 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
284 | if (strlen(tmp) < 2) {
|
---|
285 | log_it("Could not find tape device");
|
---|
286 | return 1;
|
---|
287 | }
|
---|
288 | sprintf(command,
|
---|
289 | "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | cut -d' ' -f3 | cut -d')' -f1 | head -n1",
|
---|
290 | cdr_exe);
|
---|
291 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
292 | strcpy(dev, VANILLA_SCSI_TAPE);
|
---|
293 | dev[strlen(dev) - 1] = '\0';
|
---|
294 | strcat(dev, tmp); // e.g. '/dev/st0' becomes '/dev/stN'
|
---|
295 | res = 0;
|
---|
296 | if (!mt_says_tape_exists(dev)) {
|
---|
297 | strcpy(dev, ALT_TAPE);
|
---|
298 | if (!mt_says_tape_exists(dev)) {
|
---|
299 | log_it("Cannot openin %s", dev);
|
---|
300 | strcpy(dev, "/dev/st0");
|
---|
301 | if (!mt_says_tape_exists(dev)) {
|
---|
302 | log_it("Cannot openin %s", dev);
|
---|
303 | strcpy(dev, "/dev/osst0");
|
---|
304 | if (!mt_says_tape_exists(dev)) {
|
---|
305 | res++;
|
---|
306 | } else {
|
---|
307 | res = 0;
|
---|
308 | }
|
---|
309 | }
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | log_it("At this point, dev = %s and res = %d", dev, res);
|
---|
314 |
|
---|
315 | strcpy(tmp, call_program_and_get_last_line_of_output("\
|
---|
316 | cdrecord -scanbus 2> /dev/null | tr -s '\t' ' ' | \
|
---|
317 | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep -i TAPE | \
|
---|
318 | awk '{for(i=1; i<NF; i++) { if (index($i, \"GB\")>0) { print $i;};};};'"));
|
---|
319 |
|
---|
320 | if (mt_says_tape_exists(dev)) {
|
---|
321 | res = 0;
|
---|
322 | } else {
|
---|
323 | log_it("Turning %s", dev);
|
---|
324 | strcpy(tmp, (strrchr(dev, '/') != NULL) ? strrchr(dev, '/') : dev);
|
---|
325 | sprintf(dev, "/dev/os%s", tmp);
|
---|
326 | log_it("...into %s", dev);
|
---|
327 | if (mt_says_tape_exists(dev)) {
|
---|
328 | res = 0;
|
---|
329 | } else {
|
---|
330 | res++;
|
---|
331 | }
|
---|
332 | }
|
---|
333 |
|
---|
334 | siz[0] = '\0';
|
---|
335 | log_it("res=%d; dev=%s", res, dev);
|
---|
336 |
|
---|
337 | if (res) {
|
---|
338 | return (res);
|
---|
339 | }
|
---|
340 |
|
---|
341 | if (strlen(tmp) < 2) {
|
---|
342 | siz[0] = '\0';
|
---|
343 | log_it("Warning - size of tape unknown");
|
---|
344 | return (0);
|
---|
345 | } else {
|
---|
346 | strcpy(siz, tmp);
|
---|
347 | return (0);
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 |
|
---|
353 |
|
---|
354 |
|
---|
355 |
|
---|
356 | int read_EXAT_files_from_tape(long long *ptmp_size, char *tmp_fname,
|
---|
357 | int *pctrl_chr, char *xattr_fname,
|
---|
358 | char *acl_fname)
|
---|
359 | {
|
---|
360 | int res = 0;
|
---|
361 | char *fname = (char *)&res; /* Should NOT be NULL */
|
---|
362 | char *tmp = NULL;
|
---|
363 |
|
---|
364 | // xattr
|
---|
365 | if (g_getfattr) {
|
---|
366 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
367 | if (*pctrl_chr != BLK_START_EXAT_FILE) {
|
---|
368 | wrong_marker(BLK_START_EXAT_FILE, *pctrl_chr);
|
---|
369 | }
|
---|
370 | if (strstr(fname, "xattr") == NULL) {
|
---|
371 | asprintf(&tmp,"Wrong order expected xattr, got %s, sunshine.", fname);
|
---|
372 | fatal_error(tmp);
|
---|
373 | }
|
---|
374 | read_file_from_stream_to_file(xattr_fname, *ptmp_size);
|
---|
375 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
376 | if (*pctrl_chr != BLK_STOP_EXAT_FILE) {
|
---|
377 | wrong_marker(BLK_STOP_EXAT_FILE, *pctrl_chr);
|
---|
378 | }
|
---|
379 | log_msg(1, "Got xattr");
|
---|
380 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
381 | if (*pctrl_chr != BLK_STOP_EXTENDED_ATTRIBUTES) {
|
---|
382 | wrong_marker(BLK_STOP_EXTENDED_ATTRIBUTES, *pctrl_chr);
|
---|
383 | }
|
---|
384 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
385 | if (*pctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
|
---|
386 | log_msg(1, "No acl attributes found, skipping to afio files");
|
---|
387 | return(0);
|
---|
388 | } else {
|
---|
389 | if (*pctrl_chr != BLK_START_EXTENDED_ATTRIBUTES) {
|
---|
390 | wrong_marker(BLK_START_EXTENDED_ATTRIBUTES, *pctrl_chr);
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|
394 | // acl
|
---|
395 | if (g_getfacl) {
|
---|
396 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
397 | if (*pctrl_chr != BLK_START_EXAT_FILE) {
|
---|
398 | wrong_marker(BLK_START_EXAT_FILE, *pctrl_chr);
|
---|
399 | }
|
---|
400 | if (strstr(fname, "acl") == NULL) {
|
---|
401 | asprintf(&tmp,"Wrong order expected acl, got %s, sunshine.", fname);
|
---|
402 | fatal_error(tmp);
|
---|
403 | }
|
---|
404 | read_file_from_stream_to_file(acl_fname, *ptmp_size);
|
---|
405 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
406 | if (*pctrl_chr != BLK_STOP_EXAT_FILE) {
|
---|
407 | wrong_marker(BLK_STOP_EXAT_FILE, *pctrl_chr);
|
---|
408 | }
|
---|
409 | res = read_header_block_from_stream(ptmp_size, fname, pctrl_chr);
|
---|
410 | if (*pctrl_chr != BLK_STOP_EXTENDED_ATTRIBUTES) {
|
---|
411 | wrong_marker(BLK_STOP_EXTENDED_ATTRIBUTES, *pctrl_chr);
|
---|
412 | }
|
---|
413 | log_msg(1, "Got acl");
|
---|
414 | }
|
---|
415 | // tarball itself
|
---|
416 | res = read_header_block_from_stream(ptmp_size, tmp_fname, pctrl_chr);
|
---|
417 | log_msg(1, "End of extended attributes, now looking for afioball");
|
---|
418 | return (0);
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | int write_EXAT_files_to_tape(char *xattr_fname,
|
---|
423 | char *acl_fname)
|
---|
424 | {
|
---|
425 | int res = 0;
|
---|
426 | if (g_getfattr) {
|
---|
427 | // xattr
|
---|
428 | write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname,
|
---|
429 | BLK_START_EXTENDED_ATTRIBUTES);
|
---|
430 | write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname,
|
---|
431 | BLK_START_EXAT_FILE);
|
---|
432 | write_file_to_stream_from_file(xattr_fname);
|
---|
433 | write_header_block_to_stream((off_t)-1, xattr_fname, BLK_STOP_EXAT_FILE);
|
---|
434 | write_header_block_to_stream(length_of_file(xattr_fname), xattr_fname,
|
---|
435 | BLK_STOP_EXTENDED_ATTRIBUTES);
|
---|
436 | }
|
---|
437 | if (g_getfacl) {
|
---|
438 | // acl
|
---|
439 | write_header_block_to_stream(length_of_file(acl_fname), acl_fname,
|
---|
440 | BLK_START_EXTENDED_ATTRIBUTES);
|
---|
441 | write_header_block_to_stream(length_of_file(acl_fname), acl_fname,
|
---|
442 | BLK_START_EXAT_FILE);
|
---|
443 | write_file_to_stream_from_file(acl_fname);
|
---|
444 | write_header_block_to_stream((off_t)-1, acl_fname, BLK_STOP_EXAT_FILE);
|
---|
445 | write_header_block_to_stream(length_of_file(acl_fname), acl_fname,
|
---|
446 | BLK_STOP_EXTENDED_ATTRIBUTES);
|
---|
447 | }
|
---|
448 | return (res);
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 |
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Tell the user to insert tape @p tapeno and wait for it to settle (5 seconds).
|
---|
456 | * @param tapeno The tape number to insist on.
|
---|
457 | * @bug There is currently no way to actually verify that the user has actually
|
---|
458 | * inserted the right tape.
|
---|
459 | */
|
---|
460 | void insist_on_this_tape_number(int tapeno)
|
---|
461 | {
|
---|
462 | int i;
|
---|
463 | char tmp[MAX_STR_LEN];
|
---|
464 |
|
---|
465 | log_it("Insisting on tape #%d", tapeno);
|
---|
466 | if (g_current_media_number != tapeno) {
|
---|
467 | // log_it("g_current_media_number = %d", g_current_media_number);
|
---|
468 | sprintf(tmp,
|
---|
469 | "When the tape drive goes quiet, please insert volume %d in this series.",
|
---|
470 | tapeno);
|
---|
471 | popup_and_OK(tmp);
|
---|
472 | open_evalcall_form("Waiting while the tape drive settles");
|
---|
473 | } else {
|
---|
474 | open_evalcall_form("Waiting while the tape drive rewinds");
|
---|
475 | }
|
---|
476 |
|
---|
477 | for (i = 0; i <= 100; i += 2) {
|
---|
478 | usleep(100000);
|
---|
479 | update_evalcall_form(i);
|
---|
480 | }
|
---|
481 | close_evalcall_form();
|
---|
482 | log_it("I assume user has inserted it. They _say_ they have...");
|
---|
483 | g_current_media_number = tapeno;
|
---|
484 |
|
---|
485 | // log_it("g_current_media_number = %d", g_current_media_number);
|
---|
486 | log_it("OK, I've finished insisting. On with the revelry.");
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Debugging aid - log the offset we're at on the tape (reading or writing).
|
---|
494 | */
|
---|
495 | void log_tape_pos(void)
|
---|
496 | {
|
---|
497 | /*@ buffers ***************************************************** */
|
---|
498 |
|
---|
499 |
|
---|
500 | /*@ end vars *************************************************** */
|
---|
501 |
|
---|
502 | log_it("Tape position -- %ld KB (%ld MB)", (long) g_tape_posK,
|
---|
503 | (long) g_tape_posK >> 10);
|
---|
504 | }
|
---|
505 |
|
---|
506 |
|
---|
507 |
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Add a file to a collection of recently archived filesets/slices.
|
---|
511 | * The type is determined by the filename: if it contains ".afio." it is
|
---|
512 | * assumed to be a fileset, otherwise if it contains "slice" it's a slice,
|
---|
513 | * otherwise we generate a fatal_error().
|
---|
514 | * @param td The current @c bkpinfo->tempdir (file will be placed in <tt>td</tt>/tmpfs/backcatalog/).
|
---|
515 | * @param latest_fname The file to place in the collection.
|
---|
516 | * @return 0, always.
|
---|
517 | * @bug Return value is redundant.
|
---|
518 | * @bug The detection won't work for uncompressed afioballs (they end in ".afio", no dot afterwards). // Not true. They end in '.' -Hugo
|
---|
519 | */
|
---|
520 | int maintain_collection_of_recent_archives(char *td, char *latest_fname)
|
---|
521 | {
|
---|
522 | long long final_alleged_writeK, final_projected_certain_writeK,
|
---|
523 | final_actually_certain_writeK = 0, cposK, bufsize_K;
|
---|
524 | int last, curr, i;
|
---|
525 | t_archtype type = other;
|
---|
526 | char command[MAX_STR_LEN];
|
---|
527 | char tmpdir[MAX_STR_LEN];
|
---|
528 | char old_fname[MAX_STR_LEN];
|
---|
529 | char *p;
|
---|
530 | char suffix[16];
|
---|
531 |
|
---|
532 | bufsize_K = (long long) (1024LL * (1 + g_tape_buffer_size_MB));
|
---|
533 | sprintf(tmpdir, "%s/tmpfs/backcatalog", td);
|
---|
534 | if ((p = strrchr(latest_fname, '.'))) {
|
---|
535 | strcpy(suffix, ++p);
|
---|
536 | } else {
|
---|
537 | suffix[0] = '\0';
|
---|
538 | }
|
---|
539 | if (strstr(latest_fname, ".afio.") || strstr(latest_fname, ".star.")) {
|
---|
540 | type = fileset;
|
---|
541 | } else if (strstr(latest_fname, "slice")) {
|
---|
542 | type = biggieslice;
|
---|
543 | } else {
|
---|
544 | log_it("fname = %s", latest_fname);
|
---|
545 | fatal_error
|
---|
546 | ("Unknown type. Internal error in maintain_collection_of_recent_archives()");
|
---|
547 | }
|
---|
548 | mkdir(tmpdir, 0x700);
|
---|
549 | sprintf(command, "cp -f %s %s", latest_fname, tmpdir);
|
---|
550 | if (run_program_and_log_output(command, 6)) {
|
---|
551 | log_it("Warning - failed to copy %s to backcatalog at %s",
|
---|
552 | latest_fname, tmpdir);
|
---|
553 | }
|
---|
554 | last = g_tapecatalog->entries - 1;
|
---|
555 | if (last <= 0) {
|
---|
556 | iamhere("Too early to start deleting from collection.");
|
---|
557 | return (0);
|
---|
558 | }
|
---|
559 | final_alleged_writeK = g_tapecatalog->el[last].tape_posK;
|
---|
560 | final_projected_certain_writeK = final_alleged_writeK - bufsize_K;
|
---|
561 | for (curr = last; curr >= 0; curr--) {
|
---|
562 | cposK = g_tapecatalog->el[curr].tape_posK;
|
---|
563 | if (cposK < final_projected_certain_writeK) {
|
---|
564 | final_actually_certain_writeK = cposK;
|
---|
565 | break;
|
---|
566 | }
|
---|
567 | }
|
---|
568 | if (curr < 0) {
|
---|
569 | iamhere
|
---|
570 | ("Not far enough into tape to start deleting old archives from collection.");
|
---|
571 | return (0);
|
---|
572 | }
|
---|
573 | // log_it( "There are %lld KB (more than %d KB) in my backcatalog", final_alleged_writeK - final_actually_certain_writeK, bufsize_K);
|
---|
574 |
|
---|
575 | for (i = curr - 1; i >= 0 && curr - i < 10; i--) {
|
---|
576 | sprintf(old_fname, "%s/%s", tmpdir, g_tapecatalog->el[i].fname);
|
---|
577 | unlink(old_fname);
|
---|
578 | }
|
---|
579 | return (0);
|
---|
580 | }
|
---|
581 |
|
---|
582 |
|
---|
583 |
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Open the CD stream for input.
|
---|
587 | * @param bkpinfo The backup information structure. Passed to openin_tape().
|
---|
588 | * @return 0 for success, nonzero for failure.
|
---|
589 | * @note Equivalent to openin_tape() for now, but don't count on this behavior.
|
---|
590 | */
|
---|
591 | int openin_cdstream()
|
---|
592 | {
|
---|
593 | return (openin_tape());
|
---|
594 | }
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * FIFO used to read/write to the tape device.
|
---|
598 | * @bug This seems obsolete now that we call an external @c buffer program. Please look onto this.
|
---|
599 | */
|
---|
600 | char g_tape_fifo[MAX_STR_LEN];
|
---|
601 |
|
---|
602 |
|
---|
603 |
|
---|
604 | int set_tape_block_size_with_mt(long internal_tape_block_size)
|
---|
605 | {
|
---|
606 | char *tmp;
|
---|
607 | int res;
|
---|
608 |
|
---|
609 | if (strncmp(bkpinfo->media_device, "/dev/", 5)) {
|
---|
610 | log_msg(1,
|
---|
611 | "Not using 'mt setblk'. This isn't an actual /dev entry.");
|
---|
612 | return (0);
|
---|
613 | }
|
---|
614 | malloc_string(tmp);
|
---|
615 | sprintf(tmp, "mt -f %s setblk %ld", bkpinfo->media_device, internal_tape_block_size);
|
---|
616 | res = run_program_and_log_output(tmp, 3);
|
---|
617 | paranoid_free(tmp);
|
---|
618 | return (res);
|
---|
619 | }
|
---|
620 |
|
---|
621 | /**
|
---|
622 | * Return the non-rewinding device when passed the normal one
|
---|
623 | * @param tapedev The tape device to open for writing.
|
---|
624 | * @note the caller needs to free the string returned
|
---|
625 | */
|
---|
626 | char *get_non_rewind_dev(char *tapedev)
|
---|
627 | {
|
---|
628 |
|
---|
629 | char *ntapedev = NULL;
|
---|
630 | char *p = NULL;
|
---|
631 | char *q = NULL;
|
---|
632 | char *r = NULL;
|
---|
633 |
|
---|
634 | ntapedev = (char *)malloc(strlen(tapedev)+sizeof(char));
|
---|
635 | p = strrchr(tapedev,'/');
|
---|
636 | if (p == NULL) {
|
---|
637 | log_it("Didn't find a '/' in %s",tapedev);
|
---|
638 | return(NULL);
|
---|
639 | }
|
---|
640 |
|
---|
641 | /* Copy tapedev content up to the last / */
|
---|
642 | q = tapedev;
|
---|
643 | r = ntapedev;
|
---|
644 | while (q != p) {
|
---|
645 | *r = *q;
|
---|
646 | r++;
|
---|
647 | q++;
|
---|
648 | }
|
---|
649 | /* Copy the '/' */
|
---|
650 | *r = *q;
|
---|
651 | r++;
|
---|
652 | q++;
|
---|
653 | /* Adds a 'n' - non-rewinding */
|
---|
654 | *r = 'n';
|
---|
655 | r++;
|
---|
656 | /* Copy the rest of tapedev */
|
---|
657 | while (*q != '\0') {
|
---|
658 | *r = *q;
|
---|
659 | r++;
|
---|
660 | q++;
|
---|
661 | }
|
---|
662 | *r = '\0';
|
---|
663 | if (mt_says_tape_exists(ntapedev)) {
|
---|
664 | log_it("Non-rewinding tape device is %s",ntapedev);
|
---|
665 | } else {
|
---|
666 | log_it("Unable to find non-rewinding tape device.");
|
---|
667 | ntapedev = NULL;
|
---|
668 | }
|
---|
669 | return(ntapedev);
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 |
|
---|
674 | /**
|
---|
675 | * Handle OBDR if we were asked to do so
|
---|
676 | * @param tapedev The tape device to open for reading.
|
---|
677 | */
|
---|
678 | int skip_obdr(void)
|
---|
679 | {
|
---|
680 | char *command = NULL;
|
---|
681 | int res = 0;
|
---|
682 |
|
---|
683 | log_it("Skipping OBDR headers");
|
---|
684 | asprintf(&command, "mt -f %s rewind",bkpinfo->media_device);
|
---|
685 | res = run_program_and_log_output(command, 1);
|
---|
686 | paranoid_free(command);
|
---|
687 |
|
---|
688 | asprintf(&command, "mt -f %s fsf 2",bkpinfo->media_device);
|
---|
689 | res = run_program_and_log_output(command, 1);
|
---|
690 | paranoid_free(command);
|
---|
691 |
|
---|
692 | set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
|
---|
693 | return(res);
|
---|
694 | }
|
---|
695 |
|
---|
696 | /**
|
---|
697 | * Handle OBDR if we were asked to do so
|
---|
698 | * @param tapedev The tape device to open for writing.
|
---|
699 | * @return 0 for success, nonzero for failure.
|
---|
700 | * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
|
---|
701 | * EXISTING DATA on the tape!
|
---|
702 | */
|
---|
703 | int create_obdr(void)
|
---|
704 | {
|
---|
705 |
|
---|
706 | char *command = NULL;
|
---|
707 | int res = 0;
|
---|
708 |
|
---|
709 | log_it("Creating OBDR headers");
|
---|
710 | /* OBDR: First block 10 kB of zero bs = 512 */
|
---|
711 | asprintf(&command, "mt -f %s compression off",bkpinfo->media_device);
|
---|
712 | res = run_program_and_log_output(command, 1);
|
---|
713 | paranoid_free(command);
|
---|
714 |
|
---|
715 | asprintf(&command, "mt -f %s rewind",bkpinfo->media_device);
|
---|
716 | res += run_program_and_log_output(command, 1);
|
---|
717 | paranoid_free(command);
|
---|
718 |
|
---|
719 | set_tape_block_size_with_mt(512);
|
---|
720 |
|
---|
721 | asprintf(&command, "dd if=/dev/zero of=%s bs=512 count=20",bkpinfo->media_device);
|
---|
722 | res += run_program_and_log_output(command, 1);
|
---|
723 | paranoid_free(command);
|
---|
724 |
|
---|
725 | /* OBDR: then ISO boot image bs = 2048 */
|
---|
726 | set_tape_block_size_with_mt(2048);
|
---|
727 |
|
---|
728 | asprintf(&command, "dd if=%s of=%s bs=2048",MINDI_CACHE"/mondorescue.iso",bkpinfo->media_device);
|
---|
729 | res += run_program_and_log_output(command, 1);
|
---|
730 | paranoid_free(command);
|
---|
731 |
|
---|
732 | set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
|
---|
733 | return(res);
|
---|
734 | }
|
---|
735 |
|
---|
736 |
|
---|
737 | /**
|
---|
738 | * Open the tape device for input.
|
---|
739 | * @param bkpinfo The backup information structure. Fields used:
|
---|
740 | * - @c bkpinfo->media_device
|
---|
741 | * - @c bkpinfo->tmpdir
|
---|
742 | * @return 0 for success, nonzero for failure.
|
---|
743 | * @note This will also work with a cdstream for now, but don't count on this behavior.
|
---|
744 | */
|
---|
745 | int openin_tape()
|
---|
746 | {
|
---|
747 | /*@ buffer ***************************************************** */
|
---|
748 | char fname[MAX_STR_LEN];
|
---|
749 | char *datablock;
|
---|
750 | char tmp[MAX_STR_LEN];
|
---|
751 | char old_cwd[MAX_STR_LEN];
|
---|
752 | char outfname[MAX_STR_LEN];
|
---|
753 | /*@ int ******************************************************* */
|
---|
754 | int i;
|
---|
755 | int j;
|
---|
756 | int res = 0;
|
---|
757 | long length, templong;
|
---|
758 | size_t k;
|
---|
759 | int retval = 0;
|
---|
760 | int ctrl_chr;
|
---|
761 |
|
---|
762 | /*@ long long ************************************************* */
|
---|
763 | long long size;
|
---|
764 |
|
---|
765 | /*@ pointers ************************************************** */
|
---|
766 | FILE *fout;
|
---|
767 |
|
---|
768 | /*@ end vars *************************************************** */
|
---|
769 |
|
---|
770 | assert_string_is_neither_NULL_nor_zerolength(bkpinfo->media_device);
|
---|
771 | if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
|
---|
772 | fatal_error("Cannot alloc mem for tape catalog");
|
---|
773 | }
|
---|
774 | g_tapecatalog->entries = 0;
|
---|
775 | g_tape_posK = 0;
|
---|
776 | if (g_tape_stream) {
|
---|
777 | log_it("FYI - I won't 'openin' the tape. It's already open.");
|
---|
778 | return (0);
|
---|
779 | }
|
---|
780 |
|
---|
781 | // mondoarchive should have configured everything to give the right non-rew device
|
---|
782 | if ((bkpinfo->use_obdr) && (bkpinfo->media_device != NULL)) {
|
---|
783 | res = skip_obdr();
|
---|
784 | if (res != 0) {
|
---|
785 | log_it("Not able to skip OBDR - Restore will have to be done manually");
|
---|
786 | }
|
---|
787 | } else {
|
---|
788 | if (bkpinfo->media_device == NULL) {
|
---|
789 | log_it("Not able to skip OBDR - Restore will have to be done manually");
|
---|
790 | }
|
---|
791 | set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
|
---|
792 | }
|
---|
793 |
|
---|
794 | insist_on_this_tape_number(1);
|
---|
795 | sprintf(outfname, "%s/tmp/all.tar.gz", bkpinfo->tmpdir);
|
---|
796 | make_hole_for_file(outfname);
|
---|
797 |
|
---|
798 | // start_buffer_process( bkpinfo->media_device, g_tape_fifo, FALSE);
|
---|
799 | log_it("Opening IN tape");
|
---|
800 | if (!
|
---|
801 | (g_tape_stream =
|
---|
802 | open_device_via_buffer(bkpinfo->media_device, 'r',
|
---|
803 | bkpinfo->internal_tape_block_size))) {
|
---|
804 | log_OS_error(g_tape_fifo);
|
---|
805 | log_to_screen("Cannot openin stream device");
|
---|
806 | return (1);
|
---|
807 | }
|
---|
808 | log_to_screen("Reading stream");
|
---|
809 | log_it("stream device = '%s'", bkpinfo->media_device);
|
---|
810 | /* skip data disks */
|
---|
811 | open_evalcall_form("Skipping data disks on stream");
|
---|
812 | log_to_screen("Skipping data disks on stream");
|
---|
813 | if (!(fout = fopen(outfname, "w"))) {
|
---|
814 | log_OS_error(outfname);
|
---|
815 | log_to_screen("Cannot openout datadisk all.tar.gz file");
|
---|
816 | return (-1);
|
---|
817 | }
|
---|
818 | if (!(datablock = (char *) malloc(256 * 1024))) {
|
---|
819 | log_to_screen("Unable to malloc 256*1024");
|
---|
820 | exit(1);
|
---|
821 | }
|
---|
822 | for (i = 0; i < 32; i++) {
|
---|
823 | for (j = 0; j < 4; j++) {
|
---|
824 | for (length = 0, k = 0; length < 256 * 1024; length += k) {
|
---|
825 | k = fread(datablock + length, 1, 256 * 1024 - length,
|
---|
826 | g_tape_stream);
|
---|
827 | }
|
---|
828 | (void) fwrite(datablock, 1, (size_t) length, fout);
|
---|
829 | g_tape_posK += length / 1024;
|
---|
830 | }
|
---|
831 | if (i > 8) // otherwise, 'buffer' distorts calculations
|
---|
832 | {
|
---|
833 | templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
|
---|
834 | update_evalcall_form((int) (templong));
|
---|
835 | }
|
---|
836 | }
|
---|
837 | paranoid_fclose(fout);
|
---|
838 | paranoid_free(datablock);
|
---|
839 | /* find initial blocks */
|
---|
840 | res = read_header_block_from_stream(&size, fname, &ctrl_chr);
|
---|
841 | retval += res;
|
---|
842 | if (ctrl_chr != BLK_START_OF_TAPE) {
|
---|
843 | wrong_marker(BLK_START_OF_TAPE, ctrl_chr);
|
---|
844 | }
|
---|
845 | res = read_header_block_from_stream(&size, fname, &ctrl_chr);
|
---|
846 | retval += res;
|
---|
847 | if (ctrl_chr != BLK_START_OF_BACKUP) {
|
---|
848 | wrong_marker(BLK_START_OF_BACKUP, ctrl_chr);
|
---|
849 | }
|
---|
850 | close_evalcall_form();
|
---|
851 | log_it("Saved all.tar.gz to '%s'", outfname);
|
---|
852 | (void) getcwd(old_cwd, MAX_STR_LEN);
|
---|
853 | chdir(bkpinfo->tmpdir);
|
---|
854 | sprintf(tmp, "tar -zxf %s ./tmp/mondo-restore.cfg 2> /dev/null",
|
---|
855 | outfname);
|
---|
856 | paranoid_system(tmp);
|
---|
857 | paranoid_system("cp -f tmp/mondo-restore.cfg . 2> /dev/null");
|
---|
858 | chdir(old_cwd);
|
---|
859 | unlink(outfname);
|
---|
860 | return (retval);
|
---|
861 | }
|
---|
862 |
|
---|
863 |
|
---|
864 | /**
|
---|
865 | * Start writing to a CD stream.
|
---|
866 | * @param cddev The CD device to openout via cdrecord.
|
---|
867 | * @param speed The speed to write at.
|
---|
868 | * @return 0 for success, nonzero for failure.
|
---|
869 | * @note This should be called only from backup processes.
|
---|
870 | */
|
---|
871 | int openout_cdstream(char *cddev, int speed)
|
---|
872 | {
|
---|
873 | /*@ buffers ***************************************************** */
|
---|
874 | char command[MAX_STR_LEN * 2];
|
---|
875 |
|
---|
876 | /*@ end vars *************************************************** */
|
---|
877 |
|
---|
878 | /* add 'dummy' if testing */
|
---|
879 | sprintf(command,
|
---|
880 | "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
|
---|
881 | cddev, speed, MONDO_LOGFILE, MONDO_LOGFILE);
|
---|
882 | /* initialise the catalog */
|
---|
883 | g_current_media_number = 1;
|
---|
884 | if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
|
---|
885 | fatal_error("Cannot alloc mem for tape catalog");
|
---|
886 | }
|
---|
887 | g_tapecatalog->entries = 0;
|
---|
888 | /* log stuff */
|
---|
889 | log_it("Opening OUT cdstream with the command");
|
---|
890 | log_it(command);
|
---|
891 | /* log_it("Let's see what happens, shall we?"); */
|
---|
892 | g_tape_stream = popen(command, "w");
|
---|
893 | if (g_tape_stream) {
|
---|
894 | return (0);
|
---|
895 | } else {
|
---|
896 | log_to_screen("Failed to openout to cdstream (fifo)");
|
---|
897 | return (1);
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * Start writing to a tape device for the backup.
|
---|
903 | * Handle OBDR if we were asked to do so
|
---|
904 | * @param tapedev The tape device to open for writing.
|
---|
905 | * @return 0 for success, nonzero for failure.
|
---|
906 | * @note This should be called ONLY from backup processes. It will OVERWRITE ANY
|
---|
907 | * EXISTING DATA on the tape!
|
---|
908 | */
|
---|
909 | int openout_tape() {
|
---|
910 |
|
---|
911 | g_current_media_number = 1;
|
---|
912 | if (g_tape_stream) {
|
---|
913 | log_it("FYI - I won't 'openout' the tape. It's already open.");
|
---|
914 | return (0);
|
---|
915 | }
|
---|
916 | if (!(g_tapecatalog = malloc(sizeof(struct s_tapecatalog)))) {
|
---|
917 | fatal_error("Cannot alloc mem for tape catalog");
|
---|
918 | }
|
---|
919 | g_tapecatalog->entries = 0;
|
---|
920 | g_tape_posK = 0;
|
---|
921 |
|
---|
922 | if (bkpinfo->use_obdr) {
|
---|
923 | create_obdr();
|
---|
924 | } else {
|
---|
925 | set_tape_block_size_with_mt(bkpinfo->internal_tape_block_size);
|
---|
926 | }
|
---|
927 | log_it("Opening OUT tape");
|
---|
928 | if (!
|
---|
929 | (g_tape_stream =
|
---|
930 | open_device_via_buffer(bkpinfo->media_device, 'w', bkpinfo->internal_tape_block_size))) {
|
---|
931 | log_OS_error(g_tape_fifo);
|
---|
932 | log_to_screen("Cannot openin stream device");
|
---|
933 | return (1);
|
---|
934 | }
|
---|
935 | return (0);
|
---|
936 | }
|
---|
937 |
|
---|
938 |
|
---|
939 |
|
---|
940 |
|
---|
941 | /**
|
---|
942 | * Copy a file from the opened stream (CD or tape) to @p outfile.
|
---|
943 | * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
|
---|
944 | * @param outfile The file to write to.
|
---|
945 | * @param size The size of the file in the input stream.
|
---|
946 | * @return 0 for success, nonzero for failure.
|
---|
947 | */
|
---|
948 | int
|
---|
949 | read_file_from_stream_to_file(char *outfile, long long size)
|
---|
950 | {
|
---|
951 |
|
---|
952 | /*@ int ******************************************************** */
|
---|
953 | int res;
|
---|
954 |
|
---|
955 | /*@ end vars *************************************************** */
|
---|
956 |
|
---|
957 | res = read_file_from_stream_FULL(outfile, NULL, size);
|
---|
958 |
|
---|
959 | return (res);
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 |
|
---|
964 | /**
|
---|
965 | * Copy a file from the currently opened stream (CD or tape) to the stream indicated
|
---|
966 | * by @p fout.
|
---|
967 | * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
|
---|
968 | * @param fout The stream to write the file to.
|
---|
969 | * @param size The size of the file in bytes.
|
---|
970 | * @return 0 for success, nonzero for failure.
|
---|
971 | */
|
---|
972 | int
|
---|
973 | read_file_from_stream_to_stream(FILE * fout, long long size)
|
---|
974 | {
|
---|
975 |
|
---|
976 | /*@ int ******************************************************** */
|
---|
977 | int res;
|
---|
978 |
|
---|
979 | /*@ end vars *************************************************** */
|
---|
980 |
|
---|
981 | res = read_file_from_stream_FULL(NULL, fout, size);
|
---|
982 | /* fflush(g_tape_stream);
|
---|
983 | fflush(fout);*/
|
---|
984 | return (res);
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 |
|
---|
989 | /**
|
---|
990 | * Copy a file from the currently opened stream (CD or tape) to either a
|
---|
991 | * @c FILE pointer indicating a currently-opened file, or a filename indicating
|
---|
992 | * a new file to create. This is the backbone function that read_file_from_stream_to_file()
|
---|
993 | * and read_file_from_stream_to_stream() are wrappers for.
|
---|
994 | * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
|
---|
995 | * @param outfname If non-NULL, write to this file.
|
---|
996 | * @param foutstream If non-NULL, write to this stream.
|
---|
997 | * @param orig_size The original length of the file in bytes.
|
---|
998 | * @return 0 for success, nonzero for failure.
|
---|
999 | * @note Only @b one of @p outfname or @p foutstream may be non-NULL.
|
---|
1000 | */
|
---|
1001 | int
|
---|
1002 | read_file_from_stream_FULL(char *outfname, FILE * foutstream, long long orig_size)
|
---|
1003 | {
|
---|
1004 | /*@ buffers ***************************************************** */
|
---|
1005 | char *tmp;
|
---|
1006 | char *datablock;
|
---|
1007 | char *temp_fname;
|
---|
1008 | char *temp_cksum;
|
---|
1009 | char *actual_cksum;
|
---|
1010 | // char *pA, *pB;
|
---|
1011 |
|
---|
1012 | /*@ int ********************************************************* */
|
---|
1013 | int retval = 0;
|
---|
1014 | #ifdef EXTRA_TAPE_CHECKSUMS
|
---|
1015 | int i, ch;
|
---|
1016 | #endif
|
---|
1017 | int noof_blocks;
|
---|
1018 | int ctrl_chr;
|
---|
1019 | int res;
|
---|
1020 | /*@ pointers **************************************************** */
|
---|
1021 | FILE *fout;
|
---|
1022 |
|
---|
1023 | /*@ long ***************************************************** */
|
---|
1024 | long bytes_to_write = 0L /*,i */ ;
|
---|
1025 | // long bytes_successfully_read_in_this_time = 0;
|
---|
1026 |
|
---|
1027 | /*@ long long *************************************************** */
|
---|
1028 | long long temp_size, size;
|
---|
1029 | long bytes_read, bytes_to_read;
|
---|
1030 | long long total_read_from_tape_for_this_file = 0;
|
---|
1031 | long long where_I_was_before_tape_change = 0;
|
---|
1032 | /*@ unsigned int ************************************************ */
|
---|
1033 | /* unsigned int ch; */
|
---|
1034 | unsigned int crc16;
|
---|
1035 | unsigned int crctt;
|
---|
1036 |
|
---|
1037 | bool had_to_resync = FALSE;
|
---|
1038 |
|
---|
1039 | /*@ init ******************************************************* */
|
---|
1040 | malloc_string(tmp);
|
---|
1041 | malloc_string(temp_fname);
|
---|
1042 | malloc_string(temp_cksum);
|
---|
1043 | malloc_string(actual_cksum);
|
---|
1044 | datablock = malloc(TAPE_BLOCK_SIZE);
|
---|
1045 | crc16 = 0;
|
---|
1046 | crctt = 0;
|
---|
1047 | size = orig_size;
|
---|
1048 |
|
---|
1049 | /*@ end vars *************************************************** */
|
---|
1050 |
|
---|
1051 | res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
|
---|
1052 | if (orig_size != temp_size && orig_size != -1) {
|
---|
1053 | sprintf(tmp,
|
---|
1054 | "output file's size should be %ld K but is apparently %ld K",
|
---|
1055 | (long) size >> 10, (long) temp_size >> 10);
|
---|
1056 | log_to_screen(tmp);
|
---|
1057 | }
|
---|
1058 | if (ctrl_chr != BLK_START_FILE) {
|
---|
1059 | wrong_marker(BLK_START_FILE, ctrl_chr);
|
---|
1060 | return (1);
|
---|
1061 | }
|
---|
1062 | /* Not used
|
---|
1063 | sprintf(tmp, "Reading file from tape; writing to '%s'; %ld KB",
|
---|
1064 | outfname, (long) size >> 10);
|
---|
1065 | */
|
---|
1066 |
|
---|
1067 | if (foutstream) {
|
---|
1068 | fout = foutstream;
|
---|
1069 | } else {
|
---|
1070 | fout = fopen(outfname, "w");
|
---|
1071 | }
|
---|
1072 | if (!fout) {
|
---|
1073 | log_OS_error(outfname);
|
---|
1074 | log_to_screen("Cannot openout file");
|
---|
1075 | return (1);
|
---|
1076 | }
|
---|
1077 | total_read_from_tape_for_this_file = 0;
|
---|
1078 | for (noof_blocks = 0; size > 0;
|
---|
1079 | noof_blocks++, size -=
|
---|
1080 | bytes_to_write, total_read_from_tape_for_this_file +=
|
---|
1081 | bytes_read) {
|
---|
1082 | bytes_to_write =
|
---|
1083 | (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
|
---|
1084 | bytes_to_read = TAPE_BLOCK_SIZE;
|
---|
1085 | bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
|
---|
1086 | while (bytes_read < bytes_to_read) { // next tape!
|
---|
1087 | // crctt=crc16=0;
|
---|
1088 | where_I_was_before_tape_change = size;
|
---|
1089 | log_msg(4, "where_I_was_... = %lld",
|
---|
1090 | where_I_was_before_tape_change);
|
---|
1091 | start_to_read_from_next_tape();
|
---|
1092 | log_msg(4, "Started reading from next tape.");
|
---|
1093 | skip_incoming_files_until_we_find_this_one(temp_fname);
|
---|
1094 | log_msg(4, "Skipped irrelevant files OK.");
|
---|
1095 | for (size = orig_size; size > where_I_was_before_tape_change;
|
---|
1096 | size -= bytes_to_write) {
|
---|
1097 | bytes_read =
|
---|
1098 | fread(datablock, 1, bytes_to_read, g_tape_stream);
|
---|
1099 | }
|
---|
1100 | log_msg(4, "'size' is now %lld (should be %lld)", size,
|
---|
1101 | where_I_was_before_tape_change);
|
---|
1102 | log_to_screen("Successfully re-sync'd tape");
|
---|
1103 | had_to_resync = TRUE;
|
---|
1104 | bytes_read = fread(datablock, 1, bytes_to_read, g_tape_stream);
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | (void) fwrite(datablock, 1, (size_t) bytes_to_write, fout); // for blocking reasons, bytes_successfully_read_in isn't necessarily the same as bytes_to_write
|
---|
1108 |
|
---|
1109 | #ifdef EXTRA_TAPE_CHECKSUMS
|
---|
1110 | for (i = 0; i < (int) bytes_to_write; i++) {
|
---|
1111 | ch = datablock[i];
|
---|
1112 | crc16 = updcrcr(crc16, (unsigned) ch);
|
---|
1113 | crctt = updcrc(crctt, (unsigned) ch);
|
---|
1114 | }
|
---|
1115 | #endif
|
---|
1116 | }
|
---|
1117 | log_msg(6, "Total read from tape for this file = %lld",
|
---|
1118 | total_read_from_tape_for_this_file);
|
---|
1119 | log_msg(6, ".......................... Should be %lld", orig_size);
|
---|
1120 | g_tape_posK += total_read_from_tape_for_this_file / 1024;
|
---|
1121 | sprintf(actual_cksum, "%04x%04x", crc16, crctt);
|
---|
1122 | if (foutstream) { /*log_it("Finished writing to foutstream"); */
|
---|
1123 | } else {
|
---|
1124 | paranoid_fclose(fout);
|
---|
1125 | }
|
---|
1126 | res = read_header_block_from_stream(&temp_size, temp_cksum, &ctrl_chr);
|
---|
1127 | if (ctrl_chr != BLK_STOP_FILE) {
|
---|
1128 | wrong_marker(BLK_STOP_FILE, ctrl_chr);
|
---|
1129 | // fatal_error("Bad marker"); // return(1);
|
---|
1130 | }
|
---|
1131 | if (strcmp(temp_cksum, actual_cksum)) {
|
---|
1132 | sprintf(tmp, "actual cksum=%s; recorded cksum=%s", actual_cksum,
|
---|
1133 | temp_cksum);
|
---|
1134 | log_to_screen(tmp);
|
---|
1135 | sprintf(tmp, "%s (%ld K) is corrupt on tape", temp_fname,
|
---|
1136 | (long) orig_size >> 10);
|
---|
1137 | log_to_screen(tmp);
|
---|
1138 | retval++;
|
---|
1139 | } else {
|
---|
1140 | sprintf(tmp, "%s is GOOD on tape", temp_fname);
|
---|
1141 | /* log_it(tmp); */
|
---|
1142 | }
|
---|
1143 | paranoid_free(datablock);
|
---|
1144 | paranoid_free(tmp);
|
---|
1145 | paranoid_free(temp_fname);
|
---|
1146 | paranoid_free(temp_cksum);
|
---|
1147 | paranoid_free(actual_cksum);
|
---|
1148 | return (retval);
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 |
|
---|
1152 |
|
---|
1153 | /**
|
---|
1154 | * Read a header block from the currently opened stream (CD or tape).
|
---|
1155 | * This block indicates the length of the following file (if it's file-related)
|
---|
1156 | * the filename (if it's file-related), and the block type.
|
---|
1157 | * @param plen Where to put the length of the file. Valid only for file-related header blocks.
|
---|
1158 | * @param filename Where to put the name of the file. Valid only for file-related header blocks.
|
---|
1159 | * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
|
---|
1160 | * @return 0 for success, nonzero for failure.
|
---|
1161 | * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
|
---|
1162 | */
|
---|
1163 | int
|
---|
1164 | read_header_block_from_stream(long long *plen, char *filename,
|
---|
1165 | int *pcontrol_char)
|
---|
1166 | {
|
---|
1167 |
|
---|
1168 | /*@ buffers ***************************************************** */
|
---|
1169 | char *tempblock;
|
---|
1170 |
|
---|
1171 | /*@ int ********************************************************* */
|
---|
1172 | int i, retval;
|
---|
1173 |
|
---|
1174 | /*@ end vars *************************************************** */
|
---|
1175 |
|
---|
1176 | tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
|
---|
1177 |
|
---|
1178 | for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
|
---|
1179 | tempblock[i] = 0;
|
---|
1180 | }
|
---|
1181 | while (!(*pcontrol_char = tempblock[7000])) {
|
---|
1182 | g_tape_posK +=
|
---|
1183 | fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
|
---|
1184 | g_tape_stream) / 1024;
|
---|
1185 | }
|
---|
1186 | /* memcpy((char*)plength_of_incoming_file,(char*)tempblock+7001,sizeof(long long)); */
|
---|
1187 | /* for(*plen=0,i=7;i>=0;i--) {*plen<<=8; *plen |= tempblock[7001+i];} */
|
---|
1188 | memcpy((char *) plen, tempblock + 7001, sizeof(long long));
|
---|
1189 | if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
|
---|
1190 | log_it("Bad header block at %ld K", (long) g_tape_posK);
|
---|
1191 | }
|
---|
1192 | strcpy(filename, tempblock + 1000);
|
---|
1193 | /* strcpy(cksum,tempblock+5555);*/
|
---|
1194 | /* log_it( "%s (reading) fname=%s, filesize=%ld K",
|
---|
1195 | marker_to_string (*pcontrol_char), filename,
|
---|
1196 | (long) ((*plen) >> 10));
|
---|
1197 | */
|
---|
1198 | if (*pcontrol_char == BLK_ABORTED_BACKUP) {
|
---|
1199 | log_to_screen("I can't verify an aborted backup.");
|
---|
1200 | retval = 1;
|
---|
1201 | } else {
|
---|
1202 | retval = 0;
|
---|
1203 | }
|
---|
1204 | for (i = 1000; i < 1020; i++) {
|
---|
1205 | if (tempblock[i] < 32 || tempblock[i] > 126) {
|
---|
1206 | tempblock[i] = ' ';
|
---|
1207 | }
|
---|
1208 | }
|
---|
1209 | tempblock[i] = '\0';
|
---|
1210 | log_msg(6, "%s (fname=%s, size=%ld K)",
|
---|
1211 | marker_to_string(*pcontrol_char), tempblock + 1000,
|
---|
1212 | (long) (*plen) >> 10);
|
---|
1213 | paranoid_free(tempblock);
|
---|
1214 | return (retval);
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 |
|
---|
1218 |
|
---|
1219 | /**
|
---|
1220 | * Add specified file/slice to the internal catalog of all archives written.
|
---|
1221 | * This lets us restart on a new CD/tape/whatever if it runs out of room. We just
|
---|
1222 | * write the last [buffer size] MB from the catalog to the new tape, so we know
|
---|
1223 | * we have @e all archives on some CD/tape/whatever.
|
---|
1224 | * @param type The type of file we're cataloging (afioball, slice, something else)
|
---|
1225 | * @param number The fileset number or biggiefile number.
|
---|
1226 | * @param aux The slice number if it's a biggiefile, or any other additional info.
|
---|
1227 | * @param fn The original full pathname of the file we're recording.
|
---|
1228 | * @return The index of the record we just added.
|
---|
1229 | */
|
---|
1230 | int register_in_tape_catalog(t_archtype type, int number, long aux,
|
---|
1231 | char *fn)
|
---|
1232 | {
|
---|
1233 | int last;
|
---|
1234 | char fname[MAX_TAPECAT_FNAME_LEN];
|
---|
1235 | char *p;
|
---|
1236 |
|
---|
1237 | p = strrchr(fn, '/');
|
---|
1238 | if (p) {
|
---|
1239 | p++;
|
---|
1240 | } else {
|
---|
1241 | p = fn;
|
---|
1242 | }
|
---|
1243 | strncpy(fname, p, MAX_TAPECAT_FNAME_LEN);
|
---|
1244 | fname[MAX_TAPECAT_FNAME_LEN] = '\0';
|
---|
1245 | last = g_tapecatalog->entries;
|
---|
1246 | if (last >= MAX_TAPECATALOG_ENTRIES) {
|
---|
1247 | log_it
|
---|
1248 | ("Warning - can't log #%d in tape catalog - too many entries already",
|
---|
1249 | number);
|
---|
1250 | return (-1);
|
---|
1251 | }
|
---|
1252 | g_tapecatalog->el[last].type = type;
|
---|
1253 | g_tapecatalog->el[last].number = number;
|
---|
1254 | g_tapecatalog->el[last].aux = aux;
|
---|
1255 | g_tapecatalog->el[last].tape_posK = g_tape_posK;
|
---|
1256 | strcpy(g_tapecatalog->el[last].fname, fname);
|
---|
1257 | g_tapecatalog->entries++;
|
---|
1258 | return (last); // returns the index of the record we've jsut added
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 |
|
---|
1262 |
|
---|
1263 |
|
---|
1264 | /**
|
---|
1265 | * Decide whether we should start a new tape. This is TRUE if we've run out of tape
|
---|
1266 | * (got SIGPIPE) or look like we will.
|
---|
1267 | * @param mediasize The size of the tape in megabytes.
|
---|
1268 | * @param length_of_incoming_file The length of the file we're about to write, in bytes.
|
---|
1269 | * @bug This seems like it'll only work for media_size != autodetect, but Mondo only allows
|
---|
1270 | * autodetecting the size. Huh?
|
---|
1271 | */
|
---|
1272 |
|
---|
1273 | /* BERLIOS: Should be reviewed for mediasize being a off_t ??? */
|
---|
1274 | bool
|
---|
1275 | should_we_write_to_next_tape(long mediasize,
|
---|
1276 | off_t length_of_incoming_file)
|
---|
1277 | {
|
---|
1278 | /*@ bool's ***************************************************** */
|
---|
1279 | bool we_need_a_new_tape = FALSE;
|
---|
1280 |
|
---|
1281 | /*@ end vars *************************************************** */
|
---|
1282 |
|
---|
1283 | if (mediasize == 0) {
|
---|
1284 | return (FALSE);
|
---|
1285 | }
|
---|
1286 | if (mediasize > 0 && (g_tape_posK >> 10 >= mediasize)) {
|
---|
1287 | log_it("mediasize = %ld", mediasize);
|
---|
1288 | we_need_a_new_tape = TRUE;
|
---|
1289 | log_to_screen("Should have started a new tape/CD already");
|
---|
1290 | }
|
---|
1291 | if ((g_tape_posK + length_of_incoming_file / 1024) >> 10 >=
|
---|
1292 | mediasize - (SLICE_SIZE * 4 / 1024)) {
|
---|
1293 | log_it("g_tape_posK = %ld\nmediasize = %ld\n", g_tape_posK,
|
---|
1294 | mediasize);
|
---|
1295 | we_need_a_new_tape = TRUE;
|
---|
1296 | }
|
---|
1297 | return (we_need_a_new_tape);
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 |
|
---|
1301 | /**
|
---|
1302 | * Seek through the stream until we find a header block where the NAME field matches
|
---|
1303 | * @p the_file_I_was_reading. This is useful if you've just started reading from
|
---|
1304 | * a new tape and want to find the file you were reading when the tape ended.
|
---|
1305 | * @param the_file_I_was_reading File name to look for.
|
---|
1306 | * @return 0 for success, nonzero for failure.
|
---|
1307 | */
|
---|
1308 | int skip_incoming_files_until_we_find_this_one(char
|
---|
1309 | *the_file_I_was_reading)
|
---|
1310 | {
|
---|
1311 | char *pA;
|
---|
1312 | char *pB;
|
---|
1313 | int res;
|
---|
1314 | int ctrl_chr;
|
---|
1315 | char *temp_fname;
|
---|
1316 | char *datablock;
|
---|
1317 | long long temp_size, size;
|
---|
1318 | long bytes_to_write;
|
---|
1319 |
|
---|
1320 | datablock = malloc(TAPE_BLOCK_SIZE);
|
---|
1321 | malloc_string(temp_fname);
|
---|
1322 | pB = strrchr(the_file_I_was_reading, '/');
|
---|
1323 | if (pB) {
|
---|
1324 | pB++;
|
---|
1325 | } else {
|
---|
1326 | pB = the_file_I_was_reading;
|
---|
1327 | }
|
---|
1328 | log_msg(1, "skip_incoming_..(%s)", pB);
|
---|
1329 | log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
|
---|
1330 | ctrl_chr = -1;
|
---|
1331 | while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
|
---|
1332 | res =
|
---|
1333 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1334 | &ctrl_chr);
|
---|
1335 | if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
|
---|
1336 | break;
|
---|
1337 | }
|
---|
1338 | log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
|
---|
1339 | wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
|
---|
1340 | log_msg(3, "Still trying to re-sync w/ tape");
|
---|
1341 | }
|
---|
1342 | while (ctrl_chr != BLK_START_FILE) {
|
---|
1343 | res =
|
---|
1344 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1345 | &ctrl_chr);
|
---|
1346 | if (ctrl_chr == BLK_START_FILE) {
|
---|
1347 | break;
|
---|
1348 | }
|
---|
1349 | log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
|
---|
1350 | wrong_marker(BLK_START_FILE, ctrl_chr);
|
---|
1351 | log_msg(3, "Still trying to re-sync w/ tape");
|
---|
1352 | }
|
---|
1353 | pA = strrchr(temp_fname, '/');
|
---|
1354 | if (pA) {
|
---|
1355 | pA++;
|
---|
1356 | } else {
|
---|
1357 | pA = temp_fname;
|
---|
1358 | }
|
---|
1359 | pB = strrchr(the_file_I_was_reading, '/');
|
---|
1360 | if (pB) {
|
---|
1361 | pB++;
|
---|
1362 | } else {
|
---|
1363 | pB = the_file_I_was_reading;
|
---|
1364 | }
|
---|
1365 | while (strcmp(pA, pB)) {
|
---|
1366 | log_msg(6, "Skipping %s (it's not %s)", temp_fname,
|
---|
1367 | the_file_I_was_reading);
|
---|
1368 | for (size = temp_size; size > 0; size -= bytes_to_write) {
|
---|
1369 | bytes_to_write =
|
---|
1370 | (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
|
---|
1371 | // FIXME - needs error-checking and -catching
|
---|
1372 | fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream);
|
---|
1373 | }
|
---|
1374 | res =
|
---|
1375 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1376 | &ctrl_chr);
|
---|
1377 | if (ctrl_chr != BLK_STOP_FILE) {
|
---|
1378 | wrong_marker(BLK_STOP_FILE, ctrl_chr);
|
---|
1379 | }
|
---|
1380 | res =
|
---|
1381 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1382 | &ctrl_chr);
|
---|
1383 | if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
|
---|
1384 | wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
|
---|
1385 | }
|
---|
1386 | res =
|
---|
1387 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1388 | &ctrl_chr);
|
---|
1389 | if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
|
---|
1390 | wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
|
---|
1391 | }
|
---|
1392 | res =
|
---|
1393 | read_header_block_from_stream(&temp_size, temp_fname,
|
---|
1394 | &ctrl_chr);
|
---|
1395 | if (ctrl_chr != BLK_START_FILE) {
|
---|
1396 | wrong_marker(BLK_START_FILE, ctrl_chr);
|
---|
1397 | }
|
---|
1398 | pA = strrchr(temp_fname, '/');
|
---|
1399 | if (pA) {
|
---|
1400 | pA++;
|
---|
1401 | } else {
|
---|
1402 | pA = temp_fname;
|
---|
1403 | }
|
---|
1404 | pB = strrchr(the_file_I_was_reading, '/');
|
---|
1405 | if (pB) {
|
---|
1406 | pB++;
|
---|
1407 | } else {
|
---|
1408 | pB = the_file_I_was_reading;
|
---|
1409 | }
|
---|
1410 | }
|
---|
1411 | log_msg(2, "Reading %s (it matches %s)", temp_fname,
|
---|
1412 | the_file_I_was_reading);
|
---|
1413 | paranoid_free(temp_fname);
|
---|
1414 | paranoid_free(datablock);
|
---|
1415 | return (0);
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 |
|
---|
1419 | /**
|
---|
1420 | * Start to read from the next tape. Assumes the user has already inserted it.
|
---|
1421 | * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
|
---|
1422 | * @return 0 for success, nonzero for failure.
|
---|
1423 | */
|
---|
1424 | int start_to_read_from_next_tape()
|
---|
1425 | {
|
---|
1426 | /*@ int ********************************************************* */
|
---|
1427 | int res = 0;
|
---|
1428 | char *sz_msg;
|
---|
1429 | int ctrlchr;
|
---|
1430 | long long temp_size;
|
---|
1431 | malloc_string(sz_msg);
|
---|
1432 | /*@ end vars *************************************************** */
|
---|
1433 |
|
---|
1434 | paranoid_pclose(g_tape_stream);
|
---|
1435 | sync();
|
---|
1436 | sync();
|
---|
1437 | sync();
|
---|
1438 | log_it("Next tape requested.");
|
---|
1439 | insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
|
---|
1440 | log_it("Opening IN the next tape");
|
---|
1441 | if (!
|
---|
1442 | (g_tape_stream =
|
---|
1443 | open_device_via_buffer(bkpinfo->media_device, 'r',
|
---|
1444 | bkpinfo->internal_tape_block_size))) {
|
---|
1445 | log_OS_error(g_tape_fifo);
|
---|
1446 | log_to_screen("Cannot openin stream device");
|
---|
1447 | return (1);
|
---|
1448 | }
|
---|
1449 | g_tape_posK = 0;
|
---|
1450 | g_sigpipe = FALSE;
|
---|
1451 | res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
|
---|
1452 | if (ctrlchr != BLK_START_OF_TAPE) {
|
---|
1453 | wrong_marker(BLK_START_OF_TAPE, ctrlchr);
|
---|
1454 | }
|
---|
1455 | res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
|
---|
1456 | if (ctrlchr != BLK_START_OF_BACKUP) {
|
---|
1457 | wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
|
---|
1458 | } else {
|
---|
1459 | log_msg(3, "Next tape opened OK. Whoopee!");
|
---|
1460 | }
|
---|
1461 | paranoid_free(sz_msg);
|
---|
1462 | return (res);
|
---|
1463 | }
|
---|
1464 |
|
---|
1465 |
|
---|
1466 |
|
---|
1467 | /**
|
---|
1468 | * Start to write to the next tape. Assume the user has already inserted it.
|
---|
1469 | * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
|
---|
1470 | * @return 0 for success, nonzero for failure.
|
---|
1471 | */
|
---|
1472 | int start_to_write_to_next_tape()
|
---|
1473 | {
|
---|
1474 | int res = 0;
|
---|
1475 | char command[MAX_STR_LEN * 2];
|
---|
1476 | paranoid_pclose(g_tape_stream);
|
---|
1477 | system("sync");
|
---|
1478 | system("sync");
|
---|
1479 | system("sync");
|
---|
1480 | log_it("New tape requested.");
|
---|
1481 | insist_on_this_tape_number(g_current_media_number + 1); // will increment g_current_media, too
|
---|
1482 | if (g_current_media_number > MAX_NOOF_MEDIA) {
|
---|
1483 | res++;
|
---|
1484 | log_to_screen("Too many tapes. Man, you need to use nfs!");
|
---|
1485 | }
|
---|
1486 | if (bkpinfo->backup_media_type == cdstream) {
|
---|
1487 | sprintf(command,
|
---|
1488 | "cdrecord -eject dev=%s speed=%d fs=24m -waiti - >> %s 2>> %s",
|
---|
1489 | bkpinfo->media_device, bkpinfo->cdrw_speed, MONDO_LOGFILE,
|
---|
1490 | MONDO_LOGFILE);
|
---|
1491 | log_it("Opening OUT to next CD with the command");
|
---|
1492 | log_it(command);
|
---|
1493 | log_it("Let's see what happens, shall we?");
|
---|
1494 | g_tape_stream = popen(command, "w");
|
---|
1495 | if (!g_tape_stream) {
|
---|
1496 | log_to_screen("Failed to openout to cdstream (fifo)");
|
---|
1497 | return (1);
|
---|
1498 | }
|
---|
1499 | } else {
|
---|
1500 | log_it("Opening OUT to next tape");
|
---|
1501 | if (!
|
---|
1502 | (g_tape_stream =
|
---|
1503 | open_device_via_buffer(bkpinfo->media_device, 'w',
|
---|
1504 | bkpinfo->internal_tape_block_size))) {
|
---|
1505 | log_OS_error(g_tape_fifo);
|
---|
1506 | log_to_screen("Cannot openin stream device");
|
---|
1507 | return (1);
|
---|
1508 | }
|
---|
1509 | }
|
---|
1510 | g_tape_posK = 0;
|
---|
1511 | g_sigpipe = FALSE;
|
---|
1512 | res += write_header_block_to_stream((off_t)0, "start-of-tape", BLK_START_OF_TAPE); /* just in case */
|
---|
1513 | res += write_header_block_to_stream((off_t)0, "start-of-backup", BLK_START_OF_BACKUP); /* just in case */
|
---|
1514 | return (res);
|
---|
1515 | }
|
---|
1516 |
|
---|
1517 |
|
---|
1518 |
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Write a bufferfull of the most recent archives to the tape. The
|
---|
1522 | * rationale behind this is a bit complex. If the previous tape ended
|
---|
1523 | * suddenly (EOF or otherwise) some archives were probably left in the
|
---|
1524 | * buffer. That means that Mondo thinks they have been written, but
|
---|
1525 | * the external binary @c buffer has not actually written them. So to
|
---|
1526 | * be safe, we start the new tape by writing the last bufferfull from
|
---|
1527 | * the old one. This insures that all archives will be on at least
|
---|
1528 | * one tape. Sounds inelegant, but it works.
|
---|
1529 | * @param bkpinfo The backup information structure. @c bkpinfo->tmpdir is the only field used.
|
---|
1530 | * @return 0 for success, nonzero for failure.
|
---|
1531 | */
|
---|
1532 | int write_backcatalog_to_tape()
|
---|
1533 | {
|
---|
1534 | int i, last, res = 0;
|
---|
1535 | char *fname;
|
---|
1536 |
|
---|
1537 | log_msg(2, "I am now writing back catalog to tape");
|
---|
1538 | malloc_string(fname);
|
---|
1539 | last = g_tapecatalog->entries - 1;
|
---|
1540 | for (i = 0; i <= last; i++) {
|
---|
1541 | sprintf(fname, "%s/tmpfs/backcatalog/%s", bkpinfo->tmpdir,
|
---|
1542 | g_tapecatalog->el[i].fname);
|
---|
1543 | if (!does_file_exist(fname)) {
|
---|
1544 | log_msg(6, "Can't write %s - it doesn't exist.", fname);
|
---|
1545 | } else {
|
---|
1546 | write_header_block_to_stream(length_of_file(fname),
|
---|
1547 | "start-backcatalog-afio-or-slice",
|
---|
1548 | BLK_START_AN_AFIO_OR_SLICE);
|
---|
1549 | log_msg(2, "Writing %s", fname);
|
---|
1550 | if (write_file_to_stream_from_file(fname)) {
|
---|
1551 | res++;
|
---|
1552 | log_msg(2, "%s failed", fname);
|
---|
1553 | }
|
---|
1554 | if (i != last) {
|
---|
1555 | write_header_block_to_stream((off_t)0,
|
---|
1556 | "stop-backcatalog-afio-or-slice",
|
---|
1557 | BLK_STOP_AN_AFIO_OR_SLICE);
|
---|
1558 | }
|
---|
1559 | }
|
---|
1560 | }
|
---|
1561 | paranoid_free(fname);
|
---|
1562 | log_msg(2, "Finished writing back catalog to tape");
|
---|
1563 | return (res);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 |
|
---|
1567 |
|
---|
1568 | /**
|
---|
1569 | * Write all.tar.gz (produced by Mindi) to the first 32M of the first tape.
|
---|
1570 | * @param fname The path to all.tar.gz.
|
---|
1571 | * @return 0 for success, nonzero for failure.
|
---|
1572 | */
|
---|
1573 | int write_data_disks_to_stream(char *fname)
|
---|
1574 | {
|
---|
1575 | /*@ pointers *************************************************** */
|
---|
1576 | FILE *fin;
|
---|
1577 | char tmp[MAX_STR_LEN];
|
---|
1578 |
|
---|
1579 | /*@ long ******************************************************* */
|
---|
1580 | long m = -1;
|
---|
1581 | long templong;
|
---|
1582 |
|
---|
1583 | /*@ int ******************************************************** */
|
---|
1584 | int i, j;
|
---|
1585 |
|
---|
1586 | /*@ buffers **************************************************** */
|
---|
1587 | char tempblock[256 * 1024];
|
---|
1588 |
|
---|
1589 | /*@ end vars *************************************************** */
|
---|
1590 |
|
---|
1591 | open_evalcall_form("Writing data disks to tape");
|
---|
1592 | log_to_screen("Writing data disks to tape");
|
---|
1593 | log_it("Data disks = %s", fname);
|
---|
1594 | if (!does_file_exist(fname)) {
|
---|
1595 | sprintf(tmp, "Cannot find %s", fname);
|
---|
1596 | log_to_screen(tmp);
|
---|
1597 | return (1);
|
---|
1598 | }
|
---|
1599 | if (!(fin = fopen(fname, "r"))) {
|
---|
1600 | log_OS_error(fname);
|
---|
1601 | fatal_error("Cannot openin the data disk");
|
---|
1602 | }
|
---|
1603 | for (i = 0; i < 32; i++) { /* 32MB */
|
---|
1604 | for (j = 0; j < 4; j++) { /* 256K x 4 = 1MB (1024K) */
|
---|
1605 | if (!feof(fin)) {
|
---|
1606 | m = (long) fread(tempblock, 1, 256 * 1024, fin);
|
---|
1607 | } else {
|
---|
1608 | m = 0;
|
---|
1609 | }
|
---|
1610 | for (; m < 256 * 1024; m++) {
|
---|
1611 | tempblock[m] = '\0';
|
---|
1612 | }
|
---|
1613 | g_tape_posK +=
|
---|
1614 | fwrite(tempblock, 1, 256 * 1024, g_tape_stream) / 1024;
|
---|
1615 | }
|
---|
1616 | if (i > g_tape_buffer_size_MB) // otherwise, 'buffer' distorts calculations
|
---|
1617 | {
|
---|
1618 | templong = ((i - 8) * 4 + j) * 100 / (128 - 8 * 4);
|
---|
1619 | update_evalcall_form((int) (templong));
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 | paranoid_fclose(fin);
|
---|
1623 | close_evalcall_form();
|
---|
1624 | return (0);
|
---|
1625 | }
|
---|
1626 |
|
---|
1627 |
|
---|
1628 |
|
---|
1629 |
|
---|
1630 | /**
|
---|
1631 | * Copy @p infile to the opened stream (CD or tape).
|
---|
1632 | * @param bkpinfo The backup information structure. @c bkpinfo->media_size is the only field used.
|
---|
1633 | * @param infile The file to write to the stream.
|
---|
1634 | * @return 0 for success, nonzero for failure.
|
---|
1635 | */
|
---|
1636 | int write_file_to_stream_from_file(char *infile)
|
---|
1637 | {
|
---|
1638 | /*@ buffers **************************************************** */
|
---|
1639 | char tmp[MAX_STR_LEN];
|
---|
1640 | char datablock[TAPE_BLOCK_SIZE];
|
---|
1641 | char checksum[MAX_STR_LEN];
|
---|
1642 | char *infile_basename;
|
---|
1643 |
|
---|
1644 | /*@ int ******************************************************** */
|
---|
1645 | int retval = 0;
|
---|
1646 | int noof_blocks;
|
---|
1647 |
|
---|
1648 | /* unsigned int ch; */
|
---|
1649 | unsigned int crc16;
|
---|
1650 | unsigned int crctt;
|
---|
1651 |
|
---|
1652 | /*@ pointers *************************************************** */
|
---|
1653 | FILE *fin;
|
---|
1654 | char *p;
|
---|
1655 |
|
---|
1656 | /*@ long ******************************************************* */
|
---|
1657 | long bytes_to_read = 0;
|
---|
1658 | long i;
|
---|
1659 |
|
---|
1660 | off_t filesize;
|
---|
1661 |
|
---|
1662 | #ifdef EXTRA_TAPE_CHECKSUMS
|
---|
1663 | int ch;
|
---|
1664 | #endif
|
---|
1665 |
|
---|
1666 | /*@ initialize ************************************************ */
|
---|
1667 | crc16 = 0;
|
---|
1668 | crctt = 0;
|
---|
1669 |
|
---|
1670 |
|
---|
1671 |
|
---|
1672 | /*@ end vars *************************************************** */
|
---|
1673 |
|
---|
1674 | infile_basename = strrchr(infile, '/');
|
---|
1675 | if (infile_basename) {
|
---|
1676 | infile_basename++;
|
---|
1677 | } else {
|
---|
1678 | infile_basename = infile;
|
---|
1679 | }
|
---|
1680 | filesize = length_of_file(infile);
|
---|
1681 | if (should_we_write_to_next_tape
|
---|
1682 | (bkpinfo->media_size[g_current_media_number], filesize)) {
|
---|
1683 | start_to_write_to_next_tape();
|
---|
1684 | write_backcatalog_to_tape();
|
---|
1685 | }
|
---|
1686 | p = strrchr(infile, '/');
|
---|
1687 | if (!p) {
|
---|
1688 | p = infile;
|
---|
1689 | } else {
|
---|
1690 | p++;
|
---|
1691 | }
|
---|
1692 | sprintf(tmp, "Writing file '%s' to tape (%ld KB)", p,
|
---|
1693 | (long) filesize >> 10);
|
---|
1694 | log_it(tmp);
|
---|
1695 | write_header_block_to_stream(filesize, infile_basename,
|
---|
1696 | BLK_START_FILE);
|
---|
1697 | //go_here_to_restart_saving_of_file:
|
---|
1698 | if (!(fin = fopen(infile, "r"))) {
|
---|
1699 | log_OS_error(infile);
|
---|
1700 | return (1);
|
---|
1701 | }
|
---|
1702 | for (noof_blocks = 0; filesize > 0;
|
---|
1703 | noof_blocks++, filesize -= bytes_to_read) {
|
---|
1704 | if (filesize < TAPE_BLOCK_SIZE) {
|
---|
1705 | bytes_to_read = (long) filesize;
|
---|
1706 | for (i = 0; i < TAPE_BLOCK_SIZE; i++) {
|
---|
1707 | datablock[i] = '\0';
|
---|
1708 | }
|
---|
1709 | } else {
|
---|
1710 | bytes_to_read = TAPE_BLOCK_SIZE;
|
---|
1711 | }
|
---|
1712 | (void) fread(datablock, 1, (size_t) bytes_to_read, fin);
|
---|
1713 | g_tape_posK +=
|
---|
1714 | fwrite(datablock, 1, /*bytes_to_read */
|
---|
1715 | (size_t) TAPE_BLOCK_SIZE,
|
---|
1716 | g_tape_stream) / 1024;
|
---|
1717 | if (g_sigpipe) {
|
---|
1718 | iamhere("Sigpipe occurred recently. I'll start a new tape.");
|
---|
1719 | fclose(fin);
|
---|
1720 | g_sigpipe = FALSE;
|
---|
1721 | start_to_write_to_next_tape();
|
---|
1722 | write_backcatalog_to_tape(); // kinda-sorta recursive :)
|
---|
1723 | return (0);
|
---|
1724 | }
|
---|
1725 | #ifdef EXTRA_TAPE_CHECKSUMS
|
---|
1726 | for (i = 0; i < bytes_to_read; i++) {
|
---|
1727 | ch = datablock[i];
|
---|
1728 | crc16 = updcrcr(crc16, (unsigned) ch);
|
---|
1729 | crctt = updcrc(crctt, (unsigned) ch);
|
---|
1730 | }
|
---|
1731 | #endif
|
---|
1732 | }
|
---|
1733 | paranoid_fclose(fin);
|
---|
1734 | sprintf(checksum, "%04x%04x", crc16, crctt);
|
---|
1735 | /* BERLIOS: what does it do ??? */
|
---|
1736 | write_header_block_to_stream((off_t)g_current_media_number, checksum,
|
---|
1737 | BLK_STOP_FILE);
|
---|
1738 | // log_it("File '%s' written to tape.", infile);
|
---|
1739 | return (retval);
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 |
|
---|
1743 |
|
---|
1744 |
|
---|
1745 |
|
---|
1746 | /**
|
---|
1747 | * Write a header block to the opened stream (CD or tape).
|
---|
1748 | * @param length_of_incoming_file The length to store in the header block.
|
---|
1749 | * Usually matters only if this is a file-related header, in which case it should
|
---|
1750 | * be the length of the file that will follow.
|
---|
1751 | * @param filename The filename to store in the header block. Usually matters
|
---|
1752 | * only if this is a file-related header, in which case this should be the name
|
---|
1753 | * if the file that will follow.
|
---|
1754 | * @param control_char The type of header block this is (start-file, end-file, start-tape, ...)
|
---|
1755 | * @return 0 for success, nonzero for failure.
|
---|
1756 | */
|
---|
1757 | int
|
---|
1758 | write_header_block_to_stream(off_t length_of_incoming_file,
|
---|
1759 | char *filename, int control_char)
|
---|
1760 | {
|
---|
1761 | /*@ buffers **************************************************** */
|
---|
1762 | char tempblock[TAPE_BLOCK_SIZE];
|
---|
1763 | char tmp[MAX_STR_LEN];
|
---|
1764 | char *p;
|
---|
1765 |
|
---|
1766 | /*@ int ******************************************************** */
|
---|
1767 | int i;
|
---|
1768 |
|
---|
1769 | off_t olen;
|
---|
1770 |
|
---|
1771 | /*@ end vars *************************************************** */
|
---|
1772 |
|
---|
1773 |
|
---|
1774 | olen = length_of_incoming_file;
|
---|
1775 | p = strrchr(filename, '/'); /* Make 'em go, "Unnnh!" Oh wait, that was _Master_ P... */
|
---|
1776 | if (!p) {
|
---|
1777 | p = filename;
|
---|
1778 | } else {
|
---|
1779 | p++;
|
---|
1780 | }
|
---|
1781 | if (!g_tape_stream) {
|
---|
1782 | log_to_screen
|
---|
1783 | ("You're not backing up to tape. Why write a tape header?");
|
---|
1784 | return (1);
|
---|
1785 | }
|
---|
1786 | for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
|
---|
1787 | tempblock[i] = 0;
|
---|
1788 | }
|
---|
1789 | sprintf(tempblock + 6000 + control_char, STR_HEADER);
|
---|
1790 | tempblock[7000] = control_char;
|
---|
1791 | /* for(i=0;i<8;i++) {tempblock[7001+i]=olen&0xff; olen>>=8;} */
|
---|
1792 | memcpy(tempblock + 7001, (char *) &olen, sizeof(off_t));
|
---|
1793 | /* if (length_of_incoming_file) {memcpy(tempblock+7001,(char*)&length_of_incoming_file,sizeof(long long));} */
|
---|
1794 | strcpy(tempblock + 1000, filename);
|
---|
1795 | /* strcpy(tempblock+5555,cksum); */
|
---|
1796 | g_tape_posK +=
|
---|
1797 | fwrite(tempblock, 1, (size_t) TAPE_BLOCK_SIZE,
|
---|
1798 | g_tape_stream) / 1024;
|
---|
1799 | sprintf(tmp, "%s (fname=%s, size=%ld K)",
|
---|
1800 | marker_to_string(control_char), p,
|
---|
1801 | (long) length_of_incoming_file >> 10);
|
---|
1802 | log_msg(6, tmp);
|
---|
1803 | /* log_tape_pos(); */
|
---|
1804 | return (0);
|
---|
1805 | }
|
---|
1806 |
|
---|
1807 |
|
---|
1808 |
|
---|
1809 |
|
---|
1810 |
|
---|
1811 |
|
---|
1812 |
|
---|
1813 |
|
---|
1814 |
|
---|
1815 |
|
---|
1816 | /**
|
---|
1817 | * Log (to screen) an erroneous marker, along with what it should have been.
|
---|
1818 | * @param should_be What we were expecting.
|
---|
1819 | * @param it_is What we got.
|
---|
1820 | */
|
---|
1821 | void wrong_marker(int should_be, int it_is)
|
---|
1822 | {
|
---|
1823 | /*@ buffer ***************************************************** */
|
---|
1824 | char tmp[MAX_STR_LEN];
|
---|
1825 |
|
---|
1826 |
|
---|
1827 | /*@ end vars *************************************************** */
|
---|
1828 | sprintf(tmp, "Wrong marker! (Should be %s, ",
|
---|
1829 | marker_to_string(should_be));
|
---|
1830 | sprintf(tmp + strlen(tmp), "is actually %s)", marker_to_string(it_is));
|
---|
1831 | log_to_screen(tmp);
|
---|
1832 | }
|
---|
1833 |
|
---|
1834 | /* @} - end of streamGroup */
|
---|