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

Last change on this file since 914 was 914, checked in by Bruno Cornec, 17 years ago

merge -r902:913 $SVN_M/branches/stable

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