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