123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173 |
- #include "stdafx.h"
- #include "PhotoBackupMgr.h"
- #include "Global.h"
- #include <process.h>
- #include <strsafe.h >
- #include <Winbase.h>
- #include "mysqldata.h"
- #include "TblDef.h"
- #include "CatalogObj.h"
- #include "BranchInfo.h"
- #include "BakPhotoModel\BackupFactory.h"
- #include "BakPhotoModel\BackupInterface.h"
- #include "helper\ffsco.h"
- using namespace helper_coffs;
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- typedef HRESULT (WINAPI *GetClassBackupObject)(REFCLSID, REFIID, LPVOID*);
- unsigned int WINAPI IncrementalBakThread(LPVOID lpParam);
- unsigned int WINAPI AutoBakThread(LPVOID lpParam);
- CPhotoBackupMgr::CPhotoBackupMgr()
- {
- m_nBackupModel = 0; // 备份模式
- m_hIncrementalThread = NULL;
- m_hIncrementalEvent = NULL;
- m_hAutoThread = NULL;
- m_hAutoEvent = NULL;
- m_BakInterf = NULL;
- m_BakFact = NULL;
- m_pCCatalogObj = new CCatalogObj;
- }
- CPhotoBackupMgr::~CPhotoBackupMgr()
- {
- if(m_pCCatalogObj)
- delete m_pCCatalogObj;
- m_pCCatalogObj = NULL;
- }
- /************************************************************************/
- /*
- 函数:InitCatalog
- 描述:初始化数据库实例对象;
- 参数:
- IN: dwCatalogPort 数据库实例映射端口;
- IN: lpCatalogSource 数据库实例地址;
- IN: lpCatalogAccount 数据库实例登陆账号;
- IN: lpCatalogPsw 数据库实例登陆密码;
- IN: lpCatalogName 数据库名称;
- 返回:成功连接数据库实例返回TRUE;
- 要求:
- 注意:
- 示例:
- */
- /************************************************************************/
- BOOL CPhotoBackupMgr::InitCatalog(IN CONST DWORD &dwCatalogPort, IN LPCTSTR lpCatalogSource, IN LPCTSTR lpCatalogAccount, IN LPCTSTR lpCatalogPsw, IN LPCTSTR lpCatalogName)
- {
- if(m_pCCatalogObj == NULL)
- return FALSE;
- if ( m_pCCatalogObj->IsOpen() )
- return TRUE;
-
- TCHAR szSQLConnt[MAX_PATH] = _T("");
- if (dwCatalogPort)
- {
- _stprintf_s(szSQLConnt, _T("driver={SQL Server};Server=%s,%d;database=%s;uid=%s;pwd=%s"),
- lpCatalogSource, dwCatalogPort, lpCatalogName, lpCatalogAccount, lpCatalogPsw);
- }
- else
- {
- _stprintf_s(szSQLConnt, _T("driver={SQL Server};Server=%s;database=%s;uid=%s;pwd=%s"),
- lpCatalogSource, lpCatalogName, lpCatalogAccount, lpCatalogPsw);
- }
- m_pCCatalogObj->SetConn(szSQLConnt);
- BOOL bResult = m_pCCatalogObj->OpenCatalog();
- if( !bResult )
- {
- WriteLogin(_T("增量备份打开数据库失败\n"));
- //LOG4C_NO_FILENUM((LOG_NOTICE,"打开数据库失败"));
- }
- return bResult;
- }
- /************************************************************************/
- /*
- 函数:StartBackup
- 描述:启动备份线程
- 参数:
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::StartBackup()
- {
- #if 0
- if(g_nBackupModel == BACKUPMODEL_Incremental || g_nBackupModel == BACKUPMODEL_Both)
- StartIncrementalBackup();
- if(g_nBackupModel == BACKUPMODEL_Auto || g_nBackupModel == BACKUPMODEL_Both)
- StartAutoBackup();
- #endif //#if 0
- }
- /************************************************************************/
- /*
- 函数:TransformBackupModel
- 描述:切换模式进行备份
- 参数:
- IN CONST INT nModel 模式
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::TransformBackupModel(IN CONST INT nModel)
- {
- TerminateBakThread();
- if(nModel == BACKUPMODEL_Incremental || nModel == BACKUPMODEL_Both)
- StartIncrementalBackup();
- if(nModel == BACKUPMODEL_Auto || nModel == BACKUPMODEL_Both)
- StartAutoBackup();
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:增量备份
- 参数:
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::StartIncrementalBackup()
- {
- //获取备份目录
- GetBackupOfSaveDir(m_bakDirArr);
- TerminateIncrementalBak();
- m_hIncrementalEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- unsigned int nThreadID = 0;
- m_hIncrementalThread = (HANDLE)_beginthreadex(NULL, 0, IncrementalBakThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &nThreadID);
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:结束增量备份
- 参数:
- IN DWORD dwMilliseconds 等待超时
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::TerminateIncrementalBak(IN DWORD dwMilliseconds)
- {
- if(m_hIncrementalEvent)
- SetEvent(m_hIncrementalEvent);
- if(m_hIncrementalThread)
- {
- WaitForSingleObject(m_hIncrementalThread, dwMilliseconds);
- CloseHandle(m_hIncrementalThread);
- OutputDebugString(_T("TerminateIncrementalBak exit"));
- }
- m_hIncrementalThread = NULL;
- if(m_hIncrementalEvent)
- CloseHandle(m_hIncrementalEvent);
- m_hIncrementalEvent = NULL;
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:自动备份
- 参数:
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::StartAutoBackup()
- {
- TerminateAutoBak();
- m_hAutoEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
- unsigned int nThreadID = 0;
- m_hAutoThread = (HANDLE)_beginthreadex(NULL, 0, AutoBakThread, (LPVOID)this, 0/* CREATE_SUSPENDED*/, &nThreadID);
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:结束自动备份
- 参数:
- IN DWORD dwMilliseconds 等待超时
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::TerminateAutoBak(IN DWORD dwMilliseconds)
- {
- if(m_hAutoEvent)
- SetEvent(m_hAutoEvent);
- if(m_hAutoThread)
- {
- WaitForSingleObject(m_hAutoThread, dwMilliseconds);
- CloseHandle(m_hAutoThread);
- OutputDebugString(_T("TerminateIncrementalBak exit"));
- }
- m_hAutoThread = NULL;
- if(m_hAutoEvent)
- CloseHandle(m_hAutoEvent);
- m_hAutoEvent = NULL;
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:结束自动备份
- 参数:
- LPVOID lpParam
- 返回:
- */
- /************************************************************************/
- unsigned int WINAPI IncrementalBakThread(LPVOID lpParam)
- {
- CPhotoBackupMgr *p = (CPhotoBackupMgr*)lpParam;
- do
- {
- p->IncrementWork();
- } while (WAIT_TIMEOUT == WaitForSingleObject(p->GetIncrementalEvent(), 15000) );
- return 0;
- }
- /************************************************************************/
- /*
- 函数:StartIncrementalBackup
- 描述:结束自动备份
- 参数:
- LPVOID lpParam
- 返回:
- */
- /************************************************************************/
- unsigned int WINAPI AutoBakThread(LPVOID lpParam)
- {
- CPhotoBackupMgr* p = (CPhotoBackupMgr*)lpParam;
-
- p->AutoBakWork();
- ::AfxGetApp()->GetMainWnd()->SetTimer(6, 3000, NULL);
- _endthreadex(0);
- return 0;
- }
- void CPhotoBackupMgr::AutoBakWork()
- {
- #if 0
- int step = 1;
- BOOL bError = 0;
- try
- {
- if(m_pCCatalogObj == NULL)
- return;
- g_tisks = ::GetTickCount();
- WriteLogin("开始备份照片");
- // CRstPath rsSt;
- CString path1 = _T("");
- CString path2 = _T("");
- CString path3 = _T("");
- CString path4 = _T("");
- path1 = path2 = path3 = path4 = g_localname;
- path1 = "\\\\" + path1 + "\\客户原片(管理软件)$";
- path2 = "\\\\" + path2 + "\\修好的片(管理软件)$";
- path3 = "\\\\" + path3 + "\\精修好的片(管理软件)$";
- path4 = "\\\\" + path4 + "\\设计好的片(管理软件)$";
- GetPathFromNetShareName("客户原片(管理软件)$", path1);
- GetPathFromNetShareName("修好的片(管理软件)$", path2);
- GetPathFromNetShareName("精修好的片(管理软件)$", path3);
- GetPathFromNetShareName("设计好的片(管理软件)$", path4);
- step = 2;
- path1.MakeLower();
- path2.MakeLower();
- path3.MakeLower();
- path4.MakeLower();
- CString bak1, bak2, bak3, bak4; // 是否备份(指定的备份目录是否需要备份);
- CString bak11, bak22, bak33, bak44; // 是否备份(集团版的分店备份目录是否需要备份??);
- CString bakserver1, bakserver2, bakserver3, bakserver4; // 相片备份目录 ;
- {
- CRstversion rsSt;
- rsSt.m_pDatabase = (CDatabase*)m_pCCatalogObj->GetCatalogInstance();
- rsSt.Open();
- if (!rsSt.IsEOF())
- {
- bak1 = rsSt.m_bak1;
- bak2 = rsSt.m_bak2;
- bak3 = rsSt.m_bak3;
- bak4 = rsSt.m_bak4;
- bakserver1 = rsSt.m_bakserver1;
- bakserver2 = rsSt.m_bakserver2;
- bakserver3 = rsSt.m_bakserver3;
- bakserver4 = rsSt.m_bakserver4;
- bak11 = rsSt.bak11;
- bak22 = rsSt.bak22;
- bak33 = rsSt.bak33;
- bak44 = rsSt.bak44;
- }
- rsSt.Close();
- step = 3;
- }
- #ifdef CHILD_VERSION
- if (bakserver1.Find(":") == -1 && bakserver1 != "")
- bakserver1 = "\\\\" + bakserver1 + "\\客户原片备份(儿童管理软件)$";
- GetPathFromNetShareName("客户原片备份(儿童管理软件)$", bakserver1);
- if (bakserver2.Find(":") == -1 && bakserver2 != "")
- bakserver2 = "\\\\" + bakserver2 + "\\修好的片备份(儿童管理软件)$";
- GetPathFromNetShareName("修好的片备份(儿童管理软件)$", bakserver2);
- if (bakserver3.Find(":") == -1 && bakserver3 != "")
- bakserver3 = "\\\\" + bakserver3 + "\\精修好的片备份(儿童管理软件)$";
- GetPathFromNetShareName("精修好的片备份(儿童管理软件)$", bakserver3);
- if (bakserver4.Find(":") == -1 && bakserver4 != "")
- bakserver4 = "\\\\" + bakserver4 + "\\设计好的片备份(儿童管理软件)$";
- GetPathFromNetShareName("设计好的片备份(儿童管理软件)$", bakserver4);
- #else
- if (bakserver1.Find(":") == -1 && bakserver1 != "")
- bakserver1 = "\\\\" + bakserver1 + "\\客户原片备份(管理软件)$";
- GetPathFromNetShareName("客户原片备份(管理软件)$", bakserver1);
- if (bakserver2.Find(":") == -1 && bakserver2 != "")
- bakserver2 = "\\\\" + bakserver2 + "\\修好的片备份(管理软件)$";
- GetPathFromNetShareName("修好的片备份(管理软件)$", bakserver2);
- if (bakserver3.Find(":") == -1 && bakserver3 != "")
- bakserver3 = "\\\\" + bakserver3 + "\\精修好的片备份(管理软件)$";
- GetPathFromNetShareName("精修好的片备份(管理软件)$", bakserver3);
- if (bakserver4.Find(":") == -1 && bakserver4 != "")
- bakserver4 = "\\\\" + bakserver4 + "\\设计好的片备份(管理软件)$";
- GetPathFromNetShareName("设计好的片备份(管理软件)$", bakserver4);
- #endif
- WriteLogin(path1); WriteLogin(path2); WriteLogin(path3); WriteLogin(path4);
- step = 4;
- CStringArray dirarray;
- CString str, path;
- BOOL bcheck1 = atoi(bak1);
- BOOL bcheck2 = atoi(bak2);
- BOOL bcheck3 = atoi(bak3);
- BOOL bcheck4 = atoi(bak4);
- BOOL bcheck11 = atoi(bak11);
- BOOL bcheck22 = atoi(bak22);
- BOOL bcheck33 = atoi(bak33);
- BOOL bcheck44 = atoi(bak44);
- // 下列4变量应该使用结构体保存;
- BOOL bcheck[4] = { bcheck1, bcheck2, bcheck3, bcheck4 }; //设置是否要备份组
- // 集团版;
- BOOL bcheckbranch[4] = { bcheck11, bcheck22, bcheck33, bcheck44 }; //设置分店是否要备份组
- CString patharray[4] = { path1, path2, path3, path4 }; //需要备份的目录组
- CString patharray2[4] = { bakserver1, bakserver2, bakserver3, bakserver4 }; //备份的目录组
- CArray<int, int> idxArray; //备份的目录组的引索
- int i = 0;
- for ( i = 0; i < 4; i++)
- {
- if (bcheck[i] == 0)continue; // 是否要备份
- if (patharray2[i] == "")continue; // 备份目录
- if (!CheckFolderFileExist(patharray2[i]))continue; //备份的目录组
- //多级共享目录
- if(CBranchInfo::GetInstance()->m_TblNetShareInfo.size() != 0)
- {
- vector<TblNetShareInfo*>::iterator iter = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
- for(; iter != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
- {
- //byPhotoType可能测为: 1=原片,2=初修,3=精修,4=设计好
- TblNetShareInfo* p = (*iter);
- if(p != NULL && ((int)p->byPhotoType) == (i+1) && ((int)p->byPhotoType) < 5)
- {
- CString strDir = p->szShareDirectory;
- if(strDir.Find(_T("")) != -1)
- {
- if(!CheckFolderFileExist(strDir))continue; //需要备份的目录组
- // 获取订单文件夹路径全名;
- ffsco o;
- o.dirs(1);
- o.find(LPCSTR(strDir), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_dir();
- it = coo.begin();
- it++;
- for (; coo.end() != it; it++)
- {
- str = (*it).c_str();
- if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)
- continue;
- if (FindArray(&dirarray, str) == -1)
- {
- //WriteLogin(str);
- dirarray.Add(str);
- idxArray.Add(i);
- }
- }
- }
- }
- ++iter;
- }
- //大于2是精修和设计好的片目录
- if(i >= 2 && i < 4)
- {
- if (!CheckFolderFileExist(patharray[i]))//需要备份的目录组
- continue;
- // 获取订单文件夹路径全名;
- ffsco o;
- o.dirs(1);
- o.find(LPCSTR(patharray[i]), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_dir();
- it = coo.begin();
- it++;
- for (; coo.end() != it; it++)
- {
- str = (*it).c_str();
- if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)continue;
- if (FindArray(&dirarray, str) == -1)
- {
- //WriteLogin(str);
- dirarray.Add(str);
- idxArray.Add(i);
- }
- }
- }
- }
- else
- {
- //需要备份的目录组
- if (!CheckFolderFileExist(patharray[i]))
- continue;
- // 获取订单文件夹路径全名;
- ffsco o;
- o.dirs(1);
- o.find(LPCSTR(patharray[i]), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_dir();
- it = coo.begin();
- it++;
- for (; coo.end() != it; it++)
- {
- str = (*it).c_str();
- if (str.Find(".ly.com") != -1 && bcheckbranch[i] == 0)
- continue;
- if (FindArray(&dirarray, str) == -1)
- {
- //WriteLogin(str);
- dirarray.Add(str);
- idxArray.Add(i);
- }
- }
- }
- }
- step = 5;
- // 对每个订单文件夹进行相片复制操作;
- bError = 0;
- for (i = 0; i<dirarray.GetSize(); i++)
- {
- CTime tm = CTime::GetCurrentTime();
- tm -= CTimeSpan(g_nBakDays, 0, 0, 0);
- CString tenDaysAgo = tm.Format("%Y%m%d");
- DWORD dirdate = GetLastDirTime(dirarray.ElementAt(i));
- CString ss;
- if (dirdate>atol(tenDaysAgo))
- {
- step = 6;
- if (0 == BakAllPhoto(dirarray.ElementAt(i), patharray2[idxArray.ElementAt(i)]))
- bError = 1;
- CString path = dirarray.ElementAt(i);
- path.TrimRight("\\"); path += "\\";
- path += "modifytime";
- CStdioFile fp;
- //将备份时间写入modifytime
- if (!::PathFileExists(path))
- {
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d"));
- fp.Close();
- }
- }
- else
- {
- //更新modifytime里的备份时间
- step = 7;
- CString path = dirarray.ElementAt(i);
- path.TrimRight("\\"); path += "\\";
- path += "modifytime";
- CStdioFile fp;
- if (!::PathFileExists(path))
- {
- step = 8;
- fp.Open(path, CFile::modeCreate | CFile::modeWrite);
- fp.WriteString(CTime::GetCurrentTime().Format("%Y-%m-%d"));
- fp.Close();
- }
- }
- }
- if (bError)
- g_bakphoto = "照片备份失败";
- else
- g_bakphoto = "照片备份成功";
- step = 9;
- }
- catch (CException *e)
- {
- TCHAR szError[1024] = { 0 };
- e->GetErrorMessage(szError, 1024);
- WriteLogin(CString(szError));
- e->Delete();
- if (step != 8)
- {
- CString ss;
- ss.Format("出错在函数备份照片中-step:%d", step);
- WriteLogin(ss);
- g_bakphoto = "照片备份出错";
- }
- else
- {
- if (bError)
- g_bakphoto = "照片备份失败";
- else
- g_bakphoto = "照片备份成功";
- }
- }
- #endif //if 0
- }
- //--------------------------------------------------------------------------------
- /*
- // 函数:BakAllPhoto
- // 描述:将订单目录下的相片备份
- // 参数:
- dir: 当前要备份的订单目录
- desdir: 保存的目录
- // 返回:TRUE,FALSE
- */
- //--------------------------------------------------------------------------------
- BOOL CPhotoBackupMgr::BakAllPhoto(CString dir, CString desdir)
- {
- try
- {
- CString childdir = _T("");
- CString srcdir = dir;
- srcdir.MakeLower();
-
- int nPos = srcdir.Find(_T("$"));
- if(nPos != -1)
- childdir = srcdir.Right(srcdir.GetLength() - (nPos + 1));
- else
- return FALSE;
- childdir.TrimLeft("\\");
- childdir.TrimRight("\\");
- while (childdir.Find('\\') != -1)
- {
- desdir += "\\";
- desdir += childdir.Left(childdir.Find('\\'));
- CreateDirectory(desdir, NULL);
- childdir = childdir.Right(childdir.GetLength() - childdir.Find('\\') - 1);
- }
- desdir += "\\";
- desdir += childdir;
- CreateDirectory(desdir, NULL);
- desdir += "\\";
- //进行备份复制
- ffsco o;
- o.dirs(0);
- o.find(LPCSTR(dir), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_file();
- CString path, temp, despath;
- for (it = coo.begin(); coo.end() != it; it++)
- {
- try
- {
- path = (*it).c_str();
- temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
- if (temp.GetAt(0) == 's')continue;
- despath = desdir + path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
- despath.MakeLower();
- if (PathFileExists(despath))
- {
- if (::CopyFile(path, despath, 0))
- g_bakphoto = "照片备份成功";
- }
- else
- {
- if (::CopyFile(path, despath, 0))
- g_bakphoto = "照片备份成功";
- }
- }
- catch (...)
- {
- }
- }
- }
- catch (...)
- {
- WriteLogin("出错在函数BakAllPhoto中");
- return 0;
- }
- return 1;
- }
- //--------------------------------------------------------------------------------
- // 函数:BakAllPhoto
- // 描述:备份相片
- // dir:客户原片、修好的片、精修片、设计片目录的订单文件夹路径;
- // desdir:dir对就的备份目录;
- // 参数:
- // 返回:
- //--------------------------------------------------------------------------------
- BOOL CPhotoBackupMgr::BakAllPhoto(CString dir, CString desdir, CString path1, CString path2, CString path3, CString path4)
- {
- try
- {
- CString childdir;
- CString srcdir = dir;
- srcdir.MakeLower();
- if (srcdir.Find(path1) != -1)
- childdir = srcdir.Right(srcdir.GetLength() - path1.GetLength());
- else if (srcdir.Find(path2) != -1)
- childdir = srcdir.Right(srcdir.GetLength() - path2.GetLength());
- else if (srcdir.Find(path3) != -1)
- childdir = srcdir.Right(srcdir.GetLength() - path3.GetLength());
- else if (srcdir.Find(path4) != -1)
- childdir = srcdir.Right(srcdir.GetLength() - path4.GetLength());
- else
- {
- WriteLogin("备份出错-目录未找到!");
- return 0;
- }
- childdir.TrimLeft("\\");
- childdir.TrimRight("\\");
- while (childdir.Find('\\') != -1)
- {
- desdir += "\\";
- desdir += childdir.Left(childdir.Find('\\'));
- CreateDirectory(desdir, NULL);
- childdir = childdir.Right(childdir.GetLength() - childdir.Find('\\') - 1);
- }
- desdir += "\\";
- desdir += childdir;
- CreateDirectory(desdir, NULL);
- desdir += "\\";
- ffsco o;
- o.dirs(0);
- o.find(LPCSTR(dir), LPCSTR("*.*"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_file();
- CString path, temp, despath;
- for (it = coo.begin(); coo.end() != it; it++)
- {
- try
- {
- path = (*it).c_str();
- temp = path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
- if (temp.GetAt(0) == 's')continue;
- despath = desdir + path.Right(path.GetLength() - path.ReverseFind('\\') - 1);
- if (CheckFileExist(despath))
- {
- if (::CopyFile(path, despath, 0))
- g_bakphoto = "照片备份成功";
- }
- else
- {
- if (::CopyFile(path, despath, 0))
- g_bakphoto = "照片备份成功";
- }
- }
- catch (...)
- {
- }
- }
- }
- catch (...)
- {
- WriteLogin("出错在函数BakAllPhoto中");
- return 0;
- }
- return 1;
- }
- //--------------------------------------------------------------------------------
- // 函数:IncrementWork
- // 描述:增量备份工作
- // dir:客户原片、修好的片、精修片、设计片目录的订单文件夹路径;
- // desdir:dir对就的备份目录;
- // 参数:
- // 返回:
- //--------------------------------------------------------------------------------
- void CPhotoBackupMgr::IncrementWork()
- {
- if(m_BakInterf == NULL)
- {
- WriteLogin(_T("备份模块没加载成功\n"));
- return;
- }
- if(m_BakInterf->IsBackup())
- return;
- //添加相片的共享目录
- vector<TblNetShareInfo*>::iterator it = CBranchInfo::GetInstance()->m_TblNetShareInfo.begin();
- for(;it != CBranchInfo::GetInstance()->m_TblNetShareInfo.end();)
- {
- TblNetShareInfo* p = (*it);
- if(p != NULL)
- m_BakInterf->InitShareDirData(p->bEnable, p->szBranchId, p->szShareDirectory, p->byMinCapacity, p->byMaxCapacity, p->byPhotoType, p->byPriority);
- ++it;
- }
- CString strError = _T("");
- //获取增量备份数据
- CArray<CStringArray, CStringArray> AryOfIncrement;
- CBranchInfo::GetInstance()->GetTblIncrementBackup(AryOfIncrement);
- if(AryOfIncrement.GetSize() == 0)
- {
- Sleep(1000);
- return;
- }
- for (int i = 0; i < AryOfIncrement.GetSize(); i++)
- {
- TCHAR szDomain[64] = {0};
- TCHAR szOrderNum[64] = {0};
- TCHAR szOptTime[32] = {0};
- TCHAR szPhotos[4096] = {0};
- TCHAR szDest[MAX_PATH] = {0};
- TCHAR szBackupDest[MAX_PATH] = {0};
- TCHAR* pPhotos = NULL;
-
- int nPhotoType = _ttoi(AryOfIncrement.ElementAt(i).ElementAt(4));
- _tcscpy(szDomain, AryOfIncrement.ElementAt(i).ElementAt(1));
- _tcscpy(szOrderNum, AryOfIncrement.ElementAt(i).ElementAt(3));
- _tcscpy(szOptTime, AryOfIncrement.ElementAt(i).ElementAt(2));
- pPhotos = new TCHAR[AryOfIncrement.ElementAt(i).ElementAt(5).GetLength() + 1];
- _tcscpy(pPhotos, AryOfIncrement.ElementAt(i).ElementAt(5));
- _tcscpy(szDest, m_bakDirArr.ElementAt(nPhotoType - 1).ElementAt(1));
-
- if(_tcscmp(szOrderNum, _T("")) == 0 || _tcscmp(szPhotos, _T("")) == 0)
- {
- if(pPhotos != NULL)
- delete [] pPhotos;
- continue;
- }
- CString strBackupDir = _T("");
- MakeBackupDir(nPhotoType, szDest, strBackupDir);
- if(szBackupDest == _T(""))
- {
- if(pPhotos != NULL)
- delete [] pPhotos;
- continue;
- }
- _tcscpy(szBackupDest, strBackupDir);
- m_BakInterf->InitBackupInfo(
- szDomain, // 店域名
- szOrderNum, // 订单号
- szOptTime, // 操作时间
- szPhotos, // 要备份的相片名集合
- szBackupDest, // 备份的目标目录,如:\\\\127.0.0.1\\客户原片备份(管理软件)$
- nPhotoType, // 相片类型
- FALSE // 是否备份m图
- );
- if(pPhotos != NULL)
- delete [] pPhotos;
- }
- //开始备份
- m_BakInterf->StartBackup(g_sdomain, BackupCallBack);
- Sleep(1000);
- }
- /************************************************************************/
- /*
- 函数:BackupFinish
- 描述:增量备份完成回调
- 参数:
- LPVOID, 回调传回值
- LPVOID,
- LPVOID,
- LPVOID,
- LPVOID
- 返回:1成功,0失败
- */
- /************************************************************************/
- int WINAPI CPhotoBackupMgr::BackupCallBack(IN LPVOID lpParam1, IN LPVOID lpParam2, IN LPVOID lpParam3, IN LPVOID lpParam4, IN LPVOID lpParam5, IN LPVOID lpParam6)
- {
- // 回调函数结束后,返回到DLL中,要再次调用DLL的对话框需要再次使用AFX_MANAGE_STATE;
- AfxSetResourceHandle(g_Instance);
- return CPhotoBackupMgr::GetInstance()->BackupCallBackPro(lpParam1, lpParam2, lpParam3, lpParam4, lpParam5, lpParam6);
- }
- /************************************************************************/
- /*
- 函数:BackupFinish
- 描述:增量备份完成回调
- 参数:
- LPVOID, 回调传回值
- LPVOID,
- LPVOID,
- LPVOID,
- LPVOID
- 返回:1成功,0失败
- */
- /************************************************************************/
- int CPhotoBackupMgr::BackupCallBackPro(IN LPVOID lpParam1, IN LPVOID lpParam2, IN LPVOID lpParam3, IN LPVOID lpParam4, IN LPVOID lpParam5, IN LPVOID lpParam6)
- {
- int nPercent = *((int*)lpParam1);
- CString strMsg = _T("");
- //strMsg.Format(_T("完成:%d\n"), nPercent);
- //OutputDebugString(strMsg);
- CAutoLock autolock(&m_lock);
- if ( nPercent == 100 )
- {
- try
- {
- CString strDate = _T("");
- strDate.Format(_T("update [IncrementalBackup] set [isbackup] = 1,[baktime] = getdate() where [branchid] = '%s' and [order] = '%s' and photoType = %d and content = '%s'\n"), (TCHAR*)lpParam2, (TCHAR*)lpParam4, *((BYTE*)lpParam5), (TCHAR*)lpParam6);
- m_pCCatalogObj->Execute(strDate);
- }
- catch (CException *e)
- {
- TCHAR szError[1024] = { 0 };
- e->GetErrorMessage(szError, 1024);
- CString strErrorMsg = _T("");
- strErrorMsg.Format(_T("增量备份回调错误:%s\n"), szError);
- WriteLogin(strErrorMsg);
- e->Delete();
- }
- }
- return 1;
- }
- /************************************************************************/
- /*
- 函数:LoadBakModule
- 描述:加载备份模
- 参数:
- IN HINSTANCE hInstance dll实例句柄
- 返回:1成功,0失败
- */
- /************************************************************************/
- int CPhotoBackupMgr::LoadBakModule(IN HINSTANCE hInstance)
- {
- if(hInstance == NULL)
- return 0;
- HRESULT hr = NULL;
- GetClassBackupObject getBackupObj = NULL;
- getBackupObj = (GetClassBackupObject)GetProcAddress(hInstance, "GetClassBackupObject");
- if(getBackupObj == NULL)
- {
- WriteLogin(_T("打开GetClassBackupObject失败\n"));
- return 0;
- }
- if(m_BakFact == NULL)
- {
- hr = getBackupObj(CLSID_SC_BACKUPINTERFACE, IID_IClassFactory, (void**)&m_BakFact);
- if(FAILED(hr))
- {
- WriteLogin(_T("获取CSC_BackupFactory指针失败\n"));
- return 0;
- }
- }
- if(m_BakInterf == NULL)
- {
- hr = m_BakFact->CreateInstance(NULL, IID_IUnknown, (void**)&m_BakInterf);
- if(FAILED(hr))
- {
- WriteLogin(_T("创建ISC_BackupInterface失败\n"));
- m_BakFact->Release();
- return 0;
- }
- }
- return 1;
- }
- /************************************************************************/
- /*
- 函数:FreeBakModule
- 描述:释放备份接
- 参数:
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::FreeBakModule()
- {
- if(m_BakInterf)
- m_BakInterf->Release();
- if(m_BakFact)
- m_BakFact->Release();
- }
- /************************************************************************/
- /*
- 函数:GetBackupOfSaveDir
- 描述:获取备份的保存目录
- 参数:
- OUT CArray<CStringArray, CStringArray> &AryOfValues 备份保存的目录
- 返回: TRUE, FALSE
- */
- /************************************************************************/
- BOOL CPhotoBackupMgr::GetBackupOfSaveDir(OUT CArray<CStringArray, CStringArray> &AryOfValues)
- {
- AryOfValues.RemoveAll();
- AryOfValues.SetSize(4, 1);
- CString bak1, bak2, bak3, bak4; // 是否备份(指定的备份目录是否需要备份);
- CString bak11, bak22, bak33, bak44; // 是否备份(集团版的分店备份目录是否需要备份??);
- CString bakserver1, bakserver2, bakserver3, bakserver4; // 相片备份目录 ;
- CAutoLock autolock(&m_lock);
- if(m_pCCatalogObj == NULL)
- return FALSE;
- try
- {
- CRstversion rsSt;
- rsSt.m_pDatabase = (CDatabase*)m_pCCatalogObj->GetCatalogInstance();
- rsSt.Open();
- if (!rsSt.IsEOF())
- {
- AryOfValues.ElementAt(0).RemoveAll();
- AryOfValues.ElementAt(0).Add(rsSt.m_bak1);
- AryOfValues.ElementAt(0).Add(rsSt.m_bakserver1);
- AryOfValues.ElementAt(0).Add(rsSt.bak11);
- AryOfValues.ElementAt(1).RemoveAll();
- AryOfValues.ElementAt(1).Add(rsSt.m_bak2);
- AryOfValues.ElementAt(1).Add(rsSt.m_bakserver2);
- AryOfValues.ElementAt(1).Add(rsSt.bak22);
- AryOfValues.ElementAt(2).RemoveAll();
- AryOfValues.ElementAt(2).Add(rsSt.m_bak3);
- AryOfValues.ElementAt(2).Add(rsSt.m_bakserver3);
- AryOfValues.ElementAt(2).Add(rsSt.bak33);
- AryOfValues.ElementAt(3).RemoveAll();
- AryOfValues.ElementAt(3).Add(rsSt.m_bak4);
- AryOfValues.ElementAt(3).Add(rsSt.m_bakserver4);
- AryOfValues.ElementAt(3).Add(rsSt.bak44);
- }
- rsSt.Close();
- }
- catch (...)
- {
- WriteLogin("获取备份保存的目录出错");
- }
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:GetLastDirTime
- 描述:获取文件最后修改时间
- 参数:
- CString dir 文件目录
- 返回: DWORD类型时间
- */
- /************************************************************************/
- DWORD CPhotoBackupMgr::GetLastDirTime(IN const CString& dir)
- {
- try
- {
- CString path = dir;
- path.TrimRight("\\");
- path += "\\";
- path += "modifytime";
- CStdioFile fp;
- if (::PathFileExists(path))
- {
- fp.Open(path, CFile::modeRead);
- fp.ReadString(path);
- fp.Close();
- path.TrimLeft(); path.TrimRight();
- path.Replace("-", "");
- return atoi(path);
- }
- else
- {
- ffsco o;
- o.dirs(1);
- o.find(LPCSTR(dir), LPCSTR("*.jpg"));
- ffsco::typeT coo;
- ffsco::typeT::iterator it;
- coo = o.co_file();
- CString path;
- DWORD maxdate = 0;
- for (it = coo.begin(); coo.end() != it; it++)
- {
- path = (*it).c_str();
- maxdate = max(maxdate, atol(GetModifyTime2(path)));
- }
- return maxdate;
- }
- }
- catch (...)
- {
- WriteLogin("出错");
- }
- }
- /************************************************************************/
- /*
- 函数:TerminateBakThread
- 描述:结束所有线程
- 参数:
- CString dir 文件目录
- 返回: DWORD类型时间
- */
- /************************************************************************/
- void CPhotoBackupMgr::TerminateBakThread()
- {
- TerminateIncrementalBak();
- TerminateAutoBak();
- }
- /************************************************************************/
- /*
- 函数:MakeBackupDir
- 描述:生成备份目录
- 参数:
- IN CONST CString& str, // 可能是共享计算机名/目录/ip地址
- OUT CString& strPath // 返回备份目录
- 返回:
- */
- /************************************************************************/
- void CPhotoBackupMgr::MakeBackupDir(IN CONST int nType, IN CONST CString& str, OUT CString& strPath)
- {
- if(str == _T(""))
- return;
- CString strTemp = str;
- strTemp.MakeLower();
- CString strFolderName = _T("");
- #ifdef CHILD_VERSION
- switch(nType + 4)
- {
- case 5:
- strFolderName = _T("客户原片备份(儿童管理软件)$");
- break;
- case 6:
- strFolderName = _T("修好的片备份(儿童管理软件)$");
- break;
- case 7:
- strFolderName = _T("精修好的片备份(儿童管理软件)$");
- break;
- case 8:
- strFolderName = _T("设计好的片备份(儿童管理软件)$");
- break;
- }
- #else
- switch(nType + 4)
- {
- case 5:
- strFolderName = _T("客户原片备份(管理软件)$");
- break;
- case 6:
- strFolderName = _T("修好的片备份(管理软件)$");
- break;
- case 7:
- strFolderName = _T("精修好的片备份(管理软件)$");
- break;
- case 8:
- strFolderName = _T("设计好的片备份(管理软件)$");
- break;
- }
- #endif
-
- char szServer[50] = {0};
- DWORD leng = 50;
- ::GetComputerName(szServer, &leng);
- CString strMainName = szServer;
- strMainName.MakeLower();
- if(strTemp.Find(strFolderName) != -1) //目录
- {
- if(strTemp.Find(_T(":\\")) != -1 || strTemp.Find(_T("\\\\")) != -1) // 绝对路径: D:\...\... (相对: \\...\...)
- strPath = strTemp;
- }
- else if(strTemp.CompareNoCase(strMainName) == 0) //是否为计算机名
- {
- strMainName.TrimLeft(_T("\\\\"));
- strMainName.TrimRight(_T("\\"));
- strPath = _T("\\\\") + strMainName + _T("\\") + strFolderName;
- }
- else //ip地址
- {
- if(strTemp.GetLength() <= 16)
- {
- CString strip = strTemp;
- int nCount = 0;
- while(1)
- {
- int nPos = strip.Find(_T("."));
- if(nPos == -1)
- break;
- strip = strip.Right((strip.GetLength() - 1) - nPos);
- if(strip == _T(""))
- break;
- ++nCount;
- }
- if(nCount == 3)
- strTemp = strPath;
- }
- }
- if(strPath != _T(""))
- strPath.MakeLower();
- }
|