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

Last change on this file since 2816 was 2816, checked in by Bruno Cornec, 9 years ago

svn merge (in svk) -r 2714:2773 2.2.9 in 2.2.10

r4207@localhost (orig r2716): bruno | 2011-02-03 01:29:51 +0100

  • Increase boot size again to support better fedora 13/14


r4208@localhost (orig r2717): bruno | 2011-02-12 15:43:36 +0100

  • Excluding vmhgfs FS for VMWare guests mounting areas of the host.


r4209@localhost (orig r2718): bruno | 2011-02-18 00:30:35 +0100
-Extend EXTRA-SIZE for Debian 6.0 (at least)


r4210@localhost (orig r2719): bruno | 2011-02-18 00:45:40 +0100

  • Fix #462 (removes temp dir if analyze-my-lvm launched in stand alonae mode)


r4213@localhost (orig r2722): bruno | 2011-02-25 18:44:47 +0100

  • Fixes for RHEL6 support at least around dm extensions, bnx2x and cp210x dependencies


r4214@localhost (orig r2723): bruno | 2011-02-25 19:00:17 +0100

  • Adds a restore quiet option


r4215@localhost (orig r2724): bruno | 2011-02-25 19:43:50 +0100

  • Adds support for dm-log used on RHEL6 as well.


r4216@localhost (orig r2725): bruno | 2011-02-25 21:26:54 +0100

  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3


r4217@localhost (orig r2726): bruno | 2011-02-26 11:38:29 +0100

  • Fix error introduced in previous patch in the if/then/else case without member in the then


r4218@localhost (orig r2727): bruno | 2011-02-26 12:05:33 +0100

  • With mindi-busybox 1.18.3 the syntax of openvt changed. This patch keeps a compatible init script for both previous and current version.


r4219@localhost (orig r2728): bruno | 2011-02-26 15:27:40 +0100

  • Latest lvdisplay print MiB instead of MB. Impact on analyze-my-lvm fixed.


r4220@localhost (orig r2729): bruno | 2011-02-26 16:39:32 +0100

  • Adds feature rpc to mindi-busybox in case of need



r4221@localhost (orig r2730): bruno | 2011-02-26 16:42:43 +0100

  • Adds the rebot command to the list of mandatory ones at restore time


r4222@localhost (orig r2731): bruno | 2011-02-26 17:37:13 +0100

  • Adds functions and /bin/loadkeys (new location) to minimal set of commands to have
  • provides a makemessage option to mindi and this function now works with pbdistrocheck
  • Do not create an error when a phys device has 0 size and is a /dev/dm-* device


r4223@localhost (orig r2732): bruno | 2011-02-26 19:07:43 +0100

  • Syntax fix


r4224@localhost (orig r2733): bruno | 2011-02-26 19:51:16 +0100

  • Fix test on dm which was made in wrong sense
  • Adds a driver to allow raid456 to work


r4225@localhost (orig r2734): bruno | 2011-02-26 20:55:00 +0100

  • Fix 2 redirections issues


r4317@localhost (orig r2737): bruno | 2011-03-13 13:12:57 +0100

  • Add support for libahci driver, mandatory for debian 6.


r4318@localhost (orig r2738): bruno | 2011-03-13 14:46:37 +0100

  • Update from Lester Wade (lester.wade_at_hp.com) incorporating latest ML feedbacks


r4319@localhost (orig r2739): bruno | 2011-03-13 17:49:15 +0100

  • Updated Lab for TES 2011


r4323@localhost (orig r2743): bruno | 2011-03-20 01:37:40 +0100

  • Fix a seg fault bug when using the GUI in case exclude_paths was NULL (mc_at_carreira.com.pt)


r4324@localhost (orig r2744): bruno | 2011-03-20 19:38:14 +0100

  • Adds Arch Linux Keyboard support (mc_at_carreira.com.pt)


r4325@localhost (orig r2745): bruno | 2011-03-20 19:48:01 +0100

  • Avoids coredump again when exclude_path is NULL with mr_make_devlist_from_pathlist (feedback from mc_at_carreira.com.pt)


r4326@localhost (orig r2746): bruno | 2011-03-20 19:39:47 +0100

  • Remove blkid from busybox to solve #435


r4327@localhost (orig r2747): bruno | 2011-03-20 19:43:22 +0100

  • Adds reboot to fix #435


