]>
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> |
8e368761 | 19 | #include <blkid.h> |
d115ee9b KZ |
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); | |
0b3953a3 | 54 | free(fs->root); |
d115ee9b KZ |
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 | ||
d6fead1e KZ |
356 | /* TODO: it would be possible to use built-in maps of options |
357 | * and differentiate between VFS and FS options, then we can | |
358 | * set fs_optstr and vfs_optstr */ | |
359 | ||
192c6aad | 360 | fs->optstr = p; |
d115ee9b | 361 | |
192c6aad | 362 | return 0; |
d115ee9b KZ |
363 | } |
364 | ||
365 | /** | |
366 | * mnt_fs_get_fs_optstr: | |
367 | * @fs: fstab/mtab/mountinfo entry pointer | |
368 | * | |
369 | * This function works for "mountinfo" files only. | |
370 | * | |
192c6aad | 371 | * Returns: pointer to superblock (fs-depend) mount option string or NULL. |
d115ee9b KZ |
372 | */ |
373 | const char *mnt_fs_get_fs_optstr(mnt_fs *fs) | |
374 | { | |
375 | assert(fs); | |
376 | return fs ? fs->fs_optstr : NULL; | |
377 | } | |
378 | ||
379 | /** | |
380 | * mnt_fs_get_vfs_optstr: | |
efe73c3e | 381 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
382 | * |
383 | * This function works for "mountinfo" files only. | |
384 | * | |
192c6aad | 385 | * Returns: pointer to fs-independent (VFS) mount option string or NULL. |
d115ee9b KZ |
386 | */ |
387 | const char *mnt_fs_get_vfs_optstr(mnt_fs *fs) | |
388 | { | |
389 | assert(fs); | |
390 | return fs ? fs->vfs_optstr : NULL; | |
391 | } | |
392 | ||
393 | ||
394 | /** | |
395 | * mnt_fs_get_freq: | |
396 | * @fs: fstab/mtab/mountinfo entry pointer | |
397 | * | |
3d735589 | 398 | * Returns: dump frequency in days. |
d115ee9b KZ |
399 | */ |
400 | int mnt_fs_get_freq(mnt_fs *fs) | |
401 | { | |
402 | assert(fs); | |
403 | return fs ? fs->freq : 0; | |
404 | } | |
405 | ||
406 | /** | |
407 | * mnt_fs_set_freq: | |
efe73c3e | 408 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
409 | * @freq: dump frequency in days |
410 | * | |
192c6aad | 411 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
412 | */ |
413 | int mnt_fs_set_freq(mnt_fs *fs, int freq) | |
414 | { | |
415 | assert(fs); | |
d115ee9b KZ |
416 | if (!fs) |
417 | return -1; | |
418 | fs->freq = freq; | |
419 | return 0; | |
420 | } | |
421 | ||
422 | /** | |
423 | * mnt_fs_get_passno: | |
efe73c3e | 424 | * @fs: fstab/mtab entry pointer |
d115ee9b | 425 | * |
192c6aad | 426 | * Returns: "pass number on parallel fsck". |
d115ee9b KZ |
427 | */ |
428 | int mnt_fs_get_passno(mnt_fs *fs) | |
429 | { | |
430 | assert(fs); | |
431 | return fs ? fs->passno: 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * mnt_fs_set_passno: | |
efe73c3e | 436 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
437 | * @passno: pass number |
438 | * | |
192c6aad | 439 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
440 | */ |
441 | int mnt_fs_set_passno(mnt_fs *fs, int passno) | |
442 | { | |
443 | assert(fs); | |
d115ee9b KZ |
444 | if (!fs) |
445 | return -1; | |
446 | fs->passno = passno; | |
447 | return 0; | |
448 | } | |
449 | ||
0b3953a3 KZ |
450 | /** |
451 | * mnt_fs_get_root: | |
452 | * @fs: /proc/self/mountinfo entry | |
453 | * | |
454 | * Returns: root of the mount within the filesystem or NULL | |
455 | */ | |
456 | const char *mnt_fs_get_root(mnt_fs *fs) | |
457 | { | |
458 | assert(fs); | |
459 | return fs ? fs->root : NULL; | |
460 | } | |
461 | ||
efe73c3e KZ |
462 | /** |
463 | * mnt_fs_get_id: | |
464 | * @fs: /proc/self/mountinfo entry | |
465 | * | |
466 | * Returns: mount ID (unique identifier of the mount) or -1 if ID undefined | |
467 | * (for example if @fs is not mountinfo entry). | |
468 | */ | |
469 | int mnt_fs_get_id(mnt_fs *fs) | |
470 | { | |
471 | assert(fs); | |
472 | return fs ? fs->id : -1; | |
473 | } | |
474 | ||
475 | /** | |
476 | * mnt_fs_get_parent_id: | |
477 | * @fs: /proc/self/mountinfo entry | |
478 | * | |
479 | * Returns: parent mount ID or -1 if ID undefined (for example if @fs is not | |
480 | * mountinfo entry). | |
481 | */ | |
482 | int mnt_fs_get_parent_id(mnt_fs *fs) | |
483 | { | |
484 | assert(fs); | |
485 | return fs ? fs->parent : -1; | |
486 | } | |
487 | ||
488 | /** | |
489 | * mnt_fs_get_devno: | |
0b3953a3 | 490 | * @fs: /proc/self/mountinfo entry |
efe73c3e KZ |
491 | * |
492 | * Returns: value of st_dev for files on filesystem or 0 in case of error. | |
493 | */ | |
494 | dev_t mnt_fs_get_devno(mnt_fs *fs) | |
495 | { | |
496 | assert(fs); | |
497 | return fs ? fs->devno : 0; | |
498 | } | |
499 | ||
d115ee9b KZ |
500 | /** |
501 | * mnt_fs_get_option: | |
502 | * @fs: fstab/mtab/mountinfo entry pointer | |
503 | * @name: option name | |
504 | * @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL | |
505 | * @valsz: returns size of options value or 0 | |
506 | * | |
192c6aad | 507 | * Returns: 0 on success, 1 when not found the @name or -1 in case of error. |
d115ee9b KZ |
508 | */ |
509 | int mnt_fs_get_option(mnt_fs *fs, const char *name, | |
510 | char **value, size_t *valsz) | |
511 | { | |
512 | char *optstr = (char *) mnt_fs_get_optstr(fs); | |
513 | return optstr ? mnt_optstr_get_option(optstr, name, value, valsz) : 1; | |
514 | } | |
515 | ||
3fca8422 KZ |
516 | /** |
517 | * mnt_fs_match_target: | |
518 | * @fs: filesystem | |
519 | * @target: mountpoint path | |
520 | * @cache: tags/paths cache or NULL | |
521 | * | |
522 | * Possible are three attempts: | |
523 | * 1) compare @target with @fs->target | |
524 | * 2) realpath(@target) with @fs->target | |
525 | * 3) realpath(@target) with realpath(@fs->target). | |
526 | * | |
527 | * The 2nd and 3rd attempts are not performed when @cache is NULL. | |
528 | * | |
192c6aad | 529 | * Returns: 1 if @fs target is equal to @target else 0. |
3fca8422 KZ |
530 | */ |
531 | int mnt_fs_match_target(mnt_fs *fs, const char *target, mnt_cache *cache) | |
532 | { | |
533 | int rc = 0; | |
534 | ||
535 | if (!fs || !target || !fs->target) | |
536 | return 0; | |
537 | ||
538 | /* 1) native paths */ | |
539 | rc = !strcmp(target, fs->target); | |
540 | ||
541 | if (!rc && cache) { | |
542 | /* 2) - canonicalized and non-canonicalized */ | |
543 | char *cn = mnt_resolve_path(target, cache); | |
544 | rc = (cn && strcmp(cn, fs->target) == 0); | |
545 | ||
546 | /* 3) - canonicalized and canonicalized */ | |
547 | if (!rc && cn) { | |
548 | char *tcn = mnt_resolve_path(fs->target, cache); | |
549 | rc = (tcn && strcmp(cn, tcn) == 0); | |
550 | } | |
551 | } | |
552 | ||
553 | return rc; | |
554 | } | |
555 | ||
556 | /** | |
557 | * mnt_fs_match_source: | |
558 | * @fs: filesystem | |
559 | * @source: tag or path (device or so) | |
560 | * @cache: tags/paths cache or NULL | |
561 | * | |
562 | * Possible are four attempts: | |
563 | * 1) compare @source with @fs->source | |
564 | * 2) compare realpath(@source) with @fs->source | |
565 | * 3) compare realpath(@source) with realpath(@fs->source) | |
566 | * 4) compare realpath(@source) with evaluated tag from @fs->source | |
567 | * | |
568 | * The 2nd, 3rd and 4th attempts are not performed when @cache is NULL. The | |
569 | * 2nd and 3rd attempts are not performed if @fs->source is tag. | |
570 | * | |
192c6aad | 571 | * Returns: 1 if @fs source is equal to @source else 0. |
3fca8422 KZ |
572 | */ |
573 | int mnt_fs_match_source(mnt_fs *fs, const char *source, mnt_cache *cache) | |
574 | { | |
3fca8422 KZ |
575 | char *cn; |
576 | const char *src, *t, *v; | |
577 | ||
578 | if (!fs || !source || !fs->source) | |
579 | return 0; | |
580 | ||
581 | /* 1) native paths/tags */ | |
3ef87248 KZ |
582 | if (!strcmp(source, fs->source)) |
583 | return 1; | |
3fca8422 | 584 | |
3ef87248 KZ |
585 | if (!cache) |
586 | return 0; | |
3fca8422 KZ |
587 | if (fs->flags & (MNT_FS_NET | MNT_FS_PSEUDO)) |
588 | return 0; | |
589 | ||
590 | cn = mnt_resolve_spec(source, cache); | |
591 | if (!cn) | |
592 | return 0; | |
593 | ||
594 | /* 2) canonicalized and native */ | |
595 | src = mnt_fs_get_srcpath(fs); | |
3ef87248 KZ |
596 | if (src && !strcmp(cn, src)) |
597 | return 1; | |
3fca8422 KZ |
598 | |
599 | /* 3) canonicalized and canonicalized */ | |
3ef87248 | 600 | if (src) { |
3fca8422 | 601 | src = mnt_resolve_path(src, cache); |
3ef87248 KZ |
602 | if (src && !strcmp(cn, src)) |
603 | return 1; | |
3fca8422 | 604 | } |
3ef87248 KZ |
605 | if (src || mnt_fs_get_tag(fs, &t, &v)) |
606 | /* src path does not match and tag is not defined */ | |
3fca8422 KZ |
607 | return 0; |
608 | ||
609 | /* read @source's tags to the cache */ | |
ba7232a1 | 610 | if (mnt_cache_read_tags(cache, cn) < 0) { |
3fca8422 KZ |
611 | if (errno == EACCES) { |
612 | /* we don't have permissions to read TAGs from | |
613 | * @source, but can translate @fs tag to devname. | |
614 | * | |
615 | * (because libblkid uses udev symlinks and this is | |
616 | * accessible for non-root uses) | |
617 | */ | |
618 | char *x = mnt_resolve_tag(t, v, cache); | |
619 | if (x && !strcmp(x, cn)) | |
620 | return 1; | |
621 | } | |
622 | return 0; | |
623 | } | |
624 | ||
625 | /* 4) has the @source a tag that matches with tag from @fs ? */ | |
3ef87248 KZ |
626 | if (mnt_cache_device_has_tag(cache, cn, t, v)) |
627 | return 1; | |
3fca8422 | 628 | |
3ef87248 | 629 | return 0; |
3fca8422 KZ |
630 | } |
631 | ||
632 | /** | |
633 | * mnt_fs_match_fstype: | |
634 | * @fs: filesystem | |
635 | * @types: filesystem name or comma delimited list of filesystems | |
636 | * | |
637 | * For more details see mnt_match_fstype(). | |
638 | * | |
192c6aad | 639 | * Returns: 1 if @fs type is matching to @types else 0. The function returns |
3fca8422 KZ |
640 | * 0 when types is NULL. |
641 | */ | |
642 | int mnt_fs_match_fstype(mnt_fs *fs, const char *types) | |
643 | { | |
644 | return mnt_match_fstype(fs->fstype, types); | |
645 | } | |
646 | ||
647 | /** | |
648 | * mnt_fs_match_options: | |
649 | * @fs: filesystem | |
650 | * @options: comma delimited list of options (and nooptions) | |
651 | * | |
652 | * For more details see mnt_match_options(). | |
653 | * | |
192c6aad | 654 | * Returns: 1 if @fs type is matching to @options else 0. The function returns |
3fca8422 KZ |
655 | * 0 when types is NULL. |
656 | */ | |
657 | int mnt_fs_match_options(mnt_fs *fs, const char *options) | |
658 | { | |
659 | return mnt_match_options(fs->optstr, options); | |
660 | } | |
661 | ||
d115ee9b KZ |
662 | /* Unfortunately the classical Unix /etc/mtab and /etc/fstab |
663 | do not handle directory names containing spaces. | |
664 | Here we mangle them, replacing a space by \040. | |
665 | What do other Unices do? */ | |
666 | ||
667 | static unsigned char need_escaping[] = { ' ', '\t', '\n', '\\' }; | |
668 | ||
669 | static char *mangle(const char *s) | |
670 | { | |
671 | char *ss, *sp; | |
672 | int n; | |
673 | ||
674 | n = strlen(s); | |
675 | ss = sp = malloc(4*n+1); | |
676 | if (!sp) | |
677 | return NULL; | |
678 | while(1) { | |
679 | for (n = 0; n < sizeof(need_escaping); n++) { | |
680 | if (*s == need_escaping[n]) { | |
681 | *sp++ = '\\'; | |
682 | *sp++ = '0' + ((*s & 0300) >> 6); | |
683 | *sp++ = '0' + ((*s & 070) >> 3); | |
684 | *sp++ = '0' + (*s & 07); | |
685 | goto next; | |
686 | } | |
687 | } | |
688 | *sp++ = *s; | |
689 | if (*s == 0) | |
690 | break; | |
691 | next: | |
692 | s++; | |
693 | } | |
694 | return ss; | |
695 | } | |
696 | ||
697 | /** | |
698 | * mnt_fprintf_line: | |
699 | * @f: FILE | |
192c6aad | 700 | * @fmt: printf-like format string (see MNT_TAB_PRINTFMT) |
d115ee9b KZ |
701 | * @source: (spec) device name or tag=value |
702 | * @target: mountpoint | |
703 | * @fstype: filesystem type | |
704 | * @options: mount options | |
705 | * @freq: dump frequency in days | |
706 | * @passno: pass number on parallel fsck | |
707 | * | |
192c6aad KZ |
708 | * It's recommended to use this function rather than directly call fprintf() to |
709 | * write an entry to mtab/fstab. All data in these files has to be properly | |
710 | * formatted (for example space within paths/tags has to be escaped, see | |
711 | * fstab(5) for more details). | |
712 | * | |
713 | * Returns: return value from fprintf(). | |
d115ee9b KZ |
714 | */ |
715 | int mnt_fprintf_line( FILE *f, | |
716 | const char *fmt, | |
717 | const char *source, | |
718 | const char *target, | |
719 | const char *fstype, | |
720 | const char *options, | |
721 | int freq, | |
722 | int passno) | |
723 | { | |
724 | char *m1 = NULL, *m2 = NULL, *m3 = NULL, *m4 = NULL; | |
725 | int rc = -1; | |
726 | ||
727 | if (!f || !fmt || !source || !target || !fstype || !options) | |
728 | return -1; | |
729 | ||
730 | m1 = mangle(source); | |
731 | m2 = mangle(target); | |
732 | m3 = mangle(fstype); | |
733 | m4 = mangle(options); | |
734 | ||
735 | if (!m1 || !m2 || !m3 || !m4) | |
736 | goto done; | |
737 | ||
738 | rc = fprintf(f, fmt, m1, m2, m3, m4, freq, passno); | |
739 | done: | |
740 | free(m1); | |
741 | free(m2); | |
742 | free(m3); | |
743 | free(m4); | |
744 | ||
745 | return rc; | |
746 | } | |
747 | ||
748 | /** | |
749 | * mnt_fs_fprintf: | |
750 | * @fs: fstab/mtab/mountinfo entry | |
751 | * @f: FILE | |
192c6aad | 752 | * @fmt: printf-like format string (see MNT_TAB_PRINTFMT) |
d115ee9b | 753 | * |
192c6aad | 754 | * Returns: return value from fprintf(). |
d115ee9b KZ |
755 | */ |
756 | int mnt_fs_fprintf(mnt_fs *fs, FILE *f, const char *fmt) | |
757 | { | |
758 | assert(fs); | |
759 | assert(f); | |
760 | assert(fmt); | |
761 | ||
762 | if (!fs || !f) | |
763 | return -1; | |
764 | ||
765 | return mnt_fprintf_line(f, fmt, | |
766 | mnt_fs_get_source(fs), | |
767 | mnt_fs_get_target(fs), | |
768 | mnt_fs_get_fstype(fs), | |
769 | mnt_fs_get_optstr(fs), | |
770 | mnt_fs_get_freq(fs), | |
771 | mnt_fs_get_passno(fs)); | |
772 | } | |
773 | ||
774 | /** | |
775 | * mnt_fs_print_debug | |
776 | * @fs: fstab/mtab/mountinfo entry | |
777 | * @file: output | |
778 | * | |
192c6aad | 779 | * Returns: 0 on success or -1 in case of error. |
d115ee9b KZ |
780 | */ |
781 | int mnt_fs_print_debug(mnt_fs *fs, FILE *file) | |
782 | { | |
783 | if (!fs) | |
784 | return -1; | |
785 | fprintf(file, "------ fs: %p\n", fs); | |
786 | fprintf(file, "source: %s\n", mnt_fs_get_source(fs)); | |
787 | fprintf(file, "target: %s\n", mnt_fs_get_target(fs)); | |
788 | fprintf(file, "fstype: %s\n", mnt_fs_get_fstype(fs)); | |
789 | fprintf(file, "optstr: %s\n", mnt_fs_get_optstr(fs)); | |
790 | fprintf(file, "freq: %d\n", mnt_fs_get_freq(fs)); | |
791 | fprintf(file, "pass: %d\n", mnt_fs_get_passno(fs)); | |
efe73c3e KZ |
792 | fprintf(file, "id: %d\n", mnt_fs_get_id(fs)); |
793 | fprintf(file, "parent: %d\n", mnt_fs_get_parent_id(fs)); | |
794 | fprintf(file, "devno: %d:%d\n", major(mnt_fs_get_devno(fs)), | |
795 | minor(mnt_fs_get_devno(fs))); | |
796 | ||
d115ee9b KZ |
797 | |
798 | return 0; | |
799 | } |