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.

353 lines
11 KiB

  1. /******************************************************************************
  2. *
  3. * Copyright (c) 2000 Microsoft Corporation
  4. *
  5. * Module Name:
  6. * utils.h
  7. *
  8. * Abstract:
  9. * Declarations for commonly used util functions.
  10. *
  11. * Revision History:
  12. * Brijesh Krishnaswami (brijeshk) 03/17/2000
  13. * created
  14. *
  15. *****************************************************************************/
  16. #ifndef _UTILS_H_
  17. #define _UTILS_H_
  18. // trace macros
  19. #define TENTER TraceFunctEnter
  20. #define TLEAVE TraceFunctLeave
  21. #define TRACE DebugTrace
  22. #define tenter TraceFunctEnter
  23. #define tleave TraceFunctLeave
  24. #define trace DebugTrace
  25. // lock macros
  26. #define LOCKORLEAVE(a) if (! (a = m_DSLock.Lock(CLock::TIMEOUT))) { dwRc = ERROR_TIMEOUT; goto done; }
  27. #define LOCKORLEAVE_EX(a, t) if (! (a = m_DSLock.Lock(t))) { dwRc = ERROR_TIMEOUT; goto done; }
  28. #define UNLOCK(a) if (a) { m_DSLock.Unlock(); a = FALSE; }
  29. #define CHECKERR(f, trace) dwErr = (f); if (dwErr != ERROR_SUCCESS) \
  30. { \
  31. TRACE(0, "! %s : %ld", trace, dwErr); \
  32. goto Err; \
  33. }
  34. // mem macros
  35. #define SRMemAlloc(a) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, a)
  36. #define SRMemFree(a) if (a) HeapFree(GetProcessHeap(), 0, a)
  37. // unicode-ansi conversion routines
  38. WCHAR * ConvertToUnicode(CHAR * pszString);
  39. CHAR * ConvertToANSI(WCHAR * pszwString);
  40. #define UnicodeStringToWchar(US, pwsz) CopyMemory(pwsz, US.Buffer, US.Length); \
  41. pwsz[US.Length/sizeof(WCHAR)]=L'\0'
  42. // directory traversal routines
  43. DWORD GetFileSize_Recurse (const WCHAR *pwszDir,
  44. INT64 *pllTotalBytes,
  45. BOOL *pfStop);
  46. DWORD CompressFile (const WCHAR *pwszPath, BOOL fCompress, BOOL fDirectory);
  47. DWORD TakeOwn (const WCHAR *pwszFile);
  48. DWORD Delnode_Recurse (const WCHAR *pwszDir, BOOL fDeleteRoot, BOOL *pfStop);
  49. DWORD CopyFile_Recurse (const WCHAR *pwszSource, const WCHAR *pwszDest);
  50. // returns system drive as "C:\" (if system drive is C) or as volume name
  51. #define MAX_SYS_DRIVE 10
  52. BOOL GetSystemDrive(LPWSTR pszDrive);
  53. // returns TRUE if pszDrive contains the string L"C:" (if system drive is C)
  54. BOOL IsSystemDrive(LPWSTR pszDrive);
  55. // restore point routines
  56. LPWSTR GetMachineGuid();
  57. LPWSTR MakeRestorePath(LPWSTR pszDest, LPCWSTR pszDrive, LPCWSTR pszSuffix);
  58. ULONG GetID(LPCWSTR pszStr);
  59. // registry routines
  60. DWORD RegReadDWORD(HKEY hKey, LPCWSTR pszName, PDWORD pdwValue);
  61. DWORD RegWriteDWORD(HKEY hKey, LPCWSTR pszName, PDWORD pdwValue);
  62. // set/get start type of specified service
  63. DWORD SetServiceStartup(LPCWSTR pszName, DWORD dwStartType);
  64. DWORD GetServiceStartup(LPCWSTR pszName, PDWORD pdwStartType);
  65. DWORD GetServiceStartupRegistry(LPCWSTR pszName, PDWORD pdwStartType);
  66. BOOL StopSRService(BOOL fWait);
  67. // get the current domain or workgroup name
  68. DWORD GetDomainMembershipInfo (WCHAR *pwszPath, WCHAR *pwszzBuffer);
  69. // get the LSA secrets for restore
  70. DWORD GetLsaRestoreState (HKEY hKeySoftware);
  71. DWORD SetLsaSecret (PVOID hPolicy, const WCHAR *wszSecret,
  72. WCHAR * wszSecretValue);
  73. BOOL DoesDirExist(const TCHAR * pszFileName );
  74. BOOL DoesFileExist(const TCHAR * pszFileName);
  75. // this function creates all sub directories under the specified file
  76. // name.
  77. BOOL CreateBaseDirectory(const WCHAR * pszFileName);
  78. DWORD SRLoadString(LPCWSTR pszModule, DWORD dwStringId, LPWSTR pszString, DWORD cbBytes);
  79. // sets acl allowing specific access to LocalSystem/Admin
  80. // and to everyone
  81. DWORD
  82. SetAclInObject(HANDLE hObject,
  83. DWORD dwObjectType,
  84. DWORD dwSystemMask,
  85. DWORD dwEveryoneMask,
  86. BOOL fInherit);
  87. // sets acl to a named object allowing specific access to
  88. // LocalSystem/Admin and to everyone
  89. DWORD
  90. SetAclInNamedObject(WCHAR * pszDirName, DWORD dwObjectType,
  91. DWORD dwSystemMask, DWORD dwEveryoneMask,
  92. DWORD dwSystemInherit, DWORD dwEveryOneInherit);
  93. // sets the right ACL on the root of the DS on a drive
  94. DWORD SetCorrectACLOnDSRoot(WCHAR * wcsPath);
  95. // returns if Everyone has write access to the directory
  96. BOOL IsDirectoryWorldAccessible(WCHAR * pszObjectName);
  97. // returns if the file is owned by the administrators group or system
  98. BOOL IsFileOwnedByAdminOrSystem(WCHAR * pszObjectName);
  99. void
  100. PostTestMessage(UINT msg, WPARAM wp, LPARAM lp);
  101. // inline mem alloc class
  102. class CSRAlloc
  103. {
  104. public:
  105. inline void *operator new(size_t size)
  106. {
  107. return SRMemAlloc (size);
  108. }
  109. inline void operator delete (void * pv)
  110. {
  111. SRMemFree (pv);
  112. }
  113. };
  114. //////////////////////////////////////////////////////////////////////
  115. // CLock - class that allows exclusive access to a resource
  116. // uses a mutex - does not differentiate between readers/writers
  117. class CLock
  118. {
  119. HANDLE hResource;
  120. public:
  121. BOOL fHaveLock;
  122. CLock();
  123. ~CLock();
  124. DWORD Init();
  125. BOOL Lock(int iTimeOut);
  126. void Unlock();
  127. static const enum {TIMEOUT = 10*60000};
  128. };
  129. //
  130. // util function that checks the SR Stop event
  131. // to see if it has been signalled or not
  132. // will return TRUE if the event does not exist
  133. //
  134. BOOL IsStopSignalled(HANDLE hEvent);
  135. // The following function logs the name of a file in the DS. The
  136. // problem right now is that the path of the DS is so long that the
  137. // relevant information is thrown away from the trace buffer.
  138. void LogDSFileTrace(DWORD dwTraceID,
  139. const WCHAR * pszPrefix, // Initial message to be traced
  140. const WCHAR * pszDSFile);
  141. typedef DWORD (* PPROCESSFILEMETHOD) (WCHAR * pszBaseDir,// Base Directory
  142. const WCHAR * pszFile);
  143. // File to process
  144. DWORD DeleteGivenFile(WCHAR * pszBaseDir, // Base Directory
  145. const WCHAR * pszFile); // file to delete
  146. DWORD ProcessGivenFiles(WCHAR * pszBaseDir,
  147. PPROCESSFILEMETHOD pfnMethod,
  148. WCHAR * pszFindFileData);
  149. //++-----------------------------------------------------------------------
  150. //
  151. // Function: WriteRegKey
  152. //
  153. // Synopsis: This function writes into a registry key. It also creates it
  154. // if it does not exist.
  155. //
  156. // Arguments:
  157. //
  158. // Returns: TRUE no error
  159. // FALSE a fatal error happened
  160. //
  161. // History: AshishS Created 5/22/96
  162. //------------------------------------------------------------------------
  163. BOOL WriteRegKey(BYTE * pbRegValue,
  164. DWORD dwNumBytes,
  165. const TCHAR * pszRegKey,
  166. const TCHAR * pszRegValueName,
  167. DWORD dwRegType);
  168. //++------------------------------------------------------------------------
  169. //
  170. // Function: ReadRegKey
  171. //
  172. // Synopsis: This function reads a registry key and creates it
  173. // if it does not exist with the default value.
  174. //
  175. // Arguments:
  176. //
  177. // Returns: TRUE no error
  178. // FALSE a fatal error happened
  179. //
  180. // History: AshishS Created 5/22/96
  181. //------------------------------------------------------------------------
  182. BOOL ReadRegKeyOrCreate(BYTE * pbRegValue, // The value of the reg key will be
  183. // stored here
  184. DWORD * pdwNumBytes, // Pointer to DWORD conataining
  185. // the number of bytes in the above buffer - will be
  186. // set to actual bytes stored.
  187. const TCHAR * pszRegKey, // Reg Key to be opened
  188. const TCHAR * pszRegValueName, // Reg Value to query
  189. DWORD dwRegTypeExpected,
  190. BYTE * pbDefaultValue, // default value
  191. DWORD dwDefaultValueSize); // size of default value
  192. //++------------------------------------------------------------------------
  193. //
  194. // Function: ReadRegKey
  195. //
  196. // Synopsis: This function reads a registry key.
  197. //
  198. // Arguments:
  199. //
  200. // Returns: TRUE no error
  201. // FALSE a fatal error happened
  202. //
  203. // History: AshishS Created 5/22/96
  204. //------------------------------------------------------------------------
  205. BOOL ReadRegKey(BYTE * pbRegValue, // The value of the reg key will be
  206. // stored here
  207. DWORD * pdwNumBytes, // Pointer to DWORD conataining
  208. // the number of bytes in the above buffer - will be
  209. // set to actual bytes stored.
  210. const TCHAR * pszRegKey, // Reg Key to be opened
  211. const TCHAR * pszRegValueName, // Reg Value to query
  212. DWORD dwRegTypeExpected); // Expected type of Value
  213. // this function checks to see of the restore failed because of disk space
  214. BOOL CheckForDiskSpaceError();
  215. // this function sets the error hit by restore in the registry
  216. BOOL SetRestoreError(DWORD dwRestoreError);
  217. // this function sets the status whether restore was done in safe mode
  218. BOOL SetRestoreSafeModeStatus(DWORD dwSafeModeStatus);
  219. // this function checks to see if the last restore was done in safe mode
  220. BOOL WasLastRestoreInSafeMode();
  221. LPCWSTR GetSysErrStr();
  222. LPCWSTR GetSysErrStr( DWORD dwErr );
  223. DWORD SRCopyFile( LPCWSTR cszSrc, LPCWSTR cszDst );
  224. DWORD SRCreateSubdirectory ( LPCWSTR cszDst, LPSECURITY_ATTRIBUTES pSecAttr);
  225. // this function returns whether the SR service is running
  226. BOOL IsSRServiceRunning();
  227. LPWSTR SRGetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPDWORD pdwData );
  228. BOOL SRSetRegMultiSz( HKEY hkRoot, LPCWSTR cszSubKey, LPCWSTR cszValue, LPCWSTR cszData, DWORD cbData );
  229. // this returns the name after the volume name
  230. // For example input: c:\file output: file
  231. // input \\?\Volume{GUID}\file1 output: file1
  232. WCHAR * ReturnPastVolumeName(const WCHAR * pszFileName);
  233. //This API sets the ShortFileName for a given file
  234. DWORD SetShortFileName(const WCHAR * pszFile, // complete file path
  235. const WCHAR * pszShortName); // desired short file name
  236. void SRLogEvent (HANDLE hEventSource,
  237. WORD wType,
  238. DWORD dwID,
  239. void * pRawData,
  240. DWORD dwDataSize,
  241. const WCHAR * pszS1,
  242. const WCHAR * pszS2,
  243. const WCHAR * pszS3);
  244. BOOL IsAdminOrSystem();
  245. BOOL IsPowerUsers();
  246. void ChangeCCS(HKEY hkMount, LPWSTR pszString);
  247. void RemoveTrailingFilename(WCHAR * pszString, WCHAR wchSlash);
  248. class CSRClientLoader
  249. {
  250. public:
  251. CSRClientLoader();
  252. ~CSRClientLoader();
  253. BOOL LoadSrClient();
  254. HMODULE m_hSRClient;
  255. private:
  256. HMODULE m_hFrameDyn;
  257. BOOL LoadFrameDyn();
  258. };
  259. #endif