Skip to content

feat: add system-level file I/O support to DFile#277

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/reagle-20260421from
liyigang1:develop/reagle-20260421
Apr 22, 2026
Merged

feat: add system-level file I/O support to DFile#277
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/reagle-20260421from
liyigang1:develop/reagle-20260421

Conversation

@liyigang1
Copy link
Copy Markdown
Contributor

Added FileCopyType and FileCopySyncType enums to allow selecting between GIO stream operations and system-level POSIX file operations. Added new methods for system-level file I/O (open, read, write, seek, flush, close) using POSIX system calls (open, read, write, lseek, syncfs, close).

This enhancement provides two I/O modes:

  • kCopyTypeByGioStream: Uses GIO stream (default, supports remote files)
  • kCopyTypeBySys: Uses POSIX system calls (optimized for local files)

Added:

  • FileCopyType and FileCopySyncType enums for mode selection
  • setCopyType/setSyncType/copyFileType/syncType methods
  • open(const int mode, const int permissions) overload for system mode
  • Private methods: doOpenBySys, doCloseBySys, doWriteBySys, readBySys, posBySys, seekBySys

Log: DFile文件类添加系统级文件读写支持

Influence:

  1. 验证 GIO stream 模式仍正常工作(默认模式)
  2. 测试系统级读写模式的本地文件操作
  3. 验证文件 seek 和 pos 定位功能在两种模式下正常
  4. 测试 flush 同步功能在两种模式下正常
  5. 验证非本地文件使用系统模式时正确返回错误

feat: DFile文件类添加系统级文件读写支持

添加了 FileCopyType 和 FileCopySyncType 枚举,允许在 GIO 流操作和系统级 POSIX 文件操作之间进行选择。新增了使用 POSIX 系统调用(open、read、write、 lseek、syncfs、close)的系统级文件 I/O 方法。

此增强功能提供两种 I/O 模式:

  • kCopyTypeByGioStream:使用 GIO 流(默认,支持远程文件)
  • kCopyTypeBySys:使用 POSIX 系统调用(本地文件优化)

Log: DFile文件类添加系统级文件读写支持

Influence:

  1. 验证 GIO stream 模式仍正常工作(默认模式)
  2. 测试系统级读写模式的本地文件操作
  3. 验证文件 seek 和 pos 定位功能在两种模式下正常
  4. 测试 flush 同步功能在两种模式下正常
  5. 验证非本地文件使用系统模式时正确返回错误

Bug: https://pms.uniontech.com//bug-view-357747.html

Added FileCopyType and FileCopySyncType enums to allow selecting between
GIO stream operations and system-level POSIX file operations. Added new
methods for system-level file I/O (open, read, write, seek, flush, close)
using POSIX system calls (open, read, write, lseek, syncfs, close).

This enhancement provides two I/O modes:
- kCopyTypeByGioStream: Uses GIO stream (default, supports remote files)
- kCopyTypeBySys: Uses POSIX system calls (optimized for local files)

Added:
- FileCopyType and FileCopySyncType enums for mode selection
- setCopyType/setSyncType/copyFileType/syncType methods
- open(const int mode, const int permissions) overload for system mode
- Private methods: doOpenBySys, doCloseBySys, doWriteBySys, readBySys,
  posBySys, seekBySys

Log: DFile文件类添加系统级文件读写支持

Influence:
1. 验证 GIO stream 模式仍正常工作(默认模式)
2. 测试系统级读写模式的本地文件操作
3. 验证文件 seek 和 pos 定位功能在两种模式下正常
4. 测试 flush 同步功能在两种模式下正常
5. 验证非本地文件使用系统模式时正确返回错误

feat: DFile文件类添加系统级文件读写支持

添加了 FileCopyType 和 FileCopySyncType 枚举,允许在 GIO 流操作和系统级
POSIX 文件操作之间进行选择。新增了使用 POSIX 系统调用(open、read、write、
lseek、syncfs、close)的系统级文件 I/O 方法。

此增强功能提供两种 I/O 模式:
- kCopyTypeByGioStream:使用 GIO 流(默认,支持远程文件)
- kCopyTypeBySys:使用 POSIX 系统调用(本地文件优化)

