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

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

merge -r938:954 $SVN_M/branches/stable

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