Leaked source code of windows server 2003
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.

119 lines
3.0 KiB

  1. // Copyright (c) 2000-2001 Microsoft Corporation, All Rights Reserved
  2. //
  3. // File attributes helper class.
  4. #include <precomp.h>
  5. #include <assertbreak.h>
  6. class CFileAttributes
  7. {
  8. public:
  9. CFileAttributes(
  10. LPCWSTR wstrFileName,
  11. bool fAutoRevert = true)
  12. :
  13. m_fAutoRevert(fAutoRevert),
  14. m_dwOldFileAttributes(static_cast<DWORD>(-1L))
  15. {
  16. m_chstrFileName = wstrFileName;
  17. if(m_fAutoRevert)
  18. {
  19. m_dwOldFileAttributes = ::GetFileAttributes(m_chstrFileName);
  20. }
  21. }
  22. CFileAttributes(const CFileAttributes& cfa)
  23. {
  24. m_chstrFileName = cfa.m_chstrFileName;
  25. m_fAutoRevert = cfa.m_fAutoRevert;
  26. m_dwOldFileAttributes = cfa.m_dwOldFileAttributes;
  27. }
  28. virtual ~CFileAttributes()
  29. {
  30. if(m_fAutoRevert)
  31. {
  32. if(!ResetAttributes())
  33. {
  34. ASSERT_BREAK(0);
  35. LogErrorMessage2(
  36. L"Could not reset file attributes on file %s",
  37. m_chstrFileName);
  38. }
  39. }
  40. }
  41. DWORD GetAttributes(DWORD* pdw)
  42. {
  43. DWORD dwRet = ERROR_SUCCESS;
  44. DWORD dwTemp = ::GetFileAttributes(m_chstrFileName);
  45. if(pdw)
  46. {
  47. *pdw = 0L;
  48. if(dwTemp != -1L)
  49. {
  50. *pdw = dwTemp;
  51. }
  52. else
  53. {
  54. dwRet = ::GetLastError();
  55. }
  56. }
  57. else
  58. {
  59. dwRet = ERROR_INVALID_PARAMETER;
  60. }
  61. return dwRet;
  62. }
  63. DWORD SetAttributes(DWORD dwAttributes)
  64. {
  65. DWORD dwRet = E_FAIL;
  66. DWORD dwTemp = ::GetFileAttributes(m_chstrFileName);
  67. if(dwTemp != -1L)
  68. {
  69. m_dwOldFileAttributes = dwTemp;
  70. if(::SetFileAttributes(
  71. m_chstrFileName,
  72. dwAttributes))
  73. {
  74. dwRet = ERROR_SUCCESS;
  75. }
  76. else
  77. {
  78. dwRet = ::GetLastError();
  79. }
  80. }
  81. else
  82. {
  83. dwRet = ::GetLastError();
  84. }
  85. return dwRet;
  86. }
  87. BOOL ResetAttributes()
  88. {
  89. BOOL fResult = FALSE;
  90. if(m_dwOldFileAttributes != static_cast<DWORD>(-1L))
  91. {
  92. fResult = ::SetFileAttributes(
  93. m_chstrFileName,
  94. m_dwOldFileAttributes);
  95. }
  96. return fResult;
  97. }
  98. private:
  99. CHString m_chstrFileName;
  100. bool m_fAutoRevert;
  101. DWORD m_dwOldFileAttributes;
  102. };