Skip to content

Comments

修复移动文件夹逻辑并实现SHA-256哈希校验 (Fix move folder logic with SHA-256 hash verification)#62

Merged
TC999 merged 5 commits intomainfrom
copilot/fix-aa892164-e8f8-419a-a66a-a25a34455a89
Aug 1, 2025
Merged

修复移动文件夹逻辑并实现SHA-256哈希校验 (Fix move folder logic with SHA-256 hash verification)#62
TC999 merged 5 commits intomainfrom
copilot/fix-aa892164-e8f8-419a-a66a-a25a34455a89

Conversation

Copy link
Contributor

Copilot AI commented Jul 31, 2025

Fixes #15

问题概述 (Issue Summary)

原有的移动文件夹功能存在以下问题:

  • 文件可以正常移动,但后续流程不完整
  • 缺少文件完整性校验
  • UI线程在操作过程中会阻塞
  • 进度反馈过于粗略

实现的解决方案 (Solution Implemented)

1. 完整的工作流程

按照Issue #15的要求,实现了完整的移动文件夹流程:

  1. 复制源文件夹到目标目录 - 显示文件级别的实时进度
  2. SHA-256哈希校验 - 对源目录和目标目录的所有文件进行校验
  3. 删除源目录 - 仅在哈希校验通过后执行
  4. 创建符号链接 - 使用 mklink /D (Windows) 或 symlink (Unix)
  5. 实时状态反馈 - 每个步骤都有详细的进度和状态显示

2. 技术改进

非阻塞UI设计

// 原来:阻塞主线程
for msg in rx {
    // 处理消息...
}

// 现在:非阻塞检查
while let Ok(msg) = rx.try_recv() {
    // 处理消息并请求重绘
    ctx.request_repaint();
}

SHA-256哈希校验
使用 sha2 crate 实现文件完整性校验:

fn verify_directory_hashes(source_dir: &Path, target_dir: &Path) -> Result<bool, String> {
    let source_files = collect_all_files(source_dir)?;
    let target_files = collect_all_files(target_dir)?;
    
    for (source_file, target_file) in source_files.iter().zip(target_files.iter()) {
        let source_hash = calculate_file_hash(source_file)?;
        let target_hash = calculate_file_hash(target_file)?;
        if source_hash != target_hash {
            return Ok(false);
        }
    }
    Ok(true)
}

详细进度报告

  • 文件级别的复制进度:已复制 15/32 个文件: config.json
  • 哈希校验进度:哈希校验进度: 85.2%
  • 实时状态更新:每复制一个文件就发送进度消息

3. 错误处理和安全性

  • 哈希校验失败时立即停止:如果任何文件的哈希不匹配,立即终止操作并保留源文件
  • 详细错误日志:使用 logger::log_error() 记录所有错误信息
  • 用户友好的错误消息:在UI中显示清晰的错误描述

4. 跨平台支持

改进了符号链接创建逻辑:

  • Windows: 使用 mklink /D 命令创建目录符号链接
  • Unix: 使用 std::os::unix::fs::symlink 创建软链接
  • 错误处理: 适当处理权限不足等问题

测试 (Testing)

添加了单元测试验证核心功能:

  • test_calculate_file_hash(): 验证SHA-256哈希计算正确性
  • test_count_files_in_directory(): 验证文件计数逻辑
  • test_collect_all_files(): 验证递归文件收集功能

向后兼容性 (Backward Compatibility)

  • 保持了原有的 MoveModule 公共接口
  • 所有更改都在 src/move_module.rs 内部实现
  • 不影响其他模块的使用方式

使用示例 (Usage)

用户体验流程:

  1. 选择要移动的文件夹
  2. 选择目标目录
  3. 点击"确定"开始移动
  4. 观察实时进度:已复制 5/20 个文件: settings.dat
  5. 等待哈希校验:哈希校验进度: 75%
  6. 完成:移动文件夹操作成功完成!符号链接已创建

这个实现确保了文件移动操作的完整性和可靠性,同时提供了良好的用户体验。

This pull request was created as a result of the following prompt from Copilot chat.

修复移动文件夹逻辑,使其符合 Issue #15 的需求,并在文件复制完成后加入哈希校验(使用 sha2 crate 对源目录和目标目录下的所有文件进行 SHA-256 校验)。流程如下:

  1. 复制源文件夹到目标目录,并显示进度。
  2. 每复制一个文件就实时发送进度消息。
  3. 复制完成后,分别对源、目标文件夹内的所有文件做 SHA-256 校验,对比是否完全一致。
  4. 若哈希校验全部通过,删除源目录,并继续创建符号链接(mklink /D ...),再反馈结果。
  5. 若哈希校验不通过,提示用户,并终止后续操作。
  6. UI 线程不得阻塞,能实时显示进度和状态。
  7. 保持良好日志和错误处理。

要求:

  • 需用 sha2 crate 做哈希。
  • 进度、状态和错误要能在界面实时反馈。
  • 所有逻辑和交互都在 src/move_module.rs 中实现。
  • 仅动用必要的代码变更,整体风格和当前实现一致。

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] 修复移动并增加哈希校验(sha2),UI不阻塞并实时反馈进度和状态 修复移动文件夹逻辑并实现SHA-256哈希校验 (Fix move folder logic with SHA-256 hash verification) Jul 31, 2025
Copilot AI requested a review from TC999 July 31, 2025 09:23
Copilot finished work on behalf of TC999 July 31, 2025 09:23
@TC999 TC999 marked this pull request as ready for review July 31, 2025 09:38
@coderabbitai
Copy link

coderabbitai bot commented Jul 31, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Join our Discord community for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@TC999 TC999 merged commit 07d40a4 into main Aug 1, 2025
2 of 4 checks passed
@TC999 TC999 deleted the copilot/fix-aa892164-e8f8-419a-a66a-a25a34455a89 branch October 19, 2025 08:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

移动文件逻辑 BUG 待修

2 participants