blob: 41d88184bde2b4ada84855a4e5d264c7830e0c32 [file] [log] [blame]
Eric Andersenbc341901999-11-06 04:56:00 +00001#include "internal.h"
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <string.h>
6#include <stdio.h>
7#include <mntent.h>
Erik Andersenfac10d72000-02-07 05:29:42 +00008#include <fstab.h>
Eric Andersenbc341901999-11-06 04:56:00 +00009#include <sys/mount.h>
10
11extern const char mtab_file[]; /* Defined in utility.c */
12
Eric Andersenbc341901999-11-06 04:56:00 +000013
Erik Andersenfac10d72000-02-07 05:29:42 +000014void erase_mtab(const char * name)
Eric Andersenbc341901999-11-06 04:56:00 +000015{
16 struct mntent entries[20];
17 int count = 0;
18 FILE *mountTable = setmntent(mtab_file, "r");
19 struct mntent * m;
20
Eric Andersen0ecb54a1999-12-05 23:24:55 +000021 /* Check if reading the mtab file failed */
22 if ( mountTable == 0
23#if ! defined BB_FEATURE_USE_PROCFS
24 ) {
25#else
26 /* Bummer. fall back on trying the /proc filesystem */
27 && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
28#endif
Eric Andersenbc341901999-11-06 04:56:00 +000029 perror(mtab_file);
30 return;
31 }
32
33 while ( (m = getmntent(mountTable)) != 0 ) {
Erik Andersenfac10d72000-02-07 05:29:42 +000034 entries[count].mnt_fsname = strdup(m->mnt_fsname);
35 entries[count].mnt_dir = strdup(m->mnt_dir);
36 entries[count].mnt_type = strdup(m->mnt_type);
37 entries[count].mnt_opts = strdup(m->mnt_opts);
Eric Andersenbc341901999-11-06 04:56:00 +000038 entries[count].mnt_freq = m->mnt_freq;
39 entries[count].mnt_passno = m->mnt_passno;
40 count++;
41 }
42 endmntent(mountTable);
43 if ( (mountTable = setmntent(mtab_file, "w")) ) {
44 int i;
45 for ( i = 0; i < count; i++ ) {
46 int result = ( strcmp(entries[i].mnt_fsname, name) == 0
47 || strcmp(entries[i].mnt_dir, name) == 0 );
48
49 if ( result )
50 continue;
51 else
52 addmntent(mountTable, &entries[i]);
53 }
54 endmntent(mountTable);
55 }
56 else if ( errno != EROFS )
57 perror(mtab_file);
58}
59
Erik Andersenfac10d72000-02-07 05:29:42 +000060void write_mtab(char* blockDevice, char* directory,
Eric Andersenbc341901999-11-06 04:56:00 +000061 char* filesystemType, long flags, char* string_flags)
62{
63 FILE *mountTable = setmntent(mtab_file, "a+");
64 struct mntent m;
65
66 if ( mountTable == 0 ) {
67 perror(mtab_file);
68 return;
69 }
70 if (mountTable) {
71 int length = strlen(directory);
72
73 if ( length > 1 && directory[length - 1] == '/' )
74 directory[length - 1] = '\0';
75
Eric Andersen0ecb54a1999-12-05 23:24:55 +000076#ifdef BB_FEATURE_USE_PROCFS
Eric Andersenbc341901999-11-06 04:56:00 +000077 if ( filesystemType == 0 ) {
Eric Andersen0ecb54a1999-12-05 23:24:55 +000078 struct mntent *p = findMountPoint(blockDevice, "/proc/mounts");
Eric Andersenbc341901999-11-06 04:56:00 +000079
80 if ( p && p->mnt_type )
81 filesystemType = p->mnt_type;
82 }
Eric Andersen0ecb54a1999-12-05 23:24:55 +000083#endif
Eric Andersenbc341901999-11-06 04:56:00 +000084 m.mnt_fsname = blockDevice;
85 m.mnt_dir = directory;
86 m.mnt_type = filesystemType ? filesystemType : "default";
87
88 if (*string_flags) {
89 m.mnt_opts = string_flags;
90 } else {
91 if ( (flags | MS_RDONLY) == flags )
92 m.mnt_opts = "ro";
93 else
94 m.mnt_opts = "rw";
95 }
96
97 m.mnt_freq = 0;
98 m.mnt_passno = 0;
99 addmntent(mountTable, &m);
100 endmntent(mountTable);
101 }
102}
103