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

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