A few minor updates. ;-)
[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 <fstab.h>
9 #include <sys/mount.h>
10
11 extern const char mtab_file[]; /* Defined in utility.c */
12
13
14 void erase_mtab(const char * name)
15 {
16         struct mntent   entries[20];
17         int     count = 0;
18         FILE *mountTable = setmntent(mtab_file, "r");
19         struct mntent * m;
20
21         /* 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
29                 perror(mtab_file);
30                 return;
31         }
32
33         while ( (m = getmntent(mountTable)) != 0 ) {
34                 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);
38                 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
60 void write_mtab(char* blockDevice, char* directory, 
61         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
76 #ifdef BB_FEATURE_USE_PROCFS
77             if ( filesystemType == 0 ) {
78                     struct mntent *p = findMountPoint(blockDevice, "/proc/mounts");
79
80                     if ( p && p->mnt_type )
81                             filesystemType = p->mnt_type;
82             }
83 #endif
84             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