[1] | 1 | /* libmondo-files.c file manipulation
|
---|
[116] | 2 | $Id: libmondo-files.c 1836 2007-12-06 00:17:31Z bruno $
|
---|
[1] | 3 | .
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | 04/16/04
|
---|
| 7 | - find_home_of_exe() really does return NULL now if file not found
|
---|
| 8 |
|
---|
| 9 | 03/22/04
|
---|
| 10 | - added mode_of_file()
|
---|
| 11 |
|
---|
| 12 | 10/02/03
|
---|
| 13 | - cleaned up grab_percentage_from_last_line_of_file()
|
---|
| 14 |
|
---|
| 15 | 09/18
|
---|
| 16 | - added int make_grub_install_scriptlet()
|
---|
| 17 |
|
---|
| 18 | 09/16
|
---|
| 19 | - cleaned up mkisofs feedback window
|
---|
| 20 |
|
---|
| 21 | 09/12
|
---|
| 22 | - fixed Teuton-hostile bug in size_of_all_biggiefiles_K()
|
---|
| 23 |
|
---|
| 24 | 09/05
|
---|
| 25 | - added size_of_partition_in_mountlist_K()
|
---|
| 26 | - size_of_all_biggiefiles_K() now calls get_phys_size_of_drive(fname)
|
---|
| 27 |
|
---|
| 28 | 07/02
|
---|
| 29 | - fixed calls to popup_and_get_string()
|
---|
| 30 |
|
---|
| 31 | 05/19
|
---|
| 32 | - added CP_BIN
|
---|
| 33 |
|
---|
| 34 | 05/05
|
---|
| 35 | - added Joshua Oreman's FreeBSD patches
|
---|
| 36 |
|
---|
| 37 | 05/04
|
---|
| 38 | - find_home_of_exe() now returns NULL if file not found
|
---|
| 39 |
|
---|
| 40 | 04/26
|
---|
| 41 | - if >4 media est'd, say one meeeellion
|
---|
| 42 |
|
---|
| 43 | 04/25
|
---|
| 44 | - fixed minor bug in find_home_of_exe()
|
---|
| 45 |
|
---|
| 46 | 04/24
|
---|
| 47 | - added lots of assert()'s and log_OS_error()'s
|
---|
| 48 |
|
---|
| 49 | 04/07
|
---|
| 50 | - fix find_home_of_exe()
|
---|
| 51 | - cleaned up code a bit
|
---|
| 52 |
|
---|
| 53 | 03/27
|
---|
| 54 | - copy_mondo_and_mindi_stuff --- if _homedir_/payload.tgz exists then untar it to CD
|
---|
| 55 |
|
---|
| 56 | 01/14/2003
|
---|
| 57 | - if backup media type == nfs then don't estimate no. of media reqd
|
---|
| 58 |
|
---|
| 59 | 11/25/2002
|
---|
| 60 | - don't log/echo estimated # of media required if >=50
|
---|
| 61 |
|
---|
| 62 | 10/01 - 11/09
|
---|
| 63 | - chmod uses 0x, not decimal :)
|
---|
| 64 | - added is_this_file_compressed()
|
---|
| 65 | - replace convoluted grep with wc (KP)
|
---|
| 66 |
|
---|
| 67 | 09/01 - 09/30
|
---|
| 68 | - only show "number of media" estimate if no -x
|
---|
| 69 | - run_program_and_log_output() now takes boolean operator to specify
|
---|
| 70 | whether it will log its activities in the event of _success_
|
---|
| 71 |
|
---|
| 72 | 08/01 - 08/31
|
---|
| 73 | - handle unknown media sizes
|
---|
| 74 | - cleaned up some log_it() calls
|
---|
| 75 |
|
---|
| 76 | 07/24
|
---|
| 77 | - created
|
---|
| 78 | */
|
---|
| 79 |
|
---|
| 80 | /**
|
---|
| 81 | * @file
|
---|
| 82 | * Functions to manipulate files.
|
---|
| 83 | */
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | #include "my-stuff.h"
|
---|
| 87 | #include "mondostructures.h"
|
---|
| 88 | #include "libmondo-files.h"
|
---|
| 89 |
|
---|
[541] | 90 | #include "lib-common-externs.h"
|
---|
| 91 |
|
---|
[1] | 92 | #include "libmondo-tools-EXT.h"
|
---|
[541] | 93 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 94 | #include "libmondo-devices-EXT.h"
|
---|
| 95 | #include "libmondo-fork-EXT.h"
|
---|
| 96 | #include "libmondo-string-EXT.h"
|
---|
| 97 |
|
---|
| 98 | /*@unused@*/
|
---|
[116] | 99 | //static char cvsid[] = "$Id: libmondo-files.c 1836 2007-12-06 00:17:31Z bruno $";
|
---|
[1] | 100 |
|
---|
| 101 | extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
|
---|
| 102 |
|
---|
| 103 | extern int g_currentY;
|
---|
| 104 | extern char *g_mondo_home;
|
---|
| 105 |
|
---|
[1645] | 106 | /* Reference to global bkpinfo */
|
---|
| 107 | extern struct s_bkpinfo *bkpinfo;
|
---|
| 108 |
|
---|
[1] | 109 | /**
|
---|
| 110 | * @addtogroup fileGroup
|
---|
| 111 | * @{
|
---|
| 112 | */
|
---|
| 113 | /**
|
---|
| 114 | * Get an md5 checksum of the specified file.
|
---|
| 115 | * @param filename The file to checksum.
|
---|
| 116 | * @return The 32-character ASCII representation of the 128-bit checksum.
|
---|
| 117 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 118 | */
|
---|
[116] | 119 | char *calc_checksum_of_file(char *filename)
|
---|
[1] | 120 | {
|
---|
[116] | 121 | /*@ buffers ***************************************************** */
|
---|
| 122 | static char output[MAX_STR_LEN];
|
---|
| 123 | char command[MAX_STR_LEN * 2];
|
---|
| 124 | char tmp[MAX_STR_LEN];
|
---|
[1] | 125 |
|
---|
[116] | 126 | /*@ pointers **************************************************** */
|
---|
| 127 | char *p;
|
---|
| 128 | FILE *fin;
|
---|
[1] | 129 |
|
---|
[116] | 130 | /*@ initialize pointers ***************************************** */
|
---|
[1] | 131 |
|
---|
[116] | 132 | p = output;
|
---|
[1] | 133 |
|
---|
[116] | 134 | /*@************************************************************** */
|
---|
[1] | 135 |
|
---|
[116] | 136 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 137 | if (does_file_exist(filename)) {
|
---|
| 138 | sprintf(command, "md5sum \"%s\"", filename);
|
---|
| 139 | fin = popen(command, "r");
|
---|
| 140 | if (fin) {
|
---|
| 141 | (void) fgets(output, MAX_STR_LEN, fin);
|
---|
| 142 | p = strchr(output, ' ');
|
---|
| 143 | paranoid_pclose(fin);
|
---|
| 144 | }
|
---|
| 145 | } else {
|
---|
| 146 | sprintf(tmp, "File '%s' not found; cannot calc checksum",
|
---|
| 147 | filename);
|
---|
| 148 | log_it(tmp);
|
---|
[1] | 149 | }
|
---|
[116] | 150 | if (p) {
|
---|
| 151 | *p = '\0';
|
---|
| 152 | }
|
---|
| 153 | return (output);
|
---|
[1] | 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | /**
|
---|
| 158 | * Get a not-quite-unique representation of some of the file's @c stat properties.
|
---|
| 159 | * The returned string has the form <tt>size-mtime-ctime</tt>.
|
---|
| 160 | * @param curr_fname The file to generate the "checksum" for.
|
---|
| 161 | * @return The "checksum".
|
---|
| 162 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 163 | */
|
---|
[116] | 164 | char *calc_file_ugly_minichecksum(char *curr_fname)
|
---|
[1] | 165 | {
|
---|
| 166 |
|
---|
[116] | 167 | /*@ buffers ***************************************************** */
|
---|
| 168 | static char curr_cksum[1000];
|
---|
[1] | 169 |
|
---|
[116] | 170 | /*@ pointers **************************************************** */
|
---|
[1] | 171 |
|
---|
[116] | 172 | /*@ structures ************************************************** */
|
---|
| 173 | struct stat buf;
|
---|
[1] | 174 |
|
---|
[116] | 175 | /*@ initialize data *************************************************** */
|
---|
| 176 | curr_cksum[0] = '\0';
|
---|
[1] | 177 |
|
---|
[116] | 178 | /*@************************************************************** */
|
---|
[1] | 179 |
|
---|
[116] | 180 | assert_string_is_neither_NULL_nor_zerolength(curr_fname);
|
---|
| 181 | if (lstat(curr_fname, &buf)) {
|
---|
| 182 | return (curr_cksum); // empty
|
---|
| 183 | }
|
---|
[1] | 184 |
|
---|
[116] | 185 | sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
|
---|
| 186 | (long) (buf.st_mtime), (long) (buf.st_ctime));
|
---|
| 187 | return (curr_cksum);
|
---|
[1] | 188 | }
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | /**
|
---|
| 193 | * Get the number of lines in @p filename.
|
---|
| 194 | * @param filename The file to count lines in.
|
---|
| 195 | * @return The number of lines in @p filename.
|
---|
| 196 | * @bug This function uses the shell and "wc -l"; it should probably be rewritten in C.
|
---|
| 197 | */
|
---|
[116] | 198 | long count_lines_in_file(char *filename)
|
---|
[1] | 199 | {
|
---|
| 200 |
|
---|
[116] | 201 | /*@ buffers ***************************************************** */
|
---|
| 202 | char command[MAX_STR_LEN * 2];
|
---|
| 203 | char incoming[MAX_STR_LEN];
|
---|
| 204 | char tmp[MAX_STR_LEN];
|
---|
[1] | 205 |
|
---|
[116] | 206 | /*@ long ******************************************************** */
|
---|
| 207 | long noof_lines = -1L;
|
---|
[1] | 208 |
|
---|
[116] | 209 | /*@ pointers **************************************************** */
|
---|
| 210 | FILE *fin;
|
---|
[1] | 211 |
|
---|
[116] | 212 | /*@ initialize [0] to null ******************************************** */
|
---|
| 213 | incoming[0] = '\0';
|
---|
[1] | 214 |
|
---|
[116] | 215 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 216 | if (!does_file_exist(filename)) {
|
---|
| 217 | sprintf(tmp,
|
---|
| 218 | "%s does not exist, so I cannot found the number of lines in it",
|
---|
| 219 | filename);
|
---|
| 220 | log_it(tmp);
|
---|
| 221 | return (0);
|
---|
[1] | 222 | }
|
---|
[116] | 223 | sprintf(command, "cat %s | wc -l", filename);
|
---|
| 224 | if (!does_file_exist(filename)) {
|
---|
| 225 | return (-1);
|
---|
[1] | 226 | }
|
---|
[116] | 227 | fin = popen(command, "r");
|
---|
| 228 | if (fin) {
|
---|
| 229 | if (feof(fin)) {
|
---|
| 230 | noof_lines = 0;
|
---|
| 231 | } else {
|
---|
| 232 | (void) fgets(incoming, MAX_STR_LEN - 1, fin);
|
---|
| 233 | while (strlen(incoming) > 0
|
---|
| 234 | && incoming[strlen(incoming) - 1] < 32) {
|
---|
| 235 | incoming[strlen(incoming) - 1] = '\0';
|
---|
| 236 | }
|
---|
| 237 | noof_lines = atol(incoming);
|
---|
| 238 | }
|
---|
| 239 | paranoid_pclose(fin);
|
---|
| 240 | }
|
---|
| 241 | return (noof_lines);
|
---|
[1] | 242 | }
|
---|
| 243 |
|
---|
| 244 |
|
---|
| 245 | /**
|
---|
| 246 | * Check for existence of given @p filename.
|
---|
| 247 | * @param filename The file to check for.
|
---|
| 248 | * @return TRUE if it exists, FALSE otherwise.
|
---|
| 249 | */
|
---|
[116] | 250 | bool does_file_exist(char *filename)
|
---|
[1] | 251 | {
|
---|
| 252 |
|
---|
[116] | 253 | /*@ structures ************************************************** */
|
---|
| 254 | struct stat buf;
|
---|
[1] | 255 |
|
---|
[116] | 256 | /*@************************************************************** */
|
---|
[1] | 257 |
|
---|
[116] | 258 | assert(filename != NULL);
|
---|
| 259 | // assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 260 | if (lstat(filename, &buf)) {
|
---|
| 261 | log_msg(20, "%s does not exist", filename);
|
---|
| 262 | return (FALSE);
|
---|
| 263 | } else {
|
---|
| 264 | log_msg(20, "%s exists", filename);
|
---|
| 265 | return (TRUE);
|
---|
| 266 | }
|
---|
[1] | 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
| 270 |
|
---|
| 271 |
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | /**
|
---|
| 275 | * Modify @p inout (a file containing a list of files) to only contain files
|
---|
| 276 | * that exist.
|
---|
| 277 | * @param inout The filelist to operate on.
|
---|
| 278 | * @note The original file is renamed beforehand, so it will not be accessible
|
---|
| 279 | * while the modification is in progress.
|
---|
| 280 | */
|
---|
[116] | 281 | void exclude_nonexistent_files(char *inout)
|
---|
[1] | 282 | {
|
---|
[116] | 283 | char infname[MAX_STR_LEN];
|
---|
| 284 | char outfname[MAX_STR_LEN];
|
---|
| 285 | char tmp[MAX_STR_LEN];
|
---|
| 286 | char incoming[MAX_STR_LEN];
|
---|
[1] | 287 |
|
---|
[116] | 288 | /*@ int ********************************************************* */
|
---|
| 289 | int i;
|
---|
[1] | 290 |
|
---|
[116] | 291 | /*@ pointers **************************************************** */
|
---|
| 292 | FILE *fin, *fout;
|
---|
[1] | 293 |
|
---|
| 294 |
|
---|
[116] | 295 | /*@ end vars *********************************************************** */
|
---|
[1] | 296 |
|
---|
[116] | 297 | assert_string_is_neither_NULL_nor_zerolength(inout);
|
---|
| 298 | sprintf(infname, "%s.in", inout);
|
---|
| 299 | sprintf(outfname, "%s", inout);
|
---|
| 300 | sprintf(tmp, "cp -f %s %s", inout, infname);
|
---|
| 301 | run_program_and_log_output(tmp, FALSE);
|
---|
| 302 | if (!(fin = fopen(infname, "r"))) {
|
---|
| 303 | log_OS_error("Unable to openin infname");
|
---|
| 304 | return;
|
---|
[1] | 305 | }
|
---|
[116] | 306 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 307 | log_OS_error("Unable to openout outfname");
|
---|
| 308 | return;
|
---|
[1] | 309 | }
|
---|
[116] | 310 | for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 311 | fgets(incoming, MAX_STR_LEN, fin)) {
|
---|
| 312 | i = strlen(incoming) - 1;
|
---|
| 313 | if (i >= 0 && incoming[i] < 32) {
|
---|
| 314 | incoming[i] = '\0';
|
---|
| 315 | }
|
---|
| 316 | if (does_file_exist(incoming)) {
|
---|
| 317 | fprintf(fout, "%s\n", incoming);
|
---|
| 318 | } else {
|
---|
| 319 | sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming);
|
---|
| 320 | log_it(tmp);
|
---|
| 321 | }
|
---|
[1] | 322 | }
|
---|
[116] | 323 | paranoid_fclose(fout);
|
---|
| 324 | paranoid_fclose(fin);
|
---|
| 325 | unlink(infname);
|
---|
[1] | 326 | }
|
---|
| 327 |
|
---|
| 328 |
|
---|
| 329 |
|
---|
| 330 |
|
---|
| 331 |
|
---|
| 332 |
|
---|
| 333 |
|
---|
| 334 |
|
---|
| 335 |
|
---|
| 336 | /**
|
---|
| 337 | * Attempt to find the user's kernel by calling Mindi.
|
---|
| 338 | * If Mindi can't find the kernel, ask user. If @p kernel is not empty,
|
---|
| 339 | * don't do anything.
|
---|
| 340 | * @param kernel Where to put the found kernel.
|
---|
| 341 | * @return 0 for success, 1 for failure.
|
---|
| 342 | */
|
---|
[116] | 343 | int figure_out_kernel_path_interactively_if_necessary(char *kernel)
|
---|
[1] | 344 | {
|
---|
[116] | 345 | char tmp[MAX_STR_LEN];
|
---|
[392] | 346 | char *command;
|
---|
[1] | 347 |
|
---|
[116] | 348 | if (!kernel[0]) {
|
---|
| 349 | strcpy(kernel,
|
---|
| 350 | call_program_and_get_last_line_of_output
|
---|
| 351 | ("mindi --findkernel 2> /dev/null"));
|
---|
| 352 | }
|
---|
[392] | 353 | // If we didn't get anything back, check whether mindi raised a fatal error
|
---|
| 354 | if (!kernel[0]) {
|
---|
| 355 | malloc_string(command);
|
---|
| 356 | strcpy(command, "grep 'Fatal error' /var/log/mindi.log");
|
---|
| 357 | strcpy(tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 358 | if (strlen(tmp) > 1) {
|
---|
| 359 | popup_and_OK(tmp);
|
---|
| 360 | fatal_error("Mindi gave a fatal error. Please check '/var/log/mindi.log'.");
|
---|
| 361 | }
|
---|
| 362 | paranoid_free(command);
|
---|
| 363 | }
|
---|
[116] | 364 | log_it("Calling Mindi with kernel path of '%s'", kernel);
|
---|
| 365 | while (!kernel[0]) {
|
---|
| 366 | if (!ask_me_yes_or_no
|
---|
[541] | 367 | ("Kernel not found or invalid. Choose another?")) {
|
---|
[116] | 368 | return (1);
|
---|
| 369 | }
|
---|
| 370 | if (!popup_and_get_string
|
---|
[541] | 371 | ("Kernel path",
|
---|
| 372 | "What is the full path and filename of your kernel, please?",
|
---|
[116] | 373 | kernel, MAX_STR_LEN / 4)) {
|
---|
| 374 | fatal_error
|
---|
| 375 | ("Kernel not found. Please specify with the '-k' flag.");
|
---|
| 376 | }
|
---|
| 377 | sprintf(tmp, "User says kernel is at %s", kernel);
|
---|
| 378 | log_it(tmp);
|
---|
| 379 | }
|
---|
| 380 | return (0);
|
---|
[1] | 381 | }
|
---|
| 382 |
|
---|
| 383 |
|
---|
| 384 |
|
---|
| 385 |
|
---|
| 386 |
|
---|
| 387 |
|
---|
| 388 | /**
|
---|
| 389 | * Find location of specified executable in user's PATH.
|
---|
| 390 | * @param fname The basename of the executable to search for (e.g. @c afio).
|
---|
| 391 | * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found.
|
---|
| 392 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 393 | * @bug The checks with @c file and @c dirname seem pointless. If @c incoming is "", then you're calling
|
---|
| 394 | * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts
|
---|
| 395 | * to nothing.
|
---|
| 396 | */
|
---|
[116] | 397 | char *find_home_of_exe(char *fname)
|
---|
[1] | 398 | {
|
---|
[116] | 399 | /*@ buffers ********************* */
|
---|
| 400 | static char output[MAX_STR_LEN];
|
---|
| 401 | char *incoming;
|
---|
| 402 | char *command;
|
---|
[1] | 403 |
|
---|
[116] | 404 | malloc_string(incoming);
|
---|
| 405 | malloc_string(command);
|
---|
| 406 | incoming[0] = '\0';
|
---|
| 407 | /*@******************************* */
|
---|
[1] | 408 |
|
---|
[116] | 409 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 410 | sprintf(command, "which %s 2> /dev/null", fname);
|
---|
| 411 | strcpy(incoming, call_program_and_get_last_line_of_output(command));
|
---|
| 412 | if (incoming[0] == '\0') {
|
---|
| 413 | if (system("which file > /dev/null 2> /dev/null")) {
|
---|
| 414 | paranoid_free(incoming);
|
---|
| 415 | paranoid_free(command);
|
---|
| 416 | output[0] = '\0';
|
---|
| 417 | return (NULL); // forget it :)
|
---|
| 418 | }
|
---|
| 419 | sprintf(command,
|
---|
| 420 | "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null",
|
---|
| 421 | incoming);
|
---|
| 422 | strcpy(incoming,
|
---|
| 423 | call_program_and_get_last_line_of_output(command));
|
---|
| 424 | }
|
---|
| 425 | if (incoming[0] == '\0') // yes, it is == '\0' twice, not once :)
|
---|
[1] | 426 | {
|
---|
[116] | 427 | sprintf(command, "dirname %s 2> /dev/null", incoming);
|
---|
| 428 | strcpy(incoming,
|
---|
| 429 | call_program_and_get_last_line_of_output(command));
|
---|
[1] | 430 | }
|
---|
[116] | 431 | strcpy(output, incoming);
|
---|
| 432 | if (output[0] != '\0' && does_file_exist(output)) {
|
---|
| 433 | log_msg(4, "find_home_of_exe () --- Found %s at %s", fname,
|
---|
| 434 | incoming);
|
---|
| 435 | } else {
|
---|
| 436 | output[0] = '\0';
|
---|
| 437 | log_msg(4, "find_home_of_exe() --- Could not find %s", fname);
|
---|
| 438 | }
|
---|
| 439 | paranoid_free(incoming);
|
---|
| 440 | paranoid_free(command);
|
---|
| 441 | if (!output[0]) {
|
---|
| 442 | return (NULL);
|
---|
| 443 | } else {
|
---|
| 444 | return (output);
|
---|
| 445 | }
|
---|
[1] | 446 | }
|
---|
| 447 |
|
---|
| 448 |
|
---|
| 449 |
|
---|
| 450 |
|
---|
| 451 |
|
---|
| 452 |
|
---|
| 453 |
|
---|
| 454 |
|
---|
| 455 | /**
|
---|
| 456 | * Get the last sequence of digits surrounded by non-digits in the first 32k of
|
---|
| 457 | * a file.
|
---|
| 458 | * @param logfile The file to look in.
|
---|
| 459 | * @return The number found, or 0 if none.
|
---|
| 460 | */
|
---|
[116] | 461 | int get_trackno_from_logfile(char *logfile)
|
---|
[1] | 462 | {
|
---|
| 463 |
|
---|
[116] | 464 | /*@ pointers ********************************************************* */
|
---|
| 465 | FILE *fin;
|
---|
[1] | 466 |
|
---|
[116] | 467 | /*@ int ************************************************************** */
|
---|
| 468 | int trackno = 0;
|
---|
| 469 | size_t len = 0;
|
---|
[1] | 470 |
|
---|
[116] | 471 | /*@ buffer ************************************************************ */
|
---|
| 472 | char datablock[32701];
|
---|
[1] | 473 |
|
---|
[116] | 474 | assert_string_is_neither_NULL_nor_zerolength(logfile);
|
---|
| 475 | if (!(fin = fopen(logfile, "r"))) {
|
---|
| 476 | log_OS_error("Unable to open logfile");
|
---|
| 477 | fatal_error("Unable to open logfile to read trackno");
|
---|
| 478 | }
|
---|
| 479 | len = fread(datablock, 1, 32700, fin);
|
---|
| 480 | paranoid_fclose(fin);
|
---|
| 481 | if (len <= 0) {
|
---|
| 482 | return (0);
|
---|
| 483 | }
|
---|
| 484 | for (; len > 0 && !isdigit(datablock[len - 1]); len--);
|
---|
| 485 | datablock[len--] = '\0';
|
---|
| 486 | for (; len > 0 && isdigit(datablock[len - 1]); len--);
|
---|
| 487 | trackno = atoi(datablock + len);
|
---|
| 488 | /*
|
---|
| 489 | sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno);
|
---|
| 490 | log_it(tmp);
|
---|
| 491 | */
|
---|
| 492 | return (trackno);
|
---|
[1] | 493 | }
|
---|
| 494 |
|
---|
| 495 |
|
---|
| 496 |
|
---|
| 497 |
|
---|
| 498 |
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 | /**
|
---|
| 502 | * Get a percentage from the last line of @p filename. We look for the string
|
---|
| 503 | * "% done" on the last line and, if we find it, grab the number before the last % sign.
|
---|
| 504 | * @param filename The file to get the percentage from.
|
---|
| 505 | * @return The percentage found, or 0 for error.
|
---|
| 506 | */
|
---|
[116] | 507 | int grab_percentage_from_last_line_of_file(char *filename)
|
---|
[1] | 508 | {
|
---|
| 509 |
|
---|
[116] | 510 | /*@ buffers ***************************************************** */
|
---|
| 511 | char tmp[MAX_STR_LEN];
|
---|
| 512 | char lastline[MAX_STR_LEN];
|
---|
| 513 | char command[MAX_STR_LEN];
|
---|
| 514 | /*@ pointers **************************************************** */
|
---|
| 515 | char *p;
|
---|
[1] | 516 |
|
---|
[116] | 517 | /*@ int's ******************************************************* */
|
---|
| 518 | int i;
|
---|
[1] | 519 |
|
---|
[116] | 520 | for (i = NOOF_ERR_LINES - 1;
|
---|
| 521 | i >= 0 && !strstr(err_log_lines[i], "% Done")
|
---|
| 522 | && !strstr(err_log_lines[i], "% done"); i--);
|
---|
| 523 | if (i < 0) {
|
---|
| 524 | sprintf(command,
|
---|
[681] | 525 | "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'",
|
---|
[116] | 526 | filename, '%');
|
---|
| 527 | strcpy(lastline,
|
---|
| 528 | call_program_and_get_last_line_of_output(command));
|
---|
| 529 | if (!lastline[0]) {
|
---|
| 530 | return (0);
|
---|
| 531 | }
|
---|
| 532 | } else {
|
---|
| 533 | strcpy(lastline, err_log_lines[i]);
|
---|
[1] | 534 | }
|
---|
| 535 |
|
---|
[116] | 536 | p = strrchr(lastline, '%');
|
---|
| 537 | if (p) {
|
---|
| 538 | *p = '\0';
|
---|
| 539 | }
|
---|
[1] | 540 | // log_msg(2, "lastline='%s', ", p, lastline);
|
---|
[116] | 541 | if (!p) {
|
---|
| 542 | return (0);
|
---|
| 543 | }
|
---|
| 544 | *p = '\0';
|
---|
| 545 | for (p--; *p != ' ' && p != lastline; p--);
|
---|
| 546 | if (p != lastline) {
|
---|
| 547 | p++;
|
---|
| 548 | }
|
---|
| 549 | i = atoi(p);
|
---|
[1] | 550 |
|
---|
[116] | 551 | sprintf(tmp, "'%s' --> %d", p, i);
|
---|
[1] | 552 | // log_to_screen(tmp);
|
---|
| 553 |
|
---|
[116] | 554 | return (i);
|
---|
[1] | 555 | }
|
---|
| 556 |
|
---|
| 557 |
|
---|
| 558 |
|
---|
| 559 |
|
---|
| 560 |
|
---|
| 561 | /**
|
---|
| 562 | * Return the last line of @p filename.
|
---|
| 563 | * @param filename The file to get the last line of.
|
---|
| 564 | * @return The last line of the file.
|
---|
| 565 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 566 | */
|
---|
[116] | 567 | char *last_line_of_file(char *filename)
|
---|
[1] | 568 | {
|
---|
[116] | 569 | /*@ buffers ***************************************************** */
|
---|
| 570 | static char output[MAX_STR_LEN];
|
---|
| 571 | static char command[MAX_STR_LEN * 2];
|
---|
| 572 | static char tmp[MAX_STR_LEN];
|
---|
[1] | 573 |
|
---|
[116] | 574 | /*@ pointers **************************************************** */
|
---|
| 575 | FILE *fin;
|
---|
[1] | 576 |
|
---|
[116] | 577 | /*@ end vars **************************************************** */
|
---|
[1] | 578 |
|
---|
[116] | 579 | if (!does_file_exist(filename)) {
|
---|
[541] | 580 | sprintf(tmp, "Tring to get last line of nonexistent file (%s)",
|
---|
[116] | 581 | filename);
|
---|
| 582 | log_it(tmp);
|
---|
| 583 | output[0] = '\0';
|
---|
| 584 | return (output);
|
---|
| 585 | }
|
---|
[126] | 586 | sprintf(command, "tail -n1 %s", filename);
|
---|
[116] | 587 | fin = popen(command, "r");
|
---|
| 588 | (void) fgets(output, MAX_STR_LEN, fin);
|
---|
| 589 | paranoid_pclose(fin);
|
---|
| 590 | while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
|
---|
| 591 | output[strlen(output) - 1] = '\0';
|
---|
| 592 | }
|
---|
| 593 | return (output);
|
---|
[1] | 594 | }
|
---|
| 595 |
|
---|
| 596 | /**
|
---|
| 597 | * Get the length of @p filename in bytes.
|
---|
| 598 | * @param filename The file to get the length of.
|
---|
| 599 | * @return The length of the file, or -1 for error.
|
---|
| 600 | */
|
---|
[684] | 601 | off_t length_of_file(char *filename)
|
---|
[1] | 602 | {
|
---|
[116] | 603 | /*@ pointers *************************************************** */
|
---|
| 604 | FILE *fin;
|
---|
[1] | 605 |
|
---|
[116] | 606 | /*@ long long ************************************************* */
|
---|
[684] | 607 | off_t length;
|
---|
[1] | 608 |
|
---|
[116] | 609 | fin = fopen(filename, "r");
|
---|
| 610 | if (!fin) {
|
---|
| 611 | log_it("filename=%s", filename);
|
---|
| 612 | log_OS_error("Unable to openin filename");
|
---|
| 613 | return (-1);
|
---|
| 614 | }
|
---|
[415] | 615 | fseeko(fin, 0, SEEK_END);
|
---|
[684] | 616 | length = ftello(fin);
|
---|
[116] | 617 | paranoid_fclose(fin);
|
---|
| 618 | return (length);
|
---|
[1] | 619 | }
|
---|
| 620 |
|
---|
| 621 |
|
---|
| 622 |
|
---|
| 623 | /**
|
---|
| 624 | * ?????
|
---|
| 625 | * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
|
---|
| 626 | */
|
---|
| 627 | int
|
---|
[116] | 628 | make_checksum_list_file(char *filelist, char *cksumlist, char *comppath)
|
---|
[1] | 629 | {
|
---|
[116] | 630 | /*@ pointers **************************************************** */
|
---|
| 631 | FILE *fin;
|
---|
| 632 | FILE *fout;
|
---|
[1] | 633 |
|
---|
[116] | 634 | /*@ int ******************************************************* */
|
---|
| 635 | int percentage;
|
---|
| 636 | int i;
|
---|
| 637 | int counter = 0;
|
---|
[1] | 638 |
|
---|
[116] | 639 | /*@ buffer ****************************************************** */
|
---|
| 640 | char stub_fname[1000];
|
---|
| 641 | char curr_fname[1000];
|
---|
| 642 | char curr_cksum[1000];
|
---|
| 643 | char tmp[1000];
|
---|
[1] | 644 |
|
---|
[116] | 645 | /*@ long [long] ************************************************* */
|
---|
[684] | 646 | off_t filelist_length;
|
---|
| 647 | off_t curr_pos;
|
---|
[116] | 648 | long start_time;
|
---|
| 649 | long current_time;
|
---|
| 650 | long time_taken;
|
---|
| 651 | long time_remaining;
|
---|
[1] | 652 |
|
---|
[116] | 653 | /*@ end vars *************************************************** */
|
---|
[1] | 654 |
|
---|
[116] | 655 | start_time = get_time();
|
---|
| 656 | filelist_length = length_of_file(filelist);
|
---|
| 657 | sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist);
|
---|
| 658 | log_it(tmp);
|
---|
| 659 | fin = fopen(filelist, "r");
|
---|
| 660 | if (fin == NULL) {
|
---|
| 661 | log_OS_error("Unable to fopen-in filelist");
|
---|
| 662 | log_to_screen("Can't open filelist");
|
---|
| 663 | return (1);
|
---|
[1] | 664 | }
|
---|
[116] | 665 | fout = fopen(cksumlist, "w");
|
---|
| 666 | if (fout == NULL) {
|
---|
| 667 | log_OS_error("Unable to openout cksumlist");
|
---|
| 668 | paranoid_fclose(fin);
|
---|
[541] | 669 | log_to_screen("Can't open checksum list");
|
---|
[116] | 670 | return (1);
|
---|
[1] | 671 | }
|
---|
[116] | 672 | for (fgets(stub_fname, 999, fin); !feof(fin);
|
---|
| 673 | fgets(stub_fname, 999, fin)) {
|
---|
| 674 | if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {
|
---|
| 675 | stub_fname[i] = '\0';
|
---|
| 676 | }
|
---|
| 677 | sprintf(tmp, "%s%s", comppath, stub_fname);
|
---|
| 678 | strcpy(curr_fname, tmp + 1);
|
---|
| 679 | strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname));
|
---|
| 680 | fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
|
---|
| 681 | if (counter++ > 12) {
|
---|
| 682 | current_time = get_time();
|
---|
| 683 | counter = 0;
|
---|
| 684 | curr_fname[37] = '\0';
|
---|
[684] | 685 | curr_pos = ftello(fin) / 1024;
|
---|
[116] | 686 | percentage = (int) (curr_pos * 100 / filelist_length);
|
---|
| 687 | time_taken = current_time - start_time;
|
---|
| 688 | if (percentage == 0) {
|
---|
| 689 | /* printf("%0d%% done \r",percentage); */
|
---|
| 690 | } else {
|
---|
| 691 | time_remaining =
|
---|
| 692 | time_taken * 100 / (long) (percentage) - time_taken;
|
---|
| 693 | sprintf(tmp,
|
---|
[541] | 694 | "%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r",
|
---|
[116] | 695 | percentage, (int) (time_taken / 60),
|
---|
| 696 | (int) (time_taken % 60),
|
---|
| 697 | (int) (time_remaining / 60),
|
---|
| 698 | (int) (time_remaining % 60), curr_fname);
|
---|
| 699 | log_to_screen(tmp);
|
---|
| 700 | }
|
---|
| 701 | sync();
|
---|
| 702 | }
|
---|
| 703 | }
|
---|
| 704 | paranoid_fclose(fout);
|
---|
| 705 | paranoid_fclose(fin);
|
---|
| 706 | log_it("Done.");
|
---|
| 707 | return (0);
|
---|
[1] | 708 | }
|
---|
| 709 |
|
---|
| 710 |
|
---|
| 711 | /**
|
---|
| 712 | * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
|
---|
| 713 | * @param outdir_fname The directory to create.
|
---|
| 714 | * @return The return value of @c mkdir.
|
---|
| 715 | */
|
---|
[116] | 716 | int make_hole_for_dir(char *outdir_fname)
|
---|
[1] | 717 | {
|
---|
[116] | 718 | char tmp[MAX_STR_LEN * 2];
|
---|
| 719 | int res = 0;
|
---|
[1] | 720 |
|
---|
[116] | 721 | assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
|
---|
| 722 | sprintf(tmp, "mkdir -p %s", outdir_fname);
|
---|
| 723 | res = system(tmp);
|
---|
| 724 | return (res);
|
---|
[1] | 725 | }
|
---|
| 726 |
|
---|
| 727 |
|
---|
| 728 | /**
|
---|
| 729 | * Create the parent directories of @p outfile_fname.
|
---|
| 730 | * @param outfile_fname The file to make a "hole" for.
|
---|
| 731 | * @return 0, always.
|
---|
| 732 | * @bug Return value unnecessary.
|
---|
| 733 | */
|
---|
[116] | 734 | int make_hole_for_file(char *outfile_fname)
|
---|
[1] | 735 | {
|
---|
[116] | 736 | /*@ buffer ****************************************************** */
|
---|
| 737 | char command[MAX_STR_LEN * 2];
|
---|
[1] | 738 |
|
---|
[116] | 739 | /*@ int ******************************************************** */
|
---|
| 740 | int res = 0;
|
---|
[1] | 741 |
|
---|
[116] | 742 | /*@ end vars *************************************************** */
|
---|
[1] | 743 |
|
---|
[116] | 744 | assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
|
---|
| 745 | assert(!strstr(outfile_fname, MNT_CDROM));
|
---|
| 746 | assert(!strstr(outfile_fname, "/dev/cdrom"));
|
---|
| 747 | sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
|
---|
| 748 | res += system(command);
|
---|
| 749 | sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
|
---|
| 750 | res += system(command);
|
---|
| 751 | sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
|
---|
| 752 | res += system(command);
|
---|
| 753 | unlink(outfile_fname);
|
---|
| 754 | return (0);
|
---|
[1] | 755 | }
|
---|
| 756 |
|
---|
| 757 |
|
---|
| 758 |
|
---|
| 759 |
|
---|
| 760 | /**
|
---|
| 761 | * Get the number of lines in @p filelist_fname that contain the string @p wildcard.
|
---|
| 762 | * @param filelist_fname The file to search through.
|
---|
| 763 | * @param wildcard The string to search for. This is @e not a shell glob or a regular expression.
|
---|
| 764 | * @return The number of lines matched.
|
---|
| 765 | */
|
---|
[116] | 766 | long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
|
---|
[1] | 767 | {
|
---|
[116] | 768 | /*@ long ******************************************************* */
|
---|
| 769 | long matches = 0;
|
---|
[1] | 770 |
|
---|
[116] | 771 | /*@ pointers *************************************************** */
|
---|
| 772 | FILE *fin;
|
---|
[1] | 773 |
|
---|
[116] | 774 | /*@ buffers **************************************************** */
|
---|
| 775 | char incoming[MAX_STR_LEN];
|
---|
[1] | 776 |
|
---|
[116] | 777 | /*@ end vars *************************************************** */
|
---|
[1] | 778 |
|
---|
| 779 |
|
---|
[116] | 780 | fin = fopen(filelist_fname, "r");
|
---|
[1] | 781 |
|
---|
[116] | 782 | if (!fin) {
|
---|
| 783 | log_OS_error("Unable to openin filelist_fname");
|
---|
| 784 | return (0);
|
---|
[1] | 785 | }
|
---|
[116] | 786 | (void) fgets(incoming, MAX_STR_LEN - 1, fin);
|
---|
| 787 | while (!feof(fin)) {
|
---|
| 788 | if (strstr(incoming, wildcard)) {
|
---|
| 789 | matches++;
|
---|
| 790 | }
|
---|
| 791 | (void) fgets(incoming, MAX_STR_LEN - 1, fin);
|
---|
| 792 | }
|
---|
| 793 | paranoid_fclose(fin);
|
---|
| 794 | return (matches);
|
---|
[1] | 795 | }
|
---|
| 796 |
|
---|
| 797 |
|
---|
| 798 |
|
---|
| 799 |
|
---|
| 800 | /**
|
---|
| 801 | * Register our PID in a file in /var/run.
|
---|
| 802 | * The PID will be put in /var/run/monitas-<tt>name_str</tt>.pid.
|
---|
| 803 | * @param pid 0 to remove file, anything else to create it.
|
---|
| 804 | * @param name_str The basename of the PID file (e.g. "mondo" or "server")
|
---|
| 805 | * @note This function does not provide support against multiple instances, unless you check for that yourself.
|
---|
| 806 | */
|
---|
[116] | 807 | void register_pid(pid_t pid, char *name_str)
|
---|
[1] | 808 | {
|
---|
[116] | 809 | char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1];
|
---|
| 810 | int res;
|
---|
| 811 | FILE *fin;
|
---|
[1] | 812 |
|
---|
[116] | 813 | sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str);
|
---|
| 814 | if (!pid) {
|
---|
| 815 | log_it("Unregistering PID");
|
---|
| 816 | if (unlink(lockfile_fname)) {
|
---|
| 817 | log_it("Error unregistering PID");
|
---|
| 818 | }
|
---|
| 819 | return;
|
---|
| 820 | }
|
---|
| 821 | if (does_file_exist(lockfile_fname)) {
|
---|
| 822 | tmp[0] = '\0';
|
---|
| 823 | if ((fin = fopen(lockfile_fname, "r"))) {
|
---|
| 824 | (void) fgets(tmp, MAX_STR_LEN, fin);
|
---|
| 825 | paranoid_fclose(fin);
|
---|
| 826 | } else {
|
---|
| 827 | log_OS_error("Unable to openin lockfile_fname");
|
---|
| 828 | }
|
---|
| 829 | pid = (pid_t) atol(tmp);
|
---|
| 830 | sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
|
---|
| 831 | res = system(tmp);
|
---|
| 832 | if (!res) {
|
---|
| 833 | log_it
|
---|
| 834 | ("I believe the daemon is already running. If it isn't, please delete %s and try again.",
|
---|
| 835 | lockfile_fname);
|
---|
| 836 | }
|
---|
| 837 | }
|
---|
| 838 | sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
|
---|
| 839 | lockfile_fname);
|
---|
| 840 | if (system(tmp)) {
|
---|
| 841 | fatal_error("Cannot register PID");
|
---|
| 842 | }
|
---|
[1] | 843 | }
|
---|
| 844 |
|
---|
| 845 |
|
---|
| 846 |
|
---|
| 847 | /**
|
---|
| 848 | * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/mountlist.txt.
|
---|
| 849 | * @param tmpdir The tempdir where the mountlist is stored.
|
---|
| 850 | * @param dev The device to search for.
|
---|
| 851 | * @return The size of the partition in KB.
|
---|
| 852 | */
|
---|
[116] | 853 | long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
|
---|
[1] | 854 | {
|
---|
[116] | 855 | char command[MAX_STR_LEN];
|
---|
| 856 | char mountlist[MAX_STR_LEN];
|
---|
| 857 | char sz_res[MAX_STR_LEN];
|
---|
| 858 | long file_len_K;
|
---|
| 859 |
|
---|
| 860 | sprintf(mountlist, "%s/mountlist.txt", tmpdir);
|
---|
| 861 | sprintf(command,
|
---|
[273] | 862 | "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'",
|
---|
| 863 | dev, tmpdir);
|
---|
[116] | 864 | log_it(command);
|
---|
| 865 | strcpy(sz_res, call_program_and_get_last_line_of_output(command));
|
---|
| 866 | file_len_K = atol(sz_res);
|
---|
| 867 | log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
|
---|
| 868 | return (file_len_K);
|
---|
[1] | 869 | }
|
---|
| 870 |
|
---|
| 871 | /**
|
---|
| 872 | * Calculate the total size (in KB) of all the biggiefiles in this backup.
|
---|
| 873 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 874 | * @return The total size of all biggiefiles in KB.
|
---|
| 875 | */
|
---|
[1645] | 876 | long size_of_all_biggiefiles_K()
|
---|
[1] | 877 | {
|
---|
[116] | 878 | /*@ buffers ***************************************************** */
|
---|
| 879 | char *fname;
|
---|
| 880 | char *biggielist;
|
---|
| 881 | char *comment;
|
---|
[296] | 882 | char *tmp;
|
---|
| 883 | char *command;
|
---|
[1] | 884 |
|
---|
[116] | 885 | /*@ long ******************************************************** */
|
---|
| 886 | long scratchL = 0;
|
---|
| 887 | long file_len_K;
|
---|
[1] | 888 |
|
---|
[116] | 889 | /*@ pointers *************************************************** */
|
---|
| 890 | FILE *fin = NULL;
|
---|
[1] | 891 |
|
---|
[116] | 892 | /*@ end vars *************************************************** */
|
---|
[1] | 893 |
|
---|
[116] | 894 | malloc_string(fname);
|
---|
| 895 | malloc_string(biggielist);
|
---|
| 896 | malloc_string(comment);
|
---|
[296] | 897 | malloc_string(tmp);
|
---|
| 898 | malloc_string(command);
|
---|
[116] | 899 | log_it("Calculating size of all biggiefiles (in total)");
|
---|
| 900 | sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
|
---|
| 901 | log_it("biggielist = %s", biggielist);
|
---|
| 902 | if (!(fin = fopen(biggielist, "r"))) {
|
---|
| 903 | log_OS_error
|
---|
| 904 | ("Cannot open biggielist. OK, so estimate is based on filesets only.");
|
---|
| 905 | } else {
|
---|
| 906 | log_msg(4, "Reading it...");
|
---|
| 907 | for (fgets(fname, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 908 | fgets(fname, MAX_STR_LEN, fin)) {
|
---|
| 909 | if (fname[strlen(fname) - 1] <= 32) {
|
---|
| 910 | fname[strlen(fname) - 1] = '\0';
|
---|
| 911 | }
|
---|
| 912 | if (0 == strncmp(fname, "/dev/", 5)) {
|
---|
[296] | 913 | if (is_dev_an_NTFS_dev(fname)) {
|
---|
| 914 | if ( !find_home_of_exe("ntfsresize")) {
|
---|
| 915 | fatal_error("ntfsresize not found");
|
---|
| 916 | }
|
---|
| 917 | sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
|
---|
| 918 | log_it("command = %s", command);
|
---|
| 919 | strcpy (tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 920 | log_it("res of it = %s", tmp);
|
---|
| 921 | file_len_K = atoll(tmp) / 1024L;
|
---|
| 922 | } else {
|
---|
| 923 | file_len_K = get_phys_size_of_drive(fname) * 1024L;
|
---|
| 924 | }
|
---|
[116] | 925 | } else {
|
---|
[684] | 926 | /* BERLIOS: more than long here ??? */
|
---|
[116] | 927 | file_len_K = (long) (length_of_file(fname) / 1024);
|
---|
| 928 | }
|
---|
| 929 | if (file_len_K > 0) {
|
---|
| 930 | scratchL += file_len_K;
|
---|
| 931 | log_msg(4, "%s --> %ld K", fname, file_len_K);
|
---|
| 932 | }
|
---|
| 933 | sprintf(comment,
|
---|
| 934 | "After adding %s, scratchL+%ld now equals %ld", fname,
|
---|
| 935 | file_len_K, scratchL);
|
---|
| 936 | log_msg(4, comment);
|
---|
| 937 | if (feof(fin)) {
|
---|
| 938 | break;
|
---|
| 939 | }
|
---|
| 940 | }
|
---|
[1] | 941 | }
|
---|
[116] | 942 | log_it("Closing...");
|
---|
| 943 | paranoid_fclose(fin);
|
---|
| 944 | log_it("Finished calculating total size of all biggiefiles");
|
---|
| 945 | paranoid_free(fname);
|
---|
| 946 | paranoid_free(biggielist);
|
---|
| 947 | paranoid_free(comment);
|
---|
[296] | 948 | paranoid_free(tmp);
|
---|
| 949 | paranoid_free(command);
|
---|
[116] | 950 | return (scratchL);
|
---|
[1] | 951 | }
|
---|
| 952 |
|
---|
| 953 | /**
|
---|
| 954 | * Determine the amount of space (in KB) occupied by a mounted CD.
|
---|
| 955 | * This can also be used to find the space used for other directories.
|
---|
| 956 | * @param mountpt The mountpoint/directory to check.
|
---|
| 957 | * @return The amount of space occupied in KB.
|
---|
| 958 | */
|
---|
[116] | 959 | long long space_occupied_by_cd(char *mountpt)
|
---|
[1] | 960 | {
|
---|
[116] | 961 | /*@ buffer ****************************************************** */
|
---|
| 962 | char tmp[MAX_STR_LEN];
|
---|
| 963 | char command[MAX_STR_LEN * 2];
|
---|
| 964 | long long llres;
|
---|
| 965 | /*@ pointers **************************************************** */
|
---|
| 966 | char *p;
|
---|
| 967 | FILE *fin;
|
---|
[1] | 968 |
|
---|
[116] | 969 | /*@ end vars *************************************************** */
|
---|
[1] | 970 |
|
---|
[116] | 971 | sprintf(command, "du -sk %s", mountpt);
|
---|
[816] | 972 | errno = 0;
|
---|
[116] | 973 | fin = popen(command, "r");
|
---|
[816] | 974 | if (errno) {
|
---|
| 975 | log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
|
---|
| 976 | llres = 0;
|
---|
| 977 | } else {
|
---|
| 978 | (void) fgets(tmp, MAX_STR_LEN, fin);
|
---|
| 979 | paranoid_pclose(fin);
|
---|
| 980 | p = strchr(tmp, '\t');
|
---|
| 981 | if (p) {
|
---|
| 982 | *p = '\0';
|
---|
| 983 | }
|
---|
| 984 | for (p = tmp, llres = 0; *p != '\0'; p++) {
|
---|
| 985 | llres *= 10;
|
---|
| 986 | llres += (int) (*p - '0');
|
---|
| 987 | }
|
---|
[116] | 988 | }
|
---|
[816] | 989 |
|
---|
[116] | 990 | return (llres);
|
---|
[1] | 991 | }
|
---|
| 992 |
|
---|
| 993 |
|
---|
| 994 | /**
|
---|
| 995 | * Update a CRC checksum to include another character.
|
---|
| 996 | * @param crc The original CRC checksum.
|
---|
| 997 | * @param c The character to add.
|
---|
| 998 | * @return The new CRC checksum.
|
---|
| 999 | * @ingroup utilityGroup
|
---|
| 1000 | */
|
---|
[116] | 1001 | unsigned int updcrc(unsigned int crc, unsigned int c)
|
---|
[1] | 1002 | {
|
---|
[116] | 1003 | unsigned int tmp;
|
---|
| 1004 | tmp = (crc >> 8) ^ c;
|
---|
| 1005 | crc = (crc << 8) ^ crctttab[tmp & 255];
|
---|
| 1006 | return crc;
|
---|
[1] | 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | /**
|
---|
| 1010 | * Update a reverse CRC checksum to include another character.
|
---|
| 1011 | * @param crc The original CRC checksum.
|
---|
| 1012 | * @param c The character to add.
|
---|
| 1013 | * @return The new CRC checksum.
|
---|
| 1014 | * @ingroup utilityGroup
|
---|
| 1015 | */
|
---|
[116] | 1016 | unsigned int updcrcr(unsigned int crc, unsigned int c)
|
---|
[1] | 1017 | {
|
---|
[116] | 1018 | unsigned int tmp;
|
---|
| 1019 | tmp = crc ^ c;
|
---|
| 1020 | crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
|
---|
| 1021 | return crc;
|
---|
[1] | 1022 | }
|
---|
| 1023 |
|
---|
| 1024 |
|
---|
| 1025 |
|
---|
| 1026 |
|
---|
| 1027 | /**
|
---|
| 1028 | * Check for an executable on the user's system; write a message to the
|
---|
| 1029 | * screen and the log if we can't find it.
|
---|
| 1030 | * @param fname The executable basename to look for.
|
---|
| 1031 | * @return 0 if it's found, nonzero if not.
|
---|
| 1032 | */
|
---|
[116] | 1033 | int whine_if_not_found(char *fname)
|
---|
[1] | 1034 | {
|
---|
[116] | 1035 | /*@ buffers *** */
|
---|
| 1036 | char command[MAX_STR_LEN * 2];
|
---|
| 1037 | char errorstr[MAX_STR_LEN];
|
---|
[1] | 1038 |
|
---|
| 1039 |
|
---|
[116] | 1040 | sprintf(command, "which %s > /dev/null 2> /dev/null", fname);
|
---|
| 1041 | sprintf(errorstr,
|
---|
[541] | 1042 | "Please install '%s'. I cannot find it on your system.",
|
---|
[116] | 1043 | fname);
|
---|
| 1044 | if (system(command)) {
|
---|
| 1045 | log_to_screen(errorstr);
|
---|
| 1046 | log_to_screen
|
---|
[541] | 1047 | ("There may be hyperlink at http://www.mondorescue.com which");
|
---|
| 1048 | log_to_screen("will take you to the relevant (missing) package.");
|
---|
[116] | 1049 | return (1);
|
---|
| 1050 | } else {
|
---|
| 1051 | return (0);
|
---|
| 1052 | }
|
---|
[1] | 1053 | }
|
---|
| 1054 |
|
---|
| 1055 |
|
---|
| 1056 |
|
---|
| 1057 |
|
---|
| 1058 |
|
---|
| 1059 |
|
---|
| 1060 | /**
|
---|
| 1061 | * Create a data file at @p fname containing @p contents.
|
---|
| 1062 | * The data actually can be multiple lines, despite the name.
|
---|
| 1063 | * @param fname The file to create.
|
---|
| 1064 | * @param contents The data to put in it.
|
---|
| 1065 | * @return 0 for success, 1 for failure.
|
---|
| 1066 | */
|
---|
[116] | 1067 | int write_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 1068 | {
|
---|
[116] | 1069 | /*@ pointers *************************************************** */
|
---|
| 1070 | FILE *fout;
|
---|
| 1071 | int res = 0;
|
---|
[1] | 1072 |
|
---|
[116] | 1073 | /*@ end vars *************************************************** */
|
---|
[1] | 1074 |
|
---|
[116] | 1075 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 1076 | if (!contents) {
|
---|
| 1077 | log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
|
---|
| 1078 | }
|
---|
| 1079 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 1080 | log_it("fname=%s");
|
---|
| 1081 | log_OS_error("Unable to openout fname");
|
---|
| 1082 | return (1);
|
---|
| 1083 | }
|
---|
| 1084 | fprintf(fout, "%s\n", contents);
|
---|
| 1085 | paranoid_fclose(fout);
|
---|
| 1086 | return (res);
|
---|
[1] | 1087 | }
|
---|
| 1088 |
|
---|
| 1089 |
|
---|
| 1090 |
|
---|
| 1091 | /**
|
---|
| 1092 | * Read @p fname into @p contents.
|
---|
| 1093 | * @param fname The file to read.
|
---|
| 1094 | * @param contents Where to put its contents.
|
---|
| 1095 | * @return 0 for success, nonzero for failure.
|
---|
| 1096 | */
|
---|
[116] | 1097 | int read_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 1098 | {
|
---|
[116] | 1099 | /*@ pointers *************************************************** */
|
---|
| 1100 | FILE *fin;
|
---|
| 1101 | int res = 0;
|
---|
| 1102 | int i;
|
---|
[1] | 1103 |
|
---|
[116] | 1104 | /*@ end vars *************************************************** */
|
---|
[1] | 1105 |
|
---|
[116] | 1106 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 1107 | if (!contents) {
|
---|
| 1108 | log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
|
---|
| 1109 | }
|
---|
| 1110 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 1111 | log_it("fname=%s", fname);
|
---|
| 1112 | log_OS_error("Unable to openin fname");
|
---|
| 1113 | return (1);
|
---|
| 1114 | }
|
---|
| 1115 | fscanf(fin, "%s\n", contents);
|
---|
| 1116 | i = strlen(contents);
|
---|
| 1117 | if (i > 0 && contents[i - 1] < 32) {
|
---|
| 1118 | contents[i - 1] = '\0';
|
---|
| 1119 | }
|
---|
| 1120 | paranoid_fclose(fin);
|
---|
| 1121 | return (res);
|
---|
[1] | 1122 | }
|
---|
| 1123 |
|
---|
| 1124 |
|
---|
| 1125 |
|
---|
| 1126 |
|
---|
| 1127 |
|
---|
| 1128 |
|
---|
| 1129 |
|
---|
| 1130 |
|
---|
| 1131 |
|
---|
| 1132 | /**
|
---|
| 1133 | * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir.
|
---|
| 1134 | * Currently this includes: copy Mondo's home directory to scratchdir, untar "mondo_home/payload.tgz"
|
---|
| 1135 | * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore
|
---|
| 1136 | * and post-nuke.tgz (if it exists) to tmpdir, and run "hostname > scratchdir/HOSTNAME".
|
---|
| 1137 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1138 | * - @c bkpinfo->postnuke_tarball
|
---|
| 1139 | * - @c bkpinfo->scratchdir
|
---|
| 1140 | * - @c bkpinfo->tmpdir
|
---|
| 1141 | */
|
---|
[1645] | 1142 | void copy_mondo_and_mindi_stuff_to_scratchdir()
|
---|
[1] | 1143 | {
|
---|
[116] | 1144 | /*@ Char buffers ** */
|
---|
| 1145 | char command[MAX_STR_LEN * 2];
|
---|
| 1146 | char tmp[MAX_STR_LEN];
|
---|
| 1147 | char old_pwd[MAX_STR_LEN];
|
---|
[1] | 1148 |
|
---|
[116] | 1149 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
| 1150 | "Copying Mondo's core files to the scratch directory");
|
---|
[1] | 1151 |
|
---|
[116] | 1152 | log_msg(4, "g_mondo_home='%s'", g_mondo_home);
|
---|
| 1153 | if (strlen(g_mondo_home) < 2) {
|
---|
| 1154 | find_and_store_mondoarchives_home(g_mondo_home);
|
---|
| 1155 | }
|
---|
| 1156 | sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
|
---|
| 1157 | bkpinfo->scratchdir);
|
---|
[1] | 1158 |
|
---|
[116] | 1159 | log_msg(4, "command = %s", command);
|
---|
| 1160 | if (run_program_and_log_output(command, 1)) {
|
---|
| 1161 | fatal_error("Failed to copy Mondo's stuff to scratchdir");
|
---|
| 1162 | }
|
---|
[1] | 1163 |
|
---|
[116] | 1164 | sprintf(tmp, "%s/payload.tgz", g_mondo_home);
|
---|
| 1165 | if (does_file_exist(tmp)) {
|
---|
| 1166 | log_it("Untarring payload %s to scratchdir %s", tmp,
|
---|
| 1167 | bkpinfo->scratchdir);
|
---|
| 1168 | (void) getcwd(old_pwd, MAX_STR_LEN - 1);
|
---|
| 1169 | chdir(bkpinfo->scratchdir);
|
---|
| 1170 | sprintf(command, "tar -zxvf %s", tmp);
|
---|
| 1171 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1172 | fatal_error("Failed to untar payload");
|
---|
| 1173 | }
|
---|
| 1174 | chdir(old_pwd);
|
---|
| 1175 | }
|
---|
[1] | 1176 |
|
---|
[116] | 1177 | sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
|
---|
| 1178 | bkpinfo->scratchdir);
|
---|
[1] | 1179 |
|
---|
[116] | 1180 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1181 | fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
|
---|
| 1182 | }
|
---|
[1] | 1183 |
|
---|
[116] | 1184 | strcpy(tmp,
|
---|
| 1185 | call_program_and_get_last_line_of_output("which mondorestore"));
|
---|
| 1186 | if (!tmp[0]) {
|
---|
| 1187 | fatal_error
|
---|
| 1188 | ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
|
---|
| 1189 | }
|
---|
| 1190 | sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
|
---|
| 1191 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1192 | fatal_error("Failed to copy mondorestore to tmpdir");
|
---|
| 1193 | }
|
---|
[1] | 1194 |
|
---|
[116] | 1195 | sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
|
---|
| 1196 | paranoid_system(command);
|
---|
[1] | 1197 |
|
---|
[116] | 1198 | if (bkpinfo->postnuke_tarball[0]) {
|
---|
| 1199 | sprintf(command, "cp -f %s %s/post-nuke.tgz",
|
---|
| 1200 | bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
|
---|
| 1201 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1202 | fatal_error("Unable to copy post-nuke tarball to tmpdir");
|
---|
| 1203 | }
|
---|
| 1204 | }
|
---|
[1] | 1205 |
|
---|
| 1206 |
|
---|
[116] | 1207 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
[1] | 1208 | }
|
---|
| 1209 |
|
---|
| 1210 |
|
---|
| 1211 |
|
---|
| 1212 |
|
---|
| 1213 |
|
---|
| 1214 | /**
|
---|
| 1215 | * Store the client's NFS configuration in files to be restored at restore-time.
|
---|
| 1216 | * Assumes that @c bkpinfo->media_type = nfs, but does not check for this.
|
---|
| 1217 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1218 | * - @c nfs_mount
|
---|
| 1219 | * - @c nfs_remote_dir
|
---|
| 1220 | * - @c tmpdir
|
---|
| 1221 | */
|
---|
[1645] | 1222 | void store_nfs_config()
|
---|
[1] | 1223 | {
|
---|
| 1224 |
|
---|
[116] | 1225 | /*@ buffers ******** */
|
---|
| 1226 | char nfs_dev[MAX_STR_LEN];
|
---|
[802] | 1227 | char mac_addr[MAX_STR_LEN];
|
---|
[116] | 1228 | char nfs_mount[MAX_STR_LEN];
|
---|
| 1229 | char nfs_client_ipaddr[MAX_STR_LEN];
|
---|
[148] | 1230 | char nfs_client_netmask[MAX_STR_LEN];
|
---|
[196] | 1231 | char nfs_client_broadcast[MAX_STR_LEN];
|
---|
[148] | 1232 | char nfs_client_defgw[MAX_STR_LEN];
|
---|
[116] | 1233 | char nfs_server_ipaddr[MAX_STR_LEN];
|
---|
| 1234 | char tmp[MAX_STR_LEN];
|
---|
| 1235 | char command[MAX_STR_LEN * 2];
|
---|
[1] | 1236 |
|
---|
[116] | 1237 | /*@ pointers ***** */
|
---|
| 1238 | char *p;
|
---|
[1] | 1239 |
|
---|
[116] | 1240 | log_it("Storing NFS configuration");
|
---|
| 1241 | strcpy(tmp, bkpinfo->nfs_mount);
|
---|
| 1242 | p = strchr(tmp, ':');
|
---|
| 1243 | if (!p) {
|
---|
| 1244 | fatal_error
|
---|
| 1245 | ("NFS mount doesn't have a colon in it, e.g. 192.168.1.4:/home/nfs");
|
---|
| 1246 | }
|
---|
| 1247 | *(p++) = '\0';
|
---|
| 1248 | strcpy(nfs_server_ipaddr, tmp);
|
---|
| 1249 | strcpy(nfs_mount, p);
|
---|
[1236] | 1250 |
|
---|
[802] | 1251 | /* BERLIOS : there is a bug #67 here as it only considers the first NIC */
|
---|
[116] | 1252 | sprintf(command,
|
---|
| 1253 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
|
---|
| 1254 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
|
---|
| 1255 | sprintf(command,
|
---|
| 1256 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
|
---|
| 1257 | strcpy(nfs_client_ipaddr,
|
---|
| 1258 | call_program_and_get_last_line_of_output(command));
|
---|
[148] | 1259 | sprintf(command,
|
---|
| 1260 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
|
---|
| 1261 | strcpy(nfs_client_netmask,
|
---|
| 1262 | call_program_and_get_last_line_of_output(command));
|
---|
| 1263 | sprintf(command,
|
---|
[196] | 1264 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
|
---|
| 1265 | strcpy(nfs_client_broadcast,
|
---|
| 1266 | call_program_and_get_last_line_of_output(command));
|
---|
| 1267 | sprintf(command,
|
---|
[222] | 1268 | "route -n | grep '^0.0.0.0' | awk '{print $2}'");
|
---|
[148] | 1269 | strcpy(nfs_client_defgw,
|
---|
| 1270 | call_program_and_get_last_line_of_output(command));
|
---|
[116] | 1271 | sprintf(tmp,
|
---|
| 1272 | "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s",
|
---|
| 1273 | nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount);
|
---|
| 1274 | if (strlen(nfs_dev) < 2) {
|
---|
| 1275 | fatal_error
|
---|
| 1276 | ("Unable to find ethN (eth0, eth1, ...) adapter via NFS mount you specified.");
|
---|
| 1277 | }
|
---|
[802] | 1278 | /********
|
---|
| 1279 | * If the NFS device that found above is a bonded device,
|
---|
| 1280 | * we need to replace it with an ethN device or the
|
---|
| 1281 | * networking will not start during an NFS restore.
|
---|
| 1282 | *
|
---|
[1836] | 1283 | * If the NFS device in nfs_dev begins with the word "bond", or alb or aft
|
---|
[802] | 1284 | * look for the corresponding slave ethN device and copy it to nfs_dev.
|
---|
| 1285 | * Using the common MAC address
|
---|
| 1286 | ********/
|
---|
[1836] | 1287 | if (!strncmp(nfs_dev, "bond", 4) || !strncmp(nfs_dev, "alb", 3) || !strncmp(nfs_dev, "aft", 3)) {
|
---|
[802] | 1288 | log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", nfs_dev);
|
---|
| 1289 | sprintf(command,
|
---|
[888] | 1290 | "ifconfig %s | awk '{print $5}' | head -n1", nfs_dev);
|
---|
[802] | 1291 | strcpy(mac_addr, call_program_and_get_last_line_of_output(command));
|
---|
| 1292 | sprintf(command,
|
---|
[888] | 1293 | "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr,nfs_dev);
|
---|
[802] | 1294 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
|
---|
| 1295 | log_to_screen("Replacing it with %s\n", nfs_dev);
|
---|
| 1296 | }
|
---|
[1] | 1297 |
|
---|
[116] | 1298 | sprintf(tmp, "%s/NFS-DEV", bkpinfo->tmpdir);
|
---|
| 1299 | write_one_liner_data_file(tmp, nfs_dev);
|
---|
[1] | 1300 |
|
---|
[116] | 1301 | sprintf(tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir);
|
---|
| 1302 | write_one_liner_data_file(tmp, nfs_client_ipaddr);
|
---|
[148] | 1303 | sprintf(tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir);
|
---|
| 1304 | write_one_liner_data_file(tmp, nfs_client_netmask);
|
---|
[196] | 1305 | sprintf(tmp, "%s/NFS-CLIENT-BROADCAST", bkpinfo->tmpdir);
|
---|
| 1306 | write_one_liner_data_file(tmp, nfs_client_broadcast);
|
---|
[148] | 1307 | sprintf(tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir);
|
---|
| 1308 | write_one_liner_data_file(tmp, nfs_client_defgw);
|
---|
[116] | 1309 | sprintf(tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir);
|
---|
| 1310 | write_one_liner_data_file(tmp, nfs_server_ipaddr);
|
---|
| 1311 | sprintf(tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir);
|
---|
| 1312 | write_one_liner_data_file(tmp, bkpinfo->nfs_mount);
|
---|
| 1313 | sprintf(tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir);
|
---|
| 1314 | write_one_liner_data_file(tmp, bkpinfo->nfs_remote_dir);
|
---|
[148] | 1315 | sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
|
---|
| 1316 | write_one_liner_data_file(tmp, bkpinfo->prefix);
|
---|
[116] | 1317 | log_it("Finished storing NFS configuration");
|
---|
[1] | 1318 | }
|
---|
| 1319 |
|
---|
| 1320 |
|
---|
| 1321 |
|
---|
| 1322 |
|
---|
| 1323 |
|
---|
| 1324 |
|
---|
| 1325 | /**
|
---|
| 1326 | * Determine the approximate number of media that the backup will take up,
|
---|
| 1327 | * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K()
|
---|
| 1328 | * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as
|
---|
| 1329 | * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any
|
---|
| 1330 | * "imagedevs", the estimate is not shown as it will be wildly inaccurate.
|
---|
| 1331 | * If there are more than 50 media estimated, the estimate will not be shown.
|
---|
| 1332 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1333 | * - @c bkpinfo->backup_media_type
|
---|
| 1334 | * - @c bkpinfo->image_devs
|
---|
| 1335 | * - @c bkpinfo->media_size
|
---|
| 1336 | * - @c bkpinfo->optimal_set_size
|
---|
| 1337 | * - @c bkpinfo->use_lzo
|
---|
| 1338 | * @param noof_sets The number of filesets created.
|
---|
| 1339 | * @ingroup archiveGroup
|
---|
| 1340 | */
|
---|
| 1341 | void
|
---|
[1645] | 1342 | estimate_noof_media_required(long noof_sets)
|
---|
[1] | 1343 | {
|
---|
[116] | 1344 | /*@ buffers *************** */
|
---|
| 1345 | char tmp[MAX_STR_LEN];
|
---|
[1] | 1346 |
|
---|
[116] | 1347 | /*@ long long ************* */
|
---|
| 1348 | long long scratchLL;
|
---|
[1] | 1349 |
|
---|
[1505] | 1350 | if (bkpinfo->media_size[1] <= 0) {
|
---|
[116] | 1351 | log_to_screen("Number of media required: UNKNOWN");
|
---|
| 1352 | return;
|
---|
| 1353 | }
|
---|
[1] | 1354 |
|
---|
[116] | 1355 | log_it("Estimating number of media required...");
|
---|
| 1356 | scratchLL =
|
---|
| 1357 | (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size)
|
---|
[1645] | 1358 | + (long long) (size_of_all_biggiefiles_K());
|
---|
[116] | 1359 | scratchLL = (scratchLL / 1024) / bkpinfo->media_size[1];
|
---|
| 1360 | scratchLL++;
|
---|
| 1361 | if (bkpinfo->use_lzo) {
|
---|
| 1362 | scratchLL = (scratchLL * 2) / 3;
|
---|
[1002] | 1363 | } else if (bkpinfo->use_gzip) {
|
---|
[998] | 1364 | scratchLL = (scratchLL * 2) / 3;
|
---|
[116] | 1365 | } else {
|
---|
| 1366 | scratchLL = scratchLL / 2;
|
---|
| 1367 | }
|
---|
| 1368 | if (!scratchLL) {
|
---|
| 1369 | scratchLL++;
|
---|
| 1370 | }
|
---|
| 1371 | if (scratchLL <= 1) {
|
---|
| 1372 | sprintf(tmp,
|
---|
[541] | 1373 | "Your backup will probably occupy a single %s. Maybe two.",
|
---|
[126] | 1374 | media_descriptor_string(bkpinfo->backup_media_type));
|
---|
[116] | 1375 | } else if (scratchLL > 4) {
|
---|
| 1376 | sprintf(tmp,
|
---|
[541] | 1377 | "Your backup will occupy one meeeeellion media! (maybe %s)",
|
---|
[116] | 1378 | number_to_text((int) (scratchLL + 1)));
|
---|
| 1379 | } else {
|
---|
[541] | 1380 | sprintf(tmp, "Your backup will occupy approximately %s media.",
|
---|
[116] | 1381 | number_to_text((int) (scratchLL + 1)));
|
---|
| 1382 | }
|
---|
| 1383 | if (!bkpinfo->image_devs[0] && (scratchLL < 50)) {
|
---|
| 1384 | log_to_screen(tmp);
|
---|
| 1385 | }
|
---|
[1] | 1386 | }
|
---|
| 1387 |
|
---|
| 1388 |
|
---|
| 1389 | /**
|
---|
| 1390 | * Get the last suffix of @p instr.
|
---|
| 1391 | * If @p instr was "httpd.log.gz", we would return "gz".
|
---|
| 1392 | * @param instr The filename to get the suffix of.
|
---|
| 1393 | * @return The suffix (without a dot), or "" if none.
|
---|
| 1394 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 1395 | */
|
---|
[116] | 1396 | char *sz_last_suffix(char *instr)
|
---|
[1] | 1397 | {
|
---|
[116] | 1398 | static char outstr[MAX_STR_LEN];
|
---|
| 1399 | char *p;
|
---|
[1] | 1400 |
|
---|
[116] | 1401 | p = strrchr(instr, '.');
|
---|
| 1402 | if (!p) {
|
---|
| 1403 | outstr[0] = '\0';
|
---|
| 1404 | } else {
|
---|
| 1405 | strcpy(outstr, p);
|
---|
| 1406 | }
|
---|
| 1407 | return (outstr);
|
---|
[1] | 1408 | }
|
---|
| 1409 |
|
---|
| 1410 |
|
---|
| 1411 | /**
|
---|
| 1412 | * Determine whether a file is compressed. This is done
|
---|
| 1413 | * by reading through the "do-not-compress-these" file distributed with Mondo.
|
---|
| 1414 | * @param filename The file to check.
|
---|
| 1415 | * @return TRUE if it's compressed, FALSE if not.
|
---|
| 1416 | */
|
---|
[116] | 1417 | bool is_this_file_compressed(char *filename)
|
---|
[1] | 1418 | {
|
---|
[116] | 1419 | char do_not_compress_these[MAX_STR_LEN];
|
---|
| 1420 | char tmp[MAX_STR_LEN];
|
---|
| 1421 | char *p;
|
---|
[1236] | 1422 | char *q = NULL;
|
---|
[1] | 1423 |
|
---|
[1236] | 1424 | q = strrchr(filename, '.');
|
---|
| 1425 | if (q == NULL) {
|
---|
| 1426 | return (FALSE);
|
---|
| 1427 | }
|
---|
| 1428 |
|
---|
[116] | 1429 | sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
|
---|
| 1430 | if (!does_file_exist(tmp)) {
|
---|
| 1431 | return (FALSE);
|
---|
| 1432 | }
|
---|
[1236] | 1433 | /* BERLIOS: This is just plain WRONG !! */
|
---|
| 1434 | strcpy(do_not_compress_these,last_line_of_file(tmp));
|
---|
| 1435 |
|
---|
[116] | 1436 | for (p = do_not_compress_these; p != NULL; p++) {
|
---|
| 1437 | strcpy(tmp, p);
|
---|
| 1438 | if (strchr(tmp, ' ')) {
|
---|
| 1439 | *(strchr(tmp, ' ')) = '\0';
|
---|
| 1440 | }
|
---|
[1236] | 1441 | if (!strcmp(q, tmp)) {
|
---|
[116] | 1442 | return (TRUE);
|
---|
| 1443 | }
|
---|
| 1444 | if (!(p = strchr(p, ' '))) {
|
---|
| 1445 | break;
|
---|
| 1446 | }
|
---|
| 1447 | }
|
---|
| 1448 | return (FALSE);
|
---|
[1] | 1449 | }
|
---|
| 1450 |
|
---|
| 1451 |
|
---|
| 1452 |
|
---|
[116] | 1453 | int mode_of_file(char *fname)
|
---|
[1] | 1454 | {
|
---|
[116] | 1455 | struct stat buf;
|
---|
[1] | 1456 |
|
---|
[116] | 1457 | if (lstat(fname, &buf)) {
|
---|
| 1458 | return (-1);
|
---|
| 1459 | } // error
|
---|
| 1460 | else {
|
---|
| 1461 | return (buf.st_mode);
|
---|
| 1462 | }
|
---|
[1] | 1463 | }
|
---|
| 1464 |
|
---|
| 1465 |
|
---|
| 1466 |
|
---|
| 1467 |
|
---|
| 1468 | /**
|
---|
| 1469 | * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks.
|
---|
| 1470 | * @param outfile Where to put the script.
|
---|
| 1471 | * @return 0 for success, 1 for failure.
|
---|
| 1472 | */
|
---|
[116] | 1473 | int make_grub_install_scriptlet(char *outfile)
|
---|
[1] | 1474 | {
|
---|
[116] | 1475 | FILE *fout;
|
---|
| 1476 | char *tmp;
|
---|
| 1477 | int retval = 0;
|
---|
[1] | 1478 |
|
---|
[116] | 1479 | malloc_string(tmp);
|
---|
| 1480 | if ((fout = fopen(outfile, "w"))) {
|
---|
| 1481 | fprintf(fout,
|
---|
| 1482 | "#!/bin/sh\n\nmount /boot > /dev/null 2> /dev/null\ngrub-install $@\nres=$?\nsync;sync;sync\nexit $res\n");
|
---|
| 1483 | paranoid_fclose(fout);
|
---|
| 1484 | log_msg(2, "Created %s", outfile);
|
---|
| 1485 | sprintf(tmp, "chmod +x %s", outfile);
|
---|
| 1486 | paranoid_system(tmp);
|
---|
| 1487 | retval = 0;
|
---|
| 1488 | } else {
|
---|
| 1489 | retval = 1;
|
---|
[1] | 1490 | }
|
---|
[116] | 1491 | paranoid_free(tmp);
|
---|
| 1492 | return (retval);
|
---|
[1] | 1493 | }
|
---|
| 1494 |
|
---|
| 1495 | /* @} - end fileGroup */
|
---|