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

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

Update trunk with the latest feedback from stable (obtained with meld) - First pass

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