]>
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 | |
68164f6c | 11 | * @short_description: struct libmnt_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> |
9ecdf48f | 20 | #include <stddef.h> |
d115ee9b KZ |
21 | |
22 | #include "nls.h" | |
23 | #include "mountP.h" | |
24 | ||
25 | /** | |
26 | * mnt_new_fs: | |
27 | * | |
68164f6c | 28 | * Returns: newly allocated struct libmnt_fs. |
d115ee9b | 29 | */ |
68164f6c | 30 | struct libmnt_fs *mnt_new_fs(void) |
d115ee9b | 31 | { |
68164f6c | 32 | struct libmnt_fs *fs = calloc(1, sizeof(*fs)); |
d115ee9b KZ |
33 | if (!fs) |
34 | return NULL; | |
35 | ||
309139c7 | 36 | /*DBG(FS, mnt_debug_h(fs, "alloc"));*/ |
d115ee9b KZ |
37 | INIT_LIST_HEAD(&fs->ents); |
38 | return fs; | |
39 | } | |
40 | ||
41 | /** | |
42 | * mnt_free_fs: | |
43 | * @fs: fs pointer | |
44 | * | |
45 | * Deallocates the fs. | |
46 | */ | |
68164f6c | 47 | void mnt_free_fs(struct libmnt_fs *fs) |
d115ee9b KZ |
48 | { |
49 | if (!fs) | |
50 | return; | |
51 | list_del(&fs->ents); | |
52 | ||
309139c7 KZ |
53 | /*DBG(FS, mnt_debug_h(fs, "free"));*/ |
54 | ||
d115ee9b | 55 | free(fs->source); |
dd369652 | 56 | free(fs->bindsrc); |
d115ee9b KZ |
57 | free(fs->tagname); |
58 | free(fs->tagval); | |
0b3953a3 | 59 | free(fs->root); |
d115ee9b KZ |
60 | free(fs->target); |
61 | free(fs->fstype); | |
f2b3a3a3 | 62 | free(fs->optstr); |
d115ee9b KZ |
63 | free(fs->vfs_optstr); |
64 | free(fs->fs_optstr); | |
76a06ca4 KZ |
65 | free(fs->user_optstr); |
66 | free(fs->attrs); | |
d115ee9b KZ |
67 | |
68 | free(fs); | |
69 | } | |
70 | ||
309139c7 KZ |
71 | /** |
72 | * mnt_reset_fs: | |
73 | * @fs: fs pointer | |
74 | * | |
75 | * Resets (zeroize) @fs. | |
76 | */ | |
77 | void mnt_reset_fs(struct libmnt_fs *fs) | |
78 | { | |
79 | if (fs) | |
80 | memset(fs, 0, sizeof(*fs)); | |
81 | } | |
82 | ||
83 | static inline int update_str(char **dest, const char *src) | |
9ecdf48f | 84 | { |
8b95253f KZ |
85 | size_t sz; |
86 | char *x; | |
87 | ||
88 | assert(dest); | |
9ecdf48f | 89 | |
8b95253f KZ |
90 | if (!src) { |
91 | free(*dest); | |
92 | *dest = NULL; | |
9ecdf48f | 93 | return 0; /* source (old) is empty */ |
8b95253f | 94 | } |
9ecdf48f | 95 | |
8b95253f KZ |
96 | sz = strlen(src) + 1; |
97 | x = realloc(*dest, sz); | |
98 | if (!x) | |
56be757f | 99 | return -ENOMEM; |
8b95253f KZ |
100 | *dest = x; |
101 | memcpy(*dest, src, sz); | |
9ecdf48f KZ |
102 | return 0; |
103 | } | |
104 | ||
8b95253f KZ |
105 | static inline int cpy_str_at_offset(void *new, const void *old, size_t offset) |
106 | { | |
107 | char **o = (char **) (old + offset); | |
108 | char **n = (char **) (new + offset); | |
109 | ||
309139c7 KZ |
110 | if (*n) |
111 | return 0; /* already set, not overwrite */ | |
112 | ||
113 | return update_str(n, *o); | |
8b95253f KZ |
114 | } |
115 | ||
9ecdf48f KZ |
116 | /** |
117 | * mnt_copy_fs: | |
309139c7 KZ |
118 | * @dest: destination FS |
119 | * @src: source FS | |
120 | * | |
121 | * If @dest is NULL, then a new FS is allocated, if any @dest field is already | |
122 | * set then the field is NOT overwrited. | |
9ecdf48f KZ |
123 | * |
124 | * This function does not copy userdata (se mnt_fs_set_userdata()). A new copy is | |
125 | * not linked with any existing mnt_tab. | |
126 | * | |
309139c7 | 127 | * Returns: @dest or NULL in case of error |
9ecdf48f | 128 | */ |
309139c7 KZ |
129 | struct libmnt_fs *mnt_copy_fs(struct libmnt_fs *dest, |
130 | const struct libmnt_fs *src) | |
9ecdf48f | 131 | { |
309139c7 | 132 | const struct libmnt_fs *org = dest; |
9ecdf48f | 133 | |
309139c7 KZ |
134 | if (!dest) { |
135 | dest = mnt_new_fs(); | |
136 | if (!dest) | |
137 | return NULL; | |
138 | } | |
9ecdf48f | 139 | |
309139c7 | 140 | /*DBG(FS, mnt_debug_h(dest, "copy from %p", src));*/ |
9ecdf48f | 141 | |
309139c7 KZ |
142 | dest->id = src->id; |
143 | dest->parent = src->parent; | |
144 | dest->devno = src->devno; | |
145 | ||
146 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, source))) | |
9ecdf48f | 147 | goto err; |
309139c7 | 148 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, tagname))) |
9ecdf48f | 149 | goto err; |
309139c7 | 150 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, tagval))) |
9ecdf48f | 151 | goto err; |
309139c7 | 152 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, root))) |
9ecdf48f | 153 | goto err; |
309139c7 | 154 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, target))) |
9ecdf48f | 155 | goto err; |
309139c7 | 156 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, fstype))) |
9ecdf48f | 157 | goto err; |
f2b3a3a3 KZ |
158 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, optstr))) |
159 | goto err; | |
309139c7 | 160 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, vfs_optstr))) |
9ecdf48f | 161 | goto err; |
309139c7 | 162 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, fs_optstr))) |
9ecdf48f | 163 | goto err; |
309139c7 | 164 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, user_optstr))) |
76a06ca4 | 165 | goto err; |
309139c7 | 166 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, attrs))) |
76a06ca4 | 167 | goto err; |
c4c66355 KZ |
168 | if (cpy_str_at_offset(dest, src, offsetof(struct libmnt_fs, bindsrc))) |
169 | goto err; | |
76a06ca4 | 170 | |
309139c7 KZ |
171 | dest->freq = src->freq; |
172 | dest->passno = src->passno; | |
173 | dest->flags = src->flags; | |
9ecdf48f | 174 | |
309139c7 | 175 | return dest; |
9ecdf48f | 176 | err: |
309139c7 KZ |
177 | if (!org) |
178 | mnt_free_fs(dest); | |
9ecdf48f KZ |
179 | return NULL; |
180 | } | |
181 | ||
0f32f1e2 | 182 | /* |
f84fa6f7 KZ |
183 | * This function copies all @fs description except information that does not |
184 | * belong to /etc/mtab (e.g. VFS and userspace mount options with MNT_NOMTAB | |
185 | * mask). | |
186 | * | |
187 | * Returns: copy of @fs. | |
188 | */ | |
68164f6c | 189 | struct libmnt_fs *mnt_copy_mtab_fs(const struct libmnt_fs *fs) |
f84fa6f7 | 190 | { |
68164f6c | 191 | struct libmnt_fs *n = mnt_new_fs(); |
f84fa6f7 KZ |
192 | |
193 | if (!n) | |
194 | return NULL; | |
195 | ||
68164f6c | 196 | if (cpy_str_at_offset(n, fs, offsetof(struct libmnt_fs, source))) |
f84fa6f7 | 197 | goto err; |
68164f6c | 198 | if (cpy_str_at_offset(n, fs, offsetof(struct libmnt_fs, target))) |
f84fa6f7 | 199 | goto err; |
68164f6c | 200 | if (cpy_str_at_offset(n, fs, offsetof(struct libmnt_fs, fstype))) |
f84fa6f7 KZ |
201 | goto err; |
202 | ||
203 | if (fs->vfs_optstr) { | |
204 | char *p = NULL; | |
205 | mnt_optstr_get_options(fs->vfs_optstr, &p, | |
206 | mnt_get_builtin_optmap(MNT_LINUX_MAP), | |
207 | MNT_NOMTAB); | |
208 | n->vfs_optstr = p; | |
209 | } | |
210 | ||
211 | if (fs->user_optstr) { | |
212 | char *p = NULL; | |
213 | mnt_optstr_get_options(fs->user_optstr, &p, | |
214 | mnt_get_builtin_optmap(MNT_USERSPACE_MAP), | |
215 | MNT_NOMTAB); | |
216 | n->user_optstr = p; | |
217 | } | |
218 | ||
68164f6c | 219 | if (cpy_str_at_offset(n, fs, offsetof(struct libmnt_fs, fs_optstr))) |
f84fa6f7 KZ |
220 | goto err; |
221 | ||
f2b3a3a3 KZ |
222 | /* we cannot copy original optstr, the new optstr has to be without |
223 | * non-mtab options -- so, let's generate a new string */ | |
224 | n->optstr = mnt_fs_strdup_options(n); | |
225 | ||
f84fa6f7 KZ |
226 | n->freq = fs->freq; |
227 | n->passno = fs->passno; | |
228 | n->flags = fs->flags; | |
229 | ||
230 | return n; | |
231 | err: | |
232 | mnt_free_fs(n); | |
233 | return NULL; | |
234 | ||
235 | } | |
236 | ||
26b4f9e4 KZ |
237 | /** |
238 | * mnt_fs_get_userdata: | |
68164f6c | 239 | * @fs: struct libmnt_file instance |
26b4f9e4 | 240 | * |
192c6aad | 241 | * Returns: private data set by mnt_fs_set_userdata() or NULL. |
26b4f9e4 | 242 | */ |
68164f6c | 243 | void *mnt_fs_get_userdata(struct libmnt_fs *fs) |
26b4f9e4 KZ |
244 | { |
245 | return fs ? fs->userdata : NULL; | |
246 | } | |
247 | ||
248 | /** | |
249 | * mnt_fs_set_userdata: | |
68164f6c | 250 | * @fs: struct libmnt_file instance |
0f32f1e2 | 251 | * @data: user data |
26b4f9e4 KZ |
252 | * |
253 | * The "userdata" are library independent data. | |
254 | * | |
56be757f | 255 | * Returns: 0 or negative number in case of error (if @fs is NULL). |
26b4f9e4 | 256 | */ |
68164f6c | 257 | int mnt_fs_set_userdata(struct libmnt_fs *fs, void *data) |
26b4f9e4 KZ |
258 | { |
259 | if (!fs) | |
56be757f | 260 | return -EINVAL; |
26b4f9e4 KZ |
261 | fs->userdata = data; |
262 | return 0; | |
263 | } | |
264 | ||
d115ee9b KZ |
265 | /** |
266 | * mnt_fs_get_srcpath: | |
68164f6c | 267 | * @fs: struct libmnt_file (fstab/mtab/mountinfo) fs |
d115ee9b KZ |
268 | * |
269 | * The mount "source path" is: | |
192c6aad KZ |
270 | * - a directory for 'bind' mounts (in fstab or mtab only) |
271 | * - a device name for standard mounts | |
d115ee9b KZ |
272 | * |
273 | * See also mnt_fs_get_tag() and mnt_fs_get_source(). | |
274 | * | |
192c6aad | 275 | * Returns: mount source path or NULL in case of error or when the path |
d115ee9b | 276 | * is not defined. |
d115ee9b | 277 | */ |
68164f6c | 278 | const char *mnt_fs_get_srcpath(struct libmnt_fs *fs) |
d115ee9b KZ |
279 | { |
280 | assert(fs); | |
281 | if (!fs) | |
282 | return NULL; | |
283 | ||
284 | /* fstab-like fs */ | |
285 | if (fs->tagname) | |
286 | return NULL; /* the source contains a "NAME=value" */ | |
287 | return fs->source; | |
288 | } | |
289 | ||
290 | /** | |
3d735589 | 291 | * mnt_fs_get_source: |
68164f6c | 292 | * @fs: struct libmnt_file (fstab/mtab/mountinfo) fs |
d115ee9b | 293 | * |
192c6aad | 294 | * Returns: mount source. Note that the source could be unparsed TAG |
d115ee9b KZ |
295 | * (LABEL/UUID). See also mnt_fs_get_srcpath() and mnt_fs_get_tag(). |
296 | */ | |
68164f6c | 297 | const char *mnt_fs_get_source(struct libmnt_fs *fs) |
d115ee9b KZ |
298 | { |
299 | return fs ? fs->source : NULL; | |
300 | } | |
301 | ||
68164f6c KZ |
302 | /* Used by parser struct libmnt_file ONLY (@source has to be allocated) */ |
303 | int __mnt_fs_set_source_ptr(struct libmnt_fs *fs, char *source) | |
d115ee9b | 304 | { |
b2d0b74d KZ |
305 | char *t = NULL, *v = NULL; |
306 | ||
d115ee9b KZ |
307 | assert(fs); |
308 | ||
be1a5180 KZ |
309 | if (source && !strcmp(source, "none")) |
310 | source = NULL; | |
d115ee9b | 311 | |
be1a5180 | 312 | if (source && strchr(source, '=')) { |
b2d0b74d | 313 | if (blkid_parse_tag_string(source, &t, &v) != 0) |
d115ee9b | 314 | return -1; |
d115ee9b KZ |
315 | } |
316 | ||
b2d0b74d KZ |
317 | if (fs->source != source) |
318 | free(fs->source); | |
319 | ||
320 | free(fs->tagname); | |
321 | free(fs->tagval); | |
322 | ||
d115ee9b | 323 | fs->source = source; |
b2d0b74d KZ |
324 | fs->tagname = t; |
325 | fs->tagval = v; | |
d115ee9b KZ |
326 | return 0; |
327 | } | |
328 | ||
329 | /** | |
330 | * mnt_fs_set_source: | |
331 | * @fs: fstab/mtab/mountinfo entry | |
332 | * @source: new source | |
333 | * | |
192c6aad KZ |
334 | * This function creates a private copy (strdup()) of @source. |
335 | * | |
56be757f | 336 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 337 | */ |
68164f6c | 338 | int mnt_fs_set_source(struct libmnt_fs *fs, const char *source) |
d115ee9b | 339 | { |
f5017242 | 340 | char *p = NULL; |
b2d0b74d | 341 | int rc; |
d115ee9b | 342 | |
f5017242 | 343 | if (!fs) |
56be757f | 344 | return -EINVAL; |
f5017242 KZ |
345 | if (source) { |
346 | p = strdup(source); | |
347 | if (!p) | |
348 | return -ENOMEM; | |
349 | } | |
d115ee9b | 350 | |
b2d0b74d KZ |
351 | rc = __mnt_fs_set_source_ptr(fs, p); |
352 | if (rc) | |
353 | free(p); | |
354 | return rc; | |
d115ee9b KZ |
355 | } |
356 | ||
357 | /** | |
358 | * mnt_fs_get_tag: | |
359 | * @fs: fs | |
360 | * @name: returns pointer to NAME string | |
361 | * @value: returns pointer to VALUE string | |
362 | * | |
363 | * "TAG" is NAME=VALUE (e.g. LABEL=foo) | |
364 | * | |
192c6aad KZ |
365 | * The TAG is the first column in the fstab file. The TAG or "srcpath" has to |
366 | * be always set for all entries. | |
d115ee9b KZ |
367 | * |
368 | * See also mnt_fs_get_source(). | |
369 | * | |
192c6aad KZ |
370 | * <informalexample> |
371 | * <programlisting> | |
d115ee9b | 372 | * char *src; |
68164f6c | 373 | * struct libmnt_fs *fs = mnt_table_find_target(tb, "/home", MNT_ITER_FORWARD); |
d115ee9b KZ |
374 | * |
375 | * if (!fs) | |
376 | * goto err; | |
377 | * | |
378 | * src = mnt_fs_get_srcpath(fs); | |
379 | * if (!src) { | |
380 | * char *tag, *val; | |
381 | * if (mnt_fs_get_tag(fs, &tag, &val) == 0) | |
382 | * printf("%s: %s\n", tag, val); // LABEL or UUID | |
383 | * } else | |
384 | * printf("device: %s\n", src); // device or bind path | |
192c6aad KZ |
385 | * </programlisting> |
386 | * </informalexample> | |
d115ee9b | 387 | * |
56be757f | 388 | * Returns: 0 on success or negative number in case that a TAG is not defined. |
d115ee9b | 389 | */ |
68164f6c | 390 | int mnt_fs_get_tag(struct libmnt_fs *fs, const char **name, const char **value) |
d115ee9b KZ |
391 | { |
392 | if (fs == NULL || !fs->tagname) | |
56be757f | 393 | return -EINVAL; |
d115ee9b KZ |
394 | if (name) |
395 | *name = fs->tagname; | |
396 | if (value) | |
397 | *value = fs->tagval; | |
398 | return 0; | |
399 | } | |
400 | ||
401 | /** | |
402 | * mnt_fs_get_target: | |
403 | * @fs: fstab/mtab/mountinfo entry pointer | |
404 | * | |
192c6aad | 405 | * Returns: pointer to mountpoint path or NULL |
d115ee9b | 406 | */ |
68164f6c | 407 | const char *mnt_fs_get_target(struct libmnt_fs *fs) |
d115ee9b KZ |
408 | { |
409 | assert(fs); | |
410 | return fs ? fs->target : NULL; | |
411 | } | |
412 | ||
413 | /** | |
414 | * mnt_fs_set_target: | |
415 | * @fs: fstab/mtab/mountinfo entry | |
416 | * @target: mountpoint | |
417 | * | |
192c6aad KZ |
418 | * This function creates a private copy (strdup()) of @target. |
419 | * | |
56be757f | 420 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 421 | */ |
68164f6c | 422 | int mnt_fs_set_target(struct libmnt_fs *fs, const char *target) |
d115ee9b | 423 | { |
f5017242 | 424 | char *p = NULL; |
d115ee9b KZ |
425 | |
426 | assert(fs); | |
427 | ||
f5017242 | 428 | if (!fs) |
56be757f | 429 | return -EINVAL; |
f5017242 KZ |
430 | if (target) { |
431 | p = strdup(target); | |
432 | if (!p) | |
433 | return -ENOMEM; | |
434 | } | |
d115ee9b KZ |
435 | free(fs->target); |
436 | fs->target = p; | |
437 | ||
438 | return 0; | |
439 | } | |
440 | ||
309139c7 KZ |
441 | int __mnt_fs_get_flags(struct libmnt_fs *fs) |
442 | { | |
443 | return fs ? fs->flags : 0; | |
444 | } | |
445 | ||
446 | int __mnt_fs_set_flags(struct libmnt_fs *fs, int flags) | |
447 | { | |
448 | if (fs) { | |
449 | fs->flags = flags; | |
450 | return 0; | |
451 | } | |
452 | return -EINVAL; | |
453 | } | |
454 | ||
455 | /** | |
456 | * mnt_fs_is_kernel: | |
457 | * @fs: filesystem | |
458 | * | |
459 | * Returns: 1 if the filesystem description is read from kernel e.g. /proc/mounts. | |
460 | */ | |
461 | int mnt_fs_is_kernel(struct libmnt_fs *fs) | |
462 | { | |
463 | return __mnt_fs_get_flags(fs) & MNT_FS_KERNEL; | |
464 | } | |
465 | ||
d115ee9b KZ |
466 | /** |
467 | * mnt_fs_get_fstype: | |
468 | * @fs: fstab/mtab/mountinfo entry pointer | |
469 | * | |
192c6aad | 470 | * Returns: pointer to filesystem type. |
d115ee9b | 471 | */ |
68164f6c | 472 | const char *mnt_fs_get_fstype(struct libmnt_fs *fs) |
d115ee9b KZ |
473 | { |
474 | assert(fs); | |
475 | return fs ? fs->fstype : NULL; | |
476 | } | |
477 | ||
68164f6c KZ |
478 | /* Used by struct libmnt_file parser only */ |
479 | int __mnt_fs_set_fstype_ptr(struct libmnt_fs *fs, char *fstype) | |
d115ee9b KZ |
480 | { |
481 | assert(fs); | |
482 | ||
b2d0b74d KZ |
483 | if (fstype != fs->fstype) |
484 | free(fs->fstype); | |
485 | ||
d115ee9b KZ |
486 | fs->fstype = fstype; |
487 | fs->flags &= ~MNT_FS_PSEUDO; | |
488 | fs->flags &= ~MNT_FS_NET; | |
489 | ||
490 | /* save info about pseudo filesystems */ | |
8485e709 KZ |
491 | if (fs->fstype) { |
492 | if (mnt_fstype_is_pseudofs(fs->fstype)) | |
493 | fs->flags |= MNT_FS_PSEUDO; | |
494 | else if (mnt_fstype_is_netfs(fs->fstype)) | |
495 | fs->flags |= MNT_FS_NET; | |
496 | else if (!strcmp(fs->fstype, "swap")) | |
497 | fs->flags |= MNT_FS_SWAP; | |
498 | } | |
d115ee9b KZ |
499 | return 0; |
500 | } | |
501 | ||
502 | /** | |
503 | * mnt_fs_set_fstype: | |
504 | * @fs: fstab/mtab/mountinfo entry | |
505 | * @fstype: filesystem type | |
506 | * | |
192c6aad KZ |
507 | * This function creates a private copy (strdup()) of @fstype. |
508 | * | |
56be757f | 509 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 510 | */ |
68164f6c | 511 | int mnt_fs_set_fstype(struct libmnt_fs *fs, const char *fstype) |
d115ee9b | 512 | { |
8485e709 | 513 | char *p = NULL; |
d115ee9b | 514 | |
8485e709 | 515 | if (!fs) |
56be757f | 516 | return -EINVAL; |
8485e709 KZ |
517 | if (fstype) { |
518 | p = strdup(fstype); | |
519 | if (!p) | |
520 | return -ENOMEM; | |
521 | } | |
f5017242 | 522 | return __mnt_fs_set_fstype_ptr(fs, p); |
d115ee9b KZ |
523 | } |
524 | ||
76a06ca4 KZ |
525 | /* |
526 | * Merges @vfs and @fs options strings into a new string. | |
527 | * This function cares about 'ro/rw' options. The 'ro' is | |
528 | * always used if @vfs or @fs is read-only. | |
529 | * For example: | |
530 | * | |
531 | * mnt_merge_optstr("rw,noexec", "ro,journal=update") | |
532 | * | |
533 | * returns: "ro,noexec,journal=update" | |
534 | * | |
535 | * mnt_merge_optstr("rw,noexec", "rw,journal=update") | |
536 | * | |
537 | * returns: "rw,noexec,journal=update" | |
538 | */ | |
539 | static char *merge_optstr(const char *vfs, const char *fs) | |
540 | { | |
541 | char *res, *p; | |
542 | size_t sz; | |
543 | int ro = 0, rw = 0; | |
544 | ||
545 | if (!vfs && !fs) | |
546 | return NULL; | |
547 | if (!vfs || !fs) | |
548 | return strdup(fs ? fs : vfs); | |
549 | if (!strcmp(vfs, fs)) | |
550 | return strdup(vfs); /* e.g. "aaa" and "aaa" */ | |
551 | ||
552 | /* leave space for leading "r[ow],", "," and trailing zero */ | |
553 | sz = strlen(vfs) + strlen(fs) + 5; | |
554 | res = malloc(sz); | |
555 | if (!res) | |
556 | return NULL; | |
557 | p = res + 3; /* make a room for rw/ro flag */ | |
558 | ||
559 | snprintf(p, sz - 3, "%s,%s", vfs, fs); | |
560 | ||
561 | /* remove 'rw' flags */ | |
562 | rw += !mnt_optstr_remove_option(&p, "rw"); /* from vfs */ | |
563 | rw += !mnt_optstr_remove_option(&p, "rw"); /* from fs */ | |
564 | ||
565 | /* remove 'ro' flags if necessary */ | |
566 | if (rw != 2) { | |
567 | ro += !mnt_optstr_remove_option(&p, "ro"); | |
568 | if (ro + rw < 2) | |
569 | ro += !mnt_optstr_remove_option(&p, "ro"); | |
570 | } | |
571 | ||
572 | if (!strlen(p)) | |
573 | memcpy(res, ro ? "ro" : "rw", 3); | |
574 | else | |
575 | memcpy(res, ro ? "ro," : "rw,", 3); | |
576 | return res; | |
577 | } | |
578 | ||
d115ee9b | 579 | /** |
76a06ca4 | 580 | * mnt_fs_strdup_options: |
d115ee9b KZ |
581 | * @fs: fstab/mtab/mountinfo entry pointer |
582 | * | |
76a06ca4 KZ |
583 | * Merges all mount options (VFS, FS and userspace) to the one options string |
584 | * and returns the result. This function does not modigy @fs. | |
585 | * | |
586 | * Returns: pointer to string (can be freed by free(3)) or NULL in case of error. | |
d115ee9b | 587 | */ |
68164f6c | 588 | char *mnt_fs_strdup_options(struct libmnt_fs *fs) |
d115ee9b | 589 | { |
76a06ca4 KZ |
590 | char *res; |
591 | ||
d115ee9b | 592 | assert(fs); |
76a06ca4 KZ |
593 | |
594 | errno = 0; | |
f2b3a3a3 KZ |
595 | |
596 | if (fs->optstr) | |
597 | return strdup(fs->optstr); | |
598 | ||
76a06ca4 | 599 | res = merge_optstr(fs->vfs_optstr, fs->fs_optstr); |
87a07a4c | 600 | if (!res && errno) |
76a06ca4 KZ |
601 | return NULL; |
602 | if (fs->user_optstr) { | |
603 | if (mnt_optstr_append_option(&res, fs->user_optstr, NULL)) { | |
604 | free(res); | |
605 | res = NULL; | |
606 | } | |
607 | } | |
608 | return res; | |
d115ee9b KZ |
609 | } |
610 | ||
f2b3a3a3 KZ |
611 | /** |
612 | * mnt_fs_get_options: | |
613 | * @fs: fstab/mtab/mountinfo entry pointer | |
614 | * | |
615 | * Returns: pointer to string or NULL in case of error. | |
616 | */ | |
617 | const char *mnt_fs_get_options(struct libmnt_fs *fs) | |
618 | { | |
619 | assert(fs); | |
620 | return fs ? fs->optstr : NULL; | |
621 | } | |
622 | ||
623 | ||
76a06ca4 KZ |
624 | /** |
625 | * mnt_fs_set_options: | |
626 | * @fs: fstab/mtab/mountinfo entry pointer | |
0f32f1e2 | 627 | * @optstr: options string |
76a06ca4 KZ |
628 | * |
629 | * Splits @optstr to VFS, FS and userspace mount options and update relevat | |
630 | * parts of @fs. | |
631 | * | |
632 | * Returns: 0 on success, or negative number icase of error. | |
633 | */ | |
68164f6c | 634 | int mnt_fs_set_options(struct libmnt_fs *fs, const char *optstr) |
d115ee9b | 635 | { |
f2b3a3a3 | 636 | char *v = NULL, *f = NULL, *u = NULL, *n = NULL; |
192c6aad | 637 | |
d115ee9b KZ |
638 | assert(fs); |
639 | ||
be1a5180 | 640 | if (!fs) |
56be757f | 641 | return -EINVAL; |
76a06ca4 KZ |
642 | if (optstr) { |
643 | int rc = mnt_split_optstr(optstr, &u, &v, &f, 0, 0); | |
56be757f KZ |
644 | if (rc) |
645 | return rc; | |
f2b3a3a3 KZ |
646 | n = strdup(optstr); |
647 | if (!n) | |
648 | return -ENOMEM; | |
3661b841 | 649 | } |
192c6aad | 650 | |
d115ee9b KZ |
651 | free(fs->fs_optstr); |
652 | free(fs->vfs_optstr); | |
76a06ca4 | 653 | free(fs->user_optstr); |
f2b3a3a3 | 654 | free(fs->optstr); |
d6fead1e | 655 | |
3661b841 KZ |
656 | fs->fs_optstr = f; |
657 | fs->vfs_optstr = v; | |
76a06ca4 | 658 | fs->user_optstr = u; |
f2b3a3a3 KZ |
659 | fs->optstr = n; |
660 | ||
192c6aad | 661 | return 0; |
d115ee9b KZ |
662 | } |
663 | ||
76a06ca4 KZ |
664 | /** |
665 | * mnt_fs_append_options: | |
666 | * @fs: fstab/mtab/mountinfo entry | |
667 | * @optstr: mount options | |
668 | * | |
669 | * Parses (splits) @optstr and appends results to VFS, FS and userspace lists | |
670 | * of options. | |
671 | * | |
672 | * If @optstr is NULL then @fs is not modified and 0 is returned. | |
673 | * | |
674 | * Returns: 0 on success or negative number in case of error. | |
675 | */ | |
68164f6c | 676 | int mnt_fs_append_options(struct libmnt_fs *fs, const char *optstr) |
b2d0b74d | 677 | { |
76a06ca4 | 678 | char *v = NULL, *f = NULL, *u = NULL; |
b2d0b74d KZ |
679 | int rc; |
680 | ||
681 | assert(fs); | |
682 | ||
76a06ca4 KZ |
683 | if (!fs) |
684 | return -EINVAL; | |
685 | if (!optstr) | |
686 | return 0; | |
687 | ||
688 | rc = mnt_split_optstr((char *) optstr, &u, &v, &f, 0, 0); | |
689 | if (!rc && v) | |
690 | rc = mnt_optstr_append_option(&fs->vfs_optstr, v, NULL); | |
691 | if (!rc && f) | |
f2b3a3a3 | 692 | rc = mnt_optstr_append_option(&fs->fs_optstr, f, NULL); |
76a06ca4 | 693 | if (!rc && u) |
f2b3a3a3 KZ |
694 | rc = mnt_optstr_append_option(&fs->user_optstr, u, NULL); |
695 | if (!rc) | |
696 | rc = mnt_optstr_append_option(&fs->optstr, optstr, NULL); | |
697 | ||
698 | free(v); | |
699 | free(f); | |
700 | free(u); | |
76a06ca4 | 701 | |
b2d0b74d KZ |
702 | return rc; |
703 | } | |
704 | ||
3661b841 | 705 | /** |
76a06ca4 KZ |
706 | * mnt_fs_prepend_options: |
707 | * @fs: fstab/mtab/mountinfo entry | |
708 | * @optstr: mount options | |
709 | * | |
710 | * Parses (splits) @optstr and prepands results to VFS, FS and userspace lists | |
711 | * of options. | |
712 | * | |
713 | * If @optstr is NULL then @fs is not modified and 0 is returned. | |
714 | * | |
715 | * Returns: 0 on success or negative number in case of error. | |
716 | */ | |
68164f6c | 717 | int mnt_fs_prepend_options(struct libmnt_fs *fs, const char *optstr) |
76a06ca4 KZ |
718 | { |
719 | char *v = NULL, *f = NULL, *u = NULL; | |
720 | int rc; | |
721 | ||
722 | assert(fs); | |
723 | ||
724 | if (!fs) | |
725 | return -EINVAL; | |
726 | if (!optstr) | |
727 | return 0; | |
728 | ||
729 | rc = mnt_split_optstr((char *) optstr, &u, &v, &f, 0, 0); | |
730 | if (!rc && v) | |
731 | rc = mnt_optstr_prepend_option(&fs->vfs_optstr, v, NULL); | |
732 | if (!rc && f) | |
733 | rc = mnt_optstr_prepend_option(&fs->fs_optstr, f, NULL); | |
734 | if (!rc && u) | |
735 | rc = mnt_optstr_prepend_option(&fs->user_optstr, u, NULL); | |
f2b3a3a3 KZ |
736 | if (!rc) |
737 | rc = mnt_optstr_prepend_option(&fs->optstr, optstr, NULL); | |
738 | ||
739 | free(v); | |
740 | free(f); | |
741 | free(u); | |
76a06ca4 KZ |
742 | |
743 | return rc; | |
744 | } | |
745 | ||
746 | /* | |
747 | * mnt_fs_get_fs_options: | |
748 | * @fs: fstab/mtab/mountinfo entry pointer | |
749 | * | |
750 | * Returns: pointer to superblock (fs-depend) mount option string or NULL. | |
751 | */ | |
68164f6c | 752 | const char *mnt_fs_get_fs_options(struct libmnt_fs *fs) |
76a06ca4 KZ |
753 | { |
754 | assert(fs); | |
755 | return fs ? fs->fs_optstr : NULL; | |
756 | } | |
757 | ||
76a06ca4 KZ |
758 | /** |
759 | * mnt_fs_get_vfs_options: | |
760 | * @fs: fstab/mtab entry pointer | |
761 | * | |
762 | * Returns: pointer to fs-independent (VFS) mount option string or NULL. | |
763 | */ | |
68164f6c | 764 | const char *mnt_fs_get_vfs_options(struct libmnt_fs *fs) |
76a06ca4 | 765 | { |
569f95b7 | 766 | assert(fs); |
76a06ca4 KZ |
767 | return fs ? fs->vfs_optstr : NULL; |
768 | } | |
769 | ||
76a06ca4 | 770 | /** |
68164f6c | 771 | * mnt_fs_get_user_options: |
76a06ca4 KZ |
772 | * @fs: fstab/mtab entry pointer |
773 | * | |
774 | * Returns: pointer to userspace mount option string or NULL. | |
775 | */ | |
68164f6c | 776 | const char *mnt_fs_get_user_options(struct libmnt_fs *fs) |
76a06ca4 KZ |
777 | { |
778 | assert(fs); | |
779 | return fs ? fs->user_optstr : NULL; | |
569f95b7 KZ |
780 | } |
781 | ||
d115ee9b | 782 | /** |
76a06ca4 KZ |
783 | * mnt_fs_get_attributes: |
784 | * @fs: fstab/mtab entry pointer | |
d115ee9b | 785 | * |
76a06ca4 | 786 | * Returns: pointer to attributes string or NULL. |
d115ee9b | 787 | */ |
68164f6c | 788 | const char *mnt_fs_get_attributes(struct libmnt_fs *fs) |
d115ee9b KZ |
789 | { |
790 | assert(fs); | |
76a06ca4 | 791 | return fs ? fs->attrs : NULL; |
d115ee9b KZ |
792 | } |
793 | ||
794 | /** | |
76a06ca4 KZ |
795 | * mnt_fs_set_attributes: |
796 | * @fs: fstab/mtab/mountinfo entry | |
797 | * @optstr: options string | |
d115ee9b | 798 | * |
76a06ca4 KZ |
799 | * Sets mount attributes. The attributes are mount(2) and mount(8) independent |
800 | * options, these options are not send to kernel and are not interpreted by | |
a362ae60 | 801 | * libmount. The attributes are stored in /run/mount/utab only. |
76a06ca4 KZ |
802 | * |
803 | * The atrtributes are managed by libmount in userspace only. It's possible | |
804 | * that information stored in userspace will not be available for libmount | |
805 | * after CLONE_FS unshare. Be carefull, and don't use attributes if possible. | |
806 | * | |
807 | * Returns: 0 on success or negative number in case of error. | |
d115ee9b | 808 | */ |
68164f6c | 809 | int mnt_fs_set_attributes(struct libmnt_fs *fs, const char *optstr) |
d115ee9b | 810 | { |
76a06ca4 KZ |
811 | char *p = NULL; |
812 | ||
813 | if (!fs) | |
814 | return -EINVAL; | |
815 | if (optstr) { | |
816 | p = strdup(optstr); | |
817 | if (!p) | |
818 | return -ENOMEM; | |
819 | } | |
820 | free(fs->attrs); | |
821 | fs->attrs = p; | |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
826 | /** | |
827 | * mnt_fs_append_attributes | |
828 | * @fs: fstab/mtab/mountinfo entry | |
829 | * @optstr: options string | |
830 | * | |
831 | * Appends mount attributes. (See mnt_fs_set_attributes()). | |
832 | * | |
833 | * Returns: 0 on success or negative number in case of error. | |
834 | */ | |
68164f6c | 835 | int mnt_fs_append_attributes(struct libmnt_fs *fs, const char *optstr) |
76a06ca4 KZ |
836 | { |
837 | if (!fs) | |
838 | return -EINVAL; | |
839 | if (!optstr) | |
840 | return 0; | |
841 | return mnt_optstr_append_option(&fs->attrs, optstr, NULL); | |
842 | } | |
843 | ||
844 | /** | |
845 | * mnt_fs_prepend_attributes | |
846 | * @fs: fstab/mtab/mountinfo entry | |
847 | * @optstr: options string | |
848 | * | |
849 | * Prepends mount attributes. (See mnt_fs_set_attributes()). | |
850 | * | |
851 | * Returns: 0 on success or negative number in case of error. | |
852 | */ | |
68164f6c | 853 | int mnt_fs_prepend_attributes(struct libmnt_fs *fs, const char *optstr) |
76a06ca4 KZ |
854 | { |
855 | if (!fs) | |
856 | return -EINVAL; | |
857 | if (!optstr) | |
858 | return 0; | |
859 | return mnt_optstr_prepend_option(&fs->attrs, optstr, NULL); | |
d115ee9b KZ |
860 | } |
861 | ||
862 | ||
863 | /** | |
864 | * mnt_fs_get_freq: | |
865 | * @fs: fstab/mtab/mountinfo entry pointer | |
866 | * | |
3d735589 | 867 | * Returns: dump frequency in days. |
d115ee9b | 868 | */ |
68164f6c | 869 | int mnt_fs_get_freq(struct libmnt_fs *fs) |
d115ee9b KZ |
870 | { |
871 | assert(fs); | |
872 | return fs ? fs->freq : 0; | |
873 | } | |
874 | ||
875 | /** | |
876 | * mnt_fs_set_freq: | |
efe73c3e | 877 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
878 | * @freq: dump frequency in days |
879 | * | |
56be757f | 880 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 881 | */ |
68164f6c | 882 | int mnt_fs_set_freq(struct libmnt_fs *fs, int freq) |
d115ee9b KZ |
883 | { |
884 | assert(fs); | |
d115ee9b | 885 | if (!fs) |
56be757f | 886 | return -EINVAL; |
d115ee9b KZ |
887 | fs->freq = freq; |
888 | return 0; | |
889 | } | |
890 | ||
891 | /** | |
892 | * mnt_fs_get_passno: | |
efe73c3e | 893 | * @fs: fstab/mtab entry pointer |
d115ee9b | 894 | * |
192c6aad | 895 | * Returns: "pass number on parallel fsck". |
d115ee9b | 896 | */ |
68164f6c | 897 | int mnt_fs_get_passno(struct libmnt_fs *fs) |
d115ee9b KZ |
898 | { |
899 | assert(fs); | |
900 | return fs ? fs->passno: 0; | |
901 | } | |
902 | ||
903 | /** | |
904 | * mnt_fs_set_passno: | |
efe73c3e | 905 | * @fs: fstab/mtab entry pointer |
d115ee9b KZ |
906 | * @passno: pass number |
907 | * | |
56be757f | 908 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 909 | */ |
68164f6c | 910 | int mnt_fs_set_passno(struct libmnt_fs *fs, int passno) |
d115ee9b KZ |
911 | { |
912 | assert(fs); | |
d115ee9b | 913 | if (!fs) |
56be757f | 914 | return -EINVAL; |
d115ee9b KZ |
915 | fs->passno = passno; |
916 | return 0; | |
917 | } | |
918 | ||
0b3953a3 KZ |
919 | /** |
920 | * mnt_fs_get_root: | |
921 | * @fs: /proc/self/mountinfo entry | |
922 | * | |
923 | * Returns: root of the mount within the filesystem or NULL | |
924 | */ | |
68164f6c | 925 | const char *mnt_fs_get_root(struct libmnt_fs *fs) |
0b3953a3 KZ |
926 | { |
927 | assert(fs); | |
928 | return fs ? fs->root : NULL; | |
929 | } | |
930 | ||
9ecdf48f KZ |
931 | /** |
932 | * mnt_fs_set_root: | |
933 | * @fs: mountinfo entry | |
934 | * @root: path | |
935 | * | |
56be757f | 936 | * Returns: 0 on success or negative number in case of error. |
9ecdf48f | 937 | */ |
68164f6c | 938 | int mnt_fs_set_root(struct libmnt_fs *fs, const char *root) |
9ecdf48f KZ |
939 | { |
940 | char *p = NULL; | |
941 | ||
942 | assert(fs); | |
943 | if (!fs) | |
56be757f | 944 | return -EINVAL; |
9ecdf48f KZ |
945 | if (root) { |
946 | p = strdup(root); | |
947 | if (!p) | |
56be757f | 948 | return -ENOMEM; |
9ecdf48f KZ |
949 | } |
950 | free(fs->root); | |
951 | fs->root = p; | |
952 | return 0; | |
953 | } | |
954 | ||
dd369652 KZ |
955 | /** |
956 | * mnt_fs_get_bindsrc: | |
a362ae60 | 957 | * @fs: /run/mount/utab entry |
dd369652 KZ |
958 | * |
959 | * Returns: full path that was used for mount(2) on MS_BIND | |
960 | */ | |
68164f6c | 961 | const char *mnt_fs_get_bindsrc(struct libmnt_fs *fs) |
dd369652 KZ |
962 | { |
963 | assert(fs); | |
964 | return fs ? fs->bindsrc : NULL; | |
965 | } | |
966 | ||
967 | /** | |
968 | * mnt_fs_set_bindsrc: | |
969 | * @fs: filesystem | |
970 | * @src: path | |
971 | * | |
972 | * Returns: 0 on success or negative number in case of error. | |
973 | */ | |
68164f6c | 974 | int mnt_fs_set_bindsrc(struct libmnt_fs *fs, const char *src) |
dd369652 KZ |
975 | { |
976 | char *p = NULL; | |
977 | ||
978 | assert(fs); | |
979 | if (!fs) | |
980 | return -EINVAL; | |
981 | if (src) { | |
982 | p = strdup(src); | |
983 | if (!p) | |
984 | return -ENOMEM; | |
985 | } | |
986 | free(fs->bindsrc); | |
987 | fs->bindsrc = p; | |
988 | return 0; | |
989 | } | |
990 | ||
efe73c3e KZ |
991 | /** |
992 | * mnt_fs_get_id: | |
993 | * @fs: /proc/self/mountinfo entry | |
994 | * | |
56be757f | 995 | * Returns: mount ID (unique identifier of the mount) or negative number in case of error. |
efe73c3e | 996 | */ |
68164f6c | 997 | int mnt_fs_get_id(struct libmnt_fs *fs) |
efe73c3e KZ |
998 | { |
999 | assert(fs); | |
56be757f | 1000 | return fs ? fs->id : -EINVAL; |
efe73c3e KZ |
1001 | } |
1002 | ||
1003 | /** | |
1004 | * mnt_fs_get_parent_id: | |
1005 | * @fs: /proc/self/mountinfo entry | |
1006 | * | |
56be757f | 1007 | * Returns: parent mount ID or negative number in case of error. |
efe73c3e | 1008 | */ |
68164f6c | 1009 | int mnt_fs_get_parent_id(struct libmnt_fs *fs) |
efe73c3e KZ |
1010 | { |
1011 | assert(fs); | |
56be757f | 1012 | return fs ? fs->parent : -EINVAL; |
efe73c3e KZ |
1013 | } |
1014 | ||
1015 | /** | |
1016 | * mnt_fs_get_devno: | |
0b3953a3 | 1017 | * @fs: /proc/self/mountinfo entry |
efe73c3e KZ |
1018 | * |
1019 | * Returns: value of st_dev for files on filesystem or 0 in case of error. | |
1020 | */ | |
68164f6c | 1021 | dev_t mnt_fs_get_devno(struct libmnt_fs *fs) |
efe73c3e KZ |
1022 | { |
1023 | assert(fs); | |
1024 | return fs ? fs->devno : 0; | |
1025 | } | |
1026 | ||
d115ee9b KZ |
1027 | /** |
1028 | * mnt_fs_get_option: | |
1029 | * @fs: fstab/mtab/mountinfo entry pointer | |
1030 | * @name: option name | |
1031 | * @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL | |
1032 | * @valsz: returns size of options value or 0 | |
1033 | * | |
56be757f | 1034 | * Returns: 0 on success, 1 when not found the @name or negative number in case of error. |
d115ee9b | 1035 | */ |
68164f6c | 1036 | int mnt_fs_get_option(struct libmnt_fs *fs, const char *name, |
d115ee9b KZ |
1037 | char **value, size_t *valsz) |
1038 | { | |
76a06ca4 KZ |
1039 | char rc = 1; |
1040 | ||
1041 | if (fs->fs_optstr) | |
1042 | rc = mnt_optstr_get_option(fs->fs_optstr, name, value, valsz); | |
1043 | if (rc == 1 && fs->vfs_optstr) | |
1044 | rc = mnt_optstr_get_option(fs->vfs_optstr, name, value, valsz); | |
1045 | if (rc == 1 && fs->user_optstr) | |
1046 | rc = mnt_optstr_get_option(fs->user_optstr, name, value, valsz); | |
1047 | return rc; | |
1048 | } | |
1049 | ||
1050 | /** | |
1051 | * mnt_fs_get_attribute: | |
1052 | * @fs: fstab/mtab/mountinfo entry pointer | |
1053 | * @name: option name | |
1054 | * @value: returns pointer to the begin of the value (e.g. name=VALUE) or NULL | |
1055 | * @valsz: returns size of options value or 0 | |
1056 | * | |
1057 | * Returns: 0 on success, 1 when not found the @name or negative number in case of error. | |
1058 | */ | |
68164f6c | 1059 | int mnt_fs_get_attribute(struct libmnt_fs *fs, const char *name, |
76a06ca4 KZ |
1060 | char **value, size_t *valsz) |
1061 | { | |
1062 | char rc = 1; | |
1063 | ||
1064 | if (fs->attrs) | |
1065 | rc = mnt_optstr_get_option(fs->attrs, name, value, valsz); | |
1066 | return rc; | |
d115ee9b KZ |
1067 | } |
1068 | ||
3fca8422 KZ |
1069 | /** |
1070 | * mnt_fs_match_target: | |
1071 | * @fs: filesystem | |
1072 | * @target: mountpoint path | |
1073 | * @cache: tags/paths cache or NULL | |
1074 | * | |
1075 | * Possible are three attempts: | |
1076 | * 1) compare @target with @fs->target | |
1077 | * 2) realpath(@target) with @fs->target | |
1078 | * 3) realpath(@target) with realpath(@fs->target). | |
1079 | * | |
1080 | * The 2nd and 3rd attempts are not performed when @cache is NULL. | |
1081 | * | |
192c6aad | 1082 | * Returns: 1 if @fs target is equal to @target else 0. |
3fca8422 | 1083 | */ |
68164f6c | 1084 | int mnt_fs_match_target(struct libmnt_fs *fs, const char *target, struct libmnt_cache *cache) |
3fca8422 KZ |
1085 | { |
1086 | int rc = 0; | |
1087 | ||
1088 | if (!fs || !target || !fs->target) | |
1089 | return 0; | |
1090 | ||
1091 | /* 1) native paths */ | |
1092 | rc = !strcmp(target, fs->target); | |
1093 | ||
1094 | if (!rc && cache) { | |
1095 | /* 2) - canonicalized and non-canonicalized */ | |
1096 | char *cn = mnt_resolve_path(target, cache); | |
1097 | rc = (cn && strcmp(cn, fs->target) == 0); | |
1098 | ||
1099 | /* 3) - canonicalized and canonicalized */ | |
1100 | if (!rc && cn) { | |
1101 | char *tcn = mnt_resolve_path(fs->target, cache); | |
1102 | rc = (tcn && strcmp(cn, tcn) == 0); | |
1103 | } | |
1104 | } | |
1105 | ||
1106 | return rc; | |
1107 | } | |
1108 | ||
1109 | /** | |
1110 | * mnt_fs_match_source: | |
1111 | * @fs: filesystem | |
f12aac6e | 1112 | * @source: tag or path (device or so) or NULL |
3fca8422 KZ |
1113 | * @cache: tags/paths cache or NULL |
1114 | * | |
1115 | * Possible are four attempts: | |
1116 | * 1) compare @source with @fs->source | |
1117 | * 2) compare realpath(@source) with @fs->source | |
1118 | * 3) compare realpath(@source) with realpath(@fs->source) | |
1119 | * 4) compare realpath(@source) with evaluated tag from @fs->source | |
1120 | * | |
1121 | * The 2nd, 3rd and 4th attempts are not performed when @cache is NULL. The | |
1122 | * 2nd and 3rd attempts are not performed if @fs->source is tag. | |
1123 | * | |
f12aac6e KZ |
1124 | * Note that valid source path is NULL; the libmount uses NULL instead of |
1125 | * "none". The "none" is used in /proc/{mounts,self/mountninfo} for pseudo | |
1126 | * filesystems. | |
1127 | * | |
192c6aad | 1128 | * Returns: 1 if @fs source is equal to @source else 0. |
3fca8422 | 1129 | */ |
68164f6c | 1130 | int mnt_fs_match_source(struct libmnt_fs *fs, const char *source, struct libmnt_cache *cache) |
3fca8422 | 1131 | { |
3fca8422 KZ |
1132 | char *cn; |
1133 | const char *src, *t, *v; | |
1134 | ||
f12aac6e KZ |
1135 | if (!fs) |
1136 | return 0; | |
1137 | ||
1138 | /* undefined source -- "none" in /proc */ | |
1139 | if (source == NULL && fs->source == NULL) | |
1140 | return 1; | |
1141 | ||
1142 | if (source == NULL || fs->source == NULL) | |
3fca8422 KZ |
1143 | return 0; |
1144 | ||
1145 | /* 1) native paths/tags */ | |
3ef87248 KZ |
1146 | if (!strcmp(source, fs->source)) |
1147 | return 1; | |
3fca8422 | 1148 | |
3ef87248 KZ |
1149 | if (!cache) |
1150 | return 0; | |
3fca8422 KZ |
1151 | if (fs->flags & (MNT_FS_NET | MNT_FS_PSEUDO)) |
1152 | return 0; | |
1153 | ||
1154 | cn = mnt_resolve_spec(source, cache); | |
1155 | if (!cn) | |
1156 | return 0; | |
1157 | ||
1158 | /* 2) canonicalized and native */ | |
1159 | src = mnt_fs_get_srcpath(fs); | |
3ef87248 KZ |
1160 | if (src && !strcmp(cn, src)) |
1161 | return 1; | |
3fca8422 KZ |
1162 | |
1163 | /* 3) canonicalized and canonicalized */ | |
3ef87248 | 1164 | if (src) { |
3fca8422 | 1165 | src = mnt_resolve_path(src, cache); |
3ef87248 KZ |
1166 | if (src && !strcmp(cn, src)) |
1167 | return 1; | |
3fca8422 | 1168 | } |
3ef87248 KZ |
1169 | if (src || mnt_fs_get_tag(fs, &t, &v)) |
1170 | /* src path does not match and tag is not defined */ | |
3fca8422 KZ |
1171 | return 0; |
1172 | ||
1173 | /* read @source's tags to the cache */ | |
ba7232a1 | 1174 | if (mnt_cache_read_tags(cache, cn) < 0) { |
3fca8422 KZ |
1175 | if (errno == EACCES) { |
1176 | /* we don't have permissions to read TAGs from | |
1177 | * @source, but can translate @fs tag to devname. | |
1178 | * | |
1179 | * (because libblkid uses udev symlinks and this is | |
1180 | * accessible for non-root uses) | |
1181 | */ | |
1182 | char *x = mnt_resolve_tag(t, v, cache); | |
1183 | if (x && !strcmp(x, cn)) | |
1184 | return 1; | |
1185 | } | |
1186 | return 0; | |
1187 | } | |
1188 | ||
1189 | /* 4) has the @source a tag that matches with tag from @fs ? */ | |
3ef87248 KZ |
1190 | if (mnt_cache_device_has_tag(cache, cn, t, v)) |
1191 | return 1; | |
3fca8422 | 1192 | |
3ef87248 | 1193 | return 0; |
3fca8422 KZ |
1194 | } |
1195 | ||
1196 | /** | |
1197 | * mnt_fs_match_fstype: | |
1198 | * @fs: filesystem | |
1199 | * @types: filesystem name or comma delimited list of filesystems | |
1200 | * | |
1201 | * For more details see mnt_match_fstype(). | |
1202 | * | |
192c6aad | 1203 | * Returns: 1 if @fs type is matching to @types else 0. The function returns |
3fca8422 KZ |
1204 | * 0 when types is NULL. |
1205 | */ | |
68164f6c | 1206 | int mnt_fs_match_fstype(struct libmnt_fs *fs, const char *types) |
3fca8422 KZ |
1207 | { |
1208 | return mnt_match_fstype(fs->fstype, types); | |
1209 | } | |
1210 | ||
1211 | /** | |
1212 | * mnt_fs_match_options: | |
1213 | * @fs: filesystem | |
1214 | * @options: comma delimited list of options (and nooptions) | |
1215 | * | |
1216 | * For more details see mnt_match_options(). | |
1217 | * | |
192c6aad | 1218 | * Returns: 1 if @fs type is matching to @options else 0. The function returns |
3fca8422 KZ |
1219 | * 0 when types is NULL. |
1220 | */ | |
68164f6c | 1221 | int mnt_fs_match_options(struct libmnt_fs *fs, const char *options) |
3fca8422 | 1222 | { |
d84508cf | 1223 | return mnt_match_options(mnt_fs_get_options(fs), options); |
3fca8422 KZ |
1224 | } |
1225 | ||
d115ee9b KZ |
1226 | /** |
1227 | * mnt_fs_print_debug | |
1228 | * @fs: fstab/mtab/mountinfo entry | |
1229 | * @file: output | |
1230 | * | |
56be757f | 1231 | * Returns: 0 on success or negative number in case of error. |
d115ee9b | 1232 | */ |
68164f6c | 1233 | int mnt_fs_print_debug(struct libmnt_fs *fs, FILE *file) |
d115ee9b KZ |
1234 | { |
1235 | if (!fs) | |
56be757f | 1236 | return -EINVAL; |
d115ee9b | 1237 | fprintf(file, "------ fs: %p\n", fs); |
77417bc0 KZ |
1238 | fprintf(file, "source: %s\n", mnt_fs_get_source(fs)); |
1239 | fprintf(file, "target: %s\n", mnt_fs_get_target(fs)); | |
1240 | fprintf(file, "fstype: %s\n", mnt_fs_get_fstype(fs)); | |
76a06ca4 | 1241 | |
f2b3a3a3 KZ |
1242 | if (mnt_fs_get_options(fs)) |
1243 | fprintf(file, "optstr: %s\n", mnt_fs_get_options(fs)); | |
76a06ca4 KZ |
1244 | if (mnt_fs_get_vfs_options(fs)) |
1245 | fprintf(file, "VFS-optstr: %s\n", mnt_fs_get_vfs_options(fs)); | |
1246 | if (mnt_fs_get_fs_options(fs)) | |
1247 | fprintf(file, "FS-opstr: %s\n", mnt_fs_get_fs_options(fs)); | |
68164f6c KZ |
1248 | if (mnt_fs_get_user_options(fs)) |
1249 | fprintf(file, "user-optstr: %s\n", mnt_fs_get_user_options(fs)); | |
76a06ca4 KZ |
1250 | if (mnt_fs_get_attributes(fs)) |
1251 | fprintf(file, "attributes: %s\n", mnt_fs_get_attributes(fs)); | |
dd369652 KZ |
1252 | |
1253 | if (mnt_fs_get_root(fs)) | |
77417bc0 | 1254 | fprintf(file, "root: %s\n", mnt_fs_get_root(fs)); |
dd369652 KZ |
1255 | if (mnt_fs_get_bindsrc(fs)) |
1256 | fprintf(file, "bindsrc: %s\n", mnt_fs_get_bindsrc(fs)); | |
9dd75aa6 | 1257 | if (mnt_fs_get_freq(fs)) |
77417bc0 | 1258 | fprintf(file, "freq: %d\n", mnt_fs_get_freq(fs)); |
9dd75aa6 | 1259 | if (mnt_fs_get_passno(fs)) |
77417bc0 | 1260 | fprintf(file, "pass: %d\n", mnt_fs_get_passno(fs)); |
9dd75aa6 | 1261 | if (mnt_fs_get_id(fs)) |
77417bc0 | 1262 | fprintf(file, "id: %d\n", mnt_fs_get_id(fs)); |
9dd75aa6 | 1263 | if (mnt_fs_get_parent_id(fs)) |
77417bc0 | 1264 | fprintf(file, "parent: %d\n", mnt_fs_get_parent_id(fs)); |
9dd75aa6 | 1265 | if (mnt_fs_get_devno(fs)) |
77417bc0 | 1266 | fprintf(file, "devno: %d:%d\n", major(mnt_fs_get_devno(fs)), |
9dd75aa6 | 1267 | minor(mnt_fs_get_devno(fs))); |
d115ee9b KZ |
1268 | return 0; |
1269 | } | |
8b95253f KZ |
1270 | |
1271 | /** | |
1272 | * mnt_free_mntent: | |
1273 | * @mnt: mount entry | |
1274 | * | |
1275 | * Deallocates "mntent.h" mount entry. | |
1276 | */ | |
1277 | void mnt_free_mntent(struct mntent *mnt) | |
1278 | { | |
1279 | if (mnt) { | |
1280 | free(mnt->mnt_fsname); | |
1281 | free(mnt->mnt_dir); | |
1282 | free(mnt->mnt_type); | |
1283 | free(mnt->mnt_opts); | |
1284 | free(mnt); | |
1285 | } | |
1286 | } | |
1287 | ||
1288 | /** | |
1289 | * mnt_fs_to_mntent: | |
1290 | * @fs: filesystem | |
1291 | * @mnt: mount description (as described in mntent.h) | |
1292 | * | |
1293 | * Copies information from @fs to struct mntent @mnt. If @mnt is already set | |
1294 | * then the struct mntent items are reallocated and updated. See also | |
1295 | * mnt_free_mntent(). | |
1296 | * | |
1297 | * Returns: 0 on success and negative number in case of error. | |
1298 | */ | |
68164f6c | 1299 | int mnt_fs_to_mntent(struct libmnt_fs *fs, struct mntent **mnt) |
8b95253f KZ |
1300 | { |
1301 | int rc; | |
1302 | struct mntent *m; | |
1303 | ||
1304 | if (!fs || !mnt) | |
1305 | return -EINVAL; | |
1306 | ||
1307 | m = *mnt; | |
1308 | if (!m) { | |
1309 | m = calloc(1, sizeof(*m)); | |
1310 | if (!m) | |
1311 | return -ENOMEM; | |
1312 | } | |
1313 | ||
309139c7 | 1314 | if ((rc = update_str(&m->mnt_fsname, mnt_fs_get_source(fs)))) |
8b95253f | 1315 | goto err; |
309139c7 | 1316 | if ((rc = update_str(&m->mnt_dir, mnt_fs_get_target(fs)))) |
8b95253f | 1317 | goto err; |
309139c7 | 1318 | if ((rc = update_str(&m->mnt_type, mnt_fs_get_fstype(fs)))) |
8b95253f | 1319 | goto err; |
76a06ca4 KZ |
1320 | |
1321 | errno = 0; | |
1322 | m->mnt_opts = mnt_fs_strdup_options(fs); | |
1323 | if (!m->mnt_opts && errno) { | |
1324 | rc = -errno; | |
8b95253f | 1325 | goto err; |
76a06ca4 KZ |
1326 | } |
1327 | ||
8b95253f KZ |
1328 | m->mnt_freq = mnt_fs_get_freq(fs); |
1329 | m->mnt_passno = mnt_fs_get_passno(fs); | |
1330 | ||
de511a88 KZ |
1331 | if (!m->mnt_fsname) { |
1332 | m->mnt_fsname = strdup("none"); | |
1333 | if (!m->mnt_fsname) | |
1334 | goto err; | |
1335 | } | |
8b95253f KZ |
1336 | *mnt = m; |
1337 | ||
1338 | return 0; | |
1339 | err: | |
1340 | if (m != *mnt) | |
1341 | mnt_free_mntent(m); | |
1342 | return rc; | |
1343 | } |