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