OTSFileSys.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // OTSFileSys.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "OTSModel.h"
  5. #include "OTSFileSys.h"
  6. #include "OTSHelper.h"
  7. #include "COTSUtilityDllFunExport.h"
  8. // COTSFileSys
  9. namespace OTSMODEL {
  10. using namespace OTSDATA;
  11. // COTSFileSys member functions
  12. // private functions:
  13. COTSFileSys::COTSFileSys()
  14. {
  15. }
  16. COTSFileSys::~COTSFileSys()
  17. {
  18. }
  19. BOOL HasAttribute(LPCTSTR a_strFolder, DWORD a_nAttribute)
  20. {
  21. DWORD flags = GetFileAttributes(a_strFolder);
  22. return (flags != INVALID_FILE_ATTRIBUTES) &&
  23. (flags & a_nAttribute);
  24. }
  25. // check if the file exists or not
  26. BOOL COTSFileSys::Exists(LPCTSTR a_sPath)
  27. {
  28. return ::PathFileExists(a_sPath) == TRUE;
  29. }
  30. // check if the given string is valid file name or not
  31. BOOL COTSFileSys::IsValidFileName(LPCTSTR a_sFileName)
  32. {
  33. CString strFileName = a_sFileName;
  34. const CString INVALIDFILENAMECHAR(_T("\\/:*?\"<>"));
  35. return strFileName.FindOneOf(INVALIDFILENAMECHAR) == -1;
  36. }
  37. // copy a file
  38. BOOL COTSFileSys::CopyAFile(LPCTSTR a_strSourceFile, LPCTSTR a_strTargetFile, const BOOL a_bOverwrite /*= FALSE*/)
  39. {
  40. // make sure the two file name string are not empty
  41. ASSERT(a_strSourceFile);
  42. ASSERT(a_strTargetFile);
  43. if (!a_strSourceFile || !a_strTargetFile)
  44. {
  45. LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: invalid file name"));
  46. return FALSE;
  47. }
  48. // make sure the source file exist
  49. if (!Exists(a_strSourceFile))
  50. {
  51. LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: source file doesn't exist"));
  52. return FALSE;
  53. }
  54. // quit if the target file exists and can't be overwritten
  55. if (!a_bOverwrite && Exists(a_strTargetFile))
  56. {
  57. LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: target file exists and can't be overwritten"));
  58. return FALSE;
  59. }
  60. // copy file
  61. if (!::CopyFile(a_strSourceFile, a_strTargetFile, !a_bOverwrite))
  62. {
  63. LogErrorTrace(__FILE__, __LINE__, _T("CopyAFile: copy(%s) file to %s failed.error : %s"), a_strSourceFile, a_strTargetFile, COTSHelper::GetSystemErrorString(GetLastError()));
  64. return FALSE;
  65. }
  66. // ok, return TRUE
  67. return TRUE;
  68. }
  69. // move a file
  70. BOOL COTSFileSys::MoveAFile(LPCTSTR a_strSourceFile, LPCTSTR a_strTargetFile, const BOOL a_bOverwrite /*= TRUE*/)
  71. {
  72. // make sure the two file name string are not empty
  73. ASSERT(a_strSourceFile);
  74. ASSERT(a_strTargetFile);
  75. if (!a_strSourceFile || !a_strTargetFile)
  76. {
  77. LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: invalid file name"));
  78. return FALSE;
  79. }
  80. // make sure the source file exist
  81. if (!Exists(a_strSourceFile))
  82. {
  83. LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: source file doesn't exist"));
  84. return FALSE;
  85. }
  86. // the target exists?
  87. if (Exists(a_strTargetFile))
  88. {
  89. // quit if the target file can't be overwritten
  90. if(!a_bOverwrite)
  91. {
  92. LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: target file exists and can't be overwritten"));
  93. return FALSE;
  94. }
  95. if (DeleteAFile(a_strTargetFile))
  96. {
  97. LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: can't delete the exist file %s"), a_strTargetFile);
  98. return FALSE;
  99. }
  100. }
  101. if (!::MoveFile(a_strSourceFile, a_strTargetFile))
  102. {
  103. LogErrorTrace(__FILE__, __LINE__, _T("MoveAFile: move(%s) file to %s failed.error : %s"), a_strSourceFile, a_strTargetFile, COTSHelper::GetSystemErrorString(GetLastError()));
  104. return FALSE;
  105. }
  106. // ok, return TRUE
  107. return TRUE;
  108. }
  109. // delete a file.
  110. BOOL COTSFileSys::DeleteAFile(LPCTSTR a_strFileName)
  111. {
  112. // make sure the file name string is not empty
  113. ASSERT(a_strFileName);
  114. if (!a_strFileName)
  115. {
  116. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: invalid file name"));
  117. return FALSE;
  118. }
  119. // return TRUE if the file is not exist
  120. if (!Exists(a_strFileName))
  121. {
  122. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: %s file does not exist."), a_strFileName);
  123. return TRUE;
  124. }
  125. // quit if the file is read-only
  126. if (IsReadOnly(a_strFileName))
  127. {
  128. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: %s file is readonly."), a_strFileName);
  129. return FALSE;
  130. }
  131. if (!::DeleteFile(a_strFileName))
  132. {
  133. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAFile: delete (%s) file failed. error: %s"), a_strFileName, COTSHelper::GetSystemErrorString(GetLastError()));
  134. }
  135. // ok, return TRUE
  136. return TRUE;
  137. }
  138. // delete all files in the folder
  139. BOOL COTSFileSys::DeleteAllFiles(LPCTSTR a_strFolder, LPCTSTR a_sFilter /*= nullptr*/)
  140. {
  141. // make sure the folder name string are not empty
  142. ASSERT(a_strFolder);
  143. if (!a_strFolder)
  144. {
  145. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: invalid folder name"));
  146. return FALSE;
  147. }
  148. // return TRUE if the folder is not exist
  149. if (!Exists(a_strFolder))
  150. {
  151. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s folder does not exist."), a_strFolder);
  152. return TRUE;
  153. }
  154. // make sure this is a folder
  155. if (!IsFolder(a_strFolder))
  156. {
  157. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s is not a folder."), a_strFolder);
  158. return FALSE;
  159. }
  160. // is the folder read-only?
  161. if (IsReadOnly(a_strFolder))
  162. {
  163. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: %s folder is readonly."), a_strFolder);
  164. return FALSE;
  165. }
  166. // get all files in the folder
  167. CFileFind searchFile;
  168. CString filePattern(a_strFolder);
  169. if (a_sFilter)
  170. {
  171. filePattern += _T("\\");
  172. filePattern += a_sFilter;
  173. }
  174. else
  175. {
  176. filePattern += _T("\\*.*");
  177. }
  178. BOOL ret = searchFile.FindFile(filePattern);
  179. // delete all files in the folder
  180. while (ret)
  181. {
  182. // get a file from the folder
  183. ret = searchFile.FindNextFile();
  184. CString filePath = searchFile.GetFilePath();
  185. // make sure that this is a real file, jump over if is not
  186. if (searchFile.IsDots() || searchFile.IsDirectory())
  187. {
  188. continue;
  189. }
  190. // delete the file
  191. if (!DeleteAFile(searchFile.GetFilePath()))
  192. {
  193. LogErrorTrace(__FILE__, __LINE__, _T("DeleteAllFiles: delete (%s) file failed."), searchFile.GetFilePath());
  194. return FALSE;
  195. }
  196. }
  197. searchFile.Close();
  198. // ok, return TRUE
  199. return TRUE;
  200. }
  201. // creates a folder.
  202. BOOL COTSFileSys::CreateFolder(LPCTSTR a_strFolder)
  203. {
  204. // make sure the folder name string are not empty
  205. CString strFolder = a_strFolder;
  206. strFolder.Trim();
  207. if (strFolder.IsEmpty())
  208. {
  209. LogErrorTrace(__FILE__, __LINE__, "CreateFolder: invalid folder name is empty.");
  210. return FALSE;
  211. }
  212. // if the folder exist?
  213. if (Exists(strFolder))
  214. {
  215. // is a real folder?
  216. if (IsFolder(strFolder))
  217. {
  218. return TRUE;
  219. }
  220. }
  221. // create folder
  222. // remove back slash if there
  223. CString strParentFolder = strFolder;
  224. strParentFolder.Trim(_T("\\"));
  225. // find last back slash position
  226. int nBackSlashPos = strParentFolder.ReverseFind(_T('\\'));
  227. if (nBackSlashPos > 0)
  228. {
  229. // get the folder name
  230. CString strFolderName = strParentFolder.Right(strParentFolder.GetLength() - nBackSlashPos - 1);
  231. // separate the folder string
  232. strParentFolder = strParentFolder.Left(nBackSlashPos);
  233. if (!IsValidFileName(strFolderName))
  234. {
  235. LogErrorTrace(__FILE__, __LINE__, "CreateFolder: invalid folder or file name %s.", strFolderName);
  236. return FALSE;
  237. }
  238. // try to create folder from the base folder
  239. if (!CreateFolder(strParentFolder))
  240. {
  241. LogErrorTrace(__FILE__, __LINE__, "CreateFolder: failed to create %s folder.", strParentFolder);
  242. return FALSE;
  243. }
  244. }
  245. // create the folder
  246. BOOL bRet = ::CreateDirectory(strFolder, NULL) == TRUE;
  247. // return folder create result
  248. return bRet;
  249. }
  250. // deletes a folder and contents recursively.
  251. BOOL COTSFileSys::DeleteFolder(LPCTSTR a_strFolder)
  252. {
  253. // make sure the folder name string are not empty
  254. CString strFolder = a_strFolder;
  255. strFolder.Trim();
  256. if (strFolder.IsEmpty())
  257. {
  258. LogErrorTrace(__FILE__, __LINE__, _T("CreateFolder: invalid folder name is empty."));
  259. return FALSE;
  260. }
  261. // quit if the folder is not exist
  262. if (!Exists(strFolder))
  263. {
  264. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s folder is not exist."), strFolder);
  265. return TRUE;
  266. }
  267. // make sure it is a folder
  268. if (!IsFolder(strFolder))
  269. {
  270. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s is a folder."), strFolder);
  271. return FALSE;
  272. }
  273. // readonly?
  274. if (IsReadOnly(strFolder))
  275. {
  276. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: %s folder is readonly."), strFolder);
  277. return FALSE;
  278. }
  279. // remove every thing in the folder
  280. CFileFind fileSearch;
  281. CString filePattern(strFolder);
  282. filePattern += _T("\\*.*");
  283. BOOL ret = fileSearch.FindFile(filePattern);
  284. while (ret)
  285. {
  286. // get a file or a folder
  287. ret = fileSearch.FindNextFile();
  288. // jump over if this is a dots item
  289. if (fileSearch.IsDots())
  290. {
  291. continue;
  292. }
  293. // is a sub folder
  294. if (fileSearch.IsDirectory())
  295. {
  296. // delete the sub folder
  297. if (!DeleteFolder(fileSearch.GetFilePath()))
  298. {
  299. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: delete %s sub folder failed."), fileSearch.GetFilePath());
  300. return FALSE;
  301. }
  302. }
  303. // or a file, delete the file
  304. else if (!DeleteAFile(fileSearch.GetFilePath()))
  305. {
  306. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: delete %s file failed."), fileSearch.GetFilePath());
  307. return FALSE;
  308. }
  309. }
  310. // delete the folderstrFolder
  311. if (!::RemoveDirectory(a_strFolder))
  312. {
  313. LogErrorTrace(__FILE__, __LINE__, _T("DeleteFolder: remove directory %s failed."), strFolder);
  314. return FALSE;
  315. }
  316. // ok return TRUE
  317. return TRUE;
  318. }
  319. // check if this is an existing folder
  320. BOOL COTSFileSys::IsFolder(LPCTSTR a_strFolder)
  321. {
  322. if (::PathIsDirectory(a_strFolder))
  323. {
  324. return HasAttribute(a_strFolder, FILE_ATTRIBUTE_DIRECTORY);
  325. }
  326. return FALSE;
  327. }
  328. // check if the file or folder is read-only
  329. BOOL COTSFileSys::IsReadOnly(LPCTSTR a_strPathName)
  330. {
  331. return HasAttribute(a_strPathName, FILE_ATTRIBUTE_READONLY);
  332. }
  333. // sets the read-only flag for a file or a folder.
  334. BOOL COTSFileSys::SetReadOnly(LPCTSTR a_strPathName, BOOL a_bReadOnly /*=TRUE*/)
  335. {
  336. DWORD flags = GetFileAttributes(a_strPathName);
  337. if (a_bReadOnly)
  338. {
  339. flags |= FILE_ATTRIBUTE_READONLY;
  340. }
  341. else
  342. {
  343. flags &= ~FILE_ATTRIBUTE_READONLY;
  344. }
  345. return ::SetFileAttributes(a_strPathName, flags) != 0;
  346. }
  347. // get system common data folder pathname
  348. // return "" if failed
  349. CString COTSFileSys::GetOSCommonDataPathName()
  350. {
  351. CString strPathName("");
  352. if (strPathName.Right(1) != _T(".\\"))
  353. {
  354. strPathName += _T(".\\");
  355. }
  356. return strPathName;
  357. }
  358. // get company system data path
  359. CString COTSFileSys::GetCompanySysDataPathName()
  360. {
  361. // get common data pathname string
  362. CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
  363. if (strCommonDataPathName.IsEmpty())
  364. {
  365. // failed to get common data pathname string
  366. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data pathname string."));
  367. return _T("");
  368. }
  369. // company system data pathname
  370. // e.g. "c:\ProgramData\Config\"
  371. CString strCmpSysDataPath = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_SYSTEM_DATA + _T("\\");
  372. // return company system data pathname
  373. return strCmpSysDataPath;
  374. }
  375. // get company log pathname
  376. CString COTSFileSys::GetCompanyLogPathName()
  377. {
  378. // get common data pathname string
  379. CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
  380. if (strCommonDataPathName.IsEmpty())
  381. {
  382. // failed to get company system data folder string
  383. LogErrorTrace(__FILE__, __LINE__, _T("GetCompayLogPathName: failed to common data pathname string."));
  384. return _T("");
  385. }
  386. // software package log path
  387. // e.g. "c:\ProgramData\Log\"
  388. CString strCompanyLogPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_LOG + _T("\\");
  389. // return software package log path
  390. return strCompanyLogPathName;
  391. }
  392. // get software pack system data path
  393. CString COTSFileSys::GetOTSPackSysDataPathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  394. {
  395. // get app package name
  396. CString strAppPackageName(_T(""));
  397. switch (a_nPackId)
  398. {
  399. case OTS_SOFT_PACKAGE_ID::OTSIncA:
  400. strAppPackageName = STR_APPNAME_OTSINCA;
  401. break;
  402. case OTS_SOFT_PACKAGE_ID::OTSPartA:
  403. strAppPackageName = STR_APPNAME_OTSINCA;
  404. break;
  405. default:
  406. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: invalid software package id."));
  407. return _T("");
  408. }
  409. // get common data pathname string
  410. CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
  411. if (strCommonDataPathName.IsEmpty())
  412. {
  413. // can't common data path
  414. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackSysDataPathName: failed to common data path."));
  415. return _T("");
  416. }
  417. // software package system data pathname
  418. // e.g. "c:\ProgramData\Config\SysData\"
  419. //CString strOTSSysDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + strAppPackageName + _T("\\") + STR_SYSTEM_DATA + _T("\\");
  420. CString strOTSSysDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_SYSTEM_DATA + _T("\\");
  421. // return software package system data path
  422. return strOTSSysDataPathName;
  423. }
  424. // get software pack program data path
  425. CString COTSFileSys::GetOTSPackProgDataPathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  426. {
  427. // get app package name
  428. CString strAppPackageName(_T(""));
  429. switch (a_nPackId)
  430. {
  431. case OTS_SOFT_PACKAGE_ID::OTSIncA:
  432. strAppPackageName = STR_APPNAME_OTSINCA;
  433. break;
  434. case OTS_SOFT_PACKAGE_ID::OTSPartA:
  435. strAppPackageName = STR_APPNAME_OTSINCA;
  436. break;
  437. default:
  438. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackProgDataPathName: invalid software package id."));
  439. return _T("");
  440. }
  441. // get common data pathname string
  442. CString strCommonDataPathName = COTSFileSys::GetOSCommonDataPathName();
  443. if (strCommonDataPathName.IsEmpty())
  444. {
  445. // can't common data path
  446. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackProgDataPathName: failed to common data path."));
  447. return _T("");
  448. }
  449. // software package program data pathname
  450. // e.g. "c:\ProgramData\Config\ProData\"
  451. //CString strOTSProDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + strAppPackageName + _T("\\") + STR_PROG_DATA + _T("\\");
  452. CString strOTSProDataPathName = strCommonDataPathName + STR_COMPANYNAME + _T("\\") + STR_PROG_DATA + _T("\\");
  453. // return software package program data path
  454. return strOTSProDataPathName;
  455. }
  456. // get software pack preference file path name
  457. CString COTSFileSys::GetOTSPackMeasurePrefFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  458. {
  459. // get software package system data pathname
  460. CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
  461. // check if software package system data pathname is right
  462. if (strOTSPackSysDataPathName.IsEmpty())
  463. {
  464. // failed to get software package system data pathname
  465. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
  466. return _T("");
  467. }
  468. // software package project manager file pathname
  469. // i.e. "c:\ProgramData\Config\SysData\OTSProgMgrParam.pmf"
  470. CString strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_MEASURE_PREFERENCE_FILE_NAME;
  471. // return software package license file pathname
  472. return strOTSPackProgMgrPathName;
  473. }
  474. // get software pack preference file path name
  475. CString COTSFileSys::GetOTSPackReportPrefFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  476. {
  477. // get software package system data pathname
  478. CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
  479. // check if software package system data pathname is right
  480. if (strOTSPackSysDataPathName.IsEmpty())
  481. {
  482. // failed to get software package system data pathname
  483. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
  484. return _T("");
  485. }
  486. // software package project manager file pathname
  487. // i.e. "c:\ProgramData\Config\SysData\OTSReportMgrParam.rp"
  488. CString strOTSPackProgMgrPathName = strOTSPackSysDataPathName + STR_REPORT_PREFERENCE_FILE_NAME;
  489. // return software package license file pathname
  490. return strOTSPackProgMgrPathName;
  491. }
  492. CString COTSFileSys::GetOTSPackSysSTDLibFilePathName(OTS_SOFT_PACKAGE_ID a_nPackId)
  493. {
  494. // get software package system data pathname
  495. CString strOTSPackSysDataPathName = COTSFileSys::GetOTSPackSysDataPathName(a_nPackId);
  496. // check if software package system data pathname is right
  497. if (strOTSPackSysDataPathName.IsEmpty())
  498. {
  499. // failed to get software package system data pathname
  500. LogErrorTrace(__FILE__, __LINE__, _T("GetOTSPackMeasurePrefFilePathName: failed to get software package system data path string."));
  501. return _T("");
  502. }
  503. // software package project manager file pathname
  504. // i.e. "c:\ProgramData\Config\SysData\SySSTDData.db"
  505. CString strOTSPackSysSTDlibPathName = strOTSPackSysDataPathName + SYS_STD_LIB_FILE_NAME;
  506. // return software package license file pathname
  507. return strOTSPackSysSTDlibPathName;
  508. }
  509. }