source: MondoRescue/branches/stable/mondo/src/common/libmondo-filelist.c@ 1326

Last change on this file since 1326 was 1326, checked in by Bruno Cornec, 17 years ago
  • MONDO_LOGFILE is rather a char* exported by each main program (consolidation of those mecanisms in .h files with ps_*)
  • One include has been created for each binary containing only the specific declarations
  • Log files are now consistent: mondoarchive.log for mondoarchive (containing also mindi.log) and mondorestore.log for mondorestore (copied from /tmp (ram) to /var/log (disk) at the end of the restore)
  • Doc updated accordingly
  • LOGFILE in restore process is now passed in the environment and not duplicated a nymore
  • LogIt is not redifined either
  • LOGFILE should be put in environment by mondoarchive for mindi's usage but that's a step left for later.
  • label-partitions-as-necessary should now work correctly for LABEL and UUID (grep -w removed)
  • Remove useless script compare-me

(All coming from 2.2.2) (remains changes in my-stuff.h coming later on)

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