Start 1.33.0 development cycle
[oweals/busybox.git] / libbb / mtab.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9 #include <mntent.h>
10 #include "libbb.h"
11
12 #if ENABLE_FEATURE_MTAB_SUPPORT
13 void FAST_FUNC erase_mtab(const char *name)
14 {
15         struct mntent *entries;
16         int i, count;
17         FILE *mountTable;
18         struct mntent *m;
19
20         mountTable = setmntent(bb_path_mtab_file, "r");
21         /* Bummer. Fall back on trying the /proc filesystem */
22         if (!mountTable) mountTable = setmntent("/proc/mounts", "r");
23         if (!mountTable) {
24                 bb_simple_perror_msg(bb_path_mtab_file);
25                 return;
26         }
27
28         entries = NULL;
29         count = 0;
30         while ((m = getmntent(mountTable)) != 0) {
31                 entries = xrealloc_vector(entries, 3, count);
32                 entries[count].mnt_fsname = xstrdup(m->mnt_fsname);
33                 entries[count].mnt_dir = xstrdup(m->mnt_dir);
34                 entries[count].mnt_type = xstrdup(m->mnt_type);
35                 entries[count].mnt_opts = xstrdup(m->mnt_opts);
36                 entries[count].mnt_freq = m->mnt_freq;
37                 entries[count].mnt_passno = m->mnt_passno;
38                 count++;
39         }
40         endmntent(mountTable);
41
42 //TODO: make update atomic
43         mountTable = setmntent(bb_path_mtab_file, "w");
44         if (mountTable) {
45                 for (i = 0; i < count; i++) {
46                         if (strcmp(entries[i].mnt_fsname, name) != 0
47                          && strcmp(entries[i].mnt_dir, name) != 0)
48                                 addmntent(mountTable, &entries[i]);
49                 }
50                 endmntent(mountTable);
51         } else if (errno != EROFS)
52                 bb_simple_perror_msg(bb_path_mtab_file);
53 }
54 #endif