Debian Codegraph @

src/test/test-fiemap.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/fiemap.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7
8 #include "argv-util.h"
9 #include "fd-util.h"
10 #include "hibernate-util.h"
11 #include "log.h"
12 #include "tests.h"
13
14 static int test_fiemap_one(const char *path) {
15 _cleanup_free_ struct fiemap *fiemap = NULL;
16 _cleanup_close_ int fd = -EBADF;
17 int r;
18
19 log_info("/* %s */", __func__);
20
21 fd = open(path, O_RDONLY | O_CLOEXEC | O_NONBLOCK);
22 if (fd < 0)
23 return log_error_errno(errno, "failed to open %s: %m", path);
24 r = read_fiemap(fd, &fiemap);
25 if (r == -EOPNOTSUPP)
26 exit(log_tests_skipped("Not supported"));
27 if (r < 0)
28 return log_error_errno(r, "Unable to read extent map for '%s': %m", path);
29 log_info("extent map information for %s:", path);
30 log_info("\t start: %" PRIu64, (uint64_t) fiemap->fm_start);
31 log_info("\t length: %" PRIu64, (uint64_t) fiemap->fm_length);
32 log_info("\t flags: %" PRIu32, fiemap->fm_flags);
33 log_info("\t number of mapped extents: %" PRIu32, fiemap->fm_mapped_extents);
34 log_info("\t extent count: %" PRIu32, fiemap->fm_extent_count);
35 if (fiemap->fm_extent_count > 0)
36 log_info("\t first extent location: %" PRIu64,
37 (uint64_t) (fiemap->fm_extents[0].fe_physical / page_size()));
38
39 return 0;
40 }
41
42 TEST_RET(fiemap) {
43 int r = 0;
44
45 assert_se(test_fiemap_one(saved_argv[0]) == 0);
46 for (int i = 1; i < saved_argc; i++) {
47 int k = test_fiemap_one(saved_argv[i]);
48 if (r == 0)
49 r = k;
50 }
51
52 return r;
53 }
54
55 static int intro(void) {
56 if (getuid() != 0)
57 log_warning("This program is unlikely to work for unprivileged users");
58
59 return EXIT_SUCCESS;
60 }
61
62 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);