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