forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfstab.c
282 lines (247 loc) · 7.35 KB
/
fstab.c
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include "fstab.h"
#ifndef HAVE_KDBCONFIG
#include "kdbconfig.h"
#endif
#include <kdblogger.h>
#define MAX_NUMBER_SIZE 10
/** @param name is a buffer with KDB_MAX_PATH_LENGTH space.
* @param fstabEntry will be used to get the name:
* @param swapIndex will count up for every swap
*
* - mnt_type will be checked if it is swap
*
* TODO Improvements:
* - no counting up of swap?
* - handle mountpoints none?
*
* Some logic to define the filesystem name when it is not
* so obvious.
*/
void elektraFstabFsName (char * fsname, struct mntent * fstabEntry, unsigned int * swapIndex)
{
if (!strcmp (fstabEntry->mnt_type, "swap"))
{
sprintf (fsname, "swap%02d", *swapIndex);
++(*swapIndex);
}
else if (!strcmp (fstabEntry->mnt_dir, "none"))
{
strcpy (fsname, fstabEntry->mnt_type);
}
else
{
// Otherwise take dir as-is
strcpy (fsname, fstabEntry->mnt_dir);
}
}
int elektraFstabGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned, Key * parentKey)
{
int errnosave = errno;
ssize_t nr_keys = 0;
Key * key;
Key * dir;
FILE * fstab = 0;
ELEKTRA_LOG ("get fstab %s from %s\n", keyName (parentKey), keyString (parentKey));
if (!strcmp (keyName (parentKey), "system:/elektra/modules/fstab"))
{
// clang-format off
KeySet *moduleConfig = ksNew (50,
keyNew ("system:/elektra/modules/fstab",
KEY_VALUE, "fstab plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/fstab/exports", KEY_END),
keyNew ("system:/elektra/modules/fstab/exports/get",
KEY_FUNC, elektraFstabGet,
KEY_END),
keyNew ("system:/elektra/modules/fstab/exports/set",
KEY_FUNC, elektraFstabSet,
KEY_END),
#include "readme_fstab.c"
keyNew ("system:/elektra/modules/fstab/infos/version",
KEY_VALUE, PLUGINVERSION, KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs",
KEY_VALUE, "The configuration which is needed",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct",
KEY_VALUE, "list FStab",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab",
KEY_META, "check/type", "null empty",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/device",
KEY_META, "check/type", "string",
KEY_META, "check/path", "device",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/mpoint",
KEY_META, "check/type", "string",
KEY_META, "check/path", "directory",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/type",
KEY_META, "check/type", "FSType",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/options",
KEY_META, "check/type", "string",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/dumpfreq",
KEY_META, "check/type", "unsigned_short",
KEY_END),
keyNew ("system:/elektra/modules/fstab/config/needs/struct/FStab/passno",
KEY_META, "check/type", "unsigned_short",
KEY_END),
KS_END);
// clang-format on
ksAppend (returned, moduleConfig);
ksDel (moduleConfig);
return 1;
}
key = keyDup (parentKey, KEY_CP_ALL);
ksAppendKey (returned, key);
nr_keys++;
fstab = setmntent (keyString (parentKey), "r");
if (fstab == 0)
{
ELEKTRA_SET_ERROR_GET (parentKey);
errno = errnosave;
return -1;
}
struct mntent * fstabEntry;
char fsname[KDB_MAX_PATH_LENGTH];
char buffer[MAX_NUMBER_SIZE];
unsigned int swapIndex = 0;
while ((fstabEntry = getmntent (fstab)))
{
nr_keys += 7;
elektraFstabFsName (fsname, fstabEntry, &swapIndex);
/* Include only the filesystem pseudo-names */
dir = keyDup (parentKey, KEY_CP_ALL);
keyAddBaseName (dir, fsname);
keySetString (dir, "");
keySetComment (dir, "");
keySetComment (dir, "Filesystem pseudo-name");
ksAppendKey (returned, dir);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "device");
keySetString (key, fstabEntry->mnt_fsname);
keySetComment (key, "Device or Label");
ksAppendKey (returned, key);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "mpoint");
keySetString (key, fstabEntry->mnt_dir);
keySetComment (key, "Mount point");
ksAppendKey (returned, key);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "type");
keySetString (key, fstabEntry->mnt_type);
keySetComment (key, "Filesystem type.");
ksAppendKey (returned, key);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "options");
keySetString (key, fstabEntry->mnt_opts);
keySetComment (key, "Filesystem specific options");
ksAppendKey (returned, key);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "dumpfreq");
snprintf (buffer, MAX_NUMBER_SIZE, "%d", fstabEntry->mnt_freq);
keySetString (key, buffer);
keySetComment (key, "Dump frequency in days");
ksAppendKey (returned, key);
key = keyDup (dir, KEY_CP_ALL);
keyAddBaseName (key, "passno");
snprintf (buffer, MAX_NUMBER_SIZE, "%d", fstabEntry->mnt_passno);
keySetString (key, buffer);
keySetComment (key, "Pass number on parallel fsck");
ksAppendKey (returned, key);
}
endmntent (fstab);
errno = errnosave;
return nr_keys;
}
int elektraFstabSet (Plugin * handle ELEKTRA_UNUSED, KeySet * ks, Key * parentKey)
{
int errnosave = errno;
FILE * fstab = 0;
Key * key = 0;
const void * rootname = 0;
struct mntent fstabEntry;
ELEKTRA_LOG ("set fstab %s to file %s\n", keyName (parentKey), keyString (parentKey));
ksRewind (ks);
if ((key = ksNext (ks)) != 0)
{
/*skip parent key*/
}
fstab = setmntent (keyString (parentKey), "w");
if (fstab == 0)
{
ELEKTRA_SET_ERROR_SET (parentKey);
errno = errnosave;
return -1;
}
memset (&fstabEntry, 0, sizeof (struct mntent));
while ((key = ksNext (ks)) != 0)
{
const char * basename = keyBaseName (key);
ELEKTRA_LOG ("key: %s %s\n", keyName (key), basename);
if (!strcmp (basename, "device"))
{
fstabEntry.mnt_fsname = (char *) keyValue (key);
}
else if (!strcmp (basename, "mpoint"))
{
fstabEntry.mnt_dir = (char *) keyValue (key);
}
else if (!strcmp (basename, "type"))
{
fstabEntry.mnt_type = (char *) keyValue (key);
}
else if (!strcmp (basename, "options"))
{
fstabEntry.mnt_opts = (char *) keyValue (key);
}
else if (!strcmp (basename, "dumpfreq"))
{
fstabEntry.mnt_freq = atoi ((char *) keyValue (key));
}
else if (!strcmp (basename, "passno"))
{
fstabEntry.mnt_passno = atoi ((char *) keyValue (key));
}
else
{ // new rootname
if (!rootname)
{
rootname = keyValue (key);
}
else
{
rootname = keyValue (key);
ELEKTRA_LOG ("first: %s %s %s %s %d %d\n", fstabEntry.mnt_fsname, fstabEntry.mnt_dir,
fstabEntry.mnt_type, fstabEntry.mnt_opts, fstabEntry.mnt_freq, fstabEntry.mnt_passno);
addmntent (fstab, &fstabEntry);
memset (&fstabEntry, 0, sizeof (struct mntent));
}
}
}
if (rootname)
{
ELEKTRA_LOG ("last: %s %s %s %s %d %d\n", fstabEntry.mnt_fsname, fstabEntry.mnt_dir, fstabEntry.mnt_type,
fstabEntry.mnt_opts, fstabEntry.mnt_freq, fstabEntry.mnt_passno);
addmntent (fstab, &fstabEntry);
}
endmntent (fstab);
errno = errnosave;
return 1;
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport("fstab",
ELEKTRA_PLUGIN_GET, &elektraFstabGet,
ELEKTRA_PLUGIN_SET, &elektraFstabSet,
ELEKTRA_PLUGIN_END);
}