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

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

Avoids divide by zero (idea of M. Loiseleur mloiseleur_at_linagora.com)

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