-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.h
More file actions
97 lines (79 loc) · 2.85 KB
/
Copy pathFileOperations.h
File metadata and controls
97 lines (79 loc) · 2.85 KB
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
#pragma once
#include <windows.h>
#include <string>
#include <functional>
#include <atomic>
// 文件操作类 - 负责文件移动、复制和符号链接创建
class FileOperations
{
public:
// 进度回调函数类型: (当前进度, 总大小, 当前文件名)
using ProgressCallback = std::function<void(ULONGLONG, ULONGLONG, const std::wstring&)>;
// 操作结果结构
struct OperationResult {
bool success;
std::wstring errorMessage;
DWORD errorCode;
bool rollbackFailed; // 回滚是否失败
std::wstring rollbackErrorMessage; // 回滚失败的错误信息
OperationResult() : success(false), errorCode(0), rollbackFailed(false) {}
};
FileOperations();
~FileOperations();
// 移动目录(异步操作)
// sourcePath: 源路径
// destPath: 目标路径
// createSymlink: 是否在源位置创建符号链接
// progressCallback: 进度回调函数
OperationResult MoveDirectory(
const std::wstring& sourcePath,
const std::wstring& destPath,
bool createSymlink,
ProgressCallback progressCallback = nullptr
);
// 取消当前操作
void CancelOperation();
// 检查路径是否有效
static bool IsValidPath(const std::wstring& path);
// 检查是否有足够的权限
static bool HasPermission(const std::wstring& path);
// 创建符号链接(Junction Point)
static OperationResult CreateSymbolicLink(
const std::wstring& linkPath,
const std::wstring& targetPath
);
// 获取目录大小
static ULONGLONG GetDirectorySize(const std::wstring& path);
// 检查是否在同一驱动器
static bool IsSameDrive(const std::wstring& path1, const std::wstring& path2);
// 获取目录名称(路径的最后一部分)
static std::wstring GetDirectoryName(const std::wstring& path);
private:
std::atomic<bool> m_cancelled;
// 复制目录(递归)
OperationResult CopyDirectoryRecursive(
const std::wstring& source,
const std::wstring& dest,
ULONGLONG totalSize,
ULONGLONG& processedSize,
ProgressCallback progressCallback
);
// 复制单个文件
OperationResult CopyFileTo(
const std::wstring& source,
const std::wstring& dest,
ULONGLONG totalSize,
ULONGLONG& processedSize,
ProgressCallback progressCallback
);
// 删除目录(递归)
OperationResult DeleteDirectoryRecursive(const std::wstring& path);
// 回滚操作:将目标目录的内容移回源目录
OperationResult RollbackMove(
const std::wstring& sourcePath,
const std::wstring& destPath,
ProgressCallback progressCallback = nullptr
);
// 获取错误消息
static std::wstring GetLastErrorMessage(DWORD errorCode);
};