source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-filelist.c@ 2340

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