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