source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-filelist.c@ 2404

Last change on this file since 2404 was 2404, checked in by Bruno Cornec, 15 years ago
  • At restore time read the netfs protocol in the conf file
  • Use dir for shell related commands and dir1 for C related actions in open_and_list_dir to avoid missing some files with { in names e.g.

(Backports from 2.2.9)

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