DatabaseUtilities.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. // DatabaseUtilites.cpp: implementation of the CDatabaseUtilites class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "CP_Main.h"
  6. #include "DatabaseUtilities.h"
  7. #include "ProcessPaste.h"
  8. #include <io.h>
  9. #include "AccessToSqlite.h"
  10. #include "Path.h"
  11. #include "InternetUpdate.h"
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. BOOL CreateBackup(CString csPath)
  16. {
  17. CString csOriginal;
  18. int count = 0;
  19. // create a backup of the existing database
  20. do
  21. {
  22. count++;
  23. csOriginal = csPath + StrF(_T(".%03d"), count);
  24. // in case of some weird infinite loop
  25. if( count > 50 )
  26. {
  27. ASSERT(0);
  28. return FALSE;
  29. }
  30. } while( !::CopyFile(csPath, csOriginal, TRUE));
  31. return TRUE;
  32. }
  33. CString GetDBName()
  34. {
  35. return CGetSetOptions::GetDBPath();
  36. }
  37. CString GetOLDDefaultDBName()
  38. {
  39. CString csDefaultPath;
  40. LPMALLOC pMalloc;
  41. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  42. {
  43. LPITEMIDLIST pidlPrograms;
  44. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  45. TCHAR string[MAX_PATH];
  46. SHGetPathFromIDList(pidlPrograms, string);
  47. pMalloc->Free(pidlPrograms);
  48. pMalloc->Release();
  49. csDefaultPath = string;
  50. csDefaultPath += "\\Ditto\\";
  51. csDefaultPath += "DittoDB.mdb";
  52. }
  53. return csDefaultPath;
  54. }
  55. CString GetDefaultDBName()
  56. {
  57. CString csDefaultPath = _T("c:\\program files\\Ditto\\");
  58. if(g_Opt.m_bU3)
  59. {
  60. csDefaultPath = CGetSetOptions::GetPath(PATH_DATABASE);
  61. }
  62. else
  63. {
  64. //If portable then default to the running path
  65. if(CGetSetOptions::GetIsPortableDitto())
  66. {
  67. csDefaultPath.Empty();
  68. }
  69. else
  70. {
  71. LPMALLOC pMalloc;
  72. if(SUCCEEDED(::SHGetMalloc(&pMalloc)))
  73. {
  74. LPITEMIDLIST pidlPrograms;
  75. SHGetSpecialFolderLocation(NULL, CSIDL_APPDATA, &pidlPrograms);
  76. TCHAR string[MAX_PATH];
  77. SHGetPathFromIDList(pidlPrograms, string);
  78. pMalloc->Free(pidlPrograms);
  79. pMalloc->Release();
  80. csDefaultPath = string;
  81. }
  82. FIX_CSTRING_PATH(csDefaultPath);
  83. csDefaultPath += "Ditto\\";
  84. }
  85. }
  86. CString csTempName = csDefaultPath + "Ditto.db";
  87. int i = 1;
  88. while(FileExists(csTempName))
  89. {
  90. csTempName.Format(_T("%sDitto_%d.db"), csDefaultPath, i);
  91. i++;
  92. }
  93. csDefaultPath = csTempName;
  94. return csDefaultPath;
  95. }
  96. BOOL CheckDBExists(CString csDBPath)
  97. {
  98. //If this is the first time running this version then convert the old database to the new db
  99. if(csDBPath.IsEmpty() && g_Opt.m_bU3 == false)
  100. {
  101. csDBPath = GetDefaultDBName();
  102. if(FileExists(csDBPath) == FALSE && CGetSetOptions::GetIsPortableDitto() == FALSE)
  103. {
  104. CString csOldDB = CGetSetOptions::GetDBPathOld();
  105. if(csOldDB.IsEmpty())
  106. {
  107. csOldDB = GetOLDDefaultDBName();
  108. }
  109. if(FileExists(csOldDB))
  110. {
  111. //create the new sqlite db
  112. CreateDB(csDBPath);
  113. CAccessToSqlite Convert;
  114. Convert.ConvertDatabase(csDBPath, csOldDB);
  115. }
  116. }
  117. }
  118. BOOL bRet = FALSE;
  119. if(FileExists(csDBPath) == FALSE)
  120. {
  121. csDBPath = GetDefaultDBName();
  122. nsPath::CPath FullPath(csDBPath);
  123. CString csPath = FullPath.GetPath().GetStr();
  124. if(csPath.IsEmpty() == false && FileExists(csDBPath) == FALSE)
  125. {
  126. CreateDirectory(csPath, NULL);
  127. }
  128. // -- create a new one
  129. bRet = CreateDB(csDBPath);
  130. }
  131. else
  132. {
  133. if(ValidDB(csDBPath) == FALSE)
  134. {
  135. //Db existed but was bad
  136. CString csMarkAsBad;
  137. csMarkAsBad = csDBPath;
  138. csMarkAsBad.Replace(_T("."), _T("_BAD."));
  139. CString csPath = GetDefaultDBName();
  140. CString cs;
  141. cs.Format(_T("%s \"%s\",\n")
  142. _T("%s \"%s\",\n")
  143. _T("%s,\n")
  144. _T("\"%s\""),
  145. theApp.m_Language.GetString("Database_Format", "Unrecognized Database Format"),
  146. csDBPath,
  147. theApp.m_Language.GetString("File_Renamed", "the file will be renamed"),
  148. csMarkAsBad,
  149. theApp.m_Language.GetString("New_Database", "and a new database will be created"),
  150. csPath);
  151. AfxMessageBox(cs);
  152. CFile::Rename(csDBPath, csMarkAsBad);
  153. csDBPath = csPath;
  154. bRet = CreateDB(csDBPath);
  155. }
  156. else
  157. {
  158. bRet = TRUE;
  159. }
  160. }
  161. if(bRet)
  162. {
  163. bRet = OpenDatabase(csDBPath);
  164. }
  165. return bRet;
  166. }
  167. BOOL OpenDatabase(CString csDB)
  168. {
  169. try
  170. {
  171. theApp.m_db.close();
  172. theApp.m_db.open(csDB);
  173. CGetSetOptions::SetDBPath(csDB);
  174. return TRUE;
  175. }
  176. CATCH_SQLITE_EXCEPTION
  177. return FALSE;
  178. }
  179. BOOL ValidDB(CString csPath, BOOL bUpgrade)
  180. {
  181. try
  182. {
  183. BOOL didBackup = FALSE;
  184. CString backupFilePrefix = _T("Before_Update_To");
  185. CppSQLite3DB db;
  186. db.open(csPath);
  187. db.execQuery(_T("SELECT lID, lDate, mText, lShortCut, lDontAutoDelete, ")
  188. _T("CRC, bIsGroup, lParentID, QuickPasteText ")
  189. _T("FROM Main"));
  190. db.execQuery(_T("SELECT lID, lParentID, strClipBoardFormat, ooData FROM Data"));
  191. db.execQuery(_T("SELECT lID, TypeText FROM Types"));
  192. try
  193. {
  194. db.execDML(_T("DROP TRIGGER delete_data_trigger"));
  195. }
  196. catch(CppSQLite3Exception& e)
  197. {
  198. e.errorCode();
  199. }
  200. try
  201. {
  202. db.execDML(_T("DROP TRIGGER delete_copy_buffer_trigger"));
  203. }
  204. catch(CppSQLite3Exception& e)
  205. {
  206. e.errorCode();
  207. }
  208. //This was added later so try to add each time and catch the exception here
  209. try
  210. {
  211. if(didBackup == FALSE)
  212. didBackup = BackupDB(csPath, backupFilePrefix);
  213. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  214. _T("BEGIN\n")
  215. _T("INSERT INTO MainDeletes VALUES(old.lID, datetime('now'));\n")
  216. _T("END\n"));
  217. }
  218. catch(CppSQLite3Exception& e)
  219. {
  220. e.errorCode();
  221. }
  222. //This was added later so try to add each time and catch the exception here
  223. try
  224. {
  225. db.execQuery(_T("SELECT lID, lClipID, lCopyBuffer FROM CopyBuffers"));
  226. }
  227. catch(CppSQLite3Exception& e)
  228. {
  229. if(didBackup == FALSE)
  230. didBackup = BackupDB(csPath, backupFilePrefix);
  231. e.errorCode();
  232. db.execDML(_T("CREATE TABLE CopyBuffers(")
  233. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  234. _T("lClipID INTEGER,")
  235. _T("lCopyBuffer INTEGER)"));
  236. }
  237. //This was added later so try to add each time and catch the exception here
  238. try
  239. {
  240. db.execQuery(_T("SELECT clipId FROM MainDeletes"));
  241. }
  242. catch(CppSQLite3Exception& e)
  243. {
  244. if(didBackup == FALSE)
  245. didBackup = BackupDB(csPath, backupFilePrefix);
  246. e.errorCode();
  247. db.execDML(_T("CREATE TABLE MainDeletes(")
  248. _T("clipID INTEGER,")
  249. _T("modifiedDate)"));
  250. db.execDML(_T("CREATE TRIGGER MainDeletes_delete_data_trigger BEFORE DELETE ON MainDeletes FOR EACH ROW\n")
  251. _T("BEGIN\n")
  252. _T("DELETE FROM CopyBuffers WHERE lClipID = old.clipID;\n")
  253. _T("DELETE FROM Data WHERE lParentID = old.clipID;\n")
  254. _T("END\n"));
  255. }
  256. try
  257. {
  258. db.execDML(_T("CREATE INDEX Main_ParentId on Main(lParentID DESC)"));
  259. db.execDML(_T("CREATE INDEX Main_IsGroup on Main(bIsGroup DESC)"));
  260. db.execDML(_T("CREATE INDEX Main_ShortCut on Main(lShortCut DESC)"));
  261. }
  262. catch(CppSQLite3Exception& e)
  263. {
  264. e.errorCode();
  265. }
  266. try
  267. {
  268. db.execQuery(_T("SELECT clipOrder, clipGroupOrder FROM Main"));
  269. }
  270. catch(CppSQLite3Exception& e)
  271. {
  272. if(didBackup == FALSE)
  273. didBackup = BackupDB(csPath, backupFilePrefix);
  274. db.execDML(_T("ALTER TABLE Main ADD clipOrder REAL"));
  275. db.execDML(_T("ALTER TABLE Main ADD clipGroupOrder REAL"));
  276. db.execDML(_T("Update Main set clipOrder = lDate, clipGroupOrder = lDate"));
  277. db.execDML(_T("CREATE INDEX Main_ClipOrder on Main(clipOrder DESC)"));
  278. db.execDML(_T("CREATE INDEX Main_ClipGroupOrder on Main(clipGroupOrder DESC)"));
  279. db.execDML(_T("DROP INDEX Main_Date"));
  280. e.errorCode();
  281. }
  282. try
  283. {
  284. db.execQuery(_T("SELECT globalShortCut FROM Main"));
  285. }
  286. catch(CppSQLite3Exception& e)
  287. {
  288. if(didBackup == FALSE)
  289. didBackup = BackupDB(csPath, backupFilePrefix);
  290. db.execDML(_T("ALTER TABLE Main ADD globalShortCut INTEGER"));
  291. e.errorCode();
  292. }
  293. try
  294. {
  295. db.execQuery(_T("SELECT lastPasteDate FROM Main"));
  296. }
  297. catch(CppSQLite3Exception& e)
  298. {
  299. if(didBackup == FALSE)
  300. didBackup = BackupDB(csPath, backupFilePrefix);
  301. db.execDML(_T("ALTER TABLE Main ADD lastPasteDate INTEGER"));
  302. db.execDML(_T("Update Main set lastPasteDate = lDate"));
  303. db.execDMLEx(_T("Update Main set lastPasteDate = %d where lastPasteDate <= 0"), CTime::GetCurrentTime().GetTime());
  304. e.errorCode();
  305. }
  306. }
  307. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  308. return TRUE;
  309. }
  310. BOOL BackupDB(CString dbPath, CString prefix)
  311. {
  312. CString backup = GetFilePath(dbPath);
  313. CInternetUpdate update;
  314. long runningVersion = update.GetRunningVersion();
  315. CString versionString = update.GetVersionString(runningVersion);
  316. backup += GetFileName(dbPath) += _T("_") + prefix + _T("_") + versionString;
  317. backup.Replace(_T(".db"), _T(""));
  318. backup.Replace(_T("."), _T("_"));
  319. backup += + _T(".db");
  320. BOOL ret = CopyFile(dbPath, backup, TRUE);
  321. return ret;
  322. }
  323. BOOL CreateDB(CString csFile)
  324. {
  325. try
  326. {
  327. CppSQLite3DB db;
  328. db.open(csFile);
  329. db.execDML(_T("PRAGMA auto_vacuum = 1"));
  330. db.execDML(_T("CREATE TABLE Main(")
  331. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  332. _T("lDate INTEGER, ")
  333. _T("mText TEXT, ")
  334. _T("lShortCut INTEGER, ")
  335. _T("lDontAutoDelete INTEGER, ")
  336. _T("CRC INTEGER, ")
  337. _T("bIsGroup INTEGER, ")
  338. _T("lParentID INTEGER, ")
  339. _T("QuickPasteText TEXT, ")
  340. _T("clipOrder REAL, ")
  341. _T("clipGroupOrder REAL, ")
  342. _T("globalShortCut INTEGER, ")
  343. _T("lastPasteDate INTEGER);"));
  344. db.execDML(_T("CREATE TABLE Data(")
  345. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  346. _T("lParentID INTEGER, ")
  347. _T("strClipBoardFormat TEXT, ")
  348. _T("ooData BLOB);"));
  349. db.execDML(_T("CREATE TABLE Types(")
  350. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  351. _T("TypeText TEXT);"));
  352. db.execDML(_T("CREATE UNIQUE INDEX Main_ID on Main(lID ASC)"));
  353. db.execDML(_T("CREATE UNIQUE INDEX Data_ID on Data(lID ASC)"));
  354. db.execDML(_T("CREATE INDEX Main_ClipOrder on Main(clipOrder DESC)"));
  355. db.execDML(_T("CREATE INDEX Main_ClipGroupOrder on Main(clipGroupOrder DESC)"));
  356. db.execDML(_T("CREATE INDEX Main_ParentId on Main(lParentID DESC)"));
  357. db.execDML(_T("CREATE INDEX Main_IsGroup on Main(bIsGroup DESC)"));
  358. db.execDML(_T("CREATE INDEX Main_ShortCut on Main(lShortCut DESC)"));
  359. db.execDML(_T("CREATE TRIGGER delete_data_trigger BEFORE DELETE ON Main FOR EACH ROW\n")
  360. _T("BEGIN\n")
  361. _T("INSERT INTO MainDeletes VALUES(old.lID, datetime('now'));\n")
  362. _T("END\n"));
  363. db.execDML(_T("CREATE TABLE CopyBuffers(")
  364. _T("lID INTEGER PRIMARY KEY AUTOINCREMENT, ")
  365. _T("lClipID INTEGER, ")
  366. _T("lCopyBuffer INTEGER)"));
  367. db.execDML(_T("CREATE TABLE MainDeletes(")
  368. _T("clipID INTEGER,")
  369. _T("modifiedDate)"));
  370. db.execDML(_T("CREATE TRIGGER MainDeletes_delete_data_trigger BEFORE DELETE ON MainDeletes FOR EACH ROW\n")
  371. _T("BEGIN\n")
  372. _T("DELETE FROM CopyBuffers WHERE lClipID = old.clipID;\n")
  373. _T("DELETE FROM Data WHERE lParentID = old.clipID;\n")
  374. _T("END\n"));
  375. db.close();
  376. }
  377. CATCH_SQLITE_EXCEPTION_AND_RETURN(FALSE)
  378. return TRUE;
  379. }
  380. BOOL CompactDatabase()
  381. {
  382. // if(!theApp.CloseDB())
  383. // return FALSE;
  384. //
  385. // CString csDBName = GetDBName();
  386. // CString csTempDBName = csDBName;
  387. // csTempDBName.Replace(".mdb", "TempDBName.mdb");
  388. //
  389. // //Compact the database
  390. // try
  391. // {
  392. // CDaoWorkspace::CompactDatabase(csDBName, csTempDBName);//, dbLangGeneral, 0, "andrew");//DATABASE_PASSWORD);
  393. // }
  394. // catch(CDaoException* e)
  395. // {
  396. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  397. // DeleteFile(csTempDBName);
  398. // e->Delete();
  399. // return FALSE;
  400. // }
  401. // catch(CMemoryException* e)
  402. // {
  403. // AfxMessageBox("Memory Exception");
  404. // DeleteFile(csTempDBName);
  405. // e->Delete();
  406. // return FALSE;
  407. // }
  408. //
  409. // //Since compacting the database creates a new db delete the old one and replace it
  410. // //with the compacted db
  411. // if(DeleteFile(csDBName))
  412. // {
  413. // try
  414. // {
  415. // CFile::Rename(csTempDBName, csDBName);
  416. // }
  417. // catch(CFileException *e)
  418. // {
  419. // e->ReportError();
  420. // e->Delete();
  421. // return FALSE;
  422. // }
  423. // }
  424. // else
  425. // AfxMessageBox("Error Compacting Database");
  426. return TRUE;
  427. }
  428. BOOL RepairDatabase()
  429. {
  430. // if(!theApp.CloseDB())
  431. // return FALSE;
  432. // try
  433. // {
  434. // CDaoWorkspace::RepairDatabase(GetDBName());
  435. // }
  436. // catch(CDaoException *e)
  437. // {
  438. // AfxMessageBox(e->m_pErrorInfo->m_strDescription);
  439. // e->Delete();
  440. // return FALSE;
  441. // }
  442. return TRUE;
  443. }
  444. BOOL RemoveOldEntries()
  445. {
  446. Log(StrF(_T("Beginning of RemoveOldEntries MaxEntries: %d - Keep days: %d"), CGetSetOptions::GetMaxEntries(), CGetSetOptions::GetExpiredEntries()));
  447. try
  448. {
  449. CppSQLite3DB db;
  450. CString csDbPath = CGetSetOptions::GetDBPath();
  451. db.open(csDbPath);
  452. if(CGetSetOptions::GetCheckForMaxEntries())
  453. {
  454. long lMax = CGetSetOptions::GetMaxEntries();
  455. if(lMax >= 0)
  456. {
  457. CClipIDs IDs;
  458. int clipId;
  459. CppSQLite3Query q = db.execQueryEx(_T("SELECT lID, lShortCut, lParentID, lDontAutoDelete FROM Main WHERE bIsGroup = 0 ORDER BY clipOrder DESC LIMIT -1 OFFSET %d"), lMax);
  460. while(q.eof() == false)
  461. {
  462. int shortcut = q.getIntField(_T("lShortCut"));
  463. int dontDelete = q.getIntField(_T("lDontAutoDelete"));
  464. int parentId = q.getIntField(_T("lParentID"));
  465. //Only delete entries that have no shortcut and don't have the flag set
  466. if(shortcut == 0 &&
  467. dontDelete == 0 &&
  468. parentId <= 0)
  469. {
  470. clipId = q.getIntField(_T("lID"));
  471. IDs.Add(clipId);
  472. Log(StrF(_T("From MaxEntries - Deleting Id: %d"), clipId));
  473. }
  474. q.nextRow();
  475. }
  476. if(IDs.GetCount() > 0)
  477. {
  478. IDs.DeleteIDs(false, db);
  479. }
  480. }
  481. }
  482. if(CGetSetOptions::GetCheckForExpiredEntries())
  483. {
  484. long lExpire = CGetSetOptions::GetExpiredEntries();
  485. if(lExpire)
  486. {
  487. CTime now = CTime::GetCurrentTime();
  488. now -= CTimeSpan(lExpire, 0, 0, 0);
  489. CClipIDs IDs;
  490. CppSQLite3Query q = db.execQueryEx(_T("SELECT lID FROM Main ")
  491. _T("WHERE lastPasteDate < %d AND ")
  492. _T("bIsGroup = 0 AND lShortCut = 0 AND lParentID <= 0 AND lDontAutoDelete = 0"), now.GetTime());
  493. while(q.eof() == false)
  494. {
  495. IDs.Add(q.getIntField(_T("lID")));
  496. Log(StrF(_T("From Clips Expire - Deleting Id: %d"), q.getIntField(_T("lID"))));
  497. q.nextRow();
  498. }
  499. if(IDs.GetCount() > 0)
  500. {
  501. IDs.DeleteIDs(false, db);
  502. }
  503. }
  504. }
  505. Log(_T("Before Deleting emptied out data"));
  506. //delete any data items sitting out there that the main table data was deleted
  507. //this was done to speed up deleted from the main table
  508. int deleteCount = db.execDML(_T("DELETE FROM MainDeletes"));
  509. Log(StrF(_T("After Deleting emptied out data rows, Count: %d"), deleteCount));
  510. }
  511. CATCH_SQLITE_EXCEPTION
  512. Log(_T("End of RemoveOldEntries"));
  513. return TRUE;
  514. }
  515. BOOL EnsureDirectory(CString csPath)
  516. {
  517. TCHAR drive[_MAX_DRIVE];
  518. TCHAR dir[_MAX_DIR];
  519. TCHAR fname[_MAX_FNAME];
  520. TCHAR ext[_MAX_EXT];
  521. SPLITPATH(csPath, drive, dir, fname, ext);
  522. CString csDir(drive);
  523. csDir += dir;
  524. if(FileExists(csDir) == FALSE)
  525. {
  526. if(CreateDirectory(csDir, NULL))
  527. return TRUE;
  528. }
  529. else
  530. return TRUE;
  531. return FALSE;
  532. }
  533. // BOOL RunZippApp(CString csCommandLine)
  534. // {
  535. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  536. // FIX_CSTRING_PATH(csLocalPath);
  537. //
  538. // CString csZippApp = GETENV(_T("U3_DEVICE_EXEC_PATH"));
  539. // FIX_CSTRING_PATH(csZippApp);
  540. // csZippApp += "7za.exe";
  541. //
  542. // csZippApp += " ";
  543. // csZippApp += csCommandLine;
  544. //
  545. // Log(csZippApp);
  546. //
  547. // STARTUPINFO StartupInfo;
  548. // PROCESS_INFORMATION ProcessInformation;
  549. //
  550. // ZeroMemory(&StartupInfo, sizeof(StartupInfo));
  551. // StartupInfo.cb = sizeof(StartupInfo);
  552. // ZeroMemory(&ProcessInformation, sizeof(ProcessInformation));
  553. //
  554. // StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
  555. // StartupInfo.wShowWindow = SW_HIDE;
  556. //
  557. // BOOL bRet = CreateProcess(NULL, csZippApp.GetBuffer(csZippApp.GetLength()), NULL, NULL, FALSE,
  558. // CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS, NULL, csLocalPath,
  559. // &StartupInfo, &ProcessInformation);
  560. //
  561. // if(bRet)
  562. // {
  563. // WaitForSingleObject(ProcessInformation.hProcess, INFINITE);
  564. //
  565. // DWORD dwExitCode;
  566. // GetExitCodeProcess(ProcessInformation.hProcess, &dwExitCode);
  567. //
  568. // CString cs;
  569. // cs.Format(_T("Exit code from unzip = %d"), dwExitCode);
  570. // Log(cs);
  571. //
  572. // if(dwExitCode != 0)
  573. // {
  574. // bRet = FALSE;
  575. // }
  576. // }
  577. // else
  578. // {
  579. // bRet = FALSE;
  580. // Log(_T("Create Process Failed"));
  581. // }
  582. //
  583. // csZippApp.ReleaseBuffer();
  584. //
  585. // return bRet;
  586. // }
  587. // BOOL CopyDownDatabase()
  588. // {
  589. // BOOL bRet = FALSE;
  590. //
  591. // CString csZippedPath = GETENV(_T("U3_APP_DATA_PATH"));
  592. // FIX_CSTRING_PATH(csZippedPath);
  593. //
  594. // CString csUnZippedPath = csZippedPath;
  595. // csUnZippedPath += "Ditto.db";
  596. //
  597. // csZippedPath += "Ditto.7z";
  598. //
  599. // CString csLocalPath = GETENV(_T("U3_HOST_EXEC_PATH"));
  600. // FIX_CSTRING_PATH(csLocalPath);
  601. //
  602. // if(FileExists(csZippedPath))
  603. // {
  604. // CString csCommandLine;
  605. //
  606. // //e = extract
  607. // //surround command line arguments with quotes
  608. // //-aoa = overight files with extracted files
  609. //
  610. // csCommandLine += "e ";
  611. // csCommandLine += "\"";
  612. // csCommandLine += csZippedPath;
  613. // csCommandLine += "\"";
  614. // csCommandLine += " -o";
  615. // csCommandLine += "\"";
  616. // csCommandLine += csLocalPath;
  617. // csCommandLine += "\"";
  618. // csCommandLine += " -aoa";
  619. //
  620. // bRet = RunZippApp(csCommandLine);
  621. //
  622. // csLocalPath += "Ditto.db";
  623. // }
  624. // else if(FileExists(csUnZippedPath))
  625. // {
  626. // csLocalPath += "Ditto.db";
  627. // bRet = CopyFile(csUnZippedPath, csLocalPath, FALSE);
  628. // }
  629. //
  630. // if(FileExists(csLocalPath) == FALSE)
  631. // {
  632. // Log(_T("Failed to copy files from device zip file"));
  633. // }
  634. //
  635. // g_Opt.nLastDbWriteTime = GetLastWriteTime(csLocalPath);
  636. //
  637. // return bRet;
  638. // }
  639. //BOOL CopyUpDatabase()
  640. //{
  641. // CStringA csZippedPath = "C:\\";//getenv("U3_APP_DATA_PATH");
  642. // FIX_CSTRING_PATH(csZippedPath);
  643. // csZippedPath += "Ditto.zip";
  644. // CStringA csLocalPath = GetDBName();//getenv("U3_HOST_EXEC_PATH");
  645. // //FIX_CSTRING_PATH(csLocalPath);
  646. // //csLocalPath += "Ditto.db";
  647. //
  648. // CZipper Zip;
  649. //
  650. // if(Zip.OpenZip(csZippedPath))
  651. // {
  652. // Zip.AddFileToZip(csLocalPath);
  653. // }
  654. //
  655. // return TRUE;
  656. //}