home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / MISC / SRC / INSTALL / MTAB.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-23  |  3.8 KB  |  163 lines

  1. #include <alloca.h>
  2. #include <ctype.h>
  3. #include <linux/fs.h>
  4. #include <newt.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <sys/mount.h>
  9. #include <sys/stat.h>
  10. #include <unistd.h>
  11.  
  12. #include "devices.h"
  13. #include "fs.h"
  14. #include "install.h"
  15. #include "log.h"
  16. #include "mkswap.h"
  17. #include "perror.h"
  18. #include "run.h"
  19. #include "windows.h"
  20.  
  21. int readMountTable(struct partitionTable table, struct fstab * finalFstab) {
  22.     newtComponent okay, cancel, form, text, listbox, answer;
  23.     int i, rootPartNum;
  24.     struct partition * rootPart = NULL;
  25.     FILE * f;
  26.     char buf[200];
  27.     char * start, * end;
  28.     char device[50], mntpoint[50], type[50];
  29.     struct fstab fstab;
  30.     struct fstabEntry entry;
  31.  
  32.     umountFilesystems(finalFstab);
  33.  
  34.     for (i = 0; i < table.count; i++) 
  35.     if (table.parts[i].type == PART_EXT2) break;
  36.     if (i == table.count) {
  37.     errorWindow("You don't have any Linux partitions. You "
  38.             "can't upgrade this system!");
  39.     return INST_CANCEL;        /* INST_ERROR would hang everything */
  40.     }
  41.  
  42.     newtOpenWindow(10, 2, 55, 19, "Root Partition");
  43.     text = newtTextbox(1, 1, 53, 2, NEWT_TEXTBOX_WRAP);
  44.     newtTextboxSetText(text, "What partition holds the root partition "
  45.                "of your installation?");
  46.  
  47.     okay = newtButton(11, 15, "Ok");
  48.     cancel = newtButton(33, 15, "Cancel");
  49.  
  50.     form = newtForm(NULL, NULL, 0);
  51.     listbox = addPartitionListbox(table, form, 3, 4, 7, PART_EXT2);
  52.  
  53.     newtFormAddComponents(form, text, okay, cancel, NULL);
  54.  
  55.     do {
  56.     answer = newtRunForm(form);
  57.     if (answer == cancel) continue;
  58.  
  59.     rootPart = newtListboxGetCurrent(listbox);
  60.     rootPartNum = (rootPart - table.parts);
  61.  
  62.     if (doMount(table.parts[rootPartNum].device, "/mnt", "ext2", 0, 0)) {
  63.         errorWindow("Could not mount device.");
  64.         answer = NULL;
  65.         continue;
  66.     }
  67.  
  68.     if (testing) {
  69.         continue;
  70.     }
  71.  
  72.     if (access("/mnt/etc", R_OK) || access("/mnt/etc/fstab", R_OK) ||
  73.         access("/mnt/proc", R_OK) || access("/mnt/bin", R_OK)) {
  74.         errorWindow("That doesn't appear to be a root partition.");
  75.         umount("/mnt");
  76.         answer = NULL;
  77.     }
  78.     } while (!answer);
  79.             
  80.     newtFormDestroy(form);
  81.     newtPopWindow();
  82.  
  83.     if (answer == cancel) return INST_CANCEL;
  84.  
  85.     if (testing) 
  86.     f = fopen("/etc/fstab", "r");
  87.     else
  88.     f = fopen("/mnt/etc/fstab", "r");
  89.  
  90.     if (!f) {
  91.     errorWindow("Cannot read /mnt/etc/fstab: %s");
  92.     return INST_ERROR;
  93.     }
  94.  
  95.     memset(&fstab, 0, sizeof(fstab));
  96.  
  97.     while (fgets(buf, sizeof(buf) - 1, f)) {
  98.     start = buf;
  99.     while (*start && isspace(*start)) start++;
  100.     if (!*start || *start == '#') continue;
  101.  
  102.     if (strncmp(start, "/dev/", 5)) {
  103.         continue;
  104.     }
  105.  
  106.     if (strstr(start, "noauto")) continue;
  107.  
  108.     end = start + strlen(start) - 1;
  109.     while (isspace(*end)) end--;
  110.     end++;
  111.     *end = '\0';
  112.  
  113.     if (sscanf(start, "%s %s %s", device, mntpoint, type) != 3) {
  114.         errorWindow("Bad line in /mnt/etc/fstab -- aborting");
  115.         fclose(f);
  116.         freeFstab(fstab);
  117.         umount("/mnt");
  118.         return INST_ERROR;
  119.     }
  120.  
  121.     if (strncmp(device, "/dev/hd", 7) && strncmp(device, "/dev/sd", 7))
  122.         continue;
  123.  
  124.     entry.device = strdup(device + 5);
  125.     entry.size = 0;
  126.     entry.isMounted = 0;
  127.     entry.doFormat = 0;
  128.     entry.mntpoint = strdup(mntpoint);
  129.  
  130.     if (!strcmp(type, "ext2")) {
  131.         entry.type = PART_EXT2;
  132.         entry.tagName = "Linux native";
  133.     } else if (!strcmp(type, "swap")) {
  134.         entry.type = PART_SWAP;
  135.         entry.tagName = "Linux swap";
  136.         if (canEnableSwap) {
  137.         enableswap(entry.device, 0, 0);
  138.         canEnableSwap = 0;
  139.         }
  140.     } else if (!strcmp(type, "msdos")) {
  141.         entry.type = PART_DOS;
  142.         entry.tagName = "DOS 16-bit >=32";
  143.     } else if (!strcmp(type, "hpfs")) {
  144.         entry.type = PART_HPFS;
  145.         entry.tagName = "OS/2 HPFS";
  146.     } else {
  147.         entry.type = PART_OTHER;
  148.         entry.tagName = "Unknown";
  149.     }
  150.  
  151.     addFstabEntry(&fstab, entry);
  152.     }
  153.  
  154.     fclose(f);
  155.  
  156.     umount("/mnt");
  157.  
  158.     freeFstab(*finalFstab);
  159.     *finalFstab = fstab;
  160.  
  161.     return 0;
  162. }
  163.