1

I'm trying to use the libblkid library to analyze various filesystems on a block device from a C/C++ program, and it works well. However I'm confused with the blkid_superblocks_get_name function from this library.

The function assigns some name, but name of what? Also, what is the meaning of the first (input) argument idx? That kind of functions used to have the first argument of type blkid_probe.

1 Answer 1

2

Let's have a brief look onto the function:

int blkid_superblocks_get_name(size_t idx, const char **name, int *usage)
{
    if (idx < ARRAY_SIZE(idinfos)) {
        if (name)
            *name = idinfos[idx]->name;
        if (usage)
            *usage = idinfos[idx]->usage;
        return 0;
    }
    return -1;
}

name is a pointer to a char * pointer (thus a pointer to a »string«). The intended use would be something like

char *the_name = NULL;
int usage = 0;
size_t idx;

idx = somehow_get_a_valid_index();

blkid_superblock_get_name(idx, &the_name, &usage);

The function sets your pointer the_name to point to the name of something. Lets investigate further: What's idinfos and where does it come from?

Turns out idinfos is an array of struct blkid_idinfo *'s:

static const struct blkid_idinfo *idinfos[] =
{
    &aix_pt_idinfo,
    &sgi_pt_idinfo         
    &sun_pt_idinfo,     
    &dos_pt_idinfo,
    &gpt_pt_idinfo,
    &pmbr_pt_idinfo,        /* always after GPT */
    &mac_pt_idinfo,
    &ultrix_pt_idinfo,
    &bsd_pt_idinfo,
    &unixware_pt_idinfo,
    &solaris_x86_pt_idinfo,
    &minix_pt_idinfo
};

and looking further we see struct blkid_idinfo is

struct blkid_idinfo
{
    const char      *name;          /* fs, raid or partition table name */
    int             usage;          /* BLKID_USAGE_* flag */
    int             flags;          /* BLKID_IDINFO_* flags */
    int             minsz;          /* minimal device size */

    int             (*probefunc)(blkid_probe pr, const struct blkid_idmag *mag);
    struct blkid_idmag      magics[];
};

Doxygen comment says this describes a filesystem or RAID. Let's have a look at one of the members of idinfos:

const struct blkid_idinfo dos_pt_idinfo =
{
    .name           = "dos",
    .probefunc      = probe_dos_pt,
    .magics         =
    {
        /* DOS master boot sector:
         *
         *     0 | Code Area
         *   440 | Optional Disk signature
         *   446 | Partition table
         *   510 | 0x55
         *   511 | 0xAA
         */
        { .magic = "\x55\xAA", .len = 2, .sboff = 510 },
        { NULL }
    }
};

This obviously describes the DOS partitioning scheme (a.k.a. MBR partition table).

So idinfos describes several recognized partition formats (MBR, GPT, BSD disklabels, …). Looking back to blkid_superblocks_getname() we can see that it takes an idx, that is an offset into the idinfos array (as quoted above) and makes name point to the name-element of the corresponding struct blkid_idinfo (in the above example "dos").

libblkid/samples/partitions.c in the util-linux source code shows an example how to use this to iterate supported partition types.

Note: the source code of libblkid seems to be rather clearly arranged, try having a look at it to understand what it does, it's quite good to understand (grep is your friend ;) Also have a look at the other sample source code to understand how libblkid should be used.

3
  • Sorry to say, I don't see any blkid_superblocks_get_name calls in the libblkid/samples/partitions.c file. My util-linux version is 2.20.1 and the apt-get says it's the most recent one. However I've found how this function is called in the blkid source - it's used to implement option -k (list all filesystems the blkid understands).
    – HEKTO
    Apr 10, 2014 at 16:00
  • Oh, sorry, it's blkid_partitions_get_name there. On the other hand, the answer was a bit more explanation than mentioning the examples. Apr 10, 2014 at 20:00
  • Anyway, thank you the idea to grep sources of the util-linux. I understand now, what the function does.
    – HEKTO
    Apr 10, 2014 at 21:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .