Skip to content

fix(storage): initialize default save path to Downloads instead of home#728

Open
re2zero wants to merge 1 commit intolinuxdeepin:masterfrom
re2zero:fix/default-storage-path
Open

fix(storage): initialize default save path to Downloads instead of home#728
re2zero wants to merge 1 commit intolinuxdeepin:masterfrom
re2zero:fix/default-storage-path

Conversation

@re2zero
Copy link
Copy Markdown
Contributor

@re2zero re2zero commented Apr 28, 2026

Summary

  • Initialize SessionManager._save_root to DownloadLocation in NetworkUtilPrivate constructor, eliminating dependency on storageConfig signal timing
  • Fix DaemonConfig::getStorageDir() fallback from homedir() to path::join(home, "Downloads") for compat daemon

Root Cause

PMS Bug #296181 — 新安装镜像上通过文管投送文件后,文件保存在主目录而非下载目录,必现。

5-Why 分析:

  1. _save_root(SessionManager) 和 storagedir(DaemonConfig) 未被正确设置
  2. NetworkUtil::updateStorageConfig() 从未被调用(依赖 storageConfig 信号)
  3. 信号在 deviceInfo() 中通过 std::call_once 触发,但可能在 connect 之前执行
  4. DaemonConfig::getStorageDir() fallback 是 homedir() 而非 Downloads
  5. 根因: 存储路径依赖信号时序初始化,无保底机制

为什么之前修复两次仍被激活: 打开设置对话框会自动写入默认路径并触发信号链,"修复"了问题。开发者本地无法复现是因为已有历史配置。

Test Plan

  • 新安装镜像,不打开设置对话框,投送文件 → 文件保存在 ~/Downloads/设备名(IP)/
  • 打开设置对话框,路径显示为 ~/Downloads
  • 修改保存路径后,投送文件 → 文件保存在新路径
  • 通过 compat daemon 接收文件 → 文件保存在 ~/Downloads/

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @re2zero, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: re2zero

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

SessionManager._save_root was empty until storageConfig signal arrived,
which depended on signal connection timing via std::call_once. On fresh
installs with no config, the signal never fired and files were saved to
home directory instead of Downloads.

SessionManager._save_root 在 storageConfig 信号到达前为空,而该信号
依赖 std::call_once 的时序。新安装系统无历史配置时信号不触发,导致文件
保存在主目录而非下载目录。

Log: 修复新安装系统文件保存路径默认为主目录的问题
PMS: BUG-296181
Influence: 新安装镜像上接收文件将正确保存在下载目录,而非主目录。已安装系统行为不受影响。
@re2zero re2zero force-pushed the fix/default-storage-path branch from fc5112e to d5738b0 Compare April 28, 2026 11:19
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码变更主要涉及两个文件的修改,主要是关于存储路径的调整。我将从语法逻辑、代码质量、代码性能和代码安全四个方面进行详细审查。

1. 语法逻辑审查

config.h 修改点:

  • 逻辑变更合理:将默认存储目录从用户主目录(home)修改为下载目录(Downloads)。
  • 路径拼接逻辑正确:使用了path::join进行路径拼接,这是正确的做法,可以处理不同操作系统下的路径分隔符问题。

networkutil.cpp 修改点:

  • 逻辑变更一致:同样将存储根目录设置为下载目录,与config.h的修改保持一致。
  • 头文件引入正确:添加了<QStandardPaths>头文件,用于获取系统标准路径。

2. 代码质量审查

优点:

  • 修改后的代码使用了跨平台的标准路径获取方式,提高了代码的可移植性。
  • 两处修改保持了一致性,都指向下载目录,符合用户对文件存储位置的预期。

改进建议:

  1. config.h 中的 getStorageDir 方法缺少 const 修饰

    const fastring getStorageDir() const  // 应该添加 const
  2. config.h 中路径硬编码问题

    • 当前代码中 "Downloads" 是硬编码的字符串,不同操作系统或不同语言环境下,下载目录的名称可能不同。
    • 建议使用与 networkutil.cpp 类似的方式,通过标准API获取下载目录。
  3. 版权年份更新

    • 两处都将版权年份从 2023/2024 更新到 2026,这看起来是合理的,但请确认这是否符合项目的实际计划。

3. 代码性能审查

  • getStorageDir 方法在每次调用时都会重新计算路径,如果频繁调用可能会影响性能。
  • 建议考虑缓存结果,或者在初始化时计算一次并存储。

4. 代码安全审查

潜在问题:

  1. 目录存在性检查缺失

    • 代码没有检查下载目录是否存在,如果目录不存在,后续的文件操作可能会失败。
    • 建议在设置存储路径前检查目录是否存在,如果不存在则创建。
  2. 路径权限问题

    • 没有检查程序是否有权限访问下载目录,在某些受限环境下可能会出现问题。

改进建议代码

config.h 改进建议:

const fastring getStorageDir() const
{
    // 使用标准API获取下载目录,避免硬编码
    fastring download = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation).toStdString();
    
    // 确保目录存在
    if (!os::exists(download)) {
        os::mkdir(download, true); // 递归创建目录
    }
    
    if (_targetName.empty()) {
        return download;
    } else {
        return path::join(download, _targetName);
    }
}

networkutil.cpp 改进建议:

NetworkUtilPrivate::NetworkUtilPrivate(NetworkUtil *qq)
    : q_ptr(qq)
{
    LOG << "This is only transfer?" << onlyTransfer;

    sessionManager = new SessionManager(this);
    
    // 获取并验证下载目录
    QString downloadDir = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
    if (downloadDir.isEmpty()) {
        LOG << "Warning: Failed to get download directory, fallback to home directory";
        downloadDir = QDir::homePath();
    }
    
    // 确保目录存在
    QDir dir(downloadDir);
    if (!dir.exists()) {
        if (!dir.mkpath(".")) {
            LOG << "Error: Failed to create download directory";
        }
    }
    
    sessionManager->setStorageRoot(downloadDir);
    
    if (onlyTransfer) {
        DLOG << "Running in transfer-only mode, skipping full initialization";
        return;
    }
    // ... 其余代码
}

总结

  1. 主要改进点是使用跨平台的标准API获取下载目录,而不是硬编码。
  2. 需要添加目录存在性检查和创建逻辑。
  3. 建议添加错误处理,以应对无法获取下载目录的情况。
  4. getStorageDir 方法应该添加 const 修饰符。
  5. 考虑性能优化,避免重复计算路径。

这些改进将使代码更加健壮、可移植和安全。

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.

2 participants