blob: 27489cfd4ba8e46732c99d8e03df0bee461a6be9 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenbc341901999-11-06 04:56:00 +00002#include "internal.h"
3#include <stdlib.h>
4#include <unistd.h>
5#include <errno.h>
6#include <string.h>
7#include <stdio.h>
8#include <mntent.h>
Erik Andersenfac10d72000-02-07 05:29:42 +00009#include <fstab.h>
Eric Andersenbc341901999-11-06 04:56:00 +000010#include <sys/mount.h>
11
Erik Andersene49d5ec2000-02-08 19:58:47 +000012extern const char mtab_file[]; /* Defined in utility.c */
Eric Andersenbc341901999-11-06 04:56:00 +000013
Eric Andersenbc341901999-11-06 04:56:00 +000014
Erik Andersene49d5ec2000-02-08 19:58:47 +000015void erase_mtab(const char *name)
Eric Andersenbc341901999-11-06 04:56:00 +000016{
Erik Andersene49d5ec2000-02-08 19:58:47 +000017 struct mntent entries[20];
18 int count = 0;
Eric Andersenbc341901999-11-06 04:56:00 +000019 FILE *mountTable = setmntent(mtab_file, "r");
Erik Andersene49d5ec2000-02-08 19:58:47 +000020 struct mntent *m;
Eric Andersenbc341901999-11-06 04:56:00 +000021
Eric Andersen0ecb54a1999-12-05 23:24:55 +000022 /* Check if reading the mtab file failed */
Erik Andersene49d5ec2000-02-08 19:58:47 +000023 if (mountTable == 0
Eric Andersen0ecb54a1999-12-05 23:24:55 +000024#if ! defined BB_FEATURE_USE_PROCFS
Erik Andersene49d5ec2000-02-08 19:58:47 +000025 ) {
26#else
27 /* Bummer. fall back on trying the /proc filesystem */
28 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Eric Andersen0ecb54a1999-12-05 23:24:55 +000029#endif
Eric Andersenbc341901999-11-06 04:56:00 +000030 perror(mtab_file);
31 return;
32 }
33
Erik Andersene49d5ec2000-02-08 19:58:47 +000034 while ((m = getmntent(mountTable)) != 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000035 entries[count].mnt_fsname = strdup(m->mnt_fsname);
36 entries[count].mnt_dir = strdup(m->mnt_dir);
37 entries[count].mnt_type = strdup(m->mnt_type);
38 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000039 entries[count].mnt_freq = m->mnt_freq;
40 entries[count].mnt_passno = m->mnt_passno;
41 count++;
42 }
43 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 if ((mountTable = setmntent(mtab_file, "w"))) {
45 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000046
Erik Andersene49d5ec2000-02-08 19:58:47 +000047 for (i = 0; i < count; i++) {
48 int result = (strcmp(entries[i].mnt_fsname, name) == 0
49 || strcmp(entries[i].mnt_dir, name) == 0);
50
51 if (result)
Eric Andersenbc341901999-11-06 04:56:00 +000052 continue;
53 else
54 addmntent(mountTable, &entries[i]);
55 }
56 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 } else if (errno != EROFS)
Eric Andersenbc341901999-11-06 04:56:00 +000058 perror(mtab_file);
59}
60
Erik Andersene49d5ec2000-02-08 19:58:47 +000061void write_mtab(char *blockDevice, char *directory,
62 char *filesystemType, long flags, char *string_flags)
Eric Andersenbc341901999-11-06 04:56:00 +000063{
64 FILE *mountTable = setmntent(mtab_file, "a+");
65 struct mntent m;
66
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 if (mountTable == 0) {
Eric Andersenbc341901999-11-06 04:56:00 +000068 perror(mtab_file);
69 return;
70 }
71 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 int length = strlen(directory);
Eric Andersenbc341901999-11-06 04:56:00 +000073
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 if (length > 1 && directory[length - 1] == '/')
75 directory[length - 1] = '\0';
Eric Andersenbc341901999-11-06 04:56:00 +000076
Eric Andersen0ecb54a1999-12-05 23:24:55 +000077#ifdef BB_FEATURE_USE_PROCFS
Erik Andersene49d5ec2000-02-08 19:58:47 +000078 if (filesystemType == 0) {
79 struct mntent *p = findMountPoint(blockDevice, "/proc/mounts");
Eric Andersenbc341901999-11-06 04:56:00 +000080
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 if (p && p->mnt_type)
82 filesystemType = p->mnt_type;
83 }
Eric Andersen0ecb54a1999-12-05 23:24:55 +000084#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 m.mnt_fsname = blockDevice;
86 m.mnt_dir = directory;
87 m.mnt_type = filesystemType ? filesystemType : "default";
Eric Andersenbc341901999-11-06 04:56:00 +000088
Erik Andersene49d5ec2000-02-08 19:58:47 +000089 if (*string_flags) {
90 m.mnt_opts = string_flags;
91 } else {
92 if ((flags | MS_RDONLY) == flags)
93 m.mnt_opts = "ro";
94 else
95 m.mnt_opts = "rw";
96 }
97
98 m.mnt_freq = 0;
99 m.mnt_passno = 0;
100 addmntent(mountTable, &m);
101 endmntent(mountTable);
102 }
103}