upload.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "SpIni.h"
  4. #include "upload.h"
  5. #include <memutil.h>
  6. #include <fileutil.h>
  7. #include <array.h>
  8. #include <string>
  9. #include "sstream"
  10. #ifdef RVC_OS_WIN
  11. #include "XZip.h"
  12. #include <tchar.h>
  13. #else
  14. #include "XZipZilb.h"
  15. #include <regex>
  16. #include <errno.h>
  17. #include "EventCode.h"
  18. #endif // RVC_OS_WIN
  19. using namespace std;
  20. //获取文件夹实际路径
  21. static ErrorCodeEnum expand_dir(IEntityFunction *pEntityFunc, const char *str, CSimpleStringA &out)
  22. {
  23. ErrorCodeEnum Error = Error_Unexpect;
  24. if (str) {
  25. const char *p = str;
  26. const char *sfirst;
  27. int first = 0;
  28. char tmp[1024];
  29. int k = 0;
  30. while (*p) {
  31. if (first) {
  32. if (*p == ')') {
  33. char key[MAX_PATH];
  34. key[0] = 0;
  35. CSimpleStringA strKeyPath;;
  36. memcpy(key, sfirst, p - sfirst);
  37. key[p-sfirst] = 0;
  38. Error = pEntityFunc->GetPath(key, strKeyPath);
  39. if (Error != Error_Succeed) {
  40. Dbg("sys path $(%s)$ cannot evaluate!", key);
  41. return Error;
  42. }
  43. strcpy(&tmp[k], (LPCSTR)strKeyPath);
  44. k += strKeyPath.GetLength();
  45. first = 0;
  46. }
  47. } else {
  48. if (*p == '$' && *(p+1) == '(') {
  49. p++;
  50. first = 1;
  51. sfirst = p+1;
  52. } else {
  53. tmp[k++] = *p;
  54. }
  55. }
  56. p++;
  57. }
  58. if (k != 0) {
  59. tmp[k] = 0;
  60. out = tmp;
  61. return Error_Succeed;
  62. }
  63. } else {
  64. return Error_Param;
  65. }
  66. return Error;
  67. }
  68. static upload_dir_t *upload_dir_load(IEntityFunction *pEntityFunc, IConfigInfo *pConfig, const char *dir, int default_silent_time, int default_limitation)
  69. {
  70. upload_dir_t *updir = ZALLOC_T(upload_dir_t);
  71. if (updir)
  72. {
  73. char tmp[MAX_PATH];
  74. CSimpleStringA str;
  75. ErrorCodeEnum Error = pConfig->ReadConfigValue(dir, "Path", str);
  76. if (Error == Error_Succeed)
  77. {
  78. INIT_LIST_HEAD(&updir->candidate_list);
  79. CSimpleStringA strRealPath;
  80. Error = expand_dir(pEntityFunc, (LPCSTR)str, strRealPath);
  81. if (Error == Error_Succeed)
  82. {
  83. INIT_LIST_HEAD(&updir->candidate_list);
  84. updir->name = _strdup(dir);
  85. updir->path = _strdup(strRealPath);
  86. pConfig->ReadConfigValue(dir, "MovePath", str);
  87. if (str.GetLength() > 0)
  88. {
  89. updir->flags |= UPLOAD_FLAG_MOVEPATH;
  90. Error = expand_dir(pEntityFunc, str, strRealPath);
  91. if (Error != Error_Succeed)
  92. {
  93. return NULL;
  94. }
  95. updir->movepath = _strdup(strRealPath);
  96. } else {
  97. pConfig->ReadConfigValue(dir, "AutoDelete", str);
  98. if (_stricmp(str, "true") == 0)
  99. {
  100. updir->flags |= UPLOAD_FLAG_AUTODELETE;
  101. }
  102. }
  103. pConfig->ReadConfigValue(dir, "Zip", str);
  104. if (_stricmp(str, "true") == 0)
  105. {
  106. updir->flags |= UPLOAD_FLAG_ZIP;
  107. }
  108. pConfig->ReadConfigValueInt(dir, "SilentTime", updir->silent_time);
  109. if (updir->silent_time == 0)
  110. {
  111. updir->silent_time = default_silent_time;
  112. }
  113. pConfig->ReadConfigValueInt(dir, "Limitation", updir->child_count_limitation);
  114. if (updir->child_count_limitation == 0)
  115. {
  116. updir->child_count_limitation = default_limitation;
  117. }
  118. updir->fileCount=0;//初始化设置为0
  119. updir->fileLenSum=0;//初始化设置为0
  120. }
  121. }
  122. }
  123. return updir;
  124. }
  125. static void file_destroy(file_t *file)
  126. {
  127. free(file->name);
  128. free(file->path);
  129. free(file);
  130. }
  131. void upload_file_destroy(file_t *file)
  132. {
  133. file_destroy(file);
  134. }
  135. void updir_del_file(file_t *file)
  136. {
  137. (file->owner->fileCount)--;//文件夹文件个数减1
  138. file->owner->fileLenSum=file->owner->fileLenSum-file->length/1024;//减去文件长度
  139. list_del(&file->entry);//从链表删除文件
  140. upload_file_destroy(file);//删除文件内存
  141. }
  142. //初始化要上传的文件夹配置
  143. int upload_create(struct list_head *list, IEntityFunction *pEntityFunc, IConfigInfo *pConfig, CSimpleStringA &checkDir)
  144. {
  145. assert(list_empty(list));
  146. assert(pConfig);
  147. ErrorCodeEnum Error;
  148. char type_str[1024];
  149. char *p, *c;
  150. int default_silent_time = 0;
  151. int default_limitation = 0;
  152. {
  153. CSimpleStringA str;
  154. Error = pConfig->ReadConfigValue("Main", "Type", str);
  155. if (Error == Error_Succeed)
  156. {
  157. strcpy(type_str, (LPCSTR)str);
  158. }
  159. else
  160. {
  161. return Error;
  162. }
  163. //添加lwt,读入日结时需要检查的文件类型参数
  164. /**交易受限功能代码废弃
  165. Error = pConfig->ReadConfigValue("Main", "CheckType", str);
  166. if (Error == Error_Succeed)
  167. {
  168. checkDir = str;
  169. }
  170. else
  171. {
  172. return Error;
  173. }
  174. */
  175. pConfig->ReadConfigValueInt("Main", "SilentTime", default_silent_time);//间隔时间
  176. pConfig->ReadConfigValueInt("Main", "Limitation", default_limitation);//最大文件数
  177. if (default_limitation == 0)
  178. {
  179. default_limitation = INT_MAX;
  180. }
  181. }
  182. p = strtok_s(type_str, ", ", &c);
  183. while (p) {
  184. upload_dir_t *dir = upload_dir_load(pEntityFunc, pConfig, p, default_silent_time, default_limitation);
  185. if (!dir)
  186. {
  187. return Error_Unexpect;
  188. }
  189. Dbg("load %s ok", p);
  190. list_add_tail(&dir->entry, list);
  191. p = strtok_s(NULL, ", ", &c);
  192. }
  193. return 0;
  194. }
  195. static int updir_exist_file(upload_dir_t *dir, const char *path)
  196. {
  197. file_t *pos;
  198. list_for_each_entry(pos, &dir->candidate_list, file_t, entry) {
  199. if (_stricmp(pos->path, path) == 0)
  200. return TRUE;
  201. }
  202. return false;
  203. }
  204. static int check_zero_ref(const char *path)
  205. {
  206. #ifdef RVC_OS_WIN
  207. HANDLE hFile = CreateFileA(path,
  208. GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // try open
  209. if (hFile != INVALID_HANDLE_VALUE) {
  210. CloseHandle(hFile);
  211. return TRUE;
  212. }
  213. else {
  214. //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
  215. DWORD dwRet = GetLastError();
  216. return FALSE;
  217. }
  218. #else
  219. //后期改为linux真正的文件锁,现在来看,暂时用不着
  220. FILE* fp = fopen(path, "rb");
  221. if (fp != NULL) {
  222. fclose(fp);
  223. return true;
  224. }
  225. else
  226. {
  227. //Dbg("try open file fail :%s",path);
  228. //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
  229. return false;
  230. }
  231. #endif // RVC_OS_WIN
  232. }
  233. #ifdef RVC_OS_WIN
  234. #define FT_2000_1_1_0_0_0 125911584000000000UL
  235. static void updir_add_file(upload_dir_t *dir, const char *path)
  236. {
  237. WIN32_FILE_ATTRIBUTE_DATA attr;
  238. if (GetFileAttributesExA(path, GetFileExInfoStandard, &attr))
  239. {
  240. file_t* f = ZALLOC_T(file_t);
  241. if (f)
  242. {
  243. int offset = strlen(dir->path);
  244. if (dir->path[offset - 1] != '\\')
  245. offset++;
  246. f->path = _strdup(path);
  247. f->name = _strdup(path + offset);
  248. f->owner = dir;
  249. char* p = f->name;
  250. while (p = strchr(p, '\\'))
  251. {
  252. *p = '$';
  253. }
  254. union
  255. {
  256. FILETIME ft;
  257. unsigned __int64 v;
  258. }u;
  259. u.ft.dwHighDateTime = attr.ftCreationTime.dwHighDateTime;
  260. u.ft.dwLowDateTime = attr.ftCreationTime.dwLowDateTime;
  261. f->create_time = (unsigned int)((u.v - FT_2000_1_1_0_0_0) / 10000000UL);
  262. f->length = attr.nFileSizeLow;
  263. if (strcmp(dir->name, "Debug") == 0)
  264. {
  265. string str(f->path);
  266. if (str.substr(str.find_last_of('.') + 1, str.length()) != "zip")
  267. {
  268. if (attr.nFileSizeHigh > 0 || attr.nFileSizeLow > 200 * 1024 * 1024)
  269. {
  270. //删文件
  271. Dbg("file %s name(%s) lenth (nFileSizeHigh=%d,nFileSizeLow=%d) is over 200m,prepare delete!", f->path, f->name, attr.nFileSizeHigh, attr.nFileSizeLow);
  272. WORD dwAttr = GetFileAttributesA(f->path);
  273. dwAttr &= ~FILE_ATTRIBUTE_READONLY;
  274. SetFileAttributesA(f->path, dwAttr);
  275. if (DeleteFileA(f->path)) {
  276. Dbg("file %s name(%s) lenth is over 200m,delete succ!", f->path, f->name);
  277. }
  278. else {
  279. Dbg("file %s name(%s) lenth is over 200m,delete fail!", f->path, f->name);
  280. }
  281. return;
  282. }
  283. string createtime;
  284. ostringstream oss;
  285. oss << f->create_time;
  286. createtime = oss.str();
  287. str = str.substr(0, str.find_last_of('.')) + "&" + createtime + "." + "zip";
  288. if (ZipData((LPCTSTR)str.c_str(), (LPCTSTR)f->path))
  289. {
  290. //delete source file
  291. WORD dwAttr = GetFileAttributesA(f->path);
  292. dwAttr &= ~FILE_ATTRIBUTE_READONLY;
  293. SetFileAttributesA(f->path, dwAttr);
  294. DeleteFileA(f->path);
  295. //insert zip file info
  296. string name(f->name);
  297. name = name.substr(0, name.find_last_of('.') + 1) + "zip";
  298. f->name = _strdup(name.c_str());
  299. f->path = _strdup(str.c_str());
  300. WIN32_FILE_ATTRIBUTE_DATA attr;
  301. if (GetFileAttributesExA(f->path, GetFileExInfoStandard, &attr))
  302. {
  303. f->length = attr.nFileSizeLow;
  304. }
  305. }
  306. }
  307. else
  308. {
  309. string createdata(f->path);
  310. std::size_t found = createdata.find('&');
  311. if (found != std::string::npos)
  312. {
  313. int index0 = createdata.find_last_of('\\') + 1;
  314. int index = createdata.find_last_of('&');
  315. int index2 = createdata.find_last_of('.');
  316. string filetime = createdata.substr(index0, index - index0);
  317. createdata = createdata.substr(index + 1, index2 - index);
  318. string fullname(f->name);
  319. //Dbg("get full name = %s",fullname.c_str());
  320. string name = fullname.substr(0, fullname.find_last_of('$') + 1) + filetime + "." + "zip";
  321. //Dbg("upload name = %s",name.c_str());
  322. f->name = _strdup(name.c_str());
  323. f->create_time = atoi(createdata.c_str());
  324. }
  325. }
  326. }
  327. //把文件按照时间从小到大排序放置
  328. file_t* pos;
  329. list_for_each_entry(pos, &dir->candidate_list, file_t, entry)
  330. {
  331. if (f->create_time < pos->create_time)
  332. {
  333. __list_add(&f->entry, pos->entry.prev, &pos->entry);
  334. (dir->fileCount)++;//文件夹文件个数加1
  335. dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
  336. Dbg("::file %s name(%s) added!", f->path, f->name);
  337. return;
  338. }
  339. }
  340. Dbg("file %s name(%s) added!", f->path, f->name);
  341. list_add_tail(&f->entry, &dir->candidate_list);//把文件加入要上传的列表
  342. (dir->fileCount)++;//文件夹文件个数加1
  343. dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
  344. }
  345. }
  346. }
  347. #else
  348. //#define FT_2000_1_1_0_0_0 946656000
  349. //表示2000.1.1号time_t的值 0时区
  350. #define FT_2000_1_1_0_0_0 946684800
  351. static void updir_add_file(upload_dir_t* dir, const char* path)
  352. {
  353. struct stat attr_of_src;
  354. if (lstat(path, &attr_of_src) == 0)
  355. {
  356. file_t* f = ZALLOC_T(file_t);
  357. if (f)
  358. {
  359. int offset = strlen(dir->path);
  360. if (dir->path[offset - 1] != SPLIT_SLASH)
  361. offset++;
  362. f->path = _strdup(path);
  363. f->name = _strdup(path + offset);
  364. f->owner = dir;
  365. char* p = f->name;
  366. while (p = strchr(p, SPLIT_SLASH))
  367. {
  368. *p = '$';
  369. }
  370. //因linux无法获取创建时间,故采用修改时间获取,对Debug和SilverlightDebug特殊处理
  371. if (strcmp(dir->name, "Debug") == 0 || strcmp(dir->name, "SilverlightDebug") == 0) {
  372. long createTime = getLinuxFileCTime(f->path);
  373. if (createTime == 0) {
  374. Dbg("file %s get create time fail,fileName format is error", f->path);
  375. return;
  376. }
  377. else {
  378. f->create_time = createTime;
  379. }
  380. }
  381. else {
  382. f->create_time = attr_of_src.st_mtime - FT_2000_1_1_0_0_0;
  383. }
  384. f->length = attr_of_src.st_size;
  385. //处理debug文件夹下面的压缩
  386. if (strcmp(dir->name, "Debug") == 0)
  387. {
  388. string str(f->path);
  389. if (str.substr(str.find_last_of('.') + 1, str.length()) != "zip")
  390. {
  391. if (attr_of_src.st_size> 200 * 1024 * 1024)
  392. {
  393. //删文件
  394. Dbg("file %s lenth (%d) is over 200m,prepare delete!", f->path, attr_of_src.st_size);
  395. if (changeFileAtt(f->path)==0){
  396. if (remove(f->path) == 0) {
  397. Dbg("file %s lenth is over 200m,delete succ!", f->path);
  398. }
  399. else {
  400. Dbg("file %s lenth is over 200m,delete fail!", f->path);
  401. }
  402. }
  403. else {
  404. Dbg("file %s lenth is over 200m,change attr fail!", f->path);
  405. }
  406. return;
  407. }
  408. string createtime;
  409. stringstream fmt;
  410. fmt << f->create_time;
  411. createtime = fmt.str();
  412. str = str.substr(0, str.find_last_of('.')) + "&" + createtime + "." + "zip";//新包名
  413. //if (ZipData((LPCTSTR)str.c_str(), (LPCTSTR)f->path))
  414. if( CreateZipFromFile(f->path, str))
  415. {
  416. //delete source file
  417. if (changeFileAtt(f->path) == 0) {
  418. if (remove(f->path)==0) {
  419. Dbg("file %s zip succ,delete succ",f->path);
  420. }
  421. else {
  422. //有可能删除不了旧文件,下次继续上传
  423. Dbg("file %s zip succ,delete fail",f->path);
  424. }
  425. //insert zip file info
  426. string name(f->name);
  427. name = name.substr(0, name.find_last_of('.') + 1) + "zip";
  428. f->name = _strdup(name.c_str());
  429. f->path = _strdup(str.c_str());
  430. //重新计算zip包长度
  431. struct stat attr_of_zip;
  432. if (lstat(f->path, &attr_of_zip) == 0)
  433. {
  434. f->length = attr_of_zip.st_size;
  435. }
  436. else {
  437. Dbg("file %s get attr fail !", f->path);
  438. return;
  439. }
  440. }
  441. else {
  442. Dbg("file %s change attr fail !", f->path);
  443. return;
  444. }
  445. }
  446. else {
  447. Dbg("file %s zipName(%s) zip fail !", f->path, str.c_str());
  448. return;
  449. }
  450. }
  451. else
  452. {
  453. string createdata(f->path);
  454. std::size_t found = createdata.find('&');
  455. if (found != std::string::npos)
  456. {
  457. int index0 = createdata.find_last_of(SPLIT_SLASH) + 1;
  458. int index = createdata.find_last_of('&');
  459. int index2 = createdata.find_last_of('.');
  460. string filetime = createdata.substr(index0, index - index0);
  461. createdata = createdata.substr(index + 1, index2 - index);
  462. string fullname(f->name);
  463. //Dbg("get full name = %s",fullname.c_str());
  464. string name = fullname.substr(0, fullname.find_last_of('$') + 1) + filetime + "." + "zip";
  465. //Dbg("upload name = %s",name.c_str());
  466. f->name = _strdup(name.c_str());
  467. f->create_time = atoi(createdata.c_str());
  468. }
  469. }
  470. }
  471. //把文件按照时间从小到大排序放置
  472. file_t* pos;
  473. list_for_each_entry(pos, &dir->candidate_list, file_t, entry)
  474. {
  475. if (f->create_time < pos->create_time)
  476. {
  477. __list_add(&f->entry, pos->entry.prev, &pos->entry);
  478. (dir->fileCount)++;//文件夹文件个数加1
  479. dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
  480. Dbg("::file %s name(%s) len(%d) creattime(%d) added!", f->path, f->name , f->length ,f->create_time);
  481. return;
  482. }
  483. }
  484. Dbg("file %s name(%s) len(%d) creattime(%d) added!", f->path, f->name, f->length, f->create_time);
  485. list_add_tail(&f->entry, &dir->candidate_list);//把文件加入要上传的列表
  486. (dir->fileCount)++;//文件夹文件个数加1
  487. dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
  488. }
  489. }
  490. }
  491. #endif // RVC_OS_WIN
  492. static void dir_fresh(upload_dir_t *dir, const char *path)
  493. {
  494. array_header_t *arr_files = fileutil_get_sub_files2_a(path, dir->child_count_limitation);
  495. if (arr_files) {
  496. int i;
  497. for (i = 0; i < arr_files->nelts; ++i)
  498. {
  499. char* file_path = ARRAY_IDX(arr_files, i, char*);
  500. #ifdef RVC_OS_WIN
  501. WIN32_FILE_ATTRIBUTE_DATA attr;
  502. if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
  503. {
  504. SYSTEMTIME st;
  505. SYSTEMTIME filest;
  506. FILETIME ft;
  507. FILETIME fttmp;
  508. GetLocalTime(&st);
  509. SystemTimeToFileTime(&st, &ft);
  510. if (CompareFileTime(&ft, &attr.ftLastWriteTime) > 0)
  511. {
  512. //LARGE_INTEGER *p1 = (LARGE_INTEGER *)&attr.ftLastWriteTime;
  513. FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp);
  514. FileTimeToSystemTime(&fttmp, &filest);
  515. LARGE_INTEGER* p2 = (LARGE_INTEGER*)&ft;
  516. LARGE_INTEGER* p1 = (LARGE_INTEGER*)&fttmp;
  517. LONGLONG diff = p2->QuadPart - p1->QuadPart;
  518. diff = diff / 10000000L; // convert to seconds FILETIME是以100纳秒(ns)为单位
  519. //if silent_time == 1 days
  520. if (dir->silent_time >= 86400)
  521. {
  522. int nsecondsInterval = (st.wYear - filest.wYear) * 32140800 + (st.wMonth - filest.wMonth) * 2678400 + (st.wDay - filest.wDay) * 86400;
  523. if ((nsecondsInterval >= dir->silent_time))
  524. {
  525. if (check_zero_ref(file_path))
  526. {
  527. if (!updir_exist_file(dir, file_path))
  528. {
  529. updir_add_file(dir, file_path);
  530. }
  531. }
  532. //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
  533. }
  534. }
  535. else
  536. {
  537. if (diff >= dir->silent_time)
  538. {
  539. if (check_zero_ref(file_path))
  540. {
  541. if (!updir_exist_file(dir, file_path))
  542. {
  543. updir_add_file(dir, file_path);
  544. }
  545. }
  546. //Dbg("addfile %s added! diff:%d", file_path,diff);
  547. }
  548. }
  549. }
  550. else
  551. {
  552. Dbg("the time error!");
  553. }
  554. }
  555. #else
  556. struct stat attr_of_src;
  557. //Dbg("scan dir file %s , %s", dir->name, file_path);
  558. if (lstat(file_path, &attr_of_src) == 0) {
  559. //Dbg("scan file attr %s , %s", dir->name, file_path);
  560. time_t nowTime = time((time_t*)NULL);//当前时间
  561. if (nowTime > attr_of_src.st_mtime)
  562. {
  563. long diff = nowTime - attr_of_src.st_mtime;
  564. if (dir->silent_time >= 86400)
  565. {
  566. tm nowTimeTm = { 0 };
  567. tm fileTimeTm = { 0 };
  568. localtime_r(&nowTime, &nowTimeTm);
  569. localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
  570. int nsecondsInterval = (nowTimeTm.tm_year - fileTimeTm.tm_year) * 32140800 + (nowTimeTm.tm_mon - fileTimeTm.tm_mon) * 2678400 + (nowTimeTm.tm_mday - fileTimeTm.tm_mday) * 86400;
  571. //Dbg("addfile %s added! diff:%d dir->silent_time %d", file_path, nsecondsInterval, dir->silent_time);
  572. //隔夜文件为防止文件一直被框架占用,还需要满足,修改时间超过3个小时才上传
  573. if ((nsecondsInterval >= dir->silent_time)&&(diff>=60*60*3))
  574. {
  575. if (check_zero_ref(file_path))
  576. {
  577. if (!updir_exist_file(dir, file_path))
  578. {
  579. updir_add_file(dir, file_path);
  580. }
  581. }
  582. //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
  583. }
  584. else
  585. {
  586. //对于debug中的zip文件直接上传,不用过滤时间间隔
  587. if (strcmp(dir->name, "Debug") == 0)
  588. {
  589. string str(file_path);
  590. if (str.substr(str.find_last_of('.') + 1, str.length()) == "zip") {
  591. if (check_zero_ref(file_path))
  592. {
  593. if (!updir_exist_file(dir, file_path))
  594. {
  595. updir_add_file(dir, file_path);
  596. }
  597. }
  598. //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
  599. }
  600. }
  601. }
  602. }
  603. else
  604. {
  605. if (diff >= dir->silent_time)
  606. {
  607. if (check_zero_ref(file_path))
  608. {
  609. if (!updir_exist_file(dir, file_path))
  610. {
  611. updir_add_file(dir, file_path);
  612. }
  613. }
  614. //Dbg("addfile %s added! diff:%d", file_path,diff);
  615. }
  616. }
  617. }
  618. else
  619. {
  620. Dbg("the time error!");
  621. }
  622. }
  623. #endif // RVC_OS_WIN
  624. }
  625. toolkit_array_free2(arr_files);
  626. }
  627. array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
  628. if (arr_dir) {
  629. int i;
  630. for (i = 0; i < arr_dir->nelts; ++i) {
  631. char *dir_path = ARRAY_IDX(arr_dir, i, char*);
  632. dir_fresh(dir, dir_path);
  633. }
  634. }
  635. }
  636. int upload_fresh(struct list_head *list)
  637. {
  638. upload_dir_t *pos;
  639. list_for_each_entry(pos, list, upload_dir_t, entry)
  640. {
  641. //Dbg("scan dir %s",pos->path);
  642. dir_fresh(pos, pos->path);
  643. }
  644. return 0;
  645. }
  646. /** 交易受限功能代码废弃
  647. int check_dir_fresh(const char *path,int limitation,int silentTime,int &fileSumlen){
  648. int count=0;//当前文件夹下面的文件个数
  649. int sum = 0;//当前文件夹下面的子文件夹下面的文件个数
  650. array_header_t *arr_files = fileutil_get_sub_files2_a(path, limitation);
  651. if (arr_files) {
  652. int i;
  653. for (i = 0; i < arr_files->nelts; ++i)
  654. {
  655. char *file_path = ARRAY_IDX(arr_files, i, char*);
  656. #ifdef RVC_OS_WIN
  657. WIN32_FILE_ATTRIBUTE_DATA attr;
  658. if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
  659. {
  660. SYSTEMTIME st;
  661. SYSTEMTIME filest;
  662. FILETIME ft;
  663. FILETIME fttmp;
  664. GetLocalTime(&st);
  665. SystemTimeToFileTime(&st, &ft);
  666. if (CompareFileTime(&ft, &attr.ftLastWriteTime) > 0)
  667. {
  668. //LARGE_INTEGER *p1 = (LARGE_INTEGER *)&attr.ftLastWriteTime;
  669. FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp);
  670. FileTimeToSystemTime(&fttmp, &filest);
  671. LARGE_INTEGER* p2 = (LARGE_INTEGER*)&ft;
  672. LARGE_INTEGER* p1 = (LARGE_INTEGER*)&fttmp;
  673. LONGLONG diff = p2->QuadPart - p1->QuadPart;
  674. diff = diff / 10000000L; // convert to seconds FILETIME是以100纳秒(ns)为单位
  675. //增加特殊文件不计入上传处理
  676. char* p = strstr(file_path, "SystemInitial.log");
  677. if (p != NULL) {
  678. continue;
  679. }
  680. p = strstr(file_path, "G_");
  681. if (p != NULL) {
  682. continue;
  683. }
  684. if (silentTime >= 86400)
  685. {
  686. int nsecondsInterval = (st.wYear - filest.wYear) * 32140800 + (st.wMonth - filest.wMonth) * 2678400 + (st.wDay - filest.wDay) * 86400;
  687. if ((nsecondsInterval >= silentTime))
  688. {
  689. //个数加1
  690. count++;
  691. //获取文件长度
  692. WIN32_FILE_ATTRIBUTE_DATA attr;
  693. if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
  694. {
  695. int fileSize = attr.nFileSizeLow / 1024;//单位为k
  696. fileSumlen += fileSize;
  697. }
  698. else {
  699. }
  700. Dbg("checkfile %s ! diff:%d", file_path, nsecondsInterval);
  701. }
  702. }
  703. else
  704. {
  705. if (diff >= silentTime)
  706. {
  707. //个数加1
  708. count++;
  709. //获取文件长度
  710. WIN32_FILE_ATTRIBUTE_DATA attr;
  711. if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
  712. {
  713. int fileSize = attr.nFileSizeLow / 1024;//单位为k
  714. fileSumlen += fileSize;
  715. }
  716. else {
  717. }
  718. Dbg("checkfile %s ! diff:%d", file_path, diff);
  719. }
  720. }
  721. }
  722. else
  723. {
  724. Dbg("the time error!");
  725. }
  726. }
  727. #else
  728. struct stat attr_of_src;
  729. if (lstat(file_path, &attr_of_src) == 0)
  730. {
  731. time_t nowTime = time((time_t*)NULL);//当前时间
  732. if (nowTime > attr_of_src.st_mtime)
  733. {
  734. long diff = nowTime - attr_of_src.st_mtime;
  735. //增加特殊文件不计入上传处理
  736. char* p = strstr(file_path, "SystemInitial.log");
  737. if (p != NULL) {
  738. continue;
  739. }
  740. p = strstr(file_path, "G_");
  741. if (p != NULL) {
  742. continue;
  743. }
  744. if (silentTime >= 86400)
  745. {
  746. tm nowTimeTm = { 0 };
  747. tm fileTimeTm = { 0 };
  748. localtime_r(&nowTime, &nowTimeTm);
  749. localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
  750. int nsecondsInterval = (nowTimeTm.tm_year - fileTimeTm.tm_year) * 32140800 + (nowTimeTm.tm_mon - fileTimeTm.tm_mon) * 2678400 + (nowTimeTm.tm_mday - fileTimeTm.tm_mday) * 86400;
  751. if ((nsecondsInterval >= silentTime))
  752. {
  753. //个数加1
  754. count++;
  755. //获取文件长度
  756. int fileSize = attr_of_src.st_size / 1024;//单位为k
  757. fileSumlen += fileSize;
  758. Dbg("checkfile %s ! diff:%d", file_path, nsecondsInterval);
  759. }
  760. }
  761. else
  762. {
  763. if (diff >= silentTime)
  764. {
  765. //个数加1
  766. count++;
  767. //获取文件长度
  768. int fileSize = attr_of_src.st_size / 1024;//单位为k
  769. fileSumlen += fileSize;
  770. Dbg("checkfile %s ! diff:%d", file_path, diff);
  771. }
  772. }
  773. }
  774. else
  775. {
  776. Dbg("the time error!");
  777. }
  778. }
  779. #endif // RVC_OS_WIN
  780. }
  781. toolkit_array_free2(arr_files);
  782. }
  783. array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
  784. if (arr_dir) {
  785. int i;
  786. for (i = 0; i < arr_dir->nelts; ++i) {
  787. char *dir_path = ARRAY_IDX(arr_dir, i, char*);
  788. int fileLen=0;
  789. sum += check_dir_fresh(dir_path,limitation,silentTime,fileLen);
  790. fileSumlen=fileSumlen+fileLen;
  791. }
  792. }
  793. count = sum+count;
  794. return count;
  795. }
  796. */
  797. #ifdef RVC_OS_WIN
  798. bool ZipData(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
  799. {
  800. BOOL bResult = TRUE;
  801. if (!lpszZipArchive)
  802. {
  803. Dbg("lpszZipArchive is NULL");
  804. return false;
  805. }
  806. if (!lpszSrcFile)
  807. {
  808. Dbg("lpszSrcFile is NULL");
  809. return false;
  810. }
  811. // does zip source file exist?
  812. //if (_waccess((wchar_t *)lpszSrcFile, 0) == -1)
  813. //{
  814. // Dbg("WARNING: zip source file '%s' cannot be found,operation aborted",lpszSrcFile);
  815. // return false;
  816. //}
  817. // use only the file name for zip file entry
  818. TCHAR* cp = (TCHAR*)_tcsrchr(lpszSrcFile, _T('\\'));
  819. if (cp == NULL)
  820. cp = (TCHAR*)lpszSrcFile;
  821. else
  822. cp++;
  823. HZIP hz = CreateZip((void*)lpszZipArchive, 0, ZIP_FILENAME);
  824. if (hz)
  825. {
  826. ZRESULT zr = ZipAdd(hz, cp, (void*)lpszSrcFile, 0, ZIP_FILENAME);
  827. CloseZip(hz);
  828. // did add work?
  829. if (zr == ZR_OK)
  830. {
  831. //Dbg("added '%s' to zip file '%s'",lpszSrcFile, lpszZipArchive);
  832. bResult = true;
  833. }
  834. else
  835. {
  836. Dbg("WARNING: failed to add zip source file '%s'", lpszSrcFile);
  837. bResult = false;
  838. }
  839. }
  840. else
  841. {
  842. Dbg("ERROR: failed to create zip file '%s'", lpszZipArchive);
  843. bResult = false;
  844. }
  845. return bResult;
  846. }
  847. #else
  848. unsigned long GetTickCount()
  849. {
  850. struct timespec ts;
  851. clock_gettime(CLOCK_MONOTONIC, &ts);
  852. return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
  853. }
  854. int changeFileAtt(const char* path)
  855. {
  856. struct stat attr_of_del;
  857. if (lstat(path, &attr_of_del) == 0)
  858. {
  859. //修改为775属性
  860. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  861. if (chmod(path, f_attrib) != 0)
  862. {
  863. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  864. return -1;
  865. }
  866. return 0;
  867. }
  868. else {
  869. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  870. return -1;
  871. }
  872. }
  873. long getLinuxFileCTime(const char* fileName)
  874. {
  875. string strPath(fileName);
  876. int lastSplit = strPath.find_last_of(SPLIT_SLASH);
  877. string fileNameStr = strPath.substr(lastSplit + 1, strPath.length() - lastSplit);
  878. if (fileNameStr.length() >= 8)
  879. {
  880. regex e("^[0-9]+$");
  881. if (!std::regex_match(fileNameStr.substr(0,8), e, regex_constants::match_default)) {
  882. Dbg("file name format is wrong ,eg: 20201010***");
  883. return 0;
  884. }
  885. string fileDateStr = fileNameStr.substr(0, 4) + "-" + fileNameStr.substr(4, 2) + "-" + fileNameStr.substr(6, 2) + " 00:00:00";
  886. tm tm_time ;
  887. //Dbg("fileDateStr:%s", fileDateStr.c_str());
  888. strptime(fileDateStr.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time);
  889. tm_time.tm_isdst = -1;
  890. //Dbg("tm_time %d%d%d %d:%d:%d", tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday, tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
  891. time_t ft = mktime(&tm_time);
  892. //Dbg("ft=%d", ft);
  893. if (ft == -1) {
  894. Dbg("file [%s] get time_t is -1", fileName);
  895. return 0;
  896. }
  897. else {
  898. long diff = ft - FT_2000_1_1_0_0_0;
  899. return diff;
  900. }
  901. }
  902. else
  903. {
  904. Dbg("file name format is wrong ");
  905. return 0;
  906. }
  907. }
  908. #endif // RVC_OS_WIN