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