-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevtable.h
247 lines (219 loc) · 6.41 KB
/
devtable.h
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* microfs - Minimally Improved Compressed Read Only File System
* Copyright (C) 2012, 2013, 2014, 2015, 2016, 2017, ..., +%Y
* Erik Edlund <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef DEVTABLE_H
#define DEVTABLE_H
#ifndef HOSTPROGS_H
#error "#include hostprogs.h first"
#endif
#ifndef __MICROFS_H__
#error "#include microfs.h first"
#endif
struct devtable_entry {
char dt_type;
char* dt_path;
size_t dt_pathlen;
unsigned long dt_mode;
unsigned long dt_uid;
unsigned long dt_gid;
unsigned long dt_dev_major;
unsigned long dt_dev_minor;
unsigned long dt_dev_start;
unsigned long dt_dev_incr;
unsigned long dt_dev_count;
};
struct devtable_dentry {
char* de_path;
size_t de_pathlen;
dev_t de_dev;
unsigned long de_mode;
unsigned long de_uid;
unsigned long de_gid;
};
/* Callback signature for %devtable_parse, do not try to %free
* any of the pointers in the callback.
*/
typedef void (*devtable_process_dentry_t)(struct devtable_dentry* const ent,
void* data, const char* file, const char* line, const __u64 linenumber);
static inline void devtable_interpret_entry(devtable_process_dentry_t callback,
void* data, const char* path, const char* line, const __u64 linenumber,
const int devbits)
{
struct devtable_entry devt_ent = {
.dt_type = '?',
.dt_path = NULL,
.dt_pathlen = 0,
.dt_mode = 0755,
.dt_uid = 0,
.dt_gid = 0,
.dt_dev_major = 0,
.dt_dev_minor = 0,
.dt_dev_start = 0,
.dt_dev_incr = 0,
.dt_dev_count = 0
};
/* Mute the warning about the "m"-modifier not being part
* of the C11 standard.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
if (sscanf(line, "%ms %c %lo %lu %lu %lu %lu %lu %lu %lu",
&devt_ent.dt_path, &devt_ent.dt_type, &devt_ent.dt_mode,
&devt_ent.dt_uid, &devt_ent.dt_gid,
&devt_ent.dt_dev_major, &devt_ent.dt_dev_minor,
&devt_ent.dt_dev_start, &devt_ent.dt_dev_incr,
&devt_ent.dt_dev_count) < 0) {
error("failed to read the device table entry \"%s\" at line %llu: %s",
line, linenumber, strerror(errno));
}
#pragma GCC diagnostic pop
devt_ent.dt_pathlen = strlen(devt_ent.dt_path);
if (devt_ent.dt_pathlen == 0) {
error("entry path not properly parsed, giving up at line %llu",
linenumber);
} else if (devt_ent.dt_path[0] != '/') {
error("device table entries require absolute paths,"
" \"%s\" is not at line %llu", devt_ent.dt_path, linenumber);
}
switch (devt_ent.dt_type) {
case 'd':
devt_ent.dt_mode |= S_IFDIR;
break;
case 'f':
devt_ent.dt_mode |= S_IFREG;
break;
case 'p':
devt_ent.dt_mode |= S_IFIFO;
break;
case 'c':
devt_ent.dt_mode |= S_IFCHR;
break;
case 'b':
devt_ent.dt_mode |= S_IFBLK;
break;
default:
error("unsupported file type '%c' at line %llu",
devt_ent.dt_type, linenumber);
}
struct devtable_dentry devt_dent = {
.de_mode = devt_ent.dt_mode,
.de_uid = devt_ent.dt_uid,
.de_gid = devt_ent.dt_gid
};
if (devt_ent.dt_dev_count > 0) {
for (unsigned long i = devt_ent.dt_dev_start; i < devt_ent.dt_dev_count; i++) {
if (asprintf(&devt_dent.de_path, "%s%lu", devt_ent.dt_path, i) < 0)
error("failed to format the device path: %s", strerror(errno));
devt_dent.de_pathlen = strlen(devt_dent.de_path);
devt_dent.de_dev = makedev_lim(devt_ent.dt_dev_major,
minor_n(devt_ent.dt_dev_major, devt_ent.dt_dev_start,
devt_ent.dt_dev_incr, i), devbits);
callback(&devt_dent, data, path, line, linenumber);
free(devt_dent.de_path);
}
} else {
devt_dent.de_path = devt_ent.dt_path;
devt_dent.de_pathlen = devt_ent.dt_pathlen;
devt_dent.de_dev = makedev_lim(devt_ent.dt_dev_major, devt_ent.dt_dev_minor,
devbits);
callback(&devt_dent, data, path, line, linenumber);
}
free(devt_ent.dt_path);
}
static inline void devtable_parse(devtable_process_dentry_t callback,
void* data, const char* path, const int devbits)
{
FILE* devtable = fopen(path, "r");
if (!devtable) {
error("failed to open device table \"%s\": %s",
path, strerror(errno));
}
char* line = NULL;
size_t length = 0;
size_t leading = 0;
size_t linenumber = 1;
while (getline(&line, &length, devtable) != -1) {
length = strlen(line);
/* First trim trailing, then trim leading whitespace.
*/
while (length > 0 && isspace(line[length - 1]))
line[--length] = '\0';
leading = strspn(line, " \n\r\t\v");
length -= leading;
memmove(line, line + leading, length);
if (length && *line != '#') {
devtable_interpret_entry(callback, data, path,
line, linenumber++, devbits);
}
free(line);
line = NULL;
length = 0;
}
}
static inline int devtable_writable(const struct stat* const st)
{
return (
S_ISDIR(st->st_mode) ||
S_ISCHR(st->st_mode) ||
S_ISBLK(st->st_mode) ||
S_ISFIFO(st->st_mode)
);
}
static inline void devtable_write(FILE* const devtable, const int devbits,
struct hostprog_path* const rootpath, struct hostprog_path* const dirpath,
struct stat* const dirpathst)
{
__u64 rootpath_lvl = hostprog_path_lvls(rootpath);
dev_t dev = makedev_lim(
major(dirpathst->st_rdev),
minor(dirpathst->st_rdev),
devbits
);
char dev_start = '-';
char dev_incr = '-';
char dev_count = '-';
if (hostprog_path_append(rootpath, dirpath->p_path) < 0)
error("failed to append \"%s\" to the root path", dirpath->p_path);
int err = fprintf(devtable,
"%s\t"
"%c\t"
"%lo\t"
"%lu\t"
"%lu\t"
"%d\t"
"%d\t"
"%c\t"
"%c\t"
"%c\n",
rootpath->p_path,
nodtype(dirpathst->st_mode),
(unsigned long)dirpathst->st_mode,
(unsigned long)dirpathst->st_uid,
(unsigned long)dirpathst->st_gid,
major(dev),
minor(dev),
dev_start,
dev_incr,
dev_count);
if (err < 0) {
error("failed to write a devtable row for \"%s\": %s",
rootpath->p_path, strerror(errno));
}
hostprog_path_dirnamelvl(rootpath, rootpath_lvl);
}
#endif