[49] | 1 | /* $Id: libmondo-filelist.c 1081 2007-01-28 22:20:07Z 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 1081 2007-01-28 22:20:07Z 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;
|
---|
[1043] | 430 | char *strtmp = NULL;
|
---|
[49] | 431 | int i;
|
---|
[171] | 432 | size_t n = 0;
|
---|
[49] | 433 |
|
---|
| 434 | if (!(fin = fopen(filelist, "r"))) {
|
---|
| 435 | log_msg(1, "Cannot openin filelist %s", filelist);
|
---|
| 436 | return (1);
|
---|
| 437 | }
|
---|
[900] | 438 | mr_asprintf(&pout_command, "gzip -c1 > %s", auxlist_fname);
|
---|
[49] | 439 | if (!(pout = popen(pout_command, "w"))) {
|
---|
| 440 | log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
|
---|
| 441 | fclose(fin);
|
---|
[900] | 442 | mr_free(pout_command);
|
---|
[49] | 443 | return (4);
|
---|
| 444 | }
|
---|
[900] | 445 | mr_free(pout_command);
|
---|
[120] | 446 |
|
---|
[900] | 447 | for (mr_getline(&file_to_analyze, &n, fin); !feof(fin);
|
---|
| 448 | mr_getline(&file_to_analyze, &n, fin)) {
|
---|
[49] | 449 | i = strlen(file_to_analyze);
|
---|
| 450 | if (i > 0 && file_to_analyze[i - 1] < 32) {
|
---|
| 451 | file_to_analyze[i - 1] = '\0';
|
---|
| 452 | }
|
---|
| 453 | log_msg(8, "Analyzing %s", file_to_analyze);
|
---|
[863] | 454 | /* BERLIOS : to be checked */
|
---|
[1043] | 455 | mr_asprintf(&strtmp, syscall_sprintf, mr_stresc(file_to_analyze, "`$\\\"", '\\'));
|
---|
| 456 | mr_asprintf(&syscall, "%s 2>> /dev/null", strtmp);
|
---|
[1081] | 457 | mr_free(strtmp);
|
---|
[49] | 458 | call_exe_and_pipe_output_to_fd(syscall, pout);
|
---|
[900] | 459 | mr_free(syscall);
|
---|
[49] | 460 | }
|
---|
| 461 | paranoid_fclose(fin);
|
---|
| 462 | paranoid_pclose(pout);
|
---|
[900] | 463 | mr_free(file_to_analyze);
|
---|
[49] | 464 | return (0);
|
---|
[1] | 465 | }
|
---|
| 466 |
|
---|
| 467 |
|
---|
[49] | 468 | int get_acl_list(char *filelist, char *facl_fname)
|
---|
[1] | 469 | {
|
---|
[689] | 470 | char *command = NULL;
|
---|
[49] | 471 | int retval = 0;
|
---|
| 472 |
|
---|
[956] | 473 | if (g_getfacl != NULL) {
|
---|
| 474 | mr_asprintf(&command, "touch %s", facl_fname);
|
---|
| 475 | run_program_and_log_output(command, 8);
|
---|
| 476 | mr_free(command);
|
---|
[120] | 477 |
|
---|
[900] | 478 | mr_asprintf(&command,
|
---|
[276] | 479 | "getfacl --all-effective -P %s 2>> %s | gzip -c1 > %s 2>> %s",
|
---|
[127] | 480 | filelist, MONDO_LOGFILE, facl_fname, MONDO_LOGFILE);
|
---|
[49] | 481 | iamhere(command);
|
---|
| 482 | retval = system(command);
|
---|
[900] | 483 | mr_free(command);
|
---|
[49] | 484 | }
|
---|
| 485 | return (retval);
|
---|
[1] | 486 | }
|
---|
| 487 |
|
---|
| 488 |
|
---|
[49] | 489 | int get_fattr_list(char *filelist, char *fattr_fname)
|
---|
[1] | 490 | {
|
---|
[689] | 491 | char *command = NULL;
|
---|
| 492 | char *tmp = NULL;
|
---|
[49] | 493 | int retval = 0;
|
---|
| 494 |
|
---|
[956] | 495 | if (g_getfattr != NULL) {
|
---|
| 496 | mr_asprintf(&command, "touch %s", fattr_fname);
|
---|
| 497 | run_program_and_log_output(command, 8);
|
---|
| 498 | mr_free(command);
|
---|
[120] | 499 |
|
---|
[49] | 500 | retval =
|
---|
| 501 | gen_aux_list(filelist, "getfattr --en=hex -P -d \"%s\"",
|
---|
| 502 | fattr_fname);
|
---|
| 503 | }
|
---|
| 504 | return (retval);
|
---|
[1] | 505 | }
|
---|
| 506 |
|
---|
| 507 |
|
---|
[49] | 508 | int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
|
---|
| 509 | char *executable)
|
---|
[1] | 510 | {
|
---|
[49] | 511 | const int my_depth = 8;
|
---|
[127] | 512 | char *command, *syscall_pin, *syscall_pout;
|
---|
[120] | 513 | char *incoming = NULL;
|
---|
[127] | 514 | char *current_subset_file = NULL;
|
---|
[120] | 515 | char *current_master_file, *masklist;
|
---|
[49] | 516 | int retval = 0;
|
---|
| 517 | int i;
|
---|
[171] | 518 | size_t n = 0;
|
---|
[49] | 519 | char *p, *q;
|
---|
| 520 | FILE *pin, *pout, *faclin;
|
---|
[1] | 521 |
|
---|
[49] | 522 | log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist,
|
---|
| 523 | original_exat_fname, executable);
|
---|
| 524 | if (!orig_msklist || !orig_msklist[0]
|
---|
| 525 | || !does_file_exist(orig_msklist)) {
|
---|
| 526 | log_msg(1,
|
---|
| 527 | "No masklist provided. I shall therefore set ALL attributes.");
|
---|
[900] | 528 | mr_asprintf(&command, "gzip -dc %s | %s --restore - 2>> %s",
|
---|
[127] | 529 | original_exat_fname, executable, MONDO_LOGFILE);
|
---|
[49] | 530 | log_msg(1, "command = %s", command);
|
---|
| 531 | retval = system(command);
|
---|
[900] | 532 | mr_free(command);
|
---|
[49] | 533 | log_msg(1, "Returning w/ retval=%d", retval);
|
---|
| 534 | return (retval);
|
---|
| 535 | }
|
---|
| 536 | if (length_of_file(original_exat_fname) <= 0) {
|
---|
| 537 | log_msg(1,
|
---|
| 538 | "original_exat_fname %s is empty or missing, so no need to set EXAT list",
|
---|
| 539 | original_exat_fname);
|
---|
| 540 | return (0);
|
---|
| 541 | }
|
---|
[900] | 542 | mr_asprintf(&masklist, "/tmp/%d.%d.mask", (int) (random() % 32768),
|
---|
[127] | 543 | (int) (random() % 32768));
|
---|
[900] | 544 | mr_asprintf(&command, "cp -f %s %s", orig_msklist, masklist);
|
---|
[49] | 545 | run_program_and_log_output(command, 1);
|
---|
[900] | 546 | mr_free(command);
|
---|
[120] | 547 |
|
---|
[49] | 548 | sort_file(masklist);
|
---|
[900] | 549 | mr_asprintf(&syscall_pin, "gzip -dc %s", original_exat_fname);
|
---|
| 550 | mr_asprintf(&syscall_pout, "%s --restore - 2>> %s", executable,
|
---|
[127] | 551 | MONDO_LOGFILE);
|
---|
[1] | 552 |
|
---|
[49] | 553 | log_msg(1, "syscall_pin = %s", syscall_pin);
|
---|
| 554 | log_msg(1, "syscall_pout = %s", syscall_pout);
|
---|
| 555 | pout = popen(syscall_pout, "w");
|
---|
| 556 | if (!pout) {
|
---|
| 557 | iamhere("Unable to openout to syscall_pout");
|
---|
| 558 | return (1);
|
---|
| 559 | }
|
---|
[900] | 560 | mr_free(syscall_pout);
|
---|
[120] | 561 |
|
---|
[49] | 562 | pin = popen(syscall_pin, "r");
|
---|
| 563 | if (!pin) {
|
---|
| 564 | pclose(pout);
|
---|
| 565 | iamhere("Unable to openin from syscall");
|
---|
| 566 | return (1);
|
---|
| 567 | }
|
---|
[900] | 568 | mr_free(syscall_pin);
|
---|
[120] | 569 |
|
---|
[49] | 570 | faclin = fopen(masklist, "r");
|
---|
| 571 | if (!faclin) {
|
---|
| 572 | pclose(pin);
|
---|
| 573 | pclose(pout);
|
---|
| 574 | iamhere("Unable to openin masklist");
|
---|
| 575 | return (1);
|
---|
| 576 | }
|
---|
| 577 | // printf("Hi there. Starting the loop\n");
|
---|
[1] | 578 |
|
---|
[900] | 579 | mr_getline(¤t_subset_file, &n, faclin);
|
---|
| 580 | mr_getline(&incoming, &n, pin);
|
---|
[49] | 581 | while (!feof(pin) && !feof(faclin)) {
|
---|
[1] | 582 | // printf("incoming = %s", incoming);
|
---|
| 583 |
|
---|
[900] | 584 | mr_asprintf(¤t_master_file, incoming + 8);
|
---|
[1] | 585 |
|
---|
[49] | 586 | p = current_subset_file;
|
---|
| 587 | if (*p == '/') {
|
---|
| 588 | p++;
|
---|
| 589 | }
|
---|
| 590 | i = strlen(p);
|
---|
| 591 | if (i > 0 && p[i - 1] < 32) {
|
---|
| 592 | p[i - 1] = '\0';
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 |
|
---|
| 596 | q = current_master_file;
|
---|
| 597 | if (*q == '/') {
|
---|
| 598 | q++;
|
---|
| 599 | }
|
---|
| 600 | i = strlen(q);
|
---|
| 601 | if (i > 0 && q[i - 1] < 32) {
|
---|
| 602 | q[i - 1] = '\0';
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | i = strcmp(p, q);
|
---|
| 606 | log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
|
---|
| 607 |
|
---|
[1] | 608 | // printf("%s v %s --> %d\n", p, q, i);
|
---|
[49] | 609 |
|
---|
| 610 | if (i < 0) { // read another subset file in.
|
---|
| 611 | log_msg(my_depth, "Reading next subset line in\n\n");
|
---|
[900] | 612 | mr_getline(¤t_subset_file, &n, faclin);
|
---|
[49] | 613 | continue;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | if (!i) {
|
---|
| 617 | fputs(incoming, pout);
|
---|
| 618 | }
|
---|
[900] | 619 | mr_getline(&incoming, &n, pin);
|
---|
[49] | 620 | if (!i) {
|
---|
| 621 | log_msg(my_depth, "Copying master %s", q);
|
---|
| 622 | }
|
---|
| 623 | // if (!i) { printf("Match --- %s\n", q); }
|
---|
| 624 |
|
---|
| 625 | while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
|
---|
| 626 | if (!i) {
|
---|
| 627 |
|
---|
| 628 | // printf("%s", incoming);
|
---|
| 629 |
|
---|
| 630 | fputs(incoming, pout);
|
---|
| 631 | }
|
---|
[900] | 632 | mr_getline(&incoming, &n, pin);
|
---|
[49] | 633 | }
|
---|
| 634 | if (!i) {
|
---|
[900] | 635 | mr_getline(¤t_subset_file, &n, faclin);
|
---|
[49] | 636 | }
|
---|
[900] | 637 | mr_free(current_master_file);
|
---|
[1] | 638 | }
|
---|
[900] | 639 | mr_free(current_subset_file);
|
---|
[120] | 640 |
|
---|
[49] | 641 | while (!feof(pin)) {
|
---|
[900] | 642 | mr_getline(&incoming, &n, pin);
|
---|
[49] | 643 | }
|
---|
[900] | 644 | mr_free(incoming);
|
---|
[49] | 645 | fclose(faclin);
|
---|
| 646 | pclose(pin);
|
---|
| 647 | pclose(pout);
|
---|
[1] | 648 |
|
---|
| 649 | // printf("OK, loop is done\n");
|
---|
| 650 |
|
---|
[49] | 651 | unlink(masklist);
|
---|
[900] | 652 | mr_free(masklist);
|
---|
[120] | 653 |
|
---|
[49] | 654 | return (retval);
|
---|
| 655 | }
|
---|
[1] | 656 |
|
---|
| 657 |
|
---|
[49] | 658 | int set_fattr_list(char *masklist, char *fattr_fname)
|
---|
[1] | 659 | {
|
---|
[914] | 660 | if (find_home_of_exe("setfattr")) {
|
---|
| 661 | return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
|
---|
| 662 | } else {
|
---|
| 663 | log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
|
---|
| 664 | return(0);
|
---|
| 665 | }
|
---|
[1] | 666 | }
|
---|
| 667 |
|
---|
| 668 |
|
---|
| 669 |
|
---|
[49] | 670 | int set_acl_list(char *masklist, char *acl_fname)
|
---|
[1] | 671 | {
|
---|
[914] | 672 | if (find_home_of_exe("setfacl")) {
|
---|
| 673 | return (set_EXAT_list(masklist, acl_fname, "setfacl"));
|
---|
| 674 | } else {
|
---|
| 675 | log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
|
---|
| 676 | return(0);
|
---|
| 677 | }
|
---|
[1] | 678 | }
|
---|
| 679 |
|
---|
| 680 |
|
---|
| 681 | /**
|
---|
| 682 | * Get the number of the last fileset in the backup.
|
---|
| 683 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 684 | * @return The last filelist number.
|
---|
| 685 | * @note This function should only be called at restore-time.
|
---|
| 686 | */
|
---|
[49] | 687 | int get_last_filelist_number(struct s_bkpinfo *bkpinfo)
|
---|
[1] | 688 | {
|
---|
[49] | 689 | /*@ buffers ***************************************************** */
|
---|
[783] | 690 | char *val_sz = NULL;
|
---|
| 691 | char *cfg_fname = NULL;
|
---|
[1] | 692 |
|
---|
[49] | 693 | /*@ long ******************************************************** */
|
---|
[783] | 694 | int val_i = 0;
|
---|
[1] | 695 |
|
---|
[49] | 696 | /*@ end vars **************************************************** */
|
---|
[1] | 697 |
|
---|
[49] | 698 | assert(bkpinfo != NULL);
|
---|
[1] | 699 |
|
---|
[900] | 700 | mr_asprintf(&cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
|
---|
[49] | 701 | read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
|
---|
| 702 | val_i = atoi(val_sz);
|
---|
[900] | 703 | mr_free(val_sz);
|
---|
[783] | 704 |
|
---|
[49] | 705 | if (val_i <= 0) {
|
---|
| 706 | val_i = 500;
|
---|
| 707 | }
|
---|
[900] | 708 | mr_free(cfg_fname);
|
---|
[49] | 709 | return (val_i);
|
---|
[1] | 710 | }
|
---|
| 711 |
|
---|
| 712 |
|
---|
| 713 | /**
|
---|
| 714 | * Add a string at @p startnode.
|
---|
| 715 | * @param startnode The node to start at when searching for where to add the string.
|
---|
| 716 | * @param string_to_add The string to add.
|
---|
| 717 | * @return 0 for success, 1 for failure.
|
---|
| 718 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 719 | */
|
---|
[120] | 720 |
|
---|
[49] | 721 | int add_string_at_node(struct s_node *startnode, char *string_to_add)
|
---|
[1] | 722 | {
|
---|
| 723 |
|
---|
[49] | 724 | int noof_chars;
|
---|
| 725 | int i;
|
---|
| 726 | int res;
|
---|
[1] | 727 |
|
---|
[49] | 728 | struct s_node *node, *newnode;
|
---|
[1] | 729 |
|
---|
[49] | 730 | char char_to_add;
|
---|
[1] | 731 |
|
---|
[49] | 732 | const bool sosodef = FALSE;
|
---|
[1] | 733 |
|
---|
[49] | 734 | static int depth = 0;
|
---|
| 735 | static char original_string[MAX_STR_LEN];
|
---|
[1] | 736 |
|
---|
[49] | 737 | assert(startnode != NULL);
|
---|
| 738 | assert(string_to_add != NULL);
|
---|
[1] | 739 |
|
---|
[49] | 740 | if (!depth) {
|
---|
| 741 | strcpy(original_string, string_to_add);
|
---|
| 742 | }
|
---|
[1] | 743 |
|
---|
[120] | 744 | noof_chars = strlen(string_to_add) + 1; // we include the '\0'
|
---|
[1] | 745 |
|
---|
[120] | 746 | // walk across tree if necessary
|
---|
[49] | 747 | node = startnode;
|
---|
| 748 | char_to_add = string_to_add[0];
|
---|
| 749 | if (node->right != NULL && node->ch < char_to_add) {
|
---|
| 750 | log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
|
---|
| 751 | char_to_add, node->ch, (node->right)->ch);
|
---|
| 752 | return (add_string_at_node(node->right, string_to_add));
|
---|
| 753 | }
|
---|
[120] | 754 | // walk down tree if appropriate
|
---|
[49] | 755 | if (node->down != NULL && node->ch == char_to_add) {
|
---|
| 756 | log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
|
---|
| 757 | depth++;
|
---|
| 758 | res = add_string_at_node(node->down, string_to_add + 1);
|
---|
| 759 | depth--;
|
---|
| 760 | return (res);
|
---|
| 761 | }
|
---|
[1] | 762 |
|
---|
[49] | 763 | if (char_to_add == '\0' && node->ch == '\0') {
|
---|
| 764 | log_msg(6, "%s already in tree", original_string);
|
---|
| 765 | return (1);
|
---|
| 766 | }
|
---|
[120] | 767 | // add here
|
---|
[1081] | 768 | newnode = (struct s_node *) mr_malloc(sizeof(struct s_node));
|
---|
[49] | 769 | if (char_to_add < node->ch) // add to the left of node
|
---|
[1] | 770 | {
|
---|
[49] | 771 | log_msg(7, "depth=%d char=%c --- adding (left)", depth,
|
---|
| 772 | char_to_add);
|
---|
| 773 | memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
|
---|
| 774 | node->right = newnode;
|
---|
| 775 | } else if (char_to_add > node->ch) // add to the right of node
|
---|
| 776 | {
|
---|
| 777 | log_msg(7, "depth=%d char=%c --- adding (right)", depth,
|
---|
| 778 | char_to_add);
|
---|
| 779 | newnode->right = node->right; // newnode is to the RIGHT of node
|
---|
| 780 | node->right = newnode;
|
---|
| 781 | node = newnode;
|
---|
[1] | 782 | }
|
---|
[49] | 783 | // from now on, we're working on 'node'
|
---|
| 784 | node->down = NULL;
|
---|
| 785 | node->ch = char_to_add;
|
---|
| 786 | node->expanded = node->selected = FALSE;
|
---|
| 787 | if (char_to_add == '\0') {
|
---|
| 788 | log_msg(6, "Added %s OK", original_string);
|
---|
| 789 | return (0);
|
---|
| 790 | }
|
---|
[120] | 791 | // add the rest
|
---|
[49] | 792 | log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
|
---|
| 793 | for (i = 1; i < noof_chars; i++) {
|
---|
[1081] | 794 | node->down = (struct s_node *) mr_malloc(sizeof(struct s_node));
|
---|
[49] | 795 | }
|
---|
| 796 | node = node->down;
|
---|
| 797 | char_to_add = string_to_add[i];
|
---|
| 798 | log_msg(6, "Adding '%c'", char_to_add);
|
---|
| 799 | node->ch = char_to_add;
|
---|
| 800 | node->right = node->down = NULL;
|
---|
| 801 | node->expanded = node->selected = FALSE;
|
---|
| 802 | if (!node->ch) {
|
---|
| 803 | node->selected = sosodef;
|
---|
| 804 | }
|
---|
| 805 | }
|
---|
| 806 | log_msg(6, "Finally - added %s OK", original_string);
|
---|
| 807 | return (0);
|
---|
[1] | 808 | }
|
---|
| 809 |
|
---|
| 810 |
|
---|
| 811 | /**
|
---|
| 812 | * Load a filelist into a <tt>struct s_node</tt>.
|
---|
| 813 | * When you are done with the filelist, call free_filelist().
|
---|
| 814 | * @param filelist_fname The file to load the filelist from.
|
---|
| 815 | * @return A filelist tree structure.
|
---|
| 816 | */
|
---|
[49] | 817 | struct s_node *load_filelist(char *filelist_fname)
|
---|
[1] | 818 | {
|
---|
| 819 |
|
---|
[688] | 820 | struct s_node *filelist = NULL;
|
---|
| 821 | FILE *pin = NULL;
|
---|
| 822 | char *command_to_open_fname = NULL;
|
---|
[120] | 823 | char *fname = NULL;
|
---|
[688] | 824 | char *tmp = NULL;
|
---|
| 825 | char *tmp1 = NULL;
|
---|
| 826 | int pos_in_fname = 0;
|
---|
[171] | 827 | size_t n = 0;
|
---|
[688] | 828 | int percentage = 0;
|
---|
[1] | 829 |
|
---|
[688] | 830 | long lines_in_filelist = 0L;
|
---|
| 831 | long lino = 0L;
|
---|
[1] | 832 |
|
---|
[49] | 833 | assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
|
---|
[1] | 834 |
|
---|
[49] | 835 | if (!does_file_exist(filelist_fname)) {
|
---|
| 836 | fatal_error("filelist does not exist -- cannot load it");
|
---|
[1] | 837 | }
|
---|
[507] | 838 | log_to_screen(_("Loading filelist"));
|
---|
[900] | 839 | mr_asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
|
---|
| 840 | mr_asprintf(&tmp, "zcat %s | wc -l", filelist_fname);
|
---|
[49] | 841 | log_msg(6, "tmp = %s", tmp);
|
---|
[688] | 842 | tmp1 = call_program_and_get_last_line_of_output(tmp);
|
---|
[900] | 843 | mr_free(tmp);
|
---|
[120] | 844 |
|
---|
[688] | 845 | lines_in_filelist = atol(tmp1);
|
---|
[900] | 846 | mr_free(tmp1);
|
---|
[688] | 847 |
|
---|
[49] | 848 | if (lines_in_filelist < 3) {
|
---|
[507] | 849 | log_to_screen(_("Warning - surprisingly short filelist."));
|
---|
[1] | 850 | }
|
---|
[49] | 851 | g_original_noof_lines_in_filelist = lines_in_filelist;
|
---|
[1081] | 852 | filelist = (struct s_node *) mr_malloc(sizeof(struct s_node));
|
---|
[49] | 853 | filelist->ch = '/';
|
---|
| 854 | filelist->right = NULL;
|
---|
[1081] | 855 | filelist->down = mr_malloc(sizeof(struct s_node));
|
---|
[49] | 856 | filelist->expanded = filelist->selected = FALSE;
|
---|
| 857 | (filelist->down)->ch = '\0';
|
---|
| 858 | (filelist->down)->right = (filelist->down)->down = FALSE;
|
---|
| 859 | (filelist->down)->expanded = (filelist->down)->selected = FALSE;
|
---|
| 860 | if (!(pin = popen(command_to_open_fname, "r"))) {
|
---|
| 861 | log_OS_error("Unable to openin filelist_fname");
|
---|
| 862 | return (NULL);
|
---|
| 863 | }
|
---|
[900] | 864 | mr_free(command_to_open_fname);
|
---|
[120] | 865 |
|
---|
[507] | 866 | open_evalcall_form(_("Loading filelist from disk"));
|
---|
[900] | 867 | for (mr_getline(&fname, &n, pin); !feof(pin); mr_getline(&fname, &n, pin)) {
|
---|
[49] | 868 | if ((fname[strlen(fname) - 1] == 13
|
---|
| 869 | || fname[strlen(fname) - 1] == 10) && strlen(fname) > 0) {
|
---|
| 870 | fname[strlen(fname) - 1] = '\0';
|
---|
| 871 | }
|
---|
| 872 | if (!strlen(fname)) {
|
---|
| 873 | continue;
|
---|
| 874 | }
|
---|
| 875 | for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
|
---|
| 876 | if (fname[pos_in_fname] != '/') {
|
---|
| 877 | continue;
|
---|
| 878 | }
|
---|
[900] | 879 | mr_asprintf(&tmp, fname);
|
---|
[49] | 880 | tmp[pos_in_fname] = '\0';
|
---|
| 881 | if (strlen(tmp)) {
|
---|
| 882 | add_string_at_node(filelist, tmp);
|
---|
| 883 | }
|
---|
[900] | 884 | mr_free(tmp);
|
---|
[49] | 885 | }
|
---|
| 886 | add_string_at_node(filelist, fname);
|
---|
| 887 | if (!(++lino % 1111)) {
|
---|
| 888 | percentage = (int) (lino * 100 / lines_in_filelist);
|
---|
| 889 | update_evalcall_form(percentage);
|
---|
| 890 | }
|
---|
| 891 | }
|
---|
[900] | 892 | mr_free(fname);
|
---|
[49] | 893 | paranoid_pclose(pin);
|
---|
| 894 | close_evalcall_form();
|
---|
| 895 | log_it("Finished loading filelist");
|
---|
| 896 | return (filelist);
|
---|
[1] | 897 | }
|
---|
| 898 |
|
---|
| 899 |
|
---|
| 900 | /**
|
---|
| 901 | * Log a list of files in @p node.
|
---|
| 902 | * @param node The toplevel node to use.
|
---|
| 903 | */
|
---|
[49] | 904 | void show_filelist(struct s_node *node)
|
---|
[1] | 905 | {
|
---|
[49] | 906 | static int depth = 0;
|
---|
| 907 | static char current_string[200];
|
---|
[1] | 908 |
|
---|
[49] | 909 | if (depth == 0) {
|
---|
| 910 | log_msg(0, "----------------show filelist--------------");
|
---|
| 911 | }
|
---|
| 912 | current_string[depth] = node->ch;
|
---|
[1] | 913 |
|
---|
[49] | 914 | log_msg(3, "depth=%d", depth);
|
---|
| 915 | if (node->down) {
|
---|
| 916 | log_msg(3, "moving down");
|
---|
| 917 | depth++;
|
---|
| 918 | show_filelist(node->down);
|
---|
| 919 | depth--;
|
---|
| 920 | }
|
---|
[1] | 921 |
|
---|
[49] | 922 | if (!node->ch) {
|
---|
| 923 | log_msg(0, "%s\n", current_string);
|
---|
| 924 | }
|
---|
[1] | 925 |
|
---|
[49] | 926 | if (node->right) {
|
---|
| 927 | log_msg(3, "moving right");
|
---|
| 928 | show_filelist(node->right);
|
---|
| 929 | }
|
---|
| 930 | if (depth == 0) {
|
---|
| 931 | log_msg(0, "----------------show filelist--------------");
|
---|
| 932 | }
|
---|
| 933 | return;
|
---|
[1] | 934 | }
|
---|
| 935 |
|
---|
| 936 |
|
---|
| 937 | /**
|
---|
| 938 | * Reset the filelist to the state it was when it was loaded. This does not
|
---|
| 939 | * touch the file on disk.
|
---|
| 940 | * @param filelist The filelist tree structure.
|
---|
| 941 | */
|
---|
[49] | 942 | void reload_filelist(struct s_node *filelist)
|
---|
[1] | 943 | {
|
---|
[49] | 944 | assert(filelist != NULL);
|
---|
| 945 | toggle_node_selection(filelist, FALSE);
|
---|
| 946 | toggle_path_expandability(filelist, "/", FALSE);
|
---|
| 947 | toggle_all_root_dirs_on(filelist);
|
---|
[1] | 948 | }
|
---|
| 949 |
|
---|
| 950 |
|
---|
| 951 | /**
|
---|
| 952 | * Save a filelist tree structure to disk.
|
---|
| 953 | * @param filelist The filelist tree structure to save.
|
---|
| 954 | * @param outfname Where to save it.
|
---|
| 955 | */
|
---|
[49] | 956 | void save_filelist(struct s_node *filelist, char *outfname)
|
---|
[1] | 957 | {
|
---|
[49] | 958 | /*@ int ********************************************************* */
|
---|
| 959 | static int percentage;
|
---|
| 960 | static int depth = 0;
|
---|
[1] | 961 |
|
---|
[49] | 962 | /*@ buffers ***************************************************** */
|
---|
| 963 | static char str[MAX_STR_LEN];
|
---|
[1] | 964 |
|
---|
[49] | 965 | /*@ structures ************************************************** */
|
---|
| 966 | struct s_node *node;
|
---|
[1] | 967 |
|
---|
[49] | 968 | /*@ pointers **************************************************** */
|
---|
| 969 | static FILE *fout = NULL;
|
---|
[1] | 970 |
|
---|
[49] | 971 | /*@ long ******************************************************** */
|
---|
| 972 | static long lines_in_filelist = 0;
|
---|
| 973 | static long lino = 0;
|
---|
[1] | 974 |
|
---|
[49] | 975 | /*@ end vars *************************************************** */
|
---|
[1] | 976 |
|
---|
[49] | 977 | assert(filelist != NULL);
|
---|
| 978 | assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
|
---|
| 979 | if (depth == 0) {
|
---|
[507] | 980 | log_to_screen(_("Saving filelist"));
|
---|
[49] | 981 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 982 | fatal_error("Cannot openout/save filelist");
|
---|
| 983 | }
|
---|
| 984 | lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
|
---|
[507] | 985 | open_evalcall_form(_("Saving selection to disk"));
|
---|
[1] | 986 | }
|
---|
[49] | 987 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 988 | str[depth] = node->ch;
|
---|
| 989 | log_msg(5, "depth=%d ch=%c", depth, node->ch);
|
---|
| 990 | if (!node->ch) {
|
---|
| 991 | // if (node->selected)
|
---|
| 992 | // {
|
---|
| 993 | fprintf(fout, "%s\n", str);
|
---|
| 994 | // }
|
---|
| 995 | if (!(++lino % 1111)) {
|
---|
| 996 | percentage = (int) (lino * 100 / lines_in_filelist);
|
---|
| 997 | update_evalcall_form(percentage);
|
---|
| 998 | }
|
---|
| 999 | }
|
---|
| 1000 | if (node->down) {
|
---|
| 1001 | depth++;
|
---|
| 1002 | save_filelist(node->down, "");
|
---|
| 1003 | depth--;
|
---|
| 1004 | }
|
---|
[1] | 1005 | }
|
---|
[49] | 1006 | if (depth == 0) {
|
---|
| 1007 | paranoid_fclose(fout);
|
---|
| 1008 | close_evalcall_form();
|
---|
| 1009 | log_it("Finished saving filelist");
|
---|
| 1010 | }
|
---|
[1] | 1011 | }
|
---|
| 1012 |
|
---|
| 1013 |
|
---|
| 1014 | /**
|
---|
| 1015 | * Toggle all root dirs on.
|
---|
| 1016 | * @param filelist The filelist tree structure to operate on.
|
---|
| 1017 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1018 | */
|
---|
[49] | 1019 | void toggle_all_root_dirs_on(struct s_node *filelist)
|
---|
[1] | 1020 | {
|
---|
[49] | 1021 | /*@ structures ************************************************** */
|
---|
| 1022 | struct s_node *node;
|
---|
[1] | 1023 |
|
---|
[49] | 1024 | /*@ int ********************************************************* */
|
---|
| 1025 | static int depth = 0;
|
---|
| 1026 | static int root_dirs_expanded;
|
---|
[1] | 1027 |
|
---|
[49] | 1028 | /*@ buffers ***************************************************** */
|
---|
| 1029 | static char filename[MAX_STR_LEN];
|
---|
[1] | 1030 |
|
---|
[49] | 1031 | /*@ end vars *************************************************** */
|
---|
[1] | 1032 |
|
---|
[49] | 1033 | assert(filelist != NULL);
|
---|
| 1034 | if (depth == 0) {
|
---|
| 1035 | log_it("Toggling all root dirs ON");
|
---|
| 1036 | root_dirs_expanded = 0;
|
---|
[1] | 1037 | }
|
---|
[49] | 1038 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1039 | filename[depth] = node->ch;
|
---|
| 1040 | if (node->ch == '\0' && strlen(filename) > 1
|
---|
| 1041 | && (!strchr(filename + 1, '/'))) {
|
---|
| 1042 | node->selected = FALSE;
|
---|
| 1043 | node->expanded = TRUE;
|
---|
| 1044 | // log_it (filename);
|
---|
| 1045 | root_dirs_expanded++;
|
---|
| 1046 | }
|
---|
| 1047 | if (node->down) {
|
---|
| 1048 | depth++;
|
---|
| 1049 | toggle_all_root_dirs_on(node->down);
|
---|
| 1050 | depth--;
|
---|
| 1051 | }
|
---|
[1] | 1052 | }
|
---|
[49] | 1053 | if (depth == 0) {
|
---|
| 1054 | log_it("Finished toggling all root dirs ON");
|
---|
| 1055 | }
|
---|
[1] | 1056 | }
|
---|
| 1057 |
|
---|
| 1058 |
|
---|
| 1059 | /**
|
---|
| 1060 | * Toggle the expandability of a path.
|
---|
| 1061 | * @param filelist The filelist tree to operate on.
|
---|
| 1062 | * @param pathname The path to toggle expandability of.
|
---|
| 1063 | * @param on_or_off Whether to toggle it on or off.
|
---|
| 1064 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1065 | */
|
---|
| 1066 | void
|
---|
[49] | 1067 | toggle_path_expandability(struct s_node *filelist, char *pathname,
|
---|
| 1068 | bool on_or_off)
|
---|
[1] | 1069 | {
|
---|
| 1070 |
|
---|
[49] | 1071 | /*@ int ******************************************************** */
|
---|
| 1072 | static int depth = 0;
|
---|
| 1073 | static int total_expanded;
|
---|
| 1074 | static int root_depth;
|
---|
| 1075 | int j;
|
---|
| 1076 | /*@ structures ************************************************* */
|
---|
| 1077 | struct s_node *node;
|
---|
[1] | 1078 |
|
---|
[49] | 1079 | /*@ buffers **************************************************** */
|
---|
| 1080 | static char current_filename[MAX_STR_LEN];
|
---|
[1] | 1081 |
|
---|
[49] | 1082 | /*@ end vars *************************************************** */
|
---|
[1] | 1083 |
|
---|
[49] | 1084 | assert(filelist != NULL);
|
---|
| 1085 | assert_string_is_neither_NULL_nor_zerolength(pathname);
|
---|
| 1086 | if (depth == 0) {
|
---|
| 1087 | total_expanded = 0;
|
---|
[1] | 1088 | // log_it ("Toggling path's expandability");
|
---|
[49] | 1089 | for (root_depth = (int) strlen(pathname);
|
---|
| 1090 | root_depth > 0 && pathname[root_depth - 1] != '/';
|
---|
| 1091 | root_depth--);
|
---|
| 1092 | if (root_depth < 2) {
|
---|
| 1093 | root_depth = (int) strlen(pathname);
|
---|
| 1094 | }
|
---|
[1] | 1095 | }
|
---|
[49] | 1096 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1097 | current_filename[depth] = node->ch;
|
---|
| 1098 | if (node->down) {
|
---|
| 1099 | depth++;
|
---|
| 1100 | toggle_path_expandability(node->down, pathname, on_or_off);
|
---|
| 1101 | depth--;
|
---|
[1] | 1102 | }
|
---|
[49] | 1103 | if (node->ch == '\0') {
|
---|
| 1104 | if (!strncmp(pathname, current_filename, strlen(pathname))) {
|
---|
| 1105 | for (j = root_depth;
|
---|
| 1106 | current_filename[j] != '/'
|
---|
| 1107 | && current_filename[j] != '\0'; j++);
|
---|
| 1108 | if (current_filename[j] != '\0') {
|
---|
| 1109 | for (j++;
|
---|
| 1110 | current_filename[j] != '/'
|
---|
| 1111 | && current_filename[j] != '\0'; j++);
|
---|
| 1112 | }
|
---|
| 1113 | if (current_filename[j] == '\0') {
|
---|
| 1114 | node->expanded =
|
---|
| 1115 | (!strcmp(pathname, current_filename) ? TRUE :
|
---|
| 1116 | on_or_off);
|
---|
| 1117 | }
|
---|
| 1118 | }
|
---|
[1] | 1119 | }
|
---|
[49] | 1120 | if (node->expanded) {
|
---|
| 1121 | if (total_expanded < ARBITRARY_MAXIMUM - 32
|
---|
| 1122 | || !strrchr(current_filename + strlen(pathname), '/')) {
|
---|
| 1123 | total_expanded++;
|
---|
| 1124 | } else {
|
---|
| 1125 | node->expanded = FALSE;
|
---|
| 1126 | }
|
---|
| 1127 | }
|
---|
[1] | 1128 | }
|
---|
[49] | 1129 | if (depth == 0) {
|
---|
| 1130 | // log_it ("Finished toggling expandability");
|
---|
[1] | 1131 | }
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
[120] | 1134 |
|
---|
[1] | 1135 | /**
|
---|
| 1136 | * Toggle whether a path is selected.
|
---|
| 1137 | * @param filelist The filelist tree to operate on.
|
---|
| 1138 | * @param pathname The path to toggle selection of.
|
---|
| 1139 | * @param on_or_off Whether to toggle it on or off.
|
---|
| 1140 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1141 | */
|
---|
| 1142 | void
|
---|
[49] | 1143 | toggle_path_selection(struct s_node *filelist, char *pathname,
|
---|
| 1144 | bool on_or_off)
|
---|
[1] | 1145 | {
|
---|
[49] | 1146 | /*@ int ********************************************************* */
|
---|
| 1147 | static int depth = 0;
|
---|
| 1148 | int j;
|
---|
[1] | 1149 |
|
---|
[49] | 1150 | /*@ structures ************************************************** */
|
---|
| 1151 | struct s_node *node;
|
---|
[1] | 1152 |
|
---|
[49] | 1153 | /*@ buffers ***************************************************** */
|
---|
| 1154 | static char current_filename[MAX_STR_LEN];
|
---|
[1] | 1155 |
|
---|
[49] | 1156 | /*@ end vars *************************************************** */
|
---|
| 1157 | assert(filelist != NULL);
|
---|
| 1158 | assert_string_is_neither_NULL_nor_zerolength(pathname);
|
---|
| 1159 | if (depth == 0) {
|
---|
| 1160 | log_it("Toggling path's selection");
|
---|
[1] | 1161 | }
|
---|
[49] | 1162 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1163 | current_filename[depth] = node->ch;
|
---|
| 1164 | if (node->down) {
|
---|
| 1165 | depth++;
|
---|
| 1166 | toggle_path_selection(node->down, pathname, on_or_off);
|
---|
| 1167 | depth--;
|
---|
[1] | 1168 | }
|
---|
[49] | 1169 | if (node->ch == '\0') {
|
---|
| 1170 | if (!strncmp(pathname, current_filename, strlen(pathname))) {
|
---|
| 1171 | for (j = 0;
|
---|
| 1172 | pathname[j] != '\0'
|
---|
| 1173 | && pathname[j] == current_filename[j]; j++);
|
---|
| 1174 | if (current_filename[j] == '/'
|
---|
| 1175 | || current_filename[j] == '\0') {
|
---|
| 1176 | node->selected = on_or_off;
|
---|
| 1177 | }
|
---|
| 1178 | }
|
---|
| 1179 | }
|
---|
[1] | 1180 | }
|
---|
[49] | 1181 | if (depth == 0) {
|
---|
| 1182 | log_it("Finished toggling selection");
|
---|
| 1183 | }
|
---|
[1] | 1184 | }
|
---|
| 1185 |
|
---|
| 1186 |
|
---|
| 1187 | /**
|
---|
| 1188 | * Toggle node selection of a filelist tree.
|
---|
| 1189 | * @param filelist The filelist tree to operate on.
|
---|
| 1190 | * @param on_or_off Whether to toggle selection on or off.
|
---|
| 1191 | * @bug I don't understand this function. Would someone care to explain it?
|
---|
| 1192 | */
|
---|
[49] | 1193 | void toggle_node_selection(struct s_node *filelist, bool on_or_off)
|
---|
[1] | 1194 | {
|
---|
[49] | 1195 | /*@ structure ************************************************** */
|
---|
| 1196 | struct s_node *node;
|
---|
[1] | 1197 |
|
---|
[49] | 1198 | /*@ end vars *************************************************** */
|
---|
| 1199 | assert(filelist != NULL);
|
---|
| 1200 | for (node = filelist; node != NULL; node = node->right) {
|
---|
| 1201 | if (node->ch == '/') {
|
---|
| 1202 | continue;
|
---|
| 1203 | } /* don't go deep */
|
---|
| 1204 | if (node->ch == '\0') {
|
---|
| 1205 | node->selected = on_or_off;
|
---|
| 1206 | }
|
---|
| 1207 | if (node->down) {
|
---|
| 1208 | toggle_node_selection(node->down, on_or_off);
|
---|
| 1209 | }
|
---|
[1] | 1210 | }
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 |
|
---|
| 1214 | /**
|
---|
| 1215 | * Number of entries in the skeleton filelist.
|
---|
| 1216 | */
|
---|
[49] | 1217 | long g_skeleton_entries = 0;
|
---|
[1] | 1218 |
|
---|
[120] | 1219 |
|
---|
[1] | 1220 | /**
|
---|
| 1221 | * Wrapper around mondo_makefilelist().
|
---|
| 1222 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1223 | * - @c bkpinfo->differential
|
---|
| 1224 | * - @c bkpinfo->exclude_paths
|
---|
| 1225 | * - @c bkpinfo->include_paths
|
---|
| 1226 | * - @c bkpinfo->make_filelist
|
---|
| 1227 | * - @c bkpinfo->scratchdir
|
---|
| 1228 | * - @c bkpinfo->tmpdir
|
---|
| 1229 | * @return 0 for success, nonzero for failure.
|
---|
| 1230 | * @see mondo_makefilelist
|
---|
| 1231 | */
|
---|
[49] | 1232 | int prepare_filelist(struct s_bkpinfo *bkpinfo)
|
---|
[1] | 1233 | {
|
---|
| 1234 |
|
---|
[49] | 1235 | /*@ int **************************************************** */
|
---|
| 1236 | int res = 0;
|
---|
| 1237 | /*@ buffers ************************************************ */
|
---|
[1] | 1238 |
|
---|
[49] | 1239 | assert(bkpinfo != NULL);
|
---|
| 1240 | log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir,
|
---|
| 1241 | bkpinfo->scratchdir);
|
---|
| 1242 | if (bkpinfo->make_filelist) {
|
---|
| 1243 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
[507] | 1244 | _("Making catalog of files to be backed up"));
|
---|
[49] | 1245 | } else {
|
---|
| 1246 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
[507] | 1247 | _("Using supplied catalog of files to be backed up"));
|
---|
[49] | 1248 | }
|
---|
[1] | 1249 |
|
---|
[49] | 1250 | if (bkpinfo->make_filelist) {
|
---|
| 1251 | res =
|
---|
| 1252 | mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
|
---|
| 1253 | bkpinfo->scratchdir, bkpinfo->include_paths,
|
---|
| 1254 | bkpinfo->exclude_paths,
|
---|
| 1255 | bkpinfo->differential, NULL);
|
---|
| 1256 | } else {
|
---|
| 1257 | res =
|
---|
| 1258 | mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
|
---|
| 1259 | bkpinfo->scratchdir, NULL,
|
---|
| 1260 | bkpinfo->exclude_paths,
|
---|
| 1261 | bkpinfo->differential,
|
---|
| 1262 | bkpinfo->include_paths);
|
---|
| 1263 | }
|
---|
[1] | 1264 |
|
---|
[49] | 1265 | if (res) {
|
---|
[687] | 1266 | log_OS_error("Call to mondo_makefilelist failed");
|
---|
[507] | 1267 | mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
|
---|
[49] | 1268 | } else {
|
---|
[507] | 1269 | mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
|
---|
[49] | 1270 | }
|
---|
| 1271 | return (res);
|
---|
[1] | 1272 | }
|
---|
| 1273 |
|
---|
| 1274 |
|
---|
| 1275 | /**
|
---|
| 1276 | * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
|
---|
| 1277 | * @param dir The directory to list to @p fout.
|
---|
| 1278 | * @param sth The directories to skip (exclude).
|
---|
| 1279 | * @param fout The file to write everything to.
|
---|
| 1280 | * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
|
---|
| 1281 | * @return 0, always.
|
---|
| 1282 | * @bug Return value should be @c void.
|
---|
| 1283 | */
|
---|
[49] | 1284 | int open_and_list_dir(char *dir, char *sth, FILE * fout,
|
---|
[127] | 1285 | time_t time_of_last_full_backup,
|
---|
| 1286 | char *skeleton_filelist)
|
---|
[1] | 1287 | {
|
---|
[839] | 1288 | const char delims[] = " ";
|
---|
[688] | 1289 | DIR *dip = NULL;
|
---|
| 1290 | struct dirent *dit = NULL;
|
---|
[839] | 1291 |
|
---|
[49] | 1292 | struct stat statbuf;
|
---|
[688] | 1293 | char *new = NULL;
|
---|
| 1294 | char *tmp = NULL;
|
---|
| 1295 | char *tmp1 = NULL;
|
---|
[49] | 1296 | static int percentage = 0;
|
---|
[688] | 1297 | char *skip_these = NULL;
|
---|
| 1298 | char *new_with_spaces = NULL;
|
---|
| 1299 | static char *name_of_evalcall_form = NULL;
|
---|
| 1300 | int i = 0;
|
---|
[839] | 1301 | char *strtmp = NULL;
|
---|
| 1302 | char *token = NULL;
|
---|
| 1303 | char *find_excludes = NULL;
|
---|
| 1304 | int lastpos = 0;
|
---|
[49] | 1305 | static int depth = 0;
|
---|
[688] | 1306 | char *p = NULL;
|
---|
[49] | 1307 | static int counter = 0;
|
---|
| 1308 | static int uberctr = 0;
|
---|
[688] | 1309 | static char *find_skeleton_marker = NULL;
|
---|
[49] | 1310 | static long skeleton_lino = 0;
|
---|
| 1311 | static time_t last_time = 0;
|
---|
| 1312 | time_t this_time;
|
---|
[1] | 1313 |
|
---|
[49] | 1314 | p = strrchr(dir, '/');
|
---|
| 1315 | if (p) {
|
---|
| 1316 | if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
|
---|
| 1317 | return (0);
|
---|
| 1318 | }
|
---|
| 1319 | }
|
---|
| 1320 |
|
---|
| 1321 | if (!depth) {
|
---|
| 1322 | malloc_string(find_skeleton_marker);
|
---|
[900] | 1323 | mr_asprintf(&find_excludes, " ");
|
---|
[839] | 1324 | while((token = mr_strtok (sth, delims, &lastpos))) {
|
---|
[900] | 1325 | mr_asprintf(&strtmp,"%s", find_excludes);
|
---|
| 1326 | mr_free(find_excludes);
|
---|
| 1327 | mr_asprintf(&find_excludes,"%s -path %s -prune -o", strtmp, token);
|
---|
| 1328 | mr_free(strtmp);
|
---|
| 1329 | mr_free(token);
|
---|
[839] | 1330 | }
|
---|
[1] | 1331 | #if linux
|
---|
[49] | 1332 | // 2.6 has /sys as a proc-type thing -- must be excluded
|
---|
[900] | 1333 | mr_asprintf(&strtmp,
|
---|
[839] | 1334 | "find %s -maxdepth %d -fstype mvfs -prune -o -path /dev/shm -prune -o %s -type d -print > %s 2> /dev/null",
|
---|
[848] | 1335 | dir, MAX_SKEL_DEPTH, find_excludes, skeleton_filelist);
|
---|
[1] | 1336 | #else
|
---|
[49] | 1337 | // On BSD, for example, /sys is the kernel sources -- don't exclude
|
---|
[900] | 1338 | mr_asprintf(&strtmp,
|
---|
[839] | 1339 | "find %s -maxdepth %d -fstype mvfs -prune -o -path /proc -prune -o %s -type d -print > %s 2> /dev/null",
|
---|
[848] | 1340 | dir, MAX_SKEL_DEPTH, find_excludes, skeleton_filelist);
|
---|
[1] | 1341 | #endif
|
---|
[900] | 1342 | mr_free(find_excludes);
|
---|
[839] | 1343 | log_msg(5, "find command = %s", strtmp);
|
---|
| 1344 | system(strtmp);
|
---|
[900] | 1345 | mr_free(strtmp);
|
---|
| 1346 | mr_free(tmp);
|
---|
[120] | 1347 |
|
---|
[900] | 1348 | mr_asprintf(&tmp, "wc -l %s | awk '{print $1;}'", skeleton_filelist);
|
---|
[688] | 1349 | tmp1 = call_program_and_get_last_line_of_output(tmp);
|
---|
[900] | 1350 | mr_free(tmp);
|
---|
[120] | 1351 |
|
---|
[688] | 1352 | g_skeleton_entries = 1 + atol(tmp1);
|
---|
[900] | 1353 | mr_free(tmp1);
|
---|
[688] | 1354 |
|
---|
[900] | 1355 | mr_asprintf(&name_of_evalcall_form, "Making catalog of %s", dir);
|
---|
[49] | 1356 | open_evalcall_form(name_of_evalcall_form);
|
---|
| 1357 | find_skeleton_marker[0] = '\0';
|
---|
| 1358 | skeleton_lino = 1;
|
---|
| 1359 | log_msg(5, "entries = %ld", g_skeleton_entries);
|
---|
| 1360 | percentage = 0;
|
---|
| 1361 | } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
|
---|
| 1362 | {
|
---|
| 1363 | sprintf(find_skeleton_marker,
|
---|
[687] | 1364 | "grep -Fv \"%s\" %s > %s.new 2> /dev/null", dir,
|
---|
[120] | 1365 | skeleton_filelist, skeleton_filelist);
|
---|
[49] | 1366 | // log_msg(0, "fsm = %s", find_skeleton_marker);
|
---|
| 1367 | if (!system(find_skeleton_marker)) {
|
---|
| 1368 | percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
|
---|
| 1369 | skeleton_lino++;
|
---|
| 1370 | // log_msg(5, "Found %s", dir);
|
---|
| 1371 | // log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
|
---|
| 1372 | sprintf(find_skeleton_marker, "mv -f %s.new %s",
|
---|
[120] | 1373 | skeleton_filelist, skeleton_filelist);
|
---|
[49] | 1374 | // log_msg(6, "fsm = %s", find_skeleton_marker);
|
---|
| 1375 | run_program_and_log_output(find_skeleton_marker, 8);
|
---|
| 1376 | time(&this_time);
|
---|
| 1377 | if (this_time != last_time) {
|
---|
| 1378 | last_time = this_time;
|
---|
[1] | 1379 | #ifndef _XWIN
|
---|
[49] | 1380 | if (!g_text_mode) {
|
---|
[900] | 1381 | mr_asprintf(&tmp, _("Reading %-68s"), dir);
|
---|
[49] | 1382 | newtDrawRootText(0, g_noof_rows - 3, tmp);
|
---|
[900] | 1383 | mr_free(tmp);
|
---|
[49] | 1384 | }
|
---|
[1] | 1385 | #endif
|
---|
[49] | 1386 | update_evalcall_form(percentage);
|
---|
| 1387 | }
|
---|
[1] | 1388 | }
|
---|
[49] | 1389 | }
|
---|
[1] | 1390 |
|
---|
[49] | 1391 | depth++;
|
---|
[1] | 1392 |
|
---|
| 1393 | // log_msg(0, "Cataloguing %s", dir);
|
---|
[49] | 1394 | if (sth[0] == ' ') {
|
---|
[900] | 1395 | mr_asprintf(&skip_these, "%s", sth);
|
---|
[49] | 1396 | } else {
|
---|
[900] | 1397 | mr_asprintf(&skip_these, " %s ", sth);
|
---|
[49] | 1398 | }
|
---|
[900] | 1399 | mr_asprintf(&new_with_spaces, " %s ", dir);
|
---|
[49] | 1400 | if ((dip = opendir(dir)) == NULL) {
|
---|
| 1401 | log_OS_error("opendir");
|
---|
| 1402 | } else if (strstr(skip_these, new_with_spaces)) {
|
---|
| 1403 | fprintf(fout, "%s\n", dir); // if excluded dir then print dir ONLY
|
---|
| 1404 | } else {
|
---|
| 1405 | fprintf(fout, "%s\n", dir);
|
---|
| 1406 | while ((dit = readdir(dip)) != NULL) {
|
---|
| 1407 | i++;
|
---|
| 1408 | if (strcmp(dir, "/")) {
|
---|
[900] | 1409 | mr_asprintf(&new, "%s/%s", dir, dit->d_name);
|
---|
[120] | 1410 | } else {
|
---|
[900] | 1411 | mr_asprintf(&new, "%s%s", dir, dit->d_name);
|
---|
[49] | 1412 | }
|
---|
[900] | 1413 | mr_free(new_with_spaces);
|
---|
| 1414 | mr_asprintf(&new_with_spaces, " %s ", new);
|
---|
[120] | 1415 | /* BERLIOS: Old code
|
---|
[127] | 1416 | new_with_spaces[0] = ' ';
|
---|
[783] | 1417 | str-cpy(new_with_spaces + 1, new);
|
---|
| 1418 | str-cat(new_with_spaces, " ");
|
---|
[127] | 1419 | */
|
---|
[49] | 1420 | if (strstr(skip_these, new_with_spaces)) {
|
---|
| 1421 | fprintf(fout, "%s\n", new);
|
---|
| 1422 | } else {
|
---|
| 1423 | if (!lstat(new, &statbuf)) {
|
---|
| 1424 | if (!S_ISLNK(statbuf.st_mode)
|
---|
| 1425 | && S_ISDIR(statbuf.st_mode)) {
|
---|
| 1426 | open_and_list_dir(new, skip_these, fout,
|
---|
[127] | 1427 | time_of_last_full_backup,
|
---|
| 1428 | skeleton_filelist);
|
---|
[49] | 1429 | } else {
|
---|
| 1430 | if (time_of_last_full_backup == 0
|
---|
| 1431 | || time_of_last_full_backup <
|
---|
| 1432 | statbuf.st_ctime) {
|
---|
| 1433 | fprintf(fout, "%s\n", new);
|
---|
| 1434 | if ((counter++) > 128) {
|
---|
| 1435 | counter = 0;
|
---|
| 1436 | uberctr++;
|
---|
[900] | 1437 | mr_asprintf(&tmp, " %c ",
|
---|
[127] | 1438 | special_dot_char(uberctr));
|
---|
[1] | 1439 | #ifndef _XWIN
|
---|
[49] | 1440 | if (!g_text_mode) {
|
---|
| 1441 | newtDrawRootText(77, g_noof_rows - 3,
|
---|
| 1442 | tmp);
|
---|
| 1443 | newtRefresh();
|
---|
| 1444 | }
|
---|
[900] | 1445 | mr_free(tmp);
|
---|
[1] | 1446 | #endif
|
---|
[49] | 1447 | }
|
---|
| 1448 | }
|
---|
| 1449 | }
|
---|
| 1450 | }
|
---|
| 1451 | }
|
---|
[900] | 1452 | mr_free(new);
|
---|
[1] | 1453 | }
|
---|
[49] | 1454 | }
|
---|
[900] | 1455 | mr_free(new_with_spaces);
|
---|
| 1456 | mr_free(skip_these);
|
---|
[120] | 1457 |
|
---|
[49] | 1458 | if (dip) {
|
---|
| 1459 | if (closedir(dip) == -1) {
|
---|
| 1460 | log_OS_error("closedir");
|
---|
| 1461 | }
|
---|
| 1462 | }
|
---|
| 1463 | depth--;
|
---|
| 1464 | if (!depth) {
|
---|
| 1465 | close_evalcall_form();
|
---|
[900] | 1466 | mr_free(name_of_evalcall_form);
|
---|
| 1467 | mr_free(find_skeleton_marker);
|
---|
[120] | 1468 | unlink(skeleton_filelist);
|
---|
[49] | 1469 | log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
|
---|
| 1470 | }
|
---|
| 1471 | return (0);
|
---|
[1] | 1472 | }
|
---|
| 1473 |
|
---|
| 1474 |
|
---|
| 1475 | /**
|
---|
| 1476 | * Get the next entry in the space-separated list in @p incoming.
|
---|
| 1477 | * So if @p incoming was '"one and two" three four', we would
|
---|
| 1478 | * return "one and two".
|
---|
| 1479 | * @param incoming The list to get the next entry from.
|
---|
| 1480 | * @return The first item in the list (respecting double quotes).
|
---|
| 1481 | * @note The returned string points to static data that will be overwritten with each call.
|
---|
| 1482 | */
|
---|
[49] | 1483 | char *next_entry(char *incoming)
|
---|
[1] | 1484 | {
|
---|
[49] | 1485 | static char sz_res[MAX_STR_LEN];
|
---|
[783] | 1486 | char *p = NULL;
|
---|
[49] | 1487 | bool in_quotes = FALSE;
|
---|
[1] | 1488 |
|
---|
[49] | 1489 | strcpy(sz_res, incoming);
|
---|
| 1490 | p = sz_res;
|
---|
| 1491 | while ((*p != ' ' || in_quotes) && *p != '\0') {
|
---|
| 1492 | if (*p == '\"') {
|
---|
| 1493 | in_quotes = !in_quotes;
|
---|
| 1494 | }
|
---|
| 1495 | p++;
|
---|
| 1496 | }
|
---|
| 1497 | *p = '\0';
|
---|
| 1498 | return (sz_res);
|
---|
[1] | 1499 | }
|
---|
| 1500 |
|
---|
| 1501 |
|
---|
| 1502 | /**
|
---|
| 1503 | * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
|
---|
| 1504 | * @param logfile Unused.
|
---|
| 1505 | * @param tmpdir The tmpdir of the backup.
|
---|
| 1506 | * @param scratchdir The scratchdir of the backup.
|
---|
| 1507 | * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
|
---|
| 1508 | * @param excp The paths to NOT back up.
|
---|
| 1509 | * @param differential The differential level (currently only 0 and 1 are supported).
|
---|
| 1510 | * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
|
---|
| 1511 | * @return 0, always.
|
---|
| 1512 | * @bug @p logfile is unused.
|
---|
| 1513 | * @bug Return value is meaningless.
|
---|
| 1514 | */
|
---|
[49] | 1515 | int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
|
---|
| 1516 | char *include_paths, char *excp, int differential,
|
---|
| 1517 | char *userdef_filelist)
|
---|
[1] | 1518 | {
|
---|
[539] | 1519 | char sz_datefile_wildcard[] = "/var/cache/mondo/difflevel.%d";
|
---|
[688] | 1520 | char *p = NULL;
|
---|
| 1521 | char *q = NULL;
|
---|
| 1522 | char *tmp = NULL;
|
---|
| 1523 | char *tmp1 = NULL;
|
---|
| 1524 | char *tmp2 = NULL;
|
---|
[120] | 1525 | char *sz_datefile;
|
---|
[688] | 1526 | char *sz_filelist = NULL;
|
---|
| 1527 | char *exclude_paths = NULL;
|
---|
| 1528 | int i = 0;
|
---|
| 1529 | FILE *fout = NULL;
|
---|
| 1530 | char *command = NULL;
|
---|
[49] | 1531 | time_t time_of_last_full_backup = 0;
|
---|
[1] | 1532 | struct stat statbuf;
|
---|
[688] | 1533 | char *skeleton_filelist = NULL;
|
---|
| 1534 |
|
---|
[127] | 1535 | // The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
|
---|
[900] | 1536 | mr_asprintf(&sz_datefile, sz_datefile_wildcard, 0);
|
---|
[49] | 1537 | if (!include_paths && !userdef_filelist) {
|
---|
| 1538 | fatal_error
|
---|
| 1539 | ("Please supply either include_paths or userdef_filelist");
|
---|
| 1540 | }
|
---|
[1] | 1541 | // make hole for filelist
|
---|
[900] | 1542 | mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
|
---|
[49] | 1543 | paranoid_system(command);
|
---|
[900] | 1544 | mr_free(command);
|
---|
[120] | 1545 |
|
---|
[900] | 1546 | mr_asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
|
---|
[49] | 1547 | make_hole_for_file(sz_filelist);
|
---|
[1] | 1548 |
|
---|
[49] | 1549 | if (differential == 0) {
|
---|
| 1550 | // restore last good datefile if it exists
|
---|
[900] | 1551 | mr_asprintf(&command, "cp -f %s.aborted %s", sz_datefile,
|
---|
[127] | 1552 | sz_datefile);
|
---|
[49] | 1553 | run_program_and_log_output(command, 3);
|
---|
[900] | 1554 | mr_free(command);
|
---|
[120] | 1555 |
|
---|
[49] | 1556 | // backup last known good datefile just in case :)
|
---|
| 1557 | if (does_file_exist(sz_datefile)) {
|
---|
[900] | 1558 | mr_asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
|
---|
[127] | 1559 | sz_datefile);
|
---|
[49] | 1560 | paranoid_system(command);
|
---|
[900] | 1561 | mr_free(command);
|
---|
[49] | 1562 | }
|
---|
| 1563 | make_hole_for_file(sz_datefile);
|
---|
[688] | 1564 | tmp = call_program_and_get_last_line_of_output("date +%s");
|
---|
| 1565 | write_one_liner_data_file(sz_datefile, tmp);
|
---|
[900] | 1566 | mr_free(tmp);
|
---|
[49] | 1567 | } else if (lstat(sz_datefile, &statbuf)) {
|
---|
| 1568 | log_msg(2,
|
---|
| 1569 | "Warning - unable to find date of previous backup. Full backup instead.");
|
---|
| 1570 | differential = 0;
|
---|
| 1571 | time_of_last_full_backup = 0;
|
---|
| 1572 | } else {
|
---|
| 1573 | time_of_last_full_backup = statbuf.st_mtime;
|
---|
| 1574 | log_msg(2, "Differential backup. Yay.");
|
---|
| 1575 | }
|
---|
[900] | 1576 | mr_free(sz_datefile);
|
---|
[1] | 1577 |
|
---|
| 1578 | // use user-specified filelist (if specified)
|
---|
[49] | 1579 | if (userdef_filelist) {
|
---|
| 1580 | log_msg(1,
|
---|
| 1581 | "Using the user-specified filelist - %s - instead of calculating one",
|
---|
| 1582 | userdef_filelist);
|
---|
[900] | 1583 | mr_asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
|
---|
[49] | 1584 | if (run_program_and_log_output(command, 3)) {
|
---|
| 1585 | fatal_error("Failed to copy user-specified filelist");
|
---|
| 1586 | }
|
---|
[900] | 1587 | mr_free(command);
|
---|
[49] | 1588 | } else {
|
---|
| 1589 | log_msg(2, "include_paths = '%s'", include_paths);
|
---|
| 1590 | log_msg(1, "Calculating filelist");
|
---|
[688] | 1591 | tmp = call_program_and_get_last_line_of_output("locate /win386.swp 2> /dev/null");
|
---|
| 1592 | tmp1 = call_program_and_get_last_line_of_output("locate /hiberfil.sys 2> /dev/null");
|
---|
| 1593 | tmp2 = call_program_and_get_last_line_of_output("locate /pagefile.sys 2> /dev/null");
|
---|
[900] | 1594 | mr_asprintf(&exclude_paths, " %s %s %s %s %s %s . .. \
|
---|
[1046] | 1595 | " MNT_CDROM " " MNT_FLOPPY " /media \
|
---|
[688] | 1596 | /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] | 1597 | mr_free(tmp);
|
---|
| 1598 | mr_free(tmp1);
|
---|
| 1599 | mr_free(tmp2);
|
---|
[1] | 1600 |
|
---|
[49] | 1601 | log_msg(2, "Excluding paths = '%s'", exclude_paths);
|
---|
| 1602 | log_msg(2,
|
---|
| 1603 | "Generating skeleton filelist so that we can track our progress");
|
---|
[900] | 1604 | mr_asprintf(&skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
|
---|
[120] | 1605 | make_hole_for_file(skeleton_filelist);
|
---|
| 1606 |
|
---|
[49] | 1607 | log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
|
---|
| 1608 | log_msg(2, "Opening out filelist to %s", sz_filelist);
|
---|
| 1609 | if (!(fout = fopen(sz_filelist, "w"))) {
|
---|
| 1610 | fatal_error("Cannot openout to sz_filelist");
|
---|
[1] | 1611 | }
|
---|
[49] | 1612 | i = 0;
|
---|
| 1613 | if (strlen(include_paths) == 0) {
|
---|
| 1614 | log_msg(1, "Including only '/' in %s", sz_filelist);
|
---|
| 1615 | open_and_list_dir("/", exclude_paths, fout,
|
---|
[120] | 1616 | time_of_last_full_backup, skeleton_filelist);
|
---|
[49] | 1617 | } else {
|
---|
| 1618 | p = include_paths;
|
---|
| 1619 | while (*p) {
|
---|
| 1620 | q = next_entry(p);
|
---|
| 1621 | log_msg(1, "Including %s in filelist %s", q, sz_filelist);
|
---|
| 1622 | open_and_list_dir(q, exclude_paths, fout,
|
---|
[127] | 1623 | time_of_last_full_backup,
|
---|
| 1624 | skeleton_filelist);
|
---|
[49] | 1625 | p += strlen(q);
|
---|
| 1626 | while (*p == ' ') {
|
---|
| 1627 | p++;
|
---|
| 1628 | }
|
---|
[1] | 1629 | }
|
---|
| 1630 | }
|
---|
[49] | 1631 | paranoid_fclose(fout);
|
---|
[900] | 1632 | mr_free(skeleton_filelist);
|
---|
| 1633 | mr_free(exclude_paths);
|
---|
[49] | 1634 | }
|
---|
| 1635 | log_msg(2, "Copying new filelist to scratchdir");
|
---|
[900] | 1636 | mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
|
---|
[49] | 1637 | paranoid_system(command);
|
---|
[900] | 1638 | mr_free(command);
|
---|
[120] | 1639 |
|
---|
[900] | 1640 | mr_asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
|
---|
[49] | 1641 | paranoid_system(command);
|
---|
[900] | 1642 | mr_free(command);
|
---|
[120] | 1643 |
|
---|
[900] | 1644 | mr_asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
|
---|
[49] | 1645 | paranoid_system(command);
|
---|
[900] | 1646 | mr_free(command);
|
---|
| 1647 | mr_free(sz_filelist);
|
---|
[120] | 1648 |
|
---|
[49] | 1649 | log_msg(2, "Exiting");
|
---|
| 1650 | return (0);
|
---|
[1] | 1651 | }
|
---|
| 1652 |
|
---|
| 1653 |
|
---|
| 1654 | /**
|
---|
| 1655 | * Locate the string @p string_to_find in the tree rooted at @p startnode.
|
---|
| 1656 | * @param startnode The node containing the root of the directory tree.
|
---|
| 1657 | * @param string_to_find The string to look for at @p startnode.
|
---|
| 1658 | * @return The node containing the last element of @p string_to_find, or NULL if
|
---|
| 1659 | * it was not found.
|
---|
| 1660 | */
|
---|
[49] | 1661 | struct s_node *find_string_at_node(struct s_node *startnode,
|
---|
| 1662 | char *string_to_find)
|
---|
[1] | 1663 | {
|
---|
[49] | 1664 | /*@ int ******************************************************** */
|
---|
| 1665 | int noof_chars;
|
---|
| 1666 | static int depth = 0;
|
---|
| 1667 | static char original_string[MAX_STR_LEN];
|
---|
[1] | 1668 |
|
---|
[49] | 1669 | /*@ sturctures ************************************************* */
|
---|
| 1670 | struct s_node *node;
|
---|
[1] | 1671 |
|
---|
[49] | 1672 | /*@ char ****************************************************** */
|
---|
| 1673 | char char_to_find;
|
---|
[1] | 1674 |
|
---|
[49] | 1675 | /*@ bools ****************************************************** */
|
---|
[1] | 1676 |
|
---|
[49] | 1677 | if (!depth) {
|
---|
| 1678 | strcpy(original_string, string_to_find);
|
---|
| 1679 | }
|
---|
[1] | 1680 |
|
---|
[49] | 1681 | assert(startnode != NULL);
|
---|
| 1682 | assert(string_to_find != NULL);
|
---|
[1] | 1683 |
|
---|
[49] | 1684 | noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
|
---|
[1] | 1685 |
|
---|
[49] | 1686 | log_msg(7, "starting --- str=%s", string_to_find);
|
---|
[1] | 1687 |
|
---|
| 1688 | /* walk across tree if necessary */
|
---|
[49] | 1689 | node = startnode;
|
---|
| 1690 | char_to_find = string_to_find[0];
|
---|
| 1691 | if (node->right != NULL && node->ch < char_to_find) {
|
---|
| 1692 | log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
|
---|
| 1693 | char_to_find, node->ch, (node->right)->ch);
|
---|
| 1694 | return (find_string_at_node(node->right, string_to_find));
|
---|
| 1695 | }
|
---|
[1] | 1696 |
|
---|
| 1697 | /* walk down tree if appropriate */
|
---|
[49] | 1698 | if (node->down != NULL && node->ch == char_to_find) {
|
---|
| 1699 | log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
|
---|
| 1700 | depth++;
|
---|
| 1701 | node = find_string_at_node(node->down, string_to_find + 1);
|
---|
| 1702 | depth--;
|
---|
| 1703 | return (node);
|
---|
| 1704 | }
|
---|
[1] | 1705 |
|
---|
[49] | 1706 | if (char_to_find == '\0' && node->ch == '\0') {
|
---|
| 1707 | log_msg(7, "%s is in tree", original_string);
|
---|
| 1708 | return (node);
|
---|
| 1709 | } else {
|
---|
| 1710 | log_msg(7, "%s is NOT in tree", original_string);
|
---|
| 1711 | return (NULL);
|
---|
| 1712 | }
|
---|
[1] | 1713 | }
|
---|
| 1714 |
|
---|
| 1715 |
|
---|
| 1716 | /**
|
---|
| 1717 | * Write all entries in @p needles_list_fname which are also in
|
---|
| 1718 | * @p filelist to @p matches_list_fname.
|
---|
| 1719 | * @param needles_list_fname A file containing strings to look for, 1 per line.
|
---|
| 1720 | * @param filelist The node for the root of the directory structure to search in.
|
---|
| 1721 | * @param matches_list_fname The filename where we should put the matches.
|
---|
| 1722 | * @return The number of matches found.
|
---|
| 1723 | */
|
---|
[49] | 1724 | long save_filelist_entries_in_common(char *needles_list_fname,
|
---|
| 1725 | struct s_node *filelist,
|
---|
| 1726 | char *matches_list_fname,
|
---|
| 1727 | bool use_star)
|
---|
[1] | 1728 | {
|
---|
[49] | 1729 | int retval = 0;
|
---|
| 1730 | struct s_node *found_node;
|
---|
| 1731 | FILE *fin;
|
---|
| 1732 | FILE *fout;
|
---|
| 1733 | char *fname = NULL;
|
---|
| 1734 | char *tmp;
|
---|
| 1735 | size_t len = 0; // Scrub's patch doesn't work without that
|
---|
[1] | 1736 |
|
---|
| 1737 | // log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
|
---|
[49] | 1738 | log_msg(5, "starting");
|
---|
| 1739 | log_msg(5, "needles_list_fname = %s", needles_list_fname);
|
---|
| 1740 | log_msg(5, "matches_list_fname = %s", matches_list_fname);
|
---|
| 1741 | if (!(fin = fopen(needles_list_fname, "r"))) {
|
---|
| 1742 | fatal_error("Cannot openin needles_list_fname");
|
---|
| 1743 | }
|
---|
| 1744 | if (!(fout = fopen(matches_list_fname, "w"))) {
|
---|
| 1745 | fatal_error("Cannot openout matches_list_fname");
|
---|
| 1746 | }
|
---|
| 1747 | while (!feof(fin)) {
|
---|
[900] | 1748 | mr_getline(&fname, &len, fin);
|
---|
[49] | 1749 | if (!use_star) {
|
---|
| 1750 | if (fname[0] == '/') {
|
---|
[900] | 1751 | mr_asprintf(&tmp, fname);
|
---|
[49] | 1752 | } else {
|
---|
[900] | 1753 | mr_asprintf(&tmp, "/%s", fname);
|
---|
[49] | 1754 | }
|
---|
[900] | 1755 | mr_free(fname);
|
---|
[120] | 1756 | fname = tmp;
|
---|
[49] | 1757 | }
|
---|
| 1758 | while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
|
---|
| 1759 | fname[strlen(fname) - 1] = '\0';
|
---|
| 1760 | }
|
---|
[1] | 1761 |
|
---|
[49] | 1762 | log_msg(5, "Looking for '%s'", fname);
|
---|
| 1763 | found_node = find_string_at_node(filelist, fname);
|
---|
| 1764 | if (found_node) {
|
---|
| 1765 | if (found_node->selected) {
|
---|
| 1766 | if (fname[0] == '/') {
|
---|
[900] | 1767 | mr_asprintf(&tmp, fname + 1);
|
---|
| 1768 | mr_free(fname);
|
---|
[120] | 1769 | fname = tmp;
|
---|
[49] | 1770 | }
|
---|
| 1771 | log_msg(5, "Found '%s'", fname);
|
---|
| 1772 | turn_wildcard_chars_into_literal_chars(tmp, fname);
|
---|
| 1773 | fprintf(fout, "%s\n", tmp);
|
---|
[900] | 1774 | mr_free(tmp);
|
---|
[49] | 1775 | retval++;
|
---|
| 1776 | }
|
---|
[1] | 1777 | }
|
---|
[900] | 1778 | mr_free(fname);
|
---|
[1] | 1779 | }
|
---|
[49] | 1780 | paranoid_fclose(fout);
|
---|
| 1781 | paranoid_fclose(fin);
|
---|
| 1782 | return (retval);
|
---|
[1] | 1783 | }
|
---|
| 1784 |
|
---|
| 1785 |
|
---|
| 1786 | /**
|
---|
| 1787 | * Add all files listed in @p list_of_files_fname to the directory structure rooted at
|
---|
| 1788 | * @p filelist.
|
---|
| 1789 | * @param filelist The top node of the directory structure to add the files to.
|
---|
| 1790 | * @param list_of_files_fname The file containing the files to add, 1 per line.
|
---|
| 1791 | * @param flag_em If TRUE, then flag the added files for restoration.
|
---|
| 1792 | * @return 0 for success, nonzero for failure.
|
---|
| 1793 | */
|
---|
[49] | 1794 | int add_list_of_files_to_filelist(struct s_node *filelist,
|
---|
| 1795 | char *list_of_files_fname, bool flag_em)
|
---|
[1] | 1796 | {
|
---|
[49] | 1797 | FILE *fin;
|
---|
[120] | 1798 | char *tmp = NULL;
|
---|
[171] | 1799 | size_t n = 0;
|
---|
[49] | 1800 | struct s_node *nod;
|
---|
[1] | 1801 |
|
---|
[49] | 1802 | log_msg(3, "Adding %s to filelist", list_of_files_fname);
|
---|
| 1803 | if (!(fin = fopen(list_of_files_fname, "r"))) {
|
---|
| 1804 | iamhere(list_of_files_fname);
|
---|
| 1805 | return (1);
|
---|
| 1806 | }
|
---|
[900] | 1807 | for (mr_getline(&tmp, &n, fin); !feof(fin); mr_getline(&tmp, &n, fin)) {
|
---|
[49] | 1808 | if (!tmp[0]) {
|
---|
| 1809 | continue;
|
---|
| 1810 | }
|
---|
| 1811 | if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
|
---|
| 1812 | && strlen(tmp) > 0) {
|
---|
| 1813 | tmp[strlen(tmp) - 1] = '\0';
|
---|
| 1814 | }
|
---|
| 1815 | log_msg(2, "tmp = '%s'", tmp);
|
---|
| 1816 | if (!tmp[0]) {
|
---|
| 1817 | continue;
|
---|
| 1818 | }
|
---|
| 1819 | if ((nod = find_string_at_node(filelist, tmp))) {
|
---|
| 1820 | log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
|
---|
| 1821 | } else {
|
---|
| 1822 | add_string_at_node(filelist, tmp);
|
---|
| 1823 | nod = find_string_at_node(filelist, tmp);
|
---|
| 1824 | }
|
---|
[1] | 1825 |
|
---|
[49] | 1826 | if (nod && flag_em) {
|
---|
| 1827 | toggle_path_selection(filelist, tmp, TRUE);
|
---|
| 1828 | log_msg(5, "Flagged '%s'", tmp);
|
---|
| 1829 | }
|
---|
[1] | 1830 | }
|
---|
[49] | 1831 | paranoid_fclose(fin);
|
---|
[900] | 1832 | mr_free(tmp);
|
---|
[49] | 1833 | return (0);
|
---|
[1] | 1834 | }
|
---|
| 1835 |
|
---|
| 1836 | /* @} - end of filelistGroup */
|
---|