source: trunk/mondo/mondo/common/libmondo-files.c @ 30

Last change on this file since 30 was 30, checked in by bcornec, 14 years ago

Id property added on files to allow for better conf. management

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