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.

2001 lines
65 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. filefind.c
  5. Abstract:
  6. This module implements Win32 FindFirst/FindNext
  7. Author:
  8. Mark Lucovsky (markl) 26-Sep-1990
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. VOID
  13. WINAPI
  14. BasepIoCompletion(
  15. PVOID ApcContext,
  16. PIO_STATUS_BLOCK IoStatusBlock,
  17. DWORD Reserved
  18. );
  19. VOID
  20. WINAPI
  21. BasepIoCompletionSimple(
  22. PVOID ApcContext,
  23. PIO_STATUS_BLOCK IoStatusBlock,
  24. DWORD Reserved
  25. );
  26. #define FIND_BUFFER_SIZE 4096
  27. PFINDFILE_HANDLE
  28. BasepInitializeFindFileHandle(
  29. IN HANDLE DirectoryHandle
  30. )
  31. {
  32. PFINDFILE_HANDLE FindFileHandle;
  33. FindFileHandle = RtlAllocateHeap(RtlProcessHeap(), MAKE_TAG( FIND_TAG ), sizeof(*FindFileHandle));
  34. if ( FindFileHandle ) {
  35. FindFileHandle->DirectoryHandle = DirectoryHandle;
  36. FindFileHandle->FindBufferBase = NULL;
  37. FindFileHandle->FindBufferNext = NULL;
  38. FindFileHandle->FindBufferLength = 0;
  39. FindFileHandle->FindBufferValidLength = 0;
  40. if ( !NT_SUCCESS(RtlInitializeCriticalSection(&FindFileHandle->FindBufferLock)) ){
  41. RtlFreeHeap(RtlProcessHeap(), 0,FindFileHandle);
  42. FindFileHandle = NULL;
  43. }
  44. }
  45. return FindFileHandle;
  46. }
  47. HANDLE
  48. APIENTRY
  49. FindFirstFileA(
  50. LPCSTR lpFileName,
  51. LPWIN32_FIND_DATAA lpFindFileData
  52. )
  53. /*++
  54. Routine Description:
  55. ANSI thunk to FindFirstFileW
  56. --*/
  57. {
  58. HANDLE ReturnValue;
  59. PUNICODE_STRING Unicode;
  60. NTSTATUS Status;
  61. UNICODE_STRING UnicodeString;
  62. WIN32_FIND_DATAW FindFileData;
  63. ANSI_STRING AnsiString;
  64. Unicode = Basep8BitStringToStaticUnicodeString( lpFileName );
  65. if (Unicode == NULL) {
  66. return INVALID_HANDLE_VALUE;
  67. }
  68. ReturnValue = FindFirstFileExW(
  69. (LPCWSTR)Unicode->Buffer,
  70. FindExInfoStandard,
  71. &FindFileData,
  72. FindExSearchNameMatch,
  73. NULL,
  74. 0
  75. );
  76. if ( ReturnValue == INVALID_HANDLE_VALUE ) {
  77. return ReturnValue;
  78. }
  79. RtlCopyMemory(
  80. lpFindFileData,
  81. &FindFileData,
  82. (ULONG_PTR)&FindFileData.cFileName[0] - (ULONG_PTR)&FindFileData
  83. );
  84. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cFileName);
  85. AnsiString.Buffer = lpFindFileData->cFileName;
  86. AnsiString.MaximumLength = MAX_PATH;
  87. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  88. if (NT_SUCCESS(Status)) {
  89. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cAlternateFileName);
  90. AnsiString.Buffer = lpFindFileData->cAlternateFileName;
  91. AnsiString.MaximumLength = 14;
  92. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  93. }
  94. if ( !NT_SUCCESS(Status) ) {
  95. FindClose(ReturnValue);
  96. BaseSetLastNTError(Status);
  97. return INVALID_HANDLE_VALUE;
  98. }
  99. return ReturnValue;
  100. }
  101. HANDLE
  102. APIENTRY
  103. FindFirstFileW(
  104. LPCWSTR lpFileName,
  105. LPWIN32_FIND_DATAW lpFindFileData
  106. )
  107. /*++
  108. Routine Description:
  109. A directory can be searched for the first entry whose name and
  110. attributes match the specified name using FindFirstFile.
  111. This API is provided to open a find file handle and return
  112. information about the first file whose name match the specified
  113. pattern. Once established, the find file handle can be used to
  114. search for other files that match the same pattern. When the find
  115. file handle is no longer needed, it should be closed.
  116. Note that while this interface only returns information for a single
  117. file, an implementation is free to buffer several matching files
  118. that can be used to satisfy subsequent calls to FindNextFile. Also
  119. not that matches are done by name only. This API does not do
  120. attribute based matching.
  121. This API is similar to DOS (int 21h, function 4Eh), and OS/2's
  122. DosFindFirst. For portability reasons, its data structures and
  123. parameter passing is somewhat different.
  124. Arguments:
  125. lpFileName - Supplies the file name of the file to find. The file name
  126. may contain the DOS wild card characters '*' and '?'.
  127. lpFindFileData - On a successful find, this parameter returns information
  128. about the located file:
  129. WIN32_FIND_DATA Structure:
  130. DWORD dwFileAttributes - Returns the file attributes of the found
  131. file.
  132. FILETIME ftCreationTime - Returns the time that the file was created.
  133. A value of 0,0 specifies that the file system containing the
  134. file does not support this time field.
  135. FILETIME ftLastAccessTime - Returns the time that the file was last
  136. accessed. A value of 0,0 specifies that the file system
  137. containing the file does not support this time field.
  138. FILETIME ftLastWriteTime - Returns the time that the file was last
  139. written. A file systems support this time field.
  140. DWORD nFileSizeHigh - Returns the high order 32 bits of the
  141. file's size.
  142. DWORD nFileSizeLow - Returns the low order 32-bits of the file's
  143. size in bytes.
  144. UCHAR cFileName[MAX_PATH] - Returns the null terminated name of
  145. the file.
  146. Return Value:
  147. Not -1 - Returns a find first handle
  148. that can be used in a subsequent call to FindNextFile or FindClose.
  149. 0xffffffff - The operation failed. Extended error status is available
  150. using GetLastError.
  151. --*/
  152. {
  153. return FindFirstFileExW(
  154. lpFileName,
  155. FindExInfoStandard,
  156. lpFindFileData,
  157. FindExSearchNameMatch,
  158. NULL,
  159. 0
  160. );
  161. }
  162. BOOL
  163. APIENTRY
  164. FindNextFileA(
  165. HANDLE hFindFile,
  166. LPWIN32_FIND_DATAA lpFindFileData
  167. )
  168. /*++
  169. Routine Description:
  170. ANSI thunk to FindFileDataW
  171. --*/
  172. {
  173. BOOL ReturnValue;
  174. ANSI_STRING AnsiString;
  175. NTSTATUS Status;
  176. UNICODE_STRING UnicodeString;
  177. WIN32_FIND_DATAW FindFileData;
  178. ReturnValue = FindNextFileW(hFindFile,&FindFileData);
  179. if ( !ReturnValue ) {
  180. return ReturnValue;
  181. }
  182. RtlCopyMemory(
  183. lpFindFileData,
  184. &FindFileData,
  185. (ULONG_PTR)&FindFileData.cFileName[0] - (ULONG_PTR)&FindFileData
  186. );
  187. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cFileName);
  188. AnsiString.Buffer = lpFindFileData->cFileName;
  189. AnsiString.MaximumLength = MAX_PATH;
  190. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  191. if (NT_SUCCESS(Status)) {
  192. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cAlternateFileName);
  193. AnsiString.Buffer = lpFindFileData->cAlternateFileName;
  194. AnsiString.MaximumLength = 14;
  195. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  196. }
  197. if ( !NT_SUCCESS(Status) ) {
  198. BaseSetLastNTError(Status);
  199. return FALSE;
  200. }
  201. return ReturnValue;
  202. }
  203. BOOL
  204. APIENTRY
  205. FindNextFileW(
  206. HANDLE hFindFile,
  207. LPWIN32_FIND_DATAW lpFindFileData
  208. )
  209. /*++
  210. Routine Description:
  211. Once a successful call has been made to FindFirstFile, subsequent
  212. matching files can be located using FindNextFile.
  213. This API is used to continue a file search from a previous call to
  214. FindFirstFile. This API returns successfully with the next file
  215. that matches the search pattern established in the original
  216. FindFirstFile call. If no file match can be found NO_MORE_FILES is
  217. returned.
  218. Note that while this interface only returns information for a single
  219. file, an implementation is free to buffer several matching files
  220. that can be used to satisfy subsequent calls to FindNextFile. Also
  221. not that matches are done by name only. This API does not do
  222. attribute based matching.
  223. This API is similar to DOS (int 21h, function 4Fh), and OS/2's
  224. DosFindNext. For portability reasons, its data structures and
  225. parameter passing is somewhat different.
  226. Arguments:
  227. hFindFile - Supplies a find file handle returned in a previous call
  228. to FindFirstFile.
  229. lpFindFileData - On a successful find, this parameter returns information
  230. about the located file.
  231. Return Value:
  232. TRUE - The operation was successful.
  233. FALSE/NULL - The operation failed. Extended error status is available
  234. using GetLastError.
  235. --*/
  236. {
  237. NTSTATUS Status;
  238. IO_STATUS_BLOCK IoStatusBlock;
  239. PFINDFILE_HANDLE FindFileHandle;
  240. BOOL ReturnValue;
  241. PFILE_BOTH_DIR_INFORMATION DirectoryInfo;
  242. if ( hFindFile == BASE_FIND_FIRST_DEVICE_HANDLE ) {
  243. BaseSetLastNTError(STATUS_NO_MORE_FILES);
  244. return FALSE;
  245. }
  246. if ( hFindFile == INVALID_HANDLE_VALUE ) {
  247. SetLastError(ERROR_INVALID_HANDLE);
  248. return FALSE;
  249. }
  250. ReturnValue = TRUE;
  251. FindFileHandle = (PFINDFILE_HANDLE)hFindFile;
  252. RtlEnterCriticalSection(&FindFileHandle->FindBufferLock);
  253. try {
  254. //
  255. // If we haven't called find next yet, then
  256. // allocate the find buffer.
  257. //
  258. if ( !FindFileHandle->FindBufferBase ) {
  259. FindFileHandle->FindBufferBase = RtlAllocateHeap(RtlProcessHeap(), MAKE_TAG( FIND_TAG ), FIND_BUFFER_SIZE);
  260. if (FindFileHandle->FindBufferBase) {
  261. FindFileHandle->FindBufferNext = FindFileHandle->FindBufferBase;
  262. FindFileHandle->FindBufferLength = FIND_BUFFER_SIZE;
  263. FindFileHandle->FindBufferValidLength = 0;
  264. } else {
  265. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  266. ReturnValue = FALSE;
  267. goto leavefinally;
  268. }
  269. }
  270. //
  271. // Test to see if there is no data in the find file buffer
  272. //
  273. DirectoryInfo = (PFILE_BOTH_DIR_INFORMATION)FindFileHandle->FindBufferNext;
  274. if ( FindFileHandle->FindBufferBase == (PVOID)DirectoryInfo ) {
  275. Status = NtQueryDirectoryFile(
  276. FindFileHandle->DirectoryHandle,
  277. NULL,
  278. NULL,
  279. NULL,
  280. &IoStatusBlock,
  281. DirectoryInfo,
  282. FindFileHandle->FindBufferLength,
  283. FileBothDirectoryInformation,
  284. FALSE,
  285. NULL,
  286. FALSE
  287. );
  288. //
  289. // ***** Do a kludge hack fix for now *****
  290. //
  291. // Forget about the last, partial, entry.
  292. //
  293. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  294. PULONG Ptr;
  295. PULONG PriorPtr;
  296. Ptr = (PULONG)DirectoryInfo;
  297. PriorPtr = NULL;
  298. while ( *Ptr != 0 ) {
  299. PriorPtr = Ptr;
  300. Ptr += (*Ptr / sizeof(ULONG));
  301. }
  302. if (PriorPtr != NULL) {
  303. *PriorPtr = 0;
  304. }
  305. Status = STATUS_SUCCESS;
  306. }
  307. if ( !NT_SUCCESS(Status) ) {
  308. BaseSetLastNTError(Status);
  309. ReturnValue = FALSE;
  310. goto leavefinally;
  311. }
  312. }
  313. if ( DirectoryInfo->NextEntryOffset ) {
  314. FindFileHandle->FindBufferNext = (PVOID)(
  315. (PUCHAR)DirectoryInfo + DirectoryInfo->NextEntryOffset);
  316. } else {
  317. FindFileHandle->FindBufferNext = FindFileHandle->FindBufferBase;
  318. }
  319. //
  320. // Attributes are composed of the attributes returned by NT.
  321. //
  322. lpFindFileData->dwFileAttributes = DirectoryInfo->FileAttributes;
  323. lpFindFileData->ftCreationTime = *(LPFILETIME)&DirectoryInfo->CreationTime;
  324. lpFindFileData->ftLastAccessTime = *(LPFILETIME)&DirectoryInfo->LastAccessTime;
  325. lpFindFileData->ftLastWriteTime = *(LPFILETIME)&DirectoryInfo->LastWriteTime;
  326. lpFindFileData->nFileSizeHigh = DirectoryInfo->EndOfFile.HighPart;
  327. lpFindFileData->nFileSizeLow = DirectoryInfo->EndOfFile.LowPart;
  328. RtlCopyMemory( lpFindFileData->cFileName,
  329. DirectoryInfo->FileName,
  330. DirectoryInfo->FileNameLength );
  331. lpFindFileData->cFileName[DirectoryInfo->FileNameLength >> 1] = UNICODE_NULL;
  332. RtlCopyMemory( lpFindFileData->cAlternateFileName,
  333. DirectoryInfo->ShortName,
  334. DirectoryInfo->ShortNameLength );
  335. lpFindFileData->cAlternateFileName[DirectoryInfo->ShortNameLength >> 1] = UNICODE_NULL;
  336. //
  337. // For NTFS reparse points we return the reparse point data tag in dwReserved0.
  338. //
  339. if ( DirectoryInfo->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ) {
  340. lpFindFileData->dwReserved0 = DirectoryInfo->EaSize;
  341. }
  342. leavefinally:;
  343. } finally{
  344. RtlLeaveCriticalSection(&FindFileHandle->FindBufferLock);
  345. }
  346. return ReturnValue;
  347. }
  348. BOOL
  349. FindClose(
  350. HANDLE hFindFile
  351. )
  352. /*++
  353. Routine Description:
  354. A find file context created by FindFirstFile can be closed using
  355. FindClose.
  356. This API is used to inform the system that a find file handle
  357. created by FindFirstFile is no longer needed. On systems that
  358. maintain internal state for each find file context, this API informs
  359. the system that this state no longer needs to be maintained.
  360. Once this call has been made, the hFindFile may not be used in a
  361. subsequent call to either FindNextFile or FindClose.
  362. This API has no DOS counterpart, but is similar to OS/2's
  363. DosFindClose.
  364. Arguments:
  365. hFindFile - Supplies a find file handle returned in a previous call
  366. to FindFirstFile that is no longer needed.
  367. Return Value:
  368. TRUE - The operation was successful.
  369. FALSE/NULL - The operation failed. Extended error status is available
  370. using GetLastError.
  371. --*/
  372. {
  373. NTSTATUS Status;
  374. PFINDFILE_HANDLE FindFileHandle;
  375. HANDLE DirectoryHandle;
  376. PVOID FindBufferBase;
  377. if ( hFindFile == BASE_FIND_FIRST_DEVICE_HANDLE ) {
  378. return TRUE;
  379. }
  380. if ( hFindFile == INVALID_HANDLE_VALUE ) {
  381. SetLastError(ERROR_INVALID_HANDLE);
  382. return FALSE;
  383. }
  384. try {
  385. FindFileHandle = (PFINDFILE_HANDLE)hFindFile;
  386. RtlEnterCriticalSection(&FindFileHandle->FindBufferLock);
  387. DirectoryHandle = FindFileHandle->DirectoryHandle;
  388. FindBufferBase = FindFileHandle->FindBufferBase;
  389. FindFileHandle->DirectoryHandle = INVALID_HANDLE_VALUE;
  390. FindFileHandle->FindBufferBase = NULL;
  391. RtlLeaveCriticalSection(&FindFileHandle->FindBufferLock);
  392. Status = NtClose(DirectoryHandle);
  393. if ( NT_SUCCESS(Status) ) {
  394. if (FindBufferBase) {
  395. RtlFreeHeap(RtlProcessHeap(), 0,FindBufferBase);
  396. }
  397. RtlDeleteCriticalSection(&FindFileHandle->FindBufferLock);
  398. RtlFreeHeap(RtlProcessHeap(), 0,FindFileHandle);
  399. return TRUE;
  400. }
  401. else {
  402. BaseSetLastNTError(Status);
  403. return FALSE;
  404. }
  405. }
  406. except ( EXCEPTION_EXECUTE_HANDLER ) {
  407. BaseSetLastNTError(GetExceptionCode());
  408. return FALSE;
  409. }
  410. return FALSE;
  411. }
  412. HANDLE
  413. WINAPI
  414. FindFirstFileExA(
  415. LPCSTR lpFileName,
  416. FINDEX_INFO_LEVELS fInfoLevelId,
  417. LPVOID lpFindFileData,
  418. FINDEX_SEARCH_OPS fSearchOp,
  419. LPVOID lpSearchFilter,
  420. DWORD dwAdditionalFlags
  421. )
  422. {
  423. HANDLE ReturnValue;
  424. PUNICODE_STRING Unicode;
  425. NTSTATUS Status;
  426. UNICODE_STRING UnicodeString;
  427. WIN32_FIND_DATAW FindFileData;
  428. LPWIN32_FIND_DATAA lpFindFileDataA;
  429. ANSI_STRING AnsiString;
  430. //
  431. // this code assumes that only FindExInfoStandard is supperted by ExW version
  432. // when more info levels are added, the W->A translation code needs to be modified
  433. //
  434. lpFindFileDataA = (LPWIN32_FIND_DATAA)lpFindFileData;
  435. Unicode = Basep8BitStringToStaticUnicodeString( lpFileName );
  436. if (Unicode == NULL) {
  437. return INVALID_HANDLE_VALUE;
  438. }
  439. ReturnValue = FindFirstFileExW(
  440. (LPCWSTR)Unicode->Buffer,
  441. fInfoLevelId,
  442. (LPVOID)&FindFileData,
  443. fSearchOp,
  444. lpSearchFilter,
  445. dwAdditionalFlags
  446. );
  447. if ( ReturnValue == INVALID_HANDLE_VALUE ) {
  448. return ReturnValue;
  449. }
  450. RtlCopyMemory(
  451. lpFindFileData,
  452. &FindFileData,
  453. (ULONG_PTR)&FindFileData.cFileName[0] - (ULONG_PTR)&FindFileData
  454. );
  455. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cFileName);
  456. AnsiString.Buffer = lpFindFileDataA->cFileName;
  457. AnsiString.MaximumLength = MAX_PATH;
  458. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  459. if (NT_SUCCESS(Status)) {
  460. RtlInitUnicodeString(&UnicodeString,(PWSTR)FindFileData.cAlternateFileName);
  461. AnsiString.Buffer = lpFindFileDataA->cAlternateFileName;
  462. AnsiString.MaximumLength = 14;
  463. Status = BasepUnicodeStringTo8BitString(&AnsiString,&UnicodeString,FALSE);
  464. }
  465. if ( !NT_SUCCESS(Status) ) {
  466. FindClose(ReturnValue);
  467. BaseSetLastNTError(Status);
  468. return INVALID_HANDLE_VALUE;
  469. }
  470. return ReturnValue;
  471. }
  472. HANDLE
  473. WINAPI
  474. FindFirstFileExW(
  475. LPCWSTR lpFileName,
  476. FINDEX_INFO_LEVELS fInfoLevelId,
  477. LPVOID lpFindFileData,
  478. FINDEX_SEARCH_OPS fSearchOp,
  479. LPVOID lpSearchFilter,
  480. DWORD dwAdditionalFlags
  481. )
  482. /*++
  483. Routine Description:
  484. A directory can be searched for the first entry whose name and
  485. attributes match the specified name using FindFirstFileEx.
  486. This API is provided to open a find file handle and return
  487. information about the first file whose name matchs the specified
  488. pattern. If the fSearchOp is FindExSearchNameMatch, then that is
  489. the extent of the filtering, and lpSearchFilter MUST be NULL.
  490. Otherwise, additional subfiltering is done depending on this value.
  491. FindExSearchLimitToDirectories - If this search op is specified,
  492. then lpSearchFilter MUST be NULL. For each file that
  493. matches the specified filename, and that is a directory, and
  494. entry for that file is returned.
  495. If the underlying file/io system does not support this type
  496. of filtering, the API will fail with ERROR_NOT_SUPPORTED,
  497. and the application will have to perform its own filtering
  498. by calling this API with FindExSearchNameMatch.
  499. FindExSearchLimitToDevices - If this search op is specified, the
  500. lpFileName MUST be *, and FIND_FIRST_EX_CASE_SENSITIVE
  501. must NOT be specified. Only device names are returned.
  502. Device names are generally accessible through
  503. \\.\name-of-device naming.
  504. The data returned by this API is dependent on the fInfoLevelId.
  505. FindExInfoStandard - The lpFindFileData pointer is the standard
  506. LPWIN32_FIND_DATA structure.
  507. At this time, no other information levels are supported
  508. Once established, the find file handle can be used to search for
  509. other files that match the same pattern with the same filtering
  510. being performed. When the find file handle is no longer needed, it
  511. should be closed.
  512. Note that while this interface only returns information for a single
  513. file, an implementation is free to buffer several matching files
  514. that can be used to satisfy subsequent calls to FindNextFileEx.
  515. This API is a complete superset of existing FindFirstFile. FindFirstFile
  516. could be coded as the following macro:
  517. #define FindFirstFile(a,b)
  518. FindFirstFileEx((a),FindExInfoStandard,(b),FindExSearchNameMatch,NULL,0);
  519. Arguments:
  520. lpFileName - Supplies the file name of the file to find. The file name
  521. may contain the DOS wild card characters '*' and '?'.
  522. fInfoLevelId - Supplies the info level of the returned data.
  523. lpFindFileData - Supplies a pointer whose type is dependent on the value
  524. of fInfoLevelId. This buffer returns the appropriate file data.
  525. fSearchOp - Specified the type of filtering to perform above and
  526. beyond simple wildcard matching.
  527. lpSearchFilter - If the specified fSearchOp needs structured search
  528. information, this pointer points to the search criteria. At
  529. this point in time, both search ops do not require extended
  530. search information, so this pointer is NULL.
  531. dwAdditionalFlags - Supplies additional flag values that control the
  532. search. A flag value of FIND_FIRST_EX_CASE_SENSITIVE can be
  533. used to cause case sensitive searches to occur. The default is
  534. case insensitive.
  535. Return Value:
  536. Not -1 - Returns a find first handle that can be used in a
  537. subsequent call to FindNextFileEx or FindClose.
  538. 0xffffffff - The operation failed. Extended error status is available
  539. using GetLastError.
  540. --*/
  541. {
  542. #define FIND_FIRST_EX_INVALID_FLAGS (~FIND_FIRST_EX_CASE_SENSITIVE)
  543. HANDLE hFindFile;
  544. NTSTATUS Status;
  545. OBJECT_ATTRIBUTES Obja;
  546. UNICODE_STRING FileName;
  547. UNICODE_STRING PathName;
  548. IO_STATUS_BLOCK IoStatusBlock;
  549. PFILE_BOTH_DIR_INFORMATION DirectoryInfo;
  550. struct SEARCH_BUFFER {
  551. FILE_BOTH_DIR_INFORMATION DirInfo;
  552. WCHAR Names[MAX_PATH];
  553. } Buffer;
  554. BOOLEAN TranslationStatus;
  555. RTL_RELATIVE_NAME_U RelativeName;
  556. PVOID FreeBuffer;
  557. UNICODE_STRING UnicodeInput;
  558. PFINDFILE_HANDLE FindFileHandle;
  559. BOOLEAN EndsInDot;
  560. LPWIN32_FIND_DATAW FindFileData;
  561. BOOLEAN StrippedTrailingSlash;
  562. //
  563. // check parameters
  564. //
  565. if ( fInfoLevelId >= FindExInfoMaxInfoLevel ||
  566. fSearchOp >= FindExSearchLimitToDevices ||
  567. dwAdditionalFlags & FIND_FIRST_EX_INVALID_FLAGS ) {
  568. SetLastError(fSearchOp == FindExSearchLimitToDevices ? ERROR_NOT_SUPPORTED : ERROR_INVALID_PARAMETER);
  569. return INVALID_HANDLE_VALUE;
  570. }
  571. FindFileData = (LPWIN32_FIND_DATAW)lpFindFileData;
  572. RtlInitUnicodeString(&UnicodeInput,lpFileName);
  573. //
  574. // Bogus code to workaround ~* problem
  575. //
  576. if ( UnicodeInput.Length && UnicodeInput.Buffer[(UnicodeInput.Length>>1)-1] == (WCHAR)'.' ) {
  577. EndsInDot = TRUE;
  578. } else {
  579. EndsInDot = FALSE;
  580. }
  581. TranslationStatus = RtlDosPathNameToRelativeNtPathName_U(
  582. lpFileName,
  583. &PathName,
  584. &FileName.Buffer,
  585. &RelativeName
  586. );
  587. if ( !TranslationStatus ) {
  588. SetLastError(ERROR_PATH_NOT_FOUND);
  589. return INVALID_HANDLE_VALUE;
  590. }
  591. FreeBuffer = PathName.Buffer;
  592. //
  593. // If there is a a file portion of this name, determine the length
  594. // of the name for a subsequent call to NtQueryDirectoryFile.
  595. //
  596. if (FileName.Buffer) {
  597. FileName.Length =
  598. PathName.Length - (USHORT)((ULONG_PTR)FileName.Buffer - (ULONG_PTR)PathName.Buffer);
  599. } else {
  600. FileName.Length = 0;
  601. }
  602. FileName.MaximumLength = FileName.Length;
  603. if ( RelativeName.RelativeName.Length &&
  604. RelativeName.RelativeName.Buffer != FileName.Buffer ) {
  605. if (FileName.Buffer) {
  606. PathName.Length = (USHORT)((ULONG_PTR)FileName.Buffer - (ULONG_PTR)RelativeName.RelativeName.Buffer);
  607. PathName.MaximumLength = PathName.Length;
  608. PathName.Buffer = RelativeName.RelativeName.Buffer;
  609. }
  610. } else {
  611. RelativeName.ContainingDirectory = NULL;
  612. if (FileName.Buffer) {
  613. PathName.Length = (USHORT)((ULONG_PTR)FileName.Buffer - (ULONG_PTR)PathName.Buffer);
  614. PathName.MaximumLength = PathName.Length;
  615. }
  616. }
  617. if ( (PathName.Length>>1) >= 2 &&
  618. PathName.Buffer[(PathName.Length>>1)-2] != L':' &&
  619. PathName.Buffer[(PathName.Length>>1)-1] != L'\\' ) {
  620. PathName.Length -= sizeof(UNICODE_NULL);
  621. StrippedTrailingSlash = TRUE;
  622. } else {
  623. StrippedTrailingSlash = FALSE;
  624. }
  625. InitializeObjectAttributes(
  626. &Obja,
  627. &PathName,
  628. (dwAdditionalFlags & FIND_FIRST_EX_CASE_SENSITIVE) ? 0 : OBJ_CASE_INSENSITIVE,
  629. RelativeName.ContainingDirectory,
  630. NULL
  631. );
  632. //
  633. // Open the directory for list access
  634. //
  635. Status = NtOpenFile(
  636. &hFindFile,
  637. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  638. &Obja,
  639. &IoStatusBlock,
  640. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  641. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  642. );
  643. if ( (Status == STATUS_INVALID_PARAMETER ||
  644. Status == STATUS_NOT_A_DIRECTORY) && StrippedTrailingSlash ) {
  645. //
  646. // open of a pnp style path failed, so try putting back the trailing slash
  647. //
  648. PathName.Length += sizeof(UNICODE_NULL);
  649. Status = NtOpenFile(
  650. &hFindFile,
  651. FILE_LIST_DIRECTORY | SYNCHRONIZE,
  652. &Obja,
  653. &IoStatusBlock,
  654. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  655. FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_FOR_BACKUP_INTENT
  656. );
  657. PathName.Length -= sizeof(UNICODE_NULL);
  658. }
  659. if ( !NT_SUCCESS(Status) ) {
  660. ULONG DeviceNameData;
  661. UNICODE_STRING DeviceName;
  662. RtlReleaseRelativeName(&RelativeName);
  663. RtlFreeHeap(RtlProcessHeap(), 0,FreeBuffer);
  664. //
  665. // The full path does not refer to a directory. This could
  666. // be a device. Check for a device name.
  667. //
  668. if ( DeviceNameData = RtlIsDosDeviceName_U(UnicodeInput.Buffer) ) {
  669. DeviceName.Length = (USHORT)(DeviceNameData & 0xffff);
  670. DeviceName.MaximumLength = (USHORT)(DeviceNameData & 0xffff);
  671. DeviceName.Buffer = (PWSTR)
  672. ((PUCHAR)UnicodeInput.Buffer + (DeviceNameData >> 16));
  673. return BaseFindFirstDevice(&DeviceName,FindFileData);
  674. }
  675. if ( Status == STATUS_OBJECT_NAME_NOT_FOUND ) {
  676. Status = STATUS_OBJECT_PATH_NOT_FOUND;
  677. }
  678. if ( Status == STATUS_OBJECT_TYPE_MISMATCH ) {
  679. Status = STATUS_OBJECT_PATH_NOT_FOUND;
  680. }
  681. BaseSetLastNTError(Status);
  682. return INVALID_HANDLE_VALUE;
  683. }
  684. //
  685. // Get an entry
  686. //
  687. //
  688. // If there is no file part, but we are not looking at a device,
  689. // then bail.
  690. //
  691. if ( !FileName.Length ) {
  692. RtlReleaseRelativeName(&RelativeName);
  693. RtlFreeHeap(RtlProcessHeap(), 0,FreeBuffer);
  694. NtClose(hFindFile);
  695. SetLastError(ERROR_FILE_NOT_FOUND);
  696. return INVALID_HANDLE_VALUE;
  697. }
  698. DirectoryInfo = &Buffer.DirInfo;
  699. //
  700. // Special case *.* to * since it is so common. Otherwise transmogrify
  701. // the input name according to the following rules:
  702. //
  703. // - Change all ? to DOS_QM
  704. // - Change all . followed by ? or * to DOS_DOT
  705. // - Change all * followed by a . into DOS_STAR
  706. //
  707. // These transmogrifications are all done in place.
  708. //
  709. if ( (FileName.Length == 6) &&
  710. (RtlCompareMemory(FileName.Buffer, L"*.*", 6) == 6) ) {
  711. FileName.Length = 2;
  712. } else {
  713. ULONG Index;
  714. WCHAR *NameChar;
  715. for ( Index = 0, NameChar = FileName.Buffer;
  716. Index < FileName.Length/sizeof(WCHAR);
  717. Index += 1, NameChar += 1) {
  718. if (Index && (*NameChar == L'.') && (*(NameChar - 1) == L'*')) {
  719. *(NameChar - 1) = DOS_STAR;
  720. }
  721. if ((*NameChar == L'?') || (*NameChar == L'*')) {
  722. if (*NameChar == L'?') { *NameChar = DOS_QM; }
  723. if (Index && *(NameChar-1) == L'.') { *(NameChar-1) = DOS_DOT; }
  724. }
  725. }
  726. if (EndsInDot && *(NameChar - 1) == L'*') { *(NameChar-1) = DOS_STAR; }
  727. }
  728. Status = NtQueryDirectoryFile(
  729. hFindFile,
  730. NULL,
  731. NULL,
  732. NULL,
  733. &IoStatusBlock,
  734. DirectoryInfo,
  735. sizeof(Buffer),
  736. FileBothDirectoryInformation,
  737. TRUE,
  738. &FileName,
  739. FALSE
  740. );
  741. RtlReleaseRelativeName(&RelativeName);
  742. RtlFreeHeap(RtlProcessHeap(), 0,FreeBuffer);
  743. if ( !NT_SUCCESS(Status) ) {
  744. NtClose(hFindFile);
  745. BaseSetLastNTError(Status);
  746. return INVALID_HANDLE_VALUE;
  747. }
  748. //
  749. // Attributes are composed of the attributes returned by NT.
  750. //
  751. FindFileData->dwFileAttributes = DirectoryInfo->FileAttributes;
  752. FindFileData->ftCreationTime = *(LPFILETIME)&DirectoryInfo->CreationTime;
  753. FindFileData->ftLastAccessTime = *(LPFILETIME)&DirectoryInfo->LastAccessTime;
  754. FindFileData->ftLastWriteTime = *(LPFILETIME)&DirectoryInfo->LastWriteTime;
  755. FindFileData->nFileSizeHigh = DirectoryInfo->EndOfFile.HighPart;
  756. FindFileData->nFileSizeLow = DirectoryInfo->EndOfFile.LowPart;
  757. RtlCopyMemory( FindFileData->cFileName,
  758. DirectoryInfo->FileName,
  759. DirectoryInfo->FileNameLength );
  760. FindFileData->cFileName[DirectoryInfo->FileNameLength >> 1] = UNICODE_NULL;
  761. RtlCopyMemory( FindFileData->cAlternateFileName,
  762. DirectoryInfo->ShortName,
  763. DirectoryInfo->ShortNameLength );
  764. FindFileData->cAlternateFileName[DirectoryInfo->ShortNameLength >> 1] = UNICODE_NULL;
  765. //
  766. // For NTFS reparse points we return the reparse point data tag in dwReserved0.
  767. //
  768. if ( DirectoryInfo->FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT ) {
  769. FindFileData->dwReserved0 = DirectoryInfo->EaSize;
  770. }
  771. FindFileHandle = BasepInitializeFindFileHandle(hFindFile);
  772. if ( !FindFileHandle ) {
  773. NtClose(hFindFile);
  774. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  775. return INVALID_HANDLE_VALUE;
  776. }
  777. return (HANDLE)FindFileHandle;
  778. }
  779. HANDLE
  780. BaseFindFirstDevice(
  781. PCUNICODE_STRING FileName,
  782. LPWIN32_FIND_DATAW lpFindFileData
  783. )
  784. /*++
  785. Routine Description:
  786. This function is called when find first file encounters a device
  787. name. This function returns a successful psuedo file handle and
  788. fills in the find file data with all zeros and the devic name.
  789. Arguments:
  790. FileName - Supplies the device name of the file to find.
  791. lpFindFileData - On a successful find, this parameter returns information
  792. about the located file.
  793. Return Value:
  794. Always returns a static find file handle value of
  795. BASE_FIND_FIRST_DEVICE_HANDLE
  796. --*/
  797. {
  798. RtlZeroMemory(lpFindFileData,sizeof(*lpFindFileData));
  799. lpFindFileData->dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE;
  800. RtlMoveMemory(
  801. lpFindFileData->cFileName,
  802. FileName->Buffer,
  803. (FileName->MaximumLength < sizeof(lpFindFileData->cFileName)
  804. ? FileName->MaximumLength
  805. : sizeof(lpFindFileData->cFileName))
  806. );
  807. lpFindFileData->cFileName[MAX_PATH - 1] = UNICODE_NULL;
  808. return BASE_FIND_FIRST_DEVICE_HANDLE;
  809. }
  810. HANDLE
  811. APIENTRY
  812. FindFirstChangeNotificationA(
  813. LPCSTR lpPathName,
  814. BOOL bWatchSubtree,
  815. DWORD dwNotifyFilter
  816. )
  817. /*++
  818. Routine Description:
  819. ANSI thunk to FindFirstChangeNotificationW
  820. --*/
  821. {
  822. PUNICODE_STRING Unicode;
  823. ANSI_STRING AnsiString;
  824. NTSTATUS Status;
  825. Unicode = &NtCurrentTeb()->StaticUnicodeString;
  826. RtlInitAnsiString(&AnsiString,lpPathName);
  827. Status = RtlAnsiStringToUnicodeString(Unicode,&AnsiString,FALSE);
  828. if ( !NT_SUCCESS(Status) ) {
  829. if ( Status == STATUS_BUFFER_OVERFLOW ) {
  830. SetLastError(ERROR_FILENAME_EXCED_RANGE);
  831. }
  832. else {
  833. BaseSetLastNTError(Status);
  834. }
  835. return FALSE;
  836. }
  837. return ( FindFirstChangeNotificationW(
  838. (LPCWSTR)Unicode->Buffer,
  839. bWatchSubtree,
  840. dwNotifyFilter
  841. )
  842. );
  843. }
  844. //
  845. // this is a hack... darrylh, please remove when NT supports null
  846. // buffers to change notify
  847. //
  848. char staticchangebuff[sizeof(FILE_NOTIFY_INFORMATION) + 16];
  849. IO_STATUS_BLOCK staticIoStatusBlock;
  850. HANDLE
  851. APIENTRY
  852. FindFirstChangeNotificationW(
  853. LPCWSTR lpPathName,
  854. BOOL bWatchSubtree,
  855. DWORD dwNotifyFilter
  856. )
  857. /*++
  858. Routine Description:
  859. This API is used to create a change notification handle and to set
  860. up the initial change notification filter conditions.
  861. If successful, this API returns a waitable notification handle. A
  862. wait on a notification handle is successful when a change matching
  863. the filter conditions occurs in the directory or subtree being
  864. watched.
  865. Once a change notification object is created and the initial filter
  866. conditions are set, the appropriate directory or subtree is
  867. monitored by the system for changes that match the specified filter
  868. conditions. When one of these changes occurs, a change notification
  869. wait is satisfied. If a change occurs without an outstanding change
  870. notification request, it is remembered by the system and will
  871. satisfy the next change notification wait.
  872. Note that this means that after a call to
  873. FindFirstChangeNotification is made, the application should wait on
  874. the notification handle before making another call to
  875. FindNextChangeNotification.
  876. Arguments:
  877. lpPathName - Supplies the pathname of the directory to be watched.
  878. This path must specify the pathname of a directory.
  879. bWatchSubtree - Supplies a boolean value that if TRUE causes the
  880. system to monitor the directory tree rooted at the specified
  881. directory. A value of FALSE causes the system to monitor only
  882. the specified directory.
  883. dwNotifyFilter - Supplies a set of flags that specify the filter
  884. conditions the system uses to satisfy a change notification
  885. wait.
  886. FILE_NOTIFY_CHANGE_FILENAME - Any file name changes that occur
  887. in a directory or subtree being watched will satisfy a
  888. change notification wait. This includes renames, creations,
  889. and deletes.
  890. FILE_NOTIFY_CHANGE_DIRNAME - Any directory name changes that occur
  891. in a directory or subtree being watched will satisfy a
  892. change notification wait. This includes directory creations
  893. and deletions.
  894. FILE_NOTIFY_CHANGE_ATTRIBUTES - Any attribute changes that occur
  895. in a directory or subtree being watched will satisfy a
  896. change notification wait.
  897. FILE_NOTIFY_CHANGE_SIZE - Any file size changes that occur in a
  898. directory or subtree being watched will satisfy a change
  899. notification wait. File sizes only cause a change when the
  900. on disk structure is updated. For systems with extensive
  901. caching this may only occur when the system cache is
  902. sufficiently flushed.
  903. FILE_NOTIFY_CHANGE_LAST_WRITE - Any last write time changes that
  904. occur in a directory or subtree being watched will satisfy a
  905. change notification wait. Last write time change only cause
  906. a change when the on disk structure is updated. For systems
  907. with extensive caching this may only occur when the system
  908. cache is sufficiently flushed.
  909. FILE_NOTIFY_CHANGE_SECURITY - Any security descriptor changes
  910. that occur in a directory or subtree being watched will
  911. satisfy a change notification wait.
  912. Return Value:
  913. Not -1 - Returns a find change notification handle. The handle is a
  914. waitable handle. A wait is satisfied when one of the filter
  915. conditions occur in a directory or subtree being monitored. The
  916. handle may also be used in a subsequent call to
  917. FindNextChangeNotify and in FindCloseChangeNotify.
  918. 0xffffffff - The operation failed. Extended error status is available
  919. using GetLastError.
  920. --*/
  921. {
  922. NTSTATUS Status;
  923. OBJECT_ATTRIBUTES Obja;
  924. HANDLE Handle;
  925. UNICODE_STRING FileName;
  926. IO_STATUS_BLOCK IoStatusBlock;
  927. BOOLEAN TranslationStatus;
  928. RTL_RELATIVE_NAME_U RelativeName;
  929. PVOID FreeBuffer;
  930. TranslationStatus = RtlDosPathNameToRelativeNtPathName_U(
  931. lpPathName,
  932. &FileName,
  933. NULL,
  934. &RelativeName
  935. );
  936. if ( !TranslationStatus ) {
  937. SetLastError(ERROR_PATH_NOT_FOUND);
  938. return FALSE;
  939. }
  940. FreeBuffer = FileName.Buffer;
  941. if ( RelativeName.RelativeName.Length ) {
  942. FileName = RelativeName.RelativeName;
  943. }
  944. else {
  945. RelativeName.ContainingDirectory = NULL;
  946. }
  947. InitializeObjectAttributes(
  948. &Obja,
  949. &FileName,
  950. OBJ_CASE_INSENSITIVE,
  951. RelativeName.ContainingDirectory,
  952. NULL
  953. );
  954. //
  955. // Open the file
  956. //
  957. Status = NtOpenFile(
  958. &Handle,
  959. (ACCESS_MASK)FILE_LIST_DIRECTORY | SYNCHRONIZE,
  960. &Obja,
  961. &IoStatusBlock,
  962. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  963. FILE_DIRECTORY_FILE | FILE_OPEN_FOR_BACKUP_INTENT
  964. );
  965. RtlReleaseRelativeName(&RelativeName);
  966. RtlFreeHeap(RtlProcessHeap(), 0,FreeBuffer);
  967. if ( !NT_SUCCESS(Status) ) {
  968. BaseSetLastNTError(Status);
  969. return INVALID_HANDLE_VALUE;
  970. }
  971. //
  972. // call change notify
  973. //
  974. Status = NtNotifyChangeDirectoryFile(
  975. Handle,
  976. NULL,
  977. NULL,
  978. NULL,
  979. &staticIoStatusBlock,
  980. staticchangebuff, // should be NULL
  981. sizeof(staticchangebuff),
  982. dwNotifyFilter,
  983. (BOOLEAN)bWatchSubtree
  984. );
  985. if ( !NT_SUCCESS(Status) ) {
  986. BaseSetLastNTError(Status);
  987. NtClose(Handle);
  988. Handle = INVALID_HANDLE_VALUE;
  989. }
  990. return Handle;
  991. }
  992. BOOL
  993. APIENTRY
  994. FindNextChangeNotification(
  995. HANDLE hChangeHandle
  996. )
  997. /*++
  998. Routine Description:
  999. This API is used to request that a change notification handle
  1000. be signaled the next time the system dectects an appropriate
  1001. change.
  1002. If a change occurs prior to this call that would otherwise satisfy
  1003. a change request, it is remembered by the system and will satisfy
  1004. this request.
  1005. Once a successful change notification request has been made, the
  1006. application should wait on the change notification handle to
  1007. pick up the change.
  1008. If an application calls this API with a change request outstanding,
  1009. .
  1010. .
  1011. FindNextChangeNotification(h);
  1012. FindNextChangeNotification(h);
  1013. WaitForSingleObject(h,-1);
  1014. .
  1015. .
  1016. it may miss a change notification.
  1017. Arguments:
  1018. hChangeHandle - Supplies a change notification handle created
  1019. using FindFirstChangeNotification.
  1020. Return Value:
  1021. TRUE - The change notification request was registered. A wait on the
  1022. change handle should be issued to pick up the change notification.
  1023. FALSE - The operation failed. Extended error status is available
  1024. using GetLastError.
  1025. --*/
  1026. {
  1027. NTSTATUS Status;
  1028. BOOL ReturnValue;
  1029. ReturnValue = TRUE;
  1030. //
  1031. // call change notify
  1032. //
  1033. Status = NtNotifyChangeDirectoryFile(
  1034. hChangeHandle,
  1035. NULL,
  1036. NULL,
  1037. NULL,
  1038. &staticIoStatusBlock,
  1039. staticchangebuff, // should be NULL
  1040. sizeof(staticchangebuff),
  1041. FILE_NOTIFY_CHANGE_NAME, // not needed bug workaround
  1042. TRUE // not needed bug workaround
  1043. );
  1044. if ( !NT_SUCCESS(Status) ) {
  1045. BaseSetLastNTError(Status);
  1046. ReturnValue = FALSE;
  1047. }
  1048. return ReturnValue;
  1049. }
  1050. BOOL
  1051. APIENTRY
  1052. FindCloseChangeNotification(
  1053. HANDLE hChangeHandle
  1054. )
  1055. /*++
  1056. Routine Description:
  1057. This API is used close a change notification handle and to tell the
  1058. system to stop monitoring changes on the notification handle.
  1059. Arguments:
  1060. hChangeHandle - Supplies a change notification handle created
  1061. using FindFirstChangeNotification.
  1062. Return Value:
  1063. TRUE - The change notification handle was closed.
  1064. FALSE - The operation failed. Extended error status is available
  1065. using GetLastError.
  1066. --*/
  1067. {
  1068. return CloseHandle(hChangeHandle);
  1069. }
  1070. BOOL
  1071. WINAPI
  1072. ReadDirectoryChangesW(
  1073. HANDLE hDirectory,
  1074. LPVOID lpBuffer,
  1075. DWORD nBufferLength,
  1076. BOOL bWatchSubtree,
  1077. DWORD dwNotifyFilter,
  1078. LPDWORD lpBytesReturned,
  1079. LPOVERLAPPED lpOverlapped,
  1080. LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  1081. )
  1082. /*++
  1083. Routine Description:
  1084. This rountine allows you to read changes that occur in a directory
  1085. or a tree rooted at the specified directory. It is similar to the
  1086. FindxxxChangeNotification family of APIs, but this API can return
  1087. structured data describing the changes occuring within a directory.
  1088. This API requires the caller to pass in an open directory handle to
  1089. the directory that is to be read. The handle must be opened with
  1090. FILE_LIST_DIRECTORY acces. GENERIC_READ includes this and may also
  1091. be used. The directory may be opened for overlapped access. This
  1092. technique should be used whenever you call this API asynchronously
  1093. (by specifying and lpOverlapped value). Opening a directory in
  1094. Win32 is easy. Use CreateFile, pass in the name of a directory, and
  1095. make sure you specify FILE_FLAG_BACKUP_SEMANTICS. This will allow
  1096. you to open a directory. This technique will not force a directory
  1097. to be opened. It simply allows you to open a directory. Calling
  1098. this API with a handle to a regular file will fail.
  1099. The following code fragment illustrates how to open a directory using
  1100. CreateFile.
  1101. hDir = CreateFile(
  1102. DirName,
  1103. FILE_LIST_DIRECTORY,
  1104. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  1105. NULL,
  1106. OPEN_EXISTING,
  1107. FILE_FLAG_BACKUP_SEMANTICS | (fASync ? FILE_FLAG_OVERLAPPED : 0),
  1108. NULL
  1109. );
  1110. This API returns it's data in a structured format. The structure is defined by
  1111. the FILE_NOTIFY_INFORMATION structure.
  1112. typedef struct _FILE_NOTIFY_INFORMATION {
  1113. DWORD NextEntryOffset;
  1114. DWORD Action;
  1115. DWORD FileNameLength;
  1116. WCHAR FileName[1];
  1117. } FILE_NOTIFY_INFORMATION, *PFILE_NOTIFY_INFORMATION;
  1118. The lpBuffer/nBufferLength parameters are used to describe the
  1119. callers buffer to the system. This API fills in the buffer either
  1120. syncronously or asynchronously depending on how the directory is
  1121. opened and the presence of the lpOverlapped parameter.
  1122. Upon successful I/O completion, a formated buffer, and number of
  1123. bytes transfered into the buffer is available to the caller. If the
  1124. number of bytes transfered is 0, this means that the system was
  1125. unable to provide detailed information on all of the changes that
  1126. occured in the directory or tree. The application should manually
  1127. compute this information by enumerating the directory or tree.
  1128. Otherwise, structured data is returned to the caller.
  1129. Each record contains:
  1130. NextEntryOffest - This is the number of bytes to be skipped to get
  1131. to the next record. A value of 0 indicates that this is the last
  1132. record.
  1133. Action - This is used to describe the type of change that occured:
  1134. FILE_ACTION_ADDED - The file was added to the directory
  1135. FILE_ACTION_REMOVED - The file was removed from the
  1136. directory
  1137. FILE_ACTION_MODIFIED - The file was modified (time change,
  1138. attribute change...)
  1139. FILE_ACTION_RENAMED_OLD_NAME - The file was renamed and this
  1140. is the old name.
  1141. FILE_ACTION_RENAMED_NEW_NAME - The file was renamed and this
  1142. is the new name.
  1143. FileNameLength - This is the length in bytes of the file name portion
  1144. of this record. Note that the file name is NOT null terminated. This
  1145. length does not include a trailing NULL.
  1146. FileName - This variable length portion of the recorn contains a file name
  1147. relative to the directory handle. The name is in the UNICODE character
  1148. format and is NOT NULL terminated.
  1149. The caller of this API can specify a filter that describes to sort
  1150. of changes that should trigger a read completion on thie directory.
  1151. The first call to this API on a directory establishes the filter to
  1152. be used for that call and all subsequent calls.
  1153. The caller can also tell the system to watch for changes in the
  1154. directory, or the entire subtree under the directory. Again, the
  1155. first call to this API establishes this condition.
  1156. This call can complete either synchronously or asynchronously.
  1157. For synchronous completion, the directory should be opened without
  1158. the FILE_FLAG_OVERLAPPED flag. The I/O will complete when the
  1159. callers buffer either fills up or overflows. When this condition
  1160. occurs, the caller may parse the returned buffer. If the
  1161. *lpBytesReturned value is 0, this means that the buffer was too
  1162. small to hold all of the changes, and the caller will have to
  1163. manually enumerate the directory or tree.
  1164. For asynchronous completion, the directory should be opened with the
  1165. FILE_FLAG_OVERLAPPED flag, and an lpOverlapped parameter must be
  1166. specified. I/O completion is returned to the caller via
  1167. GetOverlappedResult(), GetQueuedCompletionStatus(), or via an I/O
  1168. completion callback.
  1169. To receive notification via GetOverlappedResult(), DO NOT specify an
  1170. lpCompletionRoutine. Set the hEvent field of the overlapped
  1171. structure to an hEvent unique to this I/O operation. Pick up your I/O completion
  1172. using GetOverlappedResult().
  1173. To receive notification via GetQueuedCompletionSTatus(), DO NOT
  1174. specify an lpCompletionRoutine. Associate the directory handle with
  1175. a completion port using CreateIoCompletionPort(). Pick up your I/O
  1176. completion using GetQueuedCompletionStatus(). To disable a
  1177. completion packet from being used on an associated directory, set
  1178. the low order bit of the hEvent in the lpOverlapped structure and
  1179. use GetOverlappedResult().
  1180. To receive notification via an I/O completion callback, DO NOT
  1181. associate the directory with a completion port. Specify an
  1182. lpCompletionRoutine. This function will be called whenever an
  1183. outstanding I/O completes while you are in an alertable wait. If an
  1184. I/O completes, but you are not waiting, the I/O notification stays
  1185. pending and will occur when you wait. Only the thread that issues
  1186. the I/O is notified. The hEvent field of the overlapped structure is not
  1187. used by the system and may be used by the caller.
  1188. Arguments:
  1189. hDirectory - SUpplies an open handle to a directory to be watched.
  1190. The directory must be opened with FILE_LIST_DIRECTORY access.
  1191. lpBuffer - Supplies the address of a buffer that will be used to return the
  1192. results of the read. The format of this buffer is described above.
  1193. nBufferLength - Supplies the length of the buffer.
  1194. bWatchSubtree - Supplies a boolean value that if TRUE causes the
  1195. system to monitor the directory tree rooted at the specified
  1196. directory. A value of FALSE causes the system to monitor only
  1197. the specified directory.
  1198. dwNotifyFilter - Supplies a set of flags that specify the filter
  1199. conditions the system uses to satisfy a read.
  1200. FILE_NOTIFY_CHANGE_FILENAME - Any file name changes that occur
  1201. in a directory or subtree being watched will satisfy a read.
  1202. This includes renames, creations, and deletes.
  1203. FILE_NOTIFY_CHANGE_DIRNAME - Any directory name changes that
  1204. occur in a directory or subtree being watched will satisfy a
  1205. read. This includes directory creations and deletions.
  1206. FILE_NOTIFY_CHANGE_ATTRIBUTES - Any attribute changes that occur
  1207. in a directory or subtree being watched will satisfy a
  1208. read.
  1209. FILE_NOTIFY_CHANGE_SIZE - Any file size changes that occur in a
  1210. directory or subtree being watched will satisfy a read.
  1211. File sizes only cause a change when the on disk structure is
  1212. updated. For systems with extensive caching this may only
  1213. occur when the system cache is sufficiently flushed.
  1214. FILE_NOTIFY_CHANGE_LAST_WRITE - Any last write time changes that
  1215. occur in a directory or subtree being watched will satisfy a
  1216. read. Last write time change only cause a change when the
  1217. on disk structure is updated. For systems with extensive
  1218. caching this may only occur when the system cache is
  1219. sufficiently flushed.
  1220. FILE_NOTIFY_CHANGE_LAST_ACCESS - Any last access time changes that
  1221. occur in a directory or subtree being watched will satisfy a
  1222. read. Last access time change only cause a change when the
  1223. on disk structure is updated. For systems with extensive
  1224. caching this may only occur when the system cache is
  1225. sufficiently flushed.
  1226. FILE_NOTIFY_CHANGE_CREATION - Any creation time changes that
  1227. occur in a directory or subtree being watched will satisfy a
  1228. read. Last creation time change only cause a change when the
  1229. on disk structure is updated. For systems with extensive
  1230. caching this may only occur when the system cache is
  1231. sufficiently flushed.
  1232. FILE_NOTIFY_CHANGE_SECURITY - Any security descriptor changes
  1233. that occur in a directory or subtree being watched will
  1234. satisfy a read.
  1235. lpBytesReturned - For synchronous calls, this returns the number of
  1236. bytes transfered into the buffer. A successful call coupled
  1237. with a value of 0 means that the buffer was too small, and the
  1238. caller must manually enumerate the directory/tree. For
  1239. asynchronous calls, this value is undefined. The system does
  1240. not attempt to store anything here. The caller must use an
  1241. asynchronous notification technique to pick up I/O completion
  1242. and number of bytes transfered.
  1243. lpOverlapped - Supplies an overlapped structure to be used in
  1244. conjunction with asynchronous I/O completion notification. The
  1245. offset fields of this structure are not used. Using this on a
  1246. directory that was not opened with FILE_FLAG_OVERLAPPED is
  1247. undefined.
  1248. lpCompletionRoutine - Supplies the address of a completion routine
  1249. that is called when this I/O completes, AND the thread that
  1250. issues the I/O enters an alertable wait. The threads wait will
  1251. be interrupted with a return code of WAIT_IO_COMPLETION, and
  1252. this I/O completion routine will be called. The routine is
  1253. passed the error code of the operation, the number of bytes
  1254. transfered, and the address of the lpOverlapped structure used
  1255. in the call. An error will occur if this parameter is specified
  1256. on a directory handle that is associated with a completion port.
  1257. Return Value:
  1258. TRUE - For synchronous calls, the operation succeeded.
  1259. lpBytesReturned is the number of bytes transferred into your
  1260. buffer. A value of 0 means that your buffer was too small to
  1261. hold all of the changes that occured and that you need to
  1262. enumerate the directory yourself to see the changes. For
  1263. asyncronous calls, the operation was queued successfully.
  1264. Results will be delivered using asynch I/O notification
  1265. (GetOverlappedResult(), GetQueuedCompletionStatus(), or your
  1266. completion callback routine).
  1267. FALSE - An error occured. GetLastError() can be used to obtain detailed
  1268. error status.
  1269. --*/
  1270. {
  1271. NTSTATUS Status;
  1272. BOOL ReturnValue;
  1273. IO_STATUS_BLOCK IoStatusBlock;
  1274. HANDLE Event;
  1275. PIO_APC_ROUTINE ApcRoutine = NULL;
  1276. PVOID ApcContext = NULL;
  1277. PBASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK ActivationBlock = NULL;
  1278. ReturnValue = TRUE;
  1279. if ( ARGUMENT_PRESENT(lpOverlapped) ) {
  1280. if ( ARGUMENT_PRESENT(lpCompletionRoutine) ) {
  1281. //
  1282. // completion is via APC routine
  1283. //
  1284. Event = NULL;
  1285. Status = BasepAllocateActivationContextActivationBlock(
  1286. BASEP_ALLOCATE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK_FLAG_DO_NOT_FREE_AFTER_CALLBACK |
  1287. BASEP_ALLOCATE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK_FLAG_DO_NOT_ALLOCATE_IF_PROCESS_DEFAULT,
  1288. lpCompletionRoutine,
  1289. lpOverlapped,
  1290. &ActivationBlock);
  1291. if (!NT_SUCCESS(Status)) {
  1292. BaseSetLastNTError(Status);
  1293. return FALSE;
  1294. }
  1295. if (ActivationBlock != NULL) {
  1296. ApcRoutine = &BasepIoCompletion;
  1297. ApcContext = (PVOID) ActivationBlock;
  1298. } else {
  1299. ApcRoutine = &BasepIoCompletionSimple;
  1300. ApcContext = lpCompletionRoutine;
  1301. }
  1302. } else {
  1303. //
  1304. // completion is via completion port or get overlapped result
  1305. //
  1306. Event = lpOverlapped->hEvent;
  1307. ApcRoutine = NULL;
  1308. ApcContext = (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped;
  1309. }
  1310. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  1311. Status = NtNotifyChangeDirectoryFile(
  1312. hDirectory,
  1313. Event,
  1314. ApcRoutine,
  1315. ApcContext,
  1316. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1317. lpBuffer,
  1318. nBufferLength,
  1319. dwNotifyFilter,
  1320. (BOOLEAN)bWatchSubtree
  1321. );
  1322. //
  1323. // Anything other than an error means that I/O completion will
  1324. // occur and caller only gets return data via completion mechanism
  1325. //
  1326. if ( NT_ERROR(Status) ) {
  1327. if (ActivationBlock != NULL)
  1328. BasepFreeActivationContextActivationBlock(ActivationBlock);
  1329. BaseSetLastNTError(Status);
  1330. ReturnValue = FALSE;
  1331. }
  1332. }
  1333. else {
  1334. Status = NtNotifyChangeDirectoryFile(
  1335. hDirectory,
  1336. NULL,
  1337. NULL,
  1338. NULL,
  1339. &IoStatusBlock,
  1340. lpBuffer,
  1341. nBufferLength,
  1342. dwNotifyFilter,
  1343. (BOOLEAN)bWatchSubtree
  1344. );
  1345. if ( Status == STATUS_PENDING) {
  1346. //
  1347. // Operation must complete before return & IoStatusBlock destroyed
  1348. //
  1349. Status = NtWaitForSingleObject( hDirectory, FALSE, NULL );
  1350. if ( NT_SUCCESS(Status)) {
  1351. Status = IoStatusBlock.Status;
  1352. }
  1353. }
  1354. if ( NT_SUCCESS(Status) ) {
  1355. *lpBytesReturned = (DWORD)IoStatusBlock.Information;
  1356. }
  1357. else {
  1358. BaseSetLastNTError(Status);
  1359. ReturnValue = FALSE;
  1360. }
  1361. }
  1362. return ReturnValue;
  1363. }
  1364. HANDLE
  1365. WINAPI
  1366. FindFirstStreamW(
  1367. LPCWSTR lpFileName,
  1368. STREAM_INFO_LEVELS InfoLevel,
  1369. LPVOID lpFindStreamData,
  1370. DWORD dwFlags
  1371. )
  1372. /*++
  1373. Routine Description:
  1374. This routine starts the enumeration of a file for substreams. All files contain
  1375. a default data stream. On ntfs they can also contain named data streams. Note:
  1376. for fat we'll return unimplemented.
  1377. We'll return a standard FINDFILE_HANDLE with the real data embedded in the back of it.
  1378. Arguments:
  1379. lpFileName - Supplies the file name of the file to enumerate streams in
  1380. InfoLevel - Currently only FindStreamInfoStandard is supported
  1381. lpFindStreamData - Buffer than returns back info the first stream dependent on the infolevel
  1382. for FindStreamInfoStandard
  1383. typedef struct _WIN32_FIND_STREAM_DATA {
  1384. LARGE_INTEGER StreamSize; <--- the stream size
  1385. WCHAR cStreamName[MAX_PATH + 36]; <--- stream name - there is enough space here
  1386. for 2 colons an attribute type name and a stream name and a null
  1387. } WIN32_FIND_STREAM_DATA, *PWIN32_FIND_STREAM_DATA;
  1388. dwFlags - Reserved for future use must be 0 for now
  1389. Return Value:
  1390. Not -1 - Returns a find first handle
  1391. that can be used in a subsequent call to FindNextFile or FindClose.
  1392. 0xffffffff - The operation failed. Extended error status is available
  1393. using GetLastError.
  1394. --*/
  1395. {
  1396. NTSTATUS Status;
  1397. IO_STATUS_BLOCK Iosb;
  1398. BYTE * Buffer = NULL;
  1399. INT BufferSize = sizeof( FILE_STREAM_INFORMATION ); // arbitrary starting point
  1400. INT Index = 0;
  1401. PFILE_STREAM_INFORMATION StreamInfo;
  1402. HANDLE File = NULL;
  1403. OBJECT_ATTRIBUTES Oa;
  1404. UNICODE_STRING FileName;
  1405. PFINDFILE_HANDLE FindFileHandle = NULL;
  1406. PWIN32_FIND_STREAM_DATA FindStreamData = (PWIN32_FIND_STREAM_DATA)lpFindStreamData;
  1407. //
  1408. // Validate the parameters
  1409. //
  1410. if (InfoLevel != FindStreamInfoStandard) {
  1411. SetLastError( ERROR_INVALID_PARAMETER );
  1412. return INVALID_HANDLE_VALUE;
  1413. }
  1414. //
  1415. // Open the file specified
  1416. //
  1417. RtlDosPathNameToNtPathName_U( lpFileName, &FileName, NULL, NULL );
  1418. InitializeObjectAttributes( &Oa, &FileName, OBJ_CASE_INSENSITIVE, NULL, NULL );
  1419. try {
  1420. Status = NtCreateFile( &File,
  1421. FILE_READ_ATTRIBUTES | SYNCHRONIZE,
  1422. &Oa,
  1423. &Iosb,
  1424. NULL,
  1425. FILE_ATTRIBUTE_NORMAL,
  1426. FILE_SHARE_READ | FILE_SHARE_WRITE,
  1427. FILE_OPEN,
  1428. FILE_SYNCHRONOUS_IO_NONALERT | FILE_OPEN_REPARSE_POINT,
  1429. NULL,
  1430. 0 );
  1431. if (STATUS_SUCCESS != Status) {
  1432. leave;
  1433. }
  1434. FindFileHandle = BasepInitializeFindFileHandle( File );
  1435. if (!FindFileHandle) {
  1436. Status = STATUS_INSUFFICIENT_RESOURCES;
  1437. leave;
  1438. }
  1439. //
  1440. // The file handle now belongs the the filefind handle
  1441. //
  1442. File = NULL;
  1443. //
  1444. // Figure out how large to really make the buffer
  1445. //
  1446. do {
  1447. if (Buffer) {
  1448. RtlFreeHeap( RtlProcessHeap(), 0, Buffer );
  1449. }
  1450. Buffer = RtlAllocateHeap( RtlProcessHeap(), 0, BufferSize );
  1451. if (!Buffer) {
  1452. Status = STATUS_INSUFFICIENT_RESOURCES;
  1453. leave;
  1454. }
  1455. Status = NtQueryInformationFile( FindFileHandle->DirectoryHandle, &Iosb, Buffer, BufferSize, FileStreamInformation );
  1456. BufferSize *=2;
  1457. } while (STATUS_BUFFER_OVERFLOW == Status);
  1458. if (STATUS_SUCCESS != Status) {
  1459. leave;
  1460. }
  1461. //
  1462. // Check if there are no results
  1463. //
  1464. if (Iosb.Information == 0) {
  1465. Status = STATUS_END_OF_FILE;
  1466. leave;
  1467. }
  1468. //
  1469. // Setup the filefind handle we're going to return and null out the buffer
  1470. // which we've given ownership to the handle for
  1471. //
  1472. FindFileHandle->FindBufferBase = Buffer;
  1473. FindFileHandle->FindBufferLength = (ULONG)Iosb.Information;
  1474. FindFileHandle->FindBufferValidLength = (ULONG)Iosb.Information;
  1475. Buffer = NULL;
  1476. //
  1477. // Transfer the first result into the output buffer
  1478. // Note if there is not enough space in the cStreamName field we'll just AV
  1479. // the string size is bounded by max attribute name (255) + 2 colons + 36 chars for the type and a null == 291
  1480. //
  1481. //
  1482. StreamInfo = (PFILE_STREAM_INFORMATION)FindFileHandle->FindBufferBase;
  1483. FindStreamData->StreamSize.QuadPart = StreamInfo->StreamSize.QuadPart;
  1484. RtlCopyMemory( FindStreamData->cStreamName, StreamInfo->StreamName, StreamInfo->StreamNameLength );
  1485. FindStreamData->cStreamName[StreamInfo->StreamNameLength / sizeof( WCHAR )] = L'\0';
  1486. if (StreamInfo->NextEntryOffset > 0) {
  1487. FindFileHandle->FindBufferNext = (PCHAR)FindFileHandle->FindBufferBase + StreamInfo->NextEntryOffset;
  1488. } else {
  1489. FindFileHandle->FindBufferNext = (PCHAR)FindFileHandle->FindBufferBase + Iosb.Information;
  1490. }
  1491. } finally {
  1492. //
  1493. // Always cleanup the allocated nt name. On failure also cleanup handles
  1494. // and any allocations
  1495. //
  1496. if (FileName.Length) {
  1497. RtlFreeHeap( RtlProcessHeap(), 0, FileName.Buffer );
  1498. }
  1499. if (STATUS_SUCCESS != Status) {
  1500. BaseSetLastNTError( Status );
  1501. if (File != NULL) {
  1502. NtClose( File );
  1503. }
  1504. if (Buffer) {
  1505. RtlFreeHeap( RtlProcessHeap(), 0, Buffer );
  1506. }
  1507. if (FindFileHandle) {
  1508. FindClose( FindFileHandle );
  1509. }
  1510. FindFileHandle = (PFINDFILE_HANDLE)INVALID_HANDLE_VALUE;
  1511. }
  1512. }
  1513. return FindFileHandle;
  1514. }
  1515. BOOL
  1516. APIENTRY
  1517. FindNextStreamW(
  1518. HANDLE hFindStream,
  1519. LPVOID lpFindStreamData
  1520. )
  1521. /*++
  1522. Routine Description:
  1523. Once a successful call has been made to FindFirstStream, subsequent
  1524. matching files can be located using FindNextStream.
  1525. This API is used to continue a file search from a previous call to
  1526. FindFirstStream. This API returns successfully with the next stream
  1527. If no file match can be found HANDLE_EOF is returned.
  1528. Arguments:
  1529. hFindStream - handle obtained previously from FindFirstStreamW
  1530. lpFindStreamData - appropriate buffer to contain next stream data
  1531. Return Value:
  1532. TRUE if more to enumerate
  1533. --*/
  1534. {
  1535. PFINDFILE_HANDLE FindFileHandle = (PFINDFILE_HANDLE)hFindStream;
  1536. PWIN32_FIND_STREAM_DATA FindStreamData = (PWIN32_FIND_STREAM_DATA)lpFindStreamData;
  1537. PFILE_STREAM_INFORMATION StreamInfo;
  1538. //
  1539. // Check to see if there is anymore data to return
  1540. //
  1541. if (FindFileHandle->FindBufferNext == (PCHAR)FindFileHandle->FindBufferBase + FindFileHandle->FindBufferLength) {
  1542. BaseSetLastNTError( STATUS_END_OF_FILE );
  1543. return FALSE;
  1544. }
  1545. //
  1546. // Transfer the next result into the output buffer
  1547. // Note if there is not enough space in the cStreamName field we'll just AV
  1548. // the string size is bounded by max attribute name (255) + 2 colons + 32 chars for the type == 292
  1549. //
  1550. //
  1551. StreamInfo = (PFILE_STREAM_INFORMATION)FindFileHandle->FindBufferNext;
  1552. FindStreamData->StreamSize.QuadPart = StreamInfo->StreamSize.QuadPart;
  1553. RtlCopyMemory( FindStreamData->cStreamName, StreamInfo->StreamName, StreamInfo->StreamNameLength );
  1554. FindStreamData->cStreamName[StreamInfo->StreamNameLength / sizeof( WCHAR )] = L'\0';
  1555. if (StreamInfo->NextEntryOffset > 0) {
  1556. FindFileHandle->FindBufferNext = (PCHAR)FindFileHandle->FindBufferNext + StreamInfo->NextEntryOffset;
  1557. } else {
  1558. FindFileHandle->FindBufferNext = (PCHAR)FindFileHandle->FindBufferBase + FindFileHandle->FindBufferLength;
  1559. }
  1560. return TRUE;
  1561. }