blob: 33b11c83e93a2575e976702ca5ceeed2c5537fa4 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3570a342000-09-25 21:45:58 +00002#include "busybox.h"
Eric Andersenbc341901999-11-06 04:56:00 +00003#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 Andersenaf4ac772001-02-01 22:43:49 +000024 /* Bummer. fall back on trying the /proc filesystem */
25 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000026 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000027 return;
28 }
29
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 while ((m = getmntent(mountTable)) != 0) {
Erik Andersenfac10d72000-02-07 05:29:42 +000031 entries[count].mnt_fsname = strdup(m->mnt_fsname);
32 entries[count].mnt_dir = strdup(m->mnt_dir);
33 entries[count].mnt_type = strdup(m->mnt_type);
34 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000035 entries[count].mnt_freq = m->mnt_freq;
36 entries[count].mnt_passno = m->mnt_passno;
37 count++;
38 }
39 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 if ((mountTable = setmntent(mtab_file, "w"))) {
41 int i;
Eric Andersenbc341901999-11-06 04:56:00 +000042
Erik Andersene49d5ec2000-02-08 19:58:47 +000043 for (i = 0; i < count; i++) {
44 int result = (strcmp(entries[i].mnt_fsname, name) == 0
45 || strcmp(entries[i].mnt_dir, name) == 0);
46
47 if (result)
Eric Andersenbc341901999-11-06 04:56:00 +000048 continue;
49 else
50 addmntent(mountTable, &entries[i]);
51 }
52 endmntent(mountTable);
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 } else if (errno != EROFS)
Matt Kraaia9819b22000-12-22 01:48:07 +000054 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000055}
56
Erik Andersene49d5ec2000-02-08 19:58:47 +000057void write_mtab(char *blockDevice, char *directory,
58 char *filesystemType, long flags, char *string_flags)
Eric Andersenbc341901999-11-06 04:56:00 +000059{
60 FILE *mountTable = setmntent(mtab_file, "a+");
61 struct mntent m;
62
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 if (mountTable == 0) {
Matt Kraaia9819b22000-12-22 01:48:07 +000064 perror_msg("%s", mtab_file);
Eric Andersenbc341901999-11-06 04:56:00 +000065 return;
66 }
67 if (mountTable) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000068 int length = strlen(directory);
Eric Andersenbc341901999-11-06 04:56:00 +000069
Erik Andersene49d5ec2000-02-08 19:58:47 +000070 if (length > 1 && directory[length - 1] == '/')
71 directory[length - 1] = '\0';
Eric Andersenbc341901999-11-06 04:56:00 +000072
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 if (filesystemType == 0) {
Mark Whitleyf57c9442000-12-07 19:56:48 +000074 struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
Eric Andersenbc341901999-11-06 04:56:00 +000075
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 if (p && p->mnt_type)
77 filesystemType = p->mnt_type;
78 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 m.mnt_fsname = blockDevice;
80 m.mnt_dir = directory;
81 m.mnt_type = filesystemType ? filesystemType : "default";
Eric Andersenbc341901999-11-06 04:56:00 +000082
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 if (*string_flags) {
84 m.mnt_opts = string_flags;
85 } else {
86 if ((flags | MS_RDONLY) == flags)
87 m.mnt_opts = "ro";
88 else
89 m.mnt_opts = "rw";
90 }
91
92 m.mnt_freq = 0;
93 m.mnt_passno = 0;
94 addmntent(mountTable, &m);
95 endmntent(mountTable);
96 }
97}