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

Last change on this file since 3194 was 3194, checked in by Bruno Cornec, 11 years ago
  • Fix all compilation/link error due to the previous giant merge with 3.1. 3.2 is ready to test
  • Property svn:keywords set to Id
File size: 49.6 KB
RevLine 
[2030]1/*
[128]2 $Id: libmondo-filelist.c 3194 2013-09-29 17:27:11Z 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 3194 2013-09-29 17:27:11Z 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;
[1552]441 char *tmp = NULL;
[128]442 int i;
443
444 if (!(fin = fopen(filelist, "r"))) {
445 log_msg(1, "Cannot openin filelist %s", filelist);
446 return (1);
447 }
[3185]448 mr_asprintf(pout_command, "gzip -c1 > %s", auxlist_fname);
[128]449 if (!(pout = popen(pout_command, "w"))) {
450 log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
451 fclose(fin);
[2521]452 mr_free(pout_command);
[128]453 return (4);
454 }
[2521]455 mr_free(pout_command);
456
[3191]457 for (mr_getline(file_to_analyze, fin); !feof(fin); mr_getline(file_to_analyze, fin)) {
[128]458 i = strlen(file_to_analyze);
459 if (i > 0 && file_to_analyze[i - 1] < 32) {
460 file_to_analyze[i - 1] = '\0';
461 }
462 log_msg(8, "Analyzing %s", file_to_analyze);
[3171]463 tmp = mr_stresc(file_to_analyze, "'", '\\', '\'');
[3185]464 mr_asprintf(syscall, "%s '%s' 2>> /dev/null", syscall_sprintf, tmp); // " MONDO_LOGFILE);
[3191]465 mr_free(tmp);
[2548]466 log_msg(20,"calling %s\n",syscall);
[128]467 call_exe_and_pipe_output_to_fd(syscall, pout);
[3191]468 mr_free(syscall);
469 mr_free(file_to_analyze);
[128]470 }
[3191]471 mr_free(file_to_analyze);
[128]472 paranoid_fclose(fin);
473 paranoid_pclose(pout);
474 return (0);
[1]475}
476
477
[128]478int get_acl_list(char *filelist, char *facl_fname)
[1]479{
[2521]480 char *command = NULL;
[128]481 int retval = 0;
482
[944]483 if (g_getfacl != NULL) {
[3185]484 mr_asprintf(command, "touch \"%s\"", facl_fname);
[943]485 run_program_and_log_output(command, 8);
[2521]486 mr_free(command);
487
[3171]488 retval = gen_aux_list(filelist, "getfacl --all-effective -P ", facl_fname);
[128]489 }
490 return (retval);
[1]491}
492
493
[128]494int get_fattr_list(char *filelist, char *fattr_fname)
[1]495{
[128]496 char *command;
497 int retval = 0;
498
[944]499 if (g_getfattr != NULL) {
[3185]500 mr_asprintf(command, "touch \"%s\"", fattr_fname);
[942]501 run_program_and_log_output(command, 8);
[2521]502 mr_free(command);
[3171]503 retval = gen_aux_list(filelist, "getfattr --en=hex -m - -h -d ", fattr_fname);
[128]504 }
505 return (retval);
[1]506}
507
508
[128]509int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
510 char *executable)
[1]511{
[128]512 const int my_depth = 8;
[2521]513 char *command = NULL;
514 char *syscall_pin = NULL;
515 char *syscall_pout = NULL;
[3191]516 char *incoming = NULL;
517 char *current_subset_file = NULL;
518 char *current_master_file = NULL;
[2521]519 char *masklist = NULL;
[128]520 int retval = 0;
521 int i;
[3191]522 char *p, *q;
[2572]523 char *tmp = NULL;
[128]524 FILE *pin, *pout, *faclin;
[1]525
[3191]526 log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist, original_exat_fname, executable);
[128]527 if (!orig_msklist || !orig_msklist[0]
528 || !does_file_exist(orig_msklist)) {
[2521]529 log_msg(1, "No masklist provided. I shall therefore set ALL attributes.");
[2572]530 if (strstr(executable,"acl")) {
531 /* setfacl needs no additional option for physical walk */
[3185]532 mr_asprintf(tmp,"");
[2572]533 } else {
534 /* setfattr needs option -h for physical walk */
[3185]535 mr_asprintf(tmp,"-h");
[2572]536 }
[3185]537 mr_asprintf(command, "gzip -dc %s | %s %s --restore - 2>> %s", original_exat_fname, executable, tmp, MONDO_LOGFILE);
[2572]538 mr_free(tmp);
[128]539 log_msg(1, "command = %s", command);
540 retval = system(command);
[2521]541 mr_free(command);
[128]542 log_msg(1, "Returning w/ retval=%d", retval);
543 return (retval);
544 }
545 if (length_of_file(original_exat_fname) <= 0) {
[3191]546 log_msg(1, "original_exat_fname %s is empty or missing, so no need to set EXAT list", original_exat_fname);
[128]547 return (0);
548 }
[3185]549 mr_asprintf(masklist, "%s/masklist", bkpinfo->tmpdir);
550 mr_asprintf(command, "cp -f %s %s", orig_msklist, masklist);
[128]551 run_program_and_log_output(command, 1);
[2521]552 mr_free(command);
553
[128]554 sort_file(masklist);
[1]555
[3185]556 mr_asprintf(syscall_pout, "%s --restore - 2>> %s", executable, MONDO_LOGFILE);
[128]557 log_msg(1, "syscall_pout = %s", syscall_pout);
558 pout = popen(syscall_pout, "w");
[2521]559 mr_free(syscall_pout);
560
[128]561 if (!pout) {
[2230]562 log_it("Unable to openout to syscall_pout");
[2521]563 mr_free(masklist);
[128]564 return (1);
565 }
[2521]566
[3185]567 mr_asprintf(syscall_pin, "gzip -dc %s", original_exat_fname);
[2521]568 log_msg(1, "syscall_pin = %s", syscall_pin);
[128]569 pin = popen(syscall_pin, "r");
[2521]570 mr_free(syscall_pin);
571
[128]572 if (!pin) {
573 pclose(pout);
[2230]574 log_it("Unable to openin from syscall");
[128]575 return (1);
576 }
577 faclin = fopen(masklist, "r");
578 if (!faclin) {
579 pclose(pin);
580 pclose(pout);
[2230]581 log_it("Unable to openin masklist");
[2521]582 mr_free(masklist);
[128]583 return (1);
584 }
585// printf("Hi there. Starting the loop\n");
[1]586
[3191]587 mr_getline(current_subset_file, faclin);
588 mr_getline(incoming, pin);
589 while (!feof(pin) && !feof(faclin)) {
590 mr_asprintf(current_master_file, "%s", incoming + 8);
[1]591
[128]592 p = current_subset_file;
593 if (*p == '/') {
594 p++;
595 }
596 i = strlen(p);
597 if (i > 0 && p[i - 1] < 32) {
598 p[i - 1] = '\0';
599 }
600
601
602 q = current_master_file;
603 if (*q == '/') {
604 q++;
605 }
606 i = strlen(q);
607 if (i > 0 && q[i - 1] < 32) {
608 q[i - 1] = '\0';
609 }
610
611 i = strcmp(p, q);
612 log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
613
[1]614// printf("%s v %s --> %d\n", p, q, i);
[128]615
616 if (i < 0) { // read another subset file in.
617 log_msg(my_depth, "Reading next subset line in\n\n");
[3191]618 mr_free(current_subset_file);
619 mr_getline(current_subset_file, faclin);
[128]620 continue;
621 }
622
623 if (!i) {
624 fputs(incoming, pout);
625 }
[3191]626 mr_free(incoming);
627 mr_getline(incoming, pin);
[128]628 if (!i) {
629 log_msg(my_depth, "Copying master %s", q);
630 }
631
632 while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
633 if (!i) {
634 fputs(incoming, pout);
635 }
[3191]636 mr_free(incoming);
637 mr_getline(incoming, pin);
[128]638 }
639 if (!i) {
[3191]640 mr_free(current_subset_file);
641 mr_getline(current_subset_file, faclin);
[128]642 }
[3191]643 mr_free(current_master_file);
[1]644 }
[3191]645 mr_free(current_subset_file);
646 mr_free(incoming);
[128]647 fclose(faclin);
648 pclose(pin);
649 pclose(pout);
[1]650
[128]651 unlink(masklist);
[2521]652 mr_free(masklist);
653
[128]654 return (retval);
655}
[1]656
657
[128]658int set_fattr_list(char *masklist, char *fattr_fname)
[1]659{
[912]660 if (find_home_of_exe("setfattr")) {
661 return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
662 } else {
663 log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
664 return(0);
665 }
[1]666}
667
668
669
[128]670int set_acl_list(char *masklist, char *acl_fname)
[1]671{
[912]672 if (find_home_of_exe("setfacl")) {
673 return (set_EXAT_list(masklist, acl_fname, "setfacl"));
674 } else {
675 log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
676 return(0);
677 }
[1]678}
679
680
681/**
682 * Get the number of the last fileset in the backup.
683 * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
684 * @return The last filelist number.
685 * @note This function should only be called at restore-time.
686 */
[1645]687int get_last_filelist_number()
[1]688{
[128]689 /*@ buffers ***************************************************** */
690 char val_sz[MAX_STR_LEN];
[2521]691 char *cfg_fname = NULL;
[1]692
[128]693 /*@ long ******************************************************** */
694 int val_i;
[1]695
[128]696 /*@ end vars **************************************************** */
[1]697
[128]698 assert(bkpinfo != NULL);
[1]699
[1552]700 strcpy(val_sz,"");
[3185]701 mr_asprintf(cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
[128]702 read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
[2521]703 mr_free(cfg_fname);
704
[128]705 val_i = atoi(val_sz);
706 if (val_i <= 0) {
707 val_i = 500;
708 }
709 return (val_i);
[1]710}
711
712
713/**
714 * Add a string at @p startnode.
715 * @param startnode The node to start at when searching for where to add the string.
716 * @param string_to_add The string to add.
717 * @return 0 for success, 1 for failure.
718 * @bug I don't understand this function. Would someone care to explain it?
719 */
[128]720int add_string_at_node(struct s_node *startnode, char *string_to_add)
[1]721{
722
723
[128]724 /*@ int ******************************************************** */
725 int noof_chars;
726 int i;
727 int res;
[1]728
[128]729 /*@ sturctures ************************************************* */
730 struct s_node *node, *newnode;
[1]731
[128]732 /*@ char ****************************************************** */
733 char char_to_add;
[1]734
[128]735 /*@ bools ****************************************************** */
[1]736
[128]737 const bool sosodef = FALSE;
[1]738
[128]739 static int depth = 0;
740 static char original_string[MAX_STR_LEN];
[1]741
[128]742 assert(startnode != NULL);
743 assert(string_to_add != NULL);
[1]744
[128]745 if (!depth) {
746 strcpy(original_string, string_to_add);
747 }
[1]748
[128]749 noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
[1]750
[3191]751 /* walk across tree if necessary */
[128]752 node = startnode;
753 char_to_add = string_to_add[0];
754 if (node->right != NULL && node->ch < char_to_add) {
755 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
756 char_to_add, node->ch, (node->right)->ch);
757 return (add_string_at_node(node->right, string_to_add));
758 }
[1]759
[3191]760 /* walk down tree if appropriate */
[128]761 if (node->down != NULL && node->ch == char_to_add) {
762 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
763 depth++;
764 res = add_string_at_node(node->down, string_to_add + 1);
765 depth--;
766 return (res);
767 }
[1]768
[128]769 if (char_to_add == '\0' && node->ch == '\0') {
770 log_msg(6, "%s already in tree", original_string);
771 return (1);
772 }
[1]773
[3191]774 /* add here */
[128]775 if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
[541]776 log_to_screen("failed to malloc");
[128]777 depth--;
778 return (1);
779 }
780 if (char_to_add < node->ch) // add to the left of node
[1]781 {
[128]782 log_msg(7, "depth=%d char=%c --- adding (left)", depth,
783 char_to_add);
784 memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
785 node->right = newnode;
786 } else if (char_to_add > node->ch) // add to the right of node
787 {
788 log_msg(7, "depth=%d char=%c --- adding (right)", depth,
789 char_to_add);
790 newnode->right = node->right; // newnode is to the RIGHT of node
791 node->right = newnode;
792 node = newnode;
[1]793 }
[128]794 // from now on, we're working on 'node'
795 node->down = NULL;
796 node->ch = char_to_add;
797 node->expanded = node->selected = FALSE;
798 if (char_to_add == '\0') {
799 log_msg(6, "Added %s OK", original_string);
800 return (0);
801 }
802// add the rest
803 log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
804 for (i = 1; i < noof_chars; i++) {
805 if (!
806 (node->down =
807 (struct s_node *) malloc(sizeof(struct s_node)))) {
[541]808 log_to_screen("%s - failed to malloc", string_to_add);
[128]809 return (1);
810 }
811 node = node->down;
812 char_to_add = string_to_add[i];
813 log_msg(6, "Adding '%c'", char_to_add);
814 node->ch = char_to_add;
815 node->right = node->down = NULL;
816 node->expanded = node->selected = FALSE;
817 if (!node->ch) {
818 node->selected = sosodef;
819 }
820 }
821 log_msg(6, "Finally - added %s OK", original_string);
822 return (0);
[1]823}
824
825
826
827
828/**
829 * Load a filelist into a <tt>struct s_node</tt>.
830 * When you are done with the filelist, call free_filelist().
831 * @param filelist_fname The file to load the filelist from.
832 * @return A filelist tree structure.
833 */
[128]834struct s_node *load_filelist(char *filelist_fname)
[1]835{
836
[128]837 /*@ structures ************************************************* */
838 struct s_node *filelist;
[1]839
[128]840 /*@ pointers *************************************************** */
841 FILE *pin;
[1]842
[128]843 /*@ buffers **************************************************** */
[2521]844 char *command_to_open_fname = NULL;
[3191]845 char *fname = NULL;
846 char *tmp = NULL;
[2521]847 char *tmp1 = NULL;
[128]848 int pos_in_fname;
849 /*@ int ******************************************************** */
850 int percentage;
[1]851
[128]852 /*@ long ******************************************************* */
853 long lines_in_filelist;
854 long lino = 0;
855 /*@ end vars *************************************************** */
[1]856
[128]857 assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
[1]858
[128]859 if (!does_file_exist(filelist_fname)) {
860 fatal_error("filelist does not exist -- cannot load it");
[1]861 }
[541]862 log_to_screen("Loading filelist");
[3185]863 mr_asprintf(tmp1, "zcat %s | wc -l", filelist_fname);
[2521]864 log_msg(6, "tmp1 = %s", tmp1);
865 lines_in_filelist = atol(call_program_and_get_last_line_of_output(tmp1));
866 mr_free(tmp1);
867
[128]868 if (lines_in_filelist < 3) {
[541]869 log_to_screen("Warning - surprisingly short filelist.");
[1]870 }
[128]871 g_original_noof_lines_in_filelist = lines_in_filelist;
872 if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
873 return (NULL);
[1]874 }
[128]875 filelist->ch = '/';
876 filelist->right = NULL;
877 filelist->down = malloc(sizeof(struct s_node));
878 filelist->expanded = filelist->selected = FALSE;
879 (filelist->down)->ch = '\0';
880 (filelist->down)->right = (filelist->down)->down = FALSE;
881 (filelist->down)->expanded = (filelist->down)->selected = FALSE;
[2521]882
[3185]883 mr_asprintf(command_to_open_fname, "gzip -dc %s", filelist_fname);
[128]884 if (!(pin = popen(command_to_open_fname, "r"))) {
885 log_OS_error("Unable to openin filelist_fname");
[2521]886 mr_free(command_to_open_fname);
[128]887 return (NULL);
888 }
[2521]889 mr_free(command_to_open_fname);
890
[541]891 open_evalcall_form("Loading filelist from disk");
[3191]892 for (mr_getline(fname, pin); !feof(pin); mr_getline(fname, pin)) {
893 if ((strlen(fname) > 0) && (fname[strlen(fname) - 1] == 13 || fname[strlen(fname) - 1] == 10)) {
[128]894 fname[strlen(fname) - 1] = '\0';
895 }
896 if (!strlen(fname)) {
[3191]897 mr_free(fname);
[128]898 continue;
899 }
900 for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
901 if (fname[pos_in_fname] != '/') {
902 continue;
903 }
[3191]904 mr_asprintf(tmp, "%s", fname);
[128]905 tmp[pos_in_fname] = '\0';
906 if (strlen(tmp)) {
907 add_string_at_node(filelist, tmp);
908 }
[3191]909 mr_free(tmp);
[128]910 }
911 add_string_at_node(filelist, fname);
[3191]912
[128]913 if (!(++lino % 1111)) {
914 percentage = (int) (lino * 100 / lines_in_filelist);
915 update_evalcall_form(percentage);
916 }
[3191]917 mr_free(fname);
[128]918 }
[3191]919 mr_free(fname);
920
[128]921 paranoid_pclose(pin);
922 close_evalcall_form();
923 log_it("Finished loading filelist");
924 return (filelist);
[1]925}
926
927
928/**
929 * Log a list of files in @p node.
930 * @param node The toplevel node to use.
931 */
[128]932void show_filelist(struct s_node *node)
[1]933{
[128]934 static int depth = 0;
935 static char current_string[200];
[1]936
[128]937 if (depth == 0) {
938 log_msg(0, "----------------show filelist--------------");
939 }
940 current_string[depth] = node->ch;
[1]941
[128]942 log_msg(3, "depth=%d", depth);
943 if (node->down) {
944 log_msg(3, "moving down");
945 depth++;
946 show_filelist(node->down);
947 depth--;
948 }
[1]949
[128]950 if (!node->ch) {
951 log_msg(0, "%s\n", current_string);
952 }
[1]953
[128]954 if (node->right) {
955 log_msg(3, "moving right");
956 show_filelist(node->right);
957 }
958 if (depth == 0) {
959 log_msg(0, "----------------show filelist--------------");
960 }
961 return;
[1]962}
963
964
965
966
967/**
968 * Reset the filelist to the state it was when it was loaded. This does not
969 * touch the file on disk.
970 * @param filelist The filelist tree structure.
971 */
[128]972void reload_filelist(struct s_node *filelist)
[1]973{
[128]974 assert(filelist != NULL);
975 toggle_node_selection(filelist, FALSE);
976 toggle_path_expandability(filelist, "/", FALSE);
977 toggle_all_root_dirs_on(filelist);
[1]978}
979
980
981
982/**
983 * Save a filelist tree structure to disk.
984 * @param filelist The filelist tree structure to save.
985 * @param outfname Where to save it.
986 */
[128]987void save_filelist(struct s_node *filelist, char *outfname)
[1]988{
[128]989 /*@ int ********************************************************* */
990 static int percentage;
991 static int depth = 0;
[1]992
[128]993 /*@ buffers ***************************************************** */
994 static char str[MAX_STR_LEN];
[1]995
[128]996 /*@ structures ************************************************** */
997 struct s_node *node;
[1]998
[128]999 /*@ pointers **************************************************** */
1000 static FILE *fout = NULL;
[1]1001
[128]1002 /*@ long ******************************************************** */
1003 static long lines_in_filelist = 0;
1004 static long lino = 0;
[1]1005
[128]1006 /*@ end vars *************************************************** */
[1]1007
[128]1008 assert(filelist != NULL);
1009 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1010 if (depth == 0) {
[541]1011 log_to_screen("Saving filelist");
[128]1012 if (!(fout = fopen(outfname, "w"))) {
1013 fatal_error("Cannot openout/save filelist");
1014 }
1015 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
[541]1016 open_evalcall_form("Saving selection to disk");
[1]1017 }
[128]1018 for (node = filelist; node != NULL; node = node->right) {
1019 str[depth] = node->ch;
1020 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1021 if (!node->ch) {
1022// if (node->selected)
1023// {
1024 fprintf(fout, "%s\n", str);
1025// }
1026 if (!(++lino % 1111)) {
1027 percentage = (int) (lino * 100 / lines_in_filelist);
1028 update_evalcall_form(percentage);
1029 }
1030 }
1031 if (node->down) {
1032 depth++;
1033 save_filelist(node->down, "");
1034 depth--;
1035 }
[1]1036 }
[128]1037 if (depth == 0) {
1038 paranoid_fclose(fout);
1039 close_evalcall_form();
1040 log_it("Finished saving filelist");
1041 }
[1]1042}
1043
1044
1045
1046/**
1047 * Toggle all root dirs on.
1048 * @param filelist The filelist tree structure to operate on.
1049 * @bug I don't understand this function. Would someone care to explain it?
1050 */
[128]1051void toggle_all_root_dirs_on(struct s_node *filelist)
[1]1052{
[128]1053 /*@ structures ************************************************** */
1054 struct s_node *node;
[1]1055
[128]1056 /*@ int ********************************************************* */
1057 static int depth = 0;
1058 static int root_dirs_expanded;
[1]1059
[128]1060 /*@ buffers ***************************************************** */
1061 static char filename[MAX_STR_LEN];
[1]1062
[128]1063 /*@ end vars *************************************************** */
[1]1064
[128]1065 assert(filelist != NULL);
1066 if (depth == 0) {
1067 log_it("Toggling all root dirs ON");
1068 root_dirs_expanded = 0;
[1]1069 }
[128]1070 for (node = filelist; node != NULL; node = node->right) {
1071 filename[depth] = node->ch;
1072 if (node->ch == '\0' && strlen(filename) > 1
1073 && (!strchr(filename + 1, '/'))) {
1074 node->selected = FALSE;
1075 node->expanded = TRUE;
1076// log_it (filename);
1077 root_dirs_expanded++;
1078 }
1079 if (node->down) {
1080 depth++;
1081 toggle_all_root_dirs_on(node->down);
1082 depth--;
1083 }
[1]1084 }
[128]1085 if (depth == 0) {
1086 log_it("Finished toggling all root dirs ON");
1087 }
[1]1088}
1089
1090
1091/**
1092 * Toggle the expandability of a path.
1093 * @param filelist The filelist tree to operate on.
1094 * @param pathname The path to toggle expandability of.
1095 * @param on_or_off Whether to toggle it on or off.
1096 * @bug I don't understand this function. Would someone care to explain it?
1097 */
1098void
[128]1099toggle_path_expandability(struct s_node *filelist, char *pathname,
1100 bool on_or_off)
[1]1101{
1102
[128]1103 /*@ int ******************************************************** */
1104 static int depth = 0;
1105 static int total_expanded;
1106 static int root_depth;
1107 int j;
1108 /*@ structures ************************************************* */
1109 struct s_node *node;
[1]1110
[128]1111 /*@ buffers **************************************************** */
1112 static char current_filename[MAX_STR_LEN];
[1]1113
1114/* char tmp[MAX_STR_LEN+2]; */
1115
[128]1116 /*@ end vars *************************************************** */
[1]1117
[128]1118 assert(filelist != NULL);
1119 assert_string_is_neither_NULL_nor_zerolength(pathname);
1120 if (depth == 0) {
1121 total_expanded = 0;
[1]1122// log_it ("Toggling path's expandability");
[128]1123 for (root_depth = (int) strlen(pathname);
1124 root_depth > 0 && pathname[root_depth - 1] != '/';
1125 root_depth--);
1126 if (root_depth < 2) {
1127 root_depth = (int) strlen(pathname);
1128 }
[1]1129 }
[128]1130 for (node = filelist; node != NULL; node = node->right) {
1131 current_filename[depth] = node->ch;
1132 if (node->down) {
1133 depth++;
1134 toggle_path_expandability(node->down, pathname, on_or_off);
1135 depth--;
[1]1136 }
[128]1137 if (node->ch == '\0') {
1138 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1139 for (j = root_depth;
1140 current_filename[j] != '/'
1141 && current_filename[j] != '\0'; j++);
1142 if (current_filename[j] != '\0') {
1143 for (j++;
1144 current_filename[j] != '/'
1145 && current_filename[j] != '\0'; j++);
1146 }
1147 if (current_filename[j] == '\0') {
1148 node->expanded =
1149 (!strcmp(pathname, current_filename) ? TRUE :
1150 on_or_off);
1151 }
1152 }
[1]1153 }
[128]1154 if (node->expanded) {
1155 if (total_expanded < ARBITRARY_MAXIMUM - 32
1156 || !strrchr(current_filename + strlen(pathname), '/')) {
1157 total_expanded++;
1158 } else {
1159 node->expanded = FALSE;
1160 }
1161 }
[1]1162 }
[128]1163 if (depth == 0) {
1164// log_it ("Finished toggling expandability");
[1]1165 }
1166}
1167
1168/**
1169 * Toggle whether a path is selected.
1170 * @param filelist The filelist tree to operate on.
1171 * @param pathname The path to toggle selection of.
1172 * @param on_or_off Whether to toggle it on or off.
1173 * @bug I don't understand this function. Would someone care to explain it?
1174 */
1175void
[128]1176toggle_path_selection(struct s_node *filelist, char *pathname,
1177 bool on_or_off)
[1]1178{
[128]1179 /*@ int ********************************************************* */
1180 static int depth = 0;
1181 int j;
[1]1182
[128]1183 /*@ structures ************************************************** */
1184 struct s_node *node;
[1]1185
[128]1186 /*@ buffers ***************************************************** */
1187 static char current_filename[MAX_STR_LEN];
[1]1188
[128]1189 /*@ end vars *************************************************** */
1190 assert(filelist != NULL);
1191 assert_string_is_neither_NULL_nor_zerolength(pathname);
1192 if (depth == 0) {
1193 log_it("Toggling path's selection");
[1]1194 }
[128]1195 for (node = filelist; node != NULL; node = node->right) {
1196 current_filename[depth] = node->ch;
1197 if (node->down) {
1198 depth++;
1199 toggle_path_selection(node->down, pathname, on_or_off);
1200 depth--;
[1]1201 }
[128]1202 if (node->ch == '\0') {
1203 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1204 for (j = 0;
1205 pathname[j] != '\0'
1206 && pathname[j] == current_filename[j]; j++);
[2521]1207 if (current_filename[j] == '/'
1208 || current_filename[j] == '\0') {
1209 node->selected = on_or_off;
[128]1210 }
1211 }
1212 }
[1]1213 }
[128]1214 if (depth == 0) {
1215 log_it("Finished toggling selection");
1216 }
[1]1217}
1218
1219
1220/**
1221 * Toggle node selection of a filelist tree.
1222 * @param filelist The filelist tree to operate on.
1223 * @param on_or_off Whether to toggle selection on or off.
1224 * @bug I don't understand this function. Would someone care to explain it?
1225 */
[128]1226void toggle_node_selection(struct s_node *filelist, bool on_or_off)
[1]1227{
[128]1228 /*@ structure ************************************************** */
1229 struct s_node *node;
[1]1230
[128]1231 /*@ end vars *************************************************** */
1232 assert(filelist != NULL);
1233 for (node = filelist; node != NULL; node = node->right) {
1234 if (node->ch == '/') {
1235 continue;
1236 } /* don't go deep */
1237 if (node->ch == '\0') {
1238 node->selected = on_or_off;
1239 }
1240 if (node->down) {
1241 toggle_node_selection(node->down, on_or_off);
1242 }
[1]1243 }
1244}
1245
1246
1247
[2366]1248/**
[2369]1249 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
[2366]1250 */
[2369]1251char *g_skeleton_filelist = NULL;
[1]1252
1253
1254
[2369]1255/**
1256 * Get the next entry in the space-separated list in @p incoming.
1257 * So if @p incoming was '"one and two" three four', we would
1258 * return "one and two".
1259 * @param incoming The list to get the next entry from.
1260 * @return The first item in the list (respecting double quotes).
1261 * @note The returned string points to static data that will be overwritten with each call.
1262 */
1263char *next_entry(char *incoming)
1264{
1265 char *sz_res;
1266 char *p;
1267 bool in_quotes = FALSE;
[1]1268
[3185]1269 mr_asprintf(sz_res, "%s", incoming);
[2369]1270 p = sz_res;
[2713]1271 while ((*p != '|' || in_quotes) && *p != '\0') {
[2369]1272 if (*p == '\"') {
1273 in_quotes = !in_quotes;
[2366]1274 }
[2369]1275 p++;
[2366]1276 }
[2369]1277 *p = '\0';
1278 return (sz_res);
[2366]1279}
1280
1281
[1]1282/**
1283 * Number of entries in the skeleton filelist.
1284 */
[128]1285long g_skeleton_entries = 0;
[1]1286
1287
1288/**
1289 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1290 * @param dir The directory to list to @p fout.
1291 * @param sth The directories to skip (exclude).
1292 * @param fout The file to write everything to.
1293 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1294 * @return 0, always.
1295 * @bug Return value should be @c void.
1296 */
[3191]1297int open_and_list_dir(char *dir1, char *sth, FILE * fout, time_t time_of_last_full_backup) {
1298
[2713]1299 const char delims[] = "|";
[820]1300
[128]1301 DIR *dip;
1302 struct dirent *dit;
1303 struct stat statbuf;
[2099]1304 char *new;
[3191]1305 char *tmp = NULL;
[2198]1306 char *dir = NULL;
[128]1307 static int percentage = 0;
[3191]1308 char *skip_these = NULL;
[2713]1309 char *new_with_pipe;
[2972]1310 char *strtmp = NULL;
1311 char *token = NULL;
[2711]1312 char *find_excludes = NULL;
[3191]1313 char *name_of_evalcall_form = NULL;
1314 char *find_skeleton_marker = NULL;
[128]1315 int i;
[820]1316 int lastpos = 0;
[128]1317 static int depth = 0;
1318 char *p;
1319 static int counter = 0;
1320 static int uberctr = 0;
[3191]1321 static long skeleton_lino = 0L;
1322 static time_t last_time = (time_t)0;
[128]1323 time_t this_time;
[1]1324
[2402]1325 /* dir is needed when we pass it to the shell */
[3171]1326 dir = mr_stresc(dir1, "'", '\\', '\'');
[2436]1327 p = strrchr(dir1, '/');
[128]1328 if (p) {
1329 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
[2241]1330 mr_free(dir);
[128]1331 return (0);
1332 }
1333 }
[3185]1334 mr_asprintf(find_excludes, " ");
[128]1335
1336 if (!depth) {
[2972]1337 while((token = mr_strtok(sth, delims, &lastpos)) != NULL) {
1338 mr_strcat(find_excludes," -path %s -prune -o", token);
[2241]1339 mr_free(token);
[820]1340 }
[2641]1341
1342/**
1343 * The maximum depth of directories to put in the skeleton filelist.
1344 * This is a balance between performance and a good progress indicator.
1345 */
1346#define MAX_SKEL_DEPTH 3
1347
[1]1348#if linux
[128]1349 // 2.6 has /sys as a proc-type thing -- must be excluded
[3185]1350 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]1351#else
[128]1352 // On BSD, for example, /sys is the kernel sources -- don't exclude
[3185]1353 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]1354#endif
[3191]1355 mr_free(find_excludes);
1356
[820]1357 log_msg(5, "find command = %s", strtmp);
[3060]1358 paranoid_system(strtmp);
[3191]1359 mr_free(strtmp);
1360
[3185]1361 mr_asprintf(tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
[3191]1362 g_skeleton_entries = 1 + atol(call_program_and_get_last_line_of_output(tmp));
1363 mr_free(tmp);
1364
1365 mr_asprintf(name_of_evalcall_form, "Making catalog of %s", dir1);
[128]1366 open_evalcall_form(name_of_evalcall_form);
[3191]1367 mr_free(name_of_evalcall_form);
1368
[128]1369 skeleton_lino = 1;
1370 log_msg(5, "entries = %ld", g_skeleton_entries);
1371 percentage = 0;
[2641]1372 } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
[128]1373 {
[3191]1374 mr_asprintf(find_skeleton_marker, "grep -Fv '%s' %s > %s.new 2> /dev/null", dir, g_skeleton_filelist, g_skeleton_filelist);
[128]1375 if (!system(find_skeleton_marker)) {
1376 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1377 skeleton_lino++;
[3191]1378 mr_free(find_skeleton_marker);
1379
1380 mr_asprintf(find_skeleton_marker, "mv -f %s.new %s", g_skeleton_filelist, g_skeleton_filelist);
[2436]1381 paranoid_system(find_skeleton_marker);
[128]1382 time(&this_time);
1383 if (this_time != last_time) {
1384 last_time = this_time;
1385 if (!g_text_mode) {
[2766]1386 int cols, rows;
1387 newtGetScreenSize(&cols, &rows);
[3185]1388 mr_asprintf(tmp, "Reading %-*s", cols, dir1);
[128]1389 newtDrawRootText(0, g_noof_rows - 3, tmp);
[2766]1390 mr_free(tmp);
[128]1391 }
1392 update_evalcall_form(percentage);
1393 }
[1]1394 }
[3191]1395 mr_free(find_skeleton_marker);
[128]1396 }
[1]1397
[128]1398 depth++;
[1]1399
1400// log_msg(0, "Cataloguing %s", dir);
[3185]1401 mr_asprintf(skip_these, "|%s|", sth);
[2713]1402
[3185]1403 mr_asprintf(new_with_pipe, "|%s|", dir1);
[2402]1404 if ((dip = opendir(dir1)) == NULL) {
[3185]1405 mr_asprintf(tmp,"opendir %s", dir1);
[1260]1406 log_OS_error(tmp);
[3191]1407 mr_free(tmp);
[2713]1408 } else if (strstr(skip_these, new_with_pipe)) {
[2436]1409 log_msg(10, "Found dir ***%s**** excluded", dir1);
[2402]1410 fprintf(fout, "%s\n", dir1); // if excluded dir then print dir ONLY
[128]1411 } else {
[2436]1412 log_msg(10, "Found dir ***%s**** parsed", dir1);
[2402]1413 fprintf(fout, "%s\n", dir1);
[128]1414 while ((dit = readdir(dip)) != NULL) {
1415 i++;
[2402]1416 if (strcmp(dir1, "/")) {
[3185]1417 mr_asprintf(new,"%s/%s",dir1,dit->d_name);
[2099]1418 } else {
[3185]1419 mr_asprintf(new,"%s%s",dir1,dit->d_name);
[128]1420 }
[3191]1421 mr_free(new_with_pipe);
[3185]1422 mr_asprintf(new_with_pipe, "|%s|", new);
[2713]1423 if (strstr(skip_these, new_with_pipe)) {
[128]1424 fprintf(fout, "%s\n", new);
[2436]1425 log_msg(10, "Found child dir ***%s**** excluded", new);
[3191]1426 mr_free(new_with_pipe);
[128]1427 } else {
[3191]1428 mr_free(new_with_pipe);
[128]1429 if (!lstat(new, &statbuf)) {
1430 if (!S_ISLNK(statbuf.st_mode)
1431 && S_ISDIR(statbuf.st_mode)) {
[2436]1432 log_msg(10, "Found child dir ***%s**** parsed", new);
[3191]1433 open_and_list_dir(new, skip_these, fout, time_of_last_full_backup);
[128]1434 } else {
[3191]1435 if (time_of_last_full_backup == 0 || time_of_last_full_backup < statbuf.st_ctime) {
[2436]1436 log_msg(10, "Found child file ***%s**** parsed", new);
[128]1437 fprintf(fout, "%s\n", new);
1438 if ((counter++) > 128) {
1439 counter = 0;
1440 uberctr++;
[3185]1441 mr_asprintf(tmp, " %c ", special_dot_char(uberctr));
[128]1442 if (!g_text_mode) {
[3191]1443 newtDrawRootText(77, g_noof_rows - 3, tmp);
[128]1444 newtRefresh();
1445 }
[3191]1446 mr_free(tmp);
[128]1447 }
1448 }
1449 }
1450 }
1451 }
[3191]1452 mr_free(new);
[1]1453 }
[128]1454 }
[3191]1455 mr_free(new_with_pipe);
1456 mr_free(skip_these);
[2241]1457 mr_free(dir);
[2099]1458
[128]1459 if (dip) {
1460 if (closedir(dip) == -1) {
1461 log_OS_error("closedir");
1462 }
1463 }
1464 depth--;
1465 if (!depth) {
1466 close_evalcall_form();
1467 unlink(g_skeleton_filelist);
1468 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1469 }
1470 return (0);
[1]1471}
1472
1473
1474
1475/**
[2369]1476 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1477 * @param logfile Unused.
1478 * @param tmpdir The tmpdir of the backup.
1479 * @param scratchdir The scratchdir of the backup.
1480 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1481 * @param excp The paths to NOT back up.
1482 * @param differential The differential level (currently only 0 and 1 are supported).
1483 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1484 * @return 0, always.
1485 * @bug @p logfile is unused.
1486 * @bug Return value is meaningless.
[1]1487 */
[2369]1488int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1489 char *include_paths, char *excp, int differential,
1490 char *userdef_filelist)
[1]1491{
[2369]1492 char *p, *q;
1493 char *sz_datefile;
[3191]1494 char *sz_filelist;
[2713]1495 char *exclude_paths = NULL;
[2369]1496 FILE *fout;
[2521]1497 char *command = NULL;
[2369]1498 time_t time_of_last_full_backup = 0;
1499 struct stat statbuf;
1500 char *tmp1 = NULL;
1501 char *tmp2 = NULL;
[1]1502
[3185]1503 mr_asprintf(sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
[2369]1504 if (!include_paths && !userdef_filelist) {
[3191]1505 fatal_error("Please supply either include_paths or userdef_filelist");
[2369]1506 }
[3191]1507 // make hole for filelist
[3185]1508 mr_asprintf(command, "mkdir -p %s/archives", scratchdir);
[2369]1509 paranoid_system(command);
[2521]1510 mr_free(command);
1511
[3185]1512 mr_asprintf(sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
[2369]1513 make_hole_for_file(sz_filelist);
1514
1515 if (differential == 0) {
1516 // restore last good datefile if it exists
[3185]1517 mr_asprintf(command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
[2369]1518 run_program_and_log_output(command, 3);
[2521]1519 mr_free(command);
1520
[2369]1521 // backup last known good datefile just in case :)
1522 if (does_file_exist(sz_datefile)) {
[3191]1523 mr_asprintf(command, "mv -f %s %s.aborted", sz_datefile, sz_datefile);
[2369]1524 paranoid_system(command);
[2521]1525 mr_free(command);
[128]1526 }
[2369]1527 make_hole_for_file(sz_datefile);
1528 write_one_liner_data_file(sz_datefile,
1529 call_program_and_get_last_line_of_output
1530 ("date +%s"));
1531 } else if (lstat(sz_datefile, &statbuf)) {
[3191]1532 log_msg(2, "Warning - unable to find date of previous backup. Full backup instead.");
[2369]1533 differential = 0;
1534 time_of_last_full_backup = 0;
1535 } else {
1536 time_of_last_full_backup = statbuf.st_mtime;
1537 log_msg(2, "Differential backup. Yay.");
[128]1538 }
[2369]1539 paranoid_free(sz_datefile);
1540
1541// use user-specified filelist (if specified)
1542 if (userdef_filelist) {
[3191]1543 log_msg(1, "Using the user-specified filelist - %s - instead of calculating one", userdef_filelist);
[3185]1544 mr_asprintf(command, "cp -f %s %s", userdef_filelist, sz_filelist);
[2369]1545 if (run_program_and_log_output(command, 3)) {
[2521]1546 mr_free(command);
[2369]1547 fatal_error("Failed to copy user-specified filelist");
1548 }
[2521]1549 mr_free(command);
[2369]1550 } else {
[3191]1551 if (include_paths) {
1552 log_msg(2, "include_paths = '%s'", include_paths);
1553 }
[2369]1554 log_msg(1, "Calculating filelist");
[3185]1555 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]1556 if (strlen(tmp2) < 1) {
[3185]1557 mr_asprintf(tmp1," ");
[2369]1558 } else {
1559 log_msg(2, "Found windows FS: %s",tmp2);
[3185]1560 mr_asprintf(tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
[3191]1561 mr_free(tmp2);
[3185]1562 mr_asprintf(tmp2, "%s", call_program_and_get_last_line_of_output(tmp1));
[2369]1563 log_msg(2, "Found windows files: %s",tmp2);
1564 }
[3191]1565 mr_free(tmp1);
[2369]1566
[3185]1567 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]1568 if (excp != NULL) {
1569 mr_strcat(exclude_paths,"|%s",excp);
1570 }
[3191]1571 mr_free(tmp2);
[2369]1572
1573 log_msg(2, "Excluding paths = '%s'", exclude_paths);
[3191]1574 log_msg(2, "Generating skeleton filelist so that we can track our progress");
1575 mr_asprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
[2369]1576 make_hole_for_file(g_skeleton_filelist);
[3191]1577
[2369]1578 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1579 log_msg(2, "Opening out filelist to %s", sz_filelist);
1580 if (!(fout = fopen(sz_filelist, "w"))) {
1581 fatal_error("Cannot openout to sz_filelist");
1582 }
[3191]1583 if ((include_paths != NULL) && (strlen(include_paths) == 0)) {
[2369]1584 log_msg(1, "Including only '/' in %s", sz_filelist);
1585 open_and_list_dir("/", exclude_paths, fout,
1586 time_of_last_full_backup);
1587 } else {
1588 p = include_paths;
1589 while (*p) {
1590 q = next_entry(p);
1591 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1592 open_and_list_dir(q, exclude_paths, fout,
1593 time_of_last_full_backup);
1594 p += strlen(q);
1595 paranoid_free(q);
[2713]1596 while (*p == '|') {
[2369]1597 p++;
1598 }
1599 }
1600 }
[3191]1601 mr_free(exclude_paths);
[2369]1602 paranoid_fclose(fout);
1603 }
1604 log_msg(2, "Copying new filelist to scratchdir");
[3185]1605 mr_asprintf(command, "mkdir -p %s/archives", scratchdir);
[2369]1606 paranoid_system(command);
[2521]1607 mr_free(command);
1608
[3185]1609 mr_asprintf(command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
[2369]1610 paranoid_system(command);
[2521]1611 mr_free(command);
1612
[3185]1613 mr_asprintf(command, "mv -f %s %s", sz_filelist, tmpdir);
[2369]1614 paranoid_system(command);
[2521]1615 mr_free(command);
1616
[2369]1617 paranoid_free(sz_filelist);
1618 log_msg(2, "Freeing variables");
[3191]1619 mr_free(g_skeleton_filelist);
[2369]1620 log_msg(2, "Exiting");
1621 return (0);
[1]1622}
1623
[3191]1624
[2369]1625/**
1626 * Wrapper around mondo_makefilelist().
1627 * @param bkpinfo The backup information structure. Fields used:
1628 * - @c bkpinfo->differential
1629 * - @c bkpinfo->exclude_paths
1630 * - @c bkpinfo->include_paths
1631 * - @c bkpinfo->make_filelist
1632 * - @c bkpinfo->scratchdir
1633 * - @c bkpinfo->tmpdir
1634 * @return 0 for success, nonzero for failure.
1635 * @see mondo_makefilelist
1636 */
1637int prepare_filelist()
1638{
[1]1639
[2369]1640 /*@ int **************************************************** */
1641 int res = 0;
[1]1642
[2369]1643 assert(bkpinfo != NULL);
[3191]1644 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir, bkpinfo->scratchdir);
[2369]1645 if (bkpinfo->make_filelist) {
1646 mvaddstr_and_log_it(g_currentY, 0,
1647 "Making catalog of files to be backed up");
1648 } else {
1649 mvaddstr_and_log_it(g_currentY, 0,
1650 "Using supplied catalog of files to be backed up");
1651 }
1652
1653 if (bkpinfo->make_filelist) {
1654 res =
1655 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1656 bkpinfo->scratchdir, bkpinfo->include_paths,
1657 bkpinfo->exclude_paths,
1658 bkpinfo->differential, NULL);
1659 } else {
1660 res =
1661 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1662 bkpinfo->scratchdir, NULL,
1663 bkpinfo->exclude_paths,
1664 bkpinfo->differential,
1665 bkpinfo->include_paths);
1666 }
1667
1668 if (res) {
1669 log_OS_error("Call to mondo_makefilelist failed");
1670 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1671 } else {
1672 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1673 }
1674 return (res);
1675}
1676
1677
[1]1678/**
1679 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1680 * @param startnode The node containing the root of the directory tree.
1681 * @param string_to_find The string to look for at @p startnode.
1682 * @return The node containing the last element of @p string_to_find, or NULL if
1683 * it was not found.
1684 */
[128]1685struct s_node *find_string_at_node(struct s_node *startnode,
1686 char *string_to_find)
[1]1687{
[128]1688 /*@ int ******************************************************** */
1689 static int depth = 0;
1690 static char original_string[MAX_STR_LEN];
[1]1691
[128]1692 /*@ sturctures ************************************************* */
1693 struct s_node *node;
[1]1694
[128]1695 /*@ char ****************************************************** */
1696 char char_to_find;
[1]1697
[128]1698 /*@ bools ****************************************************** */
[1]1699
[128]1700 if (!depth) {
1701 strcpy(original_string, string_to_find);
1702 }
[1]1703
[128]1704 assert(startnode != NULL);
1705 assert(string_to_find != NULL);
[1]1706
[128]1707 log_msg(7, "starting --- str=%s", string_to_find);
[1]1708
1709/* walk across tree if necessary */
[128]1710 node = startnode;
1711 char_to_find = string_to_find[0];
1712 if (node->right != NULL && node->ch < char_to_find) {
1713 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1714 char_to_find, node->ch, (node->right)->ch);
1715 return (find_string_at_node(node->right, string_to_find));
1716 }
[1]1717
1718/* walk down tree if appropriate */
[128]1719 if (node->down != NULL && node->ch == char_to_find) {
1720 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1721 depth++;
1722 node = find_string_at_node(node->down, string_to_find + 1);
1723 depth--;
1724 return (node);
1725 }
[1]1726
[128]1727 if (char_to_find == '\0' && node->ch == '\0') {
1728 log_msg(7, "%s is in tree", original_string);
1729 return (node);
1730 } else {
1731 log_msg(7, "%s is NOT in tree", original_string);
1732 return (NULL);
1733 }
[1]1734}
1735
1736
1737
1738/**
1739 * Write all entries in @p needles_list_fname which are also in
1740 * @p filelist to @p matches_list_fname.
1741 * @param needles_list_fname A file containing strings to look for, 1 per line.
1742 * @param filelist The node for the root of the directory structure to search in.
1743 * @param matches_list_fname The filename where we should put the matches.
1744 * @return The number of matches found.
1745 */
[128]1746long save_filelist_entries_in_common(char *needles_list_fname,
1747 struct s_node *filelist,
1748 char *matches_list_fname,
1749 bool use_star)
[1]1750{
[128]1751 int retval = 0;
1752 struct s_node *found_node;
1753 FILE *fin;
1754 FILE *fout;
[3191]1755 char *fname = NULL;
1756 char *tmp = NULL;
[1]1757
[128]1758 log_msg(5, "starting");
1759 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1760 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1761 if (!(fin = fopen(needles_list_fname, "r"))) {
1762 fatal_error("Cannot openin needles_list_fname");
1763 }
1764 if (!(fout = fopen(matches_list_fname, "w"))) {
1765 fatal_error("Cannot openout matches_list_fname");
1766 }
1767 while (!feof(fin)) {
[3191]1768 mr_getline(fname, fin);
[128]1769 if (!use_star) {
1770 if (fname[0] == '/') {
[3191]1771 mr_asprintf(tmp, "%s", fname);
[128]1772 } else {
[3191]1773 mr_asprintf(tmp, "/%s", fname);
[128]1774 }
[3191]1775 mr_free(fname);
1776 fname = tmp;
[128]1777 }
1778 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1779 fname[strlen(fname) - 1] = '\0';
1780 }
[1]1781
[128]1782 log_msg(5, "Looking for '%s'", fname);
1783 found_node = find_string_at_node(filelist, fname);
1784 if (found_node) {
1785 if (found_node->selected) {
1786 if (fname[0] == '/') {
[3191]1787 mr_asprintf(tmp, "%s", fname + 1);
1788 mr_free(fname);
1789 fname = tmp;
[128]1790 }
1791 log_msg(5, "Found '%s'", fname);
[3194]1792 tmp = mr_stresc(fname, "[]*?", '\\', '\'');
[3191]1793 mr_free(fname);
1794 fname = tmp;
1795 fprintf(fout, "%s\n", fname);
[128]1796 retval++;
1797 }
[1]1798 }
[3191]1799 mr_free(fname);
[1]1800 }
[128]1801 paranoid_fclose(fout);
1802 paranoid_fclose(fin);
1803 return (retval);
[1]1804}
1805
1806
1807
1808/**
1809 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1810 * @p filelist.
1811 * @param filelist The top node of the directory structure to add the files to.
1812 * @param list_of_files_fname The file containing the files to add, 1 per line.
1813 * @param flag_em If TRUE, then flag the added files for restoration.
1814 * @return 0 for success, nonzero for failure.
1815 */
[128]1816int add_list_of_files_to_filelist(struct s_node *filelist,
1817 char *list_of_files_fname, bool flag_em)
[1]1818{
[3191]1819 FILE *fin = NULL;
1820 char *tmp = NULL;
1821 struct s_node *nod = NULL;
[1]1822
[128]1823 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1824 if (!(fin = fopen(list_of_files_fname, "r"))) {
[2227]1825 log_it("%s",list_of_files_fname);
[128]1826 return (1);
1827 }
[3191]1828 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
[128]1829 if (!tmp[0]) {
[3191]1830 mr_free(tmp);
[128]1831 continue;
1832 }
[3191]1833 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)) {
[128]1834 tmp[strlen(tmp) - 1] = '\0';
1835 }
1836 log_msg(2, "tmp = '%s'", tmp);
1837 if (!tmp[0]) {
[3191]1838 mr_free(tmp);
[128]1839 continue;
1840 }
1841 if ((nod = find_string_at_node(filelist, tmp))) {
1842 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1843 } else {
1844 add_string_at_node(filelist, tmp);
1845 nod = find_string_at_node(filelist, tmp);
1846 }
[1]1847
[128]1848 if (nod && flag_em) {
1849 toggle_path_selection(filelist, tmp, TRUE);
1850 log_msg(5, "Flagged '%s'", tmp);
1851 }
[3191]1852 mr_free(tmp);
[1]1853 }
[3191]1854 mr_free(tmp);
[128]1855 paranoid_fclose(fin);
1856 return (0);
[1]1857}
1858
1859/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.