[2030] | 1 | /*
|
---|
[128] | 2 | $Id: libmondo-filelist.c 2549 2010-01-25 13:38:47Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Functions which create, chop, and edit the filelist.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include "my-stuff.h"
|
---|
| 11 | #include "mondostructures.h"
|
---|
[541] | 12 | #include "lib-common-externs.h"
|
---|
[1] | 13 | #include "libmondo-filelist.h"
|
---|
| 14 | #include "libmondo-string-EXT.h"
|
---|
| 15 | #include "libmondo-files-EXT.h"
|
---|
| 16 | #include "libmondo-fork-EXT.h"
|
---|
[541] | 17 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 18 | #include "libmondo-tools-EXT.h"
|
---|
[2211] | 19 | #include "mr_mem.h"
|
---|
[2241] | 20 | #include "mr_str.h"
|
---|
[1] | 21 |
|
---|
| 22 | #include <time.h>
|
---|
| 23 | #include <stdio.h>
|
---|
| 24 | #include <sys/types.h>
|
---|
| 25 | #include <sys/stat.h>
|
---|
| 26 | #include <dirent.h>
|
---|
| 27 | #include <errno.h>
|
---|
| 28 | #include <stdio.h>
|
---|
| 29 |
|
---|
| 30 |
|
---|
[128] | 31 | extern ssize_t getline(char **lineptr, size_t * n, FILE * stream);
|
---|
[1316] | 32 | extern char *MONDO_LOGFILE;
|
---|
[1] | 33 |
|
---|
[1645] | 34 | /* Reference to global bkpinfo */
|
---|
| 35 | extern struct s_bkpinfo *bkpinfo;
|
---|
[1] | 36 |
|
---|
[1645] | 37 |
|
---|
[1] | 38 | /*@unused@*/
|
---|
[128] | 39 | //static char cvsid[] = "$Id: libmondo-filelist.c 2549 2010-01-25 13:38:47Z bruno $";
|
---|
[1] | 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Number of lines in the filelist last loaded.
|
---|
| 43 | * @warning This implies that two filesets cannot be loaded at once.
|
---|
| 44 | * @ingroup globalGroup
|
---|
| 45 | */
|
---|
| 46 | long g_original_noof_lines_in_filelist = 0;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Number of filesets in the current backup.
|
---|
| 50 | * @ingroup globalGroup
|
---|
| 51 | */
|
---|
[128] | 52 | long g_noof_sets = 0;
|
---|
[1] | 53 |
|
---|
| 54 | extern bool g_text_mode;
|
---|
| 55 | extern newtComponent g_progressForm;
|
---|
| 56 | extern int g_currentY;
|
---|
| 57 | extern int g_noof_rows;
|
---|
| 58 |
|
---|
[944] | 59 | extern char *g_getfacl;
|
---|
| 60 | extern char *g_getfattr;
|
---|
[1] | 61 |
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | /**
|
---|
| 65 | * @addtogroup filelistGroup
|
---|
| 66 | * @{
|
---|
| 67 | */
|
---|
| 68 | /**
|
---|
| 69 | * Call chop_filelist() to chop the filelist into sets.
|
---|
| 70 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 71 | * - @c bkpinfo->image_devs
|
---|
| 72 | * - @c bkpinfo->optimal_set_size
|
---|
| 73 | * - @c bkpinfo->scratchdir
|
---|
| 74 | * - @c bkpinfo->tmpdir
|
---|
| 75 | * @see chop_filelist
|
---|
| 76 | */
|
---|
[1645] | 77 | int call_filelist_chopper()
|
---|
[1] | 78 | {
|
---|
[128] | 79 | /*@ buffers *********************** */
|
---|
[2521] | 80 | char *dev = NULL;
|
---|
| 81 | char *filelist = NULL;
|
---|
| 82 | char *tempfile = NULL;
|
---|
[128] | 83 | long noof_sets;
|
---|
[1] | 84 |
|
---|
[128] | 85 | /*@ pointers ********************** */
|
---|
| 86 | char *ptr;
|
---|
| 87 | FILE *fout;
|
---|
[1] | 88 |
|
---|
[128] | 89 | /*@ int *************************** */
|
---|
| 90 | int i, retval = 0;
|
---|
[1] | 91 |
|
---|
[541] | 92 | mvaddstr_and_log_it(g_currentY, 0, "Dividing filelist into sets");
|
---|
[1] | 93 |
|
---|
[541] | 94 | log_to_screen("Dividing filelist into sets. Please wait.");
|
---|
[128] | 95 | i = 0;
|
---|
[2521] | 96 | mr_asprintf(&filelist, "%s/archives/filelist.full", bkpinfo->scratchdir);
|
---|
[128] | 97 | if (!does_file_exist(filelist)) {
|
---|
| 98 | log_it("filelist %s not found", filelist);
|
---|
[2521] | 99 | mr_free(filelist);
|
---|
[128] | 100 | fatal_error("call_filelist_chopper() -- filelist not found!");
|
---|
| 101 | }
|
---|
[1] | 102 |
|
---|
[2521] | 103 | noof_sets = chop_filelist(filelist, bkpinfo->tmpdir, bkpinfo->optimal_set_size);
|
---|
| 104 | mr_free(filelist);
|
---|
[1645] | 105 | estimate_noof_media_required(noof_sets); // for cosmetic purposes
|
---|
[1] | 106 |
|
---|
[2521] | 107 | mr_asprintf(&tempfile, "%s/biggielist.txt", bkpinfo->tmpdir);
|
---|
[128] | 108 | if (!(fout = fopen(tempfile, "a"))) {
|
---|
| 109 | log_OS_error("Cannot append to biggielist");
|
---|
| 110 | retval++;
|
---|
[2521] | 111 | mr_free(tempfile);
|
---|
| 112 | return (retval);
|
---|
[128] | 113 | }
|
---|
[2521] | 114 | mr_free(tempfile);
|
---|
| 115 |
|
---|
[128] | 116 | log_it(bkpinfo->image_devs);
|
---|
[1] | 117 |
|
---|
[128] | 118 | ptr = bkpinfo->image_devs;
|
---|
[1] | 119 |
|
---|
[2521] | 120 | malloc_string(dev);
|
---|
[128] | 121 | while (ptr && *ptr) {
|
---|
| 122 | strcpy(dev, ptr);
|
---|
| 123 | log_it("Examining imagedev %s", dev);
|
---|
| 124 | for (i = 0; i < (int) strlen(dev) && dev[i] != ' '; i++);
|
---|
| 125 | dev[i] = '\0';
|
---|
| 126 | if (!strlen(dev)) {
|
---|
| 127 | continue;
|
---|
| 128 | }
|
---|
| 129 | fprintf(fout, "%s\n", dev);
|
---|
| 130 | log_it("Adding '%s' to biggielist", dev);
|
---|
| 131 | if ((ptr = strchr(ptr, ' '))) {
|
---|
| 132 | ptr++;
|
---|
| 133 | }
|
---|
[1] | 134 | }
|
---|
[128] | 135 | paranoid_fclose(fout);
|
---|
| 136 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
[1] | 137 |
|
---|
[128] | 138 | paranoid_free(dev);
|
---|
| 139 | return (retval);
|
---|
[1] | 140 | }
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 |
|
---|
[128] | 144 | int sort_file(char *orig_fname)
|
---|
[1] | 145 | {
|
---|
[2521] | 146 | char *tmp_fname = NULL;
|
---|
| 147 | char *command = NULL;
|
---|
[128] | 148 | int retval = 0;
|
---|
[1] | 149 |
|
---|
[1475] | 150 | log_msg(5, "Sorting file %s", orig_fname);
|
---|
[1] | 151 |
|
---|
[128] | 152 | if (!does_file_exist(orig_fname)) {
|
---|
[1475] | 153 | log_msg(2, "file %s empty", orig_fname);
|
---|
[128] | 154 | return (0);
|
---|
| 155 | } // no sense in trying to sort an empty file
|
---|
| 156 |
|
---|
[2521] | 157 | mr_asprintf(&tmp_fname, "%s/sortfile", bkpinfo->tmpdir);
|
---|
| 158 |
|
---|
| 159 | mr_asprintf(&command, "sort %s > %s 2>> %s", orig_fname, tmp_fname, MONDO_LOGFILE);
|
---|
[128] | 160 | retval = system(command);
|
---|
[2521] | 161 | mr_free(command);
|
---|
| 162 |
|
---|
[128] | 163 | if (retval) {
|
---|
| 164 | log_msg(2, "Failed to sort %s - oh dear", orig_fname);
|
---|
| 165 | } else {
|
---|
[2521] | 166 | log_msg(5, "Sorted %s --> %s OK. Copying it back to %s now", orig_fname, tmp_fname, orig_fname);
|
---|
| 167 | mr_asprintf(&command, "mv -f %s %s", tmp_fname, orig_fname);
|
---|
[1475] | 168 | retval += run_program_and_log_output(command, 5);
|
---|
[2521] | 169 | mr_free(command);
|
---|
| 170 |
|
---|
[128] | 171 | if (retval) {
|
---|
[2521] | 172 | log_msg(2, "Failed to copy %s back to %s - oh dear", tmp_fname, orig_fname);
|
---|
[128] | 173 | } else {
|
---|
[1475] | 174 | log_msg(5, "%s was sorted OK.", orig_fname);
|
---|
[128] | 175 | }
|
---|
[1] | 176 | }
|
---|
[2521] | 177 | mr_free(tmp_fname);
|
---|
[1475] | 178 | log_msg(5, "Finished sorting file %s", orig_fname);
|
---|
[128] | 179 | return (retval);
|
---|
[1] | 180 | }
|
---|
| 181 |
|
---|
| 182 |
|
---|
| 183 |
|
---|
| 184 | /**
|
---|
| 185 | * Chop the filelist into sets.
|
---|
| 186 | * Each fileset is a list of files whose total (uncompressed) size is usually
|
---|
| 187 | * about X KB. Files bigger than 8X KB are placed in a "biggielist"; they will
|
---|
| 188 | * be sliced and compressed separately from the regular files.
|
---|
| 189 | *
|
---|
| 190 | * @param filelist The big filelist (filelist.full) to chop up.
|
---|
| 191 | * @param outdir The directory to place the files (filelist.N where N is
|
---|
| 192 | * an integer, biggielist.txt, and LAST-FILELIST-NUMBER) created
|
---|
| 193 | * @param maxsetsizeK Optimal size of a fileset (X above).
|
---|
| 194 | * @return number of errors encountered (0 for success).
|
---|
| 195 | */
|
---|
[128] | 196 | int chop_filelist(char *filelist, char *outdir, long maxsetsizeK)
|
---|
[1] | 197 | {
|
---|
| 198 | /*@ long ****************************************/
|
---|
[128] | 199 | long lino = 0;
|
---|
| 200 | long max_sane_size_for_a_file;
|
---|
| 201 | long curr_set_size;
|
---|
| 202 | long noof_lines;
|
---|
| 203 | long siz;
|
---|
[1] | 204 |
|
---|
[128] | 205 | /*@ int **************************************** */
|
---|
| 206 | int i;
|
---|
| 207 | long curr_set_no;
|
---|
[1] | 208 |
|
---|
[128] | 209 | /*@ buffers ************************************* */
|
---|
[2521] | 210 | char *outfname = NULL;
|
---|
| 211 | char *biggie_fname = NULL;
|
---|
[128] | 212 | char *incoming;
|
---|
[2521] | 213 | char *tmp = NULL;
|
---|
[1] | 214 |
|
---|
[128] | 215 | /*@ pointers *********************************** */
|
---|
| 216 | FILE *fin;
|
---|
| 217 | FILE *fout;
|
---|
| 218 | FILE *fbig;
|
---|
[1] | 219 |
|
---|
[128] | 220 | /*@ structures ********************************* */
|
---|
| 221 | struct stat buf;
|
---|
| 222 | int err = 0;
|
---|
[1] | 223 |
|
---|
[128] | 224 | assert_string_is_neither_NULL_nor_zerolength(filelist);
|
---|
| 225 | assert_string_is_neither_NULL_nor_zerolength(outdir);
|
---|
| 226 | assert(maxsetsizeK > 0);
|
---|
| 227 |
|
---|
[1236] | 228 | max_sane_size_for_a_file = 64L * 1024L;
|
---|
[1] | 229 | // max_sane_size_for_a_file = maxsetsizeK*2;
|
---|
| 230 | // if (max_sane_size_for_a_file > 32*1024)
|
---|
| 231 | // { max_sane_size_for_a_file = 32*1024; }
|
---|
[128] | 232 |
|
---|
| 233 | log_it("filelist=%s;", filelist);
|
---|
| 234 | open_evalcall_form("Dividing filelist into sets");
|
---|
| 235 | noof_lines = count_lines_in_file(filelist);
|
---|
| 236 | if (!(fin = fopen(filelist, "r"))) {
|
---|
| 237 | log_OS_error("Cannot openin filelist");
|
---|
| 238 | return (0);
|
---|
[1] | 239 | }
|
---|
[128] | 240 | curr_set_no = 0;
|
---|
| 241 | curr_set_size = 0;
|
---|
[2521] | 242 | mr_asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
|
---|
| 243 | mr_asprintf(&biggie_fname, "%s/biggielist.txt", outdir);
|
---|
[128] | 244 | log_it("outfname=%s; biggie_fname=%s", outfname, biggie_fname);
|
---|
| 245 | if (!(fbig = fopen(biggie_fname, "w"))) {
|
---|
| 246 | log_OS_error("Cannot openout biggie_fname");
|
---|
| 247 | err++;
|
---|
[2521] | 248 | mr_free(outfname);
|
---|
| 249 | mr_free(biggie_fname);
|
---|
| 250 | return (curr_set_no + 1);
|
---|
[1] | 251 | }
|
---|
[128] | 252 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 253 | log_OS_error("Cannot openout outfname");
|
---|
| 254 | err++;
|
---|
[2521] | 255 | mr_free(outfname);
|
---|
| 256 | mr_free(biggie_fname);
|
---|
| 257 | return (curr_set_no + 1);
|
---|
[1] | 258 | }
|
---|
[2521] | 259 | incoming = malloc(MAX_STR_LEN * 2);
|
---|
| 260 |
|
---|
[128] | 261 | (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
|
---|
| 262 | while (!feof(fin)) {
|
---|
| 263 | lino++;
|
---|
| 264 | i = strlen(incoming) - 1;
|
---|
| 265 | if (i < 0) {
|
---|
| 266 | i = 0;
|
---|
| 267 | }
|
---|
| 268 | if (i > MAX_STR_LEN - 1) {
|
---|
| 269 | incoming[MAX_STR_LEN - 30] = '\0';
|
---|
| 270 | log_msg(1, "Warning - truncating file %s's name", incoming);
|
---|
| 271 | err++;
|
---|
| 272 | }
|
---|
| 273 | if (incoming[i] < 32) {
|
---|
| 274 | incoming[i] = '\0';
|
---|
| 275 | }
|
---|
| 276 | if (!strncmp(incoming, "/dev/", 5)) {
|
---|
| 277 | siz = 1;
|
---|
| 278 | } else if (lstat(incoming, &buf) != 0) {
|
---|
| 279 | siz = 0;
|
---|
| 280 | } else {
|
---|
| 281 | siz = (long) (buf.st_size >> 10);
|
---|
| 282 | }
|
---|
[2521] | 283 | if (siz > max_sane_size_for_a_file) {
|
---|
[2548] | 284 | log_msg(10, "Adding %s to big files\n", incoming);
|
---|
[128] | 285 | fprintf(fbig, "%s\n", incoming);
|
---|
| 286 | } else {
|
---|
| 287 | curr_set_size += siz;
|
---|
[2548] | 288 | log_msg(10, "Adding %s to filelist %d\n", incoming, curr_set_no);
|
---|
[128] | 289 | fprintf(fout, "%s\n", incoming);
|
---|
| 290 | if (curr_set_size > maxsetsizeK) {
|
---|
| 291 | paranoid_fclose(fout);
|
---|
| 292 | sort_file(outfname);
|
---|
[2521] | 293 | mr_free(outfname);
|
---|
[128] | 294 | curr_set_no++;
|
---|
| 295 | curr_set_size = 0;
|
---|
[2521] | 296 |
|
---|
| 297 | mr_asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
|
---|
[128] | 298 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 299 | log_OS_error("Unable to openout outfname");
|
---|
| 300 | err++;
|
---|
[2521] | 301 | mr_free(outfname);
|
---|
| 302 | mr_free(biggie_fname);
|
---|
| 303 | paranoid_free(incoming);
|
---|
| 304 | return (curr_set_no + 1);
|
---|
[128] | 305 | }
|
---|
| 306 | update_evalcall_form((int) (lino * 100 / noof_lines));
|
---|
| 307 | }
|
---|
| 308 | }
|
---|
| 309 | (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
|
---|
[1] | 310 | }
|
---|
[2521] | 311 | paranoid_free(incoming);
|
---|
[128] | 312 | paranoid_fclose(fin);
|
---|
| 313 | paranoid_fclose(fout);
|
---|
| 314 | paranoid_fclose(fbig);
|
---|
| 315 |
|
---|
| 316 | if (length_of_file(outfname) <= 2) {
|
---|
| 317 | unlink(outfname);
|
---|
| 318 | g_noof_sets--;
|
---|
[1] | 319 | }
|
---|
[128] | 320 | g_noof_sets = curr_set_no;
|
---|
| 321 | sort_file(outfname);
|
---|
[2521] | 322 | mr_free(outfname);
|
---|
| 323 |
|
---|
[128] | 324 | sort_file(biggie_fname);
|
---|
[2521] | 325 | mr_free(biggie_fname);
|
---|
| 326 |
|
---|
| 327 | mr_asprintf(&outfname, "%s/LAST-FILELIST-NUMBER", outdir);
|
---|
| 328 | mr_asprintf(&tmp, "%ld", curr_set_no);
|
---|
[128] | 329 | if (write_one_liner_data_file(outfname, tmp)) {
|
---|
| 330 | log_OS_error
|
---|
| 331 | ("Unable to echo write one-liner to LAST-FILELIST-NUMBER");
|
---|
| 332 | err = 1;
|
---|
[1] | 333 | }
|
---|
[2521] | 334 | mr_free(tmp);
|
---|
| 335 | mr_free(outfname);
|
---|
| 336 |
|
---|
[128] | 337 | if (curr_set_no == 0) {
|
---|
[2521] | 338 | mr_asprintf(&tmp, "Only one fileset. Fine.");
|
---|
[128] | 339 | } else {
|
---|
[2521] | 340 | mr_asprintf(&tmp, "Filelist divided into %ld sets", curr_set_no + 1);
|
---|
[1] | 341 | }
|
---|
[128] | 342 | log_msg(1, tmp);
|
---|
[2521] | 343 | mr_free(tmp);
|
---|
[128] | 344 | close_evalcall_form();
|
---|
| 345 | /* This is to work around an obscure bug in Newt; open a form, close it,
|
---|
| 346 | carry on... I don't know why it works but it works. If you don't do this
|
---|
| 347 | then update_progress_form() won't show the "time taken / time remaining"
|
---|
| 348 | line. The bug only crops up AFTER the call to chop_filelist(). Weird. */
|
---|
| 349 | #ifndef _XWIN
|
---|
| 350 | if (!g_text_mode) {
|
---|
| 351 | open_progress_form("", "", "", "", 100);
|
---|
| 352 | newtPopHelpLine();
|
---|
| 353 | newtFormDestroy(g_progressForm);
|
---|
| 354 | newtPopWindow();
|
---|
[1] | 355 | }
|
---|
| 356 | #endif
|
---|
[128] | 357 | return (err ? 0 : curr_set_no + 1);
|
---|
[1] | 358 | }
|
---|
| 359 |
|
---|
| 360 |
|
---|
| 361 |
|
---|
| 362 |
|
---|
| 363 |
|
---|
| 364 | /**
|
---|
| 365 | * Free all the memory used by a filelist structure.
|
---|
| 366 | * Since this may take a long time for large filelists, a progress bar will be displayed.
|
---|
| 367 | * @param filelist The filelist to free.
|
---|
| 368 | */
|
---|
[128] | 369 | void free_filelist(struct s_node *filelist)
|
---|
[1] | 370 | {
|
---|
[128] | 371 | /*@ int's ******************************************************* */
|
---|
| 372 | static int depth = 0;
|
---|
| 373 | int percentage;
|
---|
[1] | 374 |
|
---|
[128] | 375 | /*@ long's ****************************************************** */
|
---|
| 376 | static long i = 0;
|
---|
[1] | 377 |
|
---|
[128] | 378 | /*@ end vars **************************************************** */
|
---|
[1] | 379 |
|
---|
[128] | 380 | assert(filelist != NULL);
|
---|
| 381 | if (depth == 0) {
|
---|
| 382 | open_evalcall_form("Freeing memory");
|
---|
[541] | 383 | log_to_screen("Freeing memory formerly occupied by filelist");
|
---|
[128] | 384 | }
|
---|
| 385 | depth++;
|
---|
[1] | 386 |
|
---|
[128] | 387 | if (filelist->ch == '\0') {
|
---|
| 388 | if (!(i++ % 1111)) {
|
---|
| 389 | percentage =
|
---|
| 390 | (int) (i * 100 / g_original_noof_lines_in_filelist);
|
---|
| 391 | update_evalcall_form(percentage);
|
---|
[1] | 392 |
|
---|
[128] | 393 | }
|
---|
[1] | 394 | }
|
---|
| 395 |
|
---|
[128] | 396 | if (filelist->right) {
|
---|
| 397 | free_filelist(filelist->right);
|
---|
| 398 | filelist->right = NULL;
|
---|
| 399 | }
|
---|
| 400 | if (filelist->down) {
|
---|
[1] | 401 | /* if (!(i++ %39999)) { update_evalcall_form(0); } */
|
---|
[128] | 402 | free_filelist(filelist->down);
|
---|
| 403 | filelist->down = NULL;
|
---|
| 404 | }
|
---|
| 405 | filelist->ch = '\0';
|
---|
| 406 | paranoid_free(filelist);
|
---|
| 407 | depth--;
|
---|
| 408 | if (depth == 0) {
|
---|
| 409 | close_evalcall_form();
|
---|
| 410 | log_it("Finished freeing memory");
|
---|
| 411 | }
|
---|
[1] | 412 | }
|
---|
| 413 |
|
---|
| 414 |
|
---|
[128] | 415 | int call_exe_and_pipe_output_to_fd(char *syscall, FILE * pout)
|
---|
[1] | 416 | {
|
---|
[128] | 417 | FILE *pattr;
|
---|
| 418 | char *tmp;
|
---|
| 419 | pattr = popen(syscall, "r");
|
---|
| 420 | if (!pattr) {
|
---|
| 421 | log_msg(1, "Failed to open fattr() %s", syscall);
|
---|
| 422 | return (1);
|
---|
[1] | 423 | }
|
---|
[128] | 424 | if (feof(pattr)) {
|
---|
| 425 | log_msg(1, "Failed to call fattr() %s", syscall);
|
---|
| 426 | paranoid_pclose(pattr);
|
---|
| 427 | return (2);
|
---|
[1] | 428 | }
|
---|
[128] | 429 | malloc_string(tmp);
|
---|
[2202] | 430 | for ((void)fgets(tmp, MAX_STR_LEN, pattr); !feof(pattr);
|
---|
| 431 | (void)fgets(tmp, MAX_STR_LEN, pattr)) {
|
---|
[128] | 432 | fputs(tmp, pout);
|
---|
| 433 | }
|
---|
| 434 | paranoid_pclose(pattr);
|
---|
| 435 | paranoid_free(tmp);
|
---|
| 436 | return (0);
|
---|
[1] | 437 | }
|
---|
| 438 |
|
---|
| 439 |
|
---|
| 440 |
|
---|
[128] | 441 | int gen_aux_list(char *filelist, char *syscall_sprintf,
|
---|
| 442 | char *auxlist_fname)
|
---|
[1] | 443 | {
|
---|
[128] | 444 | FILE *fin;
|
---|
| 445 | FILE *pout;
|
---|
[2521] | 446 | char *pout_command = NULL;
|
---|
[128] | 447 | char *syscall;
|
---|
| 448 | char *file_to_analyze;
|
---|
[1015] | 449 | char *strtmp = NULL;
|
---|
[1552] | 450 | char *tmp = NULL;
|
---|
[128] | 451 | int i;
|
---|
| 452 |
|
---|
| 453 | if (!(fin = fopen(filelist, "r"))) {
|
---|
| 454 | log_msg(1, "Cannot openin filelist %s", filelist);
|
---|
| 455 | return (1);
|
---|
| 456 | }
|
---|
[2521] | 457 | mr_asprintf(&pout_command, "gzip -c1 > %s", auxlist_fname);
|
---|
[128] | 458 | if (!(pout = popen(pout_command, "w"))) {
|
---|
| 459 | log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
|
---|
| 460 | fclose(fin);
|
---|
[2521] | 461 | mr_free(pout_command);
|
---|
[128] | 462 | return (4);
|
---|
| 463 | }
|
---|
[2521] | 464 | mr_free(pout_command);
|
---|
| 465 |
|
---|
[128] | 466 | malloc_string(file_to_analyze);
|
---|
[2202] | 467 | for ((void)fgets(file_to_analyze, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 468 | (void)fgets(file_to_analyze, MAX_STR_LEN, fin)) {
|
---|
[128] | 469 | i = strlen(file_to_analyze);
|
---|
| 470 | if (i > 0 && file_to_analyze[i - 1] < 32) {
|
---|
| 471 | file_to_analyze[i - 1] = '\0';
|
---|
| 472 | }
|
---|
| 473 | log_msg(8, "Analyzing %s", file_to_analyze);
|
---|
[2206] | 474 | tmp = mr_stresc(file_to_analyze, "`$\\\"(){}[]'*?&|!#~", '\\');
|
---|
[2211] | 475 | mr_asprintf(&strtmp, syscall_sprintf, tmp);
|
---|
[1552] | 476 | paranoid_free(tmp);
|
---|
[2211] | 477 | mr_asprintf(&syscall, "%s 2>> /dev/null", strtmp); // " MONDO_LOGFILE);
|
---|
[1015] | 478 | paranoid_free(strtmp);
|
---|
[2548] | 479 | log_msg(20,"calling %s\n",syscall);
|
---|
[128] | 480 | call_exe_and_pipe_output_to_fd(syscall, pout);
|
---|
[1015] | 481 | paranoid_free(syscall);
|
---|
[128] | 482 | }
|
---|
| 483 | paranoid_fclose(fin);
|
---|
| 484 | paranoid_pclose(pout);
|
---|
| 485 | paranoid_free(file_to_analyze);
|
---|
| 486 | return (0);
|
---|
[1] | 487 | }
|
---|
| 488 |
|
---|
| 489 |
|
---|
[128] | 490 | int get_acl_list(char *filelist, char *facl_fname)
|
---|
[1] | 491 | {
|
---|
[2521] | 492 | char *command = NULL;
|
---|
[128] | 493 | int retval = 0;
|
---|
| 494 |
|
---|
[944] | 495 | if (g_getfacl != NULL) {
|
---|
[2521] | 496 | mr_asprintf(&command, "touch %s", facl_fname);
|
---|
[943] | 497 | run_program_and_log_output(command, 8);
|
---|
[2521] | 498 | mr_free(command);
|
---|
| 499 |
|
---|
| 500 | mr_asprintf(&command,
|
---|
[275] | 501 | "getfacl --all-effective -P %s 2>> %s | gzip -c1 > %s 2>> %s",
|
---|
[128] | 502 | filelist, MONDO_LOGFILE, facl_fname, MONDO_LOGFILE);
|
---|
[2227] | 503 | log_it("%s",command);
|
---|
[128] | 504 | retval = system(command);
|
---|
[2521] | 505 | mr_free(command);
|
---|
[128] | 506 | }
|
---|
| 507 | return (retval);
|
---|
[1] | 508 | }
|
---|
| 509 |
|
---|
| 510 |
|
---|
[128] | 511 | int get_fattr_list(char *filelist, char *fattr_fname)
|
---|
[1] | 512 | {
|
---|
[128] | 513 | char *command;
|
---|
| 514 | int retval = 0;
|
---|
| 515 |
|
---|
[944] | 516 | if (g_getfattr != NULL) {
|
---|
[2521] | 517 | mr_asprintf(&command, "touch %s", fattr_fname);
|
---|
[942] | 518 | run_program_and_log_output(command, 8);
|
---|
[2521] | 519 | mr_free(command);
|
---|
[128] | 520 | retval =
|
---|
[2549] | 521 | gen_aux_list(filelist, "getfattr --en=hex -m - -h -d \"%s\"",
|
---|
[128] | 522 | fattr_fname);
|
---|
| 523 | }
|
---|
| 524 | return (retval);
|
---|
[1] | 525 | }
|
---|
| 526 |
|
---|
| 527 |
|
---|
[128] | 528 | int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
|
---|
| 529 | char *executable)
|
---|
[1] | 530 | {
|
---|
[128] | 531 | const int my_depth = 8;
|
---|
[2521] | 532 | char *command = NULL;
|
---|
| 533 | char *syscall_pin = NULL;
|
---|
| 534 | char *syscall_pout = NULL;
|
---|
| 535 | char *incoming;
|
---|
| 536 | char *current_subset_file, *current_master_file;
|
---|
| 537 | char *masklist = NULL;
|
---|
[128] | 538 | int retval = 0;
|
---|
| 539 | int i;
|
---|
| 540 | char *p, *q;
|
---|
| 541 | FILE *pin, *pout, *faclin;
|
---|
[1] | 542 |
|
---|
[128] | 543 | log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist,
|
---|
| 544 | original_exat_fname, executable);
|
---|
| 545 | if (!orig_msklist || !orig_msklist[0]
|
---|
| 546 | || !does_file_exist(orig_msklist)) {
|
---|
[2521] | 547 | log_msg(1, "No masklist provided. I shall therefore set ALL attributes.");
|
---|
| 548 | mr_asprintf(&command, "gzip -dc %s | %s --restore - 2>> %s", original_exat_fname, executable, MONDO_LOGFILE);
|
---|
[128] | 549 | log_msg(1, "command = %s", command);
|
---|
| 550 | retval = system(command);
|
---|
[2521] | 551 | mr_free(command);
|
---|
[128] | 552 | log_msg(1, "Returning w/ retval=%d", retval);
|
---|
| 553 | return (retval);
|
---|
| 554 | }
|
---|
| 555 | if (length_of_file(original_exat_fname) <= 0) {
|
---|
| 556 | log_msg(1,
|
---|
| 557 | "original_exat_fname %s is empty or missing, so no need to set EXAT list",
|
---|
| 558 | original_exat_fname);
|
---|
| 559 | return (0);
|
---|
| 560 | }
|
---|
| 561 | malloc_string(incoming);
|
---|
| 562 | malloc_string(current_subset_file);
|
---|
| 563 | malloc_string(current_master_file);
|
---|
[2521] | 564 | mr_asprintf(&masklist, "%s/masklist", bkpinfo->tmpdir);
|
---|
| 565 | mr_asprintf(&command, "cp -f %s %s", orig_msklist, masklist);
|
---|
[128] | 566 | run_program_and_log_output(command, 1);
|
---|
[2521] | 567 | mr_free(command);
|
---|
| 568 |
|
---|
[128] | 569 | sort_file(masklist);
|
---|
| 570 | current_subset_file[0] = current_master_file[0] = '\0';
|
---|
[1] | 571 |
|
---|
[2521] | 572 | mr_asprintf(&syscall_pout, "%s --restore - 2>> %s", executable, MONDO_LOGFILE);
|
---|
[128] | 573 | log_msg(1, "syscall_pout = %s", syscall_pout);
|
---|
| 574 | pout = popen(syscall_pout, "w");
|
---|
[2521] | 575 | mr_free(syscall_pout);
|
---|
| 576 |
|
---|
[128] | 577 | if (!pout) {
|
---|
[2230] | 578 | log_it("Unable to openout to syscall_pout");
|
---|
[2521] | 579 | mr_free(masklist);
|
---|
[128] | 580 | return (1);
|
---|
| 581 | }
|
---|
[2521] | 582 |
|
---|
| 583 | mr_asprintf(&syscall_pin, "gzip -dc %s", original_exat_fname);
|
---|
| 584 | log_msg(1, "syscall_pin = %s", syscall_pin);
|
---|
[128] | 585 | pin = popen(syscall_pin, "r");
|
---|
[2521] | 586 | mr_free(syscall_pin);
|
---|
| 587 |
|
---|
[128] | 588 | if (!pin) {
|
---|
| 589 | pclose(pout);
|
---|
[2230] | 590 | log_it("Unable to openin from syscall");
|
---|
[128] | 591 | return (1);
|
---|
| 592 | }
|
---|
| 593 | faclin = fopen(masklist, "r");
|
---|
| 594 | if (!faclin) {
|
---|
| 595 | pclose(pin);
|
---|
| 596 | pclose(pout);
|
---|
[2230] | 597 | log_it("Unable to openin masklist");
|
---|
[2521] | 598 | mr_free(masklist);
|
---|
[128] | 599 | return (1);
|
---|
| 600 | }
|
---|
| 601 | // printf("Hi there. Starting the loop\n");
|
---|
[1] | 602 |
|
---|
[2202] | 603 | (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
|
---|
| 604 | (void)fgets(incoming, MAX_STR_LEN, pin);
|
---|
[128] | 605 | while (!feof(pin) && !feof(faclin)) {
|
---|
[1] | 606 | // printf("incoming = %s", incoming);
|
---|
| 607 |
|
---|
[128] | 608 | strcpy(current_master_file, incoming + 8);
|
---|
[1] | 609 |
|
---|
[128] | 610 | p = current_subset_file;
|
---|
| 611 | if (*p == '/') {
|
---|
| 612 | p++;
|
---|
| 613 | }
|
---|
| 614 | i = strlen(p);
|
---|
| 615 | if (i > 0 && p[i - 1] < 32) {
|
---|
| 616 | p[i - 1] = '\0';
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 |
|
---|
| 620 | q = current_master_file;
|
---|
| 621 | if (*q == '/') {
|
---|
| 622 | q++;
|
---|
| 623 | }
|
---|
| 624 | i = strlen(q);
|
---|
| 625 | if (i > 0 && q[i - 1] < 32) {
|
---|
| 626 | q[i - 1] = '\0';
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | i = strcmp(p, q);
|
---|
| 630 | log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
|
---|
| 631 |
|
---|
[1] | 632 | // printf("%s v %s --> %d\n", p, q, i);
|
---|
[128] | 633 |
|
---|
| 634 | if (i < 0) { // read another subset file in.
|
---|
| 635 | log_msg(my_depth, "Reading next subset line in\n\n");
|
---|
[2202] | 636 | (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
|
---|
[128] | 637 | continue;
|
---|
| 638 | }
|
---|
| 639 |
|
---|
| 640 | if (!i) {
|
---|
| 641 | fputs(incoming, pout);
|
---|
| 642 | }
|
---|
[2202] | 643 | (void)fgets(incoming, MAX_STR_LEN, pin);
|
---|
[128] | 644 | if (!i) {
|
---|
| 645 | log_msg(my_depth, "Copying master %s", q);
|
---|
| 646 | }
|
---|
| 647 | // if (!i) { printf("Match --- %s\n", q); }
|
---|
| 648 |
|
---|
| 649 | while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
|
---|
| 650 | if (!i) {
|
---|
| 651 |
|
---|
| 652 | // printf("%s", incoming);
|
---|
| 653 |
|
---|
| 654 | fputs(incoming, pout);
|
---|
| 655 | }
|
---|
[2202] | 656 | (void)fgets(incoming, MAX_STR_LEN, pin);
|
---|
[128] | 657 | }
|
---|
| 658 | if (!i) {
|
---|
[2202] | 659 | (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
|
---|
[128] | 660 | }
|
---|
[1] | 661 | }
|
---|
[128] | 662 | while (!feof(pin)) {
|
---|
[2202] | 663 | (void)fgets(incoming, MAX_STR_LEN, pin);
|
---|
[128] | 664 | }
|
---|
| 665 | fclose(faclin);
|
---|
| 666 | pclose(pin);
|
---|
| 667 | pclose(pout);
|
---|
[1] | 668 |
|
---|
| 669 | // printf("OK, loop is done\n");
|
---|
| 670 |
|
---|
[128] | 671 | unlink(masklist);
|
---|
[2521] | 672 | mr_free(masklist);
|
---|
| 673 |
|
---|
[128] | 674 | paranoid_free(current_subset_file);
|
---|
| 675 | paranoid_free(current_master_file);
|
---|
| 676 | paranoid_free(incoming);
|
---|
| 677 | return (retval);
|
---|
| 678 | }
|
---|
[1] | 679 |
|
---|
| 680 |
|
---|
[128] | 681 | int set_fattr_list(char *masklist, char *fattr_fname)
|
---|
[1] | 682 | {
|
---|
[912] | 683 | if (find_home_of_exe("setfattr")) {
|
---|
| 684 | return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
|
---|
| 685 | } else {
|
---|
| 686 | log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
|
---|
| 687 | return(0);
|
---|
| 688 | }
|
---|
[1] | 689 | }
|
---|
| 690 |
|
---|
| 691 |
|
---|
| 692 |
|
---|
[128] | 693 | int set_acl_list(char *masklist, char *acl_fname)
|
---|
[1] | 694 | {
|
---|
[912] | 695 | if (find_home_of_exe("setfacl")) {
|
---|
| 696 | return (set_EXAT_list(masklist, acl_fname, "setfacl"));
|
---|
| 697 | } else {
|
---|
| 698 | log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
|
---|
| 699 | return(0);
|
---|
| 700 | }
|
---|
[1] | 701 | }
|
---|
| 702 |
|
---|
| 703 |
|
---|
| 704 | /**
|
---|
| 705 | * Get the number of the last fileset in the backup.
|
---|
| 706 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 707 | * @return The last filelist number.
|
---|
| 708 | * @note This function should only be called at restore-time.
|
---|
| 709 | */
|
---|
[1645] | 710 | int get_last_filelist_number()
|
---|
[1] | 711 | {
|
---|
[128] | 712 | /*@ buffers ***************************************************** */
|
---|
| 713 | char val_sz[MAX_STR_LEN];
|
---|
[2521] | 714 | char *cfg_fname = NULL;
|
---|
[1] | 715 |
|
---|
[128] | 716 | /*@ long ******************************************************** */
|
---|
| 717 | int val_i;
|
---|
[1] | 718 |
|
---|
[128] | 719 | /*@ end vars **************************************************** */
|
---|
[1] | 720 |
|
---|
[128] | 721 | assert(bkpinfo != NULL);
|
---|
[1] | 722 |
|
---|
[1552] | 723 | strcpy(val_sz,"");
|
---|
[2521] | 724 | mr_asprintf(&cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
|
---|
[128] | 725 | read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
|
---|
[2521] | 726 | mr_free(cfg_fname);
|
---|
| 727 |
|
---|
[128] | 728 | val_i = atoi(val_sz);
|
---|
| 729 | if (val_i <= 0) {
|
---|
| 730 | val_i = 500;
|
---|
| 731 | }
|
---|
| 732 | return (val_i);
|
---|
[1] | 733 | }
|
---|
| 734 |
|
---|
| 735 |
|
---|
| 736 | /**
|
---|
| 737 | * Add a string at @p startnode.
|
---|
| 738 | * @param startnode The node to start at when searching for where to add the string.
|
---|
| 739 | * @param string_to_add The string to add.
|
---|
| 740 | * @return 0 for success, 1 for failure.
|
---|
| 741 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 742 | */
|
---|
[128] | 743 | int add_string_at_node(struct s_node *startnode, char *string_to_add)
|
---|
[1] | 744 | {
|
---|
| 745 |
|
---|
| 746 |
|
---|
[128] | 747 | /*@ int ******************************************************** */
|
---|
| 748 | int noof_chars;
|
---|
| 749 | int i;
|
---|
| 750 | int res;
|
---|
[1] | 751 |
|
---|
[128] | 752 | /*@ sturctures ************************************************* */
|
---|
| 753 | struct s_node *node, *newnode;
|
---|
[1] | 754 |
|
---|
[128] | 755 | /*@ char ****************************************************** */
|
---|
| 756 | char char_to_add;
|
---|
[1] | 757 |
|
---|
[128] | 758 | /*@ bools ****************************************************** */
|
---|
[1] | 759 |
|
---|
[128] | 760 | const bool sosodef = FALSE;
|
---|
[1] | 761 |
|
---|
[128] | 762 | static int depth = 0;
|
---|
| 763 | static char original_string[MAX_STR_LEN];
|
---|
[1] | 764 |
|
---|
[128] | 765 | assert(startnode != NULL);
|
---|
| 766 | assert(string_to_add != NULL);
|
---|
[1] | 767 |
|
---|
[128] | 768 | if (!depth) {
|
---|
| 769 | strcpy(original_string, string_to_add);
|
---|
| 770 | }
|
---|
[1] | 771 |
|
---|
[128] | 772 | noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
|
---|
[1] | 773 |
|
---|
| 774 | /* walk across tree if necessary */
|
---|
[128] | 775 | node = startnode;
|
---|
| 776 | char_to_add = string_to_add[0];
|
---|
| 777 | if (node->right != NULL && node->ch < char_to_add) {
|
---|
| 778 | log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
|
---|
| 779 | char_to_add, node->ch, (node->right)->ch);
|
---|
| 780 | return (add_string_at_node(node->right, string_to_add));
|
---|
| 781 | }
|
---|
[1] | 782 |
|
---|
| 783 | /* walk down tree if appropriate */
|
---|
[128] | 784 | if (node->down != NULL && node->ch == char_to_add) {
|
---|
| 785 | log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
|
---|
| 786 | depth++;
|
---|
| 787 | res = add_string_at_node(node->down, string_to_add + 1);
|
---|
| 788 | depth--;
|
---|
| 789 | return (res);
|
---|
| 790 | }
|
---|
[1] | 791 |
|
---|
[128] | 792 | if (char_to_add == '\0' && node->ch == '\0') {
|
---|
| 793 | log_msg(6, "%s already in tree", original_string);
|
---|
| 794 | return (1);
|
---|
| 795 | }
|
---|
[1] | 796 |
|
---|
| 797 | /* add here */
|
---|
[128] | 798 | if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
|
---|
[541] | 799 | log_to_screen("failed to malloc");
|
---|
[128] | 800 | depth--;
|
---|
| 801 | return (1);
|
---|
| 802 | }
|
---|
| 803 | if (char_to_add < node->ch) // add to the left of node
|
---|
[1] | 804 | {
|
---|
[128] | 805 | log_msg(7, "depth=%d char=%c --- adding (left)", depth,
|
---|
| 806 | char_to_add);
|
---|
| 807 | memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
|
---|
| 808 | node->right = newnode;
|
---|
| 809 | } else if (char_to_add > node->ch) // add to the right of node
|
---|
| 810 | {
|
---|
| 811 | log_msg(7, "depth=%d char=%c --- adding (right)", depth,
|
---|
| 812 | char_to_add);
|
---|
| 813 | newnode->right = node->right; // newnode is to the RIGHT of node
|
---|
| 814 | node->right = newnode;
|
---|
| 815 | node = newnode;
|
---|
[1] | 816 | }
|
---|
[128] | 817 | // from now on, we're working on 'node'
|
---|
| 818 | node->down = NULL;
|
---|
| 819 | node->ch = char_to_add;
|
---|
| 820 | node->expanded = node->selected = FALSE;
|
---|
| 821 | if (char_to_add == '\0') {
|
---|
| 822 | log_msg(6, "Added %s OK", original_string);
|
---|
| 823 | return (0);
|
---|
| 824 | }
|
---|
| 825 | // add the rest
|
---|
| 826 | log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
|
---|
| 827 | for (i = 1; i < noof_chars; i++) {
|
---|
| 828 | if (!
|
---|
| 829 | (node->down =
|
---|
| 830 | (struct s_node *) malloc(sizeof(struct s_node)))) {
|
---|
[541] | 831 | log_to_screen("%s - failed to malloc", string_to_add);
|
---|
[128] | 832 | return (1);
|
---|
| 833 | }
|
---|
| 834 | node = node->down;
|
---|
| 835 | char_to_add = string_to_add[i];
|
---|
| 836 | log_msg(6, "Adding '%c'", char_to_add);
|
---|
| 837 | node->ch = char_to_add;
|
---|
| 838 | node->right = node->down = NULL;
|
---|
| 839 | node->expanded = node->selected = FALSE;
|
---|
| 840 | if (!node->ch) {
|
---|
| 841 | node->selected = sosodef;
|
---|
| 842 | }
|
---|
| 843 | }
|
---|
| 844 | log_msg(6, "Finally - added %s OK", original_string);
|
---|
| 845 | return (0);
|
---|
[1] | 846 | }
|
---|
| 847 |
|
---|
| 848 |
|
---|
| 849 |
|
---|
| 850 |
|
---|
| 851 | /**
|
---|
| 852 | * Load a filelist into a <tt>struct s_node</tt>.
|
---|
| 853 | * When you are done with the filelist, call free_filelist().
|
---|
| 854 | * @param filelist_fname The file to load the filelist from.
|
---|
| 855 | * @return A filelist tree structure.
|
---|
| 856 | */
|
---|
[128] | 857 | struct s_node *load_filelist(char *filelist_fname)
|
---|
[1] | 858 | {
|
---|
| 859 |
|
---|
[128] | 860 | /*@ structures ************************************************* */
|
---|
| 861 | struct s_node *filelist;
|
---|
[1] | 862 |
|
---|
[128] | 863 | /*@ pointers *************************************************** */
|
---|
| 864 | FILE *pin;
|
---|
[1] | 865 |
|
---|
[128] | 866 | /*@ buffers **************************************************** */
|
---|
[2521] | 867 | char *command_to_open_fname = NULL;
|
---|
[128] | 868 | char fname[MAX_STR_LEN];
|
---|
| 869 | char tmp[MAX_STR_LEN];
|
---|
[2521] | 870 | char *tmp1 = NULL;
|
---|
[128] | 871 | int pos_in_fname;
|
---|
| 872 | /*@ int ******************************************************** */
|
---|
| 873 | int percentage;
|
---|
[1] | 874 |
|
---|
[128] | 875 | /*@ long ******************************************************* */
|
---|
| 876 | long lines_in_filelist;
|
---|
| 877 | long lino = 0;
|
---|
| 878 | /*@ end vars *************************************************** */
|
---|
[1] | 879 |
|
---|
[128] | 880 | assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
|
---|
[1] | 881 |
|
---|
[128] | 882 | if (!does_file_exist(filelist_fname)) {
|
---|
| 883 | fatal_error("filelist does not exist -- cannot load it");
|
---|
[1] | 884 | }
|
---|
[541] | 885 | log_to_screen("Loading filelist");
|
---|
[2521] | 886 | mr_asprintf(&tmp1, "zcat %s | wc -l", filelist_fname);
|
---|
| 887 | log_msg(6, "tmp1 = %s", tmp1);
|
---|
| 888 | lines_in_filelist = atol(call_program_and_get_last_line_of_output(tmp1));
|
---|
| 889 | mr_free(tmp1);
|
---|
| 890 |
|
---|
[128] | 891 | if (lines_in_filelist < 3) {
|
---|
[541] | 892 | log_to_screen("Warning - surprisingly short filelist.");
|
---|
[1] | 893 | }
|
---|
[128] | 894 | g_original_noof_lines_in_filelist = lines_in_filelist;
|
---|
| 895 | if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
|
---|
| 896 | return (NULL);
|
---|
[1] | 897 | }
|
---|
[128] | 898 | filelist->ch = '/';
|
---|
| 899 | filelist->right = NULL;
|
---|
| 900 | filelist->down = malloc(sizeof(struct s_node));
|
---|
| 901 | filelist->expanded = filelist->selected = FALSE;
|
---|
| 902 | (filelist->down)->ch = '\0';
|
---|
| 903 | (filelist->down)->right = (filelist->down)->down = FALSE;
|
---|
| 904 | (filelist->down)->expanded = (filelist->down)->selected = FALSE;
|
---|
[2521] | 905 |
|
---|
| 906 | mr_asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
|
---|
[128] | 907 | if (!(pin = popen(command_to_open_fname, "r"))) {
|
---|
| 908 | log_OS_error("Unable to openin filelist_fname");
|
---|
[2521] | 909 | mr_free(command_to_open_fname);
|
---|
[128] | 910 | return (NULL);
|
---|
| 911 | }
|
---|
[2521] | 912 | mr_free(command_to_open_fname);
|
---|
| 913 |
|
---|
[541] | 914 | open_evalcall_form("Loading filelist from disk");
|
---|
[2202] | 915 | for ((void)fgets(fname, MAX_STR_LEN, pin); !feof(pin);
|
---|
| 916 | (void)fgets(fname, MAX_STR_LEN, pin)) {
|
---|
[128] | 917 | if ((fname[strlen(fname) - 1] == 13
|
---|
| 918 | || fname[strlen(fname) - 1] == 10) && strlen(fname) > 0) {
|
---|
| 919 | fname[strlen(fname) - 1] = '\0';
|
---|
| 920 | }
|
---|
| 921 | // strip_spaces (fname);
|
---|
| 922 | if (!strlen(fname)) {
|
---|
| 923 | continue;
|
---|
| 924 | }
|
---|
| 925 | for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
|
---|
| 926 | if (fname[pos_in_fname] != '/') {
|
---|
| 927 | continue;
|
---|
| 928 | }
|
---|
| 929 | strcpy(tmp, fname);
|
---|
| 930 | tmp[pos_in_fname] = '\0';
|
---|
| 931 | if (strlen(tmp)) {
|
---|
| 932 | add_string_at_node(filelist, tmp);
|
---|
| 933 | }
|
---|
| 934 | }
|
---|
| 935 | add_string_at_node(filelist, fname);
|
---|
| 936 | if (!(++lino % 1111)) {
|
---|
| 937 | percentage = (int) (lino * 100 / lines_in_filelist);
|
---|
| 938 | update_evalcall_form(percentage);
|
---|
| 939 | }
|
---|
| 940 | }
|
---|
| 941 | paranoid_pclose(pin);
|
---|
| 942 | close_evalcall_form();
|
---|
| 943 | log_it("Finished loading filelist");
|
---|
| 944 | return (filelist);
|
---|
[1] | 945 | }
|
---|
| 946 |
|
---|
| 947 |
|
---|
| 948 | /**
|
---|
| 949 | * Log a list of files in @p node.
|
---|
| 950 | * @param node The toplevel node to use.
|
---|
| 951 | */
|
---|
[128] | 952 | void show_filelist(struct s_node *node)
|
---|
[1] | 953 | {
|
---|
[128] | 954 | static int depth = 0;
|
---|
| 955 | static char current_string[200];
|
---|
[1] | 956 |
|
---|
[128] | 957 | if (depth == 0) {
|
---|
| 958 | log_msg(0, "----------------show filelist--------------");
|
---|
| 959 | }
|
---|
| 960 | current_string[depth] = node->ch;
|
---|
[1] | 961 |
|
---|
[128] | 962 | log_msg(3, "depth=%d", depth);
|
---|
| 963 | if (node->down) {
|
---|
| 964 | log_msg(3, "moving down");
|
---|
| 965 | depth++;
|
---|
| 966 | show_filelist(node->down);
|
---|
| 967 | depth--;
|
---|
| 968 | }
|
---|
[1] | 969 |
|
---|
[128] | 970 | if (!node->ch) {
|
---|
| 971 | log_msg(0, "%s\n", current_string);
|
---|
| 972 | }
|
---|
[1] | 973 |
|
---|
[128] | 974 | if (node->right) {
|
---|
| 975 | log_msg(3, "moving right");
|
---|
| 976 | show_filelist(node->right);
|
---|
| 977 | }
|
---|
| 978 | if (depth == 0) {
|
---|
| 979 | log_msg(0, "----------------show filelist--------------");
|
---|
| 980 | }
|
---|
| 981 | return;
|
---|
[1] | 982 | }
|
---|
| 983 |
|
---|
| 984 |
|
---|
| 985 |
|
---|
| 986 |
|
---|
| 987 | /**
|
---|
| 988 | * Reset the filelist to the state it was when it was loaded. This does not
|
---|
| 989 | * touch the file on disk.
|
---|
| 990 | * @param filelist The filelist tree structure.
|
---|
| 991 | */
|
---|
[128] | 992 | void reload_filelist(struct s_node *filelist)
|
---|
[1] | 993 | {
|
---|
[128] | 994 | assert(filelist != NULL);
|
---|
| 995 | toggle_node_selection(filelist, FALSE);
|
---|
| 996 | toggle_path_expandability(filelist, "/", FALSE);
|
---|
| 997 | toggle_all_root_dirs_on(filelist);
|
---|
[1] | 998 | }
|
---|
| 999 |
|
---|
| 1000 |
|
---|
| 1001 |
|
---|
| 1002 | /**
|
---|
| 1003 | * Save a filelist tree structure to disk.
|
---|
| 1004 | * @param filelist The filelist tree structure to save.
|
---|
| 1005 | * @param outfname Where to save it.
|
---|
| 1006 | */
|
---|
[128] | 1007 | void save_filelist(struct s_node *filelist, char *outfname)
|
---|
[1] | 1008 | {
|
---|
[128] | 1009 | /*@ int ********************************************************* */
|
---|
| 1010 | static int percentage;
|
---|
| 1011 | static int depth = 0;
|
---|
[1] | 1012 |
|
---|
[128] | 1013 | /*@ buffers ***************************************************** */
|
---|
| 1014 | static char str[MAX_STR_LEN];
|
---|
[1] | 1015 |
|
---|
[128] | 1016 | /*@ structures ************************************************** */
|
---|
| 1017 | struct s_node *node;
|
---|
[1] | 1018 |
|
---|
[128] | 1019 | /*@ pointers **************************************************** */
|
---|
| 1020 | static FILE *fout = NULL;
|
---|
[1] | 1021 |
|
---|
[128] | 1022 | /*@ long ******************************************************** */
|
---|
| 1023 | static long lines_in_filelist = 0;
|
---|
| 1024 | static long lino = 0;
|
---|
[1] | 1025 |
|
---|
[128] | 1026 | /*@ end vars *************************************************** */
|
---|
[1] | 1027 |
|
---|
[128] | 1028 | assert(filelist != NULL);
|
---|
| 1029 | assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
|
---|
| 1030 | if (depth == 0) {
|
---|
[541] | 1031 | log_to_screen("Saving filelist");
|
---|
[128] | 1032 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 1033 | fatal_error("Cannot openout/save filelist");
|
---|
| 1034 | }
|
---|
| 1035 | lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
|
---|
[541] | 1036 | open_evalcall_form("Saving selection to disk");
|
---|
[1] | 1037 | }
|
---|
[128] | 1038 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1039 | str[depth] = node->ch;
|
---|
| 1040 | log_msg(5, "depth=%d ch=%c", depth, node->ch);
|
---|
| 1041 | if (!node->ch) {
|
---|
| 1042 | // if (node->selected)
|
---|
| 1043 | // {
|
---|
| 1044 | fprintf(fout, "%s\n", str);
|
---|
| 1045 | // }
|
---|
| 1046 | if (!(++lino % 1111)) {
|
---|
| 1047 | percentage = (int) (lino * 100 / lines_in_filelist);
|
---|
| 1048 | update_evalcall_form(percentage);
|
---|
| 1049 | }
|
---|
| 1050 | }
|
---|
| 1051 | if (node->down) {
|
---|
| 1052 | depth++;
|
---|
| 1053 | save_filelist(node->down, "");
|
---|
| 1054 | depth--;
|
---|
| 1055 | }
|
---|
[1] | 1056 | }
|
---|
[128] | 1057 | if (depth == 0) {
|
---|
| 1058 | paranoid_fclose(fout);
|
---|
| 1059 | close_evalcall_form();
|
---|
| 1060 | log_it("Finished saving filelist");
|
---|
| 1061 | }
|
---|
[1] | 1062 | }
|
---|
| 1063 |
|
---|
| 1064 |
|
---|
| 1065 |
|
---|
| 1066 | /**
|
---|
| 1067 | * Toggle all root dirs on.
|
---|
| 1068 | * @param filelist The filelist tree structure to operate on.
|
---|
| 1069 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1070 | */
|
---|
[128] | 1071 | void toggle_all_root_dirs_on(struct s_node *filelist)
|
---|
[1] | 1072 | {
|
---|
[128] | 1073 | /*@ structures ************************************************** */
|
---|
| 1074 | struct s_node *node;
|
---|
[1] | 1075 |
|
---|
[128] | 1076 | /*@ int ********************************************************* */
|
---|
| 1077 | static int depth = 0;
|
---|
| 1078 | static int root_dirs_expanded;
|
---|
[1] | 1079 |
|
---|
[128] | 1080 | /*@ buffers ***************************************************** */
|
---|
| 1081 | static char filename[MAX_STR_LEN];
|
---|
[1] | 1082 |
|
---|
[128] | 1083 | /*@ end vars *************************************************** */
|
---|
[1] | 1084 |
|
---|
[128] | 1085 | assert(filelist != NULL);
|
---|
| 1086 | if (depth == 0) {
|
---|
| 1087 | log_it("Toggling all root dirs ON");
|
---|
| 1088 | root_dirs_expanded = 0;
|
---|
[1] | 1089 | }
|
---|
[128] | 1090 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1091 | filename[depth] = node->ch;
|
---|
| 1092 | if (node->ch == '\0' && strlen(filename) > 1
|
---|
| 1093 | && (!strchr(filename + 1, '/'))) {
|
---|
| 1094 | node->selected = FALSE;
|
---|
| 1095 | node->expanded = TRUE;
|
---|
| 1096 | // log_it (filename);
|
---|
| 1097 | root_dirs_expanded++;
|
---|
| 1098 | }
|
---|
| 1099 | if (node->down) {
|
---|
| 1100 | depth++;
|
---|
| 1101 | toggle_all_root_dirs_on(node->down);
|
---|
| 1102 | depth--;
|
---|
| 1103 | }
|
---|
[1] | 1104 | }
|
---|
[128] | 1105 | if (depth == 0) {
|
---|
| 1106 | log_it("Finished toggling all root dirs ON");
|
---|
| 1107 | }
|
---|
[1] | 1108 | }
|
---|
| 1109 |
|
---|
| 1110 |
|
---|
| 1111 | /**
|
---|
| 1112 | * Toggle the expandability of a path.
|
---|
| 1113 | * @param filelist The filelist tree to operate on.
|
---|
| 1114 | * @param pathname The path to toggle expandability of.
|
---|
| 1115 | * @param on_or_off Whether to toggle it on or off.
|
---|
| 1116 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1117 | */
|
---|
| 1118 | void
|
---|
[128] | 1119 | toggle_path_expandability(struct s_node *filelist, char *pathname,
|
---|
| 1120 | bool on_or_off)
|
---|
[1] | 1121 | {
|
---|
| 1122 |
|
---|
[128] | 1123 | /*@ int ******************************************************** */
|
---|
| 1124 | static int depth = 0;
|
---|
| 1125 | static int total_expanded;
|
---|
| 1126 | static int root_depth;
|
---|
| 1127 | int j;
|
---|
| 1128 | /*@ structures ************************************************* */
|
---|
| 1129 | struct s_node *node;
|
---|
[1] | 1130 |
|
---|
[128] | 1131 | /*@ buffers **************************************************** */
|
---|
| 1132 | static char current_filename[MAX_STR_LEN];
|
---|
[1] | 1133 |
|
---|
| 1134 | /* char tmp[MAX_STR_LEN+2]; */
|
---|
| 1135 |
|
---|
[128] | 1136 | /*@ end vars *************************************************** */
|
---|
[1] | 1137 |
|
---|
[128] | 1138 | assert(filelist != NULL);
|
---|
| 1139 | assert_string_is_neither_NULL_nor_zerolength(pathname);
|
---|
| 1140 | if (depth == 0) {
|
---|
| 1141 | total_expanded = 0;
|
---|
[1] | 1142 | // log_it ("Toggling path's expandability");
|
---|
[128] | 1143 | for (root_depth = (int) strlen(pathname);
|
---|
| 1144 | root_depth > 0 && pathname[root_depth - 1] != '/';
|
---|
| 1145 | root_depth--);
|
---|
| 1146 | if (root_depth < 2) {
|
---|
| 1147 | root_depth = (int) strlen(pathname);
|
---|
| 1148 | }
|
---|
[1] | 1149 | }
|
---|
[128] | 1150 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1151 | current_filename[depth] = node->ch;
|
---|
| 1152 | if (node->down) {
|
---|
| 1153 | depth++;
|
---|
| 1154 | toggle_path_expandability(node->down, pathname, on_or_off);
|
---|
| 1155 | depth--;
|
---|
[1] | 1156 | }
|
---|
[128] | 1157 | if (node->ch == '\0') {
|
---|
| 1158 | if (!strncmp(pathname, current_filename, strlen(pathname))) {
|
---|
| 1159 | for (j = root_depth;
|
---|
| 1160 | current_filename[j] != '/'
|
---|
| 1161 | && current_filename[j] != '\0'; j++);
|
---|
| 1162 | if (current_filename[j] != '\0') {
|
---|
| 1163 | for (j++;
|
---|
| 1164 | current_filename[j] != '/'
|
---|
| 1165 | && current_filename[j] != '\0'; j++);
|
---|
| 1166 | }
|
---|
| 1167 | if (current_filename[j] == '\0') {
|
---|
| 1168 | node->expanded =
|
---|
| 1169 | (!strcmp(pathname, current_filename) ? TRUE :
|
---|
| 1170 | on_or_off);
|
---|
| 1171 | }
|
---|
| 1172 | }
|
---|
[1] | 1173 | }
|
---|
[128] | 1174 | if (node->expanded) {
|
---|
| 1175 | if (total_expanded < ARBITRARY_MAXIMUM - 32
|
---|
| 1176 | || !strrchr(current_filename + strlen(pathname), '/')) {
|
---|
| 1177 | total_expanded++;
|
---|
| 1178 | } else {
|
---|
| 1179 | node->expanded = FALSE;
|
---|
| 1180 | }
|
---|
| 1181 | }
|
---|
[1] | 1182 | }
|
---|
[128] | 1183 | if (depth == 0) {
|
---|
| 1184 | // log_it ("Finished toggling expandability");
|
---|
[1] | 1185 | }
|
---|
| 1186 | }
|
---|
| 1187 |
|
---|
| 1188 | /**
|
---|
| 1189 | * Toggle whether a path is selected.
|
---|
| 1190 | * @param filelist The filelist tree to operate on.
|
---|
| 1191 | * @param pathname The path to toggle selection of.
|
---|
| 1192 | * @param on_or_off Whether to toggle it on or off.
|
---|
| 1193 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1194 | */
|
---|
| 1195 | void
|
---|
[128] | 1196 | toggle_path_selection(struct s_node *filelist, char *pathname,
|
---|
| 1197 | bool on_or_off)
|
---|
[1] | 1198 | {
|
---|
[128] | 1199 | /*@ int ********************************************************* */
|
---|
| 1200 | static int depth = 0;
|
---|
| 1201 | int j;
|
---|
[1] | 1202 |
|
---|
[128] | 1203 | /*@ structures ************************************************** */
|
---|
| 1204 | struct s_node *node;
|
---|
[1] | 1205 |
|
---|
[128] | 1206 | /*@ buffers ***************************************************** */
|
---|
| 1207 | static char current_filename[MAX_STR_LEN];
|
---|
[1] | 1208 |
|
---|
[128] | 1209 | /*@ end vars *************************************************** */
|
---|
| 1210 | assert(filelist != NULL);
|
---|
| 1211 | assert_string_is_neither_NULL_nor_zerolength(pathname);
|
---|
| 1212 | if (depth == 0) {
|
---|
| 1213 | log_it("Toggling path's selection");
|
---|
[1] | 1214 | }
|
---|
[128] | 1215 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1216 | current_filename[depth] = node->ch;
|
---|
| 1217 | if (node->down) {
|
---|
| 1218 | depth++;
|
---|
| 1219 | toggle_path_selection(node->down, pathname, on_or_off);
|
---|
| 1220 | depth--;
|
---|
[1] | 1221 | }
|
---|
[128] | 1222 | if (node->ch == '\0') {
|
---|
| 1223 | if (!strncmp(pathname, current_filename, strlen(pathname))) {
|
---|
| 1224 | for (j = 0;
|
---|
| 1225 | pathname[j] != '\0'
|
---|
| 1226 | && pathname[j] == current_filename[j]; j++);
|
---|
[2521] | 1227 | if (current_filename[j] == '/'
|
---|
| 1228 | || current_filename[j] == '\0') {
|
---|
| 1229 | node->selected = on_or_off;
|
---|
[128] | 1230 | }
|
---|
| 1231 | }
|
---|
| 1232 | }
|
---|
[1] | 1233 | }
|
---|
[128] | 1234 | if (depth == 0) {
|
---|
| 1235 | log_it("Finished toggling selection");
|
---|
| 1236 | }
|
---|
[1] | 1237 | }
|
---|
| 1238 |
|
---|
| 1239 |
|
---|
| 1240 | /**
|
---|
| 1241 | * Toggle node selection of a filelist tree.
|
---|
| 1242 | * @param filelist The filelist tree to operate on.
|
---|
| 1243 | * @param on_or_off Whether to toggle selection on or off.
|
---|
| 1244 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1245 | */
|
---|
[128] | 1246 | void toggle_node_selection(struct s_node *filelist, bool on_or_off)
|
---|
[1] | 1247 | {
|
---|
[128] | 1248 | /*@ structure ************************************************** */
|
---|
| 1249 | struct s_node *node;
|
---|
[1] | 1250 |
|
---|
[128] | 1251 | /*@ end vars *************************************************** */
|
---|
| 1252 | assert(filelist != NULL);
|
---|
| 1253 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1254 | if (node->ch == '/') {
|
---|
| 1255 | continue;
|
---|
| 1256 | } /* don't go deep */
|
---|
| 1257 | if (node->ch == '\0') {
|
---|
| 1258 | node->selected = on_or_off;
|
---|
| 1259 | }
|
---|
| 1260 | if (node->down) {
|
---|
| 1261 | toggle_node_selection(node->down, on_or_off);
|
---|
| 1262 | }
|
---|
[1] | 1263 | }
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 |
|
---|
| 1267 |
|
---|
[2366] | 1268 | /**
|
---|
[2369] | 1269 | * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
|
---|
[2366] | 1270 | */
|
---|
[2369] | 1271 | char *g_skeleton_filelist = NULL;
|
---|
[1] | 1272 |
|
---|
| 1273 |
|
---|
| 1274 |
|
---|
[2369] | 1275 | /**
|
---|
| 1276 | * Get the next entry in the space-separated list in @p incoming.
|
---|
| 1277 | * So if @p incoming was '"one and two" three four', we would
|
---|
| 1278 | * return "one and two".
|
---|
| 1279 | * @param incoming The list to get the next entry from.
|
---|
| 1280 | * @return The first item in the list (respecting double quotes).
|
---|
| 1281 | * @note The returned string points to static data that will be overwritten with each call.
|
---|
| 1282 | */
|
---|
| 1283 | char *next_entry(char *incoming)
|
---|
| 1284 | {
|
---|
| 1285 | char *sz_res;
|
---|
| 1286 | char *p;
|
---|
| 1287 | bool in_quotes = FALSE;
|
---|
[1] | 1288 |
|
---|
[2369] | 1289 | mr_asprintf(&sz_res, "%s", incoming);
|
---|
| 1290 | p = sz_res;
|
---|
| 1291 | while ((*p != ' ' || in_quotes) && *p != '\0') {
|
---|
| 1292 | if (*p == '\"') {
|
---|
| 1293 | in_quotes = !in_quotes;
|
---|
[2366] | 1294 | }
|
---|
[2369] | 1295 | p++;
|
---|
[2366] | 1296 | }
|
---|
[2369] | 1297 | *p = '\0';
|
---|
| 1298 | return (sz_res);
|
---|
[2366] | 1299 | }
|
---|
| 1300 |
|
---|
| 1301 |
|
---|
| 1302 |
|
---|
| 1303 |
|
---|
[1] | 1304 | /**
|
---|
| 1305 | * Number of entries in the skeleton filelist.
|
---|
| 1306 | */
|
---|
[128] | 1307 | long g_skeleton_entries = 0;
|
---|
[1] | 1308 |
|
---|
| 1309 |
|
---|
| 1310 | /**
|
---|
| 1311 | * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
|
---|
| 1312 | * @param dir The directory to list to @p fout.
|
---|
| 1313 | * @param sth The directories to skip (exclude).
|
---|
| 1314 | * @param fout The file to write everything to.
|
---|
| 1315 | * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
|
---|
| 1316 | * @return 0, always.
|
---|
| 1317 | * @bug Return value should be @c void.
|
---|
| 1318 | */
|
---|
[2198] | 1319 | int open_and_list_dir(char *dir1, char *sth, FILE * fout,
|
---|
[128] | 1320 | time_t time_of_last_full_backup)
|
---|
[1] | 1321 | {
|
---|
[820] | 1322 | const char delims[] = " ";
|
---|
| 1323 |
|
---|
[128] | 1324 | DIR *dip;
|
---|
| 1325 | struct dirent *dit;
|
---|
| 1326 | struct stat statbuf;
|
---|
[2099] | 1327 | char *new;
|
---|
[128] | 1328 | char *tmp;
|
---|
[2198] | 1329 | char *dir = NULL;
|
---|
[128] | 1330 | static int percentage = 0;
|
---|
| 1331 | char *skip_these;
|
---|
| 1332 | char *new_with_spaces;
|
---|
[820] | 1333 | char *strtmp;
|
---|
| 1334 | char *token;
|
---|
| 1335 | char *find_excludes;
|
---|
[128] | 1336 | static char *name_of_evalcall_form;
|
---|
| 1337 | int i;
|
---|
[820] | 1338 | int lastpos = 0;
|
---|
[128] | 1339 | static int depth = 0;
|
---|
| 1340 | char *p;
|
---|
| 1341 | static int counter = 0;
|
---|
| 1342 | static int uberctr = 0;
|
---|
| 1343 | static char *find_skeleton_marker;
|
---|
| 1344 | static long skeleton_lino = 0;
|
---|
| 1345 | static time_t last_time = 0;
|
---|
| 1346 | time_t this_time;
|
---|
[1] | 1347 |
|
---|
[2402] | 1348 | /* dir is needed when we pass it to the shell */
|
---|
[2206] | 1349 | dir = mr_stresc(dir1, "`$\\\"(){}'[]&*?|!#~", '\\');
|
---|
[2436] | 1350 | p = strrchr(dir1, '/');
|
---|
[128] | 1351 | if (p) {
|
---|
| 1352 | if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
|
---|
[2241] | 1353 | mr_free(dir);
|
---|
[128] | 1354 | return (0);
|
---|
| 1355 | }
|
---|
| 1356 | }
|
---|
| 1357 |
|
---|
| 1358 | if (!depth) {
|
---|
| 1359 | malloc_string(name_of_evalcall_form);
|
---|
| 1360 | malloc_string(find_skeleton_marker);
|
---|
[2211] | 1361 | mr_asprintf(&find_excludes, " ");
|
---|
[820] | 1362 | while((token = mr_strtok (sth, delims, &lastpos))) {
|
---|
[2211] | 1363 | mr_asprintf(&strtmp,"%s", find_excludes);
|
---|
[820] | 1364 | paranoid_free(find_excludes);
|
---|
[2211] | 1365 | mr_asprintf(&find_excludes,"%s -path %s -prune -o", strtmp, token);
|
---|
[820] | 1366 | paranoid_free(strtmp);
|
---|
[2241] | 1367 | mr_free(token);
|
---|
[820] | 1368 | }
|
---|
[1] | 1369 | #if linux
|
---|
[128] | 1370 | // 2.6 has /sys as a proc-type thing -- must be excluded
|
---|
[2548] | 1371 | mr_asprintf(&strtmp, "find '%s' -fstype mvfs -prune -o -fstype devpts -prune -o -fstype tmpfs -prune -o -fstype proc -prune -o -fstype sysfs -prune -o -fstype rpc_pipefs -prune -o %s -type d -print > %s 2> /dev/null", dir, find_excludes, g_skeleton_filelist);
|
---|
[1] | 1372 | #else
|
---|
[128] | 1373 | // On BSD, for example, /sys is the kernel sources -- don't exclude
|
---|
[2290] | 1374 | mr_asprintf(&strtmp, "find '%s' -fstype mvfs -prune -o -path /proc -prune -o %s -type d -print > %s 2> /dev/null", dir, find_excludes, g_skeleton_filelist);
|
---|
[1] | 1375 | #endif
|
---|
[820] | 1376 | paranoid_free(find_excludes);
|
---|
| 1377 | log_msg(5, "find command = %s", strtmp);
|
---|
[2202] | 1378 | (void)system(strtmp);
|
---|
[820] | 1379 | paranoid_free(strtmp);
|
---|
[2211] | 1380 | mr_asprintf(&tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
|
---|
[128] | 1381 | g_skeleton_entries =
|
---|
| 1382 | 1 + atol(call_program_and_get_last_line_of_output(tmp));
|
---|
[2099] | 1383 | paranoid_free(tmp);
|
---|
[2402] | 1384 | sprintf(name_of_evalcall_form, "Making catalog of %s", dir1);
|
---|
[128] | 1385 | open_evalcall_form(name_of_evalcall_form);
|
---|
| 1386 | find_skeleton_marker[0] = '\0';
|
---|
| 1387 | skeleton_lino = 1;
|
---|
| 1388 | log_msg(5, "entries = %ld", g_skeleton_entries);
|
---|
| 1389 | percentage = 0;
|
---|
[2190] | 1390 | } else // update evalcall form if appropriate
|
---|
[128] | 1391 | {
|
---|
| 1392 | sprintf(find_skeleton_marker,
|
---|
[2198] | 1393 | "grep -Fv '%s' %s > %s.new 2> /dev/null", dir,
|
---|
[128] | 1394 | g_skeleton_filelist, g_skeleton_filelist);
|
---|
| 1395 | // log_msg(0, "fsm = %s", find_skeleton_marker);
|
---|
| 1396 | if (!system(find_skeleton_marker)) {
|
---|
| 1397 | percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
|
---|
| 1398 | skeleton_lino++;
|
---|
| 1399 | // log_msg(5, "Found %s", dir);
|
---|
| 1400 | // log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
|
---|
| 1401 | sprintf(find_skeleton_marker, "mv -f %s.new %s",
|
---|
| 1402 | g_skeleton_filelist, g_skeleton_filelist);
|
---|
| 1403 | // log_msg(6, "fsm = %s", find_skeleton_marker);
|
---|
[2436] | 1404 | paranoid_system(find_skeleton_marker);
|
---|
[128] | 1405 | time(&this_time);
|
---|
| 1406 | if (this_time != last_time) {
|
---|
| 1407 | last_time = this_time;
|
---|
[1] | 1408 | #ifndef _XWIN
|
---|
[128] | 1409 | if (!g_text_mode) {
|
---|
[2402] | 1410 | mr_asprintf(&tmp, "Reading %-68s", dir1);
|
---|
[128] | 1411 | newtDrawRootText(0, g_noof_rows - 3, tmp);
|
---|
[2099] | 1412 | paranoid_free(tmp);
|
---|
[128] | 1413 | }
|
---|
[1] | 1414 | #endif
|
---|
[128] | 1415 | update_evalcall_form(percentage);
|
---|
| 1416 | }
|
---|
[1] | 1417 | }
|
---|
[128] | 1418 | }
|
---|
[1] | 1419 |
|
---|
[128] | 1420 | depth++;
|
---|
[1] | 1421 |
|
---|
| 1422 | // log_msg(0, "Cataloguing %s", dir);
|
---|
[128] | 1423 | if (sth[0] == ' ') {
|
---|
[2211] | 1424 | mr_asprintf(&skip_these, "%s", sth);
|
---|
[128] | 1425 | } else {
|
---|
[2211] | 1426 | mr_asprintf(&skip_these, " %s ", sth);
|
---|
[128] | 1427 | }
|
---|
[2402] | 1428 | mr_asprintf(&new_with_spaces, " %s ", dir1);
|
---|
| 1429 | if ((dip = opendir(dir1)) == NULL) {
|
---|
| 1430 | mr_asprintf(&tmp,"opendir %s", dir1);
|
---|
[1260] | 1431 | log_OS_error(tmp);
|
---|
[2099] | 1432 | paranoid_free(tmp);
|
---|
[128] | 1433 | } else if (strstr(skip_these, new_with_spaces)) {
|
---|
[2436] | 1434 | log_msg(10, "Found dir ***%s**** excluded", dir1);
|
---|
[2402] | 1435 | fprintf(fout, "%s\n", dir1); // if excluded dir then print dir ONLY
|
---|
[128] | 1436 | } else {
|
---|
[2436] | 1437 | log_msg(10, "Found dir ***%s**** parsed", dir1);
|
---|
[2402] | 1438 | fprintf(fout, "%s\n", dir1);
|
---|
[128] | 1439 | while ((dit = readdir(dip)) != NULL) {
|
---|
| 1440 | i++;
|
---|
[2402] | 1441 | if (strcmp(dir1, "/")) {
|
---|
| 1442 | mr_asprintf(&new,"%s/%s",dir1,dit->d_name);
|
---|
[2099] | 1443 | } else {
|
---|
[2402] | 1444 | mr_asprintf(&new,"%s%s",dir1,dit->d_name);
|
---|
[128] | 1445 | }
|
---|
[2099] | 1446 | paranoid_free(new_with_spaces);
|
---|
[2211] | 1447 | mr_asprintf(&new_with_spaces, " %s ", new);
|
---|
[128] | 1448 | if (strstr(skip_these, new_with_spaces)) {
|
---|
| 1449 | fprintf(fout, "%s\n", new);
|
---|
[2436] | 1450 | log_msg(10, "Found child dir ***%s**** excluded", new);
|
---|
[2099] | 1451 | paranoid_free(new_with_spaces);
|
---|
[128] | 1452 | } else {
|
---|
[2099] | 1453 | paranoid_free(new_with_spaces);
|
---|
[128] | 1454 | if (!lstat(new, &statbuf)) {
|
---|
| 1455 | if (!S_ISLNK(statbuf.st_mode)
|
---|
| 1456 | && S_ISDIR(statbuf.st_mode)) {
|
---|
[2436] | 1457 | log_msg(10, "Found child dir ***%s**** parsed", new);
|
---|
[128] | 1458 | open_and_list_dir(new, skip_these, fout,
|
---|
| 1459 | time_of_last_full_backup);
|
---|
| 1460 | } else {
|
---|
| 1461 | if (time_of_last_full_backup == 0
|
---|
| 1462 | || time_of_last_full_backup <
|
---|
| 1463 | statbuf.st_ctime) {
|
---|
[2436] | 1464 | log_msg(10, "Found child file ***%s**** parsed", new);
|
---|
[128] | 1465 | fprintf(fout, "%s\n", new);
|
---|
| 1466 | if ((counter++) > 128) {
|
---|
| 1467 | counter = 0;
|
---|
| 1468 | uberctr++;
|
---|
[2290] | 1469 | mr_asprintf(&tmp, " %c ", special_dot_char(uberctr));
|
---|
[1] | 1470 | #ifndef _XWIN
|
---|
[128] | 1471 | if (!g_text_mode) {
|
---|
| 1472 | newtDrawRootText(77, g_noof_rows - 3,
|
---|
| 1473 | tmp);
|
---|
| 1474 | newtRefresh();
|
---|
| 1475 | }
|
---|
[1] | 1476 | #endif
|
---|
[2099] | 1477 | paranoid_free(tmp);
|
---|
[128] | 1478 | }
|
---|
| 1479 | }
|
---|
| 1480 | }
|
---|
| 1481 | }
|
---|
| 1482 | }
|
---|
[2099] | 1483 | paranoid_free(new);
|
---|
[1] | 1484 | }
|
---|
[128] | 1485 | }
|
---|
[2099] | 1486 | paranoid_free(new_with_spaces);
|
---|
| 1487 | paranoid_free(skip_these);
|
---|
[2241] | 1488 | mr_free(dir);
|
---|
[2099] | 1489 |
|
---|
[128] | 1490 | if (dip) {
|
---|
| 1491 | if (closedir(dip) == -1) {
|
---|
| 1492 | log_OS_error("closedir");
|
---|
| 1493 | }
|
---|
| 1494 | }
|
---|
| 1495 | depth--;
|
---|
| 1496 | if (!depth) {
|
---|
| 1497 | close_evalcall_form();
|
---|
| 1498 | paranoid_free(name_of_evalcall_form);
|
---|
| 1499 | paranoid_free(find_skeleton_marker);
|
---|
| 1500 | unlink(g_skeleton_filelist);
|
---|
| 1501 | log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
|
---|
| 1502 | }
|
---|
| 1503 | return (0);
|
---|
[1] | 1504 | }
|
---|
| 1505 |
|
---|
| 1506 |
|
---|
| 1507 |
|
---|
| 1508 | /**
|
---|
[2369] | 1509 | * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
|
---|
| 1510 | * @param logfile Unused.
|
---|
| 1511 | * @param tmpdir The tmpdir of the backup.
|
---|
| 1512 | * @param scratchdir The scratchdir of the backup.
|
---|
| 1513 | * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
|
---|
| 1514 | * @param excp The paths to NOT back up.
|
---|
| 1515 | * @param differential The differential level (currently only 0 and 1 are supported).
|
---|
| 1516 | * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
|
---|
| 1517 | * @return 0, always.
|
---|
| 1518 | * @bug @p logfile is unused.
|
---|
| 1519 | * @bug Return value is meaningless.
|
---|
[1] | 1520 | */
|
---|
[2369] | 1521 | int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
|
---|
| 1522 | char *include_paths, char *excp, int differential,
|
---|
| 1523 | char *userdef_filelist)
|
---|
[1] | 1524 | {
|
---|
[2369] | 1525 | char *p, *q;
|
---|
| 1526 | char *sz_datefile;
|
---|
| 1527 | char *sz_filelist, *exclude_paths, *tmp;
|
---|
| 1528 | int i;
|
---|
| 1529 | FILE *fout;
|
---|
[2521] | 1530 | char *command = NULL;
|
---|
[2369] | 1531 | time_t time_of_last_full_backup = 0;
|
---|
| 1532 | struct stat statbuf;
|
---|
| 1533 | char *tmp1 = NULL;
|
---|
| 1534 | char *tmp2 = NULL;
|
---|
[1] | 1535 |
|
---|
[2369] | 1536 | malloc_string(tmp);
|
---|
| 1537 | malloc_string(g_skeleton_filelist);
|
---|
| 1538 | if (!(exclude_paths = malloc(8*MAX_STR_LEN))) {
|
---|
| 1539 | fatal_error("Cannot malloc exclude_paths");
|
---|
| 1540 | }
|
---|
| 1541 | mr_asprintf(&sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
|
---|
| 1542 | if (!include_paths && !userdef_filelist) {
|
---|
| 1543 | fatal_error
|
---|
| 1544 | ("Please supply either include_paths or userdef_filelist");
|
---|
| 1545 | }
|
---|
| 1546 | // make hole for filelist
|
---|
[2521] | 1547 | mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
|
---|
[2369] | 1548 | paranoid_system(command);
|
---|
[2521] | 1549 | mr_free(command);
|
---|
| 1550 |
|
---|
[2369] | 1551 | mr_asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
|
---|
| 1552 | make_hole_for_file(sz_filelist);
|
---|
| 1553 |
|
---|
| 1554 | if (differential == 0) {
|
---|
| 1555 | // restore last good datefile if it exists
|
---|
[2521] | 1556 | mr_asprintf(&command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
|
---|
[2369] | 1557 | run_program_and_log_output(command, 3);
|
---|
[2521] | 1558 | mr_free(command);
|
---|
| 1559 |
|
---|
[2369] | 1560 | // backup last known good datefile just in case :)
|
---|
| 1561 | if (does_file_exist(sz_datefile)) {
|
---|
[2521] | 1562 | mr_asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
|
---|
[2369] | 1563 | sz_datefile);
|
---|
| 1564 | paranoid_system(command);
|
---|
[2521] | 1565 | mr_free(command);
|
---|
[128] | 1566 | }
|
---|
[2369] | 1567 | make_hole_for_file(sz_datefile);
|
---|
| 1568 | write_one_liner_data_file(sz_datefile,
|
---|
| 1569 | call_program_and_get_last_line_of_output
|
---|
| 1570 | ("date +%s"));
|
---|
| 1571 | } else if (lstat(sz_datefile, &statbuf)) {
|
---|
| 1572 | log_msg(2,
|
---|
| 1573 | "Warning - unable to find date of previous backup. Full backup instead.");
|
---|
| 1574 | differential = 0;
|
---|
| 1575 | time_of_last_full_backup = 0;
|
---|
| 1576 | } else {
|
---|
| 1577 | time_of_last_full_backup = statbuf.st_mtime;
|
---|
| 1578 | log_msg(2, "Differential backup. Yay.");
|
---|
[128] | 1579 | }
|
---|
[2369] | 1580 | paranoid_free(sz_datefile);
|
---|
| 1581 |
|
---|
| 1582 | // use user-specified filelist (if specified)
|
---|
| 1583 | if (userdef_filelist) {
|
---|
| 1584 | log_msg(1,
|
---|
| 1585 | "Using the user-specified filelist - %s - instead of calculating one",
|
---|
| 1586 | userdef_filelist);
|
---|
[2521] | 1587 | mr_asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
|
---|
[2369] | 1588 | if (run_program_and_log_output(command, 3)) {
|
---|
[2521] | 1589 | mr_free(command);
|
---|
[2369] | 1590 | fatal_error("Failed to copy user-specified filelist");
|
---|
| 1591 | }
|
---|
[2521] | 1592 | mr_free(command);
|
---|
[2369] | 1593 | } else {
|
---|
| 1594 | log_msg(2, "include_paths = '%s'", include_paths);
|
---|
| 1595 | log_msg(1, "Calculating filelist");
|
---|
| 1596 | mr_asprintf(&tmp2, "%s", call_program_and_get_last_line_of_output("mount | grep -Ew 'ntfs|ntfs-3g|fat|vfat|dos' | awk '{print $3}'"));
|
---|
| 1597 | if (strlen(tmp2) < 1) {
|
---|
| 1598 | mr_asprintf(&tmp1," ");
|
---|
| 1599 | } else {
|
---|
| 1600 | log_msg(2, "Found windows FS: %s",tmp2);
|
---|
| 1601 | mr_asprintf(&tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
|
---|
| 1602 | paranoid_free(tmp2);
|
---|
| 1603 | mr_asprintf(&tmp2, "%s", call_program_and_get_last_line_of_output(tmp1));
|
---|
| 1604 | log_msg(2, "Found windows files: %s",tmp2);
|
---|
| 1605 | }
|
---|
| 1606 | paranoid_free(tmp1);
|
---|
| 1607 |
|
---|
| 1608 | snprintf(exclude_paths, (size_t)8*MAX_STR_LEN," %s %s %s %s %s . .. \
|
---|
| 1609 | " MNT_CDROM " " MNT_FLOPPY " /media /tmp \
|
---|
[2469] | 1610 | /proc /sys /dev/shm " MINDI_CACHE, MONDO_CACHE, excp, tmp2, (tmpdir[0] == '/' && tmpdir[1] == '/') ? (tmpdir + 1) : tmpdir, (scratchdir[0] == '/' && scratchdir[1] == '/') ? (scratchdir + 1) : scratchdir);
|
---|
[2369] | 1611 | paranoid_free(tmp2);
|
---|
| 1612 |
|
---|
| 1613 | log_msg(2, "Excluding paths = '%s'", exclude_paths);
|
---|
| 1614 | log_msg(2,
|
---|
| 1615 | "Generating skeleton filelist so that we can track our progress");
|
---|
| 1616 | sprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
|
---|
| 1617 | make_hole_for_file(g_skeleton_filelist);
|
---|
| 1618 | log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
|
---|
| 1619 | log_msg(2, "Opening out filelist to %s", sz_filelist);
|
---|
| 1620 | if (!(fout = fopen(sz_filelist, "w"))) {
|
---|
| 1621 | fatal_error("Cannot openout to sz_filelist");
|
---|
| 1622 | }
|
---|
| 1623 | i = 0;
|
---|
| 1624 | if (strlen(include_paths) == 0) {
|
---|
| 1625 | log_msg(1, "Including only '/' in %s", sz_filelist);
|
---|
| 1626 | open_and_list_dir("/", exclude_paths, fout,
|
---|
| 1627 | time_of_last_full_backup);
|
---|
| 1628 | } else {
|
---|
| 1629 | p = include_paths;
|
---|
| 1630 | while (*p) {
|
---|
| 1631 | q = next_entry(p);
|
---|
| 1632 | log_msg(1, "Including %s in filelist %s", q, sz_filelist);
|
---|
| 1633 | open_and_list_dir(q, exclude_paths, fout,
|
---|
| 1634 | time_of_last_full_backup);
|
---|
| 1635 | p += strlen(q);
|
---|
| 1636 | paranoid_free(q);
|
---|
| 1637 | while (*p == ' ') {
|
---|
| 1638 | p++;
|
---|
| 1639 | }
|
---|
| 1640 | }
|
---|
| 1641 | }
|
---|
| 1642 | paranoid_fclose(fout);
|
---|
| 1643 | }
|
---|
| 1644 | log_msg(2, "Copying new filelist to scratchdir");
|
---|
[2521] | 1645 | mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
|
---|
[2369] | 1646 | paranoid_system(command);
|
---|
[2521] | 1647 | mr_free(command);
|
---|
| 1648 |
|
---|
| 1649 | mr_asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
|
---|
[2369] | 1650 | paranoid_system(command);
|
---|
[2521] | 1651 | mr_free(command);
|
---|
| 1652 |
|
---|
| 1653 | mr_asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
|
---|
[2369] | 1654 | paranoid_system(command);
|
---|
[2521] | 1655 | mr_free(command);
|
---|
| 1656 |
|
---|
[2369] | 1657 | paranoid_free(sz_filelist);
|
---|
| 1658 | log_msg(2, "Freeing variables");
|
---|
| 1659 | paranoid_free(exclude_paths);
|
---|
| 1660 | paranoid_free(tmp);
|
---|
| 1661 | paranoid_free(g_skeleton_filelist);
|
---|
| 1662 | log_msg(2, "Exiting");
|
---|
| 1663 | return (0);
|
---|
[1] | 1664 | }
|
---|
| 1665 |
|
---|
[2369] | 1666 | /**
|
---|
| 1667 | * Wrapper around mondo_makefilelist().
|
---|
| 1668 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1669 | * - @c bkpinfo->differential
|
---|
| 1670 | * - @c bkpinfo->exclude_paths
|
---|
| 1671 | * - @c bkpinfo->include_paths
|
---|
| 1672 | * - @c bkpinfo->make_filelist
|
---|
| 1673 | * - @c bkpinfo->scratchdir
|
---|
| 1674 | * - @c bkpinfo->tmpdir
|
---|
| 1675 | * @return 0 for success, nonzero for failure.
|
---|
| 1676 | * @see mondo_makefilelist
|
---|
| 1677 | */
|
---|
| 1678 | int prepare_filelist()
|
---|
| 1679 | {
|
---|
[1] | 1680 |
|
---|
[2369] | 1681 | /*@ int **************************************************** */
|
---|
| 1682 | int res = 0;
|
---|
[1] | 1683 |
|
---|
[2369] | 1684 | assert(bkpinfo != NULL);
|
---|
| 1685 | log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir,
|
---|
| 1686 | bkpinfo->scratchdir);
|
---|
| 1687 | if (bkpinfo->make_filelist) {
|
---|
| 1688 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
| 1689 | "Making catalog of files to be backed up");
|
---|
| 1690 | } else {
|
---|
| 1691 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
| 1692 | "Using supplied catalog of files to be backed up");
|
---|
| 1693 | }
|
---|
| 1694 |
|
---|
| 1695 | if (bkpinfo->make_filelist) {
|
---|
| 1696 | res =
|
---|
| 1697 | mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
|
---|
| 1698 | bkpinfo->scratchdir, bkpinfo->include_paths,
|
---|
| 1699 | bkpinfo->exclude_paths,
|
---|
| 1700 | bkpinfo->differential, NULL);
|
---|
| 1701 | } else {
|
---|
| 1702 | res =
|
---|
| 1703 | mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
|
---|
| 1704 | bkpinfo->scratchdir, NULL,
|
---|
| 1705 | bkpinfo->exclude_paths,
|
---|
| 1706 | bkpinfo->differential,
|
---|
| 1707 | bkpinfo->include_paths);
|
---|
| 1708 | }
|
---|
| 1709 |
|
---|
| 1710 | if (res) {
|
---|
| 1711 | log_OS_error("Call to mondo_makefilelist failed");
|
---|
| 1712 | mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
|
---|
| 1713 | } else {
|
---|
| 1714 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
| 1715 | }
|
---|
| 1716 | return (res);
|
---|
| 1717 | }
|
---|
| 1718 |
|
---|
| 1719 |
|
---|
[1] | 1720 | /**
|
---|
| 1721 | * Locate the string @p string_to_find in the tree rooted at @p startnode.
|
---|
| 1722 | * @param startnode The node containing the root of the directory tree.
|
---|
| 1723 | * @param string_to_find The string to look for at @p startnode.
|
---|
| 1724 | * @return The node containing the last element of @p string_to_find, or NULL if
|
---|
| 1725 | * it was not found.
|
---|
| 1726 | */
|
---|
[128] | 1727 | struct s_node *find_string_at_node(struct s_node *startnode,
|
---|
| 1728 | char *string_to_find)
|
---|
[1] | 1729 | {
|
---|
[128] | 1730 | /*@ int ******************************************************** */
|
---|
| 1731 | int noof_chars;
|
---|
| 1732 | static int depth = 0;
|
---|
| 1733 | static char original_string[MAX_STR_LEN];
|
---|
[1] | 1734 |
|
---|
[128] | 1735 | /*@ sturctures ************************************************* */
|
---|
| 1736 | struct s_node *node;
|
---|
[1] | 1737 |
|
---|
[128] | 1738 | /*@ char ****************************************************** */
|
---|
| 1739 | char char_to_find;
|
---|
[1] | 1740 |
|
---|
[128] | 1741 | /*@ bools ****************************************************** */
|
---|
[1] | 1742 |
|
---|
[128] | 1743 | if (!depth) {
|
---|
| 1744 | strcpy(original_string, string_to_find);
|
---|
| 1745 | }
|
---|
[1] | 1746 |
|
---|
[128] | 1747 | assert(startnode != NULL);
|
---|
| 1748 | assert(string_to_find != NULL);
|
---|
[1] | 1749 |
|
---|
[128] | 1750 | noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
|
---|
[1] | 1751 |
|
---|
[128] | 1752 | log_msg(7, "starting --- str=%s", string_to_find);
|
---|
[1] | 1753 |
|
---|
| 1754 | /* walk across tree if necessary */
|
---|
[128] | 1755 | node = startnode;
|
---|
| 1756 | char_to_find = string_to_find[0];
|
---|
| 1757 | if (node->right != NULL && node->ch < char_to_find) {
|
---|
| 1758 | log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
|
---|
| 1759 | char_to_find, node->ch, (node->right)->ch);
|
---|
| 1760 | return (find_string_at_node(node->right, string_to_find));
|
---|
| 1761 | }
|
---|
[1] | 1762 |
|
---|
| 1763 | /* walk down tree if appropriate */
|
---|
[128] | 1764 | if (node->down != NULL && node->ch == char_to_find) {
|
---|
| 1765 | log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
|
---|
| 1766 | depth++;
|
---|
| 1767 | node = find_string_at_node(node->down, string_to_find + 1);
|
---|
| 1768 | depth--;
|
---|
| 1769 | return (node);
|
---|
| 1770 | }
|
---|
[1] | 1771 |
|
---|
[128] | 1772 | if (char_to_find == '\0' && node->ch == '\0') {
|
---|
| 1773 | log_msg(7, "%s is in tree", original_string);
|
---|
| 1774 | return (node);
|
---|
| 1775 | } else {
|
---|
| 1776 | log_msg(7, "%s is NOT in tree", original_string);
|
---|
| 1777 | return (NULL);
|
---|
| 1778 | }
|
---|
[1] | 1779 | }
|
---|
| 1780 |
|
---|
| 1781 |
|
---|
| 1782 |
|
---|
| 1783 | /**
|
---|
| 1784 | * Write all entries in @p needles_list_fname which are also in
|
---|
| 1785 | * @p filelist to @p matches_list_fname.
|
---|
| 1786 | * @param needles_list_fname A file containing strings to look for, 1 per line.
|
---|
| 1787 | * @param filelist The node for the root of the directory structure to search in.
|
---|
| 1788 | * @param matches_list_fname The filename where we should put the matches.
|
---|
| 1789 | * @return The number of matches found.
|
---|
| 1790 | */
|
---|
[128] | 1791 | long save_filelist_entries_in_common(char *needles_list_fname,
|
---|
| 1792 | struct s_node *filelist,
|
---|
| 1793 | char *matches_list_fname,
|
---|
| 1794 | bool use_star)
|
---|
[1] | 1795 | {
|
---|
[128] | 1796 | int retval = 0;
|
---|
| 1797 | struct s_node *found_node;
|
---|
| 1798 | FILE *fin;
|
---|
| 1799 | FILE *fout;
|
---|
| 1800 | char *fname;
|
---|
| 1801 | char *tmp;
|
---|
| 1802 | size_t len = 0; // Scrub's patch doesn't work without that
|
---|
[1] | 1803 |
|
---|
| 1804 | // log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
|
---|
[128] | 1805 | malloc_string(fname);
|
---|
| 1806 | malloc_string(tmp);
|
---|
| 1807 | log_msg(5, "starting");
|
---|
| 1808 | log_msg(5, "needles_list_fname = %s", needles_list_fname);
|
---|
| 1809 | log_msg(5, "matches_list_fname = %s", matches_list_fname);
|
---|
| 1810 | if (!(fin = fopen(needles_list_fname, "r"))) {
|
---|
| 1811 | fatal_error("Cannot openin needles_list_fname");
|
---|
| 1812 | }
|
---|
| 1813 | if (!(fout = fopen(matches_list_fname, "w"))) {
|
---|
| 1814 | fatal_error("Cannot openout matches_list_fname");
|
---|
| 1815 | }
|
---|
| 1816 | while (!feof(fin)) {
|
---|
[1] | 1817 | // fscanf(fin, "%s\n", fname);
|
---|
[128] | 1818 | len = MAX_STR_LEN - 1;
|
---|
[2202] | 1819 | (void)getline(&fname, &len, fin); // patch by Scrub
|
---|
[128] | 1820 | if (!use_star) {
|
---|
| 1821 | if (fname[0] == '/') {
|
---|
| 1822 | strcpy(tmp, fname);
|
---|
| 1823 | } else {
|
---|
| 1824 | tmp[0] = '/';
|
---|
| 1825 | strcpy(tmp + 1, fname);
|
---|
| 1826 | }
|
---|
| 1827 | strcpy(fname, tmp);
|
---|
| 1828 | }
|
---|
| 1829 | while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
|
---|
| 1830 | fname[strlen(fname) - 1] = '\0';
|
---|
| 1831 | }
|
---|
[1] | 1832 |
|
---|
[128] | 1833 | log_msg(5, "Looking for '%s'", fname);
|
---|
| 1834 | found_node = find_string_at_node(filelist, fname);
|
---|
| 1835 | if (found_node) {
|
---|
| 1836 | if (found_node->selected) {
|
---|
| 1837 | if (fname[0] == '/') {
|
---|
| 1838 | strcpy(tmp, fname + 1);
|
---|
| 1839 | strcpy(fname, tmp);
|
---|
| 1840 | }
|
---|
| 1841 | log_msg(5, "Found '%s'", fname);
|
---|
| 1842 | turn_wildcard_chars_into_literal_chars(tmp, fname);
|
---|
| 1843 | fprintf(fout, "%s\n", tmp);
|
---|
| 1844 | retval++;
|
---|
| 1845 | }
|
---|
[1] | 1846 | }
|
---|
| 1847 | }
|
---|
[128] | 1848 | paranoid_fclose(fout);
|
---|
| 1849 | paranoid_fclose(fin);
|
---|
| 1850 | paranoid_free(fname);
|
---|
| 1851 | paranoid_free(tmp);
|
---|
| 1852 | return (retval);
|
---|
[1] | 1853 | }
|
---|
| 1854 |
|
---|
| 1855 |
|
---|
| 1856 |
|
---|
| 1857 |
|
---|
| 1858 |
|
---|
| 1859 |
|
---|
| 1860 | /**
|
---|
| 1861 | * Add all files listed in @p list_of_files_fname to the directory structure rooted at
|
---|
| 1862 | * @p filelist.
|
---|
| 1863 | * @param filelist The top node of the directory structure to add the files to.
|
---|
| 1864 | * @param list_of_files_fname The file containing the files to add, 1 per line.
|
---|
| 1865 | * @param flag_em If TRUE, then flag the added files for restoration.
|
---|
| 1866 | * @return 0 for success, nonzero for failure.
|
---|
| 1867 | */
|
---|
[128] | 1868 | int add_list_of_files_to_filelist(struct s_node *filelist,
|
---|
| 1869 | char *list_of_files_fname, bool flag_em)
|
---|
[1] | 1870 | {
|
---|
[128] | 1871 | FILE *fin;
|
---|
| 1872 | char *tmp;
|
---|
| 1873 | struct s_node *nod;
|
---|
[1] | 1874 |
|
---|
[128] | 1875 | malloc_string(tmp);
|
---|
| 1876 | log_msg(3, "Adding %s to filelist", list_of_files_fname);
|
---|
| 1877 | if (!(fin = fopen(list_of_files_fname, "r"))) {
|
---|
[2227] | 1878 | log_it("%s",list_of_files_fname);
|
---|
[128] | 1879 | return (1);
|
---|
| 1880 | }
|
---|
[2202] | 1881 | for ((void)fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 1882 | (void)fgets(tmp, MAX_STR_LEN, fin)) {
|
---|
[128] | 1883 | if (!tmp[0]) {
|
---|
| 1884 | continue;
|
---|
| 1885 | }
|
---|
| 1886 | if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
|
---|
| 1887 | && strlen(tmp) > 0) {
|
---|
| 1888 | tmp[strlen(tmp) - 1] = '\0';
|
---|
| 1889 | }
|
---|
| 1890 | log_msg(2, "tmp = '%s'", tmp);
|
---|
| 1891 | if (!tmp[0]) {
|
---|
| 1892 | continue;
|
---|
| 1893 | }
|
---|
| 1894 | if ((nod = find_string_at_node(filelist, tmp))) {
|
---|
| 1895 | log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
|
---|
| 1896 | } else {
|
---|
| 1897 | add_string_at_node(filelist, tmp);
|
---|
| 1898 | nod = find_string_at_node(filelist, tmp);
|
---|
| 1899 | }
|
---|
[1] | 1900 |
|
---|
[128] | 1901 | if (nod && flag_em) {
|
---|
| 1902 | toggle_path_selection(filelist, tmp, TRUE);
|
---|
| 1903 | log_msg(5, "Flagged '%s'", tmp);
|
---|
| 1904 | }
|
---|
[1] | 1905 | }
|
---|
[128] | 1906 | paranoid_fclose(fin);
|
---|
| 1907 | paranoid_free(tmp);
|
---|
| 1908 | return (0);
|
---|
[1] | 1909 | }
|
---|
| 1910 |
|
---|
| 1911 | /* @} - end of filelistGroup */
|
---|