Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
4.0 KiB

  1. //
  2. // MODULE: FileTracker.cpp
  3. //
  4. // PURPOSE: Abstract classes in support of tracking file changes over time.
  5. // Completely implements CFileToTrack, CFileTracker
  6. //
  7. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  8. //
  9. // AUTHOR: Joe Mabel
  10. //
  11. // ORIGINAL DATE: 9-15-98
  12. //
  13. // NOTES:
  14. //
  15. // Version Date By Comments
  16. //--------------------------------------------------------------------
  17. // V3.0 09-15-98 JM
  18. //
  19. #include "stdafx.h"
  20. #include "event.h"
  21. #include "FileTracker.h"
  22. #include "Functions.h"
  23. #include "baseexception.h"
  24. #include "CharConv.h"
  25. //////////////////////////////////////////////////////////////////////
  26. // CFileToTrack
  27. //////////////////////////////////////////////////////////////////////
  28. CFileToTrack::CFileToTrack(const CString & strPathName) :
  29. m_strPathName(strPathName),
  30. m_bFileExists(false)
  31. {
  32. m_ftLastWriteTime.dwLowDateTime = 0;
  33. m_ftLastWriteTime.dwHighDateTime = 0;
  34. }
  35. CFileToTrack::~CFileToTrack()
  36. {
  37. }
  38. void CFileToTrack::CheckFile(bool & bFileExists, bool & bTimeChanged, const bool bLogIfMissing )
  39. {
  40. HANDLE hSearch;
  41. WIN32_FIND_DATA FindData;
  42. hSearch = ::FindFirstFile(m_strPathName, &FindData);
  43. bFileExists = (hSearch != INVALID_HANDLE_VALUE);
  44. // initialize bTimeChanged: we always consider coming into existence as a time change.
  45. bTimeChanged = bFileExists && ! m_bFileExists;
  46. m_bFileExists = bFileExists;
  47. if (bFileExists)
  48. {
  49. ::FindClose(hSearch);
  50. // for some reason, we can't compile
  51. // bTimeChanged |= (m_ftLastWriteTime != FindData.ftLastWriteTime);
  52. // so:
  53. bTimeChanged |= (0 != memcmp(&m_ftLastWriteTime, &(FindData.ftLastWriteTime), sizeof(m_ftLastWriteTime)));
  54. m_ftLastWriteTime = FindData.ftLastWriteTime;
  55. }
  56. else
  57. {
  58. // file disappeared or never existed, ignore for now
  59. m_bFileExists = false;
  60. bFileExists = false;
  61. if (bLogIfMissing)
  62. {
  63. CString strErr;
  64. FormatLastError(&strErr, ::GetLastError());
  65. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  66. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  67. SrcLoc.GetSrcFileLineStr(),
  68. m_strPathName,
  69. strErr,
  70. EV_GTS_ERROR_FILE_MISSING );
  71. }
  72. bTimeChanged = false;
  73. }
  74. }
  75. //////////////////////////////////////////////////////////////////////
  76. // CFileTracker
  77. //////////////////////////////////////////////////////////////////////
  78. CFileTracker::CFileTracker()
  79. {
  80. }
  81. CFileTracker::~CFileTracker()
  82. {
  83. }
  84. void CFileTracker::AddFile(const CString & strPathName)
  85. {
  86. try
  87. {
  88. m_arrFile.push_back(CFileToTrack(strPathName));
  89. }
  90. catch (exception& x)
  91. {
  92. CString str;
  93. // Note STL exception in event log.
  94. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  95. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  96. SrcLoc.GetSrcFileLineStr(),
  97. CCharConversion::ConvertACharToString(x.what(), str),
  98. _T(""),
  99. EV_GTS_STL_EXCEPTION );
  100. }
  101. }
  102. bool CFileTracker::Changed( const bool bLogIfMissing )
  103. {
  104. bool bChange = false;
  105. bool bSomethingMissing = false;
  106. //
  107. // This try-catch block was added as a measure to handle an unexplainable problem.
  108. //
  109. // Previously this function was throwning a (...) exception in release builds but
  110. // not in debug builds. Adding this try-catch block had the effect of making the
  111. // (...) exception in release builds disappear. This problem was causing the
  112. // directory monitor thread to die so if you change this function, please verify
  113. // that the directory monitor thread is still viable.
  114. // RAB-981112.
  115. try
  116. {
  117. for(vector<CFileToTrack>::iterator it = m_arrFile.begin();
  118. it != m_arrFile.end();
  119. it ++
  120. )
  121. {
  122. bool bFileExists;
  123. bool bTimeChanged;
  124. it->CheckFile(bFileExists, bTimeChanged, bLogIfMissing );
  125. bChange |= bTimeChanged;
  126. bSomethingMissing |= !bFileExists;
  127. }
  128. }
  129. catch (...)
  130. {
  131. // Catch any other exception thrown.
  132. CBuildSrcFileLinenoStr SrcLoc( __FILE__, __LINE__ );
  133. CEvent::ReportWFEvent( SrcLoc.GetSrcFileLineStr(),
  134. SrcLoc.GetSrcFileLineStr(),
  135. _T(""), _T(""),
  136. EV_GTS_GEN_EXCEPTION );
  137. }
  138. return (bChange && !bSomethingMissing);
  139. }