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