Fix a namespace aliasing problem wereby du and dutmp, or
[oweals/busybox.git] / mtab.c
1 #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>
8 #include <sys/mount.h>
9
10 extern const char mtab_file[]; /* Defined in utility.c */
11
12 static char *
13 stralloc(const char * string)
14 {
15         int     length = strlen(string) + 1;
16         char *  n = malloc(length);
17         memcpy(n, string, length);
18         return n;
19 }
20
21 extern void
22 erase_mtab(const char * name)
23 {
24         struct mntent   entries[20];
25         int     count = 0;
26         FILE *mountTable = setmntent(mtab_file, "r");
27         struct mntent * m;
28
29         /* Check if reading the mtab file failed */
30         if ( mountTable == 0 
31 #if ! defined BB_FEATURE_USE_PROCFS
32                               ) {
33 #else 
34             /* Bummer.  fall back on trying the /proc filesystem */
35              && (mountTable = setmntent("/proc/mounts", "r")) == 0 ) {
36 #endif
37                 perror(mtab_file);
38                 return;
39         }
40
41         while ( (m = getmntent(mountTable)) != 0 ) {
42                 entries[count].mnt_fsname = stralloc(m->mnt_fsname);
43                 entries[count].mnt_dir = stralloc(m->mnt_dir);
44                 entries[count].mnt_type = stralloc(m->mnt_type);
45                 entries[count].mnt_opts = stralloc(m->mnt_opts);
46                 entries[count].mnt_freq = m->mnt_freq;
47                 entries[count].mnt_passno = m->mnt_passno;
48                 count++;
49         }
50         endmntent(mountTable);
51         if ( (mountTable = setmntent(mtab_file, "w")) ) {
52                 int     i;
53                 for ( i = 0; i < count; i++ ) {
54                         int result = ( strcmp(entries[i].mnt_fsname, name) == 0
55                          || strcmp(entries[i].mnt_dir, name) == 0 );
56
57                         if ( result )
58                                 continue;
59                         else
60                                 addmntent(mountTable, &entries[i]);
61                 }
62                 endmntent(mountTable);
63         }
64         else if ( errno != EROFS )
65                 perror(mtab_file);
66 }
67
68 extern void 
69 write_mtab(char* blockDevice, char* directory, 
70         char* filesystemType, long flags, char* string_flags)
71 {
72         FILE *mountTable = setmntent(mtab_file, "a+");
73         struct mntent m;
74
75         if ( mountTable == 0 ) {
76                 perror(mtab_file);
77                 return;
78         }
79         if (mountTable) {
80             int length = strlen(directory);
81
82             if ( length > 1 && directory[length - 1] == '/' )
83                     directory[length - 1] = '\0';
84
85 #ifdef BB_FEATURE_USE_PROCFS
86             if ( filesystemType == 0 ) {
87                     struct mntent *p = findMountPoint(blockDevice, "/proc/mounts");
88
89                     if ( p && p->mnt_type )
90                             filesystemType = p->mnt_type;
91             }
92 #endif
93             m.mnt_fsname = blockDevice;
94             m.mnt_dir = directory;
95             m.mnt_type = filesystemType ? filesystemType : "default";
96             
97             if (*string_flags) {
98                     m.mnt_opts = string_flags;
99             } else {
100                     if ( (flags | MS_RDONLY) == flags )
101                             m.mnt_opts = "ro";
102                     else        
103                             m.mnt_opts = "rw";
104             }
105             
106             m.mnt_freq = 0;
107             m.mnt_passno = 0;
108             addmntent(mountTable, &m);
109             endmntent(mountTable);
110     }
111 }
112
113