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

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

Fix bugs reported in #179

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