0

I need to check filesystem type on a thumb drive in my C++ application. It must be done before mounting a new partition. I also prefer not to call system() function. I tried to use the following test code:

#include <blkid/blkid.h>
#include <stdio.h>
int main()
{
    blkid_probe pr;
    const char *ptname;
    const char* devname = "/dev/sdb1";
    pr = blkid_new_probe_from_filename(devname);
    if (!pr)
        printf("faild to open device\n");
    else
    {
        blkid_probe_enable_partitions(pr, true);
        blkid_do_fullprobe(pr);

        blkid_probe_lookup_value(pr, "PTTYPE", &ptname, NULL);
        printf("%s partition type detected\n", ptname);

        blkid_free_probe(pr);   
    }
}

When I plug in thumb drive with ntfs this piece of code shows that my partions is dos. When I plug in thumb drive with fat or ext4 the code returns strange string but the same for these two filesystems: AWAVI��AUATL�%� .

What causes these strange outputs? Maybe there is a better way to check a filesystem? Thank you in advance for any help.

user6758
  • 23
  • 6

1 Answers1

0

If you are interested in what filesystem is on sdb1 you should use USAGE to first check whether it is a filesystem and then get filesystem type with TYPE.

You'll need to set these flags to enable filesystem lookup:

blkid_probe_set_superblocks_flags(probe, BLKID_SUBLKS_USAGE | BLKID_SUBLKS_TYPE |
                                         BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM);

About your result: you need to check return value of blkid_probe_lookup_value (or use blkid_probe_has_value first), you are not initializing ptname to NULL so you'll get garbage if the lookup fails. And the lookup will fail, because partitions don't have PTTYPE. (I'm not sure why it worked for you with NTFS, in my case both NTFS and ext4 partition don't have PTTYPE.)

Version with usage and type could look like this

#include <blkid/blkid.h>
#include <stdio.h>
#include <string.h>

int main()
{
    blkid_probe pr;
    const char *value = NULL;
    const char* devname = "/dev/sdb1";
    int ret = 0;
    pr = blkid_new_probe_from_filename(devname);
    if (!pr)
        printf("faild to open device\n");
    else
    {
        blkid_probe_enable_partitions(pr, 1);
        blkid_probe_set_superblocks_flags(pr, BLKID_SUBLKS_USAGE | BLKID_SUBLKS_TYPE |
                                              BLKID_SUBLKS_MAGIC | BLKID_SUBLKS_BADCSUM);
        blkid_do_fullprobe(pr);

        ret = blkid_probe_lookup_value(pr, "USAGE", &value, NULL);
        if (ret != 0) {
                printf("lookup failed\n");
                return 1;
        } else
                printf("usage: %s\n", value);

        if (strcmp(value, "filesystem") != 0) {
                printf("not filesystem\n");
                return 1;
        }

        ret = blkid_probe_lookup_value(pr, "TYPE", &value, NULL);
        if (ret != 0) {
                printf("lookup failed\n");
                return 1;
        } else
                printf("type: %s\n", value);

        blkid_free_probe(pr);
    }
}
Vojtech Trefny
  • 16,922
  • 6
  • 24
  • 48