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