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.

448 lines
14 KiB

  1. /*** fileutil.h - Utility routines for dealing with files
  2. *
  3. * Microsoft Confidential
  4. * Copyright (C) Microsoft Corporation 1994
  5. * All Rights Reserved.
  6. *
  7. * Author:
  8. * Benjamin W. Slivka
  9. *
  10. * History:
  11. * 20-Feb-1994 bens Initial version (code from diamond.c)
  12. * 23-Feb-1994 bens Added createTempFile()
  13. * 23-Feb-1994 bens Added richer tempfile routines
  14. * 23-Mar-1994 bens Added Win32<->MS-DOS file attribute mapping
  15. * 03-Jun-1994 bens VER.DLL support
  16. * 07-Jun-1994 bens Move VER.DLL stuff to filever.c
  17. * 14-Dec-1994 bens Update list of exported functions
  18. *
  19. * Exported Functions:
  20. * CopyOneFile - Make a faithful copy of a file
  21. * getFileSize - Get size of a file
  22. * GetFileTimeAndAttr - Get date, time, and attributes from a file
  23. * SetFileTimeAndAttr - Set date, time, and attributes of a file
  24. * appendPathSeparator - Append a path separator only if necessary
  25. * catDirAndFile - Concatenate a possibly empty dir and file name
  26. * ensureDirectory - Ensure directory exists (creating as needed)
  27. * ensureFile - Ensure a file can be created
  28. * getJustFileNameAndExt - Get last component in filespec
  29. * Attr32FromAttrFAT - Convert FAT file attributes to Win32 form
  30. * AttrFATFromAttr32 - Convert Win32 file attributes to FAT form
  31. * IsWildMatch - Test filespec against wild card specification
  32. * IsPathRemovable - See if path refers to a removable media drive
  33. * TmpCreate - Create a temporary file
  34. * TmpGetStream - Get FILE* from HTEMPFILE, to perform I/O
  35. * TmpGetDescription - Get description of temporary file
  36. * TmpGetFileName - Get filename of temporary file
  37. * TmpClose - Close temporary file, but keep tempfile handle
  38. * TmpOpen - Open the stream for a temporary file
  39. * TmpDestroy - Delete tempfil and destroy handle
  40. */
  41. #ifndef INCLUDED_FILEUTIL
  42. #define INCLUDED_FILEUTIL 1
  43. #include "error.h"
  44. #include <stdio.h>
  45. typedef void *HTEMPFILE;
  46. //** Maximum path length
  47. #define cbFILE_NAME_MAX 256 // Maximum filespec length
  48. #define pszALL_FILES "*.*" // Match all files
  49. //** File name characters & wild card characters
  50. #define chPATH_SEP1 '\\' // Character to separate file path components
  51. #define chPATH_SEP2 '/' // Character to separate file path components
  52. // Ex: one<\>two<\>foo.dat
  53. #define chNAME_EXT_SEP '.' // Character that separates file name and ext
  54. // Ex: one\two\foo<.>dat
  55. #define chDRIVE_SEP ':' // Character that separates drive letter
  56. // Ex: a<:>\foo.dat
  57. #define chWILD_RUN '*' // Wild card character that matches a run
  58. #define chWILD_CHAR '?' // Wild card character that matches single char
  59. /*** FILETIMEATTR - Lowest common denominator file date/time/attributes
  60. *
  61. * The format of these match the MS-DOS FAT file system.
  62. */
  63. typedef struct {
  64. USHORT date; // file date
  65. USHORT time; // file time
  66. USHORT attr; // file attibutes
  67. } FILETIMEATTR; /* fta */
  68. typedef FILETIMEATTR *PFILETIMEATTR; /* pfta */
  69. /*** PFNOVERRIDEFILEPROPERTIES - Function type for CopyOneFile override
  70. *** FNOVERRIDEFILEPROPERTIES - macro to help define CopyOneFile override
  71. *
  72. * Entry:
  73. * pfta - File date/time/attr structure
  74. * pv - Client context pointer
  75. * perr - ERROR structure
  76. *
  77. * Exit-Success:
  78. * Returns TRUE, pfta structure may have been modified.
  79. *
  80. * Exit-Failure:
  81. * Returns FALSE,
  82. * ERROR structure filled in with details of error.
  83. */
  84. typedef BOOL (*PFNOVERRIDEFILEPROPERTIES)(PFILETIMEATTR pfta, /* pfnofp */
  85. void *pv,
  86. PERROR perr);
  87. #define FNOVERRIDEFILEPROPERTIES(fn) BOOL fn(PFILETIMEATTR pfta, \
  88. void *pv, \
  89. PERROR perr)
  90. /*** CopyOneFile - Make a faithful copy of a file
  91. *
  92. * Entry:
  93. * pszDst - Name of destination file
  94. * pszSrc - Name of source file
  95. * fCopy - TRUE => copy file; FALSE => open src, call pfnofp to
  96. * merge file date/time/attr values, but skip COPY!
  97. * cbBuffer - Amount of temporary buffer space to use for copying
  98. * pfnofp - Function to override file properties; called with the
  99. * file date/time/attributes for pszSrc to permit client
  100. * to override these values. Pass NULL if no override
  101. * desired.
  102. * pv - Client context pointer
  103. * perr - ERROR structure
  104. *
  105. * Exit-Success:
  106. * Returns TRUE; file copied successfully
  107. *
  108. * Exit-Failure:
  109. * Returns FALSE; perr filled in
  110. */
  111. BOOL CopyOneFile(char *pszDst,
  112. char *pszSrc,
  113. BOOL fCopy,
  114. UINT cbBuffer,
  115. PFNOVERRIDEFILEPROPERTIES pfnofp,
  116. void *pv,
  117. PERROR perr);
  118. /*** getFileSize - Get size of a file
  119. *
  120. * Entry:
  121. * pszFile - Filespec
  122. * perr - ERROR structure
  123. *
  124. * Exit-Success:
  125. * Returns size of file.
  126. *
  127. * Exit-Failure:
  128. * Returns -1; perr filled in with error.
  129. */
  130. long getFileSize(char *pszFile, PERROR perr);
  131. /*** GetFileTimeAndAttr - Get date, time, and attributes from a file
  132. *
  133. * Entry:
  134. * pfta - Structure to receive date, time, and attributes
  135. * pszFile - Name of file to inspect
  136. * perr - ERROR structure
  137. *
  138. * Exit-Success:
  139. * Returns TRUE; pfta filled in
  140. *
  141. * Exit-Failure:
  142. * Returns FALSE; perr filled in
  143. */
  144. BOOL GetFileTimeAndAttr(PFILETIMEATTR pfta, char *pszFile, PERROR perr);
  145. /*** SetFileTimeAndAttr - Set date, time, and attributes of a file
  146. *
  147. * Entry:
  148. * pszFile - Name of file to modify
  149. * pfta - Structure to receive date, time, and attributes
  150. * perr - ERROR structure
  151. *
  152. * Exit-Success:
  153. * Returns TRUE; pfta filled in
  154. *
  155. * Exit-Failure:
  156. * Returns FALSE; perr filled in
  157. */
  158. BOOL SetFileTimeAndAttr(char *pszFile, PFILETIMEATTR pfta, PERROR perr);
  159. /*** appendPathSeparator - Append a path separator only if necessary
  160. *
  161. * Entry:
  162. * pszPathEnd - Pointer to last character in a path string
  163. * (will be NULL byte if path is empty).
  164. * Buffer is assumed to have room for one more character!
  165. *
  166. * Exit:
  167. * Returns 1 if a path separator was appended
  168. * Returns 0 if no path separator was appended:
  169. * 1) Path was empty -or-
  170. * 2) Last character of path was '\', '/', or ':'
  171. */
  172. int appendPathSeparator(char *pszPathEnd);
  173. /*** catDirAndFile - Concatenate a possibly empty dir and file name
  174. *
  175. * Note: pszFile/pszFileDef can actually have path characters, and do not
  176. * need to be file names. Essentially, this function just concatenates
  177. * two strings together, ensuring a path separator is between them!
  178. *
  179. * Entry:
  180. * pszResult - Buffer to receive concatentation
  181. * cbResult - Size of pszResult
  182. * pszDir - Possibly empty directory string
  183. * pszFile - Possibly empty file string
  184. * pszFileDef - Path string to use if pszFile is empty; if NULL, then
  185. * pszFile must NOT be empty. If not NULL, and pszFile
  186. * is empty, then pszFileDef is examined, any path
  187. * prefixes are removed, and the base filename.ext
  188. * is used.
  189. * perr - ERROR structure to fill in
  190. *
  191. * Exit-Success:
  192. * Returns TRUE; pszResult filled in.
  193. *
  194. * Exit-Failure:
  195. * Returns FALSE; perr filled in with error.
  196. */
  197. BOOL catDirAndFile(char * pszResult,
  198. int cbResult,
  199. char * pszDir,
  200. char * pszFile,
  201. char * pszFileDef,
  202. PERROR perr);
  203. /*** ensureDirectory - Ensure directory exists (creating as needed)
  204. *
  205. * Entry:
  206. * pszPath - File spec with directory names to ensure exist
  207. * fHasFileName - TRUE if pszPath has file name at end. In this case
  208. * the last component of pszPath is ignored.
  209. * perr - ERROR structure
  210. *
  211. * Exit-Success:
  212. * Returns TRUE; directory exists
  213. *
  214. * Exit-Failure:
  215. * Returns FALSE; could not create directory, perr filled in with error.
  216. */
  217. BOOL ensureDirectory(char *pszPath, BOOL fHasFileName, PERROR perr);
  218. /*** ensureFile - Ensure a file can be created
  219. *
  220. * Creates any directories that are needed, then creates file and deletes it.
  221. *
  222. * Entry:
  223. * pszPath - File spec with directory names to ensure exist
  224. * pszDesc - Description of type of file (for error message).
  225. * perr - ERROR structure
  226. *
  227. * Exit-Success:
  228. * Returns TRUE; file can be created
  229. *
  230. * Exit-Failure:
  231. * Returns FALSE; could not create file, perr filled in with error.
  232. */
  233. BOOL ensureFile(char *pszFile, char *pszDesc, PERROR perr);
  234. /*** getJustFileNameAndExt - Get last component in filespec
  235. *
  236. * Entry:
  237. * pszPath - Filespec to parse
  238. * perr - ERROR structure to fill in
  239. *
  240. * Exit-Success:
  241. * Returns pointer into pszPath where last component starts
  242. *
  243. * Exit-Failure:
  244. * Returns NULL; perr filled in with error.
  245. */
  246. char *getJustFileNameAndExt(char *pszPath, PERROR perr);
  247. /*** Attr32FromAttrFAT - Convert FAT file attributes to Win32 form
  248. *
  249. * Entry:
  250. * attrMSDOS - MS-DOS (FAT file system) file attributes
  251. *
  252. * Exit:
  253. * Returns equivalent attributes in Win32 format.
  254. */
  255. DWORD Attr32FromAttrFAT(WORD attrMSDOS);
  256. /*** AttrFATFromAttr32 - Convert Win32 file attributes to FAT form
  257. *
  258. * Entry:
  259. * attrMSDOS - MS-DOS (FAT file system) file attributes
  260. *
  261. * Exit:
  262. * Returns equivalent attributes in Win32 format.
  263. */
  264. WORD AttrFATFromAttr32(DWORD attr32);
  265. /*** IsWildMatch - Test filespec against wild card specification
  266. *
  267. * Entry:
  268. * pszPath - Filespec to test; Must not have path characters -- use
  269. * getJustFileNameAndExt() to get rid of them.
  270. * pszWild - Pattern to test against (may have wild cards)
  271. * perr - ERROR structure to fill in
  272. *
  273. * Exit-Success:
  274. * Returns TRUE; pszPath matches pszWild
  275. *
  276. * Exit-Failure:
  277. * Returns FALSE; no match; use ErrIsError(perr) to see if error
  278. * occured.
  279. */
  280. BOOL IsWildMatch(char *pszPath, char *pszWild, PERROR perr);
  281. /*** IsPathRemovable - See if path refers to a removable media drive
  282. *
  283. * Entry:
  284. * pszPath - Path to test
  285. * pchDrive - Pointer to character to receive drive letter
  286. *
  287. * Exit-Success:
  288. * Returns TRUE; path refers to removable media
  289. *
  290. * Exit-Failure:
  291. * Returns FALSE; path is not removable
  292. * occured.
  293. */
  294. BOOL IsPathRemovable(char *pszPath, char *pchDrive);
  295. /*** TmpCreate - Create a temporary file
  296. *
  297. * Entry:
  298. * pszDesc - Description of temp file (for error reporting)
  299. * pszPrefix - Filename prefix
  300. * pszMode - Mode string passed to fopen ("wt", "wb", "rt", etc.)
  301. * perr - ERROR structure
  302. *
  303. * Exit-Success:
  304. * Returns non-null HTEMPFILE; temp file created and open
  305. *
  306. * Exit-Failure:
  307. * Returns NULL; perr filled in
  308. */
  309. HTEMPFILE TmpCreate(char *pszDesc, char *pszPrefix, char *pszMode, PERROR perr);
  310. /*** TmpGetStream - Get FILE* from HTEMPFILE, to perform I/O
  311. *
  312. * Entry:
  313. * htmp - Handle to temp file
  314. * perr - ERROR structure
  315. *
  316. * Exit-Success:
  317. * Returns non-null FILE*
  318. *
  319. * Exit-Failure:
  320. * Returns NULL; If ErrIsError(perr) indicates no error, then the
  321. * stream had simply been closed with TmpClose(). Else, perr has
  322. * error details.
  323. */
  324. FILE *TmpGetStream(HTEMPFILE htmp, PERROR perr);
  325. /*** TmpGetDescription - Get description of temporary file
  326. *
  327. * Entry:
  328. * htmp - Handle to temp file
  329. * perr - ERROR structure
  330. *
  331. * Exit-Success:
  332. * Returns non-null pointer to description
  333. *
  334. * Exit-Failure:
  335. * Returns NULL; perr filled in
  336. */
  337. char *TmpGetDescription(HTEMPFILE htmp, PERROR perr);
  338. /*** TmpGetFileName - Get filename of temporary file
  339. *
  340. * Entry:
  341. * htmp - Handle to temp file
  342. * perr - ERROR structure
  343. *
  344. * Exit-Success:
  345. * Returns non-null pointer to temp filename
  346. *
  347. * Exit-Failure:
  348. * Returns NULL; perr filled in
  349. */
  350. char *TmpGetFileName(HTEMPFILE htmp, PERROR perr);
  351. /*** TmpClose - Close a temporary file stream, but keep tempfile handle
  352. *
  353. * Entry:
  354. * htmp - Handle to temp file;
  355. * NOTE: This call is a NOP if the stream is already closed.
  356. * perr - ERROR structure
  357. *
  358. * Exit-Success:
  359. * Returns TRUE; stream closed
  360. *
  361. * Exit-Failure:
  362. * Returns FALSE; perr filled in
  363. */
  364. BOOL TmpClose(HTEMPFILE htmp, PERROR perr);
  365. /*** TmpOpen - Open the stream for a temporary file
  366. *
  367. * Entry:
  368. * htmp - Handle to temp file
  369. * pszMode - Mode string passed to fopen ("wt", "wb", "rt", etc.)
  370. * perr - ERROR structure
  371. *
  372. * Exit-Success:
  373. * Returns TRUE; stream opened
  374. *
  375. * Exit-Failure:
  376. * Returns NULL; perr filled in
  377. */
  378. BOOL TmpOpen(HTEMPFILE htmp, char *pszMode, PERROR perr);
  379. /*** TmpDestroy - Delete tempfil and destroy handle
  380. *
  381. * Entry:
  382. * htmp - Handle to temp file
  383. * perr - ERROR structure
  384. *
  385. * Exit-Success:
  386. * Returns TRUE; tempfile destroyed
  387. *
  388. * Exit-Failure:
  389. * Returns NULL; perr filled in
  390. */
  391. BOOL TmpDestroy(HTEMPFILE htmp, PERROR perr);
  392. #endif // !INCLUDED_FILEUTIL