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

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

Fix a compiler error on mr_free

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