Debian Codegraph @

libmount/samples/statmount.c

raw
1 /*
2 * Copyright (C) 2023 Karel Zak <[email protected]>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7 #include <stdlib.h>
8
9 #include "c.h"
10 #include "strutils.h"
11 #include "libmount.h"
12
13 #include "mountutils.h" /* fallback for old linux/mount.h */
14
15 int main(int argc, char *argv[])
16 {
17 struct libmnt_fs *fs;
18 struct libmnt_statmnt *sm;
19 const char *mnt;
20 uint64_t id = 0;
21
22 if (argc != 2)
23 errx(EXIT_FAILURE, "usage: %s <mountpoint | id>",
24 program_invocation_short_name);
25
26 mnt_init_debug(0);
27
28 fs = mnt_new_fs();
29 if (!fs)
30 err(EXIT_FAILURE, "failed to allocate fs handler");
31
32 /* define target (mountpoint) or mount ID */
33 mnt = argv[1];
34 if (isdigit_string(mnt)) {
35 id = strtou64_or_err(mnt, "cannot ID");
36 mnt_fs_set_uniq_id(fs, id);
37 } else
38 mnt_fs_set_target(fs, mnt);
39
40 /*
41 * A) fetch all data without reference to libmnt_statmnt
42 */
43 if (mnt_fs_fetch_statmount(fs, 0) != 0)
44 warn("failed to read data by statmount()");
45 mnt_fs_print_debug(fs, stdout);
46
47 /* reset */
48 id = mnt_fs_get_uniq_id(fs);
49 mnt_reset_fs(fs);
50 mnt_fs_set_uniq_id(fs, id);
51
52 /*
53 * B) fetch data by on-demand way
54 */
55 sm = mnt_new_statmnt();
56 if (!sm)
57 err(EXIT_FAILURE, "failed to allocate statmount handler");
58
59 mnt_fs_refer_statmnt(fs, sm);
60
61 /* read fs type, but nothing else */
62 mnt_fs_get_fstype(fs);
63 mnt_fs_print_debug(fs, stdout);
64
65 /* read fs root, but nothing else */
66 mnt_fs_get_root(fs);
67 mnt_fs_print_debug(fs, stdout);
68
69 /* read all missing data */
70 mnt_fs_fetch_statmount(fs, 0);
71 mnt_fs_print_debug(fs, stdout);
72
73 /* see debug, this is no-op for statmount() */
74 mnt_fs_get_fstype(fs);
75
76 mnt_unref_fs(fs);
77 mnt_unref_statmnt(sm);
78
79 return EXIT_SUCCESS;
80 }