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