]>
Commit | Line | Data |
---|---|---|
d115ee9b KZ |
1 | /* |
2 | * Copyright (C) 2008-2009 Karel Zak <kzak@redhat.com> | |
3 | * | |
4 | * This file may be redistributed under the terms of the | |
5 | * GNU Lesser General Public License. | |
d115ee9b KZ |
6 | */ |
7 | ||
192c6aad KZ |
8 | /** |
9 | * SECTION: fs | |
10 | * @title: Filesystem | |
3d735589 | 11 | * @short_description: mnt_fs represents one entry in fstab/mtab/mountinfo |
192c6aad KZ |
12 | * |
13 | */ | |
d115ee9b KZ |
14 | #include <stdio.h> |
15 | #include <string.h> | |
16 | #include <stdlib.h> | |
17 | #include <ctype.h> | |
3fca8422 | 18 | #include <errno.h> |
d115ee9b KZ |
19 | #include <blkid/blkid.h> |
20 | ||
21 | #include "nls.h" | |
22 | #include "mountP.h" | |
23 | ||
24 | /** | |
25 | * mnt_new_fs: | |
26 | * | |
192c6aad | 27 | * Returns: newly allocated mnt_file fs. |
d115ee9b KZ |
28 | */ |
29 | mnt_fs *mnt_new_fs(void) | |
30 | { | |
31 | mnt_fs *fs = calloc(1, sizeof(struct _mnt_fs)); | |
32 | if (!fs) | |
33 | return NULL; | |
34 | ||
35 | INIT_LIST_HEAD(&fs->ents); | |
36 | return fs; | |
37 | } | |
38 | ||
39 | /** | |
40 | * mnt_free_fs: | |
41 | * @fs: fs pointer | |
42 | * | |
43 | * Deallocates the fs. | |
44 | */ | |
45 | void mnt_free_fs(mnt_fs *fs) | |
46 | { | |
47 | if (!fs) | |
48 | return; | |
49 | list_del(&fs->ents); | |
50 | ||
51 | free(fs->source); | |
52 | free(fs->tagname); | |
53 | free(fs->tagval); | |
54 | free(fs->mntroot); | |
55 | free(fs->target); | |
56 | free(fs->fstype); | |
57 | free(fs->optstr); | |
58 | free(fs->vfs_optstr); | |
59 | free(fs->fs_optstr); | |
60 | ||
61 | free(fs); | |
62 | } | |
63 | ||
26b4f9e4 KZ |
64 | /** |
65 | * mnt_fs_get_userdata: | |
66 | * @fs: mnt_file instance | |
67 | * | |
192c6aad | 68 | * Returns: private data set by mnt_fs_set_userdata() or NULL. |
26b4f9e4 KZ |
69 | */ |
70 | void *mnt_fs_get_userdata(mnt_fs *fs) | |
71 | { | |
72 | return fs ? fs->userdata : NULL; | |
73 | } | |
74 | ||
75 | /** | |
76 | * mnt_fs_set_userdata: | |
77 | * @fs: mnt_file instance | |
78 | * | |
79 | * The "userdata" are library independent data. | |
80 | * | |
192c6aad | 81 | * Returns: 0 or -1 in case of error (if @fs is NULL). |
26b4f9e4 KZ |
82 | */ |
83 | int mnt_fs_set_userdata(mnt_fs *fs, void *data) | |
84 | { | |
85 | if (!fs) | |
86 | return -1; | |
87 | fs->userdata = data; | |
88 | return 0; | |
89 | } | |
90 | ||
d115ee9b KZ |
91 | /** |
92 | * mnt_fs_get_srcpath: | |
93 | * @fs: mnt_file (fstab/mtab/mountinfo) fs | |
94 | * | |
95 | * The mount "source path" is: | |
192c6aad KZ |
96 | * - a directory for 'bind' mounts (in fstab or mtab only) |
97 | * - a device name for standard mounts | |
d115ee9b KZ |
98 | * |
99 | * See also mnt_fs_get_tag() and mnt_fs_get_source(). | |
100 | * | |
192c6aad | 101 | * Returns: mount source path or NULL in case of error or when the path |
d115ee9b | 102 | * is not defined. |
d115ee9b KZ |
103 | */ |
104 | const char *mnt_fs_get_srcpath(mnt_fs *fs) | |
105 | { | |
106 | assert(fs); | |
107 | if (!fs) | |
108 | return NULL; | |
109 | ||
110 | /* fstab-like fs */ | |
111 | if (fs->tagname) | |
112 | return NULL; /* the source contains a "NAME=value" */ | |
113 | return fs->source; | |
114 | } | |
115 | ||
116 | /** | |
3d735589 | 117 | * mnt_fs_get_source: |
d115ee9b KZ |
118 | * @fs: mnt_file (fstab/mtab/mountinfo) fs |
119 | * | |
192c6aad | 120 | * Returns: mount source. Note that the source could be unparsed TAG |
d115ee9b KZ |
121 | * (LABEL/UUID). See also mnt_fs_get_srcpath() and mnt_fs_get_tag(). |
122 | */ | |
123 | const char *mnt_fs_get_source(mnt_fs *fs) | |
124 | { | |
125 | return fs ? fs->source : NULL; | |
126 | } | |
127 | ||
128 | /* Used by parser mnt_file ONLY (@source has to be allocated) */ | |
129 | int __mnt_fs_set_source(mnt_fs *fs, char *source) | |
130 | { | |
131 | assert(fs); | |
132 | ||
133 | if (!source) | |
134 | return -1; | |
135 | ||
136 | if (strchr(source, '=')) { | |
137 | char *name, *val; | |
138 | ||
139 | if (blkid_parse_tag_string(source, &name, &val) != 0) | |
140 | return -1; | |
141 | ||
142 | fs->tagval = val; | |
143 | fs->tagname = name; | |
144 | } | |
145 | ||
146 | fs->source = source; | |
147 | return 0; | |
148 | } | |
149 | ||
150 | /** | |
151 | * mnt_fs_set_source: | |
152 | * @fs: fstab/mtab/mountinfo entry | |
153 | * @source: new source | |
154 | * | |
192c6aad KZ |
155 | * This function creates a private copy (strdup()) of @source. |
156 | * | |
157 | * Returns: 0 on success or -1 in case of error. | |
d115ee9b KZ |
158 | */ |
159 | int mnt_fs_set_source(mnt_fs *fs, const char *source) | |
160 | { | |
161 | char *p; | |
162 | ||
163 | if (!fs && !source) | |
164 | return -1; | |
165 | ||
166 | p = strdup(source); | |
167 | if (!p) | |
168 | return -1; | |
169 | ||
170 | free(fs->tagval); | |
171 | free(fs->tagname); | |
172 | free(fs->source); | |
173 | fs->tagval = fs->tagname = fs->source = NULL; | |
174 | ||
175 | return __mnt_fs_set_source(fs, p); | |
176 | } | |
177 | ||
178 | /** | |
179 | * mnt_fs_get_tag: | |
180 | * @fs: fs | |
181 | * @name: returns pointer to NAME string | |
182 | * @value: returns pointer to VALUE string | |
183 | * | |
184 | * "TAG" is NAME=VALUE (e.g. LABEL=foo) | |
185 | * | |
192c6aad KZ |
186 | * The TAG is the first column in the fstab file. The TAG or "srcpath" has to |
187 | * be always set for all entries. | |
d115ee9b KZ |
188 | * |
189 | * See also mnt_fs_get_source(). | |
190 | * | |
192c6aad KZ |
191 | * <informalexample> |
192 | * <programlisting> | |
d115ee9b | 193 | * char *src; |
192c6aad | 194 | * mnt_fs *fs = mnt_tab_find_target(tb, "/home", MNT_ITER_FORWARD); |
d115ee9b KZ |
195 | * |
196 | * if (!fs) | |
197 | * goto err; | |
198 | * | |
199 | * src = mnt_fs_get_srcpath(fs); | |
200 | * if (!src) { | |
201 | * char *tag, *val; | |
202 | * if (mnt_fs_get_tag(fs, &tag, &val) == 0) | |
203 | * printf("%s: %s\n", tag, val); // LABEL or UUID | |
204 | * } else | |
205 | * printf("device: %s\n", src); // device or bind path | |
192c6aad KZ |
206 | * </programlisting> |
207 | * </informalexample> | |
d115ee9b | 208 | * |
192c6aad | 209 | * Returns: 0 on success or -1 in case that a TAG is not defined. |
d115ee9b KZ |
210 | */ |
211 | int mnt_fs_get_tag(mnt_fs *fs, const char **name, const char **value) | |
212 | { | |
213 | if (fs == NULL || !fs->tagname) | |
214 | return -1; | |
215 | if (name) | |
216 | *name = fs->tagname; | |
217 | if (value) | |
218 | *value = fs->tagval; | |
219 | return 0; | |
220 | } | |
221 | ||
222 | /** | |
223 | * mnt_fs_get_target: | |
224 | * @fs: fstab/mtab/mountinfo entry pointer | |
225 | * | |
192c6aad | 226 | * Returns: pointer to mountpoint path or NULL |
d115ee9b KZ |
227 | */ |
228 | const char *mnt_fs_get_target(mnt_fs *fs) | |
229 | { | |
230 | assert(fs); | |
231 | return fs ? fs->target : NULL; | |
232 | } | |
233 | ||
234 | /** | |
235 | * mnt_fs_set_target: | |
236 | * @fs: fstab/mtab/mountinfo entry | |
237 | * @target: mountpoint | |
238 | * | |
192c6aad KZ |
239 | * This function creates a private copy (strdup()) of @target. |
240 | * | |
241 | * Returns: 0 on success or -1 in case of error. | |
d115ee9b KZ |
242 | */ |
243 | int mnt_fs_set_target(mnt_fs *fs, const char *target) | |
244 | { | |
245 | char *p; | |
246 | ||
247 | assert(fs); | |
248 | ||
249 | if (!fs || !target) | |
250 | return -1; | |
251 | ||
252 | p = strdup(target); | |
253 | if (!p) | |
254 | return -1; | |
255 | free(fs->target); | |
256 | fs->target = p; | |
257 | ||
258 | return 0; | |
259 | } | |
260 | ||
261 | /** | |
262 | * mnt_fs_get_fstype: | |
263 | * @fs: fstab/mtab/mountinfo entry pointer | |
264 | * | |
192c6aad | 265 | * Returns: pointer to filesystem type. |
d115ee9b KZ |
266 | */ |
267 | const char *mnt_fs_get_fstype(mnt_fs *fs) | |
268 | { | |
269 | assert(fs); | |
270 | return fs ? fs->fstype : NULL; | |
271 | } | |
272 | ||
273 | /* Used by mnt_file parser only */ | |
274 | int __mnt_fs_set_fstype(mnt_fs *fs, char *fstype) | |
275 | { | |
276 | assert(fs); | |
277 | ||
278 | if (!fstype) | |
279 | return -1; | |
280 | ||
281 | fs->fstype = fstype; | |
282 | fs->flags &= ~MNT_FS_PSEUDO; | |
283 | fs->flags &= ~MNT_FS_NET; | |
284 | ||
285 | /* save info about pseudo filesystems */ | |
286 | if (mnt_fstype_is_pseudofs(fs->fstype)) | |
287 | fs->flags |= MNT_FS_PSEUDO; | |
288 | else if (mnt_fstype_is_netfs(fs->fstype)) | |
289 | fs->flags |= MNT_FS_NET; | |
290 | ||
291 | return 0; | |
292 | } | |
293 | ||
294 | /** | |
295 | * mnt_fs_set_fstype: | |
296 | * @fs: fstab/mtab/mountinfo entry | |
297 | * @fstype: filesystem type | |
298 | * | |
192c6aad KZ |
299 | * This function creates a private copy (strdup()) of @fstype. |
300 | * | |
301 | * Returns: 0 on success or -1 in case of error. | |
d115ee9b KZ |
302 | */ |
303 | int mnt_fs_set_fstype(mnt_fs *fs, const char *fstype) | |
304 | { | |
305 | char *p; | |
306 | ||
307 | if (!fs || !fstype) | |
308 | return -1; | |
309 | ||
310 | p = strdup(fstype); | |
311 | if (!p) | |
312 | return -1; | |
313 | free(fs->fstype); | |
314 | ||
315 | return __mnt_fs_set_fstype(fs, p); | |
316 | } | |
317 | ||
318 | /** | |
319 | * mnt_fs_get_optstr: | |
320 | * @fs: fstab/mtab/mountinfo entry pointer | |
321 | * | |
192c6aad | 322 | * Returns: pointer to mount option string with all options (FS and VFS) |
d115ee9b KZ |
323 | */ |
324 | const char *mnt_fs_get_optstr(mnt_fs *fs) | |
325 | { | |
326 | assert(fs); | |
327 | return fs ? fs->optstr : NULL; | |
328 | } | |
329 | ||
330 | /** | |
331 | * mnt_fs_set_optstr: | |
332 | * @fs: fstab/mtab/mountinfo entry | |
333 | * @optstr: options string | |
334 | * | |
192c6aad KZ |
335 | * This function creates a private copy (strdup()) of @optstr. |
336 | * | |
337 | * Returns: 0 on success or -1 in case of error. | |
d115ee9b KZ |
338 | */ |
339 | int mnt_fs_set_optstr(mnt_fs *fs, const char *optstr) | |
340 | { | |
192c6aad KZ |
341 | char *p; |
342 | ||
d115ee9b KZ |
343 | assert(fs); |
344 | ||
345 | if (!fs || !optstr) | |
346 | return -1; | |
192c6aad KZ |
347 | p = strdup(optstr); |
348 | if (!p) | |
349 | return -1; | |
350 | ||
d115ee9b KZ |
351 | free(fs->optstr); |
352 | free(fs->fs_optstr); | |
353 | free(fs->vfs_optstr); | |
354 | fs->fs_optstr = fs->vfs_optstr = NULL; | |
355 | ||
192c6aad | 356 | fs->optstr = p; |
d115ee9b | 357 | |
192c6aad | 358 | return 0; |
d115ee9b KZ |
359 | } |
360 | ||
361 | /** | |
362 | * mnt_fs_get_fs_optstr: | |
363 | * @fs: fstab/mtab/mountinfo entry pointer | |
364 | * | |
365 | * This function works for "mountinfo" files only. | |
366 | * | |
192c6aad | 367 | * Returns: pointer to superblock (fs-depend) mount option string or NULL. |
d115ee9b KZ |
368 | */ |
369 | const char *mnt_fs_get_fs_optstr(mnt_fs *fs) | |
370 | { | |
371 | assert(fs); | |
372 | return fs ? fs->fs_optstr : NULL; | |
373 | } | |
374 | ||
375 | /** | |
376 | * mnt_fs_get_vfs_optstr: | |
efe73c3e | 377 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
378 | * |
379 | * This function works for "mountinfo" files only. | |
380 | * | |
192c6aad | 381 | * Returns: pointer to fs-independent (VFS) mount option string or NULL. |
d115ee9b KZ |
382 | */ |
383 | const char *mnt_fs_get_vfs_optstr(mnt_fs *fs) | |
384 | { | |
385 | assert(fs); | |
386 | return fs ? fs->vfs_optstr : NULL; | |
387 | } | |
388 | ||
389 | ||
390 | /** | |
391 | * mnt_fs_get_freq: | |
392 | * @fs: fstab/mtab/mountinfo entry pointer | |
393 | * | |
3d735589 | 394 | * Returns: dump frequency in days. |
d115ee9b KZ |
395 | */ |
396 | int mnt_fs_get_freq(mnt_fs *fs) | |
397 | { | |
398 | assert(fs); | |
399 | return fs ? fs->freq : 0; | |
400 | } | |
401 | ||
402 | /** | |
403 | * mnt_fs_set_freq: | |
efe73c3e | 404 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
405 | * @freq: dump frequency in days |
406 | * | |
192c6aad | 407 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
408 | */ |
409 | int mnt_fs_set_freq(mnt_fs *fs, int freq) | |
410 | { | |
411 | assert(fs); | |
d115ee9b KZ |
412 | if (!fs) |
413 | return -1; | |
414 | fs->freq = freq; | |
415 | return 0; | |
416 | } | |
417 | ||
418 | /** | |
419 | * mnt_fs_get_passno: | |
efe73c3e | 420 | * @fs: fstab/mtab entry pointer |
d115ee9b | 421 | * |
192c6aad | 422 | * Returns: "pass number on parallel fsck". |
d115ee9b KZ |
423 | */ |
424 | int mnt_fs_get_passno(mnt_fs *fs) | |
425 | { | |
426 | assert(fs); | |
427 | return fs ? fs->passno: 0; | |
428 | } | |
429 | ||
430 | /** | |
431 | * mnt_fs_set_passno: | |
efe73c3e | 432 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
433 | * @passno: pass number |
434 | * | |
192c6aad | 435 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
436 | */ |
437 | int mnt_fs_set_passno(mnt_fs *fs, int passno) | |
438 | { | |
439 | assert(fs); | |
d115ee9b KZ |
440 | if (!fs) |
441 | return -1; | |
442 | fs->passno = passno; | |
443 | return 0; | |
444 | } | |
445 | ||
efe73c3e KZ |
446 | /** |
447 | * mnt_fs_get_id: | |
448 | * @fs: /proc/self/mountinfo entry | |
449 | * | |
450 | * Returns: mount ID (unique identifier of the mount) or -1 if ID undefined | |
451 | * (for example if @fs is not mountinfo entry). | |
452 | */ | |
453 | int mnt_fs_get_id(mnt_fs *fs) | |
454 | { | |
455 | assert(fs); | |
456 | return fs ? fs->id : -1; | |
457 | } | |
458 | ||
459 | /** | |
460 | * mnt_fs_get_parent_id: | |
461 | * @fs: /proc/self/mountinfo entry | |
462 | * | |
463 | * Returns: parent mount ID or -1 if ID undefined (for example if @fs is not | |
464 | * mountinfo entry). | |
465 | */ | |
466 | int mnt_fs_get_parent_id(mnt_fs *fs) | |
467 | { | |
468 | assert(fs); | |
469 | return fs ? fs->parent : -1; | |
470 | } | |
471 | ||
472 | /** | |
473 | * mnt_fs_get_devno: | |
474 | * @fs: /proc/self/mountinfo | |
475 | * | |
476 | * Returns: value of st_dev for files on filesystem or 0 in case of error. | |
477 | */ | |
478 | dev_t mnt_fs_get_devno(mnt_fs *fs) | |
479 | { | |
480 | assert(fs); | |
481 | return fs ? fs->devno : 0; | |
482 | } | |
483 | ||
d115ee9b KZ |
484 | /** |
485 | * mnt_fs_get_option: | |
486 | * @fs: fstab/mtab/mountinfo entry pointer | |
487 | * @name: option name | |
488 | * @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL | |
489 | * @valsz: returns size of options value or 0 | |
490 | * | |
192c6aad | 491 | * Returns: 0 on success, 1 when not found the @name or -1 in case of error. |
d115ee9b KZ |
492 | */ |
493 | int mnt_fs_get_option(mnt_fs *fs, const char *name, | |
494 | char **value, size_t *valsz) | |
495 | { | |
496 | char *optstr = (char *) mnt_fs_get_optstr(fs); | |
497 | return optstr ? mnt_optstr_get_option(optstr, name, value, valsz) : 1; | |
498 | } | |
499 | ||
3fca8422 KZ |
500 | /** |
501 | * mnt_fs_match_target: | |
502 | * @fs: filesystem | |
503 | * @target: mountpoint path | |
504 | * @cache: tags/paths cache or NULL | |
505 | * | |
506 | * Possible are three attempts: | |
507 | * 1) compare @target with @fs->target | |
508 | * 2) realpath(@target) with @fs->target | |
509 | * 3) realpath(@target) with realpath(@fs->target). | |
510 | * | |
511 | * The 2nd and 3rd attempts are not performed when @cache is NULL. | |
512 | * | |
192c6aad | 513 | * Returns: 1 if @fs target is equal to @target else 0. |
3fca8422 KZ |
514 | */ |
515 | int mnt_fs_match_target(mnt_fs *fs, const char *target, mnt_cache *cache) | |
516 | { | |
517 | int rc = 0; | |
518 | ||
519 | if (!fs || !target || !fs->target) | |
520 | return 0; | |
521 | ||
522 | /* 1) native paths */ | |
523 | rc = !strcmp(target, fs->target); | |
524 | ||
525 | if (!rc && cache) { | |
526 | /* 2) - canonicalized and non-canonicalized */ | |
527 | char *cn = mnt_resolve_path(target, cache); | |
528 | rc = (cn && strcmp(cn, fs->target) == 0); | |
529 | ||
530 | /* 3) - canonicalized and canonicalized */ | |
531 | if (!rc && cn) { | |
532 | char *tcn = mnt_resolve_path(fs->target, cache); | |
533 | rc = (tcn && strcmp(cn, tcn) == 0); | |
534 | } | |
535 | } | |
536 | ||
537 | return rc; | |
538 | } | |
539 | ||
540 | /** | |
541 | * mnt_fs_match_source: | |
542 | * @fs: filesystem | |
543 | * @source: tag or path (device or so) | |
544 | * @cache: tags/paths cache or NULL | |
545 | * | |
546 | * Possible are four attempts: | |
547 | * 1) compare @source with @fs->source | |
548 | * 2) compare realpath(@source) with @fs->source | |
549 | * 3) compare realpath(@source) with realpath(@fs->source) | |
550 | * 4) compare realpath(@source) with evaluated tag from @fs->source | |
551 | * | |
552 | * The 2nd, 3rd and 4th attempts are not performed when @cache is NULL. The | |
553 | * 2nd and 3rd attempts are not performed if @fs->source is tag. | |
554 | * | |
192c6aad | 555 | * Returns: 1 if @fs source is equal to @source else 0. |
3fca8422 KZ |
556 | */ |
557 | int mnt_fs_match_source(mnt_fs *fs, const char *source, mnt_cache *cache) | |
558 | { | |
3fca8422 KZ |
559 | char *cn; |
560 | const char *src, *t, *v; | |
561 | ||
562 | if (!fs || !source || !fs->source) | |
563 | return 0; | |
564 | ||
565 | /* 1) native paths/tags */ | |
3ef87248 KZ |
566 | if (!strcmp(source, fs->source)) |
567 | return 1; | |
3fca8422 | 568 | |
3ef87248 KZ |
569 | if (!cache) |
570 | return 0; | |
3fca8422 KZ |
571 | if (fs->flags & (MNT_FS_NET | MNT_FS_PSEUDO)) |
572 | return 0; | |
573 | ||
574 | cn = mnt_resolve_spec(source, cache); | |
575 | if (!cn) | |
576 | return 0; | |
577 | ||
578 | /* 2) canonicalized and native */ | |
579 | src = mnt_fs_get_srcpath(fs); | |
3ef87248 KZ |
580 | if (src && !strcmp(cn, src)) |
581 | return 1; | |
3fca8422 KZ |
582 | |
583 | /* 3) canonicalized and canonicalized */ | |
3ef87248 | 584 | if (src) { |
3fca8422 | 585 | src = mnt_resolve_path(src, cache); |
3ef87248 KZ |
586 | if (src && !strcmp(cn, src)) |
587 | return 1; | |
3fca8422 | 588 | } |
3ef87248 KZ |
589 | if (src || mnt_fs_get_tag(fs, &t, &v)) |
590 | /* src path does not match and tag is not defined */ | |
3fca8422 KZ |
591 | return 0; |
592 | ||
593 | /* read @source's tags to the cache */ | |
ba7232a1 | 594 | if (mnt_cache_read_tags(cache, cn) < 0) { |
3fca8422 KZ |
595 | if (errno == EACCES) { |
596 | /* we don't have permissions to read TAGs from | |
597 | * @source, but can translate @fs tag to devname. | |
598 | * | |
599 | * (because libblkid uses udev symlinks and this is | |
600 | * accessible for non-root uses) | |
601 | */ | |
602 | char *x = mnt_resolve_tag(t, v, cache); | |
603 | if (x && !strcmp(x, cn)) | |
604 | return 1; | |
605 | } | |
606 | return 0; | |
607 | } | |
608 | ||
609 | /* 4) has the @source a tag that matches with tag from @fs ? */ | |
3ef87248 KZ |
610 | if (mnt_cache_device_has_tag(cache, cn, t, v)) |
611 | return 1; | |
3fca8422 | 612 | |
3ef87248 | 613 | return 0; |
3fca8422 KZ |
614 | } |
615 | ||
616 | /** | |
617 | * mnt_fs_match_fstype: | |
618 | * @fs: filesystem | |
619 | * @types: filesystem name or comma delimited list of filesystems | |
620 | * | |
621 | * For more details see mnt_match_fstype(). | |
622 | * | |
192c6aad | 623 | * Returns: 1 if @fs type is matching to @types else 0. The function returns |
3fca8422 KZ |
624 | * 0 when types is NULL. |
625 | */ | |
626 | int mnt_fs_match_fstype(mnt_fs *fs, const char *types) | |
627 | { | |
628 | return mnt_match_fstype(fs->fstype, types); | |
629 | } | |
630 | ||
631 | /** | |
632 | * mnt_fs_match_options: | |
633 | * @fs: filesystem | |
634 | * @options: comma delimited list of options (and nooptions) | |
635 | * | |
636 | * For more details see mnt_match_options(). | |
637 | * | |
192c6aad | 638 | * Returns: 1 if @fs type is matching to @options else 0. The function returns |
3fca8422 KZ |
639 | * 0 when types is NULL. |
640 | */ | |
641 | int mnt_fs_match_options(mnt_fs *fs, const char *options) | |
642 | { | |
643 | return mnt_match_options(fs->optstr, options); | |
644 | } | |
645 | ||
d115ee9b KZ |
646 | /* Unfortunately the classical Unix /etc/mtab and /etc/fstab |
647 | do not handle directory names containing spaces. | |
648 | Here we mangle them, replacing a space by \040. | |
649 | What do other Unices do? */ | |
650 | ||
651 | static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' }; | |
652 | ||
653 | static char *mangle(const char *s) | |
654 | { | |
655 | char *ss, *sp; | |
656 | int n; | |
657 | ||
658 | n = strlen(s); | |
659 | ss = sp = malloc(4*n+1); | |
660 | if (!sp) | |
661 | return NULL; | |
662 | while(1) { | |
663 | for (n = 0; n < sizeof(need_escaping); n++) { | |
664 | if (*s == need_escaping[n]) { | |
665 | *sp++ = '\\'; | |
666 | *sp++ = '0' + ((*s & 0300) >> 6); | |
667 | *sp++ = '0' + ((*s & 070) >> 3); | |
668 | *sp++ = '0' + (*s & 07); | |
669 | goto next; | |
670 | } | |
671 | } | |
672 | *sp++ = *s; | |
673 | if (*s == 0) | |
674 | break; | |
675 | next: | |
676 | s++; | |
677 | } | |
678 | return ss; | |
679 | } | |
680 | ||
681 | /** | |
682 | * mnt_fprintf_line: | |
683 | * @f: FILE | |
192c6aad | 684 | * @fmt: printf-like format string (see MNT_TAB_PRINTFMT) |
d115ee9b KZ |
685 | * @source: (spec) device name or tag=value |
686 | * @target: mountpoint | |
687 | * @fstype: filesystem type | |
688 | * @options: mount options | |
689 | * @freq: dump frequency in days | |
690 | * @passno: pass number on parallel fsck | |
691 | * | |
192c6aad KZ |
692 | * It's recommended to use this function rather than directly call fprintf() to |
693 | * write an entry to mtab/fstab. All data in these files has to be properly | |
694 | * formatted (for example space within paths/tags has to be escaped, see | |
695 | * fstab(5) for more details). | |
696 | * | |
697 | * Returns: return value from fprintf(). | |
d115ee9b KZ |
698 | */ |
699 | int mnt_fprintf_line( FILE *f, | |
700 | const char *fmt, | |
701 | const char *source, | |
702 | const char *target, | |
703 | const char *fstype, | |
704 | const char *options, | |
705 | int freq, | |
706 | int passno) | |
707 | { | |
708 | char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL; | |
709 | int rc = -1; | |
710 | ||
711 | if (!f || !fmt || !source || !target || !fstype || !options) | |
712 | return -1; | |
713 | ||
714 | m1 = mangle(source); | |
715 | m2 = mangle(target); | |
716 | m3 = mangle(fstype); | |
717 | m4 = mangle(options); | |
718 | ||
719 | if (!m1 || !m2 || !m3 || !m4) | |
720 | goto done; | |
721 | ||
722 | rc = fprintf(f, fmt, m1, m2, m3, m4, freq, passno); | |
723 | done: | |
724 | free(m1); | |
725 | free(m2); | |
726 | free(m3); | |
727 | free(m4); | |
728 | ||
729 | return rc; | |
730 | } | |
731 | ||
732 | /** | |
733 | * mnt_fs_fprintf: | |
734 | * @fs: fstab/mtab/mountinfo entry | |
735 | * @f: FILE | |
192c6aad | 736 | * @fmt: printf-like format string (see MNT_TAB_PRINTFMT) |
d115ee9b | 737 | * |
192c6aad | 738 | * Returns: return value from fprintf(). |
d115ee9b KZ |
739 | */ |
740 | int mnt_fs_fprintf(mnt_fs *fs, FILE *f, const char *fmt) | |
741 | { | |
742 | assert(fs); | |
743 | assert(f); | |
744 | assert(fmt); | |
745 | ||
746 | if (!fs || !f) | |
747 | return -1; | |
748 | ||
749 | return mnt_fprintf_line(f, fmt, | |
750 | mnt_fs_get_source(fs), | |
751 | mnt_fs_get_target(fs), | |
752 | mnt_fs_get_fstype(fs), | |
753 | mnt_fs_get_optstr(fs), | |
754 | mnt_fs_get_freq(fs), | |
755 | mnt_fs_get_passno(fs)); | |
756 | } | |
757 | ||
758 | /** | |
759 | * mnt_fs_print_debug | |
760 | * @fs: fstab/mtab/mountinfo entry | |
761 | * @file: output | |
762 | * | |
192c6aad | 763 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
764 | */ |
765 | int mnt_fs_print_debug(mnt_fs *fs, FILE *file) | |
766 | { | |
767 | if (!fs) | |
768 | return -1; | |
769 | fprintf(file, "------ fs: %p\n", fs); | |
770 | fprintf(file, "source: %s\n", mnt_fs_get_source(fs)); | |
771 | fprintf(file, "target: %s\n", mnt_fs_get_target(fs)); | |
772 | fprintf(file, "fstype: %s\n", mnt_fs_get_fstype(fs)); | |
773 | fprintf(file, "optstr: %s\n", mnt_fs_get_optstr(fs)); | |
774 | fprintf(file, "freq: %d\n", mnt_fs_get_freq(fs)); | |
775 | fprintf(file, "pass: %d\n", mnt_fs_get_passno(fs)); | |
efe73c3e KZ |
776 | fprintf(file, "id: %d\n", mnt_fs_get_id(fs)); |
777 | fprintf(file, "parent: %d\n", mnt_fs_get_parent_id(fs)); | |
778 | fprintf(file, "devno: %d:%d\n", major(mnt_fs_get_devno(fs)), | |
779 | minor(mnt_fs_get_devno(fs))); | |
780 | ||
d115ee9b KZ |
781 | |
782 | return 0; | |
783 | } |