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