如何利用Linux API在C语言中获取文件系统类型名称(C字符串)?
The good news is you don’t have to parse terminal command outputs or hack around with external files—Linux provides a direct system call for this: statfs(). This is the same API tools like df or mount use under the hood, so it’s efficient and reliable.
Using statfs()
The statfs() function retrieves information about the filesystem containing a given path. The key members of the struct statfs it fills are:
f_fstypename: A null-terminated C string holding the filesystem type name (e.g.,"ext4","btrfs","tmpfs").f_type: A numeric "magic number" that uniquely identifies the filesystem (useful if you need to programmatically check types instead of relying on string names).
Here’s a complete, working example:
#include <stdio.h> #include <sys/statfs.h> #include <linux/magic.h> #include <string.h> #include <errno.h> int main(int argc, char *argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s <path>\n", argv[0]); return EXIT_FAILURE; } struct statfs fs_data; if (statfs(argv[1], &fs_data) == -1) { fprintf(stderr, "Failed to get filesystem info: %s\n", strerror(errno)); return EXIT_FAILURE; } // Print the filesystem name directly printf("Filesystem type name: %s\n", fs_data.f_fstypename); // Optional: Map magic number to known filesystem types printf("Magic number: 0x%lx\n", (unsigned long)fs_data.f_type); if (fs_data.f_type == EXT4_SUPER_MAGIC) { printf("Recognized as: ext4\n"); } else if (fs_data.f_type == BTRFS_SUPER_MAGIC) { printf("Recognized as: btrfs\n"); } else if (fs_data.f_type == TMPFS_MAGIC) { printf("Recognized as: tmpfs\n"); } else if (fs_data.f_type == XFS_SUPER_MAGIC) { printf("Recognized as: xfs\n"); } // Add more magic numbers as needed (check linux/magic.h for full list) return EXIT_SUCCESS; }
Key Notes:
- Compilation: You can compile this with
gcc -o fs_type fs_type.c—no special libraries needed, sincestatfs()is part of the standard Linux C library. - Error Handling: The example checks for invalid arguments and
statfs()failures (like permission issues or non-existent paths) usingstrerror(errno)to give meaningful error messages. - String Storage:
f_fstypenameis a fixed-size array (usually 16 characters) defined aschar f_fstypename[MFSNAMELEN]. If you need to store this string long-term, copy it to a dynamically allocated buffer or a larger array to avoid overflow. - Magic Numbers: The
linux/magic.hheader defines constants for most common filesystems. If you need to support less common ones, you can look up their magic numbers online or check the kernel source.
Alternative: Parsing /proc/mounts
If you need additional context (like mount options or the device path), you can parse /proc/mounts. This file lists all mounted filesystems in a space-separated format. However, this requires more work: you’ll need to read the file line by line, split each line into fields, and match the mount point to the path you’re interested in (resolving symlinks if necessary). For just the filesystem name, statfs() is far simpler.
内容的提问来源于stack exchange,提问作者Ilya Veselov

