-
Notifications
You must be signed in to change notification settings - Fork 1
/
SyncThread.cpp
296 lines (280 loc) · 8.56 KB
/
SyncThread.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "SyncThread.h"
#include "crc64util.h"
#include "globalvalue.h"
// 用多线程遍历本地文件夹
void SyncThread::run()
{
crc64_init();
if (getStatus() == 1)
{
readDirectory(path);
}
else if (getStatus() == 2)
{
readDirectory(path);
}
else if (getStatus() == 3)
{
readCloudDirectory(remotePath);
return ;
}
if(task==nullptr){
return ;
}
// Sync!
emit this->localTotalSize(totalSize);
emit this->upTotalSize(upFileSize);
QString machine = QSysInfo::machineHostName();
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QMap<QString, QString> map;
map["computerName"] = machine;
map["lastSyncTime"] = time;
cosclient->putObjectTagging(getRemotePath(), "", map);
QDir listen = path;
if (getStatus() == 1 || getStatus() == 2)
{
qDebug() << "upload/download";
auto path = listen.filesystemAbsolutePath();
watch *w = new watch(path, [this](struct event e)
{
QFileInfo info(e.path_name);
QString path = info.absoluteFilePath();
QString relativePath = path.mid(task->getLocalPath().length() + 1);
QString cloudPath = task->getRemotePath() + relativePath;
// 读取 path_type 状态
// 读取 effect_type 状态
switch (e.effect_type) {
case event::effect_type::rename:
qDebug() << "Effect type: Rename";
break;
case event::effect_type::modify:{
qDebug() << "Effect type: Modify";
if (isTheSameFile(path, cloudPath))
{
qDebug() << "Same file";
}
else
{
qDebug() << "Different file";
QFile file(path);
//是否可读
if (!file.open(QIODevice::ReadOnly))
{
break;
}
int fileTaskId = getNextFileTaskId();
emit this->newUploadTask(path, fileTaskId);
emit this->callUploadTask(path, cloudPath, fileTaskId);
QString machine = QSysInfo::machineHostName();
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QMap<QString, QString> map;
map["computerName"] = machine;
map["lastSyncTime"] = time;
cosclient->putObjectTagging(getRemotePath(), "", map);
}
break;
}
case event::effect_type::create:{
qDebug() << "Effect type: Create";
if (isTheSameFile(path, cloudPath))
{
qDebug() << "Same file";
}
else
{
qDebug() << "Different file";
QFile file(path);
//是否可读
if (!file.open(QIODevice::ReadOnly))
{
break;
}
int fileTaskId = getNextFileTaskId();
emit this->newUploadTask(path, fileTaskId);
emit this->callUploadTask(path, cloudPath, fileTaskId);
QString machine = QSysInfo::machineHostName();
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
QMap<QString, QString> map;
map["computerName"] = machine;
map["lastSyncTime"] = time;
cosclient->putObjectTagging(getRemotePath(), "", map);
}
break;
}
case event::effect_type::destroy:{
qDebug() << "Effect type: Destroy";
int fileTaskId = getNextFileTaskId();
emit this->callDeleteFileTask(cloudPath, "", fileTaskId);
break;
}
case event::effect_type::owner:
qDebug() << "Effect type: Owner change";
break;
case event::effect_type::other:
qDebug() << "Effect type: Other";
break;
} });
}
}
void SyncThread::readDirectory(const QString &path)
{
// 递归读取文件夹和子文件夹
recursiveRead(path);
}
void SyncThread::recursiveRead(const QString &path)
{
QDir dir(path);
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach (const QFileInfo &info, list)
{
if (info.isDir())
{
// 如果是目录,递归读取
recursiveRead(info.filePath());
}
else
{
// 如果是文件,添加到文件信息列表
qDebug() << info.fileName();
totalSize += info.size();
addSynctask(info);
}
}
}
void SyncThread::readCloudDirectory(const QString &cloudpath)
{
XmlProcesser processer;
QString xml;
Bucket bucket;
bucket.isTruncated = false;
bucket.nextMarker = "";
do
{
xml = cosclient->listObjects(cloudpath, bucket.nextMarker);
bucket = processer.processXml(xml);
for (auto ct : bucket.contents)
{
// 不保留key的md5
QString localPath = path + "/" + ct.key.mid(getRemotePath().length() + 33);
QFileInfo info(localPath);
bool needDownload = false;
if (!info.exists())
{
needDownload = true;
}
else if (info.isDir())
{
// 如果是文件夹,直接跳过
continue;
}
else
{
headHeader tmpHeaders;
preResponse response = cosclient->headObject(ct.key, "", tmpHeaders);
// read local file
QFile file(localPath);
file.open(QIODevice::ReadOnly);
QByteArray data = file.readAll();
file.close();
uint64_t crc64_data = 0;
crc64_data = crc64(crc64_data, data.data(), data.size());
uint64_t cloudCRC = response.headers["x-cos-hash-crc64ecma"].toULongLong();
if (crc64_data != cloudCRC)
{
needDownload = true;
}
}
if (needDownload)
{
downFileSize += ct.size;
int fileTaskId = getNextFileTaskId();
emit this->newDownloadTask(localPath, fileTaskId, ct.size);
emit this->callDownloadTask(localPath, ct.key, fileTaskId);
}
}
} while (bucket.isTruncated);
}
void SyncThread::addSynctask(const QFileInfo &info)
{
// 把path前面和task->getLocalPath()相同的部分去掉
QString myPath = info.absoluteFilePath();
QString relativePath = myPath.mid(path.length() + 1);
QString cloudPath = getRemotePath() + relativePath;
headHeader tmpHeaders;
preResponse response = cosclient->headObject(cloudPath, "", tmpHeaders);
if (cosclient->isExist(response))
{
}
else
{
upFileSize += info.size();
int fileTaskId = getNextFileTaskId();
emit this->newUploadTask(myPath, fileTaskId);
emit this->callUploadTask(myPath, cloudPath, fileTaskId);
}
}
bool SyncThread::isTheSameFile(const QString &localPath, const QString &cloudPath = "")
{
QString _cloudPath = cloudPath;
if (_cloudPath.isEmpty())
{
_cloudPath = getRemotePath() + localPath.mid(path.length() + 1);
}
bool result = false;
QFile file(localPath);
QFileInfo info(localPath);
if(file.isOpen()||info.size()==0){
return true;//we wont handle the file that is being written
}
if (!file.open(QIODevice::ReadOnly))
{
return false;
}
QByteArray data = file.readAll();
file.close();
uint64_t crc64_data = 0;
crc64_data = crc64(crc64_data, data.data(), data.size());
headHeader tmpHeaders;
preResponse response = cosclient->headObject(_cloudPath, "", tmpHeaders);
if (!cosclient->isExist(response))
{
return false;
}
uint64_t cloudCRC = response.headers["x-cos-hash-crc64ecma"].toULongLong();
if (crc64_data == cloudCRC)
{
result = true;
}
return result;
}
void SyncThread::onTaskCanceled(int fileTaskId)
{
}
int SyncThread::getStatus()
{
if (task==nullptr)
{
return syncStatus;
}
return task->getSyncStatus();
}
QString SyncThread::getRemotePath()
{
if(task==nullptr)
{
return remotePath;
}
return task->getRemotePath();
}
SyncThread::SyncThread(QString pathi, COSConfig configi, SyncTask *taski)
: path(pathi), config(configi), task(taski) {
cosclient = new COSClient(config);
}
SyncThread::SyncThread(QString pathi, COSConfig configi, QString _remotePath, int _syncStatus)
{
path = pathi;
remotePath = _remotePath;
syncStatus = _syncStatus;
config = configi;
cosclient = new COSClient(config);
};