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