source: MondoRescue/branches/3.0/mondo/src/common/libmondo-filelist.c@ 3139

Last change on this file since 3139 was 3139, checked in by Bruno Cornec, 12 years ago

r5341@localhost: bruno | 2013-06-13 03:02:29 +0200

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