]> git.ipfire.org Git - thirdparty/util-linux.git/blob - lib/swapprober.c
lscpu: fix variable shadowing
[thirdparty/util-linux.git] / lib / swapprober.c
1 /*
2 * No copyright is claimed. This code is in the public domain; do with
3 * it what you wish.
4 *
5 * Written by Karel Zak <kzak@redhat.com>
6 */
7 #include "c.h"
8 #include "nls.h"
9
10 #include "swapheader.h"
11 #include "swapprober.h"
12
13 blkid_probe get_swap_prober(const char *devname)
14 {
15 blkid_probe pr;
16 int rc;
17 const char *version = NULL;
18 char *swap_filter[] = { "swap", NULL };
19
20 pr = blkid_new_probe_from_filename(devname);
21 if (!pr) {
22 warn(_("%s: unable to probe device"), devname);
23 return NULL;
24 }
25
26 blkid_probe_enable_superblocks(pr, TRUE);
27 blkid_probe_set_superblocks_flags(pr,
28 BLKID_SUBLKS_LABEL | BLKID_SUBLKS_UUID |
29 BLKID_SUBLKS_VERSION);
30
31 blkid_probe_filter_superblocks_type(pr, BLKID_FLTR_ONLYIN, swap_filter);
32
33 rc = blkid_do_safeprobe(pr);
34 if (rc == -1)
35 warn(_("%s: unable to probe device"), devname);
36 else if (rc == -2)
37 warnx(_("%s: ambiguous probing result; use wipefs(8)"), devname);
38 else if (rc == 1)
39 warnx(_("%s: not a valid swap partition"), devname);
40
41 if (rc == 0) {
42 /* Only the SWAPSPACE2 is supported. */
43 if (blkid_probe_lookup_value(pr, "VERSION", &version, NULL) == 0
44 && version
45 && strcmp(version, stringify_value(SWAP_VERSION)) != 0)
46 warnx(_("%s: unsupported swap version '%s'"),
47 devname, version);
48 else
49 return pr;
50 }
51
52 blkid_free_probe(pr);
53 return NULL;
54 }