File: statmount.c

package info (click to toggle)
util-linux 2.41-5
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 95,208 kB
  • sloc: ansic: 179,016; sh: 22,689; yacc: 1,284; makefile: 528; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (80 lines) | stat: -rw-r--r-- 1,737 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
 * Copyright (C) 2023 Karel Zak <kzak@redhat.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */
#include <stdlib.h>

#include "c.h"
#include "strutils.h"
#include "libmount.h"

#include "mount-api-utils.h"	/* fallback for old linux/mount.h */

int main(int argc, char *argv[])
{
        struct libmnt_fs *fs;
	struct libmnt_statmnt *sm;
	const char *mnt;
	uint64_t id = 0;

	if (argc != 2)
		errx(EXIT_FAILURE, "usage: %s <mountpoint | id>",
				program_invocation_short_name);

	mnt_init_debug(0);

	fs = mnt_new_fs();
	if (!fs)
		err(EXIT_FAILURE, "failed to allocate fs handler");

	/* define target (mountpoint) or mount ID */
	mnt = argv[1];
	if (isdigit_string(mnt)) {
		id = strtou64_or_err(mnt, "cannot ID");
		mnt_fs_set_uniq_id(fs, id);
	} else
		mnt_fs_set_target(fs, mnt);

	/*
	 * A) fetch all data without reference to libmnt_statmnt
	 */
	if (mnt_fs_fetch_statmount(fs, 0) != 0)
		warn("failed to read data by statmount()");
	mnt_fs_print_debug(fs, stdout);

	/* reset */
	id = mnt_fs_get_uniq_id(fs);
	mnt_reset_fs(fs);
	mnt_fs_set_uniq_id(fs, id);

	/*
	 * B) fetch data by on-demand way
	 */
	sm = mnt_new_statmnt();
	if (!sm)
		err(EXIT_FAILURE, "failed to allocate statmount handler");

	mnt_fs_refer_statmnt(fs, sm);

	/* read fs type, but nothing else */
	mnt_fs_get_fstype(fs);
	mnt_fs_print_debug(fs, stdout);

	/* read fs root, but nothing else */
        mnt_fs_get_root(fs);
        mnt_fs_print_debug(fs, stdout);

	/* read all mising data */
	mnt_fs_fetch_statmount(fs, 0);
	mnt_fs_print_debug(fs, stdout);

	/* see debug, this is no-op for statmount() */
	mnt_fs_get_fstype(fs);

	mnt_unref_fs(fs);
	mnt_unref_statmnt(sm);

	return EXIT_SUCCESS;
}