[1] | 1 | /* libmondo-files.c file manipulation
|
---|
[116] | 2 | $Id: libmondo-files.c 2048 2008-10-22 16:53:57Z 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 2048 2008-10-22 16:53:57Z 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 | * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/mountlist.txt.
|
---|
| 801 | * @param tmpdir The tempdir where the mountlist is stored.
|
---|
| 802 | * @param dev The device to search for.
|
---|
| 803 | * @return The size of the partition in KB.
|
---|
| 804 | */
|
---|
[116] | 805 | long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
|
---|
[1] | 806 | {
|
---|
[116] | 807 | char command[MAX_STR_LEN];
|
---|
| 808 | char mountlist[MAX_STR_LEN];
|
---|
| 809 | char sz_res[MAX_STR_LEN];
|
---|
| 810 | long file_len_K;
|
---|
| 811 |
|
---|
| 812 | sprintf(mountlist, "%s/mountlist.txt", tmpdir);
|
---|
| 813 | sprintf(command,
|
---|
[273] | 814 | "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'",
|
---|
| 815 | dev, tmpdir);
|
---|
[116] | 816 | log_it(command);
|
---|
| 817 | strcpy(sz_res, call_program_and_get_last_line_of_output(command));
|
---|
| 818 | file_len_K = atol(sz_res);
|
---|
| 819 | log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
|
---|
| 820 | return (file_len_K);
|
---|
[1] | 821 | }
|
---|
| 822 |
|
---|
| 823 | /**
|
---|
| 824 | * Calculate the total size (in KB) of all the biggiefiles in this backup.
|
---|
| 825 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 826 | * @return The total size of all biggiefiles in KB.
|
---|
| 827 | */
|
---|
[1645] | 828 | long size_of_all_biggiefiles_K()
|
---|
[1] | 829 | {
|
---|
[116] | 830 | /*@ buffers ***************************************************** */
|
---|
| 831 | char *fname;
|
---|
| 832 | char *biggielist;
|
---|
| 833 | char *comment;
|
---|
[296] | 834 | char *tmp;
|
---|
| 835 | char *command;
|
---|
[1] | 836 |
|
---|
[116] | 837 | /*@ long ******************************************************** */
|
---|
| 838 | long scratchL = 0;
|
---|
| 839 | long file_len_K;
|
---|
[1] | 840 |
|
---|
[116] | 841 | /*@ pointers *************************************************** */
|
---|
| 842 | FILE *fin = NULL;
|
---|
[1] | 843 |
|
---|
[116] | 844 | /*@ end vars *************************************************** */
|
---|
[1] | 845 |
|
---|
[116] | 846 | malloc_string(fname);
|
---|
| 847 | malloc_string(biggielist);
|
---|
| 848 | malloc_string(comment);
|
---|
[296] | 849 | malloc_string(tmp);
|
---|
| 850 | malloc_string(command);
|
---|
[116] | 851 | log_it("Calculating size of all biggiefiles (in total)");
|
---|
| 852 | sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
|
---|
| 853 | log_it("biggielist = %s", biggielist);
|
---|
| 854 | if (!(fin = fopen(biggielist, "r"))) {
|
---|
| 855 | log_OS_error
|
---|
| 856 | ("Cannot open biggielist. OK, so estimate is based on filesets only.");
|
---|
| 857 | } else {
|
---|
| 858 | log_msg(4, "Reading it...");
|
---|
| 859 | for (fgets(fname, MAX_STR_LEN, fin); !feof(fin);
|
---|
| 860 | fgets(fname, MAX_STR_LEN, fin)) {
|
---|
| 861 | if (fname[strlen(fname) - 1] <= 32) {
|
---|
| 862 | fname[strlen(fname) - 1] = '\0';
|
---|
| 863 | }
|
---|
| 864 | if (0 == strncmp(fname, "/dev/", 5)) {
|
---|
[296] | 865 | if (is_dev_an_NTFS_dev(fname)) {
|
---|
| 866 | if ( !find_home_of_exe("ntfsresize")) {
|
---|
| 867 | fatal_error("ntfsresize not found");
|
---|
| 868 | }
|
---|
| 869 | sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
|
---|
| 870 | log_it("command = %s", command);
|
---|
| 871 | strcpy (tmp, call_program_and_get_last_line_of_output(command));
|
---|
| 872 | log_it("res of it = %s", tmp);
|
---|
| 873 | file_len_K = atoll(tmp) / 1024L;
|
---|
| 874 | } else {
|
---|
| 875 | file_len_K = get_phys_size_of_drive(fname) * 1024L;
|
---|
| 876 | }
|
---|
[116] | 877 | } else {
|
---|
[684] | 878 | /* BERLIOS: more than long here ??? */
|
---|
[116] | 879 | file_len_K = (long) (length_of_file(fname) / 1024);
|
---|
| 880 | }
|
---|
| 881 | if (file_len_K > 0) {
|
---|
| 882 | scratchL += file_len_K;
|
---|
| 883 | log_msg(4, "%s --> %ld K", fname, file_len_K);
|
---|
| 884 | }
|
---|
| 885 | sprintf(comment,
|
---|
| 886 | "After adding %s, scratchL+%ld now equals %ld", fname,
|
---|
| 887 | file_len_K, scratchL);
|
---|
| 888 | log_msg(4, comment);
|
---|
| 889 | if (feof(fin)) {
|
---|
| 890 | break;
|
---|
| 891 | }
|
---|
| 892 | }
|
---|
[1] | 893 | }
|
---|
[116] | 894 | log_it("Closing...");
|
---|
| 895 | paranoid_fclose(fin);
|
---|
| 896 | log_it("Finished calculating total size of all biggiefiles");
|
---|
| 897 | paranoid_free(fname);
|
---|
| 898 | paranoid_free(biggielist);
|
---|
| 899 | paranoid_free(comment);
|
---|
[296] | 900 | paranoid_free(tmp);
|
---|
| 901 | paranoid_free(command);
|
---|
[116] | 902 | return (scratchL);
|
---|
[1] | 903 | }
|
---|
| 904 |
|
---|
| 905 | /**
|
---|
| 906 | * Determine the amount of space (in KB) occupied by a mounted CD.
|
---|
| 907 | * This can also be used to find the space used for other directories.
|
---|
| 908 | * @param mountpt The mountpoint/directory to check.
|
---|
| 909 | * @return The amount of space occupied in KB.
|
---|
| 910 | */
|
---|
[116] | 911 | long long space_occupied_by_cd(char *mountpt)
|
---|
[1] | 912 | {
|
---|
[116] | 913 | /*@ buffer ****************************************************** */
|
---|
| 914 | char tmp[MAX_STR_LEN];
|
---|
| 915 | char command[MAX_STR_LEN * 2];
|
---|
| 916 | long long llres;
|
---|
| 917 | /*@ pointers **************************************************** */
|
---|
| 918 | char *p;
|
---|
| 919 | FILE *fin;
|
---|
[1] | 920 |
|
---|
[116] | 921 | /*@ end vars *************************************************** */
|
---|
[1] | 922 |
|
---|
[116] | 923 | sprintf(command, "du -sk %s", mountpt);
|
---|
[816] | 924 | errno = 0;
|
---|
[116] | 925 | fin = popen(command, "r");
|
---|
[816] | 926 | if (errno) {
|
---|
| 927 | log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
|
---|
| 928 | llres = 0;
|
---|
| 929 | } else {
|
---|
| 930 | (void) fgets(tmp, MAX_STR_LEN, fin);
|
---|
| 931 | paranoid_pclose(fin);
|
---|
| 932 | p = strchr(tmp, '\t');
|
---|
| 933 | if (p) {
|
---|
| 934 | *p = '\0';
|
---|
| 935 | }
|
---|
| 936 | for (p = tmp, llres = 0; *p != '\0'; p++) {
|
---|
| 937 | llres *= 10;
|
---|
| 938 | llres += (int) (*p - '0');
|
---|
| 939 | }
|
---|
[116] | 940 | }
|
---|
[816] | 941 |
|
---|
[116] | 942 | return (llres);
|
---|
[1] | 943 | }
|
---|
| 944 |
|
---|
| 945 |
|
---|
| 946 | /**
|
---|
| 947 | * Update a CRC checksum to include another character.
|
---|
| 948 | * @param crc The original CRC checksum.
|
---|
| 949 | * @param c The character to add.
|
---|
| 950 | * @return The new CRC checksum.
|
---|
| 951 | * @ingroup utilityGroup
|
---|
| 952 | */
|
---|
[116] | 953 | unsigned int updcrc(unsigned int crc, unsigned int c)
|
---|
[1] | 954 | {
|
---|
[116] | 955 | unsigned int tmp;
|
---|
| 956 | tmp = (crc >> 8) ^ c;
|
---|
| 957 | crc = (crc << 8) ^ crctttab[tmp & 255];
|
---|
| 958 | return crc;
|
---|
[1] | 959 | }
|
---|
| 960 |
|
---|
| 961 | /**
|
---|
| 962 | * Update a reverse CRC checksum to include another character.
|
---|
| 963 | * @param crc The original CRC checksum.
|
---|
| 964 | * @param c The character to add.
|
---|
| 965 | * @return The new CRC checksum.
|
---|
| 966 | * @ingroup utilityGroup
|
---|
| 967 | */
|
---|
[116] | 968 | unsigned int updcrcr(unsigned int crc, unsigned int c)
|
---|
[1] | 969 | {
|
---|
[116] | 970 | unsigned int tmp;
|
---|
| 971 | tmp = crc ^ c;
|
---|
| 972 | crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
|
---|
| 973 | return crc;
|
---|
[1] | 974 | }
|
---|
| 975 |
|
---|
| 976 |
|
---|
| 977 |
|
---|
| 978 |
|
---|
| 979 | /**
|
---|
| 980 | * Check for an executable on the user's system; write a message to the
|
---|
| 981 | * screen and the log if we can't find it.
|
---|
| 982 | * @param fname The executable basename to look for.
|
---|
| 983 | * @return 0 if it's found, nonzero if not.
|
---|
| 984 | */
|
---|
[116] | 985 | int whine_if_not_found(char *fname)
|
---|
[1] | 986 | {
|
---|
[116] | 987 | /*@ buffers *** */
|
---|
| 988 | char command[MAX_STR_LEN * 2];
|
---|
| 989 | char errorstr[MAX_STR_LEN];
|
---|
[1] | 990 |
|
---|
| 991 |
|
---|
[116] | 992 | sprintf(command, "which %s > /dev/null 2> /dev/null", fname);
|
---|
| 993 | sprintf(errorstr,
|
---|
[541] | 994 | "Please install '%s'. I cannot find it on your system.",
|
---|
[116] | 995 | fname);
|
---|
| 996 | if (system(command)) {
|
---|
| 997 | log_to_screen(errorstr);
|
---|
| 998 | log_to_screen
|
---|
[541] | 999 | ("There may be hyperlink at http://www.mondorescue.com which");
|
---|
| 1000 | log_to_screen("will take you to the relevant (missing) package.");
|
---|
[116] | 1001 | return (1);
|
---|
| 1002 | } else {
|
---|
| 1003 | return (0);
|
---|
| 1004 | }
|
---|
[1] | 1005 | }
|
---|
| 1006 |
|
---|
| 1007 |
|
---|
| 1008 |
|
---|
| 1009 |
|
---|
| 1010 |
|
---|
| 1011 |
|
---|
| 1012 | /**
|
---|
| 1013 | * Create a data file at @p fname containing @p contents.
|
---|
| 1014 | * The data actually can be multiple lines, despite the name.
|
---|
| 1015 | * @param fname The file to create.
|
---|
| 1016 | * @param contents The data to put in it.
|
---|
| 1017 | * @return 0 for success, 1 for failure.
|
---|
| 1018 | */
|
---|
[116] | 1019 | int write_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 1020 | {
|
---|
[116] | 1021 | /*@ pointers *************************************************** */
|
---|
| 1022 | FILE *fout;
|
---|
| 1023 | int res = 0;
|
---|
[1] | 1024 |
|
---|
[116] | 1025 | /*@ end vars *************************************************** */
|
---|
[1] | 1026 |
|
---|
[116] | 1027 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 1028 | if (!contents) {
|
---|
| 1029 | log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
|
---|
| 1030 | }
|
---|
| 1031 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 1032 | log_it("fname=%s");
|
---|
| 1033 | log_OS_error("Unable to openout fname");
|
---|
| 1034 | return (1);
|
---|
| 1035 | }
|
---|
| 1036 | fprintf(fout, "%s\n", contents);
|
---|
| 1037 | paranoid_fclose(fout);
|
---|
| 1038 | return (res);
|
---|
[1] | 1039 | }
|
---|
| 1040 |
|
---|
| 1041 |
|
---|
| 1042 |
|
---|
| 1043 | /**
|
---|
| 1044 | * Read @p fname into @p contents.
|
---|
| 1045 | * @param fname The file to read.
|
---|
| 1046 | * @param contents Where to put its contents.
|
---|
| 1047 | * @return 0 for success, nonzero for failure.
|
---|
| 1048 | */
|
---|
[116] | 1049 | int read_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 1050 | {
|
---|
[116] | 1051 | /*@ pointers *************************************************** */
|
---|
| 1052 | FILE *fin;
|
---|
| 1053 | int res = 0;
|
---|
| 1054 | int i;
|
---|
[1] | 1055 |
|
---|
[116] | 1056 | /*@ end vars *************************************************** */
|
---|
[1] | 1057 |
|
---|
[116] | 1058 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 1059 | if (!contents) {
|
---|
| 1060 | log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
|
---|
| 1061 | }
|
---|
| 1062 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 1063 | log_it("fname=%s", fname);
|
---|
| 1064 | log_OS_error("Unable to openin fname");
|
---|
| 1065 | return (1);
|
---|
| 1066 | }
|
---|
| 1067 | fscanf(fin, "%s\n", contents);
|
---|
| 1068 | i = strlen(contents);
|
---|
| 1069 | if (i > 0 && contents[i - 1] < 32) {
|
---|
| 1070 | contents[i - 1] = '\0';
|
---|
| 1071 | }
|
---|
| 1072 | paranoid_fclose(fin);
|
---|
| 1073 | return (res);
|
---|
[1] | 1074 | }
|
---|
| 1075 |
|
---|
| 1076 |
|
---|
| 1077 |
|
---|
| 1078 |
|
---|
| 1079 |
|
---|
| 1080 |
|
---|
| 1081 |
|
---|
| 1082 |
|
---|
| 1083 |
|
---|
| 1084 | /**
|
---|
| 1085 | * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir.
|
---|
| 1086 | * Currently this includes: copy Mondo's home directory to scratchdir, untar "mondo_home/payload.tgz"
|
---|
| 1087 | * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore
|
---|
| 1088 | * and post-nuke.tgz (if it exists) to tmpdir, and run "hostname > scratchdir/HOSTNAME".
|
---|
| 1089 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1090 | * - @c bkpinfo->postnuke_tarball
|
---|
| 1091 | * - @c bkpinfo->scratchdir
|
---|
| 1092 | * - @c bkpinfo->tmpdir
|
---|
| 1093 | */
|
---|
[1645] | 1094 | void copy_mondo_and_mindi_stuff_to_scratchdir()
|
---|
[1] | 1095 | {
|
---|
[116] | 1096 | /*@ Char buffers ** */
|
---|
| 1097 | char command[MAX_STR_LEN * 2];
|
---|
| 1098 | char tmp[MAX_STR_LEN];
|
---|
| 1099 | char old_pwd[MAX_STR_LEN];
|
---|
[1] | 1100 |
|
---|
[116] | 1101 | mvaddstr_and_log_it(g_currentY, 0,
|
---|
| 1102 | "Copying Mondo's core files to the scratch directory");
|
---|
[1] | 1103 |
|
---|
[116] | 1104 | log_msg(4, "g_mondo_home='%s'", g_mondo_home);
|
---|
| 1105 | if (strlen(g_mondo_home) < 2) {
|
---|
| 1106 | find_and_store_mondoarchives_home(g_mondo_home);
|
---|
| 1107 | }
|
---|
| 1108 | sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
|
---|
| 1109 | bkpinfo->scratchdir);
|
---|
[1] | 1110 |
|
---|
[116] | 1111 | log_msg(4, "command = %s", command);
|
---|
| 1112 | if (run_program_and_log_output(command, 1)) {
|
---|
| 1113 | fatal_error("Failed to copy Mondo's stuff to scratchdir");
|
---|
| 1114 | }
|
---|
[1] | 1115 |
|
---|
[116] | 1116 | sprintf(tmp, "%s/payload.tgz", g_mondo_home);
|
---|
| 1117 | if (does_file_exist(tmp)) {
|
---|
| 1118 | log_it("Untarring payload %s to scratchdir %s", tmp,
|
---|
| 1119 | bkpinfo->scratchdir);
|
---|
| 1120 | (void) getcwd(old_pwd, MAX_STR_LEN - 1);
|
---|
| 1121 | chdir(bkpinfo->scratchdir);
|
---|
| 1122 | sprintf(command, "tar -zxvf %s", tmp);
|
---|
| 1123 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1124 | fatal_error("Failed to untar payload");
|
---|
| 1125 | }
|
---|
| 1126 | chdir(old_pwd);
|
---|
| 1127 | }
|
---|
[1] | 1128 |
|
---|
[116] | 1129 | sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir,
|
---|
| 1130 | bkpinfo->scratchdir);
|
---|
[1] | 1131 |
|
---|
[116] | 1132 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1133 | fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
|
---|
| 1134 | }
|
---|
[1] | 1135 |
|
---|
[116] | 1136 | strcpy(tmp,
|
---|
| 1137 | call_program_and_get_last_line_of_output("which mondorestore"));
|
---|
| 1138 | if (!tmp[0]) {
|
---|
| 1139 | fatal_error
|
---|
| 1140 | ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
|
---|
| 1141 | }
|
---|
| 1142 | sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
|
---|
| 1143 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1144 | fatal_error("Failed to copy mondorestore to tmpdir");
|
---|
| 1145 | }
|
---|
[1] | 1146 |
|
---|
[116] | 1147 | sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
|
---|
| 1148 | paranoid_system(command);
|
---|
[1] | 1149 |
|
---|
[116] | 1150 | if (bkpinfo->postnuke_tarball[0]) {
|
---|
| 1151 | sprintf(command, "cp -f %s %s/post-nuke.tgz",
|
---|
| 1152 | bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
|
---|
| 1153 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1154 | fatal_error("Unable to copy post-nuke tarball to tmpdir");
|
---|
| 1155 | }
|
---|
| 1156 | }
|
---|
[1] | 1157 |
|
---|
| 1158 |
|
---|
[116] | 1159 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
[1] | 1160 | }
|
---|
| 1161 |
|
---|
| 1162 |
|
---|
| 1163 |
|
---|
| 1164 |
|
---|
| 1165 |
|
---|
| 1166 | /**
|
---|
| 1167 | * Store the client's NFS configuration in files to be restored at restore-time.
|
---|
| 1168 | * Assumes that @c bkpinfo->media_type = nfs, but does not check for this.
|
---|
| 1169 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1170 | * - @c nfs_mount
|
---|
| 1171 | * - @c nfs_remote_dir
|
---|
| 1172 | * - @c tmpdir
|
---|
| 1173 | */
|
---|
[1645] | 1174 | void store_nfs_config()
|
---|
[1] | 1175 | {
|
---|
| 1176 |
|
---|
[116] | 1177 | /*@ buffers ******** */
|
---|
| 1178 | char nfs_dev[MAX_STR_LEN];
|
---|
[802] | 1179 | char mac_addr[MAX_STR_LEN];
|
---|
[116] | 1180 | char nfs_mount[MAX_STR_LEN];
|
---|
| 1181 | char nfs_client_ipaddr[MAX_STR_LEN];
|
---|
[148] | 1182 | char nfs_client_netmask[MAX_STR_LEN];
|
---|
[196] | 1183 | char nfs_client_broadcast[MAX_STR_LEN];
|
---|
[148] | 1184 | char nfs_client_defgw[MAX_STR_LEN];
|
---|
[116] | 1185 | char nfs_server_ipaddr[MAX_STR_LEN];
|
---|
| 1186 | char tmp[MAX_STR_LEN];
|
---|
| 1187 | char command[MAX_STR_LEN * 2];
|
---|
[1] | 1188 |
|
---|
[116] | 1189 | /*@ pointers ***** */
|
---|
| 1190 | char *p;
|
---|
[1] | 1191 |
|
---|
[116] | 1192 | log_it("Storing NFS configuration");
|
---|
| 1193 | strcpy(tmp, bkpinfo->nfs_mount);
|
---|
| 1194 | p = strchr(tmp, ':');
|
---|
| 1195 | if (!p) {
|
---|
| 1196 | fatal_error
|
---|
| 1197 | ("NFS mount doesn't have a colon in it, e.g. 192.168.1.4:/home/nfs");
|
---|
| 1198 | }
|
---|
| 1199 | *(p++) = '\0';
|
---|
| 1200 | strcpy(nfs_server_ipaddr, tmp);
|
---|
| 1201 | strcpy(nfs_mount, p);
|
---|
[1236] | 1202 |
|
---|
[802] | 1203 | /* BERLIOS : there is a bug #67 here as it only considers the first NIC */
|
---|
[116] | 1204 | sprintf(command,
|
---|
| 1205 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
|
---|
| 1206 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
|
---|
| 1207 | sprintf(command,
|
---|
| 1208 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
|
---|
| 1209 | strcpy(nfs_client_ipaddr,
|
---|
| 1210 | call_program_and_get_last_line_of_output(command));
|
---|
[148] | 1211 | sprintf(command,
|
---|
| 1212 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
|
---|
| 1213 | strcpy(nfs_client_netmask,
|
---|
| 1214 | call_program_and_get_last_line_of_output(command));
|
---|
| 1215 | sprintf(command,
|
---|
[196] | 1216 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
|
---|
| 1217 | strcpy(nfs_client_broadcast,
|
---|
| 1218 | call_program_and_get_last_line_of_output(command));
|
---|
| 1219 | sprintf(command,
|
---|
[222] | 1220 | "route -n | grep '^0.0.0.0' | awk '{print $2}'");
|
---|
[148] | 1221 | strcpy(nfs_client_defgw,
|
---|
| 1222 | call_program_and_get_last_line_of_output(command));
|
---|
[116] | 1223 | sprintf(tmp,
|
---|
| 1224 | "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s",
|
---|
| 1225 | nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount);
|
---|
| 1226 | if (strlen(nfs_dev) < 2) {
|
---|
| 1227 | fatal_error
|
---|
| 1228 | ("Unable to find ethN (eth0, eth1, ...) adapter via NFS mount you specified.");
|
---|
| 1229 | }
|
---|
[802] | 1230 | /********
|
---|
| 1231 | * If the NFS device that found above is a bonded device,
|
---|
| 1232 | * we need to replace it with an ethN device or the
|
---|
| 1233 | * networking will not start during an NFS restore.
|
---|
| 1234 | *
|
---|
[1836] | 1235 | * If the NFS device in nfs_dev begins with the word "bond", or alb or aft
|
---|
[802] | 1236 | * look for the corresponding slave ethN device and copy it to nfs_dev.
|
---|
| 1237 | * Using the common MAC address
|
---|
| 1238 | ********/
|
---|
[1836] | 1239 | if (!strncmp(nfs_dev, "bond", 4) || !strncmp(nfs_dev, "alb", 3) || !strncmp(nfs_dev, "aft", 3)) {
|
---|
[802] | 1240 | log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", nfs_dev);
|
---|
| 1241 | sprintf(command,
|
---|
[888] | 1242 | "ifconfig %s | awk '{print $5}' | head -n1", nfs_dev);
|
---|
[802] | 1243 | strcpy(mac_addr, call_program_and_get_last_line_of_output(command));
|
---|
| 1244 | sprintf(command,
|
---|
[888] | 1245 | "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr,nfs_dev);
|
---|
[802] | 1246 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command));
|
---|
| 1247 | log_to_screen("Replacing it with %s\n", nfs_dev);
|
---|
| 1248 | }
|
---|
[1] | 1249 |
|
---|
[116] | 1250 | sprintf(tmp, "%s/NFS-DEV", bkpinfo->tmpdir);
|
---|
| 1251 | write_one_liner_data_file(tmp, nfs_dev);
|
---|
[1] | 1252 |
|
---|
[116] | 1253 | sprintf(tmp, "%s/NFS-CLIENT-IPADDR", bkpinfo->tmpdir);
|
---|
| 1254 | write_one_liner_data_file(tmp, nfs_client_ipaddr);
|
---|
[148] | 1255 | sprintf(tmp, "%s/NFS-CLIENT-NETMASK", bkpinfo->tmpdir);
|
---|
| 1256 | write_one_liner_data_file(tmp, nfs_client_netmask);
|
---|
[196] | 1257 | sprintf(tmp, "%s/NFS-CLIENT-BROADCAST", bkpinfo->tmpdir);
|
---|
| 1258 | write_one_liner_data_file(tmp, nfs_client_broadcast);
|
---|
[148] | 1259 | sprintf(tmp, "%s/NFS-CLIENT-DEFGW", bkpinfo->tmpdir);
|
---|
| 1260 | write_one_liner_data_file(tmp, nfs_client_defgw);
|
---|
[116] | 1261 | sprintf(tmp, "%s/NFS-SERVER-IPADDR", bkpinfo->tmpdir);
|
---|
| 1262 | write_one_liner_data_file(tmp, nfs_server_ipaddr);
|
---|
| 1263 | sprintf(tmp, "%s/NFS-SERVER-MOUNT", bkpinfo->tmpdir);
|
---|
| 1264 | write_one_liner_data_file(tmp, bkpinfo->nfs_mount);
|
---|
| 1265 | sprintf(tmp, "%s/NFS-SERVER-PATH", bkpinfo->tmpdir);
|
---|
| 1266 | write_one_liner_data_file(tmp, bkpinfo->nfs_remote_dir);
|
---|
[148] | 1267 | sprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
|
---|
| 1268 | write_one_liner_data_file(tmp, bkpinfo->prefix);
|
---|
[116] | 1269 | log_it("Finished storing NFS configuration");
|
---|
[1] | 1270 | }
|
---|
| 1271 |
|
---|
| 1272 |
|
---|
| 1273 |
|
---|
| 1274 |
|
---|
| 1275 |
|
---|
| 1276 |
|
---|
| 1277 | /**
|
---|
| 1278 | * Determine the approximate number of media that the backup will take up,
|
---|
| 1279 | * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K()
|
---|
| 1280 | * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as
|
---|
| 1281 | * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any
|
---|
| 1282 | * "imagedevs", the estimate is not shown as it will be wildly inaccurate.
|
---|
| 1283 | * If there are more than 50 media estimated, the estimate will not be shown.
|
---|
| 1284 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1285 | * - @c bkpinfo->backup_media_type
|
---|
| 1286 | * - @c bkpinfo->image_devs
|
---|
| 1287 | * - @c bkpinfo->media_size
|
---|
| 1288 | * - @c bkpinfo->optimal_set_size
|
---|
| 1289 | * - @c bkpinfo->use_lzo
|
---|
| 1290 | * @param noof_sets The number of filesets created.
|
---|
| 1291 | * @ingroup archiveGroup
|
---|
| 1292 | */
|
---|
| 1293 | void
|
---|
[1645] | 1294 | estimate_noof_media_required(long noof_sets)
|
---|
[1] | 1295 | {
|
---|
[116] | 1296 | /*@ buffers *************** */
|
---|
| 1297 | char tmp[MAX_STR_LEN];
|
---|
[1] | 1298 |
|
---|
[116] | 1299 | /*@ long long ************* */
|
---|
| 1300 | long long scratchLL;
|
---|
[1] | 1301 |
|
---|
[1505] | 1302 | if (bkpinfo->media_size[1] <= 0) {
|
---|
[116] | 1303 | log_to_screen("Number of media required: UNKNOWN");
|
---|
| 1304 | return;
|
---|
| 1305 | }
|
---|
[1] | 1306 |
|
---|
[116] | 1307 | log_it("Estimating number of media required...");
|
---|
| 1308 | scratchLL =
|
---|
| 1309 | (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size)
|
---|
[1645] | 1310 | + (long long) (size_of_all_biggiefiles_K());
|
---|
[116] | 1311 | scratchLL = (scratchLL / 1024) / bkpinfo->media_size[1];
|
---|
| 1312 | scratchLL++;
|
---|
| 1313 | if (bkpinfo->use_lzo) {
|
---|
| 1314 | scratchLL = (scratchLL * 2) / 3;
|
---|
[1002] | 1315 | } else if (bkpinfo->use_gzip) {
|
---|
[998] | 1316 | scratchLL = (scratchLL * 2) / 3;
|
---|
[116] | 1317 | } else {
|
---|
| 1318 | scratchLL = scratchLL / 2;
|
---|
| 1319 | }
|
---|
| 1320 | if (!scratchLL) {
|
---|
| 1321 | scratchLL++;
|
---|
| 1322 | }
|
---|
| 1323 | if (scratchLL <= 1) {
|
---|
| 1324 | sprintf(tmp,
|
---|
[541] | 1325 | "Your backup will probably occupy a single %s. Maybe two.",
|
---|
[126] | 1326 | media_descriptor_string(bkpinfo->backup_media_type));
|
---|
[116] | 1327 | } else if (scratchLL > 4) {
|
---|
| 1328 | sprintf(tmp,
|
---|
[541] | 1329 | "Your backup will occupy one meeeeellion media! (maybe %s)",
|
---|
[116] | 1330 | number_to_text((int) (scratchLL + 1)));
|
---|
| 1331 | } else {
|
---|
[541] | 1332 | sprintf(tmp, "Your backup will occupy approximately %s media.",
|
---|
[116] | 1333 | number_to_text((int) (scratchLL + 1)));
|
---|
| 1334 | }
|
---|
| 1335 | if (!bkpinfo->image_devs[0] && (scratchLL < 50)) {
|
---|
| 1336 | log_to_screen(tmp);
|
---|
| 1337 | }
|
---|
[1] | 1338 | }
|
---|
| 1339 |
|
---|
| 1340 |
|
---|
| 1341 | /**
|
---|
| 1342 | * Get the last suffix of @p instr.
|
---|
| 1343 | * If @p instr was "httpd.log.gz", we would return "gz".
|
---|
| 1344 | * @param instr The filename to get the suffix of.
|
---|
| 1345 | * @return The suffix (without a dot), or "" if none.
|
---|
| 1346 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 1347 | */
|
---|
[116] | 1348 | char *sz_last_suffix(char *instr)
|
---|
[1] | 1349 | {
|
---|
[116] | 1350 | static char outstr[MAX_STR_LEN];
|
---|
| 1351 | char *p;
|
---|
[1] | 1352 |
|
---|
[116] | 1353 | p = strrchr(instr, '.');
|
---|
| 1354 | if (!p) {
|
---|
| 1355 | outstr[0] = '\0';
|
---|
| 1356 | } else {
|
---|
| 1357 | strcpy(outstr, p);
|
---|
| 1358 | }
|
---|
| 1359 | return (outstr);
|
---|
[1] | 1360 | }
|
---|
| 1361 |
|
---|
| 1362 |
|
---|
| 1363 | /**
|
---|
| 1364 | * Determine whether a file is compressed. This is done
|
---|
| 1365 | * by reading through the "do-not-compress-these" file distributed with Mondo.
|
---|
| 1366 | * @param filename The file to check.
|
---|
| 1367 | * @return TRUE if it's compressed, FALSE if not.
|
---|
| 1368 | */
|
---|
[116] | 1369 | bool is_this_file_compressed(char *filename)
|
---|
[1] | 1370 | {
|
---|
[116] | 1371 | char do_not_compress_these[MAX_STR_LEN];
|
---|
| 1372 | char tmp[MAX_STR_LEN];
|
---|
| 1373 | char *p;
|
---|
[1236] | 1374 | char *q = NULL;
|
---|
[1] | 1375 |
|
---|
[1236] | 1376 | q = strrchr(filename, '.');
|
---|
| 1377 | if (q == NULL) {
|
---|
| 1378 | return (FALSE);
|
---|
| 1379 | }
|
---|
| 1380 |
|
---|
[116] | 1381 | sprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
|
---|
| 1382 | if (!does_file_exist(tmp)) {
|
---|
| 1383 | return (FALSE);
|
---|
| 1384 | }
|
---|
[1236] | 1385 | /* BERLIOS: This is just plain WRONG !! */
|
---|
| 1386 | strcpy(do_not_compress_these,last_line_of_file(tmp));
|
---|
| 1387 |
|
---|
[116] | 1388 | for (p = do_not_compress_these; p != NULL; p++) {
|
---|
| 1389 | strcpy(tmp, p);
|
---|
| 1390 | if (strchr(tmp, ' ')) {
|
---|
| 1391 | *(strchr(tmp, ' ')) = '\0';
|
---|
| 1392 | }
|
---|
[1236] | 1393 | if (!strcmp(q, tmp)) {
|
---|
[116] | 1394 | return (TRUE);
|
---|
| 1395 | }
|
---|
| 1396 | if (!(p = strchr(p, ' '))) {
|
---|
| 1397 | break;
|
---|
| 1398 | }
|
---|
| 1399 | }
|
---|
| 1400 | return (FALSE);
|
---|
[1] | 1401 | }
|
---|
| 1402 |
|
---|
| 1403 |
|
---|
| 1404 |
|
---|
[116] | 1405 | int mode_of_file(char *fname)
|
---|
[1] | 1406 | {
|
---|
[116] | 1407 | struct stat buf;
|
---|
[1] | 1408 |
|
---|
[116] | 1409 | if (lstat(fname, &buf)) {
|
---|
| 1410 | return (-1);
|
---|
| 1411 | } // error
|
---|
| 1412 | else {
|
---|
| 1413 | return (buf.st_mode);
|
---|
| 1414 | }
|
---|
[1] | 1415 | }
|
---|
| 1416 |
|
---|
| 1417 |
|
---|
| 1418 |
|
---|
| 1419 |
|
---|
| 1420 | /**
|
---|
| 1421 | * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks.
|
---|
| 1422 | * @param outfile Where to put the script.
|
---|
| 1423 | * @return 0 for success, 1 for failure.
|
---|
| 1424 | */
|
---|
[116] | 1425 | int make_grub_install_scriptlet(char *outfile)
|
---|
[1] | 1426 | {
|
---|
[116] | 1427 | FILE *fout;
|
---|
| 1428 | char *tmp;
|
---|
| 1429 | int retval = 0;
|
---|
[1] | 1430 |
|
---|
[116] | 1431 | malloc_string(tmp);
|
---|
| 1432 | if ((fout = fopen(outfile, "w"))) {
|
---|
| 1433 | fprintf(fout,
|
---|
| 1434 | "#!/bin/sh\n\nmount /boot > /dev/null 2> /dev/null\ngrub-install $@\nres=$?\nsync;sync;sync\nexit $res\n");
|
---|
| 1435 | paranoid_fclose(fout);
|
---|
| 1436 | log_msg(2, "Created %s", outfile);
|
---|
| 1437 | sprintf(tmp, "chmod +x %s", outfile);
|
---|
| 1438 | paranoid_system(tmp);
|
---|
| 1439 | retval = 0;
|
---|
| 1440 | } else {
|
---|
| 1441 | retval = 1;
|
---|
[1] | 1442 | }
|
---|
[116] | 1443 | paranoid_free(tmp);
|
---|
| 1444 | return (retval);
|
---|
[1] | 1445 | }
|
---|
| 1446 |
|
---|
| 1447 | /* @} - end fileGroup */
|
---|