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