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