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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
| #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <string.h> #include <dirent.h>
typedef struct { int fd; char path[256]; int is_dir; } file_handle_t;
#define MAX_HANDLES 64 static file_handle_t handles[MAX_HANDLES]; static int handle_count = 0;
int register_handle(int fd, const char *path, int is_dir) { if (handle_count >= MAX_HANDLES) { return -1; } handles[handle_count].fd = fd; strncpy(handles[handle_count].path, path, sizeof(handles[handle_count].path) - 1); handles[handle_count].path[sizeof(handles[handle_count].path) - 1] = '\0'; handles[handle_count].is_dir = is_dir; return handle_count++; }
int unregister_handle(int fd) { for (int i = 0; i < handle_count; i++) { if (handles[i].fd == fd) { // 将后续句柄前移 for (int j = i; j < handle_count - 1; j++) { handles[j] = handles[j + 1]; } handle_count--; return 0; } } return -1; }
void list_handles() { printf("=== 当前打开的文件句柄 ===\n"); if (handle_count == 0) { printf("没有打开的文件句柄\n"); return; } printf("%-4s %-6s %-8s %s\n", "ID", "FD", "类型", "路径"); printf("%-4s %-6s %-8s %s\n", "--", "--", "--", "--"); for (int i = 0; i < handle_count; i++) { printf("%-4d %-6d %-8s %s\n", i, handles[i].fd, handles[i].is_dir ? "目录" : "文件", handles[i].path); } printf("总计: %d 个句柄\n", handle_count); }
void interactive_file_manager() { int choice; char path[256]; int dirfd; int flags; mode_t mode; while (1) { printf("\n=== openat 文件管理工具 ===\n"); printf("1. 相对打开文件\n"); printf("2. 打开目录\n"); printf("3. 创建文件\n"); printf("4. 关闭文件句柄\n"); printf("5. 列出所有句柄\n"); printf("6. 读取文件内容\n"); printf("7. 写入文件内容\n"); printf("8. 显示文件状态\n"); printf("0. 退出\n"); printf("请选择操作: "); if (scanf("%d", &choice) != 1) { printf("输入无效\n"); while (getchar() != '\n'); // 清空输入缓冲区 continue; } switch (choice) { case 1: printf("相对打开文件:\n"); printf("输入目录句柄 ID (或 -1 表示 AT_FDCWD): "); int dir_id; if (scanf("%d", &dir_id) == 1) { if (dir_id == -1) { dirfd = AT_FDCWD; } else if (dir_id >= 0 && dir_id < handle_count && handles[dir_id].is_dir) { dirfd = handles[dir_id].fd; } else { printf("无效的目录句柄 ID\n"); break; } printf("输入文件路径: "); scanf("%255s", path); printf("输入打开标志 (O_RDONLY=0, O_WRONLY=1, O_RDWR=2): "); int flag_choice; if (scanf("%d", &flag_choice) == 1) { switch (flag_choice) { case 0: flags = O_RDONLY; break; case 1: flags = O_WRONLY; break; case 2: flags = O_RDWR; break; default: flags = O_RDONLY; break; } int fd = openat(dirfd, path, flags); if (fd != -1) { char full_path[512]; if (dir_id == -1) { snprintf(full_path, sizeof(full_path), "%s", path); } else { snprintf(full_path, sizeof(full_path), "%s/%s", handles[dir_id].path, path); } int id = register_handle(fd, full_path, 0); if (id != -1) { printf("✓ 成功打开文件 (ID: %d, FD: %d)\n", id, fd); } else { printf("✗ 注册句柄失败\n"); close(fd); } } else { printf("✗ 打开文件失败: %s\n", strerror(errno)); } } } break; case 2: printf("打开目录:\n"); printf("输入目录路径: "); scanf("%255s", path); int dir_fd = open(path, O_RDONLY); if (dir_fd != -1) { int id = register_handle(dir_fd, path, 1); if (id != -1) { printf("✓ 成功打开目录 (ID: %d, FD: %d)\n", id, dir_fd); } else { printf("✗ 注册句柄失败\n"); close(dir_fd); } } else { printf("✗ 打开目录失败: %s\n", strerror(errno)); } break; case 3: printf("创建文件:\n"); printf("输入目录句柄 ID (或 -1 表示 AT_FDCWD): "); if (scanf("%d", &dir_id) == 1) { if (dir_id == -1) { dirfd = AT_FDCWD; } else if (dir_id >= 0 && dir_id < handle_count && handles[dir_id].is_dir) { dirfd = handles[dir_id].fd; } else { printf("无效的目录句柄 ID\n"); break; } printf("输入文件路径: "); scanf("%255s", path); flags = O_CREAT | O_WRONLY | O_TRUNC; mode = 0644; int fd = openat(dirfd, path, flags, mode); if (fd != -1) { char full_path[512]; if (dir_id == -1) { snprintf(full_path, sizeof(full_path), "%s", path); } else { snprintf(full_path, sizeof(full_path), "%s/%s", handles[dir_id].path, path); } int id = register_handle(fd, full_path, 0); if (id != -1) { printf("✓ 成功创建文件 (ID: %d, FD: %d)\n", id, fd); } else { printf("✗ 注册句柄失败\n"); close(fd); } } else { printf("✗ 创建文件失败: %s\n", strerror(errno)); } } break; case 4: { if (handle_count == 0) { printf("没有打开的句柄\n"); break; } list_handles(); printf("输入要关闭的句柄 ID: "); int handle_id; if (scanf("%d", &handle_id) == 1) { if (handle_id >= 0 && handle_id < handle_count) { printf("关闭句柄: %s (FD: %d)\n", handles[handle_id].path, handles[handle_id].fd); close(handles[handle_id].fd); unregister_handle(handles[handle_id].fd); printf("✓ 成功关闭句柄\n"); } else { printf("无效的句柄 ID\n"); } } break; } case 5: list_handles(); break; case 6: { if (handle_count == 0) { printf("没有打开的句柄\n"); break; } list_handles(); printf("输入要读取的文件句柄 ID: "); int handle_id; if (scanf("%d", &handle_id) == 1) { if (handle_id >= 0 && handle_id < handle_count && !handles[handle_id].is_dir) { char buffer[256]; ssize_t bytes_read = read(handles[handle_id].fd, buffer, sizeof(buffer) - 1); if (bytes_read > 0) { buffer[bytes_read] = '\0'; printf("文件内容:\n%s\n", buffer); } else if (bytes_read == 0) { printf("文件为空\n"); } else { printf("读取文件失败: %s\n", strerror(errno)); } } else { printf("无效的文件句柄 ID\n"); } } break; } case 7: { if (handle_count == 0) { printf("没有打开的句柄\n"); break; } list_handles(); printf("输入要写入的文件句柄 ID: "); int handle_id; if (scanf("%d", &handle_id) == 1) { if (handle_id >= 0 && handle_id < handle_count && !handles[handle_id].is_dir) { printf("输入要写入的内容: "); char content[256]; scanf("%255s", content); ssize_t bytes_written = write(handles[handle_id].fd, content, strlen(content)); if (bytes_written > 0) { printf("✓ 成功写入 %zd 字节\n", bytes_written); } else { printf("✗ 写入文件失败: %s\n", strerror(errno)); } } else { printf("无效的文件句柄 ID\n"); } } break; } case 8: { if (handle_count == 0) { printf("没有打开的句柄\n"); break; } list_handles(); printf("输入要查看状态的句柄 ID: "); int handle_id; if (scanf("%d", &handle_id) == 1) { if (handle_id >= 0 && handle_id < handle_count) { struct stat st; if (fstat(handles[handle_id].fd, &st) == 0) { printf("文件状态:\n"); printf(" 路径: %s\n", handles[handle_id].path); printf(" 大小: %ld 字节\n", (long)st.st_size); printf(" 权限: %o\n", st.st_mode & 0777); printf(" inode: %ld\n", (long)st.st_ino); printf(" 链接数: %ld\n", (long)st.st_nlink); printf(" 所有者: %d\n", st.st_uid); printf(" 组: %d\n", st.st_gid); } else { printf("获取文件状态失败: %s\n", strerror(errno)); } } else { printf("无效的句柄 ID\n"); } } break; } case 0: printf("退出文件管理工具\n"); // 清理所有打开的句柄 for (int i = 0; i < handle_count; i++) { close(handles[i].fd); } handle_count = 0; return; default: printf("无效选择\n"); break; } } }
void demonstrate_openat_features() { printf("=== openat 特性演示 ===\n"); printf("openat 主要特性:\n"); printf("1. 相对路径支持: 相对于目录文件描述符打开文件\n"); printf("2. 安全性提升: 避免路径解析竞态条件\n"); printf("3. 灵活性: 支持 AT_FDCWD 和绝对路径\n"); printf("4. 原子操作: 结合 O_CREAT|O_EXCL 实现原子创建\n"); printf("5. 容器友好: 在受限环境中更安全\n"); printf("\n使用场景:\n"); printf("• 容器和沙箱环境中的文件操作\n"); printf("• 多线程程序中的安全文件访问\n"); printf("• 需要避免竞态条件的文件操作\n"); printf("• 相对路径操作的系统工具\n"); }
int main() { printf("=== openat 高级文件操作工具 ===\n"); // 显示系统信息 printf("系统信息:\n"); printf(" PID: %d\n", getpid()); printf(" 页面大小: %ld 字节\n", (long)getpagesize()); // 演示特性 demonstrate_openat_features(); // 启动交互式管理器 char choice; printf("\n是否启动交互式文件管理器? (y/N): "); if (scanf(" %c", &choice) == 1 && (choice == 'y' || choice == 'Y')) { interactive_file_manager(); } return 0; }
|