123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- #include "stdafx.h"
- #include <algorithm>
- #include "common_api.h"
- #include "FileManager.h"
- FileManager g_Filemanager;
- FileManager::FileManager()
- {
- m_Mutex = CreateMutex(0, 0, 0);
- }
- FileManager::~FileManager()
- {
- WaitForSingleObject(m_Mutex, INFINITE);
- map<string, HANDLE>::iterator iter = m_FileMap.begin();
- while (iter != m_FileMap.end())
- {
- CloseHandle(iter->second);
- ++iter;
- }
- m_FileMap.clear();
- ReleaseMutex(m_Mutex);
- CloseHandle(m_Mutex);
- }
- bool FileManager::PrepFilePath(const char *pFileName)
- {
- return CreateFileDirectory(string(pFileName));
- }
- bool FileManager::PrepLogFile(LOGFILESTATUS &Pattern, const char *pFileName)
- {
- HANDLE filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (filehandle == INVALID_HANDLE_VALUE)
- {
- if (PrepFilePath(pFileName))
- {
- filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (filehandle == INVALID_HANDLE_VALUE)
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- //file exist
- INT64 filesize = 0;
- //do the max size check
- if (GetFileSizeEx(filehandle, (PLARGE_INTEGER)&filesize))
- {
- CloseHandle(filehandle);
- if ((size_t)filesize < Pattern.MaxFileSize)
- {
- return true;
- }
- //bigger than maxsize,then trim it to zero
- if (Pattern.MaxBackupCount == 0)
- {
- if ((size_t)filesize >(Pattern.MaxFileSize))
- {
- filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (filehandle == INVALID_HANDLE_VALUE)
- {
- return false;
- }
- CloseHandle(filehandle);
- }
- }
- else
- {
- //rename it to last (date+time+filename)
- WIN32_FIND_DATA data = { 0 };
- HANDLE Attrhand = FindFirstFile(pFileName, &data);
- if (Attrhand != INVALID_HANDLE_VALUE)
- {
- FindClose(Attrhand);
- //succeed
- for (DWORD i = 0; i < 1000;i++)
- {
- //get it's name
- string FileDir = GetFileDirectory(string(pFileName));
- char szNewFileTitle[MAX_PATH] = { 0 };
- //make name as date+time+name
- SYSTEMTIME st;
- GetLocalTime(&st);
- _snprintf_s(szNewFileTitle, MAX_PATH, _TRUNCATE, "%s.%04d%02d%02d_%02d%02d%02d%03d",
- data.cFileName,st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
- st.wMilliseconds + i);
- //if file exist,do [make name] again.
- string NewFileName = FileDir + "\\" + szNewFileTitle;
- memset(&data, 0, sizeof(WIN32_FIND_DATA));
- Attrhand = FindFirstFile(NewFileName.c_str(), &data);
- if (Attrhand == INVALID_HANDLE_VALUE)
- {
- //new file ok to move
- MoveFile(pFileName, NewFileName.c_str());
- break;
- }
- FindClose(Attrhand);
- }
- }
- //do the del check
- vector<string> fileList;
- if (FindSortedSubFiles(string(pFileName), fileList))
- {
- //count check
- while (Pattern.MaxBackupCount < fileList.size())
- {
- DeleteFile(fileList[fileList.size() - 1].c_str());
- fileList.pop_back();
- }
- //timeperiod check
- if (Pattern.MaxTimePeriod > 0)
- {
- SYSTEMTIME st;
- GetSystemTime(&st);
- ULARGE_INTEGER TimePeriod;
- UINT64 TimeBase = 10000;
- TimePeriod.QuadPart = TimeBase * 1000 * 60 * 60 * 24 * Pattern.MaxTimePeriod;
- ULARGE_INTEGER CurFileTime;/*CURFILETIME*/
- SystemTimeToFileTime(&st, (FILETIME*)&CurFileTime);
- while (fileList.size() > 0)
- {
-
- WIN32_FIND_DATA data = { 0 };
- HANDLE Attrhand = FindFirstFile(fileList[fileList.size() - 1].c_str(), &data);
- if (Attrhand != INVALID_HANDLE_VALUE)
- {
- FindClose(Attrhand);
- ULARGE_INTEGER *pFileTime = (ULARGE_INTEGER *)&(data.ftLastWriteTime);
- if (CurFileTime.QuadPart > (*pFileTime).QuadPart)
- {
- if (TimePeriod.QuadPart <= (CurFileTime.QuadPart - (*pFileTime).QuadPart))
- {
- //del the file
- DeleteFile(fileList[fileList.size() - 1].c_str());
- fileList.pop_back();
- }
- else
- {
- break;
- }
- }
- else
- {
- fileList.pop_back();
- }
- }
- else
- {
- fileList.pop_back();
- }
- }
- }
- }
- }
- }
- else
- {
- CloseHandle(filehandle);
- }
- return true;
- }
- bool FileManager::SaveContextToFile(const char *pFileName, const char *pContext, size_t ContextSize)
- {
- bool ret = true;
- HANDLE filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
- if (filehandle == INVALID_HANDLE_VALUE)
- {
- return false;
- }
- if (SetFilePointer(filehandle, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER)
- {
- DWORD returnedLenth = 0;
- if (WriteFile(filehandle, pContext, (DWORD)ContextSize, &returnedLenth, 0) == FALSE)
- {
- //OutputDebugStringW(L"write itam_log.txt failed \r\n");
- ret = false;
- }
- }
- CloseHandle(filehandle);
- return ret;
- }
- //bool FileManager::SaveLogFile(Logger_Pattern &Pattern, const char *pFileName, const char *pContext, size_t ContextSize)
- //{
- // bool ret = false;
- // HANDLE filemutex = 0;
- // string filenameLow = pFileName;
- // transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), tolower);
- //
- // if (WaitForSingleObject(m_Mutex, INFINITE) == WAIT_OBJECT_0)
- // {
- // //check the map
- // map<string, HANDLE>::iterator file_iter = m_FileMap.find(filenameLow);
- // if (file_iter != m_FileMap.end())
- // {
- // //already exist
- // filemutex = file_iter->second;
- // }
- // else
- // {
- // //add one
- //
- // //mutex name not support the backslash path-separator character
- // string mutexname = ReplaceSubString(filenameLow, string("\\"), string(" "));
- // filemutex = CreateMutex(0, 0, mutexname.c_str());
- // if (filemutex == NULL)
- // {
- // filemutex = OpenMutex(0, 0, mutexname.c_str());
- // if (filemutex == NULL)
- // {
- // ReleaseMutex(m_Mutex);
- // return ret;
- // }
- // }
- //
- // m_FileMap[filenameLow] = filemutex;
- // }
- // ReleaseMutex(m_Mutex);
- //
- // }
- // else
- // {
- // return ret;
- // }
- //
- // //got mutex
- // //can't wait INFINITE,Too Dangerous!!!!!!
- // if (WaitForSingleObject(filemutex, 2000) == WAIT_OBJECT_0)
- // {
- // //Do the Save
- //
- // //1. rename or delfile
- // if (PrepLogFile(Pattern, pFileName))
- // {
- // //2. save file
- // ret = SaveContextToFile(pFileName, pContext, ContextSize);
- // }
- //
- //
- //
- //
- // ReleaseMutex(filemutex);
- // }
- //
- // return ret;
- //
- //
- //
- //
- //
- //}
|