1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| #define _GNU_SOURCE // 启用 GNU 扩展 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/vfs.h> // 包含 statfs #include <sys/statvfs.h> // 包含 statvfs #include <string.h> #include <errno.h> #include <fcntl.h> // 包含 open
// 辅助函数:打印人类可读的大小 void print_human_readable(double bytes, const char* prefix) { const char *units[] = {"B", "KiB", "MiB", "GiB", "TiB"}; int unit_index = 0; double size_to_print = bytes; while (size_to_print >= 1024 && unit_index < 4) { size_to_print /= 1024; unit_index++; } printf("%s%.2f %s", prefix, size_to_print, units[unit_index]); }
// 使用 statfs 打印信息 void print_fs_info_statfs(const char* path, const struct statfs *sfs) { printf("=== Filesystem information for '%s' (via statfs) ===\n", path); printf(" Optimal I/O Block Size: %ld bytes\n", (long)sfs->f_bsize); printf(" Fragment Size: %ld bytes\n", (long)sfs->f_frsize); printf(" Total Fragments: %ld\n", (long)sfs->f_blocks); printf(" Free Fragments: %ld\n", (long)sfs->f_bfree); printf(" Available Fragments: %ld (for non-root users)\n", (long)sfs->f_bavail); printf(" Total Inodes: %ld\n", (long)sfs->f_files); printf(" Free Inodes: %ld\n", (long)sfs->f_ffree); printf(" Filesystem ID: %d:%d\n", (int)sfs->f_fsid.__val[0], (int)sfs->f_fsid.__val[1]); printf(" Maximum Filename Length: %ld\n", (long)sfs->f_namelen); printf(" Mount Flags: 0x%lx\n", (unsigned long)sfs->f_flags);
// 计算并打印人类可读的空间大小 double total_bytes = (double)sfs->f_blocks * sfs->f_frsize; double free_bytes = (double)sfs->f_bfree * sfs->f_frsize; double avail_bytes = (double)sfs->f_bavail * sfs->f_frsize;
printf(" Total Size: "); print_human_readable(total_bytes, ""); printf("\n"); printf(" Available Size: "); print_human_readable(avail_bytes, ""); printf("\n");
// 识别文件系统类型 (仅列举几个常见类型) switch (sfs->f_type) { case 0xEF53: // EXT2/3/4 printf(" Filesystem Type: ext2/ext3/ext4\n"); break; case 0x6969: // NFS printf(" Filesystem Type: NFS\n"); break; case 0x517B: // SMB/CIFS printf(" Filesystem Type: SMB/CIFS\n"); break; case 0x52654973: // ReiserFS printf(" Filesystem Type: ReiserFS\n"); break; case 0x5346544E: // NTFS printf(" Filesystem Type: NTFS\n"); break; case 0x4d44: // FAT printf(" Filesystem Type: FAT\n"); break; case 0x01021994: // TMPFS printf(" Filesystem Type: tmpfs\n"); break; case 0x9123683E: // Btrfs printf(" Filesystem Type: Btrfs\n"); break; default: printf(" Filesystem Type: Unknown (Magic: 0x%lx)\n", (unsigned long)sfs->f_type); break; } printf("\n"); }
// 使用 statvfs 打印信息 void print_fs_info_statvfs(const char* path, const struct statvfs *svfs) { printf("=== Filesystem information for '%s' (via statvfs) ===\n", path); printf(" Filesystem Block Size: %ld bytes\n", (long)svfs->f_bsize); printf(" Fragment Size: %ld bytes\n", (long)svfs->f_frsize); printf(" Total Fragments: %ld\n", (long)svfs->f_blocks); printf(" Free Fragments: %ld\n", (long)svfs->f_bfree); printf(" Available Fragments: %ld (for non-root users)\n", (long)svfs->f_bavail); printf(" Available Fragments (non-root): %ld\n", (long)svfs->f_bavail); // Same as f_bavail in most cases printf(" Total Inodes: %ld\n", (long)svfs->f_files); printf(" Free Inodes: %ld\n", (long)svfs->f_ffree); printf(" Available Inodes (non-root): %ld\n", (long)svfs->f_favail); printf(" Filesystem ID: %ld\n", (unsigned long)svfs->f_fsid); printf(" Maximum Filename Length: %ld\n", (long)svfs->f_namemax); printf(" Mount Flags: 0x%lx\n", svfs->f_flag);
// 计算并打印人类可读的空间大小 double total_bytes = (double)svfs->f_blocks * svfs->f_frsize; double free_bytes = (double)svfs->f_bfree * svfs->f_frsize; double avail_bytes = (double)svfs->f_bavail * svfs->f_frsize;
printf(" Total Size: "); print_human_readable(total_bytes, ""); printf("\n"); printf(" Available Size: "); print_human_readable(avail_bytes, ""); printf("\n"); printf("\n"); }
int main(int argc, char *argv[]) { struct statfs sfs; struct statvfs svfs; int fd;
printf("--- Demonstrating statfs vs statvfs ---\n");
// 1. 如果没有提供命令行参数,则查询根目录 "/" const char *path = "/"; if (argc > 1) { path = argv[1]; } printf("Querying path: '%s'\n\n", path);
// 2. 使用 statfs 查询 if (statfs(path, &sfs) == 0) { print_fs_info_statfs(path, &sfs); } else { printf("statfs failed for '%s': %s\n\n", path, strerror(errno)); }
// 3. 使用 statvfs 查询 if (statvfs(path, &svfs) == 0) { print_fs_info_statvfs(path, &svfs); } else { printf("statvfs failed for '%s': %s\n\n", path, strerror(errno)); }
// 4. 演示 fstatfs 和 fstatvfs (通过文件描述符) printf("=== Comparing fstatfs and fstatvfs ===\n"); fd = open(path, O_RDONLY); // 尝试打开路径 (对于目录是合法的) if (fd != -1) { printf("Opened '%s' as file descriptor %d\n", path, fd);
if (fstatfs(fd, &sfs) == 0) { printf("\n--- fstatfs via fd %d ---\n", fd); printf(" Total Fragments (fstatfs): %ld\n", (long)sfs.f_blocks); printf(" Free Fragments (fstatfs): %ld\n", (long)sfs.f_bfree); } else { printf("fstatfs failed: %s\n", strerror(errno)); }
if (fstatvfs(fd, &svfs) == 0) { printf("\n--- fstatvfs via fd %d ---\n", fd); printf(" Total Fragments (fstatvfs): %ld\n", (long)svfs.f_blocks); printf(" Free Fragments (fstatvfs): %ld\n", (long)svfs.f_bfree); } else { printf("fstatvfs failed: %s\n", strerror(errno)); }
close(fd); } else { // 如果打开失败(例如 path 是文件),尝试打开文件本身 fd = open(path, O_RDONLY); if (fd != -1) { printf("Opened file '%s' as file descriptor %d\n", path, fd); // ... (对文件进行 fstatfs/fstatvfs 调用) // 为简洁起见,此处省略对文件的详细查询,逻辑同上 close(fd); } else { printf("Could not open '%s' as file/dir for fstatfs/fstatvfs demo.\n", path); } }
// 5. 演示一个实际应用:检查磁盘空间是否充足 printf("\n=== Checking Disk Space ===\n"); const char *check_path = "/tmp"; // 检查 /tmp 分区 if (statvfs(check_path, &svfs) == 0) { double avail_bytes = (double)svfs.f_bavail * svfs.f_frsize; double required_bytes = 100 * 1024 * 1024; // 假设需要 100MB
printf("Checking if '%s' has at least %.2f MiB available...\n", check_path, required_bytes / (1024.0*1024.0));
if (avail_bytes >= required_bytes) { printf("OK: "); print_human_readable(avail_bytes, ""); printf(" available, %.2f MiB required.\n", required_bytes / (1024.0*1024.0)); } else { printf("WARNING: Only "); print_human_readable(avail_bytes, ""); printf(" available, %.2f MiB required. Insufficient space!\n", required_bytes / (1024.0*1024.0)); } } else { perror("statvfs for space check"); }
return 0; }
|