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.

394 lines
13 KiB

  1. //=======================================================================
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: fileutil.h
  6. //
  7. // Description:
  8. //
  9. // IU file utility library
  10. //
  11. //=======================================================================
  12. #ifndef __FILEUTIL_INC
  13. #define __FILEUTIL_INC
  14. // ----------------------------------------------------------------------
  15. //
  16. // define constant chars often used in file path processing
  17. //
  18. // ----------------------------------------------------------------------
  19. #ifndef TCHAR_EOS
  20. #define TCHAR_EOS _T('\0')
  21. #endif
  22. #ifndef TCHAR_STAR
  23. #define TCHAR_STAR _T('*')
  24. #endif
  25. #ifndef TCHAR_BACKSLASH
  26. #define TCHAR_BACKSLASH _T('\\')
  27. #endif
  28. #ifndef TCHAR_FWDSLASH
  29. #define TCHAR_FWDSLASH _T('/')
  30. #endif
  31. #ifndef TCHAR_COLON
  32. #define TCHAR_COLON _T(':')
  33. #endif
  34. #ifndef TCHAR_DOT
  35. #define TCHAR_DOT _T('.')
  36. #endif
  37. #ifndef TCHAR_SPACE
  38. #define TCHAR_SPACE _T(' ')
  39. #endif
  40. #ifndef TCHAR_TAB
  41. #define TCHAR_TAB _T('\t')
  42. #endif
  43. // ----------------------------------------------------------------------
  44. //
  45. // define constants used by path related operations.
  46. // these constants can be found either from CRT or Shlwapi header
  47. // files.
  48. //
  49. // ----------------------------------------------------------------------
  50. #ifdef _MAX_PATH
  51. #undef _MAX_PATH
  52. #endif
  53. #define _MAX_PATH MAX_PATH
  54. #ifdef _MAX_DRIVE
  55. #undef _MAX_DRIVE
  56. #endif
  57. #define _MAX_DRIVE 3 // buffer size to take drive letter & ":"
  58. #ifdef _MAX_DIR
  59. #undef _MAX_DIR
  60. #endif
  61. #define _MAX_DIR 256 // max. length of path component
  62. #ifdef _MAX_FNAME
  63. #undef _MAX_FNAME
  64. #endif
  65. #define _MAX_FNAME 256 // max. length of file name component
  66. #ifdef _MAX_EXT
  67. #undef _MAX_EXT
  68. #endif
  69. #define _MAX_EXT 256 // max. length of extension component
  70. #define ARRAYSIZE(x) (sizeof(x)/sizeof(x[0]))
  71. //Error code for files which are not valid binaries or which do not support the machine architecture
  72. #define BIN_E_MACHINE_MISMATCH HRESULT_FROM_WIN32(ERROR_EXE_MACHINE_TYPE_MISMATCH)
  73. #define BIN_E_BAD_FORMAT HRESULT_FROM_WIN32(ERROR_BAD_FORMAT)
  74. // ----------------------------------------------------------------------
  75. //
  76. // Public function MySplitPath() - same as CRT _tsplitpath()
  77. // to break a path into pieces
  78. //
  79. // Input:
  80. // see below
  81. //
  82. // Return:
  83. // Returns the address of the last occurrence of the character in
  84. // the string if successful, or NULL otherwise.
  85. //
  86. // ----------------------------------------------------------------------
  87. void MySplitPath(
  88. LPCTSTR lpcszPath, // original path
  89. LPTSTR lpszDrive, // point to buffer to receive drive letter
  90. LPTSTR lpszDir, // point to buffer to receive directory
  91. LPTSTR lpszFName, // point to buffer to receive file name
  92. LPTSTR lpszExt // point to buffer to receive extension
  93. );
  94. //---------------------------------------------------------------------
  95. // CreateNestedDirectory
  96. // Creates the full path of the directory (nested directories)
  97. //---------------------------------------------------------------------
  98. BOOL CreateNestedDirectory(LPCTSTR pszDir);
  99. //-----------------------------------------------------------------------------------
  100. // GetIndustryUpdateDirectory
  101. // This function returns the location of the IndustryUpdate directory. All local
  102. // files are stored in this directory. The pszPath parameter needs to be at least
  103. // MAX_PATH.
  104. //-----------------------------------------------------------------------------------
  105. void GetIndustryUpdateDirectory(LPTSTR pszPath);
  106. //-----------------------------------------------------------------------------------
  107. // GetWindowsUpdateV3Directory - used for V3 history migration
  108. // This function returns the location of the WindowsUpdate(V3) directory. All V3
  109. // local files are stored in this directory. The pszPath parameter needs to be
  110. // at least MAX_PATH. The directory is created if not found
  111. //-----------------------------------------------------------------------------------
  112. void GetWindowsUpdateV3Directory(LPTSTR pszPath);
  113. // **********************************************************************************
  114. //
  115. // File version related declarations
  116. //
  117. // **********************************************************************************
  118. // ----------------------------------------------------------------------------------
  119. //
  120. // define a type to hold file version data
  121. //
  122. // ----------------------------------------------------------------------------------
  123. typedef struct _FILE_VERSION
  124. {
  125. WORD Major;
  126. WORD Minor;
  127. WORD Build;
  128. WORD Ext;
  129. } FILE_VERSION, *LPFILE_VERSION;
  130. // ----------------------------------------------------------------------------------
  131. //
  132. // public function to retrieve file version
  133. //
  134. // ----------------------------------------------------------------------------------
  135. BOOL GetFileVersion(LPCTSTR lpsFile, LPFILE_VERSION lpstVersion);
  136. // ----------------------------------------------------------------------------------
  137. //
  138. // publif function to retrieve the creation time of a file in ISO 8601 format
  139. // without zone info
  140. //
  141. // if buffer too small, call GetLastError();
  142. //
  143. // ----------------------------------------------------------------------------------
  144. BOOL GetFileTimeStamp(
  145. LPCTSTR lpsFile, // file path
  146. LPTSTR lpsTimeStamp, // buffer to receive timestamp
  147. int iBufSize // buffer size in characters
  148. );
  149. // ----------------------------------------------------------------------------------
  150. //
  151. // public functions to compare file versions
  152. //
  153. // return:
  154. // -1: if file ver of 1st parameter < file ver of 2nd parameter
  155. // 0: if file ver of 1st parameter = file ver of 2nd parameter
  156. // +1: if file ver of 1st parameter > file ver of 2nd parameter
  157. //
  158. // ----------------------------------------------------------------------------------
  159. HRESULT CompareFileVersion(LPCTSTR lpsFile1, LPCTSTR lpsFile2, int *pCompareResult);
  160. HRESULT CompareFileVersion(LPCTSTR lpsFile, FILE_VERSION stVersion, int *pCompareResult);
  161. int CompareFileVersion(const FILE_VERSION stVersion1, const FILE_VERSION stVersion2);
  162. // ----------------------------------------------------------------------------------
  163. //
  164. // public function to convert a string type functoin to FILE_VERSION type
  165. //
  166. // ----------------------------------------------------------------------------------
  167. BOOL ConvertStringVerToFileVer(LPCSTR lpsVer, LPFILE_VERSION lpstVer);
  168. // ----------------------------------------------------------------------------------
  169. //
  170. // publif function to convert a FILE_VERSION to a string
  171. //
  172. // ----------------------------------------------------------------------------------
  173. BOOL ConvertFileVerToStringVer(
  174. FILE_VERSION stVer, // version to convert
  175. char chDel, // delimiter to use
  176. LPSTR lpsBuffer, // buffer of string
  177. int ccBufSize // size of buffer
  178. );
  179. // **********************************************************************************
  180. //
  181. // detection related, in addition to file version group
  182. //
  183. // **********************************************************************************
  184. // ----------------------------------------------------------------------------------
  185. //
  186. // public function to check if a file exists
  187. //
  188. // ----------------------------------------------------------------------------------
  189. BOOL FileExists(
  190. LPCTSTR lpsFile // file with path to check
  191. );
  192. // ----------------------------------------------------------------------------------
  193. //
  194. // public function to expand the file path
  195. //
  196. // Assumption: lpszFilePath points to allocated buffer of MAX_PATH.
  197. // if the expanded path is longer than MAX_PATH, error returned.
  198. //
  199. // ----------------------------------------------------------------------------------
  200. HRESULT ExpandFilePath(
  201. LPCTSTR lpszFilePath, // original string
  202. LPTSTR lpszDestination, // buffer for expanded string
  203. UINT cChars // number of chars that buffer can take
  204. );
  205. // ----------------------------------------------------------------------------------
  206. //
  207. // public function to find the free disk space in KB
  208. //
  209. // ----------------------------------------------------------------------------------
  210. HRESULT GetFreeDiskSpace(
  211. TCHAR tcDriveLetter, // drive letter
  212. int *piKBytes // out result in KB if successful, 0 if fail
  213. );
  214. HRESULT GetFreeDiskSpace(
  215. LPCTSTR pszUNC, // UNC Path
  216. int *piKBytes // out result in KB if successful, 0 if fail
  217. );
  218. //----------------------------------------------------------------------
  219. //
  220. // function to validate the folder to make sure
  221. // user has required priviledge
  222. //
  223. // folder will be verified exist. then required priviledge will be checked.
  224. //
  225. // ASSUMPTION: lpszFolder not exceeding MAX_PATH long!!!
  226. //
  227. //----------------------------------------------------------------------
  228. DWORD ValidateFolder(LPTSTR lpszFolder, BOOL fCheckForWrite);
  229. //----------------------------------------------------------------------
  230. //
  231. // function to get a QueryServer from the Ident File for a Given ClientName
  232. // This also looks in the registry for the IsBeta regkey indicating Beta
  233. // functionlality
  234. //
  235. //----------------------------------------------------------------------
  236. HRESULT GetClientQueryServer(LPCTSTR pszClientName, // Client Name to get QueryServer for
  237. LPTSTR pszQueryServer, // Buffer for Query Server Return
  238. UINT cChars); // Length of Buffer in Chars
  239. //----------------------------------------------------------------------
  240. //
  241. // function to walk a directory and extract all cabinet files
  242. //
  243. //----------------------------------------------------------------------
  244. HRESULT DecompressFolderCabs(LPCTSTR pszDecompressPath);
  245. //----------------------------------------------------------------------
  246. //
  247. // wrapper function for AdvPack ExtractFiles API (forces conversion to ANSI);
  248. //
  249. //----------------------------------------------------------------------
  250. BOOL IUExtractFiles(LPCTSTR pszCabFile, LPCTSTR pszDecompressFolder, LPCTSTR pszFileNames = NULL);
  251. //Replace the file extension with a new extension
  252. BOOL ReplaceFileExtension( LPCTSTR pszPath, LPCTSTR pszNewExt, LPTSTR pszNewPathBuf, DWORD cchNewPathBuf);
  253. // ReplaceFileInPath
  254. BOOL ReplaceFileInPath( LPCTSTR pszPath, LPCTSTR pszNewFile, LPTSTR pszNewPathBuf, DWORD cchNewPathBuf);
  255. //Function to verify that the specified file is a binary
  256. //and that it is compatible with the OS architecture
  257. HRESULT IsBinaryCompatible(LPCTSTR lpszFile);
  258. // file exists routine
  259. inline BOOL fFileExists(LPCTSTR lpszFileName, BOOL *pfIsDir = NULL)
  260. {
  261. DWORD dwAttribute = GetFileAttributes(lpszFileName); //GetFileAttributes do not like "\" at the end of direcotry
  262. if (INVALID_FILE_ATTRIBUTES != dwAttribute)
  263. {
  264. if (NULL != pfIsDir)
  265. {
  266. *pfIsDir = (FILE_ATTRIBUTE_DIRECTORY & dwAttribute);
  267. }
  268. return TRUE;
  269. }
  270. else
  271. {
  272. return FALSE;
  273. }
  274. }
  275. //Get the path to the WindowsUpdate Directory (without the backslash at the end)
  276. BOOL GetWUDirectory(LPTSTR lpszDirPath, DWORD chCount, BOOL fGetV4Path = FALSE);
  277. /*****************************************************************************
  278. //Function to set ACL's on Windows Update directories, optionally creates the
  279. //directory if it doesnt already exists
  280. //This function will:
  281. // * Take ownership of the directory and it's children
  282. // * Set all the children to inherit ACL's from parent
  283. // * Set the specified directory to NOT inherit properties from it's parent
  284. // * Set the required ACL's on the specified directory
  285. // * Replace the ACL's on the children (i.e. propogate own ACL's and remove
  286. // those ACL's which were explicitly set
  287. //
  288. // Input:
  289. // lpszDirectory: Path to the directory to ACL, If it is NULL we use the
  290. path to the WindowsUpdate directory
  291. fCreateAlways: Flag to indicate creation of new directory if it doesnt
  292. already exist
  293. ******************************************************************************/
  294. HRESULT CreateDirectoryAndSetACLs(LPCTSTR lpszDirectory, BOOL fCreateAlways);
  295. // ----------------------------------------------------------------------------------
  296. //
  297. // File CRC API and Structure Definitions
  298. // Note: This logic is taken from the Windows Update V3 Implemention of File CRC's.
  299. // We use the CryptCATAdminCalcHashFromFileHandle API to calculate a CRC of the file
  300. // and compare it to the passed in CRC_HASH.
  301. //
  302. // ----------------------------------------------------------------------------------
  303. // size of the CRC hash in bytes
  304. const int CRC_HASH_SIZE = 20;
  305. const int CRC_HASH_STRING_LENGTH = CRC_HASH_SIZE * 2 + 1; // Double the CRC HASH SIZE (2 characters for each byte), + 1 for the NULL
  306. // ----------------------------------------------------------------------------------
  307. //
  308. // VerifyFileCRC : This function takes a File Path, calculates the hash on this file
  309. // and compares it to the passed in Hash (pCRC).
  310. // Returns:
  311. // S_OK: CRC's Match
  312. // ERROR_CRC (HRESULT_FROM_WIN32(ERROR_CRC): if the CRC's do not match
  313. // Otherwise an HRESULT Error Code
  314. //
  315. // ----------------------------------------------------------------------------------
  316. HRESULT VerifyFileCRC(LPCTSTR pszFileToVerify, LPCTSTR pszHash);
  317. // ----------------------------------------------------------------------------------
  318. //
  319. // CalculateFileCRC : This function takes a File Path, calculates a CRC from the file
  320. // and returns it in passed in CRC_HASH pointer.
  321. //
  322. // ----------------------------------------------------------------------------------
  323. HRESULT CalculateFileCRC(LPCTSTR pszFileToHash, LPTSTR pszHash, int cchBuf);
  324. #endif //__FILEUTIL_INC