rodolfo gomes dias
rodolfo gomes dias

Reputation: 173

Linux system call to discover the filesystem of a device

My question is the following: I need to get the filesystem of a device (a pendrive in my case) to use this information. My application is running in a Linux embedded system and I want to accept only pendrives with FAT and FAT32 filesystem to perform a file exportation. I searched the internet, but I didn't find what is the system call that I need. About the source code, my application is being written in C++.

I already used the struct statfs, however after a test I discover that the value of the field f_type is the same when I use a NTFS and a FAT32 formatted pendrive. The output of the test is the value 0x1021994.

I know that is possible to discover the filesystem, the "fdisk -l" command do the job, however I can't figure out in the fdisk code how.

Upvotes: 3

Views: 1966

Answers (4)

Stephen P
Stephen P

Reputation: 14820

You can use the statfs system call which includes uint32_t f_type; /* type of filesystem */ in the returned struct statfs


Note that, as JoshuaRLi points out, statfs is now deprecated in favor of statvfs — and struct statvfs does not include an f_type field.

Upvotes: 3

jilles
jilles

Reputation: 11252

Specify the filesystem type when mounting (do not use the default -t auto). If you need to support multiple types, consider trying them all.

Upvotes: 0

Oskari Timperi
Oskari Timperi

Reputation: 311

Maybe you could parse /proc/mounts file?

Upvotes: 0

paulsm4
paulsm4

Reputation: 121869

The easiest way is:

1) Run the "mount" command to list one or more filesystems

2) Parse out the information you need

3) You can invoke "mount" from the "popen()" API

PS:

There's also a "mount()" API, which could eliminate steps 2) and 3), if you prefer.

Linux being Linux, there's also probably at least half a dozen other viable alternatives - your choice :)

Upvotes: 0

Related Questions