feat: add system-level file I/O support to DFile#277
Conversation
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
e03c02a to
063523c
Compare
deepin pr auto reviewGit Diff 代码审查报告1. 总体评价这段代码为 2. 语法与逻辑问题2.1 变量命名不一致// dfile.cpp:193
if (isOpen) {
return true;
}问题: 建议:统一使用 2.2 错误处理不完整// dfile.cpp:312
return doCloseBySys();问题: 建议:根据当前打开的文件类型选择正确的关闭方法。 2.3 函数参数类型不匹配// dfile.cpp:425
bool DFilePrivate::doOpenBySys(const int model, const int permissions)问题:参数名 建议: 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);问题:在栈上分配大数组可能导致栈溢出,特别是当 建议:使用堆分配或限制最大读取大小: 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 重复代码问题: 建议:将公共逻辑提取到 3.3 缺少注释问题:新增的枚举和函数缺少详细注释。 建议:为新增的枚举和函数添加详细注释,说明用途、参数和返回值。 4. 性能问题4.1 频繁的类型转换// dfile.cpp:437
GInputStream *inputStream = const_cast<DFilePrivate *>(this)->inputStream();问题:频繁使用 建议:重新设计接口,减少 4.2 不必要的内存操作// dfile.cpp:438
memset(&data, 0, maxSize + 1);问题:对于二进制数据, 建议:只在需要字符串操作时才进行 5. 安全性问题5.1 文件描述符泄漏风险// dfile.cpp:425
bool DFilePrivate::doCloseBySys()
{
if (fileFd < 0) {
return true;
}
// ...
}问题:如果 建议:即使 5.2 缺少输入验证// dfile.cpp:425
bool DFilePrivate::doOpenBySys(const int model, const int permissions)
{
// ...
fileFd = ::open(uri.path().toUtf8().data(), model, permissions);
// ...
}问题:没有验证 建议:添加参数验证: 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);问题:直接使用 建议:验证路径是否在允许的目录内: 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. 总结这段代码实现了基本的系统调用文件操作功能,但在安全性、错误处理和代码结构方面需要改进。主要问题包括:
建议按照上述改进意见进行修改,特别是加强安全性和错误处理部分,以提高代码的健壮性和安全性。 |
|
[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. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
ad4333b
into
linuxdeepin:develop/reagle-20260421
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:
Added:
Log: DFile文件类添加系统级文件读写支持
Influence:
feat: DFile文件类添加系统级文件读写支持
添加了 FileCopyType 和 FileCopySyncType 枚举,允许在 GIO 流操作和系统级 POSIX 文件操作之间进行选择。新增了使用 POSIX 系统调用(open、read、write、 lseek、syncfs、close)的系统级文件 I/O 方法。
此增强功能提供两种 I/O 模式:
Log: DFile文件类添加系统级文件读写支持
Influence:
Bug: https://pms.uniontech.com//bug-view-357747.html