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