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