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