1f2083709f9612a4fe00ca41504eaafb07461138
[oweals/busybox.git] / mtab.c
1 /* vi: set sw=4 ts=4: */
2 #include "busybox.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>
9 #include <fstab.h>
10 #include <sys/mount.h>
11
12 extern const char mtab_file[];  /* Defined in utility.c */
13
14
15 void erase_mtab(const char *name)
16 {
17         struct mntent entries[20];
18         int count = 0;
19         FILE *mountTable = setmntent(mtab_file, "r");
20         struct mntent *m;
21
22         /* Check if reading the mtab file failed */
23         if (mountTable == 0
24 #if ! defined BB_FEATURE_USE_PROCFS
25                 ) {
26 #else
27                 /* Bummer.  fall back on trying the /proc filesystem */
28                 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
29 #endif
30                 perror(mtab_file);
31                 return;
32         }
33
34         while ((m = getmntent(mountTable)) != 0) {
35                 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);
39                 entries[count].mnt_freq = m->mnt_freq;
40                 entries[count].mnt_passno = m->mnt_passno;
41                 count++;
42         }
43         endmntent(mountTable);
44         if ((mountTable = setmntent(mtab_file, "w"))) {
45                 int i;
46
47                 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)
52                                 continue;
53                         else
54                                 addmntent(mountTable, &entries[i]);
55                 }
56                 endmntent(mountTable);
57         } else if (errno != EROFS)
58                 perror(mtab_file);
59 }
60
61 void write_mtab(char *blockDevice, char *directory,
62                                 char *filesystemType, long flags, char *string_flags)
63 {
64         FILE *mountTable = setmntent(mtab_file, "a+");
65         struct mntent m;
66
67         if (mountTable == 0) {
68                 perror(mtab_file);
69                 return;
70         }
71         if (mountTable) {
72                 int length = strlen(directory);
73
74                 if (length > 1 && directory[length - 1] == '/')
75                         directory[length - 1] = '\0';
76
77 #ifdef BB_FEATURE_USE_PROCFS
78                 if (filesystemType == 0) {
79                         struct mntent *p = find_mount_point(blockDevice, "/proc/mounts");
80
81                         if (p && p->mnt_type)
82                                 filesystemType = p->mnt_type;
83                 }
84 #endif
85                 m.mnt_fsname = blockDevice;
86                 m.mnt_dir = directory;
87                 m.mnt_type = filesystemType ? filesystemType : "default";
88
89                 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 }