Log: DFile文件类添加系统级文件读写支持

Influence:
1. 验证 GIO stream 模式仍正常工作(默认模式)
2. 测试系统级读写模式的本地文件操作
3. 验证文件 seek 和 pos 定位功能在两种模式下正常
4. 测试 flush 同步功能在两种模式下正常
5. 验证非本地文件使用系统模式时正确返回错误

Bug: https://pms.uniontech.com//bug-view-357747.html
@liyigang1 liyigang1 force-pushed the develop/reagle-20260421 branch from e03c02a to 063523c Compare April 21, 2026 13:17
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

Git Diff 代码审查报告

1. 总体评价

这段代码为 DFile 类添加了使用系统调用(open/read/write/close)替代 GIO 流操作的功能,主要用于本地文件的读写操作。代码实现了基本的文件操作功能,但在安全性、错误处理和代码结构方面存在一些需要改进的地方。

2. 语法与逻辑问题

2.1 变量命名不一致

// dfile.cpp:193
if (isOpen) {
    return true;
}

问题:isOpen 是成员变量,但其他地方使用 d->isOpen,命名不一致。

建议:统一使用 d->isOpen 或将 isOpen 作为成员变量。

2.2 错误处理不完整

// dfile.cpp:312
return doCloseBySys();

问题:doClose() 函数现在总是调用 doCloseBySys(),但原始代码可能需要处理 GIO 流的关闭。

建议:根据当前打开的文件类型选择正确的关闭方法。

2.3 函数参数类型不匹配

// dfile.cpp:425
bool DFilePrivate::doOpenBySys(const int model, const int permissions)

问题:参数名 model 应该是 mode,且 permissions 应该使用 mode_t 类型。

建议:

bool DFilePrivate::doOpenBySys(const int mode, const mode_t permissions)

3. 代码质量问题

3.1 栈溢出风险

// dfile.cpp:437
char data[maxSize + 1];
memset(&data, 0, maxSize + 1);

问题:在栈上分配大数组可能导致栈溢出,特别是当 maxSize 很大时。

建议:使用堆分配或限制最大读取大小:

const qint64 kMaxStackSize = 1024 * 1024; // 1MB
if (maxSize > kMaxStackSize) {
    // 使用堆分配或分块读取
    QByteArray buffer;
    buffer.resize(maxSize);
    char *data = buffer.data();
    // ...
} else {
    char data[maxSize + 1];
    // ...
}

3.2 重复代码

问题:pos()seek() 函数在 DFileDFilePrivate 中有重复实现。

建议:将公共逻辑提取到 DFilePrivate 中,DFile 只负责调用。

3.3 缺少注释

问题:新增的枚举和函数缺少详细注释。

建议:为新增的枚举和函数添加详细注释,说明用途、参数和返回值。

4. 性能问题

4.1 频繁的类型转换

// dfile.cpp:437
GInputStream *inputStream = const_cast<DFilePrivate *>(this)->inputStream();

问题:频繁使用 const_cast 可能影响性能。

建议:重新设计接口,减少 const_cast 的使用。

4.2 不必要的内存操作

// dfile.cpp:438
memset(&data, 0, maxSize + 1);

问题:对于二进制数据,memset 是不必要的。

建议:只在需要字符串操作时才进行 memset

5. 安全性问题

5.1 文件描述符泄漏风险

// dfile.cpp:425
bool DFilePrivate::doCloseBySys()
{
    if (fileFd < 0) {
        return true;
    }
    // ...
}

问题:如果 close() 失败,文件描述符可能仍然有效,导致资源泄漏。

建议:即使 close() 失败,也应将 fileFd 设为无效值。

5.2 缺少输入验证

// dfile.cpp:425
bool DFilePrivate::doOpenBySys(const int model, const int permissions)
{
    // ...
    fileFd = ::open(uri.path().toUtf8().data(), model, permissions);
    // ...
}

问题:没有验证 modelpermissions 的值是否合法。

建议:添加参数验证:

