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