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.

261 lines
6.6 KiB

  1. /******************************************************************************
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. srshutil.h
  5. Abstract:
  6. This file contains declaration of utility functions/classes like
  7. CSRStr, CDynArray, etc.
  8. Revision History:
  9. Seong Kook Khang (SKKhang) 06/22/00
  10. created
  11. ******************************************************************************/
  12. #ifndef _SRSHUTIL_H__INCLUDED_
  13. #define _SRSHUTIL_H__INCLUDED_
  14. #pragma once
  15. /////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Constant Definitions
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #define FA_BLOCK ( FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM )
  21. /////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Utility Functions
  24. //
  25. /////////////////////////////////////////////////////////////////////////////
  26. extern LPWSTR IStrDup( LPCWSTR cszSrc );
  27. extern DWORD StrCpyAlign4( LPBYTE pbDst, LPCWSTR cszSrc );
  28. extern BOOL ReadStrAlign4( HANDLE hFile, LPWSTR szStr );
  29. extern BOOL SRFormatMessage( LPWSTR szMsg, UINT uFmtId, ... );
  30. extern BOOL ShowSRErrDlg( UINT uMsgId );
  31. extern BOOL SRGetRegDword( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, DWORD *pdwData );
  32. extern BOOL SRSetRegDword( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, DWORD dwData );
  33. extern BOOL SRSetRegStr( HKEY hKey, LPCWSTR cszSubKey, LPCWSTR cszValue, LPCWSTR cszData );
  34. extern BOOL SRGetAltFileName( LPCWSTR cszPath, LPWSTR szAltName );
  35. // NTFS.CPP
  36. extern DWORD ClearFileAttribute( LPCWSTR cszFile, DWORD dwMask );
  37. extern DWORD TakeOwnership( LPCWSTR cszPath );
  38. extern DWORD SRCopyFile( LPCWSTR cszSrc, LPCWSTR cszDst );
  39. /////////////////////////////////////////////////////////////////////////////
  40. //
  41. // CSRStr class
  42. //
  43. /////////////////////////////////////////////////////////////////////////////
  44. //
  45. // NOTE - 7/26/00 - skkhang
  46. // CSRStr has one issue -- NULL return in case of memory failure. Even though
  47. // the behavior is just same with regular C language pointer, many codes are
  48. // blindly passing it to some external functions (e.g. strcmp) which does not
  49. // gracefully handle NULL pointer. Ideally and eventually all of code should
  50. // prevent any possible NULL pointers from getting passed to such functions,
  51. // but for now, I'm using an alternative workaround -- GetID, GetMount, and
  52. // GetLabel returns a static empty string instead of NULL pointer.
  53. //
  54. class CSRStr
  55. {
  56. public:
  57. CSRStr();
  58. CSRStr( LPCWSTR cszSrc );
  59. ~CSRStr();
  60. // Attributes
  61. public:
  62. int Length();
  63. operator LPCWSTR();
  64. protected:
  65. int m_cch;
  66. LPWSTR m_str;
  67. // Operations
  68. public:
  69. void Empty();
  70. BOOL SetStr( LPCWSTR cszSrc, int cch = -1 );
  71. const CSRStr& operator =( LPCWSTR cszSrc );
  72. };
  73. /////////////////////////////////////////////////////////////////////////////
  74. //
  75. // CSRDynPtrArray class
  76. //
  77. /////////////////////////////////////////////////////////////////////////////
  78. template<class type, int nBlock>
  79. class CSRDynPtrArray
  80. {
  81. public:
  82. CSRDynPtrArray();
  83. ~CSRDynPtrArray();
  84. // Attributes
  85. public:
  86. int GetSize()
  87. { return( m_nCur ); }
  88. int GetUpperBound()
  89. { return( m_nCur-1 ); }
  90. type GetItem( int nItem );
  91. type operator[]( int nItem )
  92. { return( GetItem( nItem ) ); }
  93. protected:
  94. int m_nMax; // Maximum Item Count
  95. int m_nCur; // Current Item Count
  96. type *m_ppTable;
  97. // Operations
  98. public:
  99. BOOL AddItem( type item );
  100. BOOL SetItem( int nIdx, type item );
  101. BOOL Empty();
  102. void DeleteAll();
  103. void ReleaseAll();
  104. };
  105. template<class type, int nBlock>
  106. CSRDynPtrArray<type, nBlock>::CSRDynPtrArray()
  107. {
  108. m_nMax = 0;
  109. m_nCur = 0;
  110. m_ppTable = NULL;
  111. }
  112. template<class type, int nBlock>
  113. CSRDynPtrArray<type, nBlock>::~CSRDynPtrArray()
  114. {
  115. Empty();
  116. }
  117. template<class type, int nBlock>
  118. type CSRDynPtrArray<type, nBlock>::GetItem( int nItem )
  119. {
  120. if ( nItem < 0 || nItem >= m_nCur )
  121. {
  122. // ERROR - Out of Range
  123. }
  124. return( m_ppTable[nItem] );
  125. }
  126. template<class type, int nBlock>
  127. BOOL CSRDynPtrArray<type, nBlock>::AddItem( type item )
  128. {
  129. TraceFunctEnter("CSRDynPtrArray::AddItem");
  130. BOOL fRet = FALSE;
  131. type *ppTableNew;
  132. if ( m_nCur == m_nMax )
  133. {
  134. m_nMax += nBlock;
  135. // Assuming m_ppTable and m_nMax are always in sync.
  136. // Review if it's necessary to validate this assumption.
  137. if ( m_ppTable == NULL )
  138. ppTableNew = (type*)::HeapAlloc( ::GetProcessHeap(), 0, m_nMax*sizeof(type) );
  139. else
  140. ppTableNew = (type*)::HeapReAlloc( ::GetProcessHeap(), 0, m_ppTable, m_nMax * sizeof(type) );
  141. if ( ppTableNew == NULL )
  142. {
  143. FatalTrace(0, "Insufficient memory...");
  144. goto Exit;
  145. }
  146. m_ppTable = ppTableNew;
  147. }
  148. m_ppTable[m_nCur++] = item;
  149. fRet = TRUE;
  150. Exit:
  151. TraceFunctLeave();
  152. return( fRet );
  153. }
  154. template<class type, int nBlock>
  155. BOOL CSRDynPtrArray<type, nBlock>::SetItem( int nIdx, type item )
  156. {
  157. if ( nIdx >= m_nMax )
  158. return( FALSE );
  159. m_ppTable[nIdx] = item;
  160. if ( nIdx >= m_nCur )
  161. m_nCur = nIdx+1;
  162. return( TRUE );
  163. }
  164. template<class type, int nBlock>
  165. BOOL CSRDynPtrArray<type, nBlock>::Empty()
  166. {
  167. if ( m_ppTable != NULL )
  168. {
  169. ::HeapFree( ::GetProcessHeap(), 0, m_ppTable );
  170. m_ppTable = NULL;
  171. m_nMax = 0;
  172. m_nCur = 0;
  173. }
  174. return( TRUE );
  175. }
  176. template<class type, int nBlock>
  177. void CSRDynPtrArray<type, nBlock>::DeleteAll()
  178. {
  179. for ( int i = m_nCur-1; i >= 0; i-- )
  180. delete m_ppTable[i];
  181. Empty();
  182. }
  183. template<class type, int nBlock>
  184. void CSRDynPtrArray<type, nBlock>::ReleaseAll()
  185. {
  186. for ( int i = m_nCur-1; i >= 0; i-- )
  187. m_ppTable[i]->Release();
  188. Empty();
  189. }
  190. /////////////////////////////////////////////////////////////////////////////
  191. //
  192. // CSRLockFile class
  193. //
  194. /////////////////////////////////////////////////////////////////////////////
  195. //
  196. // This class reads multiple paths from the registry and either lock them
  197. // using ::CreateFile or load them using ::LoadLibrary. This is only for
  198. // test purpose - initiate locked-file-handling during a restoration.
  199. //
  200. class CSRLockFile
  201. {
  202. public:
  203. CSRLockFile();
  204. ~CSRLockFile();
  205. protected:
  206. CSRDynPtrArray<HANDLE,16> m_aryLock;
  207. CSRDynPtrArray<HMODULE,16> m_aryLoad;
  208. };
  209. #endif //_SRSHUTIL_H__INCLUDED_