Created
April 15, 2020 05:05
-
-
Save jiangzc/93a19df3fb4e1e1184bc82234970638c to your computer and use it in GitHub Desktop.
an example using blkid API in C lang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compile hint: gcc xxx.c -lblkid | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <errno.h> | |
#include <blkid/blkid.h> | |
int list_ext4_partitions(void) | |
{ | |
int res; | |
blkid_cache cache; | |
res = blkid_get_cache(&cache, NULL); | |
if (res != 0) | |
perror("cannot get cache"); | |
blkid_dev_iterate it; | |
blkid_dev dev; | |
const char *name; | |
const char *tag_value = "ext4"; | |
it = blkid_dev_iterate_begin(cache); | |
res = 0; | |
while (true) | |
{ | |
res = blkid_dev_next(it, &dev); | |
if (res != 0) | |
break; | |
name = blkid_dev_devname(dev); | |
res = blkid_dev_has_tag(dev, "TYPE", tag_value); | |
if (res == 0) | |
continue; | |
printf("%s %s %d\n", name, tag_value, res); | |
} | |
printf("\n"); | |
return 0; | |
} | |
int get_size_sda1(void) | |
{ | |
blkid_probe pr; | |
const char *ptname; | |
const char *devname = "/dev/sda1"; | |
pr = blkid_new_probe_from_filename(devname); | |
if (!pr) | |
printf("%s: failed to open device", devname); | |
int res = blkid_probe_enable_partitions(pr, 1); | |
blkid_probe_set_partitions_flags(pr, BLKID_PARTS_ENTRY_DETAILS); | |
printf("%d\n", res); | |
res = blkid_do_fullprobe(pr); | |
printf("%d\n", res); | |
res = blkid_probe_lookup_value(pr, "PART_ENTRY_SIZE", &ptname, NULL); | |
printf("/dev/sda1 size: %s\n", ptname); | |
blkid_free_probe(pr); | |
} | |
int main() | |
{ | |
get_size_sda1(); | |
list_ext4_partitions(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment