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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <errno.h> #include <string.h> #include <getopt.h> #include <time.h>
// 配置结构体 struct handle_tool_config { char *filename; char *handle_file; int create_handle; int open_file; int show_info; int verbose; int flags; char *output_file; };
// 保存文件句柄到文件 int save_handle_to_file(const struct file_handle *handle, int mount_id, const char *filename) { FILE *fp = fopen(filename, "wb"); if (!fp) { perror("打开句柄文件失败"); return -1; } // 写入挂载 ID if (fwrite(&mount_id, sizeof(mount_id), 1, fp) != 1) { perror("写入挂载 ID 失败"); fclose(fp); return -1; } // 写入句柄大小 if (fwrite(&handle->handle_bytes, sizeof(handle->handle_bytes), 1, fp) != 1) { perror("写入句柄大小失败"); fclose(fp); return -1; } // 写入句柄类型 if (fwrite(&handle->handle_type, sizeof(handle->handle_type), 1, fp) != 1) { perror("写入句柄类型失败"); fclose(fp); return -1; } // 写入句柄数据 if (fwrite(handle->f_handle, handle->handle_bytes, 1, fp) != 1) { perror("写入句柄数据失败"); fclose(fp); return -1; } fclose(fp); printf("✓ 句柄已保存到: %s\n", filename); return 0; }
// 从文件加载文件句柄 struct file_handle* load_handle_from_file(const char *filename, int *mount_id) { FILE *fp = fopen(filename, "rb"); if (!fp) { perror("打开句柄文件失败"); return NULL; } // 读取挂载 ID if (fread(mount_id, sizeof(*mount_id), 1, fp) != 1) { perror("读取挂载 ID 失败"); fclose(fp); return NULL; } // 读取句柄大小 unsigned int handle_bytes; if (fread(&handle_bytes, sizeof(handle_bytes), 1, fp) != 1) { perror("读取句柄大小失败"); fclose(fp); return NULL; } // 分配句柄内存 size_t handle_size = sizeof(struct file_handle) + handle_bytes; struct file_handle *handle = malloc(handle_size); if (!handle) { perror("内存分配失败"); fclose(fp); return NULL; } handle->handle_bytes = handle_bytes; // 读取句柄类型 if (fread(&handle->handle_type, sizeof(handle->handle_type), 1, fp) != 1) { perror("读取句柄类型失败"); free(handle); fclose(fp); return NULL; } // 读取句柄数据 if (fread(handle->f_handle, handle_bytes, 1, fp) != 1) { perror("读取句柄数据失败"); free(handle); fclose(fp); return NULL; } fclose(fp); printf("✓ 从 %s 加载句柄成功\n", filename); return handle; }
// 获取文件句柄 int get_file_handle_safe(const char *filename, struct file_handle **handle, int *mount_id) { size_t handle_size = sizeof(struct file_handle); *handle = malloc(handle_size); if (!*handle) { return -1; } (*handle)->handle_bytes = 0; int result = name_to_handle_at(AT_FDCWD, filename, *handle, mount_id, 0); if (result == -1 && errno == EOVERFLOW) { handle_size = sizeof(struct file_handle) + (*handle)->handle_bytes; free(*handle); *handle = malloc(handle_size); if (!*handle) { return -1; } result = name_to_handle_at(AT_FDCWD, filename, *handle, mount_id, 0); } return result; }
// 通过句柄打开文件 int open_file_by_handle_safe(struct file_handle *handle, int flags, const char *description) { if (description) { printf("通过句柄打开文件: %s\n", description); } int fd = open_by_handle_at(AT_FDCWD, handle, flags); if (fd != -1) { if (description) { printf(" ✓ 成功打开文件 (fd: %d)\n", fd); } return fd; } else { if (description) { printf(" ✗ 打开文件失败: %s\n", strerror(errno)); } return -1; } }
// 显示句柄信息 void show_handle_info(const struct file_handle *handle, int mount_id) { printf("=== 文件句柄信息 ===\n"); printf("挂载 ID: %d\n", mount_id); printf("句柄类型: %d\n", handle->handle_type); printf("句柄大小: %u 字节\n", handle->handle_bytes); printf("句柄数据 (十六进制): "); for (unsigned int i = 0; i < handle->handle_bytes && i < 64; i++) { printf("%02x", handle->f_handle[i]); } if (handle->handle_bytes > 64) { printf("...(还有 %u 字节)", handle->handle_bytes - 64); } printf("\n"); }
// 复制文件内容 int copy_file_content(int src_fd, const char *output_filename) { int dst_fd = open(output_filename, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (dst_fd == -1) { perror("创建输出文件失败"); return -1; } char buffer[4096]; ssize_t bytes_read, bytes_written; off_t total_bytes = 0; while ((bytes_read = read(src_fd, buffer, sizeof(buffer))) > 0) { bytes_written = write(dst_fd, buffer, bytes_read); if (bytes_written != bytes_read) { perror("写入输出文件失败"); close(dst_fd); return -1; } total_bytes += bytes_written; } if (bytes_read == -1) { perror("读取源文件失败"); close(dst_fd); return -1; } close(dst_fd); printf("✓ 成功复制 %ld 字节到: %s\n", (long)total_bytes, output_filename); return 0; }
// 显示帮助信息 void show_help(const char *program_name) { printf("用法: %s [选项]\n", program_name); printf("\n选项:\n"); printf(" -f, --file=FILE 源文件名\n"); printf(" -h, --handle=FILE 句柄文件名\n"); printf(" -c, --create 创建文件句柄\n"); printf(" -o, --open 通过句柄打开文件\n"); printf(" -i, --info 显示句柄信息\n"); printf(" -r, --read-only 以只读方式打开\n"); printf(" -w, --read-write 以读写方式打开\n"); printf(" -O, --output=FILE 输出文件名(用于复制)\n"); printf(" -v, --verbose 详细输出\n"); printf(" --help 显示此帮助信息\n"); printf("\n示例:\n"); printf(" %s -f /etc/passwd -c -h passwd.handle # 创建句柄\n", program_name); printf(" %s -h passwd.handle -o -r # 通过句柄只读打开\n", program_name); printf(" %s -h passwd.handle -o -w -O copy.txt # 通过句柄复制文件\n", program_name); printf(" %s -h passwd.handle -i # 显示句柄信息\n", program_name); }
int main(int argc, char *argv[]) { struct handle_tool_config config = { .filename = NULL, .handle_file = NULL, .create_handle = 0, .open_file = 0, .show_info = 0, .verbose = 0, .flags = O_RDONLY, .output_file = NULL }; printf("=== 文件句柄管理工具 ===\n\n"); // 解析命令行参数 static struct option long_options[] = { {"file", required_argument, 0, 'f'}, {"handle", required_argument, 0, 'h'}, {"create", no_argument, 0, 'c'}, {"open", no_argument, 0, 'o'}, {"info", no_argument, 0, 'i'}, {"read-only", no_argument, 0, 'r'}, {"read-write", no_argument, 0, 'w'}, {"output", required_argument, 0, 'O'}, {"verbose", no_argument, 0, 'v'}, {"help", no_argument, 0, 1000}, {0, 0, 0, 0} }; int opt; while ((opt = getopt_long(argc, argv, "f:h:coirwO:v", long_options, NULL)) != -1) { switch (opt) { case 'f': config.filename = optarg; break; case 'h': config.handle_file = optarg; break; case 'c': config.create_handle = 1; break; case 'o': config.open_file = 1; break; case 'i': config.show_info = 1; break; case 'r': config.flags = O_RDONLY; break; case 'w': config.flags = O_RDWR; break; case 'O': config.output_file = optarg; break; case 'v': config.verbose = 1; break; case 1000: // --help show_help(argv[0]); return 0; default: fprintf(stderr, "使用 '%s --help' 查看帮助信息\n", argv[0]); return 1; } } // 验证参数 if (!config.create_handle && !config.open_file && !config.show_info) { show_help(argv[0]); return 0; } struct file_handle *handle = NULL; int mount_id; // 如果需要创建句柄 if (config.create_handle && config.filename) { if (access(config.filename, F_OK) != 0) { fprintf(stderr, "文件不存在: %s\n", config.filename); return 1; } printf("创建文件句柄: %s\n", config.filename); if (get_file_handle_safe(config.filename, &handle, &mount_id) == 0) { printf("✓ 成功获取文件句柄\n"); if (config.show_info) { show_handle_info(handle, mount_id); } if (config.handle_file) { if (save_handle_to_file(handle, mount_id, config.handle_file) == 0) { printf("✓ 句柄保存成功\n"); } else { fprintf(stderr, "句柄保存失败\n"); free(handle); return 1; } } } else { fprintf(stderr, "获取文件句柄失败: %s\n", strerror(errno)); return 1; } } // 如果需要加载句柄 else if (config.handle_file) { if (access(config.handle_file, F_OK) != 0) { fprintf(stderr, "句柄文件不存在: %s\n", config.handle_file); return 1; } handle = load_handle_from_file(config.handle_file, &mount_id); if (!handle) { return 1; } if (config.show_info) { show_handle_info(handle, mount_id); } } else { fprintf(stderr, "需要指定文件或句柄文件\n"); show_help(argv[0]); return 1; } // 通过句柄打开文件 if (config.open_file && handle) { int fd = open_file_by_handle_safe(handle, config.flags, config.filename ? config.filename : "加载的句柄"); if (fd != -1) { printf("✓ 文件打开成功\n"); // 获取文件信息 struct stat st; if (fstat(fd, &st) == 0) { printf("文件信息:\n"); printf(" 大小: %ld 字节\n", (long)st.st_size); printf(" 权限: %o\n", st.st_mode & 0777); printf(" 修改时间: %s", ctime(&st.st_mtime)); } // 如果指定了输出文件,复制内容 if (config.output_file) { if (copy_file_content(fd, config.output_file) != 0) { close(fd); free(handle); return 1; } } // 否则显示部分内容 else if (config.flags & (O_RDONLY | O_RDWR)) { printf("文件内容预览:\n"); char buffer[512]; ssize_t bytes_read = read(fd, buffer, sizeof(buffer) - 1); if (bytes_read > 0) { buffer[bytes_read] = '\0'; // 只显示前 200 个字符 if (strlen(buffer) > 200) { buffer[200] = '\0'; printf("%s...\n", buffer); } else { printf("%s\n", buffer); } } } close(fd); } else { free(handle); return 1; } } // 清理资源 if (handle) { free(handle); } printf("\n=== 文件句柄工具使用建议 ===\n"); printf("适用场景:\n"); printf("1. 文件监控和审计系统\n"); printf("2. 备份和同步工具\n"); printf("3. 容器和虚拟化环境\n"); printf("4. 网络文件传输\n"); printf("5. 安全文件访问控制\n"); printf("\n"); printf("安全建议:\n"); printf("1. 妥善保管句柄文件\n"); printf("2. 使用适当的文件权限\n"); printf("3. 验证句柄的有效性\n"); printf("4. 及时关闭文件描述符\n"); printf("5. 处理句柄失效的情况\n"); printf("\n"); printf("性能优化:\n"); printf("1. 批量处理多个文件\n"); printf("2. 缓存常用文件句柄\n"); printf("3. 异步操作大文件\n"); printf("4. 合理设置缓冲区大小\n"); return 0; }
|