123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- #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>
- #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);
- 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, IConfigInfo *pConfig, const char *dir, int default_silent_time, int default_limitation)
- {
- upload_dir_t *updir = ZALLOC_T(upload_dir_t);
- if (updir)
- {
- char tmp[MAX_PATH];
- CSimpleStringA str;
- ErrorCodeEnum Error = pConfig->ReadConfigValue(dir, "Path", str);
- if (Error == Error_Succeed)
- {
- 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);
- pConfig->ReadConfigValue(dir, "MovePath", str);
- if (str.GetLength() > 0)
- {
- updir->flags |= UPLOAD_FLAG_MOVEPATH;
- Error = expand_dir(pEntityFunc, str, strRealPath);
- if (Error != Error_Succeed)
- {
- return NULL;
- }
- updir->movepath = _strdup(strRealPath);
- } else {
- pConfig->ReadConfigValue(dir, "AutoDelete", str);
- if (_stricmp(str, "true") == 0)
- {
- updir->flags |= UPLOAD_FLAG_AUTODELETE;
- }
- }
- pConfig->ReadConfigValue(dir, "Zip", str);
- if (_stricmp(str, "true") == 0)
- {
- updir->flags |= UPLOAD_FLAG_ZIP;
- }
- pConfig->ReadConfigValueInt(dir, "SilentTime", updir->silent_time);
- if (updir->silent_time == 0)
- {
- updir->silent_time = default_silent_time;
- }
- pConfig->ReadConfigValueInt(dir, "Limitation", updir->child_count_limitation);
- if (updir->child_count_limitation == 0)
- {
- updir->child_count_limitation = default_limitation;
- }
- updir->fileCount=0;//初始化设置为0
- updir->fileLenSum=0;//初始化设置为0
- }
- }
- }
- return updir;
- }
- static void file_destroy(file_t *file)
- {
- free(file->name);
- free(file->path);
- free(file);
- }
- void upload_file_destroy(file_t *file)
- {
- file_destroy(file);
- }
- void updir_del_file(file_t *file)
- {
- (file->owner->fileCount)--;//文件夹文件个数减1
- file->owner->fileLenSum=file->owner->fileLenSum-file->length/1024;//减去文件长度
- list_del(&file->entry);//从链表删除文件
- upload_file_destroy(file);//删除文件内存
- }
- //初始化要上传的文件夹配置
- int upload_create(struct list_head *list, IEntityFunction *pEntityFunc, IConfigInfo *pConfig, CSimpleStringA &checkDir)
- {
- assert(list_empty(list));
- assert(pConfig);
- ErrorCodeEnum Error;
- char type_str[1024];
- char *p, *c;
- int default_silent_time = 0;
- int default_limitation = 0;
- {
- CSimpleStringA str;
- Error = pConfig->ReadConfigValue("Main", "Type", str);
- if (Error == Error_Succeed)
- {
- strcpy(type_str, (LPCSTR)str);
- }
- else
- {
- return Error;
- }
- //添加lwt,读入日结时需要检查的文件类型参数
- /**交易受限功能代码废弃
- Error = pConfig->ReadConfigValue("Main", "CheckType", str);
- if (Error == Error_Succeed)
- {
- checkDir = str;
- }
- else
- {
- return Error;
- }
- */
- pConfig->ReadConfigValueInt("Main", "SilentTime", default_silent_time);//间隔时间
- pConfig->ReadConfigValueInt("Main", "Limitation", default_limitation);//最大文件数
- if (default_limitation == 0)
- {
- default_limitation = INT_MAX;
- }
- }
- p = strtok_s(type_str, ", ", &c);
-
- while (p) {
- upload_dir_t *dir = upload_dir_load(pEntityFunc, pConfig, p, default_silent_time, default_limitation);
- if (!dir)
- {
- return Error_Unexpect;
- }
- Dbg("load %s ok", p);
- list_add_tail(&dir->entry, list);
- p = strtok_s(NULL, ", ", &c);
- }
- return 0;
- }
- static int updir_exist_file(upload_dir_t *dir, const char *path)
- {
- file_t *pos;
- list_for_each_entry(pos, &dir->candidate_list, file_t, entry) {
- if (_stricmp(pos->path, path) == 0)
- return TRUE;
- }
- return false;
- }
- 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
- #define FT_2000_1_1_0_0_0 125911584000000000UL
- static void updir_add_file(upload_dir_t *dir, const char *path)
- {
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(path, GetFileExInfoStandard, &attr))
- {
- file_t* f = ZALLOC_T(file_t);
- if (f)
- {
- int offset = strlen(dir->path);
- if (dir->path[offset - 1] != '\\')
- offset++;
- f->path = _strdup(path);
- f->name = _strdup(path + offset);
- f->owner = dir;
- char* p = f->name;
- while (p = strchr(p, '\\'))
- {
- *p = '$';
- }
- union
- {
- FILETIME ft;
- unsigned __int64 v;
- }u;
- u.ft.dwHighDateTime = attr.ftCreationTime.dwHighDateTime;
- u.ft.dwLowDateTime = attr.ftCreationTime.dwLowDateTime;
- f->create_time = (unsigned int)((u.v - FT_2000_1_1_0_0_0) / 10000000UL);
- f->length = attr.nFileSizeLow;
- if (strcmp(dir->name, "Debug") == 0)
- {
- string str(f->path);
- if (str.substr(str.find_last_of('.') + 1, str.length()) != "zip")
- {
- if (attr.nFileSizeHigh > 0 || attr.nFileSizeLow > 200 * 1024 * 1024)
- {
- //删文件
- Dbg("file %s name(%s) lenth (nFileSizeHigh=%d,nFileSizeLow=%d) is over 200m,prepare delete!", f->path, f->name, attr.nFileSizeHigh, attr.nFileSizeLow);
- WORD dwAttr = GetFileAttributesA(f->path);
- dwAttr &= ~FILE_ATTRIBUTE_READONLY;
- SetFileAttributesA(f->path, dwAttr);
- if (DeleteFileA(f->path)) {
- Dbg("file %s name(%s) lenth is over 200m,delete succ!", f->path, f->name);
- }
- else {
- Dbg("file %s name(%s) lenth is over 200m,delete fail!", f->path, f->name);
- }
-
- return;
- }
- string createtime;
- ostringstream oss;
- oss << f->create_time;
- createtime = oss.str();
- str = str.substr(0, str.find_last_of('.')) + "&" + createtime + "." + "zip";
- if (ZipData((LPCTSTR)str.c_str(), (LPCTSTR)f->path))
- {
- //delete source file
- WORD dwAttr = GetFileAttributesA(f->path);
- dwAttr &= ~FILE_ATTRIBUTE_READONLY;
- SetFileAttributesA(f->path, dwAttr);
- DeleteFileA(f->path);
- //insert zip file info
- string name(f->name);
- name = name.substr(0, name.find_last_of('.') + 1) + "zip";
- f->name = _strdup(name.c_str());
- f->path = _strdup(str.c_str());
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(f->path, GetFileExInfoStandard, &attr))
- {
- f->length = attr.nFileSizeLow;
- }
- }
- }
- else
- {
- string createdata(f->path);
- std::size_t found = createdata.find('&');
- if (found != std::string::npos)
- {
- int index0 = createdata.find_last_of('\\') + 1;
- int index = createdata.find_last_of('&');
- int index2 = createdata.find_last_of('.');
- string filetime = createdata.substr(index0, index - index0);
- createdata = createdata.substr(index + 1, index2 - index);
- string fullname(f->name);
- //Dbg("get full name = %s",fullname.c_str());
- string name = fullname.substr(0, fullname.find_last_of('$') + 1) + filetime + "." + "zip";
- //Dbg("upload name = %s",name.c_str());
- f->name = _strdup(name.c_str());
- f->create_time = atoi(createdata.c_str());
- }
- }
- }
- //把文件按照时间从小到大排序放置
- file_t* pos;
- list_for_each_entry(pos, &dir->candidate_list, file_t, entry)
- {
- if (f->create_time < pos->create_time)
- {
- __list_add(&f->entry, pos->entry.prev, &pos->entry);
- (dir->fileCount)++;//文件夹文件个数加1
- dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
- Dbg("::file %s name(%s) added!", f->path, f->name);
- return;
- }
- }
- Dbg("file %s name(%s) added!", f->path, f->name);
- list_add_tail(&f->entry, &dir->candidate_list);//把文件加入要上传的列表
- (dir->fileCount)++;//文件夹文件个数加1
- dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
- }
- }
-
- }
- #else
- //#define FT_2000_1_1_0_0_0 946656000
- //表示2000.1.1号time_t的值 0时区
- #define FT_2000_1_1_0_0_0 946684800
- static void updir_add_file(upload_dir_t* dir, const char* path)
- {
- struct stat attr_of_src;
- if (lstat(path, &attr_of_src) == 0)
- {
- file_t* f = ZALLOC_T(file_t);
- if (f)
- {
- int offset = strlen(dir->path);
- if (dir->path[offset - 1] != SPLIT_SLASH)
- offset++;
- f->path = _strdup(path);
- f->name = _strdup(path + offset);
- f->owner = dir;
- char* p = f->name;
- while (p = strchr(p, SPLIT_SLASH))
- {
- *p = '$';
- }
- //因linux无法获取创建时间,故采用修改时间获取,对Debug和SilverlightDebug特殊处理
- if (strcmp(dir->name, "Debug") == 0 || strcmp(dir->name, "SilverlightDebug") == 0) {
- long createTime = getLinuxFileCTime(f->path);
- if (createTime == 0) {
- Dbg("file %s get create time fail,fileName format is error", f->path);
- return;
- }
- else {
- f->create_time = createTime;
- }
- }
- else {
- f->create_time = attr_of_src.st_mtime - FT_2000_1_1_0_0_0;
- }
- f->length = attr_of_src.st_size;
- //处理debug文件夹下面的压缩
- if (strcmp(dir->name, "Debug") == 0)
- {
- string str(f->path);
- if (str.substr(str.find_last_of('.') + 1, str.length()) != "zip")
- {
- if (attr_of_src.st_size> 200 * 1024 * 1024)
- {
- //删文件
- Dbg("file %s lenth (%d) is over 200m,prepare delete!", f->path, attr_of_src.st_size);
-
- if (changeFileAtt(f->path)==0){
- if (remove(f->path) == 0) {
- Dbg("file %s lenth is over 200m,delete succ!", f->path);
- }
- else {
- Dbg("file %s lenth is over 200m,delete fail!", f->path);
- }
- }
- else {
- Dbg("file %s lenth is over 200m,change attr fail!", f->path);
- }
- return;
- }
- string createtime;
- stringstream fmt;
- fmt << f->create_time;
- createtime = fmt.str();
- str = str.substr(0, str.find_last_of('.')) + "&" + createtime + "." + "zip";//新包名
- //if (ZipData((LPCTSTR)str.c_str(), (LPCTSTR)f->path))
- if( CreateZipFromFile(f->path, str))
- {
- //delete source file
- if (changeFileAtt(f->path) == 0) {
- if (remove(f->path)==0) {
- Dbg("file %s zip succ,delete succ",f->path);
- }
- else {
- //有可能删除不了旧文件,下次继续上传
- Dbg("file %s zip succ,delete fail",f->path);
- }
- //insert zip file info
- string name(f->name);
- name = name.substr(0, name.find_last_of('.') + 1) + "zip";
- f->name = _strdup(name.c_str());
- f->path = _strdup(str.c_str());
- //重新计算zip包长度
- struct stat attr_of_zip;
- if (lstat(f->path, &attr_of_zip) == 0)
- {
- f->length = attr_of_zip.st_size;
- }
- else {
- Dbg("file %s get attr fail !", f->path);
- return;
- }
- }
- else {
- Dbg("file %s change attr fail !", f->path);
- return;
- }
-
- }
- else {
- Dbg("file %s zipName(%s) zip fail !", f->path, str.c_str());
- return;
- }
- }
- else
- {
- string createdata(f->path);
- std::size_t found = createdata.find('&');
- if (found != std::string::npos)
- {
- int index0 = createdata.find_last_of(SPLIT_SLASH) + 1;
- int index = createdata.find_last_of('&');
- int index2 = createdata.find_last_of('.');
- string filetime = createdata.substr(index0, index - index0);
- createdata = createdata.substr(index + 1, index2 - index);
- string fullname(f->name);
- //Dbg("get full name = %s",fullname.c_str());
- string name = fullname.substr(0, fullname.find_last_of('$') + 1) + filetime + "." + "zip";
- //Dbg("upload name = %s",name.c_str());
- f->name = _strdup(name.c_str());
- f->create_time = atoi(createdata.c_str());
- }
- }
- }
- //把文件按照时间从小到大排序放置
- file_t* pos;
- list_for_each_entry(pos, &dir->candidate_list, file_t, entry)
- {
- if (f->create_time < pos->create_time)
- {
- __list_add(&f->entry, pos->entry.prev, &pos->entry);
- (dir->fileCount)++;//文件夹文件个数加1
- dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
- Dbg("::file %s name(%s) len(%d) creattime(%d) added!", f->path, f->name , f->length ,f->create_time);
- return;
- }
- }
- Dbg("file %s name(%s) len(%d) creattime(%d) added!", f->path, f->name, f->length, f->create_time);
- list_add_tail(&f->entry, &dir->candidate_list);//把文件加入要上传的列表
- (dir->fileCount)++;//文件夹文件个数加1
- dir->fileLenSum = dir->fileLenSum + f->length / 1024; //统计传送文件长度
-
- }
- }
- }
- #endif // RVC_OS_WIN
-
- static void dir_fresh(upload_dir_t *dir, const char *path)
- {
- array_header_t *arr_files = fileutil_get_sub_files2_a(path, dir->child_count_limitation);
- if (arr_files) {
- int i;
- for (i = 0; i < arr_files->nelts; ++i)
- {
- char* file_path = ARRAY_IDX(arr_files, i, char*);
- #ifdef RVC_OS_WIN
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
- {
- SYSTEMTIME st;
- SYSTEMTIME filest;
- FILETIME ft;
- FILETIME fttmp;
- GetLocalTime(&st);
- SystemTimeToFileTime(&st, &ft);
- if (CompareFileTime(&ft, &attr.ftLastWriteTime) > 0)
- {
- //LARGE_INTEGER *p1 = (LARGE_INTEGER *)&attr.ftLastWriteTime;
- FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp);
- FileTimeToSystemTime(&fttmp, &filest);
- LARGE_INTEGER* p2 = (LARGE_INTEGER*)&ft;
- LARGE_INTEGER* p1 = (LARGE_INTEGER*)&fttmp;
- LONGLONG diff = p2->QuadPart - p1->QuadPart;
- diff = diff / 10000000L; // convert to seconds FILETIME是以100纳秒(ns)为单位
- //if silent_time == 1 days
- if (dir->silent_time >= 86400)
- {
- int nsecondsInterval = (st.wYear - filest.wYear) * 32140800 + (st.wMonth - filest.wMonth) * 2678400 + (st.wDay - filest.wDay) * 86400;
- if ((nsecondsInterval >= dir->silent_time))
- {
- if (check_zero_ref(file_path))
- {
- if (!updir_exist_file(dir, file_path))
- {
- updir_add_file(dir, file_path);
- }
- }
- //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
- }
- }
- else
- {
- if (diff >= dir->silent_time)
- {
- if (check_zero_ref(file_path))
- {
- if (!updir_exist_file(dir, file_path))
- {
- updir_add_file(dir, file_path);
- }
- }
- //Dbg("addfile %s added! diff:%d", file_path,diff);
- }
- }
- }
- else
- {
- Dbg("the time error!");
- }
- }
- #else
- struct stat attr_of_src;
- //Dbg("scan dir file %s , %s", dir->name, file_path);
- if (lstat(file_path, &attr_of_src) == 0) {
- //Dbg("scan file attr %s , %s", dir->name, file_path);
- time_t nowTime = time((time_t*)NULL);//当前时间
- if (nowTime > attr_of_src.st_mtime)
- {
- long diff = nowTime - attr_of_src.st_mtime;
- if (dir->silent_time >= 86400)
- {
- tm nowTimeTm = { 0 };
- tm fileTimeTm = { 0 };
- localtime_r(&nowTime, &nowTimeTm);
- localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
- int nsecondsInterval = (nowTimeTm.tm_year - fileTimeTm.tm_year) * 32140800 + (nowTimeTm.tm_mon - fileTimeTm.tm_mon) * 2678400 + (nowTimeTm.tm_mday - fileTimeTm.tm_mday) * 86400;
- //Dbg("addfile %s added! diff:%d dir->silent_time %d", file_path, nsecondsInterval, dir->silent_time);
- //隔夜文件为防止文件一直被框架占用,还需要满足,修改时间超过3个小时才上传
-
- if ((nsecondsInterval >= dir->silent_time)&&(diff>=60*60*3))
- {
- if (check_zero_ref(file_path))
- {
- if (!updir_exist_file(dir, file_path))
- {
-
- updir_add_file(dir, file_path);
- }
- }
- //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
- }
- else
- {
- //对于debug中的zip文件直接上传,不用过滤时间间隔
- if (strcmp(dir->name, "Debug") == 0)
- {
- string str(file_path);
- if (str.substr(str.find_last_of('.') + 1, str.length()) == "zip") {
- if (check_zero_ref(file_path))
- {
- if (!updir_exist_file(dir, file_path))
- {
- updir_add_file(dir, file_path);
- }
- }
- //Dbg("addfile %s added! diff:%d", file_path,nsecondsInterval);
- }
- }
- }
- }
- else
- {
- if (diff >= dir->silent_time)
- {
- if (check_zero_ref(file_path))
- {
- if (!updir_exist_file(dir, file_path))
- {
- updir_add_file(dir, file_path);
- }
- }
- //Dbg("addfile %s added! diff:%d", file_path,diff);
- }
- }
- }
- else
- {
- Dbg("the time error!");
- }
- }
- #endif // RVC_OS_WIN
-
- }
- toolkit_array_free2(arr_files);
- }
- array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
- if (arr_dir) {
- int i;
- for (i = 0; i < arr_dir->nelts; ++i) {
- char *dir_path = ARRAY_IDX(arr_dir, i, char*);
- dir_fresh(dir, dir_path);
- }
- }
- }
- int upload_fresh(struct list_head *list)
- {
- upload_dir_t *pos;
- list_for_each_entry(pos, list, upload_dir_t, entry)
- {
- //Dbg("scan dir %s",pos->path);
- dir_fresh(pos, pos->path);
- }
- return 0;
- }
- /** 交易受限功能代码废弃
- int check_dir_fresh(const char *path,int limitation,int silentTime,int &fileSumlen){
- int count=0;//当前文件夹下面的文件个数
- int sum = 0;//当前文件夹下面的子文件夹下面的文件个数
- array_header_t *arr_files = fileutil_get_sub_files2_a(path, limitation);
- if (arr_files) {
- int i;
- for (i = 0; i < arr_files->nelts; ++i)
- {
- char *file_path = ARRAY_IDX(arr_files, i, char*);
- #ifdef RVC_OS_WIN
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
- {
- SYSTEMTIME st;
- SYSTEMTIME filest;
- FILETIME ft;
- FILETIME fttmp;
- GetLocalTime(&st);
- SystemTimeToFileTime(&st, &ft);
- if (CompareFileTime(&ft, &attr.ftLastWriteTime) > 0)
- {
- //LARGE_INTEGER *p1 = (LARGE_INTEGER *)&attr.ftLastWriteTime;
- FileTimeToLocalFileTime(&attr.ftLastWriteTime, &fttmp);
- FileTimeToSystemTime(&fttmp, &filest);
- LARGE_INTEGER* p2 = (LARGE_INTEGER*)&ft;
- LARGE_INTEGER* p1 = (LARGE_INTEGER*)&fttmp;
- LONGLONG diff = p2->QuadPart - p1->QuadPart;
- diff = diff / 10000000L; // convert to seconds FILETIME是以100纳秒(ns)为单位
- //增加特殊文件不计入上传处理
- char* p = strstr(file_path, "SystemInitial.log");
- if (p != NULL) {
- continue;
- }
- p = strstr(file_path, "G_");
- if (p != NULL) {
- continue;
- }
- if (silentTime >= 86400)
- {
- int nsecondsInterval = (st.wYear - filest.wYear) * 32140800 + (st.wMonth - filest.wMonth) * 2678400 + (st.wDay - filest.wDay) * 86400;
- if ((nsecondsInterval >= silentTime))
- {
- //个数加1
- count++;
- //获取文件长度
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
- {
- int fileSize = attr.nFileSizeLow / 1024;//单位为k
- fileSumlen += fileSize;
- }
- else {
- }
- Dbg("checkfile %s ! diff:%d", file_path, nsecondsInterval);
- }
- }
- else
- {
- if (diff >= silentTime)
- {
- //个数加1
- count++;
- //获取文件长度
- WIN32_FILE_ATTRIBUTE_DATA attr;
- if (GetFileAttributesExA(file_path, GetFileExInfoStandard, &attr))
- {
- int fileSize = attr.nFileSizeLow / 1024;//单位为k
- fileSumlen += fileSize;
- }
- else {
- }
- Dbg("checkfile %s ! diff:%d", file_path, diff);
- }
- }
- }
- else
- {
- Dbg("the time error!");
- }
- }
- #else
- struct stat attr_of_src;
- if (lstat(file_path, &attr_of_src) == 0)
- {
- time_t nowTime = time((time_t*)NULL);//当前时间
- if (nowTime > attr_of_src.st_mtime)
- {
- long diff = nowTime - attr_of_src.st_mtime;
- //增加特殊文件不计入上传处理
- char* p = strstr(file_path, "SystemInitial.log");
- if (p != NULL) {
- continue;
- }
- p = strstr(file_path, "G_");
- if (p != NULL) {
- continue;
- }
- if (silentTime >= 86400)
- {
- tm nowTimeTm = { 0 };
- tm fileTimeTm = { 0 };
- localtime_r(&nowTime, &nowTimeTm);
- localtime_r(&attr_of_src.st_mtime, &fileTimeTm);
- int nsecondsInterval = (nowTimeTm.tm_year - fileTimeTm.tm_year) * 32140800 + (nowTimeTm.tm_mon - fileTimeTm.tm_mon) * 2678400 + (nowTimeTm.tm_mday - fileTimeTm.tm_mday) * 86400;
- if ((nsecondsInterval >= silentTime))
- {
- //个数加1
- count++;
- //获取文件长度
- int fileSize = attr_of_src.st_size / 1024;//单位为k
- fileSumlen += fileSize;
- Dbg("checkfile %s ! diff:%d", file_path, nsecondsInterval);
- }
- }
- else
- {
- if (diff >= silentTime)
- {
- //个数加1
- count++;
- //获取文件长度
- int fileSize = attr_of_src.st_size / 1024;//单位为k
- fileSumlen += fileSize;
- Dbg("checkfile %s ! diff:%d", file_path, diff);
- }
- }
- }
- else
- {
- Dbg("the time error!");
- }
- }
- #endif // RVC_OS_WIN
-
- }
- toolkit_array_free2(arr_files);
- }
- array_header_t *arr_dir = fileutil_get_sub_dirs_a(path);
- if (arr_dir) {
- int i;
- for (i = 0; i < arr_dir->nelts; ++i) {
- char *dir_path = ARRAY_IDX(arr_dir, i, char*);
- int fileLen=0;
- sum += check_dir_fresh(dir_path,limitation,silentTime,fileLen);
- fileSumlen=fileSumlen+fileLen;
- }
- }
- count = sum+count;
- return count;
- }
- */
- #ifdef RVC_OS_WIN
- bool ZipData(LPCTSTR lpszZipArchive, LPCTSTR lpszSrcFile)
- {
- BOOL bResult = TRUE;
- if (!lpszZipArchive)
- {
- Dbg("lpszZipArchive is NULL");
- return false;
- }
- if (!lpszSrcFile)
- {
- Dbg("lpszSrcFile is NULL");
- return false;
- }
- // does zip source file exist?
- //if (_waccess((wchar_t *)lpszSrcFile, 0) == -1)
- //{
- // Dbg("WARNING: zip source file '%s' cannot be found,operation aborted",lpszSrcFile);
- // return false;
- //}
- // use only the file name for zip file entry
- TCHAR* cp = (TCHAR*)_tcsrchr(lpszSrcFile, _T('\\'));
- if (cp == NULL)
- cp = (TCHAR*)lpszSrcFile;
- else
- cp++;
- HZIP hz = CreateZip((void*)lpszZipArchive, 0, ZIP_FILENAME);
- if (hz)
- {
- ZRESULT zr = ZipAdd(hz, cp, (void*)lpszSrcFile, 0, ZIP_FILENAME);
- CloseZip(hz);
- // did add work?
- if (zr == ZR_OK)
- {
- //Dbg("added '%s' to zip file '%s'",lpszSrcFile, lpszZipArchive);
- bResult = true;
- }
- else
- {
- Dbg("WARNING: failed to add zip source file '%s'", lpszSrcFile);
- bResult = false;
- }
- }
- else
- {
- Dbg("ERROR: failed to create zip file '%s'", lpszZipArchive);
- bResult = false;
- }
- return bResult;
- }
- #else
- unsigned long GetTickCount()
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
- }
- 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
|