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