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