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