source: MondoRescue/branches/3.2/mondo/src/common/libmondo-filelist.c@ 3191

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