Changeset 59 in MondoRescue for trunk/mondo/mondo/common/libmondo-devices.c


Ignore:
Timestamp:
Oct 11, 2005, 1:34:31 AM (19 years ago)
Author:
bcornec
Message:

Trunk: indent on all source files

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/mondo/mondo/common/libmondo-devices.c

    r30 r59  
    11/* libmondo-devices.c                 Subroutines for handling devices
    2    $Id: libmondo-devices.c,v 1.26 2004/06/21 20:20:36 hugo Exp $
     2   $Id$
    33.
    44
     
    247247#elif linux
    248248#define u64 unsigned long long
    249 #include <linux/fs.h> /* for BLKGETSIZE64 */
     249#include <linux/fs.h>           /* for BLKGETSIZE64 */
    250250#include <linux/hdreg.h>
    251251#endif
    252252
    253253/*@unused@*/
    254 //static char cvsid[] = "$Id: libmondo-devices.c,v 1.26 2004/06/21 20:20:36 hugo Exp $";
     254//static char cvsid[] = "$Id$";
    255255
    256256extern int g_current_media_number;
     
    261261extern char *g_erase_tmpdir_and_scratchdir;
    262262
    263 static char g_cdrw_drive_is_here[MAX_STR_LEN/4]="";
    264 static char g_cdrom_drive_is_here[MAX_STR_LEN/4]="";
    265 static char g_dvd_drive_is_here[MAX_STR_LEN/4]="";
     263static char g_cdrw_drive_is_here[MAX_STR_LEN / 4] = "";
     264static char g_cdrom_drive_is_here[MAX_STR_LEN / 4] = "";
     265static char g_dvd_drive_is_here[MAX_STR_LEN / 4] = "";
    266266
    267267
     
    270270 * @ingroup globalGroup
    271271 */
    272 bool g_restoring_live_from_cd=FALSE;
    273 
    274 extern t_bkptype g_backup_media_type; // set by main()
     272bool g_restoring_live_from_cd = FALSE;
     273
     274extern t_bkptype g_backup_media_type;   // set by main()
    275275
    276276
     
    279279void set_g_cdrom_and_g_dvd_to_bkpinfo_value(struct s_bkpinfo *bkpinfo)
    280280{
    281   strcpy(g_cdrom_drive_is_here, bkpinfo->media_device); // just in case
    282   strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
     281    strcpy(g_cdrom_drive_is_here, bkpinfo->media_device);   // just in case
     282    strcpy(g_dvd_drive_is_here, bkpinfo->media_device); // just in case
    283283}
    284284
     
    292292{
    293293//  log_it("rctada: Retracting all CD trays", __LINE__);
    294   if (strlen(g_cdrom_drive_is_here)>0)
    295     { inject_device(g_cdrom_drive_is_here); }
    296   if (strlen(g_dvd_drive_is_here)>0)
    297     { inject_device(g_dvd_drive_is_here); }
    298   if (strlen(g_cdrw_drive_is_here)>0)
    299     { inject_device(g_cdrw_drive_is_here); }
     294    if (strlen(g_cdrom_drive_is_here) > 0) {
     295        inject_device(g_cdrom_drive_is_here);
     296    }
     297    if (strlen(g_dvd_drive_is_here) > 0) {
     298        inject_device(g_dvd_drive_is_here);
     299    }
     300    if (strlen(g_cdrw_drive_is_here) > 0) {
     301        inject_device(g_cdrw_drive_is_here);
     302    }
    300303//  log_it("rctada: killing autorun");
    301304//  run_program_and_log_output("killall autorun", TRUE);
    302   if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5))
    303     {
    304       log_it("autorun detected; sleeping for 2 seconds");
    305       sleep(2);
    306     }
    307   log_it("rctada: Unmounting all CD drives", __LINE__);
    308   run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
     305    if (!run_program_and_log_output("ps | grep autorun | grep -v grep", 5)) {
     306        log_it("autorun detected; sleeping for 2 seconds");
     307        sleep(2);
     308    }
     309    log_it("rctada: Unmounting all CD drives", __LINE__);
     310    run_program_and_log_output("umount /dev/cdr* /dev/dvd*", 5);
    309311}
    310312
     
    316318 * @ingroup utilityGroup
    317319 */
    318 bool
    319 am_I_in_disaster_recovery_mode (void)
    320 {
    321   char *tmp, *comment;
    322   bool is_this_a_ramdisk = FALSE;
    323 
    324   malloc_string(tmp);
    325   malloc_string(comment);
    326   strcpy(tmp, where_is_root_mounted());
    327   sprintf(comment, "root is mounted at %s\n", tmp);
    328   log_msg(0, comment);
    329   log_msg(0, "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().", tmp);
     320bool am_I_in_disaster_recovery_mode(void)
     321{
     322    char *tmp, *comment;
     323    bool is_this_a_ramdisk = FALSE;
     324
     325    malloc_string(tmp);
     326    malloc_string(comment);
     327    strcpy(tmp, where_is_root_mounted());
     328    sprintf(comment, "root is mounted at %s\n", tmp);
     329    log_msg(0, comment);
     330    log_msg(0,
     331            "No, Schlomo, that doesn't mean %s is the root partition. It's just a debugging message. Relax. It's part of am_I_in_disaster_recovery_mode().",
     332            tmp);
    330333
    331334#ifdef __FreeBSD__
    332   if (strstr(tmp, "/dev/md"))
    333     {
    334       is_this_a_ramdisk = TRUE;
    335     }
     335    if (strstr(tmp, "/dev/md")) {
     336        is_this_a_ramdisk = TRUE;
     337    }
    336338#else
    337   if (!strncmp(tmp, "/dev/ram", 8) || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/") && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
    338     || !strcmp(tmp, "/dev/root"))
    339     { is_this_a_ramdisk = TRUE; }
    340   else
    341     { is_this_a_ramdisk = FALSE; }
    342 #endif
    343 
    344   if (is_this_a_ramdisk)
    345     {
    346       if (!does_file_exist("/THIS-IS-A-RAMDISK") && !does_file_exist("/tmp/mountlist.txt.sample"))
    347         {
    348           log_to_screen("Using /dev/root is stupid of you but I'll forgive you.");
    349           is_this_a_ramdisk = FALSE;
    350         }
    351     }
    352   if (does_file_exist("/THIS-IS-A-RAMDISK"))
    353     {
    354       is_this_a_ramdisk = TRUE;
    355     }
    356   paranoid_free(tmp);
    357   paranoid_free(comment);
    358   log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
    359   return(is_this_a_ramdisk);
     339    if (!strncmp(tmp, "/dev/ram", 8)
     340        || (!strncmp(tmp, "/dev/rd", 7) && !strcmp(tmp, "/dev/rd/")
     341            && strncmp(tmp, "/dev/rd/cd", 10)) || strstr(tmp, "rootfs")
     342        || !strcmp(tmp, "/dev/root")) {
     343        is_this_a_ramdisk = TRUE;
     344    } else {
     345        is_this_a_ramdisk = FALSE;
     346    }
     347#endif
     348
     349    if (is_this_a_ramdisk) {
     350        if (!does_file_exist("/THIS-IS-A-RAMDISK")
     351            && !does_file_exist("/tmp/mountlist.txt.sample")) {
     352            log_to_screen
     353                ("Using /dev/root is stupid of you but I'll forgive you.");
     354            is_this_a_ramdisk = FALSE;
     355        }
     356    }
     357    if (does_file_exist("/THIS-IS-A-RAMDISK")) {
     358        is_this_a_ramdisk = TRUE;
     359    }
     360    paranoid_free(tmp);
     361    paranoid_free(comment);
     362    log_msg(1, "Is this a ramdisk? result = %d", is_this_a_ramdisk);
     363    return (is_this_a_ramdisk);
    360364}
    361365
     
    372376static char *bkptype_to_string(t_bkptype bt)
    373377{
    374   static char output[MAX_STR_LEN/4];
    375   switch(bt)
    376     {
    377     case none: strcpy(output, "none");
     378    static char output[MAX_STR_LEN / 4];
     379    switch (bt) {
     380    case none:
     381        strcpy(output, "none");
    378382        break;
    379     case iso:  strcpy(output, "iso");
     383    case iso:
     384        strcpy(output, "iso");
    380385        break;
    381     case cdr:  strcpy(output, "cdr");
     386    case cdr:
     387        strcpy(output, "cdr");
    382388        break;
    383     case cdrw: strcpy(output, "cdrw");
     389    case cdrw:
     390        strcpy(output, "cdrw");
    384391        break;
    385     case cdstream: strcpy(output, "cdstream");
     392    case cdstream:
     393        strcpy(output, "cdstream");
    386394        break;
    387     case nfs:  strcpy(output, "nfs");
     395    case nfs:
     396        strcpy(output, "nfs");
    388397        break;
    389     case tape: strcpy(output, "tape");
     398    case tape:
     399        strcpy(output, "tape");
    390400        break;
    391     case udev: strcpy(output, "udev");
     401    case udev:
     402        strcpy(output, "udev");
    392403        break;
    393     default: strcpy(output, "default");
    394     }
    395   return(output);
     404    default:
     405        strcpy(output, "default");
     406    }
     407    return (output);
    396408}
    397409
     
    407419 * @return the return value of the @c eject command. (0=success, nonzero=failure)
    408420 */
    409 int eject_device(char*dev)
    410 {
    411   char *command;
    412   int res1=0, res2=0;
    413 
    414   malloc_string(command);
    415 
    416   if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type) && g_backup_media_type!=udev)
    417     {
    418       sprintf(command, "mt -f %s offline", dev);
    419       res1 = run_program_and_log_output(command, 1);
    420     }
    421   else
    422     {
    423       res1 = 0;
    424     }
     421int eject_device(char *dev)
     422{
     423    char *command;
     424    int res1 = 0, res2 = 0;
     425
     426    malloc_string(command);
     427
     428    if (IS_THIS_A_STREAMING_BACKUP(g_backup_media_type)
     429        && g_backup_media_type != udev) {
     430        sprintf(command, "mt -f %s offline", dev);
     431        res1 = run_program_and_log_output(command, 1);
     432    } else {
     433        res1 = 0;
     434    }
    425435
    426436#ifdef __FreeBSD__
    427   if (strstr (dev, "acd")) {
    428       sprintf (command, "cdcontrol -f %s eject", dev);
    429   } else {
    430       sprintf (command, "camcontrol eject `echo %s | sed 's|/dev/||'`", dev);
    431   }
     437    if (strstr(dev, "acd")) {
     438        sprintf(command, "cdcontrol -f %s eject", dev);
     439    } else {
     440        sprintf(command, "camcontrol eject `echo %s | sed 's|/dev/||'`",
     441                dev);
     442    }
    432443#else
    433   sprintf(command, "eject %s", dev);
    434 #endif
    435 
    436   log_msg(3, "Ejecting %s", dev);
    437   res2 = run_program_and_log_output(command, 1);
    438   paranoid_free(command);
    439   if (res1 && res2) { return(1); }
    440   else { return(0); }
     444    sprintf(command, "eject %s", dev);
     445#endif
     446
     447    log_msg(3, "Ejecting %s", dev);
     448    res2 = run_program_and_log_output(command, 1);
     449    paranoid_free(command);
     450    if (res1 && res2) {
     451        return (1);
     452    } else {
     453        return (0);
     454    }
    441455}
    442456
     
    446460 * @return 0 for success, nonzero for failure.
    447461 */
    448 int inject_device(char*dev)
    449 {
    450   char *command;
    451   int i;
    452 
    453   malloc_string(command);
     462int inject_device(char *dev)
     463{
     464    char *command;
     465    int i;
     466
     467    malloc_string(command);
    454468
    455469
    456470#ifdef __FreeBSD__
    457   if (strstr (dev, "acd")) {
    458       sprintf (command, "cdcontrol -f %s close", dev);
    459   } else {
    460       sprintf (command, "camcontrol load `echo %s | sed 's|/dev/||'`", dev);
    461   }
     471    if (strstr(dev, "acd")) {
     472        sprintf(command, "cdcontrol -f %s close", dev);
     473    } else {
     474        sprintf(command, "camcontrol load `echo %s | sed 's|/dev/||'`",
     475                dev);
     476    }
    462477#else
    463   sprintf(command, "eject -t %s", dev);
    464 #endif
    465   i = run_program_and_log_output(command, FALSE);
    466   paranoid_free(command);
    467   return(i);
     478    sprintf(command, "eject -t %s", dev);
     479#endif
     480    i = run_program_and_log_output(command, FALSE);
     481    paranoid_free(command);
     482    return (i);
    468483}
    469484
     
    474489 * @return TRUE if it exists, FALSE if it doesn't.
    475490 */
    476 bool
    477 does_device_exist (char *device)
    478 {
    479 
    480     /*@ buffers ************************************************************/
    481   char *tmp;
    482 
    483   malloc_string(tmp);
    484   assert_string_is_neither_NULL_nor_zerolength(device);
    485 
    486   sprintf (tmp, "ls %s > /dev/null 2> /dev/null", device);
    487  
    488   paranoid_free(tmp);
    489   if (system (tmp))
    490     {
    491       return (FALSE);
    492     }
    493   else
    494     {
    495       return (TRUE);
    496     }
     491bool does_device_exist(char *device)
     492{
     493
     494    /*@ buffers *********************************************************** */
     495    char *tmp;
     496
     497    malloc_string(tmp);
     498    assert_string_is_neither_NULL_nor_zerolength(device);
     499
     500    sprintf(tmp, "ls %s > /dev/null 2> /dev/null", device);
     501
     502    paranoid_free(tmp);
     503    if (system(tmp)) {
     504        return (FALSE);
     505    } else {
     506        return (TRUE);
     507    }
    497508}
    498509
     
    502513 * @return TRUE (there's a Linux/FreeBSD partition) or FALSE (Microsoft has taken over yet another innocent PC).
    503514 */
    504 bool
    505 does_nonMS_partition_exist (void)
     515bool does_nonMS_partition_exist(void)
    506516{
    507517#if __FreeBSD__
    508     return !system ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
     518    return
     519        !system
     520        ("for drive in /dev/ad? /dev/da?; do fdisk $drive | grep -q FreeBSD && exit 0; done; false");
    509521#else
    510     return !system ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | egrep -qv '(MS|DOS|FAT|NTFS)'");
     522    return
     523        !system
     524        ("parted2fdisk -l 2>/dev/null | grep '^/dev/' | egrep -qv '(MS|DOS|FAT|NTFS)'");
    511525#endif
    512526}
     
    518532 * @return 0 if it exists, nonzero otherwise.
    519533 */
    520 int
    521 does_partition_exist (const char *drive, int partno)
    522 {
    523     /*@ buffers *****************************************************/
    524   char *program;
    525   char *incoming;
    526   char *searchstr;
    527   char *tmp;
    528 
    529     /*@ ints ********************************************************/
    530   int res = 0;
    531 
    532     /*@ pointers ****************************************************/
    533   FILE *fin;
    534 
    535 
    536     /*@ end vars ****************************************************/
    537   assert_string_is_neither_NULL_nor_zerolength(drive);
    538   assert(partno>=0 && partno<999);
    539 
    540   malloc_string(program);
    541   malloc_string(incoming);
    542   malloc_string(searchstr);
    543   malloc_string(tmp);
     534int does_partition_exist(const char *drive, int partno)
     535{
     536    /*@ buffers **************************************************** */
     537    char *program;
     538    char *incoming;
     539    char *searchstr;
     540    char *tmp;
     541
     542    /*@ ints ******************************************************* */
     543    int res = 0;
     544
     545    /*@ pointers *************************************************** */
     546    FILE *fin;
     547
     548
     549    /*@ end vars *************************************************** */
     550    assert_string_is_neither_NULL_nor_zerolength(drive);
     551    assert(partno >= 0 && partno < 999);
     552
     553    malloc_string(program);
     554    malloc_string(incoming);
     555    malloc_string(searchstr);
     556    malloc_string(tmp);
    544557
    545558#ifdef __FreeBSD__
    546   // We assume here that this is running from mondorestore. (It is.)
    547   sprintf (program, "ls %s >/dev/null 2>&1", drive, build_partition_name (tmp, drive, partno));
    548   return system (program);
     559    // We assume here that this is running from mondorestore. (It is.)
     560    sprintf(program, "ls %s >/dev/null 2>&1", drive,
     561            build_partition_name(tmp, drive, partno));
     562    return system(program);
    549563#else
    550   tmp[0] = '\0';
    551 #endif
    552 
    553   sprintf (program, "parted2fdisk -l %s 2> /dev/null", drive);
    554   fin = popen (program, "r");
    555   if (!fin)
    556     {
    557       log_it("program=%s", program);
    558       log_OS_error("Cannot popen-in program");
    559       return (0);
    560     }
    561   (void) build_partition_name (searchstr, drive, partno);
    562   strcat(searchstr, " ");
    563   for (res = 0; !res && fgets (incoming, MAX_STR_LEN - 1, fin); )
    564     {
    565       if (strstr (incoming, searchstr))
    566     {
    567       res = 1;
    568     }
    569     }
    570   if (pclose (fin)) { log_OS_error("Cannot pclose fin"); }
    571   paranoid_free(program);
    572   paranoid_free(incoming);
    573   paranoid_free(searchstr);
    574   paranoid_free(tmp);
    575   return (res);
     564    tmp[0] = '\0';
     565#endif
     566
     567    sprintf(program, "parted2fdisk -l %s 2> /dev/null", drive);
     568    fin = popen(program, "r");
     569    if (!fin) {
     570        log_it("program=%s", program);
     571        log_OS_error("Cannot popen-in program");
     572        return (0);
     573    }
     574    (void) build_partition_name(searchstr, drive, partno);
     575    strcat(searchstr, " ");
     576    for (res = 0; !res && fgets(incoming, MAX_STR_LEN - 1, fin);) {
     577        if (strstr(incoming, searchstr)) {
     578            res = 1;
     579        }
     580    }
     581    if (pclose(fin)) {
     582        log_OS_error("Cannot pclose fin");
     583    }
     584    paranoid_free(program);
     585    paranoid_free(incoming);
     586    paranoid_free(searchstr);
     587    paranoid_free(tmp);
     588    return (res);
    576589}
    577590
     
    586599 * @return TRUE if it exists, FALSE if it doesn't.
    587600 */
    588 bool
    589 does_string_exist_in_boot_block (char *dev, char *str)
    590 {
    591     /*@ buffers *****************************************************/
    592   char *command;
    593 
    594     /*@ end vars ****************************************************/
    595   int i;
    596 
    597   assert_string_is_neither_NULL_nor_zerolength(dev);
    598   assert_string_is_neither_NULL_nor_zerolength(str);
    599 
    600   malloc_string(command);
    601   sprintf (command,
    602        "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
    603        dev, str);
    604   i = system(command);
    605   paranoid_free(command);
    606   if (i)
    607     {
    608       return (FALSE);
    609     }
    610   else
    611     {
    612       return (TRUE);
    613     }
     601bool does_string_exist_in_boot_block(char *dev, char *str)
     602{
     603    /*@ buffers **************************************************** */
     604    char *command;
     605
     606    /*@ end vars *************************************************** */
     607    int i;
     608
     609    assert_string_is_neither_NULL_nor_zerolength(dev);
     610    assert_string_is_neither_NULL_nor_zerolength(str);
     611
     612    malloc_string(command);
     613    sprintf(command,
     614            "dd if=%s bs=446 count=1 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
     615            dev, str);
     616    i = system(command);
     617    paranoid_free(command);
     618    if (i) {
     619        return (FALSE);
     620    } else {
     621        return (TRUE);
     622    }
    614623}
    615624
     
    621630 * @param n The number of 512-byte sectors to search.
    622631 */
    623 bool
    624 does_string_exist_in_first_N_blocks (char *dev, char *str, int n)
    625 {
    626       /*@ buffers *****************************************************/
    627   char *command;
    628       /*@ end vars ****************************************************/
    629   int i;
    630 
    631   malloc_string(command);
    632   sprintf (command,
    633          "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
    634          dev, n, str);
    635   i = system(command);
    636   paranoid_free(command);
    637   if (i)
    638     {
    639       return (FALSE);
    640     }
    641   else
    642     {
    643       return (TRUE);
    644     }
     632bool does_string_exist_in_first_N_blocks(char *dev, char *str, int n)
     633{
     634    /*@ buffers **************************************************** */
     635    char *command;
     636    /*@ end vars *************************************************** */
     637    int i;
     638
     639    malloc_string(command);
     640    sprintf(command,
     641            "dd if=%s bs=512 count=%i 2> /dev/null | strings | grep \"%s\" > /dev/null 2> /dev/null",
     642            dev, n, str);
     643    i = system(command);
     644    paranoid_free(command);
     645    if (i) {
     646        return (FALSE);
     647    } else {
     648        return (TRUE);
     649    }
    645650}
    646651
     
    655660 * @see mount_CDROM_here
    656661 */
    657 int
    658 find_and_mount_actual_cd (struct s_bkpinfo *bkpinfo, char *mountpoint)
    659 {
    660     /*@ buffers ******************************************************/
    661 
    662     /*@ int's  *******************************************************/
    663   int res;
    664   char *dev;
    665 
    666     /*@ end vars *****************************************************/
    667 
    668   malloc_string(dev);
    669   assert(bkpinfo!=NULL);
    670   assert_string_is_neither_NULL_nor_zerolength(mountpoint);
    671 
    672   if (g_backup_media_type == dvd)
    673     {
    674       strcpy(dev, g_dvd_drive_is_here);
    675       if (!dev[0])
    676         { find_dvd_device(dev,FALSE); }
    677     }
    678   else
    679     {
    680       strcpy(dev, g_cdrom_drive_is_here);
    681       if (!dev[0])
    682         { find_cdrom_device(dev,FALSE); }
    683     }
    684 
    685   if (bkpinfo->backup_media_type != iso)
    686     { retract_CD_tray_and_defeat_autorun(); }
    687 
    688   if (!dev[0] || (res=mount_CDROM_here (dev, mountpoint)))
    689     {
    690       if (!popup_and_get_string
     662int find_and_mount_actual_cd(struct s_bkpinfo *bkpinfo, char *mountpoint)
     663{
     664    /*@ buffers ***************************************************** */
     665
     666    /*@ int's  ****************************************************** */
     667    int res;
     668    char *dev;
     669
     670    /*@ end vars **************************************************** */
     671
     672    malloc_string(dev);
     673    assert(bkpinfo != NULL);
     674    assert_string_is_neither_NULL_nor_zerolength(mountpoint);
     675
     676    if (g_backup_media_type == dvd) {
     677        strcpy(dev, g_dvd_drive_is_here);
     678        if (!dev[0]) {
     679            find_dvd_device(dev, FALSE);
     680        }
     681    } else {
     682        strcpy(dev, g_cdrom_drive_is_here);
     683        if (!dev[0]) {
     684            find_cdrom_device(dev, FALSE);
     685        }
     686    }
     687
     688    if (bkpinfo->backup_media_type != iso) {
     689        retract_CD_tray_and_defeat_autorun();
     690    }
     691
     692    if (!dev[0] || (res = mount_CDROM_here(dev, mountpoint))) {
     693        if (!popup_and_get_string
    691694            ("CD-ROM device", "Please enter your CD-ROM's /dev device",
    692             dev, MAX_STR_LEN/4))
    693     { res = 1; }
    694       else
    695     { res=mount_CDROM_here (dev, mountpoint); }
    696     }
    697   if (res) { log_msg(1, "mount failed"); }
    698   else { log_msg(1, "mount succeeded with %s", dev); }
    699   paranoid_free(dev);
    700   return (res);
     695             dev, MAX_STR_LEN / 4)) {
     696            res = 1;
     697        } else {
     698            res = mount_CDROM_here(dev, mountpoint);
     699        }
     700    }
     701    if (res) {
     702        log_msg(1, "mount failed");
     703    } else {
     704        log_msg(1, "mount succeeded with %s", dev);
     705    }
     706    paranoid_free(dev);
     707    return (res);
    701708}
    702709
     
    717724 * @return 0 for success, nonzero for failure.
    718725 */
    719 int
    720 find_cdrw_device (char *cdrw_device)
    721 {
    722     /*@ buffers *************************/
    723   char *comment;
    724   char *tmp;
    725   char *cdr_exe;
    726   char *command;
    727 
    728   malloc_string(comment);
    729   malloc_string(tmp);
    730   malloc_string(cdr_exe);
    731   malloc_string(command);
    732   if (g_cdrw_drive_is_here[0])
    733     {
    734       strcpy(cdrw_device, g_cdrw_drive_is_here);
    735       log_msg(3, "Been there, done that. Returning %s", cdrw_device);
    736   paranoid_free(comment);
    737   paranoid_free(tmp);
    738   paranoid_free(cdr_exe);
    739   paranoid_free(command);
    740       return(0);
    741     }
    742   if (g_backup_media_type == dvd)
    743     {
    744       log_msg(1, "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
    745   paranoid_free(comment);
    746   paranoid_free(tmp);
    747   paranoid_free(cdr_exe);
    748   paranoid_free(command);
    749       return(1);
    750     }
    751   run_program_and_log_output("insmod ide-scsi", -1);
    752   if (find_home_of_exe("cdrecord"))
    753     { strcpy(cdr_exe, "cdrecord"); }
    754   else
    755     { strcpy(cdr_exe, "dvdrecord"); }
    756   tmp[0]='\0';
    757   if (find_home_of_exe(cdr_exe))
    758     {
    759       sprintf(command, "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1", cdr_exe);
    760       strcpy (tmp, call_program_and_get_last_line_of_output(command));
    761     }
    762   if (strlen (tmp) < 2)
    763     {
    764   paranoid_free(comment);
    765   paranoid_free(tmp);
    766   paranoid_free(cdr_exe);
    767   paranoid_free(command);
    768       return 1;
    769     }
    770   else
    771     {
    772       strcpy (cdrw_device, tmp);
    773       sprintf (comment, "Found CDRW device - %s", cdrw_device);
    774       log_it (comment);
    775       strcpy (g_cdrw_drive_is_here, cdrw_device);
    776       paranoid_free(comment);
    777       paranoid_free(tmp);
    778       paranoid_free(cdr_exe);
    779       paranoid_free(command);
    780       return (0);
    781     }
     726int find_cdrw_device(char *cdrw_device)
     727{
     728    /*@ buffers ************************ */
     729    char *comment;
     730    char *tmp;
     731    char *cdr_exe;
     732    char *command;
     733
     734    malloc_string(comment);
     735    malloc_string(tmp);
     736    malloc_string(cdr_exe);
     737    malloc_string(command);
     738    if (g_cdrw_drive_is_here[0]) {
     739        strcpy(cdrw_device, g_cdrw_drive_is_here);
     740        log_msg(3, "Been there, done that. Returning %s", cdrw_device);
     741        paranoid_free(comment);
     742        paranoid_free(tmp);
     743        paranoid_free(cdr_exe);
     744        paranoid_free(command);
     745        return (0);
     746    }
     747    if (g_backup_media_type == dvd) {
     748        log_msg(1,
     749                "This is dumb. You're calling find_cdrw_device() but you're backing up to DVD. WTF?");
     750        paranoid_free(comment);
     751        paranoid_free(tmp);
     752        paranoid_free(cdr_exe);
     753        paranoid_free(command);
     754        return (1);
     755    }
     756    run_program_and_log_output("insmod ide-scsi", -1);
     757    if (find_home_of_exe("cdrecord")) {
     758        strcpy(cdr_exe, "cdrecord");
     759    } else {
     760        strcpy(cdr_exe, "dvdrecord");
     761    }
     762    tmp[0] = '\0';
     763    if (find_home_of_exe(cdr_exe)) {
     764        sprintf(command,
     765                "%s -scanbus 2> /dev/null | tr -s '\t' ' ' | grep \"[0-9]*,[0-9]*,[0-9]*\" | grep -v \"[0-9]*) \\*\" | grep CD | cut -d' ' -f2 | head -n1",
     766                cdr_exe);
     767        strcpy(tmp, call_program_and_get_last_line_of_output(command));
     768    }
     769    if (strlen(tmp) < 2) {
     770        paranoid_free(comment);
     771        paranoid_free(tmp);
     772        paranoid_free(cdr_exe);
     773        paranoid_free(command);
     774        return 1;
     775    } else {
     776        strcpy(cdrw_device, tmp);
     777        sprintf(comment, "Found CDRW device - %s", cdrw_device);
     778        log_it(comment);
     779        strcpy(g_cdrw_drive_is_here, cdrw_device);
     780        paranoid_free(comment);
     781        paranoid_free(tmp);
     782        paranoid_free(cdr_exe);
     783        paranoid_free(command);
     784        return (0);
     785    }
    782786}
    783787
     
    794798 * @return 0 for success, nonzero for failure.
    795799 */
    796 int
    797 find_cdrom_device (char *output, bool try_to_mount)
    798 {
    799     /*@ pointers *****************************************************/
    800   FILE *fin;
    801   char *p;
    802   char *q;
    803   char *r;
    804   int retval=0;
    805 
    806     /*@ bool's *******************************************************/
    807   bool found_it = FALSE;
    808 
    809     /*@ buffers ******************************************************/
    810   char *tmp;
    811   char *cdr_exe;
    812   char *phrase_one;
    813   char *phrase_two;
    814   char *command;
    815   char *dvd_last_resort;
    816   char *mountpoint;
    817   static char the_last_place_i_found_it[MAX_STR_LEN] = "";
    818 
    819     /*@ intialize ****************************************************/
    820   malloc_string(tmp);
    821   malloc_string(cdr_exe);
    822   malloc_string(phrase_one);
    823   malloc_string(phrase_two);
    824   malloc_string(command);
    825   malloc_string(dvd_last_resort);
    826   malloc_string(mountpoint);
    827  
    828   output[0] = '\0';
    829   phrase_one[0] = '\0';
    830   phrase_two[0] = '\0';
    831   dvd_last_resort[0] = '\0';
    832 
    833     /*@ end vars *****************************************************/
    834 
    835   if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0]))
    836     {
    837       strcpy(output, g_cdrom_drive_is_here);
    838       log_msg(3, "Been there, done that. Returning %s", output);
    839       retval=0;
    840       goto end_of_find_cdrom_device;
    841     }
    842   if (the_last_place_i_found_it[0]!='\0' && !try_to_mount)
    843     {
    844         strcpy(output, the_last_place_i_found_it);
    845         log_msg(3, "find_cdrom_device() --- returning last found location - '%s'", output);
    846         retval=0;
    847     goto end_of_find_cdrom_device;
    848     }
    849 
    850   sprintf(mountpoint,"/tmp/cd.%d", (int)(random()%32767));
    851   make_hole_for_dir(mountpoint);
    852 
    853   if (find_home_of_exe("cdrecord"))
    854     { strcpy(cdr_exe, "cdrecord"); }
    855   else
    856     { strcpy(cdr_exe, "dvdrecord"); }
    857   tmp[0]='\0';
    858   if (!find_home_of_exe(cdr_exe))
    859     {
    860       strcpy(output, "/dev/cdrom");
    861       log_msg(4, "Can't find cdrecord; assuming %s", output);
    862       if (!does_device_exist(output))
    863         {
    864           log_msg(4, "That didn't work. Sorry.");
    865           retval=1;
    866           goto end_of_find_cdrom_device;
    867         }
    868       else
    869         {
    870           retval=0;
    871           goto end_of_find_cdrom_device;
    872         }
    873     }
    874 
    875   sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
    876   fin = popen (command, "r");
    877   if (!fin) { log_msg(4, "command=%s", command); log_OS_error("Cannot popen command"); return (1); }
    878   for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin);
    879        fgets (tmp, MAX_STR_LEN, fin))
    880     {
    881       p = strchr (tmp, '\'');
    882       if (p)
    883     {
    884       q = strchr (++p, '\'');
    885       if (q)
    886         {
    887           for (r = q; *(r - 1) == ' '; r--);
    888           *r = '\0';
    889           strcpy (phrase_one, p);
    890           p = strchr (++q, '\'');
    891           if (p)
    892         {
    893           q = strchr (++p, '\'');
    894           if (q)
    895             {
    896               while (*(q - 1) == ' ')
    897             {
    898               q--;
    899             }
    900               *q = '\0';
    901               strcpy (phrase_two, p);
    902             }
    903         }
    904         }
    905     }
    906     }
    907   paranoid_pclose(fin);
     800int find_cdrom_device(char *output, bool try_to_mount)
     801{
     802    /*@ pointers **************************************************** */
     803    FILE *fin;
     804    char *p;
     805    char *q;
     806    char *r;
     807    int retval = 0;
     808
     809    /*@ bool's ****************************************************** */
     810    bool found_it = FALSE;
     811
     812    /*@ buffers ***************************************************** */
     813    char *tmp;
     814    char *cdr_exe;
     815    char *phrase_one;
     816    char *phrase_two;
     817    char *command;
     818    char *dvd_last_resort;
     819    char *mountpoint;
     820    static char the_last_place_i_found_it[MAX_STR_LEN] = "";
     821
     822    /*@ intialize *************************************************** */
     823    malloc_string(tmp);
     824    malloc_string(cdr_exe);
     825    malloc_string(phrase_one);
     826    malloc_string(phrase_two);
     827    malloc_string(command);
     828    malloc_string(dvd_last_resort);
     829    malloc_string(mountpoint);
     830
     831    output[0] = '\0';
     832    phrase_one[0] = '\0';
     833    phrase_two[0] = '\0';
     834    dvd_last_resort[0] = '\0';
     835
     836    /*@ end vars **************************************************** */
     837
     838    if (g_cdrom_drive_is_here[0] && !isdigit(g_cdrom_drive_is_here[0])) {
     839        strcpy(output, g_cdrom_drive_is_here);
     840        log_msg(3, "Been there, done that. Returning %s", output);
     841        retval = 0;
     842        goto end_of_find_cdrom_device;
     843    }
     844    if (the_last_place_i_found_it[0] != '\0' && !try_to_mount) {
     845        strcpy(output, the_last_place_i_found_it);
     846        log_msg(3,
     847                "find_cdrom_device() --- returning last found location - '%s'",
     848                output);
     849        retval = 0;
     850        goto end_of_find_cdrom_device;
     851    }
     852
     853    sprintf(mountpoint, "/tmp/cd.%d", (int) (random() % 32767));
     854    make_hole_for_dir(mountpoint);
     855
     856    if (find_home_of_exe("cdrecord")) {
     857        strcpy(cdr_exe, "cdrecord");
     858    } else {
     859        strcpy(cdr_exe, "dvdrecord");
     860    }
     861    tmp[0] = '\0';
     862    if (!find_home_of_exe(cdr_exe)) {
     863        strcpy(output, "/dev/cdrom");
     864        log_msg(4, "Can't find cdrecord; assuming %s", output);
     865        if (!does_device_exist(output)) {
     866            log_msg(4, "That didn't work. Sorry.");
     867            retval = 1;
     868            goto end_of_find_cdrom_device;
     869        } else {
     870            retval = 0;
     871            goto end_of_find_cdrom_device;
     872        }
     873    }
     874
     875    sprintf(command, "%s -scanbus 2> /dev/null", cdr_exe);
     876    fin = popen(command, "r");
     877    if (!fin) {
     878        log_msg(4, "command=%s", command);
     879        log_OS_error("Cannot popen command");
     880        return (1);
     881    }
     882    for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
     883         fgets(tmp, MAX_STR_LEN, fin)) {
     884        p = strchr(tmp, '\'');
     885        if (p) {
     886            q = strchr(++p, '\'');
     887            if (q) {
     888                for (r = q; *(r - 1) == ' '; r--);
     889                *r = '\0';
     890                strcpy(phrase_one, p);
     891                p = strchr(++q, '\'');
     892                if (p) {
     893                    q = strchr(++p, '\'');
     894                    if (q) {
     895                        while (*(q - 1) == ' ') {
     896                            q--;
     897                        }
     898                        *q = '\0';
     899                        strcpy(phrase_two, p);
     900                    }
     901                }
     902            }
     903        }
     904    }
     905    paranoid_pclose(fin);
    908906
    909907#ifndef __FreeBSD__
    910   if (strlen(phrase_two)==0)
    911     { log_msg(4, "Not running phase two. String is empty."); }
    912   else
    913     {
    914       sprintf (command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
    915       fin = popen (command, "r");
    916       if (!fin)
    917         {
    918           log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
    919         }
    920       else
    921         {
    922           for (fgets (tmp, MAX_STR_LEN, fin); !feof (fin); fgets (tmp, MAX_STR_LEN, fin))
    923         {
    924               log_msg(5, "--> '%s'", tmp);
    925               if (tmp[0] != ' ' && tmp[1] != ' ')
    926             {
    927               p = strchr (tmp, ':');
    928               if (p)
    929             {
    930               *p = '\0';
    931               if (strstr(tmp, "DVD"))
    932                 {
    933                   sprintf(dvd_last_resort, "/dev/%s", tmp);
    934                   log_msg(4, "Ignoring '%s' because it's a DVD drive", tmp);
    935                 }
    936               else
    937                 {
    938                   sprintf (output, "/dev/%s", tmp);
    939                   found_it = TRUE;
    940                 }
    941             }
    942                 }
    943             }
    944           paranoid_pclose(fin);
    945         }
    946     }
     908    if (strlen(phrase_two) == 0) {
     909        log_msg(4, "Not running phase two. String is empty.");
     910    } else {
     911        sprintf(command, "dmesg | grep \"%s\" 2> /dev/null", phrase_two);
     912        fin = popen(command, "r");
     913        if (!fin) {
     914            log_msg(4, "Cannot run 2nd command - non-fatal, fortunately");
     915        } else {
     916            for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
     917                 fgets(tmp, MAX_STR_LEN, fin)) {
     918                log_msg(5, "--> '%s'", tmp);
     919                if (tmp[0] != ' ' && tmp[1] != ' ') {
     920                    p = strchr(tmp, ':');
     921                    if (p) {
     922                        *p = '\0';
     923                        if (strstr(tmp, "DVD")) {
     924                            sprintf(dvd_last_resort, "/dev/%s", tmp);
     925                            log_msg(4,
     926                                    "Ignoring '%s' because it's a DVD drive",
     927                                    tmp);
     928                        } else {
     929                            sprintf(output, "/dev/%s", tmp);
     930                            found_it = TRUE;
     931                        }
     932                    }
     933                }
     934            }
     935            paranoid_pclose(fin);
     936        }
     937    }
    947938
    948939#endif
    949940#ifdef __FreeBSD__
    950   if (!found_it)
    951       { log_msg(4, "OK, approach 2"); if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom")))
    952         { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom1")))
    953           { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/dvd")))
    954             { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/acd0")))
    955               { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cd01")))
    956                 { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/acd1")))
    957                   { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cd1")))
    958               { retval=1; goto end_of_find_cdrom_device; }
    959           } } } } } } }
     941    if (!found_it) {
     942        log_msg(4, "OK, approach 2");
     943        if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
     944            if (!
     945                (found_it =
     946                 set_dev_to_this_if_rx_OK(output, "/dev/cdrom1"))) {
     947                if (!
     948                    (found_it =
     949                     set_dev_to_this_if_rx_OK(output, "/dev/dvd"))) {
     950                    if (!
     951                        (found_it =
     952                         set_dev_to_this_if_rx_OK(output, "/dev/acd0"))) {
     953                        if (!
     954                            (found_it =
     955                             set_dev_to_this_if_rx_OK(output,
     956                                                      "/dev/cd01"))) {
     957                            if (!
     958                                (found_it =
     959                                 set_dev_to_this_if_rx_OK(output,
     960                                                          "/dev/acd1"))) {
     961                                if (!
     962                                    (found_it =
     963                                     set_dev_to_this_if_rx_OK(output,
     964                                                              "/dev/cd1")))
     965                                {
     966                                    retval = 1;
     967                                    goto end_of_find_cdrom_device;
     968                                }
     969                            }
     970                        }
     971                    }
     972                }
     973            }
     974        }
     975    }
    960976#else
    961   if (!found_it && strlen(dvd_last_resort)>0)
    962     {
    963       log_msg(4, "Well, I'll use the DVD - %s - as a last resort", dvd_last_resort);
    964       strcpy(output, dvd_last_resort);
    965       found_it = TRUE;
    966     }
    967   if (found_it)
    968     {
    969       sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null", strrchr(output,'/')+1);
    970       if (system(tmp) == 0)
    971     {
    972       log_msg(4, "%s is not right. It's being SCSI-emulated. Continuing.", output);
    973       found_it = FALSE;
    974       output[0] = '\0';
    975     }
    976     }
    977 
    978   if (found_it)
    979     {
    980       log_msg(4, "(find_cdrom_device) --> '%s'", output);
    981       if (!does_device_exist(output)) { found_it=FALSE; log_msg(4, "OK, I was wrong, I haven't found it... yet."); }
    982     }
    983 
    984   if (!found_it)
    985       { log_msg(4, "OK, approach 2"); if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0")))
    986         { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0" )))
    987           { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom")))
    988             { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom0")))
    989               { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/cdrom1")))
    990         { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr1")))
    991                   { if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/dvd")))
    992             { if (!(found_it = set_dev_to_this_if_rx_OK(output, g_cdrw_drive_is_here)))
    993               { retval=1; goto end_of_find_cdrom_device; }
    994             } } } } } } } }
    995 #endif
    996 
    997   if (found_it && try_to_mount)
    998     {
    999       if (mount_CDROM_here (output, mountpoint))
    1000         {
    1001           log_msg(4, "[Cardigans] I've changed my mind");
    1002           found_it = FALSE;
    1003         }
    1004       else
    1005         {
    1006           sprintf(tmp, "%s/archives", mountpoint);
    1007           if (!does_file_exist(tmp))
    1008             {
    1009               log_msg(4, "[Cardigans] I'll take it back");
    1010               found_it = FALSE;
    1011             }
    1012           else
    1013             {
    1014               sprintf(command, "umount %s", output);
    1015               paranoid_system(command);
    1016               log_msg(4, "I'm confident the Mondo CD is in %s", output);
    1017         }
    1018         }
    1019     }
    1020   unlink(mountpoint);
    1021 
    1022   if (found_it)
    1023     {
    1024       if (!does_file_exist(output)) { log_msg(3, "I still haven't found it."); return(1); }
    1025       log_msg(3, "(find_cdrom_device) --> '%s'", output);
    1026       strcpy(the_last_place_i_found_it, output);
    1027       strcpy(g_cdrom_drive_is_here, output);
    1028       retval=0;
    1029       goto end_of_find_cdrom_device;
    1030     }
    1031 
    1032   sprintf(command, "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2", cdr_exe, g_cdrw_drive_is_here);
    1033   log_msg(1, "command=%s", command);
    1034   strcpy(tmp, call_program_and_get_last_line_of_output(command));
    1035   if (tmp[0])
    1036     {
    1037       strcpy(output, tmp);
    1038       log_msg(4, "Finally found it at %s", output);
    1039       retval=0;
    1040       goto end_of_find_cdrom_device;
    1041     }
    1042   else
    1043     {
    1044       log_msg(4, "Still couldn't find it.");
    1045       retval=1;
    1046       goto end_of_find_cdrom_device;
    1047     }
    1048 end_of_find_cdrom_device:
    1049   paranoid_free(tmp);
    1050   paranoid_free(cdr_exe);
    1051   paranoid_free(phrase_one);
    1052   paranoid_free(phrase_two);
    1053   paranoid_free(command);
    1054   paranoid_free(dvd_last_resort);
    1055   paranoid_free(mountpoint);
    1056   return(retval);
    1057 }
    1058 
    1059 
    1060 
    1061 
    1062 
    1063 int find_dvd_device(char*output, bool try_to_mount)
    1064 {
    1065   char *command;
    1066   char *tmp;
    1067   int retval=0, devno=-1;
    1068 
    1069   malloc_string(command);
    1070   malloc_string(tmp);
    1071 
    1072     if (g_dvd_drive_is_here[0])
    1073     {
    1074       strcpy(output, g_dvd_drive_is_here);
    1075       log_msg(3, "Been there, done that. Returning %s", output);
    1076       return(0);
    1077     }
    1078 
    1079   sprintf(tmp, call_program_and_get_last_line_of_output
    1080             (
    1081 "dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1"
    1082             )
     977    if (!found_it && strlen(dvd_last_resort) > 0) {
     978        log_msg(4, "Well, I'll use the DVD - %s - as a last resort",
     979                dvd_last_resort);
     980        strcpy(output, dvd_last_resort);
     981        found_it = TRUE;
     982    }
     983    if (found_it) {
     984        sprintf(tmp, "grep \"%s=ide-scsi\" /proc/cmdline &> /dev/null",
     985                strrchr(output, '/') + 1);
     986        if (system(tmp) == 0) {
     987            log_msg(4,
     988                    "%s is not right. It's being SCSI-emulated. Continuing.",
     989                    output);
     990            found_it = FALSE;
     991            output[0] = '\0';
     992        }
     993    }
     994
     995    if (found_it) {
     996        log_msg(4, "(find_cdrom_device) --> '%s'", output);
     997        if (!does_device_exist(output)) {
     998            found_it = FALSE;
     999            log_msg(4, "OK, I was wrong, I haven't found it... yet.");
     1000        }
     1001    }
     1002
     1003    if (!found_it) {
     1004        log_msg(4, "OK, approach 2");
     1005        if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/scd0"))) {
     1006            if (!(found_it = set_dev_to_this_if_rx_OK(output, "/dev/sr0"))) {
     1007                if (!
     1008                    (found_it =
     1009                     set_dev_to_this_if_rx_OK(output, "/dev/cdrom"))) {
     1010                    if (!
     1011                        (found_it =
     1012                         set_dev_to_this_if_rx_OK(output,
     1013                                                  "/dev/cdrom0"))) {
     1014                        if (!
     1015                            (found_it =
     1016                             set_dev_to_this_if_rx_OK(output,
     1017                                                      "/dev/cdrom1"))) {
     1018                            if (!
     1019                                (found_it =
     1020                                 set_dev_to_this_if_rx_OK(output,
     1021                                                          "/dev/sr1"))) {
     1022                                if (!
     1023                                    (found_it =
     1024                                     set_dev_to_this_if_rx_OK(output,
     1025                                                              "/dev/dvd")))
     1026                                {
     1027                                    if (!
     1028                                        (found_it =
     1029                                         set_dev_to_this_if_rx_OK(output,
     1030                                                                  g_cdrw_drive_is_here)))
     1031                                    {
     1032                                        retval = 1;
     1033                                        goto end_of_find_cdrom_device;
     1034                                    }
     1035                                }
     1036                            }
     1037                        }
     1038                    }
     1039                }
     1040            }
     1041        }
     1042    }
     1043#endif
     1044
     1045    if (found_it && try_to_mount) {
     1046        if (mount_CDROM_here(output, mountpoint)) {
     1047            log_msg(4, "[Cardigans] I've changed my mind");
     1048            found_it = FALSE;
     1049        } else {
     1050            sprintf(tmp, "%s/archives", mountpoint);
     1051            if (!does_file_exist(tmp)) {
     1052                log_msg(4, "[Cardigans] I'll take it back");
     1053                found_it = FALSE;
     1054            } else {
     1055                sprintf(command, "umount %s", output);
     1056                paranoid_system(command);
     1057                log_msg(4, "I'm confident the Mondo CD is in %s", output);
     1058            }
     1059        }
     1060    }
     1061    unlink(mountpoint);
     1062
     1063    if (found_it) {
     1064        if (!does_file_exist(output)) {
     1065            log_msg(3, "I still haven't found it.");
     1066            return (1);
     1067        }
     1068        log_msg(3, "(find_cdrom_device) --> '%s'", output);
     1069        strcpy(the_last_place_i_found_it, output);
     1070        strcpy(g_cdrom_drive_is_here, output);
     1071        retval = 0;
     1072        goto end_of_find_cdrom_device;
     1073    }
     1074
     1075    sprintf(command,
     1076            "%s -scanbus | grep \"[0-9],[0-9],[0-9]\" | grep \"[D|C][V|D]\" | grep -n \"\" | grep \"%s\" | cut -d':' -f2",
     1077            cdr_exe, g_cdrw_drive_is_here);
     1078    log_msg(1, "command=%s", command);
     1079    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1080    if (tmp[0]) {
     1081        strcpy(output, tmp);
     1082        log_msg(4, "Finally found it at %s", output);
     1083        retval = 0;
     1084        goto end_of_find_cdrom_device;
     1085    } else {
     1086        log_msg(4, "Still couldn't find it.");
     1087        retval = 1;
     1088        goto end_of_find_cdrom_device;
     1089    }
     1090  end_of_find_cdrom_device:
     1091    paranoid_free(tmp);
     1092    paranoid_free(cdr_exe);
     1093    paranoid_free(phrase_one);
     1094    paranoid_free(phrase_two);
     1095    paranoid_free(command);
     1096    paranoid_free(dvd_last_resort);
     1097    paranoid_free(mountpoint);
     1098    return (retval);
     1099}
     1100
     1101
     1102
     1103
     1104
     1105int find_dvd_device(char *output, bool try_to_mount)
     1106{
     1107    char *command;
     1108    char *tmp;
     1109    int retval = 0, devno = -1;
     1110
     1111    malloc_string(command);
     1112    malloc_string(tmp);
     1113
     1114    if (g_dvd_drive_is_here[0]) {
     1115        strcpy(output, g_dvd_drive_is_here);
     1116        log_msg(3, "Been there, done that. Returning %s", output);
     1117        return (0);
     1118    }
     1119
     1120    sprintf(tmp, call_program_and_get_last_line_of_output
     1121            ("dvdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
    10831122        );
    1084   log_msg(5, "tmp = '%s'", tmp);
    1085   if (!tmp[0]) sprintf (tmp, call_program_and_get_last_line_of_output
    1086                  (
    1087 "cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1"
    1088                              )
    1089                        );
    1090   if (tmp[0])
    1091     {
    1092       devno=atoi(tmp) - 1;
    1093     }
    1094   if (devno>=0)
    1095     {
    1096       retval=0;
    1097       sprintf(output, "/dev/scd%d", devno);
    1098       strcpy(g_dvd_drive_is_here, output);
    1099       log_msg(2, "I think DVD is at %s", output);
    1100     }
    1101   else
    1102     {
    1103       log_msg(2,"I cannot find DVD");
    1104       retval=1;
    1105     }
    1106 
    1107   if (try_to_mount)
    1108     {
    1109       log_msg(1, "Ignoring the fact that try_to_mount==TRUE");
    1110     }
    1111   return(retval);
     1123    log_msg(5, "tmp = '%s'", tmp);
     1124    if (!tmp[0])
     1125        sprintf(tmp, call_program_and_get_last_line_of_output
     1126                ("cdrecord -scanbus 2> /dev/null | grep \") '\" | grep -n \"\" | grep DVD | cut -d':' -f1")
     1127            );
     1128    if (tmp[0]) {
     1129        devno = atoi(tmp) - 1;
     1130    }
     1131    if (devno >= 0) {
     1132        retval = 0;
     1133        sprintf(output, "/dev/scd%d", devno);
     1134        strcpy(g_dvd_drive_is_here, output);
     1135        log_msg(2, "I think DVD is at %s", output);
     1136    } else {
     1137        log_msg(2, "I cannot find DVD");
     1138        retval = 1;
     1139    }
     1140
     1141    if (try_to_mount) {
     1142        log_msg(1, "Ignoring the fact that try_to_mount==TRUE");
     1143    }
     1144    return (retval);
    11121145}
    11131146
     
    11241157 * @return size in megabytes.
    11251158 */
    1126 long
    1127 get_phys_size_of_drive (char *drive)
    1128 {
    1129     int fd;
     1159long get_phys_size_of_drive(char *drive)
     1160{
     1161    int fd;
    11301162#if linux
    1131     unsigned long long s=0;
    1132     int fileid, cylinders=0, cylindersleft=0;
    1133     int cylindersize=0;
     1163    unsigned long long s = 0;
     1164    int fileid, cylinders = 0, cylindersleft = 0;
     1165    int cylindersize = 0;
    11341166    int gotgeo = 0;
    11351167
    1136    
    1137   struct hd_geometry hdgeo;
     1168
     1169    struct hd_geometry hdgeo;
    11381170#elif __FreeBSD__
    1139     off_t s;
    1140 #endif
    1141 
    1142     long outvalA=-1;
    1143     long outvalB=-1;
    1144     long outvalC=-1;
    1145 
    1146     if ((fd = open (drive, O_RDONLY)) != -1) {
    1147     if (ioctl (fd,
     1171    off_t s;
     1172#endif
     1173
     1174    long outvalA = -1;
     1175    long outvalB = -1;
     1176    long outvalC = -1;
     1177
     1178    if ((fd = open(drive, O_RDONLY)) != -1) {
     1179        if (ioctl(fd,
    11481180#if linux
    1149     #ifdef BLKGETSIZE64
    1150             BLKGETSIZE64,
    1151     #else
    1152             BLKGETSIZE,
    1153    #endif
     1181#ifdef BLKGETSIZE64
     1182                  BLKGETSIZE64,
     1183#else
     1184                  BLKGETSIZE,
     1185#endif
    11541186#elif __FreeBSD__
    1155            DIOCGMEDIASIZE,
    1156 #endif
    1157            &s) != -1) {
    1158         close (fd);
    1159                     // s>>11 works for older disks but not for newer ones
    1160         outvalB =
     1187                  DIOCGMEDIASIZE,
     1188#endif
     1189                  &s) != -1) {
     1190            close(fd);
     1191            // s>>11 works for older disks but not for newer ones
     1192            outvalB =
    11611193#if linux
    1162     #ifdef BLKGETSIZE64 
    1163           s >> 20
    1164     #else
    1165               s >> 11
    1166     #endif
     1194#ifdef BLKGETSIZE64
     1195                s >> 20
    11671196#else
    1168               s >> 20
    1169 #endif
    1170 ;
    1171     }
    1172     }
    1173 
    1174     if (outvalB<=0)
    1175       { log_msg (1, "Error getting size of %s: %s", drive, strerror (errno)); }
     1197                s >> 11
     1198#endif
     1199#else
     1200                s >> 20
     1201#endif
     1202                ;
     1203        }
     1204    }
     1205
     1206    if (outvalB <= 0) {
     1207        log_msg(1, "Error getting size of %s: %s", drive, strerror(errno));
     1208    }
    11761209#if linux
    1177     fileid = open (drive, O_RDONLY);
     1210    fileid = open(drive, O_RDONLY);
    11781211    if (fileid) {
    1179         if (ioctl (fileid, HDIO_GETGEO, &hdgeo) != -1) {
     1212        if (ioctl(fileid, HDIO_GETGEO, &hdgeo) != -1) {
    11801213            if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
    11811214                cylindersleft = cylinders = hdgeo.cylinders;
    11821215                cylindersize = hdgeo.heads * hdgeo.sectors / 2;
    11831216                outvalA = cylindersize * cylinders / 1024;
    1184                 log_msg (2, "Got Harddisk geometry, C:%d, H:%d, S:%d", hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
     1217                log_msg(2, "Got Harddisk geometry, C:%d, H:%d, S:%d",
     1218                        hdgeo.cylinders, hdgeo.heads, hdgeo.sectors);
    11851219                gotgeo = 1;
    11861220            } else {
    1187                 log_msg (1, "Harddisk geometry wrong");
     1221                log_msg(1, "Harddisk geometry wrong");
    11881222            }
    11891223        } else {
    1190             log_msg (1, "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode", strerror(errno));
     1224            log_msg(1,
     1225                    "Error in ioctl() getting new hard disk geometry (%s), resizing in unsafe mode",
     1226                    strerror(errno));
    11911227        }
    11921228        close(fileid);
    11931229    } else {
    1194         log_msg (1, "Failed to open %s for reading: %s", drive, strerror(errno));
     1230        log_msg(1, "Failed to open %s for reading: %s", drive,
     1231                strerror(errno));
    11951232    }
    11961233    if (!gotgeo) {
    1197         log_msg (1, "Failed to get harddisk geometry, using old mode");
     1234        log_msg(1, "Failed to get harddisk geometry, using old mode");
    11981235    }
    11991236/* 
     
    12131250// NEWER DISKS will give sane value for outvalB (close to outvalA, in other words) :)
    12141251
    1215   outvalC = (outvalA > outvalB) ? outvalA : outvalB;
    1216  
     1252    outvalC = (outvalA > outvalB) ? outvalA : outvalB;
     1253
    12171254//  log_msg (5, "drive = %s, error = %s", drive, strerror (errno));
    12181255//  fatal_error ("GPSOD: Unable to get size of drive");
    1219   log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB, outvalC);
    1220  
    1221   return(outvalC);
    1222 }
    1223    
     1256    log_msg(1, "%s --> %ld or %ld --> %ld", drive, outvalA, outvalB,
     1257            outvalC);
     1258
     1259    return (outvalC);
     1260}
     1261
    12241262/* The old version */
    1225 #if 0
    1226 long
    1227 get_phys_size_of_drive (char *drive)
    1228 {
    1229     /*@ pointers *****************************************************/
     1263#if 0
     1264long get_phys_size_of_drive(char *drive)
     1265{
     1266    /*@ pointers **************************************************** */
    12301267#if linux
    1231   FILE *fin;
    1232   char *p;
    1233   char *q;
    1234   char *r;
    1235     /*@ buffers ******************************************************/
    1236   char *tmp;
    1237   char *command;
    1238 
    1239     /*@ long *********************************************************/
    1240   long outL;
    1241   long tempLa;
    1242   long tempLb;
    1243   long tempLc;
    1244 
    1245 #endif
    1246 
    1247   struct hd_geometry hdgeo;
    1248   int fd;
     1268    FILE *fin;
     1269    char *p;
     1270    char *q;
     1271    char *r;
     1272    /*@ buffers ***************************************************** */
     1273    char *tmp;
     1274    char *command;
     1275
     1276    /*@ long ******************************************************** */
     1277    long outL;
     1278    long tempLa;
     1279    long tempLb;
     1280    long tempLc;
     1281
     1282#endif
     1283
     1284    struct hd_geometry hdgeo;
     1285    int fd;
    12491286
    12501287#ifdef __FreeBSD__
    1251   off_t o;
    1252 
    1253   if ((fd = open (drive, O_RDONLY)) != -1) {
    1254       if (ioctl (fd, DIOCGMEDIASIZE, &o) != -1) {
    1255       close (fd);
    1256       return (long) (o / (off_t) (1024 * 1024));
    1257       }
    1258       close (fd);
    1259   }
    1260   log_msg (4, "drive = %s, error = %s", drive, strerror (errno));
    1261   fatal_error ("GPSOD: Unable to get size of drive");
     1288    off_t o;
     1289
     1290    if ((fd = open(drive, O_RDONLY)) != -1) {
     1291        if (ioctl(fd, DIOCGMEDIASIZE, &o) != -1) {
     1292            close(fd);
     1293            return (long) (o / (off_t) (1024 * 1024));
     1294        }
     1295        close(fd);
     1296    }
     1297    log_msg(4, "drive = %s, error = %s", drive, strerror(errno));
     1298    fatal_error("GPSOD: Unable to get size of drive");
    12621299#else
    12631300
    1264   malloc_string(tmp);
    1265   malloc_string(command);
    1266 
    1267   if ((fd = open (drive, O_RDONLY)) != -1) {
    1268       if (ioctl (fd, HDIO_GETGEO, &hdgeo) != -1)  {
    1269       close (fd);
    1270       log_msg (2, "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB", drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors, (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
    1271       if ( hdgeo.cylinders && hdgeo.heads && hdgeo.sectors ) {
    1272           return ((long) (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 / 1024));
    1273       }
    1274       }
    1275       close (fd);
    1276   }
    1277 
    1278   assert_string_is_neither_NULL_nor_zerolength(drive);
    1279 
    1280   sprintf (command,
    1281        "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16",
    1282        drive);
    1283   strcpy (tmp, call_program_and_get_last_line_of_output (command));
    1284   if (tmp[0])
    1285     {
    1286       p = tmp;
    1287       q = strchr (p, ' ');
    1288       if (q)
    1289     {
    1290       *(q++) = '\0';
    1291       r = strchr (q, ' ');
    1292       if (r)
    1293         {
    1294           *(r++) = '\0';
    1295           tempLa = atol (p);
    1296           tempLb = atol (q);
    1297           tempLc = atol (r);
    1298           outL = tempLa * tempLb / 1024 * tempLc / 1024;
    1299           if (outL > 100)
    1300         {
    1301       paranoid_free(tmp);
    1302       paranoid_free(command);
    1303           return (outL);
    1304         }
    1305         }
    1306     }
    1307     }
    1308 
    1309   /* try to grep for 'Drive xxxx: yyy MB' */
    1310   sprintf(command, "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3", drive);
    1311   strcpy(tmp, call_program_and_get_last_line_of_output(command));
    1312   if (atol(tmp) > 0)
    1313     {
    1314       paranoid_free(tmp);
    1315       paranoid_free(command);
    1316       return (atol(tmp));
    1317     }
    1318 
    1319   /* else, do it the old-fashioned way */
    1320   p = strrchr (drive, (int) '/');
    1321   if (p)
    1322     {
    1323       strcpy (tmp, p + 1);
    1324     }
    1325   else
    1326     {
    1327       paranoid_free(tmp);
    1328       paranoid_free(command);
    1329       return (-1);
    1330     }
    1331   sprintf (command, "dmesg | grep %s 2> /dev/null", tmp);
    1332   if (!(fin = popen (command, "r")))
    1333     { log_OS_error("Cannot popen dmesg command"); }
    1334   else
    1335     {
    1336       fgets (tmp, MAX_STR_LEN - 1, fin);
    1337       while (!feof (fin) && !strstr (tmp, "GB") && !strstr (tmp, "MB"))
    1338         {
    1339           fgets (tmp, MAX_STR_LEN - 1, fin);
    1340         }
    1341       if (pclose (fin)) { log_OS_error("Cannot pclose dmesg fin"); }
    1342     }
    1343   if (!(p = strstr (tmp, "GB")) && !(p = strstr (tmp, "MB")))
    1344     {
    1345       log_msg (3, "Cannot find %s's size: dmesg isn't helping either.",
    1346            drive);
    1347       paranoid_free(tmp);
    1348       paranoid_free(command);
    1349       return (-1);
    1350     }
    1351   for (; !isdigit (*(p - 1)); p--);
    1352   *p = '\0';
    1353   for (p--; isdigit (*(p - 1)); p--);
    1354   outL = atol (p);
    1355   if (outL <= 0)
    1356     {
    1357       paranoid_free(tmp);
    1358       paranoid_free(command);
    1359       return (-1);
    1360     }
    1361   if (strstr (tmp, "GB"))
    1362     {
    1363       outL = outL * 1024;
    1364     }
    1365       paranoid_free(tmp);
    1366       paranoid_free(command);
    1367   return (outL * 19 / 20);
    1368 #endif
    1369 }
    1370 #endif /* 0 */
     1301    malloc_string(tmp);
     1302    malloc_string(command);
     1303
     1304    if ((fd = open(drive, O_RDONLY)) != -1) {
     1305        if (ioctl(fd, HDIO_GETGEO, &hdgeo) != -1) {
     1306            close(fd);
     1307            log_msg(2,
     1308                    "Geometry of drive %s is C:%d, H:%d, S%d, its size is %d MB",
     1309                    drive, hdgeo.cylinders, hdgeo.heads, hdgeo.sectors,
     1310                    (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors / 2 /
     1311                     1024));
     1312            if (hdgeo.cylinders && hdgeo.heads && hdgeo.sectors) {
     1313                return ((long)
     1314                        (hdgeo.cylinders * hdgeo.heads * hdgeo.sectors /
     1315                         2 / 1024));
     1316            }
     1317        }
     1318        close(fd);
     1319    }
     1320
     1321    assert_string_is_neither_NULL_nor_zerolength(drive);
     1322
     1323    sprintf(command,
     1324            "parted2fdisk -l %s | head -n4 | tr -s '\n' '\t' | tr -s ' ' '\t' | cut -f8,14,16",
     1325            drive);
     1326    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1327    if (tmp[0]) {
     1328        p = tmp;
     1329        q = strchr(p, ' ');
     1330        if (q) {
     1331            *(q++) = '\0';
     1332            r = strchr(q, ' ');
     1333            if (r) {
     1334                *(r++) = '\0';
     1335                tempLa = atol(p);
     1336                tempLb = atol(q);
     1337                tempLc = atol(r);
     1338                outL = tempLa * tempLb / 1024 * tempLc / 1024;
     1339                if (outL > 100) {
     1340                    paranoid_free(tmp);
     1341                    paranoid_free(command);
     1342                    return (outL);
     1343                }
     1344            }
     1345        }
     1346    }
     1347
     1348    /* try to grep for 'Drive xxxx: yyy MB' */
     1349    sprintf(command,
     1350            "parted2fdisk -l %s | grep MB | tr -s ' ' '\t' | cut -f3",
     1351            drive);
     1352    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     1353    if (atol(tmp) > 0) {
     1354        paranoid_free(tmp);
     1355        paranoid_free(command);
     1356        return (atol(tmp));
     1357    }
     1358
     1359    /* else, do it the old-fashioned way */
     1360    p = strrchr(drive, (int) '/');
     1361    if (p) {
     1362        strcpy(tmp, p + 1);
     1363    } else {
     1364        paranoid_free(tmp);
     1365        paranoid_free(command);
     1366        return (-1);
     1367    }
     1368    sprintf(command, "dmesg | grep %s 2> /dev/null", tmp);
     1369    if (!(fin = popen(command, "r"))) {
     1370        log_OS_error("Cannot popen dmesg command");
     1371    } else {
     1372        fgets(tmp, MAX_STR_LEN - 1, fin);
     1373        while (!feof(fin) && !strstr(tmp, "GB") && !strstr(tmp, "MB")) {
     1374            fgets(tmp, MAX_STR_LEN - 1, fin);
     1375        }
     1376        if (pclose(fin)) {
     1377            log_OS_error("Cannot pclose dmesg fin");
     1378        }
     1379    }
     1380    if (!(p = strstr(tmp, "GB")) && !(p = strstr(tmp, "MB"))) {
     1381        log_msg(3, "Cannot find %s's size: dmesg isn't helping either.",
     1382                drive);
     1383        paranoid_free(tmp);
     1384        paranoid_free(command);
     1385        return (-1);
     1386    }
     1387    for (; !isdigit(*(p - 1)); p--);
     1388    *p = '\0';
     1389    for (p--; isdigit(*(p - 1)); p--);
     1390    outL = atol(p);
     1391    if (outL <= 0) {
     1392        paranoid_free(tmp);
     1393        paranoid_free(command);
     1394        return (-1);
     1395    }
     1396    if (strstr(tmp, "GB")) {
     1397        outL = outL * 1024;
     1398    }
     1399    paranoid_free(tmp);
     1400    paranoid_free(command);
     1401    return (outL * 19 / 20);
     1402#endif
     1403}
     1404#endif                          /* 0 */
    13711405
    13721406
     
    13801414 * @return TRUE if the format is supported, FALSE if not.
    13811415 */
    1382 bool
    1383 is_this_a_valid_disk_format (char *format)
    1384 {
    1385   char *good_formats;
     1416bool is_this_a_valid_disk_format(char *format)
     1417{
     1418    char *good_formats;
    13861419    char *command;
    13871420    char *format_sz;
    13881421
    1389   FILE *pin;
    1390   int retval;
    1391   malloc_string(good_formats);
    1392   malloc_string(command);
    1393   malloc_string(format_sz);
    1394 
    1395   assert_string_is_neither_NULL_nor_zerolength(format);
    1396 
    1397   sprintf (format_sz, "%s ", format);
     1422    FILE *pin;
     1423    int retval;
     1424    malloc_string(good_formats);
     1425    malloc_string(command);
     1426    malloc_string(format_sz);
     1427
     1428    assert_string_is_neither_NULL_nor_zerolength(format);
     1429
     1430    sprintf(format_sz, "%s ", format);
    13981431
    13991432#ifdef __FreeBSD__
    1400   sprintf (command,
    1401            "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
     1433    sprintf(command,
     1434            "lsvfs | tr -s '\t' ' ' | grep -v Filesys | grep -v -- -- | cut -d' ' -f1 | tr -s '\n' ' '");
    14021435#else
    1403   sprintf (command,
    1404        "cat /proc/filesystems | grep -v nodev | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
    1405 #endif
    1406 
    1407   pin = popen (command, "r");
    1408   if (!pin)
    1409     {
    1410       log_OS_error ("Unable to read good formats");
    1411       retval=0;
    1412     }
    1413   else
    1414     {
    1415       strcpy (good_formats, " ");
    1416       (void) fgets (good_formats + 1, MAX_STR_LEN, pin);
    1417       if (pclose (pin))
    1418         { log_OS_error("Cannot pclose good formats"); }
    1419       strip_spaces (good_formats);
    1420       strcat (good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
    1421       if (strstr (good_formats, format_sz))
    1422         {
    1423       retval=1;
    1424         }
    1425       else
    1426         {
    1427       retval=0;
    1428         }
    1429     }
    1430   paranoid_free(good_formats);
    1431   paranoid_free(command);
    1432   paranoid_free(format_sz);
    1433   return (retval);
     1436    sprintf(command,
     1437            "cat /proc/filesystems | grep -v nodev | tr -s '\t' ' ' | cut -d' ' -f2 | tr -s '\n' ' '");
     1438#endif
     1439
     1440    pin = popen(command, "r");
     1441    if (!pin) {
     1442        log_OS_error("Unable to read good formats");
     1443        retval = 0;
     1444    } else {
     1445        strcpy(good_formats, " ");
     1446        (void) fgets(good_formats + 1, MAX_STR_LEN, pin);
     1447        if (pclose(pin)) {
     1448            log_OS_error("Cannot pclose good formats");
     1449        }
     1450        strip_spaces(good_formats);
     1451        strcat(good_formats, " swap lvm raid ntfs 7 "); // " ntfs 7 " -- um, cheating much? :)
     1452        if (strstr(good_formats, format_sz)) {
     1453            retval = 1;
     1454        } else {
     1455            retval = 0;
     1456        }
     1457    }
     1458    paranoid_free(good_formats);
     1459    paranoid_free(command);
     1460    paranoid_free(format_sz);
     1461    return (retval);
    14341462}
    14351463
     
    14421470 * @return TRUE if it's mounted, FALSE if not.
    14431471 */
    1444 bool is_this_device_mounted (char *device_raw)
    1445 {
    1446 
    1447     /*@ pointers *****************************************************/
    1448   FILE *fin;
    1449 
    1450     /*@ buffers ******************************************************/
    1451   char *incoming;
    1452   char *device_with_tab;
    1453   char *device_with_space;
    1454   char *tmp;
    1455   int retval=0;
    1456  
     1472bool is_this_device_mounted(char *device_raw)
     1473{
     1474
     1475    /*@ pointers **************************************************** */
     1476    FILE *fin;
     1477
     1478    /*@ buffers ***************************************************** */
     1479    char *incoming;
     1480    char *device_with_tab;
     1481    char *device_with_space;
     1482    char *tmp;
     1483    int retval = 0;
     1484
    14571485#ifdef __FreeBSD__
    1458   #define SWAPLIST_COMMAND "swapinfo"
     1486#define SWAPLIST_COMMAND "swapinfo"
    14591487#else
    1460   #define SWAPLIST_COMMAND "cat /proc/swaps"
    1461 #endif
    1462 
    1463     /*@ end vars *****************************************************/
    1464 
    1465   malloc_string(incoming);
    1466   malloc_string(device_with_tab);
    1467   malloc_string(device_with_space);
    1468   malloc_string(tmp);
    1469   assert(device_raw!=NULL);
     1488#define SWAPLIST_COMMAND "cat /proc/swaps"
     1489#endif
     1490
     1491    /*@ end vars **************************************************** */
     1492
     1493    malloc_string(incoming);
     1494    malloc_string(device_with_tab);
     1495    malloc_string(device_with_space);
     1496    malloc_string(tmp);
     1497    assert(device_raw != NULL);
    14701498//  assert_string_is_neither_NULL_nor_zerolength(device_raw);
    1471   if (device_raw[0] != '/' && !strstr(device_raw, ":/"))
    1472     {
    1473       log_msg(1, "%s needs to have a '/' prefixed - I'll do it", device_raw);
    1474       sprintf(tmp, "/%s", device_raw);
    1475     }
    1476   else
    1477     {
    1478       strcpy(tmp, device_raw);
    1479     }
    1480   log_msg(1, "Is %s mounted?", tmp);
    1481   if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc"))
    1482     {
    1483       log_msg(1, "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
    1484       return(0);
    1485     }
    1486   sprintf (device_with_tab, "%s\t", tmp);
    1487   sprintf (device_with_space, "%s ", tmp);
    1488 
    1489   if (!(fin = popen ("mount", "r"))) { log_OS_error("Cannot popen 'mount'"); return(FALSE); }
    1490   for (fgets (incoming, MAX_STR_LEN - 1, fin); !feof (fin);
    1491        fgets (incoming, MAX_STR_LEN - 1, fin))
    1492     {
    1493       if ( strstr (incoming, device_with_space) //> incoming
    1494       || strstr (incoming, device_with_tab) ) // > incoming)
    1495     {
    1496       paranoid_pclose (fin);
    1497       retval=1;
    1498       goto end_of_func;
    1499     }
    1500     }
    1501   paranoid_pclose(fin);
    1502   sprintf (tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null",
    1503        SWAPLIST_COMMAND, device_with_space);
    1504   log_msg(4, "tmp (command) = '%s'", tmp);
    1505   if (!system (tmp))
    1506     {
    1507       retval=1;
    1508       goto end_of_func;
    1509     }
    1510 end_of_func:
    1511   paranoid_free(incoming);
    1512   paranoid_free(device_with_tab);
    1513   paranoid_free(device_with_space);
    1514   paranoid_free(tmp);
    1515   return (retval);
     1499    if (device_raw[0] != '/' && !strstr(device_raw, ":/")) {
     1500        log_msg(1, "%s needs to have a '/' prefixed - I'll do it",
     1501                device_raw);
     1502        sprintf(tmp, "/%s", device_raw);
     1503    } else {
     1504        strcpy(tmp, device_raw);
     1505    }
     1506    log_msg(1, "Is %s mounted?", tmp);
     1507    if (!strcmp(tmp, "/proc") || !strcmp(tmp, "proc")) {
     1508        log_msg(1,
     1509                "I don't know how the heck /proc made it into the mountlist. I'll ignore it.");
     1510        return (0);
     1511    }
     1512    sprintf(device_with_tab, "%s\t", tmp);
     1513    sprintf(device_with_space, "%s ", tmp);
     1514
     1515    if (!(fin = popen("mount", "r"))) {
     1516        log_OS_error("Cannot popen 'mount'");
     1517        return (FALSE);
     1518    }
     1519    for (fgets(incoming, MAX_STR_LEN - 1, fin); !feof(fin);
     1520         fgets(incoming, MAX_STR_LEN - 1, fin)) {
     1521        if (strstr(incoming, device_with_space) //> incoming
     1522            || strstr(incoming, device_with_tab))   // > incoming)
     1523        {
     1524            paranoid_pclose(fin);
     1525            retval = 1;
     1526            goto end_of_func;
     1527        }
     1528    }
     1529    paranoid_pclose(fin);
     1530    sprintf(tmp, "%s | grep -w \"%s\" > /dev/null 2> /dev/null",
     1531            SWAPLIST_COMMAND, device_with_space);
     1532    log_msg(4, "tmp (command) = '%s'", tmp);
     1533    if (!system(tmp)) {
     1534        retval = 1;
     1535        goto end_of_func;
     1536    }
     1537  end_of_func:
     1538    paranoid_free(incoming);
     1539    paranoid_free(device_with_tab);
     1540    paranoid_free(device_with_space);
     1541    paranoid_free(tmp);
     1542    return (retval);
    15161543}
    15171544
     
    15231550 * @return /dev entry for the device, or NULL if it couldn't be allocated.
    15241551 */
    1525 char *
    1526 make_vn (char *fname)
    1527 {
    1528     char *device   = (char*) malloc (MAX_STR_LEN);
    1529     char *mddevice = (char*) malloc (32);
    1530     char command[MAX_STR_LEN];
    1531     int vndev = 2;
    1532     if (atoi (call_program_and_get_last_line_of_output ("/sbin/sysctl -n kern.osreldate")) <
    1533         500000) {
    1534         do {
    1535             sprintf (mddevice, "vn%ic", vndev++);
    1536             sprintf (command, "vnconfig %s %s", mddevice, fname);
    1537             if (vndev > 10) {
    1538                 return NULL;
    1539             }
    1540         }
    1541        while (system (command));
    1542     } else {
    1543         sprintf (command, "mdconfig -a -t vnode -f %s", fname);
    1544         mddevice = call_program_and_get_last_line_of_output (command);
    1545         if (!strstr (mddevice, "md")) {
    1546             return NULL;
    1547         }
    1548     }
    1549     sprintf (device, "/dev/%s", mddevice);
    1550     return device;
     1552char *make_vn(char *fname)
     1553{
     1554    char *device = (char *) malloc(MAX_STR_LEN);
     1555    char *mddevice = (char *) malloc(32);
     1556    char command[MAX_STR_LEN];
     1557    int vndev = 2;
     1558    if (atoi
     1559        (call_program_and_get_last_line_of_output
     1560         ("/sbin/sysctl -n kern.osreldate")) < 500000) {
     1561        do {
     1562            sprintf(mddevice, "vn%ic", vndev++);
     1563            sprintf(command, "vnconfig %s %s", mddevice, fname);
     1564            if (vndev > 10) {
     1565                return NULL;
     1566            }
     1567        }
     1568        while (system(command));
     1569    } else {
     1570        sprintf(command, "mdconfig -a -t vnode -f %s", fname);
     1571        mddevice = call_program_and_get_last_line_of_output(command);
     1572        if (!strstr(mddevice, "md")) {
     1573            return NULL;
     1574        }
     1575    }
     1576    sprintf(device, "/dev/%s", mddevice);
     1577    return device;
    15511578}
    15521579
     
    15611588 * @return 0 for success, nonzero for failure.
    15621589 */
    1563 int
    1564 kick_vn (char *dname)
    1565 {
    1566     char command[MAX_STR_LEN];
    1567 
    1568     if (strncmp (dname, "/dev/", 5) == 0) {
    1569         dname += 5;
    1570     }
    1571 
    1572     if (atoi (call_program_and_get_last_line_of_output ("/sbin/sysctl -n kern.osreldate")) <
    1573         500000) {
    1574         sprintf (command, "vnconfig -d %s", dname);
    1575         return system (command);
    1576     } else {
    1577         sprintf (command, "mdconfig -d -u %s", dname);
    1578         return system (command);
    1579     }
    1580     /*NOTREACHED*/
    1581     return 255;
     1590int kick_vn(char *dname)
     1591{
     1592    char command[MAX_STR_LEN];
     1593
     1594    if (strncmp(dname, "/dev/", 5) == 0) {
     1595        dname += 5;
     1596    }
     1597
     1598    if (atoi
     1599        (call_program_and_get_last_line_of_output
     1600         ("/sbin/sysctl -n kern.osreldate")) < 500000) {
     1601        sprintf(command, "vnconfig -d %s", dname);
     1602        return system(command);
     1603    } else {
     1604        sprintf(command, "mdconfig -d -u %s", dname);
     1605        return system(command);
     1606    }
     1607     /*NOTREACHED*/ return 255;
    15821608}
    15831609#endif
     
    15901616 * @return 0 for success, nonzero for failure.
    15911617 */
    1592 int
    1593 mount_CDROM_here (char *device, char *mountpoint)
    1594 {
    1595     /*@ buffer *******************************************************/
    1596   char *command;
    1597   char *dev;
    1598   char *options;
    1599   int retval;
    1600 
    1601   malloc_string(command);
    1602   malloc_string(dev);
    1603   malloc_string(options);
    1604   assert_string_is_neither_NULL_nor_zerolength(device);
    1605   assert_string_is_neither_NULL_nor_zerolength(mountpoint);
    1606 
    1607   make_hole_for_dir(mountpoint);
    1608   strcpy(options, "ro");
    1609   if (isdigit(device[0]))
    1610     { find_cdrom_device(device, FALSE); }
    1611   else
    1612     { strcpy(dev, device); }
    1613   if (g_ISO_restore_mode)
    1614     {
     1618int mount_CDROM_here(char *device, char *mountpoint)
     1619{
     1620    /*@ buffer ****************************************************** */
     1621    char *command;
     1622    char *dev;
     1623    char *options;
     1624    int retval;
     1625
     1626    malloc_string(command);
     1627    malloc_string(dev);
     1628    malloc_string(options);
     1629    assert_string_is_neither_NULL_nor_zerolength(device);
     1630    assert_string_is_neither_NULL_nor_zerolength(mountpoint);
     1631
     1632    make_hole_for_dir(mountpoint);
     1633    strcpy(options, "ro");
     1634    if (isdigit(device[0])) {
     1635        find_cdrom_device(device, FALSE);
     1636    } else {
     1637        strcpy(dev, device);
     1638    }
     1639    if (g_ISO_restore_mode) {
    16151640
    16161641#ifdef __FreeBSD__
    1617       strcpy (dev, make_vn (device));
    1618       if (!dev) {
    1619           sprintf (command, "Unable to mount ISO (make_vn(%s) failed)", device);
    1620           fatal_error (command);
    1621         }
    1622       strcpy (device, dev);
     1642        strcpy(dev, make_vn(device));
     1643        if (!dev) {
     1644            sprintf(command, "Unable to mount ISO (make_vn(%s) failed)",
     1645                    device);
     1646            fatal_error(command);
     1647        }
     1648        strcpy(device, dev);
    16231649#else
    1624       strcat(options,",loop");
    1625 #endif
    1626 
    1627     }
    1628   log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device, mountpoint);
    1629     /*@ end vars ****************************************************/
     1650        strcat(options, ",loop");
     1651#endif
     1652
     1653    }
     1654    log_msg(4, "(mount_CDROM_here --- device=%s, mountpoint=%s", device,
     1655            mountpoint);
     1656    /*@ end vars *************************************************** */
    16301657
    16311658#ifdef __FreeBSD__
    1632   sprintf (command, "mount_cd9660 -r %s %s 2>> %s",
    1633            device, mountpoint, MONDO_LOGFILE);
     1659    sprintf(command, "mount_cd9660 -r %s %s 2>> %s",
     1660            device, mountpoint, MONDO_LOGFILE);
    16341661
    16351662#else
    1636   sprintf (command, "mount %s -o %s -t iso9660 %s 2>> %s",
    1637            device, options, mountpoint, MONDO_LOGFILE);
    1638 #endif
    1639 
    1640   log_msg (4, command);
    1641   if (strncmp(device, "/dev/", 5) == 0)
    1642     {
    1643       retract_CD_tray_and_defeat_autorun();
    1644     }
    1645   retval = system (command);
    1646   log_msg(1, "system(%s) returned %d", command, retval);
    1647 
    1648   paranoid_free(command);
    1649   paranoid_free(dev);
    1650   paranoid_free(options);
    1651   return(retval);
     1663    sprintf(command, "mount %s -o %s -t iso9660 %s 2>> %s",
     1664            device, options, mountpoint, MONDO_LOGFILE);
     1665#endif
     1666
     1667    log_msg(4, command);
     1668    if (strncmp(device, "/dev/", 5) == 0) {
     1669        retract_CD_tray_and_defeat_autorun();
     1670    }
     1671    retval = system(command);
     1672    log_msg(1, "system(%s) returned %d", command, retval);
     1673
     1674    paranoid_free(command);
     1675    paranoid_free(dev);
     1676    paranoid_free(options);
     1677    return (retval);
    16521678}
    16531679
     
    16691695 */
    16701696void
    1671 insist_on_this_cd_number (struct s_bkpinfo* bkpinfo, int cd_number_i_want)
    1672 {
    1673 
    1674     /*@ int **************************************************************/
    1675   int res = 0;
    1676 
    1677 
    1678     /*@ buffers **********************************************************/
    1679   char *tmp;
    1680   char *request;
    1681 
    1682   assert(bkpinfo!=NULL);
    1683   assert(cd_number_i_want > 0);
     1697insist_on_this_cd_number(struct s_bkpinfo *bkpinfo, int cd_number_i_want)
     1698{
     1699
     1700    /*@ int ************************************************************* */
     1701    int res = 0;
     1702
     1703
     1704    /*@ buffers ********************************************************* */
     1705    char *tmp;
     1706    char *request;
     1707
     1708    assert(bkpinfo != NULL);
     1709    assert(cd_number_i_want > 0);
    16841710
    16851711//  log_msg(3, "Insisting on CD number %d", cd_number_i_want);
    16861712
    1687   if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type))
    1688     {
    1689       log_msg(3, "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
    1690       return;
    1691     }
    1692   malloc_string(tmp);
    1693   malloc_string(request);
    1694   sprintf(tmp, "mkdir -p "MNT_CDROM);
    1695   run_program_and_log_output(tmp, 5);
    1696   if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == nfs)
    1697     {
    1698       log_msg(3, "Remounting CD");
    1699       g_ISO_restore_mode = TRUE;
     1713    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
     1714        log_msg(3,
     1715                "No need to insist_on_this_cd_number when the backup type isn't CD-R(W) or NFS or ISO");
     1716        return;
     1717    }
     1718    malloc_string(tmp);
     1719    malloc_string(request);
     1720    sprintf(tmp, "mkdir -p " MNT_CDROM);
     1721    run_program_and_log_output(tmp, 5);
     1722    if (g_ISO_restore_mode || bkpinfo->backup_media_type == iso
     1723        || bkpinfo->backup_media_type == nfs) {
     1724        log_msg(3, "Remounting CD");
     1725        g_ISO_restore_mode = TRUE;
    17001726// FIXME --- I'm tempted to do something about this...
    17011727// Why unmount and remount again and again?
    1702       if (is_this_device_mounted(MNT_CDROM)) { run_program_and_log_output("umount "MNT_CDROM, 5); }
    1703       system("mkdir -p /tmp/isodir &> /dev/null");
    1704       sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir, bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
    1705       if (!does_file_exist(tmp))
    1706         {
    1707       sprintf(tmp, "/tmp/isodir/%s/%s-%d.iso", bkpinfo->nfs_remote_dir, bkpinfo->prefix, cd_number_i_want);
    1708       if (does_file_exist(tmp))
    1709         {
    1710           log_msg(1, "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir", bkpinfo->isodir);
    1711           strcpy(bkpinfo->isodir, "/tmp/isodir");
    1712         }
    1713         }
    1714       log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
    1715       if (mount_CDROM_here(tmp, MNT_CDROM)) { fatal_error("Mommy!"); }
    1716 //    g_current_media_number = cd_number_i_want;
    1717 //    return;
    1718     }
    1719   if ((res=what_number_cd_is_this (bkpinfo)) != cd_number_i_want)
    1720     {
    1721       log_msg(3, "Currently, we hold %d but we want %d", res, cd_number_i_want);
    1722       sprintf (tmp, "Insisting on %s #%d", media_descriptor_string(bkpinfo->backup_media_type), cd_number_i_want);
    1723       sprintf (request, "Please insert %s #%d and press Enter.",
    1724                media_descriptor_string(bkpinfo->backup_media_type),
    1725            cd_number_i_want);
    1726       log_msg (3, tmp);
    1727       while (what_number_cd_is_this (bkpinfo) != cd_number_i_want)
    1728     {
    1729       paranoid_system ("sync");
    1730       if (is_this_device_mounted (MNT_CDROM))
    1731         {
    1732           res = run_program_and_log_output ("umount "MNT_CDROM, FALSE);
    1733         }
    1734       else
    1735         {
    1736           res = 0;
    1737         }
    1738       if (res)
    1739         {
    1740           log_to_screen ("WARNING - failed to unmount CD-ROM drive");
    1741         }
    1742           if (!bkpinfo->please_dont_eject)
    1743             { res = eject_device(bkpinfo->media_device); }
    1744           else
    1745             { res = 0; }
    1746       if (res)
    1747         {
    1748               log_to_screen ("WARNING - failed to eject CD-ROM disk");
    1749         }
    1750           popup_and_OK (request);
    1751           if (!bkpinfo->please_dont_eject)
    1752             {
    1753               inject_device(bkpinfo->media_device);
    1754             }
    1755       paranoid_system ("sync");
    1756     }
    1757       log_msg (1, "Thankyou. Proceeding...");
    1758       g_current_media_number = cd_number_i_want;
    1759     }
    1760   paranoid_free(tmp);
    1761   paranoid_free(request);
     1728        if (is_this_device_mounted(MNT_CDROM)) {
     1729            run_program_and_log_output("umount " MNT_CDROM, 5);
     1730        }
     1731        system("mkdir -p /tmp/isodir &> /dev/null");
     1732        sprintf(tmp, "%s/%s/%s-%d.iso", bkpinfo->isodir,
     1733                bkpinfo->nfs_remote_dir, bkpinfo->prefix,
     1734                cd_number_i_want);
     1735        if (!does_file_exist(tmp)) {
     1736            sprintf(tmp, "/tmp/isodir/%s/%s-%d.iso",
     1737                    bkpinfo->nfs_remote_dir, bkpinfo->prefix,
     1738                    cd_number_i_want);
     1739            if (does_file_exist(tmp)) {
     1740                log_msg(1,
     1741                        "FIXME - hacking bkpinfo->isodir from '%s' to /tmp/isodir",
     1742                        bkpinfo->isodir);
     1743                strcpy(bkpinfo->isodir, "/tmp/isodir");
     1744            }
     1745        }
     1746        log_msg(3, "Mounting %s at %s", tmp, MNT_CDROM);
     1747        if (mount_CDROM_here(tmp, MNT_CDROM)) {
     1748            fatal_error("Mommy!");
     1749        }
     1750//    g_current_media_number = cd_number_i_want;
     1751//    return;
     1752    }
     1753    if ((res = what_number_cd_is_this(bkpinfo)) != cd_number_i_want) {
     1754        log_msg(3, "Currently, we hold %d but we want %d", res,
     1755                cd_number_i_want);
     1756        sprintf(tmp, "Insisting on %s #%d",
     1757                media_descriptor_string(bkpinfo->backup_media_type),
     1758                cd_number_i_want);
     1759        sprintf(request, "Please insert %s #%d and press Enter.",
     1760                media_descriptor_string(bkpinfo->backup_media_type),
     1761                cd_number_i_want);
     1762        log_msg(3, tmp);
     1763        while (what_number_cd_is_this(bkpinfo) != cd_number_i_want) {
     1764            paranoid_system("sync");
     1765            if (is_this_device_mounted(MNT_CDROM)) {
     1766                res =
     1767                    run_program_and_log_output("umount " MNT_CDROM, FALSE);
     1768            } else {
     1769                res = 0;
     1770            }
     1771            if (res) {
     1772                log_to_screen("WARNING - failed to unmount CD-ROM drive");
     1773            }
     1774            if (!bkpinfo->please_dont_eject) {
     1775                res = eject_device(bkpinfo->media_device);
     1776            } else {
     1777                res = 0;
     1778            }
     1779            if (res) {
     1780                log_to_screen("WARNING - failed to eject CD-ROM disk");
     1781            }
     1782            popup_and_OK(request);
     1783            if (!bkpinfo->please_dont_eject) {
     1784                inject_device(bkpinfo->media_device);
     1785            }
     1786            paranoid_system("sync");
     1787        }
     1788        log_msg(1, "Thankyou. Proceeding...");
     1789        g_current_media_number = cd_number_i_want;
     1790    }
     1791    paranoid_free(tmp);
     1792    paranoid_free(request);
    17621793}
    17631794
     
    17781809 * @ingroup archiveGroup
    17791810 */
    1780 int interactively_obtain_media_parameters_from_user(struct s_bkpinfo *bkpinfo, bool archiving_to_media)
     1811int interactively_obtain_media_parameters_from_user(struct s_bkpinfo
     1812                                                    *bkpinfo,
     1813                                                    bool
     1814                                                    archiving_to_media)
    17811815// archiving_to_media is TRUE if I'm being called by mondoarchive
    17821816// archiving_to_media is FALSE if I'm being called by mondorestore
    17831817{
    1784   char *tmp;
    1785   char *sz_size;
    1786   char *command;
    1787   char *comment;
    1788   int i;
    1789   FILE*fin;
    1790 
    1791   malloc_string(tmp);
    1792   malloc_string(sz_size);
    1793   malloc_string(command);
    1794   malloc_string(comment);
    1795   assert(bkpinfo!=NULL);
    1796   sz_size[0] = '\0';
    1797   bkpinfo->nonbootable_backup = FALSE;
     1818    char *tmp;
     1819    char *sz_size;
     1820    char *command;
     1821    char *comment;
     1822    int i;
     1823    FILE *fin;
     1824
     1825    malloc_string(tmp);
     1826    malloc_string(sz_size);
     1827    malloc_string(command);
     1828    malloc_string(comment);
     1829    assert(bkpinfo != NULL);
     1830    sz_size[0] = '\0';
     1831    bkpinfo->nonbootable_backup = FALSE;
    17981832
    17991833// Tape, CD, NFS, ...?
    1800   srandom(getpid());
    1801   bkpinfo->backup_media_type = (g_restoring_live_from_cd) ? cdr : which_backup_media_type(bkpinfo->restore_data);
    1802   if (bkpinfo->backup_media_type == none)
    1803     { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1804   if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data)
    1805     { popup_and_OK("Please remove CD/floppy from drive(s)"); }
    1806   log_msg(3, "media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
    1807   if (archiving_to_media)
    1808     {
    1809       sensibly_set_tmpdir_and_scratchdir(bkpinfo);
    1810     }
    1811   bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
    1812   bkpinfo->compression_level = (bkpinfo->backup_media_type == cdstream) ? 1:5;
    1813   bkpinfo->use_lzo =  (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
     1834    srandom(getpid());
     1835    bkpinfo->backup_media_type =
     1836        (g_restoring_live_from_cd) ? cdr :
     1837        which_backup_media_type(bkpinfo->restore_data);
     1838    if (bkpinfo->backup_media_type == none) {
     1839        log_to_screen("User has chosen not to backup the PC");
     1840        finish(1);
     1841    }
     1842    if (bkpinfo->backup_media_type == tape && bkpinfo->restore_data) {
     1843        popup_and_OK("Please remove CD/floppy from drive(s)");
     1844    }
     1845    log_msg(3, "media type = %s",
     1846            bkptype_to_string(bkpinfo->backup_media_type));
     1847    if (archiving_to_media) {
     1848        sensibly_set_tmpdir_and_scratchdir(bkpinfo);
     1849    }
     1850    bkpinfo->cdrw_speed = (bkpinfo->backup_media_type == cdstream) ? 2 : 4;
     1851    bkpinfo->compression_level =
     1852        (bkpinfo->backup_media_type == cdstream) ? 1 : 5;
     1853    bkpinfo->use_lzo =
     1854        (bkpinfo->backup_media_type == cdstream) ? TRUE : FALSE;
    18141855
    18151856/*
     
    18261867*/
    18271868
    1828   mvaddstr_and_log_it(2, 0, " ");
     1869    mvaddstr_and_log_it(2, 0, " ");
    18291870
    18301871// Find device's /dev (or SCSI) entry
    1831   switch(bkpinfo->backup_media_type)
    1832     {
    1833       case cdr:
    1834       case cdrw:
    1835       case dvd:
    1836     if (archiving_to_media)
    1837           {
    1838             if (ask_me_yes_or_no("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
    1839               { bkpinfo->manual_cd_tray = TRUE; }
    1840             if ((bkpinfo->compression_level = which_compression_level()) == -1)
    1841               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1842         sprintf(comment, "What speed is your %s (re)writer?", media_descriptor_string(bkpinfo->backup_media_type));
    1843         if (bkpinfo->backup_media_type==dvd)
    1844           {
    1845         find_dvd_device(bkpinfo->media_device, FALSE);
    1846             strcpy(tmp, "1");
    1847         sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE); // 4.7 salesman's GB = 4.482 real GB = 4582 MB
    1848         log_msg(1, "Setting to DVD defaults");
    1849           }
    1850         else
    1851           {
    1852                 strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
    1853             strcpy(tmp, "4");
    1854         strcpy(sz_size, "650");
    1855         log_msg(1, "Setting to CD defaults");
    1856           }
    1857         if (bkpinfo->backup_media_type != dvd)
    1858           {
    1859             if (!popup_and_get_string("Speed", comment, tmp, 4))
    1860               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1861           }
    1862             bkpinfo->cdrw_speed = atoi(tmp); // if DVD then this shouldn't ever be used anyway :)
    1863         sprintf(comment, "How much data (in Megabytes) will each %s store?", media_descriptor_string(bkpinfo->backup_media_type));
    1864         if (!popup_and_get_string("Size", comment, sz_size, 5))
    1865           { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1866         for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = atoi(sz_size); }
    1867         if (bkpinfo->media_size[0] <=0)
    1868               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1869           }
    1870       case cdstream:
    1871     if (bkpinfo->disaster_recovery)
    1872       {
    1873             strcpy(bkpinfo->media_device, "/dev/cdrom");
    1874         log_msg(2, "CD-ROM device assumed to be at %s", bkpinfo->media_device);
    1875       }
    1876     else if (bkpinfo->restore_data
    1877         || bkpinfo->backup_media_type == dvd)
    1878       {
    1879             if (!bkpinfo->media_device[0])
    1880               { strcpy(bkpinfo->media_device, "/dev/cdrom"); } // just for the heck of it :)
    1881         log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
    1882         if (bkpinfo->backup_media_type == dvd || find_cdrom_device (bkpinfo->media_device, FALSE))
    1883           {
    1884         log_msg(1, "bkpinfo->media_device = %s", bkpinfo->media_device);
    1885             sprintf(comment, "Please specify your %s drive's /dev entry", media_descriptor_string(bkpinfo->backup_media_type));
    1886             if (!popup_and_get_string("Device?", comment, bkpinfo->media_device, MAX_STR_LEN/4))
    1887               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1888           }
    1889         log_msg(2, "%s device found at %s", media_descriptor_string(bkpinfo->backup_media_type), bkpinfo->media_device);
    1890       }
    1891     else
    1892       {
    1893         if (find_cdrw_device(bkpinfo->media_device)) { bkpinfo->media_device[0]='\0'; }
    1894         if (bkpinfo->media_device[0])
    1895           {
    1896             sprintf(tmp, "I think I've found your %s burner at SCSI node %s; am I right on the money?", media_descriptor_string(bkpinfo->backup_media_type), bkpinfo->media_device);
    1897             if (!ask_me_yes_or_no(tmp)) { bkpinfo->media_device[0]='\0'; }
    1898           }
    1899            if (!bkpinfo->media_device[0])
    1900           {
    1901         if (g_kernel_version < 2.6)
    1902           {
    1903             i=popup_and_get_string("Device node?", "What is the SCSI node of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN/4);
    1904           }
    1905         else
    1906           {
    1907             i=popup_and_get_string("/dev entry?", "What is the /dev entry of your CD (re)writer, please?", bkpinfo->media_device, MAX_STR_LEN/4);
    1908           }
    1909         if (!i)
    1910               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1911           }
    1912       }
    1913     if (bkpinfo->backup_media_type == cdstream)
    1914       {
    1915         for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650; }
    1916       }
    1917     break;
    1918       case udev:
    1919     if (!ask_me_yes_or_no("This option is for advanced users only. Are you sure?"))
    1920       { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1921       case tape:
    1922 
    1923     if (find_tape_device_and_size(bkpinfo->media_device, sz_size))
    1924       {
    1925         log_msg(3, "Ok, using vanilla scsi tape.");
    1926         strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
    1927         if ((fin=fopen(bkpinfo->media_device, "r")))
    1928           { paranoid_fclose(fin); }
    1929         else
    1930           { strcpy(bkpinfo->media_device, "/dev/osst0"); }
    1931       }
    1932     if (bkpinfo->media_device[0])
    1933       {
    1934         if ((fin=fopen(bkpinfo->media_device, "r")))
    1935           { paranoid_fclose(fin); }
    1936         else
    1937           {
    1938         if (does_file_exist("/tmp/mondo-restore.cfg"))
    1939           {
    1940             read_cfg_var("/tmp/mondo-restore.cfg", "media-dev", bkpinfo->media_device);
    1941           }
    1942           }
    1943         sprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
    1944       }
    1945     if (bkpinfo->media_device[0])
    1946       {
    1947         sprintf(tmp, "I think I've found your tape streamer at %s; am I right on the money?", bkpinfo->media_device);
    1948         if (!ask_me_yes_or_no(tmp)) { bkpinfo->media_device[0]='\0'; }
    1949       }
    1950         if (!bkpinfo->media_device[0])
    1951       {
    1952         if (!popup_and_get_string("Device name?", "What is the /dev entry of your tape streamer?", bkpinfo->media_device, MAX_STR_LEN/4))
    1953           { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1954       }
    1955     sprintf(tmp, "ls -l %s", bkpinfo->media_device);
    1956     if (run_program_and_log_output(tmp, FALSE))
    1957           { log_to_screen("User has not specified a valid /dev entry"); finish(1); }
    1958     log_msg(4, "sz_size = %s", sz_size);
    1959         sz_size[0] = '\0';
     1872    switch (bkpinfo->backup_media_type) {
     1873    case cdr:
     1874    case cdrw:
     1875    case dvd:
     1876        if (archiving_to_media) {
     1877            if (ask_me_yes_or_no
     1878                ("Is your computer a laptop, or does the CD writer incorporate BurnProof technology?"))
     1879            {
     1880                bkpinfo->manual_cd_tray = TRUE;
     1881            }
     1882            if ((bkpinfo->compression_level =
     1883                 which_compression_level()) == -1) {
     1884                log_to_screen("User has chosen not to backup the PC");
     1885                finish(1);
     1886            }
     1887            sprintf(comment, "What speed is your %s (re)writer?",
     1888                    media_descriptor_string(bkpinfo->backup_media_type));
     1889            if (bkpinfo->backup_media_type == dvd) {
     1890                find_dvd_device(bkpinfo->media_device, FALSE);
     1891                strcpy(tmp, "1");
     1892                sprintf(sz_size, "%d", DEFAULT_DVD_DISK_SIZE);  // 4.7 salesman's GB = 4.482 real GB = 4582 MB
     1893                log_msg(1, "Setting to DVD defaults");
     1894            } else {
     1895                strcpy(bkpinfo->media_device, VANILLA_SCSI_CDROM);
     1896                strcpy(tmp, "4");
     1897                strcpy(sz_size, "650");
     1898                log_msg(1, "Setting to CD defaults");
     1899            }
     1900            if (bkpinfo->backup_media_type != dvd) {
     1901                if (!popup_and_get_string("Speed", comment, tmp, 4)) {
     1902                    log_to_screen("User has chosen not to backup the PC");
     1903                    finish(1);
     1904                }
     1905            }
     1906            bkpinfo->cdrw_speed = atoi(tmp);    // if DVD then this shouldn't ever be used anyway :)
     1907            sprintf(comment,
     1908                    "How much data (in Megabytes) will each %s store?",
     1909                    media_descriptor_string(bkpinfo->backup_media_type));
     1910            if (!popup_and_get_string("Size", comment, sz_size, 5)) {
     1911                log_to_screen("User has chosen not to backup the PC");
     1912                finish(1);
     1913            }
     1914            for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
     1915                bkpinfo->media_size[i] = atoi(sz_size);
     1916            }
     1917            if (bkpinfo->media_size[0] <= 0) {
     1918                log_to_screen("User has chosen not to backup the PC");
     1919                finish(1);
     1920            }
     1921        }
     1922    case cdstream:
     1923        if (bkpinfo->disaster_recovery) {
     1924            strcpy(bkpinfo->media_device, "/dev/cdrom");
     1925            log_msg(2, "CD-ROM device assumed to be at %s",
     1926                    bkpinfo->media_device);
     1927        } else if (bkpinfo->restore_data
     1928                   || bkpinfo->backup_media_type == dvd) {
     1929            if (!bkpinfo->media_device[0]) {
     1930                strcpy(bkpinfo->media_device, "/dev/cdrom");
     1931            }                   // just for the heck of it :)
     1932            log_msg(1, "bkpinfo->media_device = %s",
     1933                    bkpinfo->media_device);
     1934            if (bkpinfo->backup_media_type == dvd
     1935                || find_cdrom_device(bkpinfo->media_device, FALSE)) {
     1936                log_msg(1, "bkpinfo->media_device = %s",
     1937                        bkpinfo->media_device);
     1938                sprintf(comment,
     1939                        "Please specify your %s drive's /dev entry",
     1940                        media_descriptor_string(bkpinfo->
     1941                                                backup_media_type));
     1942                if (!popup_and_get_string
     1943                    ("Device?", comment, bkpinfo->media_device,
     1944                     MAX_STR_LEN / 4)) {
     1945                    log_to_screen("User has chosen not to backup the PC");
     1946                    finish(1);
     1947                }
     1948            }
     1949            log_msg(2, "%s device found at %s",
     1950                    media_descriptor_string(bkpinfo->backup_media_type),
     1951                    bkpinfo->media_device);
     1952        } else {
     1953            if (find_cdrw_device(bkpinfo->media_device)) {
     1954                bkpinfo->media_device[0] = '\0';
     1955            }
     1956            if (bkpinfo->media_device[0]) {
     1957                sprintf(tmp,
     1958                        "I think I've found your %s burner at SCSI node %s; am I right on the money?",
     1959                        media_descriptor_string(bkpinfo->
     1960                                                backup_media_type),
     1961                        bkpinfo->media_device);
     1962                if (!ask_me_yes_or_no(tmp)) {
     1963                    bkpinfo->media_device[0] = '\0';
     1964                }
     1965            }
     1966            if (!bkpinfo->media_device[0]) {
     1967                if (g_kernel_version < 2.6) {
     1968                    i = popup_and_get_string("Device node?",
     1969                                             "What is the SCSI node of your CD (re)writer, please?",
     1970                                             bkpinfo->media_device,
     1971                                             MAX_STR_LEN / 4);
     1972                } else {
     1973                    i = popup_and_get_string("/dev entry?",
     1974                                             "What is the /dev entry of your CD (re)writer, please?",
     1975                                             bkpinfo->media_device,
     1976                                             MAX_STR_LEN / 4);
     1977                }
     1978                if (!i) {
     1979                    log_to_screen("User has chosen not to backup the PC");
     1980                    finish(1);
     1981                }
     1982            }
     1983        }
     1984        if (bkpinfo->backup_media_type == cdstream) {
     1985            for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
     1986                bkpinfo->media_size[i] = 650;
     1987            }
     1988        }
     1989        break;
     1990    case udev:
     1991        if (!ask_me_yes_or_no
     1992            ("This option is for advanced users only. Are you sure?")) {
     1993            log_to_screen("User has chosen not to backup the PC");
     1994            finish(1);
     1995        }
     1996    case tape:
     1997
     1998        if (find_tape_device_and_size(bkpinfo->media_device, sz_size)) {
     1999            log_msg(3, "Ok, using vanilla scsi tape.");
     2000            strcpy(bkpinfo->media_device, VANILLA_SCSI_TAPE);
     2001            if ((fin = fopen(bkpinfo->media_device, "r"))) {
     2002                paranoid_fclose(fin);
     2003            } else {
     2004                strcpy(bkpinfo->media_device, "/dev/osst0");
     2005            }
     2006        }
     2007        if (bkpinfo->media_device[0]) {
     2008            if ((fin = fopen(bkpinfo->media_device, "r"))) {
     2009                paranoid_fclose(fin);
     2010            } else {
     2011                if (does_file_exist("/tmp/mondo-restore.cfg")) {
     2012                    read_cfg_var("/tmp/mondo-restore.cfg", "media-dev",
     2013                                 bkpinfo->media_device);
     2014                }
     2015            }
     2016            sprintf(tmp,
     2017                    "I think I've found your tape streamer at %s; am I right on the money?",
     2018                    bkpinfo->media_device);
     2019        }
     2020        if (bkpinfo->media_device[0]) {
     2021            sprintf(tmp,
     2022                    "I think I've found your tape streamer at %s; am I right on the money?",
     2023                    bkpinfo->media_device);
     2024            if (!ask_me_yes_or_no(tmp)) {
     2025                bkpinfo->media_device[0] = '\0';
     2026            }
     2027        }
     2028        if (!bkpinfo->media_device[0]) {
     2029            if (!popup_and_get_string
     2030                ("Device name?",
     2031                 "What is the /dev entry of your tape streamer?",
     2032                 bkpinfo->media_device, MAX_STR_LEN / 4)) {
     2033                log_to_screen("User has chosen not to backup the PC");
     2034                finish(1);
     2035            }
     2036        }
     2037        sprintf(tmp, "ls -l %s", bkpinfo->media_device);
     2038        if (run_program_and_log_output(tmp, FALSE)) {
     2039            log_to_screen("User has not specified a valid /dev entry");
     2040            finish(1);
     2041        }
     2042        log_msg(4, "sz_size = %s", sz_size);
     2043        sz_size[0] = '\0';
    19602044/*
    19612045    if ((size_sz[0]=='\0' || atol(size_sz)==0) && archiving_to_media)
     
    19652049      }
    19662050*/
    1967     if (sz_size[0]=='\0')
    1968       { bkpinfo->media_size[0] = 0; }
    1969         else
    1970       { bkpinfo->media_size[0] = friendly_sizestr_to_sizelong (sz_size)/2 - 50; }
    1971     log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
    1972         if (bkpinfo->media_size[0] <= 0) { bkpinfo->media_size[0] = 0; }
    1973     for(i=1; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = bkpinfo->media_size[0]; }
    1974         if (archiving_to_media)
    1975           {
    1976             if ((bkpinfo->compression_level = which_compression_level()) == -1)
    1977               { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    1978           }
    1979         break;
    1980 
    1981 
    1982 
    1983       case nfs:
    1984     if (!bkpinfo->nfs_mount[0]) { strcpy(bkpinfo->nfs_mount, call_program_and_get_last_line_of_output("mount | grep \":\" | cut -d' ' -f1 | head -n1")); }
     2051        if (sz_size[0] == '\0') {
     2052            bkpinfo->media_size[0] = 0;
     2053        } else {
     2054            bkpinfo->media_size[0] =
     2055                friendly_sizestr_to_sizelong(sz_size) / 2 - 50;
     2056        }
     2057        log_msg(4, "media_size[0] = %ld", bkpinfo->media_size[0]);
     2058        if (bkpinfo->media_size[0] <= 0) {
     2059            bkpinfo->media_size[0] = 0;
     2060        }
     2061        for (i = 1; i <= MAX_NOOF_MEDIA; i++) {
     2062            bkpinfo->media_size[i] = bkpinfo->media_size[0];
     2063        }
     2064        if (archiving_to_media) {
     2065            if ((bkpinfo->compression_level =
     2066                 which_compression_level()) == -1) {
     2067                log_to_screen("User has chosen not to backup the PC");
     2068                finish(1);
     2069            }
     2070        }
     2071        break;
     2072
     2073
     2074
     2075    case nfs:
     2076        if (!bkpinfo->nfs_mount[0]) {
     2077            strcpy(bkpinfo->nfs_mount,
     2078                   call_program_and_get_last_line_of_output
     2079                   ("mount | grep \":\" | cut -d' ' -f1 | head -n1"));
     2080        }
    19852081#ifdef __FreeBSD__
    1986         if (TRUE)
     2082        if (TRUE)
    19872083#else
    1988     if (!bkpinfo->disaster_recovery )
    1989 #endif
    1990       {
    1991         if (!popup_and_get_string("NFS dir.", "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)", bkpinfo->nfs_mount, MAX_STR_LEN/4))
    1992           {
    1993         log_to_screen("User has chosen not to backup the PC");
    1994         finish(1);
    1995           }
    1996             if (!bkpinfo->restore_data)
    1997               {
    1998                 if ((bkpinfo->compression_level = which_compression_level()) == -1)
    1999                   {
    2000                     log_to_screen("User has chosen not to backup the PC");
    2001                     finish(1);
    2002                   }
    2003               }
    2004             sprintf(command, "mount | grep %s | cut -d' ' -f3", bkpinfo->nfs_mount);
    2005             strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(command));
    2006       }
    2007     if (bkpinfo->disaster_recovery)
    2008           {
    2009         system("umount /tmp/isodir 2> /dev/null");
    2010         if (!popup_and_get_string("NFS share", "Which remote NFS share should I mount?", bkpinfo->nfs_mount, MAX_STR_LEN))
    2011           {
    2012                 log_to_screen("User has chosen not to backup the PC");
    2013                 finish(1);
    2014           }
    2015           }
    2016         if (!is_this_device_mounted(bkpinfo->nfs_mount))
    2017           {
    2018             sprintf(bkpinfo->isodir, "/tmp/isodir");
    2019         run_program_and_log_output("mkdir -p /tmp/isodir", 5);
    2020         sprintf(tmp, "mount %s -t nfs -o nolock /tmp/isodir", bkpinfo->nfs_mount);
    2021         run_program_and_log_output(tmp, 5);
    2022           }
    2023     if (!is_this_device_mounted(bkpinfo->nfs_mount))
    2024       {
    2025         popup_and_OK("Please mount that partition before you try to backup to or restore from it.");
    2026         finish(1);
    2027       }
    2028         strcpy(tmp, bkpinfo->nfs_remote_dir);
    2029         if (!popup_and_get_string("Directory", "Which directory within that mountpoint?", tmp, MAX_STR_LEN))
    2030           {
    2031             log_to_screen("User has chosen not to backup the PC");
    2032              finish(1);
    2033           }
    2034         strcpy(bkpinfo->nfs_remote_dir, tmp);
    2035     for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650; }
    2036         log_msg(3, "Just set nfs_remote_dir to %s", bkpinfo->nfs_remote_dir);
    2037     log_msg(3, "isodir is still %s", bkpinfo->isodir);
    2038     break;
    2039 
    2040       case iso:
    2041         if (!bkpinfo->disaster_recovery)
    2042           {
    2043              if (!popup_and_get_string("Storage dir.", "Please enter the full path that contains your ISO images.  Example: /mnt/raid0_0", bkpinfo->isodir, MAX_STR_LEN/4))
    2044           { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2045              if (archiving_to_media) {
    2046                  if ((bkpinfo->compression_level = which_compression_level()) == -1)
    2047                      { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2048                  if (!popup_and_get_string ("ISO size.", "Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s you plan to backup to.", sz_size, 16))
    2049                      { log_to_screen ("User has chosen not to backup the PC"); finish(1); }
    2050                  for(i=0; i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = atoi (sz_size); }
    2051              if (!popup_and_get_string("Prefix.", "Please enter the prefix that will be prepended to your ISO filename.  Example: machine1 to obtain machine1-[1-9]*.iso files", bkpinfo->prefix, MAX_STR_LEN/4))
    2052           { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2053              } else {
    2054                  for (i=0;i<=MAX_NOOF_MEDIA; i++) { bkpinfo->media_size[i] = 650;}
    2055              }
    2056           }
    2057     break;
    2058       default:
    2059     fatal_error("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
    2060     }
    2061 
    2062   if (archiving_to_media)
    2063     {
     2084        if (!bkpinfo->disaster_recovery)
     2085#endif
     2086        {
     2087            if (!popup_and_get_string
     2088                ("NFS dir.",
     2089                 "Please enter path and directory where archives are stored remotely. (Mondo has taken a guess at the correct value. If it is incorrect, delete it and type the correct one.)",
     2090                 bkpinfo->nfs_mount, MAX_STR_LEN / 4)) {
     2091                log_to_screen("User has chosen not to backup the PC");
     2092                finish(1);
     2093            }
     2094            if (!bkpinfo->restore_data) {
     2095                if ((bkpinfo->compression_level =
     2096                     which_compression_level()) == -1) {
     2097                    log_to_screen("User has chosen not to backup the PC");
     2098                    finish(1);
     2099                }
     2100            }
     2101            sprintf(command, "mount | grep %s | cut -d' ' -f3",
     2102                    bkpinfo->nfs_mount);
     2103            strcpy(bkpinfo->isodir,
     2104                   call_program_and_get_last_line_of_output(command));
     2105        }
     2106        if (bkpinfo->disaster_recovery) {
     2107            system("umount /tmp/isodir 2> /dev/null");
     2108            if (!popup_and_get_string
     2109                ("NFS share", "Which remote NFS share should I mount?",
     2110                 bkpinfo->nfs_mount, MAX_STR_LEN)) {
     2111                log_to_screen("User has chosen not to backup the PC");
     2112                finish(1);
     2113            }
     2114        }
     2115        if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
     2116            sprintf(bkpinfo->isodir, "/tmp/isodir");
     2117            run_program_and_log_output("mkdir -p /tmp/isodir", 5);
     2118            sprintf(tmp, "mount %s -t nfs -o nolock /tmp/isodir",
     2119                    bkpinfo->nfs_mount);
     2120            run_program_and_log_output(tmp, 5);
     2121        }
     2122        if (!is_this_device_mounted(bkpinfo->nfs_mount)) {
     2123            popup_and_OK
     2124                ("Please mount that partition before you try to backup to or restore from it.");
     2125            finish(1);
     2126        }
     2127        strcpy(tmp, bkpinfo->nfs_remote_dir);
     2128        if (!popup_and_get_string
     2129            ("Directory", "Which directory within that mountpoint?", tmp,
     2130             MAX_STR_LEN)) {
     2131            log_to_screen("User has chosen not to backup the PC");
     2132            finish(1);
     2133        }
     2134        strcpy(bkpinfo->nfs_remote_dir, tmp);
     2135        for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
     2136            bkpinfo->media_size[i] = 650;
     2137        }
     2138        log_msg(3, "Just set nfs_remote_dir to %s",
     2139                bkpinfo->nfs_remote_dir);
     2140        log_msg(3, "isodir is still %s", bkpinfo->isodir);
     2141        break;
     2142
     2143    case iso:
     2144        if (!bkpinfo->disaster_recovery) {
     2145            if (!popup_and_get_string
     2146                ("Storage dir.",
     2147                 "Please enter the full path that contains your ISO images.  Example: /mnt/raid0_0",
     2148                 bkpinfo->isodir, MAX_STR_LEN / 4)) {
     2149                log_to_screen("User has chosen not to backup the PC");
     2150                finish(1);
     2151            }
     2152            if (archiving_to_media) {
     2153                if ((bkpinfo->compression_level =
     2154                     which_compression_level()) == -1) {
     2155                    log_to_screen("User has chosen not to backup the PC");
     2156                    finish(1);
     2157                }
     2158                if (!popup_and_get_string
     2159                    ("ISO size.",
     2160                     "Please enter how big you want each ISO image to be (in megabytes). This should be less than or equal to the size of the CD-R[W]'s you plan to backup to.",
     2161                     sz_size, 16)) {
     2162                    log_to_screen("User has chosen not to backup the PC");
     2163                    finish(1);
     2164                }
     2165                for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
     2166                    bkpinfo->media_size[i] = atoi(sz_size);
     2167                }
     2168                if (!popup_and_get_string
     2169                    ("Prefix.",
     2170                     "Please enter the prefix that will be prepended to your ISO filename.  Example: machine1 to obtain machine1-[1-9]*.iso files",
     2171                     bkpinfo->prefix, MAX_STR_LEN / 4)) {
     2172                    log_to_screen("User has chosen not to backup the PC");
     2173                    finish(1);
     2174                }
     2175            } else {
     2176                for (i = 0; i <= MAX_NOOF_MEDIA; i++) {
     2177                    bkpinfo->media_size[i] = 650;
     2178                }
     2179            }
     2180        }
     2181        break;
     2182    default:
     2183        fatal_error
     2184            ("I, Mojo Jojo, shall defeat those pesky Powerpuff Girls!");
     2185    }
     2186
     2187    if (archiving_to_media) {
    20642188
    20652189#ifdef __FreeBSD__
    2066     strcpy (bkpinfo->boot_device, call_program_and_get_last_line_of_output
    2067         ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
     2190        strcpy(bkpinfo->boot_device,
     2191               call_program_and_get_last_line_of_output
     2192               ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/\\([0-9]\\).*/\\1/'"));
    20682193#else
    2069     strcpy (bkpinfo->boot_device, call_program_and_get_last_line_of_output
    2070             ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
    2071 #endif
    2072       i = which_boot_loader(bkpinfo->boot_device);
    2073       if (i=='U') // unknown
    2074         {
     2194        strcpy(bkpinfo->boot_device,
     2195               call_program_and_get_last_line_of_output
     2196               ("mount | grep ' / ' | head -1 | cut -d' ' -f1 | sed 's/[0-9].*//'"));
     2197#endif
     2198        i = which_boot_loader(bkpinfo->boot_device);
     2199        if (i == 'U')           // unknown
     2200        {
    20752201
    20762202#ifdef __FreeBSD__
    2077           if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/ad0)", bkpinfo->boot_device, MAX_STR_LEN/4))
    2078             { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2079           i = which_boot_loader (bkpinfo->boot_device);
     2203            if (!popup_and_get_string
     2204                ("Boot device",
     2205                 "What is your boot device? (e.g. /dev/ad0)",
     2206                 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
     2207                log_to_screen("User has chosen not to backup the PC");
     2208                finish(1);
     2209            }
     2210            i = which_boot_loader(bkpinfo->boot_device);
    20802211#else
    2081           if (!popup_and_get_string("Boot device", "What is your boot device? (e.g. /dev/hda)", bkpinfo->boot_device, MAX_STR_LEN/4))
    2082             { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2083           if (does_string_exist_in_boot_block(bkpinfo->boot_device, "LILO")) { i='L'; }
    2084           else if (does_string_exist_in_boot_block(bkpinfo->boot_device, "ELILO")) { i='E'; }
    2085           else if (does_string_exist_in_boot_block(bkpinfo->boot_device, "GRUB")) { i='G'; }
    2086           else { i = 'U'; }
    2087 #endif
    2088           if (i == 'U')
    2089             {
    2090           if (ask_me_yes_or_no("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))
     2212            if (!popup_and_get_string
     2213                ("Boot device",
     2214                 "What is your boot device? (e.g. /dev/hda)",
     2215                 bkpinfo->boot_device, MAX_STR_LEN / 4)) {
     2216                log_to_screen("User has chosen not to backup the PC");
     2217                finish(1);
     2218            }
     2219            if (does_string_exist_in_boot_block
     2220                (bkpinfo->boot_device, "LILO")) {
     2221                i = 'L';
     2222            } else
     2223                if (does_string_exist_in_boot_block
     2224                    (bkpinfo->boot_device, "ELILO")) {
     2225                i = 'E';
     2226            } else
     2227                if (does_string_exist_in_boot_block
     2228                    (bkpinfo->boot_device, "GRUB")) {
     2229                i = 'G';
     2230            } else {
     2231                i = 'U';
     2232            }
     2233#endif
     2234            if (i == 'U') {
     2235                if (ask_me_yes_or_no
     2236                    ("Unidentified boot loader. Shall I restore it byte-for-byte at restore time and hope for the best?"))
     2237                {
     2238                    i = 'R';    // raw
     2239                } else {
     2240                    log_to_screen
     2241                        ("I cannot find your boot loader. Please run mondoarchive with parameters.");
     2242                    finish(1);
     2243                }
     2244            }
     2245        }
     2246        bkpinfo->boot_loader = i;
     2247        strcpy(bkpinfo->include_paths, "/");
     2248        if (!popup_and_get_string
     2249            ("Backup paths",
     2250             "Please enter paths which you want me to backup. The default is '/' (i.e. everything).",
     2251             bkpinfo->include_paths, MAX_STR_LEN)) {
     2252            log_to_screen("User has chosen not to backup the PC");
     2253            finish(1);
     2254        }
     2255        strcpy(tmp, list_of_NFS_mounts_only());
     2256        if (strlen(tmp) > 2) {
     2257            if (bkpinfo->exclude_paths[0]) {
     2258                strcat(bkpinfo->exclude_paths, " ");
     2259            }
     2260            strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN);
     2261        }
     2262// NTFS
     2263        strcpy(tmp,
     2264               call_program_and_get_last_line_of_output
     2265               ("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
     2266        if (strlen(tmp) > 2) {
     2267            if (!popup_and_get_string
     2268                ("NTFS partitions",
     2269                 "Please enter/confirm the NTFS partitions you wish to backup as well.",
     2270                 tmp, MAX_STR_LEN / 4)) {
     2271                log_to_screen("User has chosen not to backup the PC");
     2272                finish(1);
     2273            }
     2274            strncpy(bkpinfo->image_devs, tmp, MAX_STR_LEN / 4);
     2275        }
     2276
     2277
     2278        if (!popup_and_get_string
     2279            ("Exclude paths",
     2280             "Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.",
     2281             bkpinfo->exclude_paths, MAX_STR_LEN)) {
     2282            log_to_screen("User has chosen not to backup the PC");
     2283            finish(1);
     2284        }
     2285        bkpinfo->make_cd_use_lilo = FALSE;
     2286        bkpinfo->backup_data = TRUE;
     2287        bkpinfo->verify_data =
     2288            ask_me_yes_or_no
     2289            ("Will you want to verify your backups after Mondo has created them?");
     2290
     2291#ifndef __FreeBSD__
     2292        if (!ask_me_yes_or_no
     2293            ("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))
     2294#endif
    20912295        {
    2092           i = 'R'; // raw
    2093         }
    2094           else
    2095         {
    2096           log_to_screen("I cannot find your boot loader. Please run mondoarchive with parameters."); finish(1);
    2097         }
    2098         }
    2099     }
    2100       bkpinfo->boot_loader = i;
    2101       strcpy(bkpinfo->include_paths, "/");
    2102       if (!popup_and_get_string("Backup paths", "Please enter paths which you want me to backup. The default is '/' (i.e. everything).", bkpinfo->include_paths, MAX_STR_LEN))
    2103         { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2104       strcpy(tmp, list_of_NFS_mounts_only());
    2105       if (strlen(tmp)>2)
    2106         {
    2107           if (bkpinfo->exclude_paths[0]) { strcat(bkpinfo->exclude_paths, " "); }
    2108           strncpy(bkpinfo->exclude_paths, tmp, MAX_STR_LEN);
    2109     }
    2110 
    2111 // NTFS
    2112       strcpy(tmp, call_program_and_get_last_line_of_output("parted2fdisk -l | grep -i ntfs | awk '{ print $1};' | tr -s '\\n' ' ' | awk '{ print $0};'"));
    2113       if (strlen(tmp) > 2)
    2114         {
    2115       if (!popup_and_get_string("NTFS partitions", "Please enter/confirm the NTFS partitions you wish to backup as well.", tmp, MAX_STR_LEN/4))
    2116             { log_to_screen ("User has chosen not to backup the PC"); finish(1); }
    2117           strncpy (bkpinfo->image_devs, tmp, MAX_STR_LEN/4);
    2118     }
    2119 
    2120 
    2121       if (!popup_and_get_string("Exclude paths", "Please enter paths which you do NOT want to backup. Separate them with spaces. NB: /tmp and /proc are always excluded. :-) Just hit 'Enter' if you want to do a full system backup.", bkpinfo->exclude_paths, MAX_STR_LEN))
    2122         { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2123       bkpinfo->make_cd_use_lilo = FALSE;
    2124       bkpinfo->backup_data = TRUE;
    2125       bkpinfo->verify_data = ask_me_yes_or_no("Will you want to verify your backups after Mondo has created them?");
    2126 
    2127 #ifndef __FreeBSD__
    2128       if (!ask_me_yes_or_no("Are you confident that your kernel is a sane, sensible, standard Linux kernel? Say 'no' if you are using a Gentoo <1.4 or Debian <3.0, please."))
    2129 #endif
    2130         { strcpy(bkpinfo->kernel_path, "FAILSAFE"); }
    2131 
    2132       if (!ask_me_yes_or_no("Are you sure you want to proceed? Hit 'no' to abort."))
    2133         { log_to_screen("User has chosen not to backup the PC"); finish(1); }
    2134     }
    2135   else
    2136     {
    2137       bkpinfo->restore_data = TRUE; // probably...
    2138     }
    2139 
    2140   if (bkpinfo->backup_media_type == iso || bkpinfo->backup_media_type == nfs)
    2141     {
    2142       g_ISO_restore_mode = TRUE;
    2143     }
    2144 
     2296            strcpy(bkpinfo->kernel_path, "FAILSAFE");
     2297        }
     2298
     2299        if (!ask_me_yes_or_no
     2300            ("Are you sure you want to proceed? Hit 'no' to abort.")) {
     2301            log_to_screen("User has chosen not to backup the PC");
     2302            finish(1);
     2303        }
     2304    } else {
     2305        bkpinfo->restore_data = TRUE;   // probably...
     2306    }
     2307
     2308    if (bkpinfo->backup_media_type == iso
     2309        || bkpinfo->backup_media_type == nfs) {
     2310        g_ISO_restore_mode = TRUE;
     2311    }
    21452312#ifdef __FreeSD__
    21462313// skip
    21472314#else
    2148   if (bkpinfo->backup_media_type == nfs)
    2149     {
    2150       sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3", bkpinfo->nfs_mount);
     2315    if (bkpinfo->backup_media_type == nfs) {
     2316        sprintf(tmp, "mount | grep \"%s\" | cut -d' ' -f3",
     2317                bkpinfo->nfs_mount);
    21512318//      strcpy(bkpinfo->isodir, call_program_and_get_last_line_of_output(tmp));
    2152       log_msg(3, "I think the NFS mount is mounted at %s", bkpinfo->isodir);
    2153     }
    2154   log_it("isodir = %s", bkpinfo->isodir);
    2155   log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
    2156 #endif
    2157 
    2158   log_it("media device = %s", bkpinfo->media_device);
    2159   log_it("media size = %ld", bkpinfo->media_size[1]);
    2160   log_it("media type = %s", bkptype_to_string(bkpinfo->backup_media_type));
    2161   log_it("compression = %ld", bkpinfo->compression_level);
    2162   log_it("include_paths = '%s'", bkpinfo->include_paths);
    2163   log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
    2164   log_it("scratchdir = '%s'", bkpinfo->scratchdir);
    2165   log_it("tmpdir = '%s'", bkpinfo->tmpdir);
    2166   log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device, bkpinfo->boot_loader);
    2167   if (bkpinfo->media_size[0] < 0)
    2168     {
    2169       if (archiving_to_media)
    2170         {
    2171           fatal_error("Media size is less than zero.");
    2172         }
    2173       else
    2174         {
    2175       log_msg(2, "Warning - media size is less than zero.");
    2176       bkpinfo->media_size[0] = 0;
    2177     }
    2178     }
    2179   paranoid_free(tmp);
    2180   paranoid_free(sz_size);
    2181   paranoid_free(command);
    2182   paranoid_free(comment);
    2183   return(0);
     2319        log_msg(3, "I think the NFS mount is mounted at %s",
     2320                bkpinfo->isodir);
     2321    }
     2322    log_it("isodir = %s", bkpinfo->isodir);
     2323    log_it("nfs_mount = '%s'", bkpinfo->nfs_mount);
     2324#endif
     2325
     2326    log_it("media device = %s", bkpinfo->media_device);
     2327    log_it("media size = %ld", bkpinfo->media_size[1]);
     2328    log_it("media type = %s",
     2329           bkptype_to_string(bkpinfo->backup_media_type));
     2330    log_it("compression = %ld", bkpinfo->compression_level);
     2331    log_it("include_paths = '%s'", bkpinfo->include_paths);
     2332    log_it("exclude_paths = '%s'", bkpinfo->exclude_paths);
     2333    log_it("scratchdir = '%s'", bkpinfo->scratchdir);
     2334    log_it("tmpdir = '%s'", bkpinfo->tmpdir);
     2335    log_it("boot_device = '%s' (loader=%c)", bkpinfo->boot_device,
     2336           bkpinfo->boot_loader);
     2337    if (bkpinfo->media_size[0] < 0) {
     2338        if (archiving_to_media) {
     2339            fatal_error("Media size is less than zero.");
     2340        } else {
     2341            log_msg(2, "Warning - media size is less than zero.");
     2342            bkpinfo->media_size[0] = 0;
     2343        }
     2344    }
     2345    paranoid_free(tmp);
     2346    paranoid_free(sz_size);
     2347    paranoid_free(command);
     2348    paranoid_free(comment);
     2349    return (0);
    21842350}
    21852351
     
    21982364char *list_of_NFS_devices_and_mounts(void)
    21992365{
    2200   char *exclude_these_devices;
    2201   char *exclude_these_directories;
    2202   static char result_sz[512];
    2203 
    2204   malloc_string(exclude_these_devices);
    2205   malloc_string(exclude_these_directories);
    2206   strcpy(exclude_these_directories, call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2207   strcpy(exclude_these_devices, call_program_and_get_last_line_of_output("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2208   sprintf(result_sz, "%s %s", exclude_these_directories, exclude_these_devices);
    2209   paranoid_free(exclude_these_devices);
    2210   paranoid_free(exclude_these_directories);
    2211   return(result_sz);
     2366    char *exclude_these_devices;
     2367    char *exclude_these_directories;
     2368    static char result_sz[512];
     2369
     2370    malloc_string(exclude_these_devices);
     2371    malloc_string(exclude_these_directories);
     2372    strcpy(exclude_these_directories,
     2373           call_program_and_get_last_line_of_output
     2374           ("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
     2375    strcpy(exclude_these_devices,
     2376           call_program_and_get_last_line_of_output
     2377           ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
     2378    sprintf(result_sz, "%s %s", exclude_these_directories,
     2379            exclude_these_devices);
     2380    paranoid_free(exclude_these_devices);
     2381    paranoid_free(exclude_these_directories);
     2382    return (result_sz);
    22122383}
    22132384
     
    22232394char *list_of_NFS_mounts_only(void)
    22242395{
    2225   char *exclude_these_devices;
    2226   char *exclude_these_directories;
    2227   static char result_sz[512];
    2228 
    2229   malloc_string(exclude_these_devices);
    2230   malloc_string(exclude_these_directories);
    2231   strcpy(exclude_these_directories, call_program_and_get_last_line_of_output("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2232   strcpy(exclude_these_devices, call_program_and_get_last_line_of_output("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
    2233   sprintf(result_sz, "%s", exclude_these_directories);
    2234   paranoid_free(exclude_these_devices);
    2235   paranoid_free(exclude_these_directories);
    2236   return(result_sz);
     2396    char *exclude_these_devices;
     2397    char *exclude_these_directories;
     2398    static char result_sz[512];
     2399
     2400    malloc_string(exclude_these_devices);
     2401    malloc_string(exclude_these_directories);
     2402    strcpy(exclude_these_directories,
     2403           call_program_and_get_last_line_of_output
     2404           ("mount -t coda,ncpfs,nfs,smbfs | tr -s '\t' ' ' | cut -d' ' -f3 | tr -s '\n' ' ' | awk '{print $0;}'"));
     2405    strcpy(exclude_these_devices,
     2406           call_program_and_get_last_line_of_output
     2407           ("cat /etc/fstab | tr -s '\t' ' ' | grep -E '( (coda|ncpfs|nfs|smbfs) )' | cut -d' ' -f1 | tr -s '\n' ' ' | awk '{print $0;}'"));
     2408    sprintf(result_sz, "%s", exclude_these_directories);
     2409    paranoid_free(exclude_these_devices);
     2410    paranoid_free(exclude_these_directories);
     2411    return (result_sz);
    22372412}
    22382413
     
    22502425 * @ingroup deviceGroup
    22512426 */
    2252 void make_fifo(char*store_name_here, char*stub)
    2253 {
    2254   char *tmp;
    2255 
    2256   malloc_string(tmp);
    2257   assert_string_is_neither_NULL_nor_zerolength(stub);
    2258 
    2259   sprintf(store_name_here, "%s%d%d", stub, (int)(random()%32768), (int)(random()%32768));
    2260   make_hole_for_file(store_name_here);
    2261   mkfifo(store_name_here, S_IRWXU|S_IRWXG);
    2262   sprintf(tmp, "chmod 770 %s", store_name_here);
    2263   paranoid_system(tmp);
    2264   paranoid_free(tmp);
     2427void make_fifo(char *store_name_here, char *stub)
     2428{
     2429    char *tmp;
     2430
     2431    malloc_string(tmp);
     2432    assert_string_is_neither_NULL_nor_zerolength(stub);
     2433
     2434    sprintf(store_name_here, "%s%d%d", stub, (int) (random() % 32768),
     2435            (int) (random() % 32768));
     2436    make_hole_for_file(store_name_here);
     2437    mkfifo(store_name_here, S_IRWXU | S_IRWXG);
     2438    sprintf(tmp, "chmod 770 %s", store_name_here);
     2439    paranoid_system(tmp);
     2440    paranoid_free(tmp);
    22652441}
    22662442
     
    22782454void sensibly_set_tmpdir_and_scratchdir(struct s_bkpinfo *bkpinfo)
    22792455{
    2280   char *tmp, *command, *sz;
    2281 
    2282   malloc_string(tmp);
    2283   malloc_string(command);
    2284   malloc_string(sz);
    2285   assert(bkpinfo!=NULL);
     2456    char *tmp, *command, *sz;
     2457
     2458    malloc_string(tmp);
     2459    malloc_string(command);
     2460    malloc_string(sz);
     2461    assert(bkpinfo != NULL);
    22862462
    22872463#ifdef __FreeBSD__
    2288       strcpy(tmp, call_program_and_get_last_line_of_output("df -m -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
     2464    strcpy(tmp,
     2465           call_program_and_get_last_line_of_output
     2466           ("df -m -t nonfs,msdosfs,ntfs,smbfs,smb,cifs | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
    22892467#else
    2290       strcpy(tmp, call_program_and_get_last_line_of_output("df -m -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/                  /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
    2291 #endif
    2292 
    2293       if (tmp[0]!='/') { strcpy(sz, tmp); strcpy(tmp, "/"); strcat(tmp, sz); }
    2294       if (!tmp[0]) {
    2295       fatal_error ("I couldn't figure out the tempdir!");
    2296       }
    2297       sprintf(bkpinfo->tmpdir, "%s/tmp.mondo.%d", tmp, (int)(random()%32768));
    2298       log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
    2299 
    2300       sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp, (int)(random()%32768));
    2301       log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
    2302 
    2303       sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
    2304      
    2305       sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
    2306       paranoid_system(command);
    2307   paranoid_free(tmp);
    2308   paranoid_free(command);
    2309   paranoid_free(sz);
     2468    strcpy(tmp,
     2469           call_program_and_get_last_line_of_output
     2470           ("df -m -x nfs -x vfat -x ntfs -x smbfs -x smb -x cifs | sed 's/                  /devdev/' | tr -s '\t' ' ' | grep -v none | grep -v Filesystem | grep -v /dev/shm | awk '{printf \"%s %s\\n\", $4, $6;}' | sort -n | tail -n1 | awk '{print $NF;}'"));
     2471#endif
     2472
     2473    if (tmp[0] != '/') {
     2474        strcpy(sz, tmp);
     2475        strcpy(tmp, "/");
     2476        strcat(tmp, sz);
     2477    }
     2478    if (!tmp[0]) {
     2479        fatal_error("I couldn't figure out the tempdir!");
     2480    }
     2481    sprintf(bkpinfo->tmpdir, "%s/tmp.mondo.%d", tmp,
     2482            (int) (random() % 32768));
     2483    log_it("bkpinfo->tmpdir is being set to %s", bkpinfo->tmpdir);
     2484
     2485    sprintf(bkpinfo->scratchdir, "%s/mondo.scratch.%d", tmp,
     2486            (int) (random() % 32768));
     2487    log_it("bkpinfo->scratchdir is being set to %s", bkpinfo->scratchdir);
     2488
     2489    sprintf(g_erase_tmpdir_and_scratchdir, "rm -Rf %s %s", bkpinfo->tmpdir,
     2490            bkpinfo->scratchdir);
     2491
     2492    sprintf(command, "rm -Rf %s/tmp.mondo.* %s/mondo.scratch.*", tmp, tmp);
     2493    paranoid_system(command);
     2494    paranoid_free(tmp);
     2495    paranoid_free(command);
     2496    paranoid_free(sz);
    23102497}
    23112498
     
    23262513 * @return TRUE if @p dev exists, FALSE if it doesn't.
    23272514 */
    2328 bool set_dev_to_this_if_rx_OK(char *output, char*dev)
    2329 {
    2330   char *command;
    2331 
    2332   malloc_string(command);
    2333   if (!dev || dev[0]=='\0') { output[0]='\0'; return(FALSE); }
     2515bool set_dev_to_this_if_rx_OK(char *output, char *dev)
     2516{
     2517    char *command;
     2518
     2519    malloc_string(command);
     2520    if (!dev || dev[0] == '\0') {
     2521        output[0] = '\0';
     2522        return (FALSE);
     2523    }
    23342524//  assert_string_is_neither_NULL_nor_zerolength(dev);
    2335   log_msg(10, "Injecting %s", dev);
    2336   inject_device(dev);
    2337   if (!does_file_exist(dev)) { log_msg(10, "%s doesn't exist. Returning FALSE.", dev); return(FALSE); }
    2338   sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null", 512L, dev);
    2339   if (!run_program_and_log_output(command, FALSE) && !run_program_and_log_output(command, FALSE))
    2340     { strcpy(output, dev); log_msg(4, "Found it - %s", dev); return(TRUE); }
    2341   else
    2342     { output[0]='\0'; log_msg(4, "It's not %s",dev); return(FALSE); }
     2525    log_msg(10, "Injecting %s", dev);
     2526    inject_device(dev);
     2527    if (!does_file_exist(dev)) {
     2528        log_msg(10, "%s doesn't exist. Returning FALSE.", dev);
     2529        return (FALSE);
     2530    }
     2531    sprintf(command, "dd bs=%ld count=1 if=%s of=/dev/null &> /dev/null",
     2532            512L, dev);
     2533    if (!run_program_and_log_output(command, FALSE)
     2534        && !run_program_and_log_output(command, FALSE)) {
     2535        strcpy(output, dev);
     2536        log_msg(4, "Found it - %s", dev);
     2537        return (TRUE);
     2538    } else {
     2539        output[0] = '\0';
     2540        log_msg(4, "It's not %s", dev);
     2541        return (FALSE);
     2542    }
    23432543}
    23442544
     
    23562556int what_number_cd_is_this(struct s_bkpinfo *bkpinfo)
    23572557{
    2358   int cd_number=-1;
    2359   char *mountdev;
    2360   char *tmp;
    2361 
    2362   malloc_string(mountdev);
    2363   malloc_string(tmp);
    2364   assert(bkpinfo!=NULL);
     2558    int cd_number = -1;
     2559    char *mountdev;
     2560    char *tmp;
     2561
     2562    malloc_string(mountdev);
     2563    malloc_string(tmp);
     2564    assert(bkpinfo != NULL);
    23652565//  log_it("Asking what_number_cd_is_this");
    2366   if (g_ISO_restore_mode)
    2367     {
    2368       sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
     2566    if (g_ISO_restore_mode) {
     2567        sprintf(tmp, "mount | grep iso9660 | awk '{print $3;}'");
    23692568//      log_it("tmp = %s", tmp);
    23702569
    2371       strcpy(mountdev, call_program_and_get_last_line_of_output(tmp));
    2372       strcat(mountdev, "/archives/THIS-CD-NUMBER");
     2570        strcpy(mountdev, call_program_and_get_last_line_of_output(tmp));
     2571        strcat(mountdev, "/archives/THIS-CD-NUMBER");
    23732572//      log_it("mountdev = %s", mountdev);
    2374       cd_number = atoi(last_line_of_file(mountdev));
     2573        cd_number = atoi(last_line_of_file(mountdev));
    23752574//      log_it("cd_number = %d", cd_number);
    2376 paranoid_free(mountdev);
    2377 paranoid_free(tmp);
    2378       return(cd_number);
    2379     }
    2380 
    2381   strcpy(mountdev, bkpinfo->media_device);
    2382   if (!mountdev[0])
    2383     {
    2384       log_it("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
    2385       find_cdrom_device(bkpinfo->media_device, FALSE);
    2386     }
    2387   if (! is_this_device_mounted(MNT_CDROM))
    2388     { mount_CDROM_here(mountdev, MNT_CDROM); }
    2389   cd_number = atoi(last_line_of_file(MNT_CDROM"/archives/THIS-CD-NUMBER"));
     2575        paranoid_free(mountdev);
     2576        paranoid_free(tmp);
     2577        return (cd_number);
     2578    }
     2579
     2580    strcpy(mountdev, bkpinfo->media_device);
     2581    if (!mountdev[0]) {
     2582        log_it
     2583            ("(what_number_cd_is_this) Warning - media_device unknown. Finding out...");
     2584        find_cdrom_device(bkpinfo->media_device, FALSE);
     2585    }
     2586    if (!is_this_device_mounted(MNT_CDROM)) {
     2587        mount_CDROM_here(mountdev, MNT_CDROM);
     2588    }
     2589    cd_number =
     2590        atoi(last_line_of_file(MNT_CDROM "/archives/THIS-CD-NUMBER"));
    23902591//  log_it("cd_number..later.. = %d", cd_number);
    2391 paranoid_free(mountdev);
    2392 paranoid_free(tmp);
    2393   return(cd_number);
     2592    paranoid_free(mountdev);
     2593    paranoid_free(tmp);
     2594    return (cd_number);
    23942595}
    23952596
     
    24072608 * The mountpoint (where it's mounted) will obviously be '/'.
    24082609 */
    2409 char *
    2410 where_is_root_mounted ()
    2411 {
    2412     /*@ buffers *****************/
    2413   static char tmp[MAX_STR_LEN];
     2610char *where_is_root_mounted()
     2611{
     2612    /*@ buffers **************** */
     2613    static char tmp[MAX_STR_LEN];
    24142614
    24152615
    24162616#ifdef __FreeBSD__
    2417   strcpy (tmp, call_program_and_get_last_line_of_output
    2418           ("mount | grep \" on / \" | cut -d' ' -f1"));
     2617    strcpy(tmp, call_program_and_get_last_line_of_output
     2618           ("mount | grep \" on / \" | cut -d' ' -f1"));
    24192619#else
    2420   strcpy (tmp, call_program_and_get_last_line_of_output
    2421       ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
    2422   if (strstr(tmp, "/dev/cciss/")) {
    2423       strcpy (tmp, call_program_and_get_last_line_of_output
    2424               ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
    2425   }
    2426   if (strstr(tmp, "/dev/md"))
    2427     {
    2428       strcpy(tmp,
    2429          call_program_and_get_last_line_of_output
    2430          ("mount | grep \" on / \" | cut -d' ' -f1"));
    2431     }
    2432 #endif
    2433 
    2434   return (tmp);
     2620    strcpy(tmp, call_program_and_get_last_line_of_output
     2621           ("mount | grep \" on / \" | cut -d' ' -f1 | sed s/[0-9]// | sed s/[0-9]//"));
     2622    if (strstr(tmp, "/dev/cciss/")) {
     2623        strcpy(tmp, call_program_and_get_last_line_of_output
     2624               ("mount | grep \" on / \" | cut -d' ' -f1 | cut -dp -f1"));
     2625    }
     2626    if (strstr(tmp, "/dev/md")) {
     2627        strcpy(tmp,
     2628               call_program_and_get_last_line_of_output
     2629               ("mount | grep \" on / \" | cut -d' ' -f1"));
     2630    }
     2631#endif
     2632
     2633    return (tmp);
    24352634}
    24362635
     
    24432642 */
    24442643#ifdef __FreeBSD__
    2445 char which_boot_loader (char *which_device)
    2446 {
    2447   int count_lilos = 0;
    2448   int count_grubs = 0;
    2449   int count_boot0s = 0;
    2450   int count_dangerouslydedicated = 0;
    2451 
    2452   log_it ("looking at drive %s's MBR", which_device);
    2453   if (does_string_exist_in_boot_block (which_device, "GRUB"))
    2454       { count_grubs++; }
    2455   if (does_string_exist_in_boot_block (which_device, "LILO"))
    2456       { count_lilos++; }
    2457   if (does_string_exist_in_boot_block (which_device, "Drive"))
    2458       { count_boot0s++; }
    2459   if (does_string_exist_in_first_N_blocks (which_device, "FreeBSD/i386", 17))
    2460       { count_dangerouslydedicated++; }
    2461   log_it ("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n", count_grubs, count_lilos, count_elilos, count_boot0s, count_dangerouslydedicated);
    2462 
    2463   if (count_grubs && !count_lilos)
    2464     { return ('G'); }
    2465   else if (count_lilos && !count_grubs)
    2466     { return ('L'); }
    2467  else if (count_grubs == 1 && count_lilos == 1)
    2468     {
    2469       log_it ("I'll bet you used to use LILO but switched to GRUB...");
    2470       return ('G');
    2471     }
    2472   else if (count_boot0s == 1)
    2473       { return ('B'); }
    2474   else if (count_dangerouslydedicated)
    2475       { return ('D'); }
    2476   else
    2477     {
    2478       log_it ("Unknown boot loader");
    2479       return ('U');
    2480     }
     2644char which_boot_loader(char *which_device)
     2645{
     2646    int count_lilos = 0;
     2647    int count_grubs = 0;
     2648    int count_boot0s = 0;
     2649    int count_dangerouslydedicated = 0;
     2650
     2651    log_it("looking at drive %s's MBR", which_device);
     2652    if (does_string_exist_in_boot_block(which_device, "GRUB")) {
     2653        count_grubs++;
     2654    }
     2655    if (does_string_exist_in_boot_block(which_device, "LILO")) {
     2656        count_lilos++;
     2657    }
     2658    if (does_string_exist_in_boot_block(which_device, "Drive")) {
     2659        count_boot0s++;
     2660    }
     2661    if (does_string_exist_in_first_N_blocks
     2662        (which_device, "FreeBSD/i386", 17)) {
     2663        count_dangerouslydedicated++;
     2664    }
     2665    log_it("%d grubs and %d lilos and %d elilos and %d boot0s and %d DD\n",
     2666           count_grubs, count_lilos, count_elilos, count_boot0s,
     2667           count_dangerouslydedicated);
     2668
     2669    if (count_grubs && !count_lilos) {
     2670        return ('G');
     2671    } else if (count_lilos && !count_grubs) {
     2672        return ('L');
     2673    } else if (count_grubs == 1 && count_lilos == 1) {
     2674        log_it("I'll bet you used to use LILO but switched to GRUB...");
     2675        return ('G');
     2676    } else if (count_boot0s == 1) {
     2677        return ('B');
     2678    } else if (count_dangerouslydedicated) {
     2679        return ('D');
     2680    } else {
     2681        log_it("Unknown boot loader");
     2682        return ('U');
     2683    }
    24812684}
    24822685
    24832686#else
    24842687
    2485 char which_boot_loader (char *which_device)
    2486 {
    2487     /*@ buffer ******************************************************/
    2488   char *list_drives_cmd;
    2489   char *current_drive;
    2490 
    2491     /*@ pointers ****************************************************/
    2492   FILE *pdrives;
    2493 
    2494     /*@ int *********************************************************/
    2495   int count_lilos = 0;
    2496   int count_grubs = 0;
    2497 
    2498     /*@ end vars ****************************************************/
    2499 
    2500   malloc_string(list_drives_cmd);
    2501   malloc_string(current_drive);
     2688char which_boot_loader(char *which_device)
     2689{
     2690    /*@ buffer ***************************************************** */
     2691    char *list_drives_cmd;
     2692    char *current_drive;
     2693
     2694    /*@ pointers *************************************************** */
     2695    FILE *pdrives;
     2696
     2697    /*@ int ******************************************************** */
     2698    int count_lilos = 0;
     2699    int count_grubs = 0;
     2700
     2701    /*@ end vars *************************************************** */
     2702
     2703    malloc_string(list_drives_cmd);
     2704    malloc_string(current_drive);
    25022705
    25032706#ifdef __IA64__
    2504   /* No choice for it */
    2505   return ('E');
    2506 #endif
    2507   assert(which_device!=NULL);
    2508   //  sprintf (list_drives_cmd,
    2509   //       "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2");
    2510 
    2511   sprintf (list_drives_cmd,
    2512   // "parted2fdisk
    2513        "fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s", where_is_root_mounted());
    2514   log_it("list_drives_cmd = %s", list_drives_cmd);
    2515 
    2516   if (!(pdrives = popen (list_drives_cmd, "r")))
    2517     {
    2518       log_OS_error ("Unable to open list of drives");
    2519       paranoid_free(list_drives_cmd);
    2520       paranoid_free(current_drive);
    2521       return ('\0');
    2522     }
    2523   for (fgets (current_drive, MAX_STR_LEN, pdrives); !feof (pdrives);
    2524        fgets (current_drive, MAX_STR_LEN, pdrives))
    2525     {
    2526       strip_spaces (current_drive);
    2527       log_it ("looking at drive %s's MBR", current_drive);
    2528       if (does_string_exist_in_boot_block (current_drive, "GRUB"))
    2529     {
    2530       count_grubs++;
    2531       strcpy (which_device, current_drive);
    2532       break;
    2533     }
    2534       if (does_string_exist_in_boot_block (current_drive, "LILO"))
    2535     {
    2536       count_lilos++;
    2537       strcpy (which_device, current_drive);
    2538       break;
    2539     }
    2540     }
    2541   if (pclose (pdrives)) { log_OS_error("Cannot pclose pdrives"); }
    2542   log_it ("%d grubs and %d lilos\n", count_grubs, count_lilos);
    2543   paranoid_free(list_drives_cmd);
    2544   paranoid_free(current_drive);
    2545   if (count_grubs && !count_lilos)
    2546     {
    2547       return ('G');
    2548     }
    2549   else if (count_lilos && !count_grubs)
    2550     {
    2551       return ('L');
    2552     }
    2553   else if (count_grubs == 1 && count_lilos == 1)
    2554     {
    2555       log_it ("I'll bet you used to use LILO but switched to GRUB...");
    2556       return ('G');
    2557     }
    2558   else
    2559     {
    2560       log_it ("Unknown boot loader");
    2561       return ('U');
    2562     }
     2707    /* No choice for it */
     2708    return ('E');
     2709#endif
     2710    assert(which_device != NULL);
     2711    //  sprintf (list_drives_cmd,
     2712    //       "fdisk -l | grep /dev | grep cyl | tr ':' ' ' | cut -d' ' -f2");
     2713
     2714    sprintf(list_drives_cmd,
     2715            // "parted2fdisk
     2716            "fdisk -l 2>/dev/null | grep \"/dev/.*:\" | tr -s ':' ' ' | tr -s ' ' '\n' | grep /dev/; echo %s",
     2717            where_is_root_mounted());
     2718    log_it("list_drives_cmd = %s", list_drives_cmd);
     2719
     2720    if (!(pdrives = popen(list_drives_cmd, "r"))) {
     2721        log_OS_error("Unable to open list of drives");
     2722        paranoid_free(list_drives_cmd);
     2723        paranoid_free(current_drive);
     2724        return ('\0');
     2725    }
     2726    for (fgets(current_drive, MAX_STR_LEN, pdrives); !feof(pdrives);
     2727         fgets(current_drive, MAX_STR_LEN, pdrives)) {
     2728        strip_spaces(current_drive);
     2729        log_it("looking at drive %s's MBR", current_drive);
     2730        if (does_string_exist_in_boot_block(current_drive, "GRUB")) {
     2731            count_grubs++;
     2732            strcpy(which_device, current_drive);
     2733            break;
     2734        }
     2735        if (does_string_exist_in_boot_block(current_drive, "LILO")) {
     2736            count_lilos++;
     2737            strcpy(which_device, current_drive);
     2738            break;
     2739        }
     2740    }
     2741    if (pclose(pdrives)) {
     2742        log_OS_error("Cannot pclose pdrives");
     2743    }
     2744    log_it("%d grubs and %d lilos\n", count_grubs, count_lilos);
     2745    paranoid_free(list_drives_cmd);
     2746    paranoid_free(current_drive);
     2747    if (count_grubs && !count_lilos) {
     2748        return ('G');
     2749    } else if (count_lilos && !count_grubs) {
     2750        return ('L');
     2751    } else if (count_grubs == 1 && count_lilos == 1) {
     2752        log_it("I'll bet you used to use LILO but switched to GRUB...");
     2753        return ('G');
     2754    } else {
     2755        log_it("Unknown boot loader");
     2756        return ('U');
     2757    }
    25632758}
    25642759#endif
     
    25722767 * @return 0 for success, 1 for failure.
    25732768 */
    2574 int
    2575 zero_out_a_device (char *device)
    2576 {
    2577   FILE *fout;
    2578   int i;
    2579 
    2580   assert_string_is_neither_NULL_nor_zerolength(device);
    2581 
    2582   log_it ("Zeroing drive %s", device);
    2583   if (!(fout=fopen (device, "w")))
    2584     {
    2585       log_OS_error ("Unable to open/write to device");
    2586       return (1);
    2587     }
    2588   for (i = 0; i < 16384; i++)
    2589     {
    2590       fputc ('\0', fout);
    2591     }
    2592   paranoid_fclose(fout);
    2593   log_it ("Device successfully zeroed.");
    2594   return (0);
     2769int zero_out_a_device(char *device)
     2770{
     2771    FILE *fout;
     2772    int i;
     2773
     2774    assert_string_is_neither_NULL_nor_zerolength(device);
     2775
     2776    log_it("Zeroing drive %s", device);
     2777    if (!(fout = fopen(device, "w"))) {
     2778        log_OS_error("Unable to open/write to device");
     2779        return (1);
     2780    }
     2781    for (i = 0; i < 16384; i++) {
     2782        fputc('\0', fout);
     2783    }
     2784    paranoid_fclose(fout);
     2785    log_it("Device successfully zeroed.");
     2786    return (0);
    25952787}
    25962788
     
    26022794 * @bug Won't work with file v4.0; needs to be written in C.
    26032795 */
    2604 char *resolve_softlinks_to_get_to_actual_device_file(char*incoming)
    2605 {
    2606   static char output[MAX_STR_LEN];
    2607   char *command;
    2608   char *curr_fname;
    2609   char *scratch;
    2610   char *tmp;
    2611   char *p;
    2612    
    2613   struct stat statbuf; 
    2614   command = malloc(1000);
    2615   malloc_string(tmp);
    2616   malloc_string(scratch);
    2617   malloc_string(curr_fname);
    2618   if (!does_file_exist(incoming))
    2619     {
    2620       log_it("resolve_softlinks_to_get_to_actual_device_file --- device not found");
    2621       strcpy(output, incoming);
    2622     }
    2623   else
    2624     {
    2625       strcpy(curr_fname, incoming);
    2626       lstat(curr_fname, &statbuf);
    2627       while(S_ISLNK(statbuf.st_mode))
    2628         {
    2629       log_msg(1, "curr_fname = %s", curr_fname);
    2630       sprintf(command, "file %s", curr_fname);
    2631       strcpy(tmp, call_program_and_get_last_line_of_output(command));
    2632       for(p=tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' '; p--);
    2633       p++;
    2634       strcpy(scratch, p);
    2635       for(p=scratch; *p != '\0' && *p != '\''; p++);
    2636       *p = '\0';
    2637       log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp, scratch);
    2638       if (scratch[0] == '/')
    2639         {
    2640           strcpy(curr_fname, scratch); // copy whole thing because it's an absolute softlink
    2641         }
    2642       else
    2643         { // copy over the basename cos it's a relative softlink
    2644           p = curr_fname + strlen(curr_fname);
    2645           while(p!=curr_fname && *p != '/') { p--;}
    2646           if (*p == '/') { p++; }
    2647           strcpy(p, scratch);
    2648         }
    2649           lstat(curr_fname, &statbuf);
    2650     }
    2651       strcpy(output, curr_fname);
    2652       log_it("resolved %s to %s", incoming, output);
    2653     }
    2654   paranoid_free(command);
    2655   paranoid_free(curr_fname);
    2656   paranoid_free(tmp);
    2657   return(output);
     2796char *resolve_softlinks_to_get_to_actual_device_file(char *incoming)
     2797{
     2798    static char output[MAX_STR_LEN];
     2799    char *command;
     2800    char *curr_fname;
     2801    char *scratch;
     2802    char *tmp;
     2803    char *p;
     2804
     2805    struct stat statbuf;
     2806    command = malloc(1000);
     2807    malloc_string(tmp);
     2808    malloc_string(scratch);
     2809    malloc_string(curr_fname);
     2810    if (!does_file_exist(incoming)) {
     2811        log_it
     2812            ("resolve_softlinks_to_get_to_actual_device_file --- device not found");
     2813        strcpy(output, incoming);
     2814    } else {
     2815        strcpy(curr_fname, incoming);
     2816        lstat(curr_fname, &statbuf);
     2817        while (S_ISLNK(statbuf.st_mode)) {
     2818            log_msg(1, "curr_fname = %s", curr_fname);
     2819            sprintf(command, "file %s", curr_fname);
     2820            strcpy(tmp, call_program_and_get_last_line_of_output(command));
     2821            for (p = tmp + strlen(tmp); p != tmp && *p != '`' && *p != ' ';
     2822                 p--);
     2823            p++;
     2824            strcpy(scratch, p);
     2825            for (p = scratch; *p != '\0' && *p != '\''; p++);
     2826            *p = '\0';
     2827            log_msg(0, "curr_fname %s --> '%s' --> %s", curr_fname, tmp,
     2828                    scratch);
     2829            if (scratch[0] == '/') {
     2830                strcpy(curr_fname, scratch);    // copy whole thing because it's an absolute softlink
     2831            } else {            // copy over the basename cos it's a relative softlink
     2832                p = curr_fname + strlen(curr_fname);
     2833                while (p != curr_fname && *p != '/') {
     2834                    p--;
     2835                }
     2836                if (*p == '/') {
     2837                    p++;
     2838                }
     2839                strcpy(p, scratch);
     2840            }
     2841            lstat(curr_fname, &statbuf);
     2842        }
     2843        strcpy(output, curr_fname);
     2844        log_it("resolved %s to %s", incoming, output);
     2845    }
     2846    paranoid_free(command);
     2847    paranoid_free(curr_fname);
     2848    paranoid_free(tmp);
     2849    return (output);
    26582850}
    26592851
     
    26662858char *which_partition_format(const char *drive)
    26672859{
    2668   static char output[4];
    2669   char *tmp;
    2670   char *command;
    2671    
    2672   malloc_string(tmp);
    2673   malloc_string(command);
    2674   log_msg(0, "Looking for partition table format type");
    2675   sprintf(command, "fdisk -l %s | grep 'EFI GPT'", drive);
    2676   strcpy(tmp, call_program_and_get_last_line_of_output(command));
    2677   if (strstr(tmp,"GPT") == NULL) {
    2678       strcpy(output,"MBR");
    2679   } else {
    2680       strcpy(output,"GPT");
    2681   }
    2682   log_msg(0, "Found %s partition table format type",output);
    2683   paranoid_free(command);
    2684   paranoid_free(tmp);
    2685   return(output);
     2860    static char output[4];
     2861    char *tmp;
     2862    char *command;
     2863
     2864    malloc_string(tmp);
     2865    malloc_string(command);
     2866    log_msg(0, "Looking for partition table format type");
     2867    sprintf(command, "fdisk -l %s | grep 'EFI GPT'", drive);
     2868    strcpy(tmp, call_program_and_get_last_line_of_output(command));
     2869    if (strstr(tmp, "GPT") == NULL) {
     2870        strcpy(output, "MBR");
     2871    } else {
     2872        strcpy(output, "GPT");
     2873    }
     2874    log_msg(0, "Found %s partition table format type", output);
     2875    paranoid_free(command);
     2876    paranoid_free(tmp);
     2877    return (output);
    26862878}
    26872879
    26882880/* @} - end of deviceGroup */
    2689 
Note: See TracChangeset for help on using the changeset viewer.