-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisktab.c
More file actions
111 lines (87 loc) · 1.98 KB
/
disktab.c
File metadata and controls
111 lines (87 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <mntent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "util.h"
#include "diskconf.h"
struct diskent {
char *mount_device;
char *mount_point;
struct list_head list;
};
LLIST_HEAD(mount_tab);
void tab_del(const char *devfile)
{
struct diskent *tmp;
struct diskent *ent = NULL;
list_for_each_entry(tmp, &mount_tab, list) {
ent = tmp;
if (!strcmp(tmp->mount_device, devfile))
break;
ent = NULL;
}
if (!ent)
return;
list_del(&ent->list);
free(ent->mount_device);
free(ent->mount_point);
free(ent);
}
void tab_add(const char *devfile, const char *mntfile)
{
struct diskent *def;
if (!strlen(devfile) || !strlen(mntfile)) {
vwarn("Skipped invalid mount entry: '%s' -> '%s'", devfile, mntfile);
return;
}
if (devfile[0] != '/') {
vinfo("Skipped incorrect mount entry: '%s' -> '%s'", devfile, mntfile);
return;
}
def = calloc(1, sizeof(*def));
if (!def)
die("malloc() failed");
def->mount_device = strdup(devfile);
def->mount_point = strdup(mntfile);
list_add_tail(&def->list, &mount_tab);
vinfo("Added mount entry: '%s' -> '%s'", devfile, mntfile);
}
void tab_load(void)
{
FILE *fp;
struct mntent *ent;
fp = setmntent("/etc/mtab", "r");
if (!fp)
fp = setmntent("/proc/mounts", "r");
if (!fp) {
warn("Cannot load mounts");
return;
}
vinfo("Loading mounts");
while (NULL != (ent = getmntent(fp))) {
if (!conf_has_mount(ent->mnt_dir)) {
vinfo("Skipped non-configed mount entry: '%s' -> '%s'",
ent->mnt_fsname, ent->mnt_dir);
continue;
}
tab_add(ent->mnt_fsname, ent->mnt_dir);
}
endmntent(fp);
}
char *tab_find(const char *devfile)
{
struct diskent *ent;
list_for_each_entry(ent, &mount_tab, list) {
if (!strcmp(ent->mount_device, devfile))
return ent->mount_point;
}
return NULL;
}
void tab_dump(FILE *fp)
{
struct diskent *ent;
fprintf(fp, "Mount cache:\n");
list_for_each_entry(ent, &mount_tab, list)
fprintf(fp, "%s\t\t%s\n", ent->mount_device, ent->mount_point);
}