FileManager.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #include "stdafx.h"
  2. #include <algorithm>
  3. #include "common_api.h"
  4. #include "FileManager.h"
  5. FileManager g_Filemanager;
  6. FileManager::FileManager()
  7. {
  8. m_Mutex = CreateMutex(0, 0, 0);
  9. }
  10. FileManager::~FileManager()
  11. {
  12. WaitForSingleObject(m_Mutex, INFINITE);
  13. map<string, HANDLE>::iterator iter = m_FileMap.begin();
  14. while (iter != m_FileMap.end())
  15. {
  16. CloseHandle(iter->second);
  17. ++iter;
  18. }
  19. m_FileMap.clear();
  20. ReleaseMutex(m_Mutex);
  21. CloseHandle(m_Mutex);
  22. }
  23. bool FileManager::PrepFilePath(const char *pFileName)
  24. {
  25. return CreateFileDirectory(string(pFileName));
  26. }
  27. bool FileManager::PrepLogFile(LOGFILESTATUS &Pattern, const char *pFileName)
  28. {
  29. HANDLE filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  30. if (filehandle == INVALID_HANDLE_VALUE)
  31. {
  32. if (PrepFilePath(pFileName))
  33. {
  34. filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  35. if (filehandle == INVALID_HANDLE_VALUE)
  36. {
  37. return false;
  38. }
  39. }
  40. else
  41. {
  42. return false;
  43. }
  44. }
  45. //file exist
  46. INT64 filesize = 0;
  47. //do the max size check
  48. if (GetFileSizeEx(filehandle, (PLARGE_INTEGER)&filesize))
  49. {
  50. CloseHandle(filehandle);
  51. if ((size_t)filesize < Pattern.MaxFileSize)
  52. {
  53. return true;
  54. }
  55. //bigger than maxsize,then trim it to zero
  56. if (Pattern.MaxBackupCount == 0)
  57. {
  58. if ((size_t)filesize >(Pattern.MaxFileSize))
  59. {
  60. filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  61. if (filehandle == INVALID_HANDLE_VALUE)
  62. {
  63. return false;
  64. }
  65. CloseHandle(filehandle);
  66. }
  67. }
  68. else
  69. {
  70. //rename it to last (date+time+filename)
  71. WIN32_FIND_DATA data = { 0 };
  72. HANDLE Attrhand = FindFirstFile(pFileName, &data);
  73. if (Attrhand != INVALID_HANDLE_VALUE)
  74. {
  75. FindClose(Attrhand);
  76. //succeed
  77. for (DWORD i = 0; i < 1000;i++)
  78. {
  79. //get it's name
  80. string FileDir = GetFileDirectory(string(pFileName));
  81. char szNewFileTitle[MAX_PATH] = { 0 };
  82. //make name as date+time+name
  83. SYSTEMTIME st;
  84. GetLocalTime(&st);
  85. _snprintf_s(szNewFileTitle, MAX_PATH, _TRUNCATE, "%s.%04d%02d%02d_%02d%02d%02d%03d",
  86. data.cFileName,st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
  87. st.wMilliseconds + i);
  88. //if file exist,do [make name] again.
  89. string NewFileName = FileDir + "\\" + szNewFileTitle;
  90. memset(&data, 0, sizeof(WIN32_FIND_DATA));
  91. Attrhand = FindFirstFile(NewFileName.c_str(), &data);
  92. if (Attrhand == INVALID_HANDLE_VALUE)
  93. {
  94. //new file ok to move
  95. MoveFile(pFileName, NewFileName.c_str());
  96. break;
  97. }
  98. FindClose(Attrhand);
  99. }
  100. }
  101. //do the del check
  102. vector<string> fileList;
  103. if (FindSortedSubFiles(string(pFileName), fileList))
  104. {
  105. //count check
  106. while (Pattern.MaxBackupCount < fileList.size())
  107. {
  108. DeleteFile(fileList[fileList.size() - 1].c_str());
  109. fileList.pop_back();
  110. }
  111. //timeperiod check
  112. if (Pattern.MaxTimePeriod > 0)
  113. {
  114. SYSTEMTIME st;
  115. GetSystemTime(&st);
  116. ULARGE_INTEGER TimePeriod;
  117. UINT64 TimeBase = 10000;
  118. TimePeriod.QuadPart = TimeBase * 1000 * 60 * 60 * 24 * Pattern.MaxTimePeriod;
  119. ULARGE_INTEGER CurFileTime;/*CURFILETIME*/
  120. SystemTimeToFileTime(&st, (FILETIME*)&CurFileTime);
  121. while (fileList.size() > 0)
  122. {
  123. WIN32_FIND_DATA data = { 0 };
  124. HANDLE Attrhand = FindFirstFile(fileList[fileList.size() - 1].c_str(), &data);
  125. if (Attrhand != INVALID_HANDLE_VALUE)
  126. {
  127. FindClose(Attrhand);
  128. ULARGE_INTEGER *pFileTime = (ULARGE_INTEGER *)&(data.ftLastWriteTime);
  129. if (CurFileTime.QuadPart > (*pFileTime).QuadPart)
  130. {
  131. if (TimePeriod.QuadPart <= (CurFileTime.QuadPart - (*pFileTime).QuadPart))
  132. {
  133. //del the file
  134. DeleteFile(fileList[fileList.size() - 1].c_str());
  135. fileList.pop_back();
  136. }
  137. else
  138. {
  139. break;
  140. }
  141. }
  142. else
  143. {
  144. fileList.pop_back();
  145. }
  146. }
  147. else
  148. {
  149. fileList.pop_back();
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. else
  157. {
  158. CloseHandle(filehandle);
  159. }
  160. return true;
  161. }
  162. bool FileManager::SaveContextToFile(const char *pFileName, const char *pContext, size_t ContextSize)
  163. {
  164. bool ret = true;
  165. HANDLE filehandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
  166. if (filehandle == INVALID_HANDLE_VALUE)
  167. {
  168. return false;
  169. }
  170. if (SetFilePointer(filehandle, 0, 0, FILE_END) != INVALID_SET_FILE_POINTER)
  171. {
  172. DWORD returnedLenth = 0;
  173. if (WriteFile(filehandle, pContext, (DWORD)ContextSize, &returnedLenth, 0) == FALSE)
  174. {
  175. //OutputDebugStringW(L"write itam_log.txt failed \r\n");
  176. ret = false;
  177. }
  178. }
  179. CloseHandle(filehandle);
  180. return ret;
  181. }
  182. //bool FileManager::SaveLogFile(Logger_Pattern &Pattern, const char *pFileName, const char *pContext, size_t ContextSize)
  183. //{
  184. // bool ret = false;
  185. // HANDLE filemutex = 0;
  186. // string filenameLow = pFileName;
  187. // transform(filenameLow.begin(), filenameLow.end(), filenameLow.begin(), tolower);
  188. //
  189. // if (WaitForSingleObject(m_Mutex, INFINITE) == WAIT_OBJECT_0)
  190. // {
  191. // //check the map
  192. // map<string, HANDLE>::iterator file_iter = m_FileMap.find(filenameLow);
  193. // if (file_iter != m_FileMap.end())
  194. // {
  195. // //already exist
  196. // filemutex = file_iter->second;
  197. // }
  198. // else
  199. // {
  200. // //add one
  201. //
  202. // //mutex name not support the backslash path-separator character
  203. // string mutexname = ReplaceSubString(filenameLow, string("\\"), string(" "));
  204. // filemutex = CreateMutex(0, 0, mutexname.c_str());
  205. // if (filemutex == NULL)
  206. // {
  207. // filemutex = OpenMutex(0, 0, mutexname.c_str());
  208. // if (filemutex == NULL)
  209. // {
  210. // ReleaseMutex(m_Mutex);
  211. // return ret;
  212. // }
  213. // }
  214. //
  215. // m_FileMap[filenameLow] = filemutex;
  216. // }
  217. // ReleaseMutex(m_Mutex);
  218. //
  219. // }
  220. // else
  221. // {
  222. // return ret;
  223. // }
  224. //
  225. // //got mutex
  226. // //can't wait INFINITE,Too Dangerous!!!!!!
  227. // if (WaitForSingleObject(filemutex, 2000) == WAIT_OBJECT_0)
  228. // {
  229. // //Do the Save
  230. //
  231. // //1. rename or delfile
  232. // if (PrepLogFile(Pattern, pFileName))
  233. // {
  234. // //2. save file
  235. // ret = SaveContextToFile(pFileName, pContext, ContextSize);
  236. // }
  237. //
  238. //
  239. //
  240. //
  241. // ReleaseMutex(filemutex);
  242. // }
  243. //
  244. // return ret;
  245. //
  246. //
  247. //
  248. //
  249. //
  250. //}