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