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