123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "SpIni.h"
- #include "upload.h"
- #include <memutil.h>
- #include <fileutil.h>
- #include <array.h>
- #include <string>
- #include "sstream"
- #ifdef RVC_OS_WIN
- #include "XZip.h"
- #include <tchar.h>
- #include "EventCode.h"
- #include <map>
- #else
- #include "XZipZilb.h"
- #include <regex>
- #include <errno.h>
- #include "EventCode.h"
- #endif // RVC_OS_WIN
- using namespace std;
- //获取文件夹实际路径
- //static ErrorCodeEnum expand_dir(IEntityFunction *pEntityFunc, const char *str, CSimpleStringA &out)
- //{
- // ErrorCodeEnum Error = Error_Unexpect;
- // if (str) {
- // const char *p = str;
- // const char *sfirst;
- // int first = 0;
- // char tmp[1024];
- // int k = 0;
- // while (*p) {
- // if (first) {
- // if (*p == ')') {
- // char key[MAX_PATH];
- // key[0] = 0;
- // CSimpleStringA strKeyPath;;
- // memcpy(key, sfirst, p - sfirst);
- // key[p-sfirst] = 0;
- // Error = pEntityFunc->GetPath(key, strKeyPath);
- // if (Error != Error_Succeed) {
- // //Dbg("sys path $(%s)$ cannot evaluate!", key);
- // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("expand_dir")("sys path $(%s)$ cannot evaluate!", key);
- // return Error;
- // }
- // strcpy(&tmp[k], (LPCSTR)strKeyPath);
- // k += strKeyPath.GetLength();
- // first = 0;
- // }
- // } else {
- // if (*p == '$' && *(p+1) == '(') {
- // p++;
- // first = 1;
- // sfirst = p+1;
- // } else {
- // tmp[k++] = *p;
- // }
- // }
- // p++;
- // }
- // if (k != 0) {
- // tmp[k] = 0;
- // out = tmp;
- // return Error_Succeed;
- // }
- // } else {
- // return Error_Param;
- // }
- // return Error;
- //}
- //static upload_dir_t *upload_dir_load(IEntityFunction *pEntityFunc, const char *dir, int default_silent_time, int default_limitation)
- //{
- // upload_dir_t *updir = ZALLOC_T(upload_dir_t);
- // if (updir)
- // {
- // CSimpleStringA str;
- //
- // std::map<std::string, std::string> macroPath;
- // macroPath["SysLog"] = "$(SysLog)"; //TODO
- // macroPath["InterLog"] = "$(InterLog)"; //TODO
- // macroPath["Photo"] = "$(UploadPhoto)";
- // macroPath["Video"] = "$(UploadVideo)";
- // macroPath["SilverlightDebug"] = "$(slv)"; //TODO
- // macroPath["Debug"] = "$(dbg)";
- //
- // std::map<std::string, bool> macroZip;
- // macroZip["SysLog"] = true;
- // macroZip["InterLog"] = true;
- // macroZip["Photo"] = false;
- // macroZip["Video"] = false;
- // macroZip["SilverlightDebug"] = true;
- // macroZip["Debug"] = true;
- //
- // ErrorCodeEnum Error = Error_Succeed;
- // if (macroPath.find(dir) == macroPath.end()) {
- // return updir;
- // }
- // str = macroPath[dir].c_str();
- //
- // INIT_LIST_HEAD(&updir->candidate_list);
- // CSimpleStringA strRealPath;
- // Error = expand_dir(pEntityFunc, (LPCSTR)str, strRealPath);
- // if (Error == Error_Succeed) {
- // INIT_LIST_HEAD(&updir->candidate_list);
- // updir->name = _strdup(dir);
- // updir->path = _strdup(strRealPath);
- //
- // if (strcmp(dir, "Video") == 0) {
- // str = "$(LocalVideo)";
- // updir->flags |= UPLOAD_FLAG_MOVEPATH;
- // Error = expand_dir(pEntityFunc, str, strRealPath);
- // if (Error != Error_Succeed) {
- // return NULL;
- // }
- // updir->movepath = _strdup(strRealPath);
- // } else {
- // updir->flags |= UPLOAD_FLAG_AUTODELETE;
- // }
- // if (macroZip.find(dir) != macroZip.end() && macroZip[dir]) {
- // updir->flags |= UPLOAD_FLAG_ZIP;
- // }
- // if (strcmp("SilverlightDebug", dir) == 0 || strcmp("Debug", dir) == 0) {
- // updir->silent_time = 86400;
- // } else {
- // updir->silent_time = 900;
- // }
- // updir->child_count_limitation = default_limitation;
- // updir->fileCount = 0;//初始化设置为0
- // updir->fileLenSum = 0;//初始化设置为0
- // }
- // }
- // return updir;
- //}
- //初始化要上传的文件夹配置
- //int upload_create(struct list_head *list, IEntityFunction *pEntityFunc, CSimpleStringA &checkDir)
- //{
- // assert(list_empty(list));
- // char type_str[1024];
- // char *p, *c;
- // int default_silent_time = 3600;
- // int default_limitation = 256;
- // strcpy(type_str, "SysLog,InterLog,Photo,Video,SilverlightDebug,Debug");
- // p = strtok_s(type_str, ", ", &c);
- // while (p) {
- // upload_dir_t *dir = upload_dir_load(pEntityFunc, p, default_silent_time, default_limitation);
- // if (!dir)
- // {
- // return Error_Unexpect;
- // }
- // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("upload_create")("load %s ok", p);
- // list_add_tail(&dir->entry, list);
- // p = strtok_s(NULL, ", ", &c);
- // }
- // return 0;
- //}
- #ifdef RVC_OS_WIN
- #define FT_2000_1_1_0_0_0 125911584000000000UL
- #else
- //表示2000.1.1号time_t的值 0时区
- #define FT_2000_1_1_0_0_0 946684800
- #endif // RVC_OS_WIN
- static int check_zero_ref(const char *path)
- {
- #ifdef RVC_OS_WIN
- HANDLE hFile = CreateFileA(path,
- GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); // try open
- if (hFile != INVALID_HANDLE_VALUE) {
- CloseHandle(hFile);
- return TRUE;
- }
- else {
- //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
- DWORD dwRet = GetLastError();
- return FALSE;
- }
- #else
- //后期改为linux真正的文件锁,现在来看,暂时用不着
- FILE* fp = fopen(path, "rb");
- if (fp != NULL) {
- fclose(fp);
- return true;
- }
- else
- {
- //Dbg("try open file fail :%s",path);
- //LogWarn(Severity_Low, Error_Unexpect, LOG_WARN_UPLOAD_SCANFILE_LOCK_ERROR, CSimpleStringA::Format("check_zero_ref is fail:%s",path).GetData());
- return false;
- }
- #endif // RVC_OS_WIN
- }
-
- #ifdef RVC_OS_WIN
- #else
- int changeFileAtt(const char* path)
- {
- struct stat attr_of_del;
- if (lstat(path, &attr_of_del) == 0)
- {
- //修改为775属性
- mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
- if (chmod(path, f_attrib) != 0)
- {
- Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
- return -1;
- }
- return 0;
- }
- else {
- Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
- return -1;
- }
- }
- long getLinuxFileCTime(const char* fileName)
- {
- string strPath(fileName);
- int lastSplit = strPath.find_last_of(SPLIT_SLASH);
- string fileNameStr = strPath.substr(lastSplit + 1, strPath.length() - lastSplit);
- if (fileNameStr.length() >= 8)
- {
- regex e("^[0-9]+$");
- if (!std::regex_match(fileNameStr.substr(0,8), e, regex_constants::match_default)) {
- Dbg("file name format is wrong ,eg: 20201010***");
- return 0;
- }
- string fileDateStr = fileNameStr.substr(0, 4) + "-" + fileNameStr.substr(4, 2) + "-" + fileNameStr.substr(6, 2) + " 00:00:00";
- tm tm_time ;
- //Dbg("fileDateStr:%s", fileDateStr.c_str());
- strptime(fileDateStr.c_str(), "%Y-%m-%d %H:%M:%S", &tm_time);
- tm_time.tm_isdst = -1;
- //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);
- time_t ft = mktime(&tm_time);
- //Dbg("ft=%d", ft);
- if (ft == -1) {
- Dbg("file [%s] get time_t is -1", fileName);
- return 0;
- }
- else {
- long diff = ft - FT_2000_1_1_0_0_0;
- return diff;
- }
- }
- else
- {
- Dbg("file name format is wrong ");
- return 0;
- }
- }
- #endif // RVC_OS_WIN
|