You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何利用Linux API在C语言中获取文件系统类型名称(C字符串)?

How to Get Filesystem Type Name via Linux API in 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, since statfs() 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) using strerror(errno) to give meaningful error messages.
  • String Storage: f_fstypename is a fixed-size array (usually 16 characters) defined as char 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.h header 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:52:32