source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-filelist.c@ 2521

Last change on this file since 2521 was 2521, checked in by Bruno Cornec, 14 years ago

r3231@localhost (orig r2265): bruno | 2009-07-12 02:04:40 +0200

r3206@localhost: bruno | 2009-07-06 23:29:05 +0200

  • Replace sprintf by mr_asprintf in libmondo-filelist.c


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