bool DFilePrivate::doOpenBySys(const int mode, const mode_t permissions)
{
    // 验证 mode 和 permissions
    if ((mode & O_ACCMODE) == 0) {
        error.setCode(DFMIOErrorCode::DFM_IO_ERROR_INVALID_ARGUMENT);
        error.setMessage("Invalid file access mode");
        return false;
    }
    
    // ...
}

5.3 路径遍历风险

// dfile.cpp:425
fileFd = ::open(uri.path().toUtf8().data(), model, permissions);

问题:直接使用 uri.path() 可能导致路径遍历攻击。

建议:验证路径是否在允许的目录内:

QString path = uri.path();
if (!isPathAllowed(path)) {
    error.setCode(DFMIOErrorCode::DFM_IO_ERROR_PERMISSION_DENIED);
    error.setMessage("Path not allowed");
    return false;
}
fileFd = ::open(path.toUtf8().data(), mode, permissions);

5.4 缺少权限检查

// dfile.cpp:425
fileFd = ::open(uri.path().toUtf8().data(), model, permissions);

问题:没有在打开文件前检查用户是否有权限。

建议:添加权限检查:

// 检查读权限
if ((mode & O_RDONLY) && !checkReadPermission(path)) {
    error.setCode(DFMIOErrorCode::DFM_IO_ERROR_PERMISSION_DENIED);
    error.setMessage("Read permission denied");
    return false;
}

// 检查写权限
if ((mode & O_WRONLY) && !checkWritePermission(path)) {
    error.setCode(DFMIOErrorCode::DFM_IO_ERROR_PERMISSION_DENIED);
    error.setMessage("Write permission denied");
    return false;
}

6. 改进建议

6.1 使用 RAII 管理文件描述符

建议创建一个 RAII 类来管理文件描述符,确保资源正确释放:

class FileDescriptor {
public:
    explicit FileDescriptor(int fd = -1) : fd_(fd) {}
    ~FileDescriptor() { close(); }
    
    void close() {
        if (fd_ >= 0) {
            ::close(fd_);
            fd_ = -1;
        }
    }
    
    int get() const { return fd_; }
    bool isValid() const { return fd_ >= 0; }
    
    // 禁止拷贝
    FileDescriptor(const FileDescriptor&) = delete;
    FileDescriptor& operator=(const FileDescriptor&) = delete;
    
    // 允许移动
    FileDescriptor(FileDescriptor&& other) : fd_(other.fd_) {
        other.fd_ = -1;
    }
    
    FileDescriptor& operator=(FileDescriptor&& other) {
        if (this != &other) {
            close();
            fd_ = other.fd_;
            other.fd_ = -1;
        }
        return *this;
    }
    
private:
    int fd_;
};

6.2 统一错误处理

建议创建一个统一的错误处理函数:

bool DFilePrivate::handleSystemError(const QString& operation) {
    error.setCode(DFMIOErrorCode::DFM_IO_ERROR_FAILED);
    error.setMessage(QString("%1 failed: %2").arg(operation).arg(strerror(errno)));
    qWarning() << operation << "failed:" << strerror(errno);
    return false;
}

6.3 添加资源限制

建议添加对文件大小和操作次数的限制:

class DFilePrivate : public QObject {
    // ...
    static constexpr qint64 kMaxFileSize = 1024 * 1024 * 1024; // 1GB
    static constexpr int kMaxReadRetries = 3;
    // ...
};

7. 总结

这段代码实现了基本的系统调用文件操作功能,但在安全性、错误处理和代码结构方面需要改进。主要问题包括:

  1. 缺少输入验证和权限检查
  2. 文件描述符管理不当,可能导致资源泄漏
  3. 栈溢出风险
  4. 代码重复和命名不一致
  5. 错误处理不完整

建议按照上述改进意见进行修改,特别是加强安全性和错误处理部分,以提高代码的健壮性和安全性。

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: liyigang1, max-lvs

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

@liyigang1
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown

deepin-bot Bot commented Apr 22, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit ad4333b into linuxdeepin:develop/reagle-20260421 Apr 22, 2026
20 of 21 checks passed
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.

3 participants