Repository navigation
Expand file tree
/
Copy pathtars_patch.cpp
More file actions
291 lines (248 loc) · 6.91 KB
/
Copy pathtars_patch.cpp
File metadata and controls
291 lines (248 loc) · 6.91 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
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
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
#include "util/tc_file.h"
#include "util/tc_option.h"
#include "util/tc_md5.h"
#include "servant/TarsLogger.h"
#include "tars_patch.h"
#include <iostream>
namespace tars
{
TarsPatch::TarsPatch()
: _remove(false)
{
}
void TarsPatch::checkLocalDir()
{
if (!tars::TC_File::isFileExistEx(_localDir, S_IFDIR))
{
throw TarsPatchException("local dir '" + _localDir + "' must be a directory");
}
}
void TarsPatch::init(const PatchPrx &patchPrx, const string &remote_dir, const string &local_dir, const bool bCheck)
{
_patchPrx = patchPrx;
_remoteDir = remote_dir;
_localDir = local_dir;
if (bCheck)
{
checkLocalDir();
}
}
void TarsPatch::setRemove(bool bRemove)
{
_remove = bRemove;
}
void TarsPatch::download(const TarsPatchNotifyInterfacePtr &pPtr)
{
//记录下载本次服务的总的时间开始
time_t timeBegin = TNOW;
checkLocalDir();
if (_remove)
{
string tmp = TC_File::simplifyDirectory(_localDir);// + "/" + _remoteDir);
if (pPtr) pPtr->onRemoveLocalDirectory(tmp);
int ret = TC_File::removeFile(tmp, true);
if (ret != 0)
{
throw TarsPatchException("remove '" + tmp + "' error", errno);
}
if (pPtr)
{
pPtr->onRemoveLocalDirectoryOK(tmp);
}
}
if (pPtr)
{
pPtr->onListFile();
}
//获取同步列表
vector<FileInfo> vf;
int ret = -1;
int nRetryTime = 0;
RETRY:
try
{
ret = _patchPrx->listFileInfo(_remoteDir, vf);
}
catch(TarsException& ex)
{
//最多重试三次
if(nRetryTime < 3)
{
nRetryTime++;
goto RETRY;
}
TLOGERROR("TarsPatch::download load error|" << __FILE__ << "," << __LINE__ << "|ex:" << ex.what() << endl);
throw TarsPatchException(ex.what());
}
if (ret != 0 && ret != 1)
{
throw TarsPatchException("listFileInfo error");
}
if (pPtr)
{
pPtr->onListFileOK(vf);
}
if (ret == 0)
{
//path是路径, 对每个文件下载
for (size_t i = 0; i < vf.size(); i++)
{
download(true, vf[i], pPtr);
}
}
else if (ret == 1)
{
//path是文件
download(false, vf[0], pPtr);
}
time_t timeEnd = TNOW;
if (pPtr)
{
pPtr->onDownloadAllOK(vf, timeBegin, timeEnd);
}
}
void TarsPatch::download(bool bDir, const FileInfo &fi, const TarsPatchNotifyInterfacePtr &pPtr)
{
if (pPtr)
{
pPtr->onDownload(fi);
}
//获取本地文件目录
string file_dir = _localDir;
if (bDir)
{
file_dir = tars::TC_File::simplifyDirectory(_localDir + "/" + tars::TC_File::extractFilePath(fi.path));
if (!file_dir.empty())
{
//创建本地目录
if (!tars::TC_File::makeDirRecursive(file_dir))
{
throw TarsPatchException("create directory '" + file_dir + "' failed!");
}
}
else
{
file_dir = ".";
}
}
//本地文件名
string file = tars::TC_File::simplifyDirectory(file_dir + "/" + tars::TC_File::extractFileName(fi.path));
FILE *fp = fopen(file.c_str(), "wb");
if (!fp)
{
throw TarsPatchException("fopen file '" + file + "' error", errno);
}
time_t timeBegin = TNOW;
try
{
//循环下载文件到本地
vector<char> v;
int pos = 0;
while (true)
{
v.clear();
int ret;
int nRetryTime = 0;
RETRY:
try
{
if (bDir)
{
ret = _patchPrx->download(tars::TC_File::simplifyDirectory(_remoteDir + "/" + fi.path), pos, v);
}
else
{
ret = _patchPrx->download(tars::TC_File::simplifyDirectory(_remoteDir), pos, v);
}
}
catch(TarsException& ex)
{
//最多重试两次
if(nRetryTime < 2)
{
nRetryTime++;
goto RETRY;
}
TLOGERROR("TarsPatch::download load error|" << __FILE__ << "," << __LINE__ << "|"<<ex.what()<<endl);
throw TarsPatchException(ex.what());
}
if (ret < 0)
{
throw TarsPatchException("download file '" + file + "' error!");
}
else if (ret == 0)
{
size_t r = fwrite((void*)&v[0], 1, v.size(), fp);
if (r == 0)
{
throw TarsPatchException("fwrite file '" + file + "' error!", errno);
}
pos += r;
if (pPtr) pPtr->onDownloading(fi, pos, file);
}
else if (ret == 1)
{
TLOGERROR("TarsPatch::download load succ|" << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5<< "|" << pos << endl);
break;
}
}
}
catch (...)
{
fclose(fp);
fp = NULL;
TLOGERROR("TarsPatch::download error|" << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5 << endl);
throw;
}
fclose(fp);
if (fi.canExec)
{
int ret = tars::TC_File::setExecutable(file, true);
if (ret == 0)
{
if (pPtr)
{
pPtr->onSetExecutable(fi);
}
}
else
{
throw TarsPatchException("set file '" + file + "' executable error!");
}
}
//检查MD5值
if (!fi.md5.empty())
{
std::string smd5 = tars::TC_MD5::md5file(file);
if (smd5 != fi.md5)
{
TLOGERROR("TarsPatch::download " << __FILE__ << "," << __LINE__ << "|" << fi.path << "|" << fi.md5 << "|" << smd5 << endl);
throw TarsPatchException(fi.path + "'s md5 is not equal to the file in patch server!");;
}
}
time_t timeEnd = TNOW;
if (pPtr)
{
pPtr->onReportTime(fi.path, timeBegin, timeEnd);
}
if (pPtr)
{
pPtr->onDownloadOK(fi, file);
}
}
}