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