source: MondoRescue/branches/3.2/mondo/src/common/libmondo-filelist.c@ 3373

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