2014年3月25日11:45:12
检查日志文件系统
1、使用VS2008创建一个带预编译头的控制台项目。
2、源文件内容:
#include "stdafx.h"
#define LWW_CONSOLE
#ifdef LWW_CONSOLE
#define CosPrintf printf
#else
#define CosPrintf //
#endif
#include <windows.h>
#include <time.h>
//#define LOG_FILE_MAX_SIZE 10*1024*1024
#define LOG_FILE_MAX_SIZE 20
#include<iostream>
using namespace std;
// 删除指定目录下所有文件及目录
bool DelDirContent(TCHAR * tcsPath)
{
WIN32_FIND_DATA wfd;
HANDLE hFind;
TCHAR tcsFullPath[MAX_PATH] = {0};
TCHAR tcsDirFilter[MAX_PATH] = {0};
_tcscpy(tcsDirFilter,tcsPath);
_tcscat(tcsDirFilter,_T("\\*"));
hFind = FindFirstFile(tcsDirFilter, &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
return false;
}
do
{
if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0 )
{
continue;
}
_tcscpy(tcsFullPath,tcsPath);
_tcscat(tcsFullPath,_T("\\"));
_tcscat(tcsFullPath,wfd.cFileName);
//去掉只读属性
DWORD dwAttributes = GetFileAttributes(tcsFullPath);
if (dwAttributes & FILE_ATTRIBUTE_READONLY)
{
dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(tcsFullPath, dwAttributes);
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
DelDirContent(tcsFullPath);
RemoveDirectory(tcsFullPath);
}
else
{
if(!DeleteFile(tcsFullPath))
{
printf("删除文件失败\n");
}
}
}while (FindNextFile(hFind, &wfd));
FindClose(hFind);
return true;
}
bool isValidYYYYMM(TCHAR * tcs,int nBeginYYYYMM,int nNowYYYYMM)
{
int nSize = _tcsclen(tcs);
if(nSize != 6)
return false;
int nYYYYMM = _tstoi(tcs);
if((nYYYYMM>=nBeginYYYYMM)&&(nYYYYMM<=nNowYYYYMM))
{
return true;
}
else
{
return false;
}
}
void getYYYYMM_months(tm * pNowTm,int nSaveMonth,int * pBeginYYYYMM,int *pNowYYYYMM)
{
int nSaveYear = nSaveMonth/12;
int nBeginYear = (pNowTm->tm_year+1900) - nSaveYear;
int nSaveMonth12 = nSaveMonth%12;
int nBeginMonth = 0;
int nNowMonth = pNowTm->tm_mon+1;
if(nSaveMonth12 <= nNowMonth)
{
nBeginMonth = nNowMonth - nSaveMonth12 + 1;
}
else
{
nBeginYear--;
nSaveMonth12 -= nNowMonth;
nBeginMonth = 12 - nSaveMonth12 + 1;
}
*pBeginYYYYMM = nBeginYear * 100 + nBeginMonth;
*pNowYYYYMM = (pNowTm->tm_year+1900) * 100 + nNowMonth;
return;
}
bool isValidLogFileName(TCHAR * tcs,TCHAR * tcsYYYYMM,int nBeginDD,int nEndDD)
{
int nSize = _tcsclen(tcs);
if((nSize != 19)&&(nSize != 12))
return false;
TCHAR tcsTmp[40] = {0};
_tcsncpy(tcsTmp,tcs,6);
if(_tcscmp(tcsTmp,tcsYYYYMM) != 0)
return false;
memset(tcsTmp,0,sizeof(tcsTmp));
_tcsncpy(tcsTmp,tcs+6,2);
int nDD = _tstoi(tcsTmp);
if((nDD<nBeginDD)||(nDD>nEndDD))
return false;
if(19 == nSize)
{
memset(tcsTmp,0,sizeof(tcsTmp));
_tcsncpy(tcsTmp,tcs+8,1);
if(_tcscmp(tcsTmp,_T("_")) != 0)
return false;
memset(tcsTmp,0,sizeof(tcsTmp));
_tcsncpy(tcsTmp,tcs+9,6);
int nHHMMSS = _tstoi(tcsTmp);//可细化
if((nHHMMSS<0)||(nHHMMSS>235959))
return false;
memset(tcsTmp,0,sizeof(tcsTmp));
_tcsncpy(tcsTmp,tcs+15,4);
if(_tcscmp(tcsTmp,_T(".txt")) != 0)
return false;
}
else if(12 == nSize)
{
memset(tcsTmp,0,sizeof(tcsTmp));
_tcsncpy(tcsTmp,tcs+8,4);
if(_tcscmp(tcsTmp,_T(".txt")) != 0)
return false;
}
return true;
}
bool protectSecondLogDir(TCHAR * tcsPath,TCHAR * tcsYYYYMM,tm * pNowTm,int nSaveDay)
{
int nBeginDD = 1;
int nEndDD = 31;//可细化
if(nSaveDay<=31)//一个月
{
nEndDD = pNowTm->tm_mday;
if(nEndDD > nSaveDay)
{
nBeginDD = nEndDD - nSaveDay +1;
}
}
WIN32_FIND_DATA wfd;
HANDLE hFind;
TCHAR tcsFullPath[MAX_PATH] = {0};
TCHAR tcsDirFilter[MAX_PATH] = {0};
_tcscpy(tcsDirFilter,tcsPath);
_tcscat(tcsDirFilter,_T("\\*"));
hFind = FindFirstFile(tcsDirFilter, &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
CosPrintf ("FindFirstFile failed (%d)\n", GetLastError());
return false;
}
do
{
if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0 )
{
continue;
}
_tcscpy(tcsFullPath,tcsPath);
_tcscat(tcsFullPath,_T("\\"));
_tcscat(tcsFullPath,wfd.cFileName);
//去掉只读属性
DWORD dwAttributes = GetFileAttributes(tcsFullPath);
if (dwAttributes & FILE_ATTRIBUTE_READONLY)
{
dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(tcsFullPath, dwAttributes);
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
DelDirContent(tcsFullPath);
RemoveDirectory(tcsFullPath);
}
else
{
if(!isValidLogFileName(wfd.cFileName,tcsYYYYMM,nBeginDD,nEndDD))
{
if(!DeleteFile(tcsFullPath))
{
CosPrintf("删除文件失败\n");
}
}
if(wfd.nFileSizeLow>2*LOG_FILE_MAX_SIZE)
{
if(!DeleteFile(tcsFullPath))
{
CosPrintf("删除文件失败\n");
}
}
}
}while (FindNextFile(hFind, &wfd));
FindClose(hFind);
return true;
}
bool protectTopLogDir(TCHAR * tcsPath,tm * pNowTm,int nSaveDay)
{
if(nSaveDay < 1)
{
nSaveDay = 1;
}
else if(nSaveDay > 30*12*10)
{
nSaveDay = 30*12*10;
}
int nSaveMonth = nSaveDay/30;
if(nSaveMonth<=0)
{
nSaveMonth = 1;
}
else
{
nSaveDay = 60;//由其他函数来精确
}
int nBeginYYYYMM = 0;
int nNowYYYYMM = 0;
getYYYYMM_months(pNowTm,nSaveMonth,&nBeginYYYYMM,&nNowYYYYMM);
WIN32_FIND_DATA wfd;
HANDLE hFind;
TCHAR tcsFullPath[MAX_PATH] = {0};
TCHAR tcsDirFilter[MAX_PATH] = {0};
_tcscpy(tcsDirFilter,tcsPath);
_tcscat(tcsDirFilter,_T("\\*"));
hFind = FindFirstFile(tcsDirFilter, &wfd);
if (hFind == INVALID_HANDLE_VALUE)
{
CosPrintf ("FindFirstFile failed (%d)\n", GetLastError());
return false;
}
do
{
if (_tcscmp(wfd.cFileName, _T(".")) == 0 ||
_tcscmp(wfd.cFileName, _T("..")) == 0 )
{
continue;
}
_tcscpy(tcsFullPath,tcsPath);
_tcscat(tcsFullPath,_T("\\"));
_tcscat(tcsFullPath,wfd.cFileName);
//去掉只读属性
DWORD dwAttributes = GetFileAttributes(tcsFullPath);
if (dwAttributes & FILE_ATTRIBUTE_READONLY)
{
dwAttributes &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributes(tcsFullPath, dwAttributes);
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if(isValidYYYYMM(wfd.cFileName,nBeginYYYYMM,nNowYYYYMM))
{
protectSecondLogDir(tcsFullPath,wfd.cFileName,pNowTm,nSaveDay);
CosPrintf("isValidYYYYMM\n");
}
else
{
DelDirContent(tcsFullPath);
RemoveDirectory(tcsFullPath);
}
}
else
{
if(!DeleteFile(tcsFullPath))
{
CosPrintf("删除文件失败\n");
}
}
}while (FindNextFile(hFind, &wfd));
FindClose(hFind);
return true;
}
bool getLocalTime(tm * pNowTm)
{
tm * pTm;
time_t nowTime;
nowTime = time(NULL);
pTm = localtime(&nowTime);
memcpy(pNowTm,pTm,sizeof(tm));
return true;
}
int main( void )
{
char ch;
int nSaveDay = 5;
tm aNowTm;
getLocalTime(&aNowTm);
TCHAR tcsFileName[MAX_PATH] = {0};
_tcscpy(tcsFileName,TEXT("CenterTransLog"));
protectTopLogDir(tcsFileName,&aNowTm,nSaveDay);
printf("程序结束\n");
cin>>ch;
return 0;
}