upload.cpp 25 KB

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