source: MondoRescue/trunk/mondo/mondo/common/libmondo-filelist.c@ 507

Last change on this file since 507 was 507, checked in by bcornec, 18 years ago

merge -r489:506 $SVN_M/branches/stable

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