source: MondoRescue/branches/2.2.2/mondo/src/common/libmondo-filelist.c@ 1260

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