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