r4328@localhost (orig r2748): bruno | 2011-03-22 00:58:07 +0100

  • Fix Arch Linux kbd printed in log file now instead of screen.


r4329@localhost (orig r2749): bruno | 2011-03-22 00:59:58 +0100

  • Again increase size for boot env to support better fedora 14 and other latest distros


r4356@localhost (orig r2750): bruno | 2011-03-25 19:30:01 +0100

  • Improve Arch Linux support for mindi tools (mc_at_carreira.com.pt)


r4357@localhost (orig r2751): bruno | 2011-03-25 19:48:33 +0100

  • Accept to work with either wodim or cdreord and genisoimage or mkisofs


r4358@localhost (orig r2752): bruno | 2011-03-25 20:20:49 +0100

  • Documents option -v of mondoarchive


r4359@localhost (orig r2753): bruno | 2011-03-29 01:34:30 +0200

  • Use modprobe instead of insmod to load loop, cd, ... modules (in order to force them at rstore time). Also drop very old aes support for mdk 9.2


r4360@localhost (orig r2754): bruno | 2011-03-29 01:38:49 +0200

  • Arch Linux support for boot message of mindi (mc_at_carreira.com.pt) using /etc/arch-release and pacman.log


r4383@localhost (orig r2755): bruno | 2011-04-07 09:49:38 +0200

  • Adds a UdevadmTrigger? function at restore time to deal with the various syntax formats changes ddepending on version (mc_at_carreira.com.pt)


r4384@localhost (orig r2756): bruno | 2011-04-07 10:05:24 +0200

  • Increase again sizes for Debian 6 support (victor.gattegno_at_hp.com)


r4385@localhost (orig r2757): bruno | 2011-04-07 10:13:00 +0200
Conform to the report of size really !


r4386@localhost (orig r2758): bruno | 2011-04-07 11:31:12 +0200

  • Improved message for Arch Linux for rebuilding the initrd (mc_at_carreira.com.pt)


r4390@localhost (orig r2759): bruno | 2011-04-12 01:15:00 +0200

  • Typo in Web site


r4392@localhost (orig r2761): bruno | 2011-04-14 00:23:38 +0200

  • Adds download references for latest versions.


r4394@localhost (orig r2763): bruno | 2011-04-16 00:35:08 +0200

  • Adds support for MT back mandatory for OBDR.


r4395@localhost (orig r2764): bruno | 2011-04-20 12:18:46 +0200
Function resize_drive_proportionately_to_suit_new_drives

  • Integrate some mr_asprintf
  • Use systematically long long data types in all computation to match the struct mountlist_reference size member and have correct precision
  • Apply JB general_at_itpsg.com patch to make the partition resizing work properly according to the drive size (keeping all calculation in KB and converting only for display)


r4396@localhost (orig r2765): bruno | 2011-04-21 11:20:58 +0200

  • Remove very old code creating confusing symlinks pico to nano in init (patch from MC). Fix #477


r4397@localhost (orig r2766): bruno | 2011-04-21 11:57:44 +0200

  • text's are properly centered in open_and_list_dir (Patch from Chucky). Fix #404.


r4398@localhost (orig r2767): bruno | 2011-04-21 12:49:40 +0200

  • Attempt to fix #432 with a better message at a start.


r4399@localhost (orig r2768): bruno | 2011-04-21 13:00:37 +0200

Log content of mondo-restore.cfg


r4400@localhost (orig r2769): bruno | 2011-04-21 13:48:52 +0200

  • Introduce a new bkpinfo member (subdir) to support storing the subdir under which ISO images are stored on an external HDD. Should fix mostly #433.


r4401@localhost (orig r2770): bruno | 2011-04-21 14:14:52 +0200

  • Fix compilation issues due to level of visibility of functions


r4402@localhost (orig r2771): bruno | 2011-04-22 01:49:03 +0200

  • Adds GUI support for compresion type. Fix half of #468.


r4403@localhost (orig r2772): bruno | 2011-04-22 02:00:07 +0200

  • Adds support of ext attr through the GUI. Fix second part of #468


r4404@localhost (orig r2773): bruno | 2011-04-22 02:22:24 +0200

  • Propose more variables in the mindi.conf example
  • More explicit messages when size unsufficient in mindi for kernel + initrd as per #471
  • Better loging of labeling in mondo when in nuke mode


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