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