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