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