// DirectoryChangeMgr.cpp : implementation file // #include "stdafx.h" #include "OIMOBOXWatchDir.h" #include "DirectoryChangeMgr.h" #include "HighTime.h" // CDirectoryChangeMgr CDirectoryChangeMgr::CDirectoryChangeMgr() { m_strLogin = _T(""); m_strServerID = _T(""); m_strDBFile = _T(""); m_bOpenDB = FALSE; m_arBackupDir.RemoveAll(); } CDirectoryChangeMgr::~CDirectoryChangeMgr() { CloseDB( ); } // CDirectoryChangeMgr member functions BOOL CDirectoryChangeMgr::OpenDB( CString strLogin, CString strPassword, CString strServerID, BOOL bCheckFile ) { if ( strLogin.IsEmpty() || strServerID.IsEmpty() ) return FALSE; CloseDB(); CString strPathName, strFileName; //CString strAccessPathName; CString strTemplateDBFile; CString strDBPassword; BOOL bExistFile = FALSE; strPathName = GetNetDiskDBWorkDir() ; if ( !ExistPath( strPathName ) ) { if ( !::CreateDirectory( strPathName, NULL ) ) return FALSE; } strPathName = strPathName + _T( "\\" ) + strLogin; if ( !ExistPath( strPathName ) ) { if ( !::CreateDirectory( strPathName , NULL ) ) return FALSE; } strFileName.Format( _T("%s\\%s_mb_watch.db"), strPathName, strServerID ); bExistFile = ExistFile( strFileName ); if ( bCheckFile && !bExistFile ) return FALSE; strTemplateDBFile = GetTemplateDBFile( ); if ( strTemplateDBFile.IsEmpty() ) return FALSE; if ( !bExistFile && !CopyFile( strTemplateDBFile, strFileName, FALSE ) ) { return FALSE; } if ( !m_AConnect.Open( strFileName ,strPassword ) ) return FALSE; m_strDBFile = strFileName; m_bOpenDB = TRUE; return TRUE; } void CDirectoryChangeMgr::CloseDB() { if ( m_bOpenDB ) { m_AConnect.Close(); m_bOpenDB = FALSE; m_strDBFile = _T( "" ); } } CString CDirectoryChangeMgr::GetTemplateDBFile( ) { CString strPathName; CString strTemplateDBFile; strPathName = GetModulePath() + _T( "\\Data" ); if ( !ExistPath( strPathName ) ) { if ( !::CreateDirectory( strPathName, NULL ) ) return _T(""); } strTemplateDBFile = strPathName + _T("\\mb_watch.db"); return strTemplateDBFile; } BOOL CDirectoryChangeMgr::InsertChangeDir(CString strFilePath, CString strOperateType, CString strFileParam ) { CString strBackupDir = _T(""); CString strDir; CString strTempPath; BOOL bReturn; if(!m_bOpenDB) return FALSE; strTempPath = strFilePath; strTempPath.MakeLower(); for(int i = 0; i < m_arBackupDir.GetCount(); i++) { strDir = m_arBackupDir.GetAt(i); strDir.MakeLower(); strDir +=_T("\\"); if(strTempPath.Find(strDir) == 0) { strBackupDir =strDir; break; } } CString strSQL; CHighTime dtCreate = CHighTime::GetPresentTime(); strSQL.Format(_T("INSERT INTO TN_ND_BACKUPDIR_FILE (CN_G_ID,CN_S_FILE_PATH,CN_S_OP_TYPE,CN_S_PARAM_VALUE,CN_T_CREATE) \ VALUES ('%s','%s','%s','%s','%s')"), GenerateGUIDString(),GetDBReplaceStr( strFilePath),strOperateType,GetDBReplaceStr( strFileParam),dtCreate.Format(_T("%Y-%m-%d %H:%M:%S"))); try { bReturn = CDBSQLiteCommFun::RunSQL( strSQL, &m_AConnect); } catch ( CDBSQliteException ex ) { CString strError = ex.GetErrInfo(); CString strErrInfo; strErrInfo = _T("CDirectoryChangeMgr::InsertChangeDir - ") + strError; WriteDBErrToFile( strErrInfo ); return FALSE; } return TRUE; } CString CDirectoryChangeMgr::GetNetDiskDBWorkDir() { return GetModulePath() +_T("\\Users"); CString strWorkDir; CRegKey reg; CString strSubKey; TCHAR szValue[256] = { 0 }; DWORD dwReaded; strSubKey.Format( _T("%s"), REG_DBSUBKEY ); if ( ERROR_SUCCESS == reg.Open( HKEY_LOCAL_MACHINE, strSubKey, KEY_READ ) ) { dwReaded = 255; if ( ERROR_SUCCESS == reg.QueryValue( szValue, _T( "WorkDir" ), &dwReaded ) && dwReaded > 0 ) { strWorkDir = szValue; strWorkDir.TrimRight(); strWorkDir.TrimRight( '\\' ); } reg.Close(); } if ( strWorkDir.IsEmpty() ) strWorkDir = GetModulePath( ); return strWorkDir; } CString CDirectoryChangeMgr::GetModulePath() { CString strFileName; GetModuleFileName( NULL, strFileName.GetBuffer( MAX_PATH ), MAX_PATH ); strFileName.ReleaseBuffer(); return GetPathName( strFileName ); } CString CDirectoryChangeMgr::GetPathName( CString strFileName ) { int nPos; CString strPathName; if ( strFileName.IsEmpty() ) return _T( "" ); nPos = strFileName.ReverseFind( '\\' ); if ( nPos < 0 ) return _T( "" ); strPathName = strFileName.Left( nPos ); return strPathName; } BOOL CDirectoryChangeMgr::ExistPath( CString strFilePath ) { DWORD dwAttr; if ( strFilePath.IsEmpty() ) return FALSE; dwAttr = ::GetFileAttributes(strFilePath); if(dwAttr != -1 && dwAttr & FILE_ATTRIBUTE_DIRECTORY ) return TRUE; return FALSE; } BOOL CDirectoryChangeMgr::ExistFile( CString strFileName ) { DWORD dwAttr; if ( strFileName.IsEmpty() ) return FALSE; dwAttr = ::GetFileAttributes(strFileName); if(dwAttr != -1 && !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) return TRUE; return FALSE; } CString CDirectoryChangeMgr::GenerateGUIDString() { CString strGUID; GUID guid; if(CoCreateGuid( &guid ) == S_OK) { OLECHAR theGUID[MAX_PATH]; StringFromGUID2(guid, theGUID, MAX_PATH); strGUID = theGUID; } return strGUID; } BOOL CDirectoryChangeMgr::GetBackupDir(CStringArray &arBackupDir ) { if ( !m_bOpenDB ) return FALSE; CString strPropClass; CString strSQL; CDBRecord ARecordset; BOOL bReturn = FALSE; CString strPropValue; arBackupDir.RemoveAll(); strPropClass = _T("_NDBACKUPDIR"); strSQL.Format( _T("SELECT CN_S_C4 FROM TN_ND_PROPERTY WHERE CN_S_C1='%s'"),strPropClass); try { ARecordset.Close(); ARecordset.Open( &m_AConnect,strSQL ); while ( S_OK == ARecordset.MoveNext() ) { ARecordset.GetValue( 1, strPropValue ); arBackupDir.Add(strPropValue); } } catch ( CDBSQliteException ex ) { CString strError = ex.GetErrInfo(); CString strErrInfo; strErrInfo = _T("CDirectoryChangeMgr::GetBackupDir - ") + strError; WriteDBErrToFile( strErrInfo ); } strPropClass = _T("_DMSBACKUPDIR"); strSQL.Format( _T("SELECT CN_S_C4 FROM TN_ND_PROPERTY WHERE CN_S_C1='%s'"),strPropClass); try { ARecordset.Close(); ARecordset.Open( &m_AConnect,strSQL ); while ( S_OK == ARecordset.MoveNext() ) { ARecordset.GetValue( 1, strPropValue ); arBackupDir.Add(strPropValue); } } catch ( CDBSQliteException ex ) { CString strError = ex.GetErrInfo(); CString strErrInfo; strErrInfo = _T("CDirectoryChangeMgr::GetBackupDir - ") + strError; WriteDBErrToFile( strErrInfo ); } m_arBackupDir.RemoveAll(); m_arBackupDir.Copy(arBackupDir); return bReturn; } void CDirectoryChangeMgr::WriteDBErrToFile( CString strErrInfo ) { CString strWorkDir = GetModulePath( ); CString strErrPath = strWorkDir + _T("\\MOBOXWatchDirErr.Log"); CHighTime dtCurTime = CHighTime::GetPresentTime( true ); CString strInfo; strInfo.Format( _T("\r\n%s:\r\n%s"), dtCurTime.Format( _T("%Y-%m-%d %H:%M:%S") ), strErrInfo ); WriteRowToFile( strErrPath, strInfo ); } bool CDirectoryChangeMgr::CStringToAnsi( LPCTSTR pszT, LPSTR *ppszA ) { ULONG cbAnsi, cCharacters; if ( pszT == NULL ) { *ppszA= NULL; return false; } #ifdef _UNICODE cCharacters = wcslen(pszT); cbAnsi = ::WideCharToMultiByte( CP_ACP, 0, pszT, -1, NULL, 0, NULL, NULL ); *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi+1); if ( NULL == *ppszA ) return false; ZeroMemory(*ppszA,cbAnsi+1); // Convert to ANSI. if (0 == WideCharToMultiByte(CP_ACP, 0, pszT, cCharacters, *ppszA, cbAnsi, NULL, NULL)) { CoTaskMemFree(*ppszA); *ppszA = NULL; return false; } #else cCharacters = strlen(pszT); *ppszA = (LPSTR) CoTaskMemAlloc(cCharacters+1); if ( NULL == *ppszA ) return false; ZeroMemory(*ppszA,cCharacters+1); memcpy(*ppszA,pszT,cCharacters); #endif return true; } void CDirectoryChangeMgr::WriteRowToFile(CString strFileName, CString strContent ) { CTime tmCurrent = CTime::GetCurrentTime(); CString strEnter = _T(" \r\n "); CString strBuff; LPSTR pDest ; CFile file; if(strFileName.IsEmpty()) return; if ( !file.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate) ) { return ; } file.Seek( 0, CFile::end ); strBuff.Format( _T("%s%s"), strContent,strEnter ); if ( CStringToAnsi(strBuff, &pDest)) { file.Write( (void*)pDest, strlen(pDest)); CoTaskMemFree(pDest); } file.Close(); /*CStdioFile cfile; CFileException ex; if (!cfile.Open(strFileName, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, &ex)) { // complain if an error happened // no need to delete the ex object TCHAR szError[1024]; ex.GetErrorMessage(szError, 1024); TRACE("Couldn't open source file:%s", szError); return; } #ifdef _UNICODE if ( cfile.GetLength( ) == 0 ) { WORD unicode = 0xFEFF; //UNICODE±àÂëÎļþÍ· cfile.Write( &unicode, 2 ); } #endif cfile.SeekToEnd(); cfile.WriteString(strContent); cfile.Close();*/ } CString CDirectoryChangeMgr::GetDBReplaceStr( CString strData ) { CString strValue; strValue = strData; strValue.Replace( _T("'"), _T("''") ); return strValue; }