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