upload.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. #include "EventCode.h"
  14. #include <map>
  15. #else
  16. #include "XZipZilb.h"
  17. #include <regex>
  18. #include <errno.h>
  19. #include "EventCode.h"
  20. #endif // RVC_OS_WIN
  21. using namespace std;
  22. //获取文件夹实际路径
  23. //static ErrorCodeEnum expand_dir(IEntityFunction *pEntityFunc, const char *str, CSimpleStringA &out)
  24. //{
  25. // ErrorCodeEnum Error = Error_Unexpect;
  26. // if (str) {
  27. // const char *p = str;
  28. // const char *sfirst;
  29. // int first = 0;
  30. // char tmp[1024];
  31. // int k = 0;
  32. // while (*p) {
  33. // if (first) {
  34. // if (*p == ')') {
  35. // char key[MAX_PATH];
  36. // key[0] = 0;
  37. // CSimpleStringA strKeyPath;;
  38. // memcpy(key, sfirst, p - sfirst);
  39. // key[p-sfirst] = 0;
  40. // Error = pEntityFunc->GetPath(key, strKeyPath);
  41. // if (Error != Error_Succeed) {
  42. // //Dbg("sys path $(%s)$ cannot evaluate!", key);
  43. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("expand_dir")("sys path $(%s)$ cannot evaluate!", key);
  44. // return Error;
  45. // }
  46. // strcpy(&tmp[k], (LPCSTR)strKeyPath);
  47. // k += strKeyPath.GetLength();
  48. // first = 0;
  49. // }
  50. // } else {
  51. // if (*p == '$' && *(p+1) == '(') {
  52. // p++;
  53. // first = 1;
  54. // sfirst = p+1;
  55. // } else {
  56. // tmp[k++] = *p;
  57. // }
  58. // }
  59. // p++;
  60. // }
  61. // if (k != 0) {
  62. // tmp[k] = 0;
  63. // out = tmp;
  64. // return Error_Succeed;
  65. // }
  66. // } else {
  67. // return Error_Param;
  68. // }
  69. // return Error;
  70. //}
  71. //static upload_dir_t *upload_dir_load(IEntityFunction *pEntityFunc, const char *dir, int default_silent_time, int default_limitation)
  72. //{
  73. // upload_dir_t *updir = ZALLOC_T(upload_dir_t);
  74. // if (updir)
  75. // {
  76. // CSimpleStringA str;
  77. //
  78. // std::map<std::string, std::string> macroPath;
  79. // macroPath["SysLog"] = "$(SysLog)"; //TODO
  80. // macroPath["InterLog"] = "$(InterLog)"; //TODO
  81. // macroPath["Photo"] = "$(UploadPhoto)";
  82. // macroPath["Video"] = "$(UploadVideo)";
  83. // macroPath["SilverlightDebug"] = "$(slv)"; //TODO
  84. // macroPath["Debug"] = "$(dbg)";
  85. //
  86. // std::map<std::string, bool> macroZip;
  87. // macroZip["SysLog"] = true;
  88. // macroZip["InterLog"] = true;
  89. // macroZip["Photo"] = false;
  90. // macroZip["Video"] = false;
  91. // macroZip["SilverlightDebug"] = true;
  92. // macroZip["Debug"] = true;
  93. //
  94. // ErrorCodeEnum Error = Error_Succeed;
  95. // if (macroPath.find(dir) == macroPath.end()) {
  96. // return updir;
  97. // }
  98. // str = macroPath[dir].c_str();
  99. //
  100. // INIT_LIST_HEAD(&updir->candidate_list);
  101. // CSimpleStringA strRealPath;
  102. // Error = expand_dir(pEntityFunc, (LPCSTR)str, strRealPath);
  103. // if (Error == Error_Succeed) {
  104. // INIT_LIST_HEAD(&updir->candidate_list);
  105. // updir->name = _strdup(dir);
  106. // updir->path = _strdup(strRealPath);
  107. //
  108. // if (strcmp(dir, "Video") == 0) {
  109. // str = "$(LocalVideo)";
  110. // updir->flags |= UPLOAD_FLAG_MOVEPATH;
  111. // Error = expand_dir(pEntityFunc, str, strRealPath);
  112. // if (Error != Error_Succeed) {
  113. // return NULL;
  114. // }
  115. // updir->movepath = _strdup(strRealPath);
  116. // } else {
  117. // updir->flags |= UPLOAD_FLAG_AUTODELETE;
  118. // }
  119. // if (macroZip.find(dir) != macroZip.end() && macroZip[dir]) {
  120. // updir->flags |= UPLOAD_FLAG_ZIP;
  121. // }
  122. // if (strcmp("SilverlightDebug", dir) == 0 || strcmp("Debug", dir) == 0) {
  123. // updir->silent_time = 86400;
  124. // } else {
  125. // updir->silent_time = 900;
  126. // }
  127. // updir->child_count_limitation = default_limitation;
  128. // updir->fileCount = 0;//初始化设置为0
  129. // updir->fileLenSum = 0;//初始化设置为0
  130. // }
  131. // }
  132. // return updir;
  133. //}
  134. //初始化要上传的文件夹配置
  135. //int upload_create(struct list_head *list, IEntityFunction *pEntityFunc, CSimpleStringA &checkDir)
  136. //{
  137. // assert(list_empty(list));
  138. // char type_str[1024];
  139. // char *p, *c;
  140. // int default_silent_time = 3600;
  141. // int default_limitation = 256;
  142. // strcpy(type_str, "SysLog,InterLog,Photo,Video,SilverlightDebug,Debug");
  143. // p = strtok_s(type_str, ", ", &c);
  144. // while (p) {
  145. // upload_dir_t *dir = upload_dir_load(pEntityFunc, p, default_silent_time, default_limitation);
  146. // if (!dir)
  147. // {
  148. // return Error_Unexpect;
  149. // }
  150. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("upload_create")("load %s ok", p);
  151. // list_add_tail(&dir->entry, list);
  152. // p = strtok_s(NULL, ", ", &c);
  153. // }
  154. // return 0;
  155. //}
  156. #ifdef RVC_OS_WIN
  157. #define FT_2000_1_1_0_0_0 125911584000000000UL
  158. #else
  159. //表示2000.1.1号time_t的值 0时区
  160. #define FT_2000_1_1_0_0_0 946684800
  161. #endif // RVC_OS_WIN
  162. static int check_zero_ref(const char *path)
  163. {
  164. #ifdef RVC_OS_WIN
  165. HANDLE hFile = CreateFileA(path,
  166. GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // try open
  167. if (hFile != INVALID_HANDLE_VALUE) {
  168. CloseHandle(hFile);
  169. return TRUE;
  170. }
  171. else {
  172. //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
  173. DWORD dwRet = GetLastError();
  174. return FALSE;
  175. }
  176. #else
  177. //后期改为linux真正的文件锁,现在来看,暂时用不着
  178. FILE* fp = fopen(path, "rb");
  179. if (fp != NULL) {
  180. fclose(fp);
  181. return true;
  182. }
  183. else
  184. {
  185. //Dbg("try open file fail :%s",path);
  186. //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
  187. return false;
  188. }
  189. #endif // RVC_OS_WIN
  190. }
  191. #ifdef RVC_OS_WIN
  192. #else
  193. int changeFileAtt(const char* path)
  194. {
  195. struct stat attr_of_del;
  196. if (lstat(path, &attr_of_del) == 0)
  197. {
  198. //修改为775属性
  199. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  200. if (chmod(path, f_attrib) != 0)
  201. {
  202. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  203. return -1;
  204. }
  205. return 0;
  206. }
  207. else {
  208. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  209. return -1;
  210. }
  211. }
  212. long getLinuxFileCTime(const char* fileName)
  213. {
  214. string strPath(fileName);
  215. int lastSplit = strPath.find_last_of(SPLIT_SLASH);
  216. string fileNameStr = strPath.substr(lastSplit + 1, strPath.length() - lastSplit);
  217. if (fileNameStr.length() >= 8)
  218. {
  219. regex e("^[0-9]+$");
  220. if (!std::regex_match(fileNameStr.substr(0,8), e, regex_constants::match_default)) {
  221. Dbg("file name format is wrong ,eg: 20201010***");
  222. return 0;
  223. }
  224. string fileDateStr = fileNameStr.substr(0, 4) + "-" + fileNameStr.substr(4, 2) + "-" + fileNameStr.substr(6, 2) + " 00:00:00";
  225. tm tm_time ;
  226. //Dbg("fileDateStr:%s", fileDateStr.c_str());
  227. strptime(fileDateStr.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time);
  228. tm_time.tm_isdst = -1;
  229. //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);
  230. time_t ft = mktime(&tm_time);
  231. //Dbg("ft=%d", ft);
  232. if (ft == -1) {
  233. Dbg("file [%s] get time_t is -1", fileName);
  234. return 0;
  235. }
  236. else {
  237. long diff = ft - FT_2000_1_1_0_0_0;
  238. return diff;
  239. }
  240. }
  241. else
  242. {
  243. Dbg("file name format is wrong ");
  244. return 0;
  245. }
  246. }
  247. #endif // RVC_OS_WIN