source: MondoRescue/branches/3.3/mondo/src/common/libmondo-filelist.c@ 3878

Last change on this file since 3878 was 3878, checked in by Bruno Cornec, 2 years ago

Fix compiler errors

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