home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / mtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-10  |  5.3 KB  |  222 lines

  1. #include <alloca.h>
  2. #include <ctype.h>
  3. #include <newt.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/mount.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10.  
  11. #include "devices.h"
  12. #include "fs.h"
  13. #include "install.h"
  14. #include "intl.h"
  15. #include "log.h"
  16. #include "mkswap.h"
  17. #include "run.h"
  18. #include "windows.h"
  19.  
  20. static int partFilter(struct partition * part) {
  21.     int rc;
  22.  
  23.     if (doMount(part->device, "/mnt", "ext2", 0, 0)) {
  24.     return 0;
  25.     }
  26.  
  27.     if (access("/mnt/etc", R_OK) || access("/mnt/etc/fstab", R_OK) ||
  28.     access("/mnt/proc", R_OK) || access("/mnt/bin", R_OK) ||
  29.     access("/mnt/etc/redhat-release", R_OK)) {
  30.     rc = 0;
  31.     } else 
  32.     rc = 1;
  33.  
  34.     umount("/mnt");
  35.  
  36.     return rc;
  37. }
  38.  
  39. int readMountTable(struct partitionTable table, struct fstab * finalFstab) {
  40.     newtComponent okay, cancel, form, text, listbox, answer;
  41.     int rootPartNum;
  42.     struct partition * rootPart = NULL;
  43.     FILE * f;
  44.     char buf[200];
  45.     char * start, * end;
  46.     char device[50], mntpoint[50], type[50];
  47.     struct fstab fstab;
  48.     struct fstabEntry entry;
  49.     int numParts;
  50.     newtGrid grid, subgrid, buttons;
  51.     char * reflowedText;
  52.     int width, height;
  53.  
  54.     umountFilesystems(finalFstab);
  55.  
  56.     addPartitionListbox(table, 7, PART_EXT2, partFilter, &numParts,
  57.             &subgrid, &listbox);
  58.  
  59.     if (!numParts) {
  60.     newtGridFree(subgrid, 1);
  61.  
  62.     /* try again, but don't be quite so picky */
  63.     addPartitionListbox(table, 7, PART_EXT2, NULL, &numParts,
  64.                 &subgrid, &listbox);
  65.     if (!numParts) {
  66.         errorWindow(_("You don't have any Linux partitions. You "
  67.                 "can't upgrade this system!"));
  68.         return INST_CANCEL;        /* INST_ERROR would hang everything */
  69.     }
  70.     }
  71.  
  72.     if (numParts == 1) {
  73.     rootPart = newtListboxGetCurrent(listbox);
  74.     rootPartNum = (rootPart - table.parts);
  75.     logMessage("Using partition %s for the root device",
  76.             table.parts[rootPartNum].device);
  77.     if (doMount(table.parts[rootPartNum].device, "/mnt", "ext2", 0, 0)) {
  78.         errorWindow(_("Could not mount automatically selected device."));
  79.         answer = NULL;
  80.         return INST_ERROR;
  81.     }
  82.     answer = okay;
  83.     } else {
  84.     reflowedText = newtReflowText(
  85.             _("What partition holds the root partition "
  86.               "of your installation?"), 53, 5, 5, &width, &height);
  87.     text = newtTextbox(-1, -1, width, height, NEWT_TEXTBOX_WRAP);
  88.     newtTextboxSetText(text, reflowedText);
  89.     free(reflowedText);
  90.  
  91.     buttons = newtButtonBar(_("Ok"), &okay, _("Cancel"), &cancel, NULL);
  92.  
  93.     form = newtForm(NULL, NULL, 0);
  94.     newtGridAddComponentsToForm(subgrid, form, 1);
  95.     newtFormAddComponents(form, text, okay, cancel, NULL);
  96.  
  97.     grid = newtCreateGrid(1, 3);
  98.  
  99.     newtGridSetField(grid, 0, 0, NEWT_GRID_COMPONENT, text,
  100.                 0, 0, 0, 0, 0, 0);
  101.     newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, subgrid,
  102.                 0, 1, 0, 1, 0, 0);
  103.     newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, buttons,
  104.                 0, 0, 0, 0, 0, NEWT_GRID_FLAG_GROWX);
  105.  
  106.     newtGridWrappedWindow(grid, _("Root Partition"));
  107.  
  108.     do {
  109.         answer = newtRunForm(form);
  110.         if (answer == cancel) continue;
  111.  
  112.         rootPart = newtListboxGetCurrent(listbox);
  113.         rootPartNum = (rootPart - table.parts);
  114.  
  115.         if (doMount(table.parts[rootPartNum].device, "/mnt", "ext2", 
  116.             0, 0)) {
  117.         errorWindow("Could not mount device.");
  118.         answer = NULL;
  119.         continue;
  120.         }
  121.  
  122.         if (testing) {
  123.         continue;
  124.         }
  125.  
  126.         if (access("/mnt/etc", R_OK) || access("/mnt/etc/fstab", R_OK) ||
  127.         access("/mnt/proc", R_OK) || access("/mnt/bin", R_OK)) {
  128.         errorWindow("That doesn't appear to be a root partition.");
  129.         umount("/mnt");
  130.         answer = NULL;
  131.         }
  132.     } while (!answer);
  133.  
  134.     newtPopWindow();
  135.     newtFormDestroy(form);
  136.     newtGridFree(grid, 1);
  137.     }
  138.  
  139.     if (answer == cancel) return INST_CANCEL;
  140.  
  141.     if (testing) 
  142.     f = fopen("/etc/fstab", "r");
  143.     else
  144.     f = fopen("/mnt/etc/fstab", "r");
  145.  
  146.     if (!f) {
  147.     errorWindow(_("Cannot read /mnt/etc/fstab: %s"));
  148.     return INST_ERROR;
  149.     }
  150.  
  151.     memset(&fstab, 0, sizeof(fstab));
  152.  
  153.     while (fgets(buf, sizeof(buf) - 1, f)) {
  154.     start = buf;
  155.     while (*start && isspace(*start)) start++;
  156.     if (!*start || *start == '#') continue;
  157.  
  158.     if (strncmp(start, "/dev/", 5)) {
  159.         continue;
  160.     }
  161.  
  162.     if (strstr(start, "noauto")) continue;
  163.  
  164.     end = start + strlen(start) - 1;
  165.     while (isspace(*end)) end--;
  166.     end++;
  167.     *end = '\0';
  168.  
  169.     if (sscanf(start, "%s %s %s", device, mntpoint, type) != 3) {
  170.         errorWindow(_("Bad line in /mnt/etc/fstab -- aborting"));
  171.         fclose(f);
  172.         freeFstab(fstab);
  173.         umount("/mnt");
  174.         return INST_ERROR;
  175.     }
  176.  
  177.     if (strncmp(device, "/dev/hd", 7) && strncmp(device, "/dev/sd", 7))
  178.         continue;
  179.  
  180.     entry.device = strdup(device + 5);
  181.     entry.size = 0;
  182.     entry.isMounted = 0;
  183.     entry.doFormat = 0;
  184.     entry.mntpoint = strdup(mntpoint);
  185.  
  186.     if (!strcmp(type, "ext2")) {
  187.         entry.type = PART_EXT2;
  188.         entry.tagName = "Linux native";
  189.     } else if (!strcmp(type, "swap")) {
  190.         entry.type = PART_SWAP;
  191.         entry.tagName = "Linux swap";
  192.         if (canEnableSwap) {
  193.         enableswap(entry.device, 0, 0);
  194.         canEnableSwap = 0;
  195.         }
  196.     } else if (!strcmp(type, "msdos")) {
  197.         entry.type = PART_DOS;
  198.         entry.tagName = "DOS 16-bit >=32";
  199.     } else if (!strcmp(type, "vfat")) {
  200.         entry.type = PART_DOS;
  201.         entry.tagName = "DOS 16-bit >=32";
  202.     } else if (!strcmp(type, "hpfs")) {
  203.         entry.type = PART_HPFS;
  204.         entry.tagName = "OS/2 HPFS";
  205.     } else {
  206.         entry.type = PART_OTHER;
  207.         entry.tagName = "Unknown";
  208.     }
  209.  
  210.     addFstabEntry(&fstab, entry);
  211.     }
  212.  
  213.     fclose(f);
  214.  
  215.     umount("/mnt");
  216.  
  217.     freeFstab(*finalFstab);
  218.     *finalFstab = fstab;
  219.  
  220.     return 0;
  221. }
  222.