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