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.

3191 lines
93 KiB

  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. filehops.c
  5. Abstract:
  6. This module implements Win32 file handle APIs
  7. Author:
  8. Mark Lucovsky (markl) 25-Sep-1990
  9. Revision History:
  10. --*/
  11. #include "basedll.h"
  12. HANDLE
  13. WINAPI
  14. GetStdHandle(
  15. DWORD nStdHandle
  16. )
  17. {
  18. PPEB Peb;
  19. HANDLE rv;
  20. Peb = NtCurrentPeb();
  21. switch( nStdHandle ) {
  22. case STD_INPUT_HANDLE:
  23. rv = Peb->ProcessParameters->StandardInput;
  24. break;
  25. case STD_OUTPUT_HANDLE:
  26. rv = Peb->ProcessParameters->StandardOutput;
  27. break;
  28. case STD_ERROR_HANDLE:
  29. rv = Peb->ProcessParameters->StandardError;
  30. break;
  31. default:
  32. rv = INVALID_HANDLE_VALUE;
  33. break;
  34. }
  35. if ( rv == INVALID_HANDLE_VALUE ) {
  36. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  37. }
  38. return rv;
  39. }
  40. BOOL
  41. WINAPI
  42. SetStdHandle(
  43. DWORD nStdHandle,
  44. HANDLE hHandle
  45. )
  46. {
  47. PPEB Peb;
  48. Peb = NtCurrentPeb();
  49. switch( nStdHandle ) {
  50. case STD_INPUT_HANDLE:
  51. Peb->ProcessParameters->StandardInput = hHandle;
  52. break;
  53. case STD_OUTPUT_HANDLE:
  54. Peb->ProcessParameters->StandardOutput = hHandle;
  55. break;
  56. case STD_ERROR_HANDLE:
  57. Peb->ProcessParameters->StandardError = hHandle;
  58. break;
  59. default:
  60. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  61. return FALSE;
  62. }
  63. return( TRUE );
  64. }
  65. DWORD
  66. WINAPI
  67. GetFileType(
  68. HANDLE hFile
  69. )
  70. /*++
  71. Routine Description:
  72. GetFileType is used to determine the file type of the specified file.
  73. Arguments:
  74. hFile - Supplies an open handle to a file whose type is to be
  75. determined
  76. Return Value:
  77. FILE_TYPE_UNKNOWN - The type of the specified file is unknown.
  78. FILE_TYPE_DISK - The specified file is a disk file.
  79. FILE_TYPE_CHAR - The specified file is a character file (LPT,
  80. console...)
  81. FILE_TYPE_PIPE - The specified file is a pipe (either a named pipe or
  82. a pipe created by CreatePipe).
  83. --*/
  84. {
  85. NTSTATUS Status;
  86. FILE_FS_DEVICE_INFORMATION DeviceInformation;
  87. IO_STATUS_BLOCK IoStatusBlock;
  88. PPEB Peb;
  89. Peb = NtCurrentPeb();
  90. switch( HandleToUlong(hFile) ) {
  91. case STD_INPUT_HANDLE:
  92. hFile = Peb->ProcessParameters->StandardInput;
  93. break;
  94. case STD_OUTPUT_HANDLE:
  95. hFile = Peb->ProcessParameters->StandardOutput;
  96. break;
  97. case STD_ERROR_HANDLE:
  98. hFile = Peb->ProcessParameters->StandardError;
  99. break;
  100. }
  101. if (CONSOLE_HANDLE(hFile) && VerifyConsoleIoHandle(hFile)) {
  102. return( FILE_TYPE_CHAR );
  103. }
  104. if (hFile == NULL) {
  105. BaseSetLastNTError( STATUS_INVALID_HANDLE );
  106. return( FILE_TYPE_UNKNOWN );
  107. }
  108. //
  109. // If handle cannot be a real kernel handle we will fail
  110. // the call instead of calling with a bogus value NtQuery.
  111. //
  112. if (((ULONG_PTR)hFile & 0x01)) {
  113. BaseSetLastNTError( STATUS_INVALID_HANDLE );
  114. return( FILE_TYPE_UNKNOWN );
  115. }
  116. Status = NtQueryVolumeInformationFile( hFile,
  117. &IoStatusBlock,
  118. &DeviceInformation,
  119. sizeof( DeviceInformation ),
  120. FileFsDeviceInformation
  121. );
  122. if (!NT_SUCCESS( Status )) {
  123. BaseSetLastNTError( Status );
  124. return( FILE_TYPE_UNKNOWN );
  125. }
  126. switch( DeviceInformation.DeviceType ) {
  127. case FILE_DEVICE_SCREEN:
  128. case FILE_DEVICE_KEYBOARD:
  129. case FILE_DEVICE_MOUSE:
  130. case FILE_DEVICE_PARALLEL_PORT:
  131. case FILE_DEVICE_PRINTER:
  132. case FILE_DEVICE_SERIAL_PORT:
  133. case FILE_DEVICE_MODEM:
  134. case FILE_DEVICE_SOUND:
  135. case FILE_DEVICE_NULL:
  136. return( FILE_TYPE_CHAR );
  137. case FILE_DEVICE_CD_ROM:
  138. case FILE_DEVICE_CD_ROM_FILE_SYSTEM:
  139. case FILE_DEVICE_CONTROLLER:
  140. case FILE_DEVICE_DATALINK:
  141. case FILE_DEVICE_DFS:
  142. case FILE_DEVICE_DISK:
  143. case FILE_DEVICE_DISK_FILE_SYSTEM:
  144. case FILE_DEVICE_VIRTUAL_DISK:
  145. return( FILE_TYPE_DISK );
  146. case FILE_DEVICE_NAMED_PIPE:
  147. return( FILE_TYPE_PIPE );
  148. case FILE_DEVICE_NETWORK:
  149. case FILE_DEVICE_NETWORK_FILE_SYSTEM:
  150. case FILE_DEVICE_PHYSICAL_NETCARD:
  151. case FILE_DEVICE_TAPE:
  152. case FILE_DEVICE_TAPE_FILE_SYSTEM:
  153. case FILE_DEVICE_TRANSPORT:
  154. // FIX, FIX - how to handle tapes, network devices, etc.
  155. case FILE_DEVICE_UNKNOWN:
  156. default:
  157. SetLastError( NO_ERROR );
  158. return( FILE_TYPE_UNKNOWN );
  159. }
  160. }
  161. BOOL
  162. WINAPI
  163. ReadFile(
  164. HANDLE hFile,
  165. LPVOID lpBuffer,
  166. DWORD nNumberOfBytesToRead,
  167. LPDWORD lpNumberOfBytesRead,
  168. LPOVERLAPPED lpOverlapped
  169. )
  170. /*++
  171. Routine Description:
  172. Data can be read from a file using ReadFile.
  173. This API is used to read data from a file. Data is read from the
  174. file from the position indicated by the file pointer. After the
  175. read completes, the file pointer is adjusted by the number of bytes
  176. actually read. A return value of TRUE coupled with a bytes read of
  177. 0 indicates that the file pointer was beyond the current end of the
  178. file at the time of the read.
  179. Arguments:
  180. hFile - Supplies an open handle to a file that is to be read. The
  181. file handle must have been created with GENERIC_READ access to
  182. the file.
  183. lpBuffer - Supplies the address of a buffer to receive the data read
  184. from the file.
  185. nNumberOfBytesToRead - Supplies the number of bytes to read from the
  186. file.
  187. lpNumberOfBytesRead - Returns the number of bytes read by this call.
  188. This parameter is always set to 0 before doing any IO or error
  189. checking.
  190. lpOverlapped - Optionally points to an OVERLAPPED structure to be used with the
  191. request. If NULL then the transfer starts at the current file position
  192. and ReadFile will not return until the operation completes.
  193. If the handle hFile was created without specifying FILE_FLAG_OVERLAPPED
  194. the file pointer is moved to the specified offset plus
  195. lpNumberOfBytesRead before ReadFile returns. ReadFile will wait for the
  196. request to complete before returning (it will not return
  197. ERROR_IO_PENDING).
  198. When FILE_FLAG_OVERLAPPED is specified, ReadFile may return
  199. ERROR_IO_PENDING to allow the calling function to continue processing
  200. while the operation completes. The event (or hFile if hEvent is NULL) will
  201. be set to the signalled state upon completion of the request.
  202. When the handle is created with FILE_FLAG_OVERLAPPED and lpOverlapped
  203. is set to NULL, ReadFile will return ERROR_INVALID_PARAMTER because
  204. the file offset is required.
  205. Return Value:
  206. TRUE - The operation was successul.
  207. FALSE - The operation failed. Extended error status is available
  208. using GetLastError.
  209. --*/
  210. {
  211. NTSTATUS Status;
  212. IO_STATUS_BLOCK IoStatusBlock;
  213. PPEB Peb;
  214. DWORD InputMode;
  215. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  216. *lpNumberOfBytesRead = 0;
  217. }
  218. Peb = NtCurrentPeb();
  219. switch( HandleToUlong(hFile) ) {
  220. case STD_INPUT_HANDLE: hFile = Peb->ProcessParameters->StandardInput;
  221. break;
  222. case STD_OUTPUT_HANDLE: hFile = Peb->ProcessParameters->StandardOutput;
  223. break;
  224. case STD_ERROR_HANDLE: hFile = Peb->ProcessParameters->StandardError;
  225. break;
  226. }
  227. if (CONSOLE_HANDLE(hFile)) {
  228. if (ReadConsoleA(hFile,
  229. lpBuffer,
  230. nNumberOfBytesToRead,
  231. lpNumberOfBytesRead,
  232. lpOverlapped
  233. )
  234. ) {
  235. Status = STATUS_SUCCESS;
  236. if (!GetConsoleMode( hFile, &InputMode )) {
  237. InputMode = 0;
  238. }
  239. if (InputMode & ENABLE_PROCESSED_INPUT) {
  240. try {
  241. if (*(PCHAR)lpBuffer == 0x1A) {
  242. *lpNumberOfBytesRead = 0;
  243. }
  244. }
  245. except( EXCEPTION_EXECUTE_HANDLER ) {
  246. Status = GetExceptionCode();
  247. }
  248. }
  249. if (NT_SUCCESS(Status)) {
  250. return TRUE;
  251. }
  252. else {
  253. BaseSetLastNTError(Status);
  254. return FALSE;
  255. }
  256. }
  257. else {
  258. return FALSE;
  259. }
  260. }
  261. if ( ARGUMENT_PRESENT( lpOverlapped ) ) {
  262. LARGE_INTEGER Li;
  263. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  264. Li.LowPart = lpOverlapped->Offset;
  265. Li.HighPart = lpOverlapped->OffsetHigh;
  266. Status = NtReadFile(
  267. hFile,
  268. lpOverlapped->hEvent,
  269. NULL,
  270. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  271. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  272. lpBuffer,
  273. nNumberOfBytesToRead,
  274. &Li,
  275. NULL
  276. );
  277. if ( NT_SUCCESS(Status) && Status != STATUS_PENDING) {
  278. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  279. try {
  280. *lpNumberOfBytesRead = (DWORD)lpOverlapped->InternalHigh;
  281. }
  282. except(EXCEPTION_EXECUTE_HANDLER) {
  283. *lpNumberOfBytesRead = 0;
  284. }
  285. }
  286. return TRUE;
  287. }
  288. else
  289. if (Status == STATUS_END_OF_FILE) {
  290. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  291. *lpNumberOfBytesRead = 0;
  292. }
  293. BaseSetLastNTError(Status);
  294. return FALSE;
  295. }
  296. else {
  297. BaseSetLastNTError(Status);
  298. return FALSE;
  299. }
  300. }
  301. else
  302. {
  303. Status = NtReadFile(
  304. hFile,
  305. NULL,
  306. NULL,
  307. NULL,
  308. &IoStatusBlock,
  309. lpBuffer,
  310. nNumberOfBytesToRead,
  311. NULL,
  312. NULL
  313. );
  314. if ( Status == STATUS_PENDING) {
  315. // Operation must complete before return & IoStatusBlock destroyed
  316. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  317. if ( NT_SUCCESS(Status)) {
  318. Status = IoStatusBlock.Status;
  319. }
  320. }
  321. if ( NT_SUCCESS(Status) ) {
  322. *lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;
  323. return TRUE;
  324. }
  325. else
  326. if (Status == STATUS_END_OF_FILE) {
  327. *lpNumberOfBytesRead = 0;
  328. return TRUE;
  329. }
  330. else {
  331. if ( NT_WARNING(Status) ) {
  332. *lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;
  333. }
  334. BaseSetLastNTError(Status);
  335. return FALSE;
  336. }
  337. }
  338. }
  339. BOOL
  340. WINAPI
  341. WriteFile(
  342. HANDLE hFile,
  343. LPCVOID lpBuffer,
  344. DWORD nNumberOfBytesToWrite,
  345. LPDWORD lpNumberOfBytesWritten,
  346. LPOVERLAPPED lpOverlapped
  347. )
  348. /*++
  349. Routine Description:
  350. Data can be written to a file using WriteFile.
  351. This API is used to write data to a file. Data is written to the
  352. file from the position indicated by the file pointer. After the
  353. write completes, the file pointer is adjusted by the number of bytes
  354. actually written.
  355. Unlike DOS, a NumberOfBytesToWrite value of zero does not truncate
  356. or extend the file. If this function is required, SetEndOfFile
  357. should be used.
  358. Arguments:
  359. hFile - Supplies an open handle to a file that is to be written. The
  360. file handle must have been created with GENERIC_WRITE access to
  361. the file.
  362. lpBuffer - Supplies the address of the data that is to be written to
  363. the file.
  364. nNumberOfBytesToWrite - Supplies the number of bytes to write to the
  365. file. Unlike DOS, a value of zero is interpreted a null write.
  366. lpNumberOfBytesWritten - Returns the number of bytes written by this
  367. call. Before doing any work or error processing, the API sets this
  368. to zero.
  369. lpOverlapped - Optionally points to an OVERLAPPED structure to be
  370. used with the request. If NULL then the transfer starts at the
  371. current file position and WriteFile will not return until the
  372. operation completes.
  373. If the handle <hFile> was created without specifying
  374. FILE_FLAG_OVERLAPPED the file pointer is moved to the specified
  375. offset plus lpNumberOfBytesWritten before WriteFile returns.
  376. WriteFile will wait for the request to complete before returning
  377. (it will not set ERROR_IO_PENDING).
  378. When FILE_FLAG_OVERLAPPED is specified, WriteFile may return
  379. ERROR_IO_PENDING to allow the calling function to continue processing
  380. while the operation completes. The event (or hFile if hEvent is NULL) will
  381. be set to the signalled state upon completion of the request.
  382. When the handle is created with FILE_FLAG_OVERLAPPED and lpOverlapped
  383. is set to NULL, WriteFile will return ERROR_INVALID_PARAMTER because
  384. the file offset is required.
  385. Return Value:
  386. TRUE - The operation was a success.
  387. FALSE - The operation failed. Extended error status is
  388. available using GetLastError.
  389. --*/
  390. {
  391. NTSTATUS Status;
  392. IO_STATUS_BLOCK IoStatusBlock;
  393. PPEB Peb;
  394. if ( ARGUMENT_PRESENT(lpNumberOfBytesWritten) ) {
  395. *lpNumberOfBytesWritten = 0;
  396. }
  397. Peb = NtCurrentPeb();
  398. switch( HandleToUlong(hFile) ) {
  399. case STD_INPUT_HANDLE: hFile = Peb->ProcessParameters->StandardInput;
  400. break;
  401. case STD_OUTPUT_HANDLE: hFile = Peb->ProcessParameters->StandardOutput;
  402. break;
  403. case STD_ERROR_HANDLE: hFile = Peb->ProcessParameters->StandardError;
  404. break;
  405. }
  406. if (CONSOLE_HANDLE(hFile)) {
  407. return WriteConsoleA(hFile,
  408. (LPVOID)lpBuffer,
  409. nNumberOfBytesToWrite,
  410. lpNumberOfBytesWritten,
  411. lpOverlapped
  412. );
  413. }
  414. if ( ARGUMENT_PRESENT( lpOverlapped ) ) {
  415. LARGE_INTEGER Li;
  416. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  417. Li.LowPart = lpOverlapped->Offset;
  418. Li.HighPart = lpOverlapped->OffsetHigh;
  419. Status = NtWriteFile(
  420. hFile,
  421. lpOverlapped->hEvent,
  422. NULL,
  423. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  424. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  425. (PVOID)lpBuffer,
  426. nNumberOfBytesToWrite,
  427. &Li,
  428. NULL
  429. );
  430. if ( !NT_ERROR(Status) && Status != STATUS_PENDING) {
  431. if ( ARGUMENT_PRESENT(lpNumberOfBytesWritten) ) {
  432. try {
  433. *lpNumberOfBytesWritten = (DWORD)lpOverlapped->InternalHigh;
  434. }
  435. except(EXCEPTION_EXECUTE_HANDLER) {
  436. *lpNumberOfBytesWritten = 0;
  437. }
  438. }
  439. return TRUE;
  440. }
  441. else {
  442. BaseSetLastNTError(Status);
  443. return FALSE;
  444. }
  445. }
  446. else {
  447. Status = NtWriteFile(
  448. hFile,
  449. NULL,
  450. NULL,
  451. NULL,
  452. &IoStatusBlock,
  453. (PVOID)lpBuffer,
  454. nNumberOfBytesToWrite,
  455. NULL,
  456. NULL
  457. );
  458. if ( Status == STATUS_PENDING) {
  459. // Operation must complete before return & IoStatusBlock destroyed
  460. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  461. if ( NT_SUCCESS(Status)) {
  462. Status = IoStatusBlock.Status;
  463. }
  464. }
  465. if ( NT_SUCCESS(Status)) {
  466. *lpNumberOfBytesWritten = (DWORD)IoStatusBlock.Information;
  467. return TRUE;
  468. }
  469. else {
  470. if ( NT_WARNING(Status) ) {
  471. *lpNumberOfBytesWritten = (DWORD)IoStatusBlock.Information;
  472. }
  473. BaseSetLastNTError(Status);
  474. return FALSE;
  475. }
  476. }
  477. }
  478. BOOL
  479. WINAPI
  480. SetEndOfFile(
  481. HANDLE hFile
  482. )
  483. /*++
  484. Routine Description:
  485. The end of file position of an open file can be set to the current
  486. file pointer using SetEndOfFile.
  487. This API is used to set the end of file position of a file to the
  488. same value as the current file pointer. This has the effect of
  489. truncating or extending a file. This functionality is similar to
  490. DOS (int 21h, function 40H with CX=0).
  491. Arguments:
  492. hFile - Supplies an open handle to a file that is to be extended or
  493. truncated. The file handle must have been created with
  494. GENERIC_WRITE access to the file.
  495. Return Value:
  496. TRUE - The operation was successful.
  497. FALSE/NULL - The operation failed. Extended error status is available
  498. using GetLastError.
  499. --*/
  500. {
  501. NTSTATUS Status;
  502. IO_STATUS_BLOCK IoStatusBlock;
  503. FILE_POSITION_INFORMATION CurrentPosition;
  504. FILE_END_OF_FILE_INFORMATION EndOfFile;
  505. FILE_ALLOCATION_INFORMATION Allocation;
  506. if (CONSOLE_HANDLE(hFile)) {
  507. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  508. return FALSE;
  509. }
  510. //
  511. // Get the current position of the file pointer
  512. //
  513. Status = NtQueryInformationFile(
  514. hFile,
  515. &IoStatusBlock,
  516. &CurrentPosition,
  517. sizeof(CurrentPosition),
  518. FilePositionInformation
  519. );
  520. if ( !NT_SUCCESS(Status) ) {
  521. BaseSetLastNTError(Status);
  522. return FALSE;
  523. }
  524. //
  525. // Set the end of file based on the current file position
  526. //
  527. EndOfFile.EndOfFile = CurrentPosition.CurrentByteOffset;
  528. Status = NtSetInformationFile(
  529. hFile,
  530. &IoStatusBlock,
  531. &EndOfFile,
  532. sizeof(EndOfFile),
  533. FileEndOfFileInformation
  534. );
  535. if ( !NT_SUCCESS(Status) ) {
  536. BaseSetLastNTError(Status);
  537. return FALSE;
  538. }
  539. //
  540. // Set the allocation based on the current file size
  541. //
  542. Allocation.AllocationSize = CurrentPosition.CurrentByteOffset;
  543. Status = NtSetInformationFile(
  544. hFile,
  545. &IoStatusBlock,
  546. &Allocation,
  547. sizeof(Allocation),
  548. FileAllocationInformation
  549. );
  550. if ( NT_SUCCESS(Status) ) {
  551. return TRUE;
  552. }
  553. else {
  554. BaseSetLastNTError(Status);
  555. return FALSE;
  556. }
  557. }
  558. DWORD
  559. WINAPI
  560. SetFilePointer(
  561. HANDLE hFile,
  562. LONG lDistanceToMove,
  563. PLONG lpDistanceToMoveHigh,
  564. DWORD dwMoveMethod
  565. )
  566. /*++
  567. Routine Description:
  568. An open file's file pointer can be set using SetFilePointer.
  569. The purpose of this function is to update the current value of a
  570. file's file pointer. Care should be taken in multi-threaded
  571. applications that have multiple threads sharing a file handle with
  572. each thread updating the file pointer and then doing a read. This
  573. sequence should be treated as a critical section of code and should
  574. be protected using either a critical section object or a mutex
  575. object.
  576. This API provides the same functionality as DOS (int 21h, function
  577. 42h) and OS/2's DosSetFilePtr.
  578. Arguments:
  579. hFile - Supplies an open handle to a file whose file pointer is to be
  580. moved. The file handle must have been created with
  581. GENERIC_READ or GENERIC_WRITE access to the file.
  582. lDistanceToMove - Supplies the number of bytes to move the file
  583. pointer. A positive value moves the pointer forward in the file
  584. and a negative value moves backwards in the file.
  585. lpDistanceToMoveHigh - An optional parameter that if specified
  586. supplies the high order 32-bits of the 64-bit distance to move.
  587. If the value of this parameter is NULL, this API can only
  588. operate on files whose maximum size is (2**32)-2. If this
  589. parameter is specified, than the maximum file size is (2**64)-2.
  590. This value also returns the high order 32-bits of the new value
  591. of the file pointer. If this value, and the return value
  592. are 0xffffffff, then an error is indicated.
  593. dwMoveMethod - Supplies a value that specifies the starting point
  594. for the file pointer move.
  595. FILE_BEGIN - The starting point is zero or the beginning of the
  596. file. If FILE_BEGIN is specified, then DistanceToMove is
  597. interpreted as an unsigned location for the new
  598. file pointer.
  599. FILE_CURRENT - The current value of the file pointer is used as
  600. the starting point.
  601. FILE_END - The current end of file position is used as the
  602. starting point.
  603. Return Value:
  604. Not -1 - Returns the low order 32-bits of the new value of the file
  605. pointer.
  606. 0xffffffff - If the value of lpDistanceToMoveHigh was NULL, then The
  607. operation failed. Extended error status is available using
  608. GetLastError. Otherwise, this is the low order 32-bits of the
  609. new value of the file pointer.
  610. --*/
  611. {
  612. NTSTATUS Status;
  613. IO_STATUS_BLOCK IoStatusBlock;
  614. FILE_POSITION_INFORMATION CurrentPosition;
  615. FILE_STANDARD_INFORMATION StandardInfo;
  616. LARGE_INTEGER Large;
  617. if (CONSOLE_HANDLE(hFile)) {
  618. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  619. return (DWORD)-1;
  620. }
  621. if (ARGUMENT_PRESENT(lpDistanceToMoveHigh)) {
  622. Large.HighPart = *lpDistanceToMoveHigh;
  623. Large.LowPart = lDistanceToMove;
  624. }
  625. else {
  626. Large.QuadPart = lDistanceToMove;
  627. }
  628. switch (dwMoveMethod) {
  629. case FILE_BEGIN :
  630. CurrentPosition.CurrentByteOffset = Large;
  631. break;
  632. case FILE_CURRENT :
  633. //
  634. // Get the current position of the file pointer
  635. //
  636. Status = NtQueryInformationFile(
  637. hFile,
  638. &IoStatusBlock,
  639. &CurrentPosition,
  640. sizeof(CurrentPosition),
  641. FilePositionInformation
  642. );
  643. if ( !NT_SUCCESS(Status) ) {
  644. BaseSetLastNTError(Status);
  645. return (DWORD)-1;
  646. }
  647. CurrentPosition.CurrentByteOffset.QuadPart += Large.QuadPart;
  648. break;
  649. case FILE_END :
  650. Status = NtQueryInformationFile(
  651. hFile,
  652. &IoStatusBlock,
  653. &StandardInfo,
  654. sizeof(StandardInfo),
  655. FileStandardInformation
  656. );
  657. if ( !NT_SUCCESS(Status) ) {
  658. BaseSetLastNTError(Status);
  659. return (DWORD)-1;
  660. }
  661. CurrentPosition.CurrentByteOffset.QuadPart =
  662. StandardInfo.EndOfFile.QuadPart + Large.QuadPart;
  663. break;
  664. default:
  665. SetLastError(ERROR_INVALID_PARAMETER);
  666. return (DWORD)-1;
  667. break;
  668. }
  669. //
  670. // If the resulting file position is negative, or if the app is not
  671. // prepared for greater than
  672. // then 32 bits than fail
  673. //
  674. if ( CurrentPosition.CurrentByteOffset.QuadPart < 0 ) {
  675. SetLastError(ERROR_NEGATIVE_SEEK);
  676. return (DWORD)-1;
  677. }
  678. if ( !ARGUMENT_PRESENT(lpDistanceToMoveHigh) &&
  679. (CurrentPosition.CurrentByteOffset.HighPart & MAXLONG) ) {
  680. SetLastError(ERROR_INVALID_PARAMETER);
  681. return (DWORD)-1;
  682. }
  683. //
  684. // Set the current file position
  685. //
  686. Status = NtSetInformationFile(
  687. hFile,
  688. &IoStatusBlock,
  689. &CurrentPosition,
  690. sizeof(CurrentPosition),
  691. FilePositionInformation
  692. );
  693. if ( NT_SUCCESS(Status) ) {
  694. if (ARGUMENT_PRESENT(lpDistanceToMoveHigh)){
  695. *lpDistanceToMoveHigh = CurrentPosition.CurrentByteOffset.HighPart;
  696. }
  697. if ( CurrentPosition.CurrentByteOffset.LowPart == -1 ) {
  698. SetLastError(0);
  699. }
  700. return CurrentPosition.CurrentByteOffset.LowPart;
  701. }
  702. else {
  703. BaseSetLastNTError(Status);
  704. if (ARGUMENT_PRESENT(lpDistanceToMoveHigh)){
  705. *lpDistanceToMoveHigh = -1;
  706. }
  707. return (DWORD)-1;
  708. }
  709. }
  710. BOOL
  711. WINAPI
  712. SetFilePointerEx(
  713. HANDLE hFile,
  714. LARGE_INTEGER liDistanceToMove,
  715. PLARGE_INTEGER lpNewFilePointer,
  716. DWORD dwMoveMethod
  717. )
  718. /*++
  719. Routine Description:
  720. An open file's file pointer can be set using SetFilePointer.
  721. The purpose of this function is to update the current value of a
  722. file's file pointer. Care should be taken in multi-threaded
  723. applications that have multiple threads sharing a file handle with
  724. each thread updating the file pointer and then doing a read. This
  725. sequence should be treated as a critical section of code and should
  726. be protected using either a critical section object or a mutex
  727. object.
  728. This API provides the same functionality as DOS (int 21h, function
  729. 42h) and OS/2's DosSetFilePtr.
  730. Arguments:
  731. hFile - Supplies an open handle to a file whose file pointer is to be
  732. moved. The file handle must have been created with
  733. GENERIC_READ or GENERIC_WRITE access to the file.
  734. liDistanceToMove - Supplies the number of bytes to move the file
  735. pointer. A positive value moves the pointer forward in the file
  736. and a negative value moves backwards in the file.
  737. lpNewFilePointer - An optional parameter that if specified returns
  738. the new file pointer
  739. dwMoveMethod - Supplies a value that specifies the starting point
  740. for the file pointer move.
  741. FILE_BEGIN - The starting point is zero or the beginning of the
  742. file. If FILE_BEGIN is specified, then DistanceToMove is
  743. interpreted as an unsigned location for the new
  744. file pointer.
  745. FILE_CURRENT - The current value of the file pointer is used as
  746. the starting point.
  747. FILE_END - The current end of file position is used as the
  748. starting point.
  749. Return Value:
  750. TRUE - The operation was successful
  751. FALSE - The operation failed. Extended error status is available using
  752. GetLastError.
  753. --*/
  754. {
  755. NTSTATUS Status;
  756. IO_STATUS_BLOCK IoStatusBlock;
  757. FILE_POSITION_INFORMATION CurrentPosition;
  758. FILE_STANDARD_INFORMATION StandardInfo;
  759. LARGE_INTEGER Large;
  760. if (CONSOLE_HANDLE(hFile)) {
  761. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  762. return FALSE;
  763. }
  764. Large = liDistanceToMove;
  765. switch (dwMoveMethod) {
  766. case FILE_BEGIN :
  767. CurrentPosition.CurrentByteOffset = Large;
  768. break;
  769. case FILE_CURRENT :
  770. //
  771. // Get the current position of the file pointer
  772. //
  773. Status = NtQueryInformationFile(
  774. hFile,
  775. &IoStatusBlock,
  776. &CurrentPosition,
  777. sizeof(CurrentPosition),
  778. FilePositionInformation
  779. );
  780. if ( !NT_SUCCESS(Status) ) {
  781. BaseSetLastNTError(Status);
  782. return FALSE;
  783. }
  784. CurrentPosition.CurrentByteOffset.QuadPart += Large.QuadPart;
  785. break;
  786. case FILE_END :
  787. Status = NtQueryInformationFile(
  788. hFile,
  789. &IoStatusBlock,
  790. &StandardInfo,
  791. sizeof(StandardInfo),
  792. FileStandardInformation
  793. );
  794. if ( !NT_SUCCESS(Status) ) {
  795. BaseSetLastNTError(Status);
  796. return FALSE;
  797. }
  798. CurrentPosition.CurrentByteOffset.QuadPart =
  799. StandardInfo.EndOfFile.QuadPart + Large.QuadPart;
  800. break;
  801. default:
  802. SetLastError(ERROR_INVALID_PARAMETER);
  803. return FALSE;
  804. break;
  805. }
  806. //
  807. // If the resulting file position is negative fail
  808. //
  809. if ( CurrentPosition.CurrentByteOffset.QuadPart < 0 ) {
  810. SetLastError(ERROR_NEGATIVE_SEEK);
  811. return FALSE;
  812. }
  813. //
  814. // Set the current file position
  815. //
  816. Status = NtSetInformationFile(
  817. hFile,
  818. &IoStatusBlock,
  819. &CurrentPosition,
  820. sizeof(CurrentPosition),
  821. FilePositionInformation
  822. );
  823. if ( NT_SUCCESS(Status) ) {
  824. if (ARGUMENT_PRESENT(lpNewFilePointer)){
  825. *lpNewFilePointer = CurrentPosition.CurrentByteOffset;
  826. }
  827. return TRUE;
  828. }
  829. else {
  830. BaseSetLastNTError(Status);
  831. return FALSE;
  832. }
  833. }
  834. BOOL
  835. WINAPI
  836. GetFileInformationByHandle(
  837. HANDLE hFile,
  838. LPBY_HANDLE_FILE_INFORMATION lpFileInformation
  839. )
  840. /*++
  841. Routine Description:
  842. Arguments:
  843. hFile - Supplies an open handle to a file whose modification date and
  844. times are to be read. The file handle must have been created with
  845. GENERIC_READ access to the file.
  846. lpCreationTime - An optional parameter that if specified points to
  847. the location to return the date and time the file was created.
  848. A returned time of all zero indicates that the file system
  849. containing the file does not support this time value.
  850. Return Value:
  851. TRUE - The operation was successful.
  852. FALSE/NULL - The operation failed. Extended error status is available
  853. using GetLastError.
  854. --*/
  855. {
  856. NTSTATUS Status;
  857. IO_STATUS_BLOCK IoStatusBlock;
  858. BY_HANDLE_FILE_INFORMATION LocalFileInformation;
  859. FILE_ALL_INFORMATION FileInformation;
  860. FILE_FS_VOLUME_INFORMATION VolumeInfo;
  861. if (CONSOLE_HANDLE(hFile)) {
  862. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  863. return FALSE;
  864. }
  865. Status = NtQueryVolumeInformationFile(
  866. hFile,
  867. &IoStatusBlock,
  868. &VolumeInfo,
  869. sizeof(VolumeInfo),
  870. FileFsVolumeInformation
  871. );
  872. if ( !NT_ERROR(Status) ) {
  873. LocalFileInformation.dwVolumeSerialNumber = VolumeInfo.VolumeSerialNumber;
  874. }
  875. else {
  876. BaseSetLastNTError(Status);
  877. return FALSE;
  878. }
  879. Status = NtQueryInformationFile(
  880. hFile,
  881. &IoStatusBlock,
  882. &FileInformation,
  883. sizeof(FileInformation),
  884. FileAllInformation
  885. );
  886. //
  887. // we really plan for buffer overflow
  888. //
  889. if ( !NT_ERROR(Status) ) {
  890. LocalFileInformation.dwFileAttributes = FileInformation.BasicInformation.FileAttributes;
  891. LocalFileInformation.ftCreationTime = *(LPFILETIME)&FileInformation.BasicInformation.CreationTime;
  892. LocalFileInformation.ftLastAccessTime = *(LPFILETIME)&FileInformation.BasicInformation.LastAccessTime;
  893. LocalFileInformation.ftLastWriteTime = *(LPFILETIME)&FileInformation.BasicInformation.LastWriteTime;
  894. LocalFileInformation.nFileSizeHigh = FileInformation.StandardInformation.EndOfFile.HighPart;
  895. LocalFileInformation.nFileSizeLow = FileInformation.StandardInformation.EndOfFile.LowPart;
  896. LocalFileInformation.nNumberOfLinks = FileInformation.StandardInformation.NumberOfLinks;
  897. LocalFileInformation.nFileIndexHigh = FileInformation.InternalInformation.IndexNumber.HighPart;
  898. LocalFileInformation.nFileIndexLow = FileInformation.InternalInformation.IndexNumber.LowPart;
  899. }
  900. else {
  901. BaseSetLastNTError(Status);
  902. return FALSE;
  903. }
  904. *lpFileInformation = LocalFileInformation;
  905. return TRUE;
  906. }
  907. BOOL
  908. APIENTRY
  909. GetFileTime(
  910. HANDLE hFile,
  911. LPFILETIME lpCreationTime,
  912. LPFILETIME lpLastAccessTime,
  913. LPFILETIME lpLastWriteTime
  914. )
  915. /*++
  916. Routine Description:
  917. The date and time that a file was created, last accessed or last
  918. modified can be read using GetFileTime. File time stamps are
  919. returned as 64-bit values, that represent the number of 100
  920. nanoseconds since January 1st, 1601. This date was chosen because
  921. it is the start of a new quadricentury. At 100ns resolution 32 bits
  922. is good for about 429 seconds (or 7 minutes) and a 63-bit integer is
  923. good for about 29,247 years, or around 10,682,247 days.
  924. This API provides the same functionality as DOS (int 21h, function
  925. 47H with AL=0), and provides a subset of OS/2's DosQueryFileInfo.
  926. Arguments:
  927. hFile - Supplies an open handle to a file whose modification date and
  928. times are to be read. The file handle must have been created with
  929. GENERIC_READ access to the file.
  930. lpCreationTime - An optional parameter that if specified points to
  931. the location to return the date and time the file was created.
  932. A returned time of all zero indicates that the file system
  933. containing the file does not support this time value.
  934. lpLastAccessTime - An optional parameter that if specified points to
  935. the location to return the date and time the file was last accessed.
  936. A returned time of all zero indicates that the file system
  937. containing the file does not support this time value.
  938. lpLastWriteTime - An optional parameter that if specified points to
  939. the location to return the date and time the file was last written.
  940. A file system must support this time and thus a valid value will
  941. always be returned for this time value.
  942. Return Value:
  943. TRUE - The operation was successful.
  944. FALSE/NULL - The operation failed. Extended error status is available
  945. using GetLastError.
  946. --*/
  947. {
  948. NTSTATUS Status;
  949. IO_STATUS_BLOCK IoStatusBlock;
  950. FILE_BASIC_INFORMATION BasicInfo;
  951. if (CONSOLE_HANDLE(hFile)) {
  952. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  953. return FALSE;
  954. }
  955. //
  956. // Get the attributes
  957. //
  958. Status = NtQueryInformationFile(
  959. hFile,
  960. &IoStatusBlock,
  961. &BasicInfo,
  962. sizeof(BasicInfo),
  963. FileBasicInformation
  964. );
  965. if ( NT_SUCCESS(Status) ) {
  966. if (ARGUMENT_PRESENT( lpCreationTime )) {
  967. *lpCreationTime = *(LPFILETIME)&BasicInfo.CreationTime;
  968. }
  969. if (ARGUMENT_PRESENT( lpLastAccessTime )) {
  970. *lpLastAccessTime = *(LPFILETIME)&BasicInfo.LastAccessTime;
  971. }
  972. if (ARGUMENT_PRESENT( lpLastWriteTime )) {
  973. *lpLastWriteTime = *(LPFILETIME)&BasicInfo.LastWriteTime;
  974. }
  975. return TRUE;
  976. }
  977. else {
  978. BaseSetLastNTError(Status);
  979. return FALSE;
  980. }
  981. }
  982. BOOL
  983. WINAPI
  984. SetFileTime(
  985. HANDLE hFile,
  986. CONST FILETIME *lpCreationTime,
  987. CONST FILETIME *lpLastAccessTime,
  988. CONST FILETIME *lpLastWriteTime
  989. )
  990. /*++
  991. Routine Description:
  992. The date and time that a file was created, last accessed or last
  993. modified can be modified using SetFileTime. File time stamps are
  994. returned as 64-bit values, that represent the number of 100
  995. nanoseconds since January 1st, 1601. This date was chosen because
  996. it is the start of a new quadricentury. At 100ns resolution 32 bits
  997. is good for about 429 seconds (or 7 minutes) and a 63-bit integer is
  998. good for about 29,247 years, or around 10,682,247 days.
  999. This API provides the same functionality as DOS (int 21h, function
  1000. 47H with AL=1), and provides a subset of OS/2's DosSetFileInfo.
  1001. Arguments:
  1002. hFile - Supplies an open handle to a file whose modification date and
  1003. times are to be written. The file handle must have been created
  1004. with GENERIC_WRITE access to the file.
  1005. lpCreationTime - An optional parameter, that if specified supplies
  1006. the new creation time for the file. Some file system's do not
  1007. support this time value, so this parameter may be ignored.
  1008. lpLastAccessTime - An optional parameter, that if specified supplies
  1009. the new last access time for the file. Some file system's do
  1010. not support this time value, so this parameter may be ignored.
  1011. lpLastWriteTime - An optional parameter, that if specified supplies
  1012. the new last write time for the file. A file system must support
  1013. this time value.
  1014. Return Value:
  1015. TRUE - The operation was successful.
  1016. FALSE/NULL - The operation failed. Extended error status is available
  1017. using GetLastError.
  1018. --*/
  1019. {
  1020. NTSTATUS Status;
  1021. IO_STATUS_BLOCK IoStatusBlock;
  1022. FILE_BASIC_INFORMATION BasicInfo;
  1023. if (CONSOLE_HANDLE(hFile)) {
  1024. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  1025. return FALSE;
  1026. }
  1027. //
  1028. // Zero all the time values we can set.
  1029. //
  1030. RtlZeroMemory(&BasicInfo,sizeof(BasicInfo));
  1031. //
  1032. // For each time value that is specified, copy it to the I/O system
  1033. // record.
  1034. //
  1035. if (ARGUMENT_PRESENT( lpCreationTime )) {
  1036. BasicInfo.CreationTime.LowPart = lpCreationTime->dwLowDateTime;
  1037. BasicInfo.CreationTime.HighPart = lpCreationTime->dwHighDateTime;
  1038. }
  1039. if (ARGUMENT_PRESENT( lpLastAccessTime )) {
  1040. BasicInfo.LastAccessTime.LowPart = lpLastAccessTime->dwLowDateTime;
  1041. BasicInfo.LastAccessTime.HighPart = lpLastAccessTime->dwHighDateTime;
  1042. }
  1043. if (ARGUMENT_PRESENT( lpLastWriteTime )) {
  1044. BasicInfo.LastWriteTime.LowPart = lpLastWriteTime->dwLowDateTime;
  1045. BasicInfo.LastWriteTime.HighPart = lpLastWriteTime->dwHighDateTime;
  1046. }
  1047. //
  1048. // Set the requested times.
  1049. //
  1050. Status = NtSetInformationFile(
  1051. hFile,
  1052. &IoStatusBlock,
  1053. &BasicInfo,
  1054. sizeof(BasicInfo),
  1055. FileBasicInformation
  1056. );
  1057. if ( NT_SUCCESS(Status) ) {
  1058. return TRUE;
  1059. }
  1060. else {
  1061. BaseSetLastNTError(Status);
  1062. return FALSE;
  1063. }
  1064. }
  1065. BOOL
  1066. WINAPI
  1067. FlushFileBuffers(
  1068. HANDLE hFile
  1069. )
  1070. /*++
  1071. Routine Description:
  1072. Buffered data may be flushed out to the file using the
  1073. FlushFileBuffers service.
  1074. The FlushFileBuffers service causes all buffered data to be written
  1075. to the specified file.
  1076. Arguments:
  1077. hFile - Supplies an open handle to a file whose buffers are to be
  1078. flushed. The file handle must have been created with
  1079. GENERIC_WRITE access to the file.
  1080. Return Value:
  1081. TRUE - The operation was successful.
  1082. FALSE/NULL - The operation failed. Extended error status is available
  1083. using GetLastError.
  1084. --*/
  1085. {
  1086. NTSTATUS Status;
  1087. IO_STATUS_BLOCK IoStatusBlock;
  1088. PPEB Peb;
  1089. Peb = NtCurrentPeb();
  1090. switch( HandleToUlong(hFile) ) {
  1091. case STD_INPUT_HANDLE: hFile = Peb->ProcessParameters->StandardInput;
  1092. break;
  1093. case STD_OUTPUT_HANDLE: hFile = Peb->ProcessParameters->StandardOutput;
  1094. break;
  1095. case STD_ERROR_HANDLE: hFile = Peb->ProcessParameters->StandardError;
  1096. break;
  1097. }
  1098. if (CONSOLE_HANDLE(hFile)) {
  1099. return( FlushConsoleInputBuffer( hFile ) );
  1100. }
  1101. Status = NtFlushBuffersFile(hFile,&IoStatusBlock);
  1102. if ( NT_SUCCESS(Status) ) {
  1103. return TRUE;
  1104. }
  1105. else {
  1106. BaseSetLastNTError(Status);
  1107. return FALSE;
  1108. }
  1109. }
  1110. BOOL
  1111. WINAPI
  1112. LockFile(
  1113. HANDLE hFile,
  1114. DWORD dwFileOffsetLow,
  1115. DWORD dwFileOffsetHigh,
  1116. DWORD nNumberOfBytesToLockLow,
  1117. DWORD nNumberOfBytesToLockHigh
  1118. )
  1119. /*++
  1120. Routine Description:
  1121. A byte range within an open file may be locked for exclusive access
  1122. using LockFile.
  1123. Locking a region of a file is used to aquire exclusive access to the
  1124. specified region of the file. File locks are not inherited by the
  1125. new process during process creation.
  1126. Locking a portion of a file denies all other processes both read and
  1127. write access to the specified region of the file. Locking a region
  1128. that goes beyond the current end-of-file position is not an error.
  1129. Locks may not overlap an existing locked region of the file.
  1130. For DOS based systems running share.exe the lock semantics work as
  1131. described above. Without share.exe, all attempts to lock or unlock
  1132. a file will fail.
  1133. Arguments:
  1134. hFile - Supplies an open handle to a file that is to have a range of
  1135. bytes locked for exclusive access. The handle must have been
  1136. created with either GENERIC_READ or GENERIC_WRITE access to the
  1137. file.
  1138. dwFileOffsetLow - Supplies the low order 32-bits of the starting
  1139. byte offset of the file where the lock should begin.
  1140. dwFileOffsetHigh - Supplies the high order 32-bits of the starting
  1141. byte offset of the file where the lock should begin.
  1142. nNumberOfBytesToLockLow - Supplies the low order 32-bits of the length
  1143. of the byte range to be locked.
  1144. nNumberOfBytesToLockHigh - Supplies the high order 32-bits of the length
  1145. of the byte range to be locked.
  1146. Return Value:
  1147. TRUE - The operation was successful.
  1148. FALSE/NULL - The operation failed. Extended error status is available
  1149. using GetLastError.
  1150. --*/
  1151. {
  1152. NTSTATUS Status;
  1153. LARGE_INTEGER ByteOffset;
  1154. LARGE_INTEGER Length;
  1155. IO_STATUS_BLOCK IoStatusBlock;
  1156. if (CONSOLE_HANDLE(hFile)) {
  1157. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  1158. return FALSE;
  1159. }
  1160. ByteOffset.LowPart = dwFileOffsetLow;
  1161. ByteOffset.HighPart = dwFileOffsetHigh;
  1162. Length.LowPart = nNumberOfBytesToLockLow;
  1163. Length.HighPart = nNumberOfBytesToLockHigh;
  1164. Status = NtLockFile( hFile,
  1165. NULL,
  1166. NULL,
  1167. NULL,
  1168. &IoStatusBlock,
  1169. &ByteOffset,
  1170. &Length,
  1171. 0,
  1172. TRUE,
  1173. TRUE
  1174. );
  1175. if (Status == STATUS_PENDING) {
  1176. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  1177. if (NT_SUCCESS( Status )) {
  1178. Status = IoStatusBlock.Status;
  1179. }
  1180. }
  1181. if ( NT_SUCCESS(Status) ) {
  1182. return TRUE;
  1183. }
  1184. else {
  1185. BaseSetLastNTError(Status);
  1186. return FALSE;
  1187. }
  1188. }
  1189. BOOL
  1190. WINAPI
  1191. LockFileEx(
  1192. HANDLE hFile,
  1193. DWORD dwFlags,
  1194. DWORD dwReserved,
  1195. DWORD nNumberOfBytesToLockLow,
  1196. DWORD nNumberOfBytesToLockHigh,
  1197. LPOVERLAPPED lpOverlapped
  1198. )
  1199. /*++
  1200. Routine Description:
  1201. A byte range within an open file may be locked for shared or
  1202. exclusive access using LockFileEx.
  1203. Locking a region of a file is used to aquire shared or exclusive
  1204. access to the specified region of the file. File locks are not
  1205. inherited by the new process during process creation.
  1206. Locking a portion of a file for exclusive access denies all other
  1207. processes both read and write access to the specified region of the
  1208. file. Locking a region that goes beyond the current end-of-file
  1209. position is not an error.
  1210. Locking a portion of a file for shared access denies all other
  1211. processes write access to the specified region of the file, but
  1212. allows other processes to read the locked region.
  1213. If requesting an exclusive lock for a file that is already locked
  1214. shared or exclusively by other threads, then this call will wait
  1215. until the lock is granted unless the LOCKFILE_FAIL_IMMEDIATELY
  1216. flag is specified.
  1217. Locks may not overlap an existing locked region of the file.
  1218. Arguments:
  1219. hFile - Supplies an open handle to a file that is to have a range of
  1220. bytes locked for exclusive access. The handle must have been
  1221. created with either GENERIC_READ or GENERIC_WRITE access to the
  1222. file.
  1223. dwFlags - Supplies flag bits that modify the behavior of this function.
  1224. LOCKFILE_FAIL_IMMEDIATELY - if set, then this function will return
  1225. immediately if it is unable to acquire the requested lock.
  1226. Otherwise it will wait.
  1227. LOCKFILE_EXCLUSIVE_LOCK - if set, then this function requests an
  1228. exclusive lock, otherwise it requested a shared lock.
  1229. dwReserved - Reserved parameter that must be zero.
  1230. nNumberOfBytesToLockLow - Supplies the low order 32-bits of the length
  1231. of the byte range to be locked.
  1232. nNumberOfBytesToLockHigh - Supplies the high order 32-bits of the length
  1233. of the byte range to be locked.
  1234. lpOverlapped - Required pointer to an OVERLAPPED structure to be
  1235. used with the request. It contains the file offset of the
  1236. beginning of the lock range.
  1237. Return Value:
  1238. TRUE - The operation was successful.
  1239. FALSE/NULL - The operation failed. Extended error status is available
  1240. using GetLastError.
  1241. --*/
  1242. {
  1243. NTSTATUS Status;
  1244. LARGE_INTEGER ByteOffset;
  1245. LARGE_INTEGER Length;
  1246. if (CONSOLE_HANDLE(hFile)) {
  1247. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  1248. return FALSE;
  1249. }
  1250. if (dwReserved != 0) {
  1251. SetLastError(ERROR_INVALID_PARAMETER);
  1252. return FALSE;
  1253. }
  1254. ByteOffset.LowPart = lpOverlapped->Offset;
  1255. ByteOffset.HighPart = lpOverlapped->OffsetHigh;
  1256. Length.LowPart = nNumberOfBytesToLockLow;
  1257. Length.HighPart = nNumberOfBytesToLockHigh;
  1258. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  1259. Status = NtLockFile( hFile,
  1260. lpOverlapped->hEvent,
  1261. NULL,
  1262. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  1263. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1264. &ByteOffset,
  1265. &Length,
  1266. 0,
  1267. (BOOLEAN)((dwFlags & LOCKFILE_FAIL_IMMEDIATELY) ? TRUE : FALSE),
  1268. (BOOLEAN)((dwFlags & LOCKFILE_EXCLUSIVE_LOCK) ? TRUE : FALSE)
  1269. );
  1270. if ( NT_SUCCESS(Status) && Status != STATUS_PENDING) {
  1271. return TRUE;
  1272. }
  1273. else {
  1274. BaseSetLastNTError(Status);
  1275. return FALSE;
  1276. }
  1277. }
  1278. BOOL
  1279. WINAPI
  1280. UnlockFile(
  1281. HANDLE hFile,
  1282. DWORD dwFileOffsetLow,
  1283. DWORD dwFileOffsetHigh,
  1284. DWORD nNumberOfBytesToUnlockLow,
  1285. DWORD nNumberOfBytesToUnlockHigh
  1286. )
  1287. /*++
  1288. Routine Description:
  1289. A previously locked byte range within an open file may be Unlocked
  1290. using UnlockFile.
  1291. Unlocking a region of a file is used release a previously aquired
  1292. lock on a file. The region to unlock must exactly correspond to an
  1293. existing locked region. Two adjacent regions of a file can not be
  1294. locked seperately and then be unlocked using a single region that
  1295. spans both locked regions.
  1296. If a process terminates with a portion of a file locked, or closes a
  1297. file that has outstanding locks, the behavior is not specified.
  1298. For DOS based systems running share.exe the lock semantics work as
  1299. described above. Without share.exe, all attempts to lock or unlock
  1300. a file will fail.
  1301. Arguments:
  1302. hFile - Supplies an open handle to a file that is to have an
  1303. existing locked region unlocked. The handle must have been
  1304. created with either GENERIC_READ or GENERIC_WRITE access to the
  1305. file.
  1306. dwFileOffsetLow - Supplies the low order 32-bits of an existing
  1307. locked region to be unlocked.
  1308. dwFileOffsetHigh - Supplies the high order 32-bits of an existing
  1309. locked region to be unlocked.
  1310. nNumberOfBytesToUnlockLow - Supplies the low order 32-bits of the
  1311. length of the byte range to be unlocked.
  1312. nNumberOfBytesToUnlockHigh - Supplies the high order 32-bits of the
  1313. length of the byte range to be unlocked.
  1314. Return Value:
  1315. TRUE - The operation was successful.
  1316. FALSE/NULL - The operation failed. Extended error status is available
  1317. using GetLastError.
  1318. --*/
  1319. {
  1320. BOOL bResult;
  1321. OVERLAPPED Overlapped;
  1322. NTSTATUS Status;
  1323. Overlapped.Offset = dwFileOffsetLow;
  1324. Overlapped.OffsetHigh = dwFileOffsetHigh;
  1325. bResult = UnlockFileEx( hFile,
  1326. 0,
  1327. nNumberOfBytesToUnlockLow,
  1328. nNumberOfBytesToUnlockHigh,
  1329. &Overlapped
  1330. );
  1331. if (!bResult && GetLastError() == ERROR_IO_PENDING) {
  1332. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  1333. if (NT_SUCCESS( Status )) {
  1334. Status = (NTSTATUS)Overlapped.Internal;
  1335. }
  1336. if ( NT_SUCCESS(Status) ) {
  1337. return TRUE;
  1338. }
  1339. else {
  1340. BaseSetLastNTError(Status);
  1341. return FALSE;
  1342. }
  1343. }
  1344. else {
  1345. return bResult;
  1346. }
  1347. }
  1348. BOOL
  1349. WINAPI
  1350. UnlockFileEx(
  1351. HANDLE hFile,
  1352. DWORD dwReserved,
  1353. DWORD nNumberOfBytesToUnlockLow,
  1354. DWORD nNumberOfBytesToUnlockHigh,
  1355. LPOVERLAPPED lpOverlapped
  1356. )
  1357. /*++
  1358. Routine Description:
  1359. A previously locked byte range within an open file may be Unlocked
  1360. using UnlockFile.
  1361. Unlocking a region of a file is used release a previously aquired
  1362. lock on a file. The region to unlock must exactly correspond to an
  1363. existing locked region. Two adjacent regions of a file can not be
  1364. locked seperately and then be unlocked using a single region that
  1365. spans both locked regions.
  1366. If a process terminates with a portion of a file locked, or closes a
  1367. file that has outstanding locks, the behavior is not specified.
  1368. Arguments:
  1369. hFile - Supplies an open handle to a file that is to have an
  1370. existing locked region unlocked. The handle must have been
  1371. created with either GENERIC_READ or GENERIC_WRITE access to the
  1372. file.
  1373. dwReserved - Reserved parameter that must be zero.
  1374. nNumberOfBytesToUnlockLow - Supplies the low order 32-bits of the
  1375. length of the byte range to be unlocked.
  1376. nNumberOfBytesToUnlockHigh - Supplies the high order 32-bits of the
  1377. length of the byte range to be unlocked.
  1378. lpOverlapped - Required pointer to an OVERLAPPED structure to be
  1379. used with the request. It contains the file offset of the
  1380. beginning of the lock range.
  1381. Return Value:
  1382. TRUE - The operation was successful.
  1383. FALSE/NULL - The operation failed. Extended error status is available
  1384. using GetLastError.
  1385. --*/
  1386. {
  1387. NTSTATUS Status;
  1388. LARGE_INTEGER ByteOffset;
  1389. LARGE_INTEGER Length;
  1390. if (CONSOLE_HANDLE(hFile)) {
  1391. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  1392. return FALSE;
  1393. }
  1394. if (dwReserved != 0) {
  1395. SetLastError(ERROR_INVALID_PARAMETER);
  1396. return FALSE;
  1397. }
  1398. ByteOffset.LowPart = lpOverlapped->Offset;
  1399. ByteOffset.HighPart = lpOverlapped->OffsetHigh;
  1400. Length.LowPart = nNumberOfBytesToUnlockLow;
  1401. Length.HighPart = nNumberOfBytesToUnlockHigh;
  1402. Status = NtUnlockFile(
  1403. hFile,
  1404. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1405. &ByteOffset,
  1406. &Length,
  1407. 0
  1408. );
  1409. if ( NT_SUCCESS(Status) ) {
  1410. return TRUE;
  1411. }
  1412. else {
  1413. BaseSetLastNTError(Status);
  1414. return FALSE;
  1415. }
  1416. }
  1417. UINT
  1418. WINAPI
  1419. SetHandleCount(
  1420. UINT uNumber
  1421. )
  1422. /*++
  1423. Routine Description:
  1424. This function changes the number of file handles available to a
  1425. process. For DOS based Win32, the default maximum number of file
  1426. handles available to a process is 20. For NT/Win32 systems, this
  1427. API has no effect.
  1428. Arguments:
  1429. uNumber - Specifies the number of file handles needed by the
  1430. application. The maximum is 255.
  1431. Return Value:
  1432. The return value specifies the number of file handles actually
  1433. available to the application. It may be less than the number
  1434. specified by the wNumber parameter.
  1435. --*/
  1436. {
  1437. return uNumber;
  1438. }
  1439. DWORD
  1440. WINAPI
  1441. GetFileSize(
  1442. HANDLE hFile,
  1443. LPDWORD lpFileSizeHigh
  1444. )
  1445. /*++
  1446. Routine Description:
  1447. This function returns the size of the file specified by
  1448. hFile. It is capable of returning 64-bits worth of file size.
  1449. The return value contains the low order 32-bits of the file's size.
  1450. The optional lpFileSizeHigh returns the high order 32-bits of the
  1451. file's size.
  1452. Arguments:
  1453. hFile - Supplies an open handle to a file whose size is to be
  1454. returned. The handle must have been created with either
  1455. GENERIC_READ or GENERIC_WRITE access to the file.
  1456. lpFileSizeHigh - An optional parameter, that if specified, returns
  1457. the high order 64-bits of the file's size.
  1458. Return Value:
  1459. Not -1 - Returns the low order 32-bits of the specified file's size.
  1460. 0xffffffff - If the value of size of the file cannot be determined,
  1461. or an invalid handle or handle with inappropriate access, or a
  1462. handle to a non-file is specified, this error is returned. If
  1463. the file's size (low 32-bits) is -1, then this value is
  1464. returned, and GetLastError() will return 0. Extended error
  1465. status is available using GetLastError.
  1466. --*/
  1467. {
  1468. BOOL b;
  1469. LARGE_INTEGER Li;
  1470. b = GetFileSizeEx(hFile,&Li);
  1471. if ( b ) {
  1472. if ( ARGUMENT_PRESENT(lpFileSizeHigh) ) {
  1473. *lpFileSizeHigh = (DWORD)Li.HighPart;
  1474. }
  1475. if (Li.LowPart == -1 ) {
  1476. SetLastError(0);
  1477. }
  1478. }
  1479. else {
  1480. Li.LowPart = -1;
  1481. }
  1482. return Li.LowPart;
  1483. }
  1484. BOOL
  1485. WINAPI
  1486. GetFileSizeEx(
  1487. HANDLE hFile,
  1488. PLARGE_INTEGER lpFileSize
  1489. )
  1490. /*++
  1491. Routine Description:
  1492. This function returns the size of the file specified by
  1493. hFile. It is capable of returning 64-bits worth of file size.
  1494. Arguments:
  1495. hFile - Supplies an open handle to a file whose size is to be
  1496. returned. The handle must have been created with either
  1497. GENERIC_READ or GENERIC_WRITE access to the file.
  1498. lpFileSize - Returns the files size
  1499. Return Value:
  1500. TRUE - The operation was successful
  1501. FALSE - The operation failed. Extended error
  1502. status is available using GetLastError.
  1503. --*/
  1504. {
  1505. NTSTATUS Status;
  1506. IO_STATUS_BLOCK IoStatusBlock;
  1507. FILE_STANDARD_INFORMATION StandardInfo;
  1508. Status = NtQueryInformationFile(
  1509. hFile,
  1510. &IoStatusBlock,
  1511. &StandardInfo,
  1512. sizeof(StandardInfo),
  1513. FileStandardInformation
  1514. );
  1515. if ( !NT_SUCCESS(Status) ) {
  1516. BaseSetLastNTError(Status);
  1517. return FALSE;
  1518. }
  1519. else {
  1520. *lpFileSize = StandardInfo.EndOfFile;
  1521. return TRUE;
  1522. }
  1523. }
  1524. VOID
  1525. WINAPI
  1526. BasepIoCompletion(
  1527. PVOID ApcContext,
  1528. PIO_STATUS_BLOCK IoStatusBlock,
  1529. DWORD Reserved
  1530. )
  1531. /*++
  1532. Routine Description:
  1533. This procedure is called to complete ReadFileEx and WriteFileEx
  1534. asynchronous I/O. Its primary function is to extract the
  1535. appropriate information from the passed IoStatusBlock and call the
  1536. users completion routine.
  1537. The users completion routine is called as:
  1538. Routine Description:
  1539. When an outstanding I/O completes with a callback, this
  1540. function is called. This function is only called while the
  1541. thread is in an alertable wait (SleepEx,
  1542. WaitForSingleObjectEx, or WaitForMultipleObjectsEx with the
  1543. bAlertable flag set to TRUE). Returning from this function
  1544. allows another pendiong I/O completion callback to be
  1545. processed. If this is the case, this callback is entered
  1546. before the termination of the thread's wait with a return
  1547. code of WAIT_IO_COMPLETION.
  1548. Note that each time your completion routine is called, the
  1549. system uses some of your stack. If you code your completion
  1550. logic to do additional ReadFileEx's and WriteFileEx's within
  1551. your completion routine, AND you do alertable waits in your
  1552. completion routine, you may grow your stack without ever
  1553. trimming it back.
  1554. Arguments:
  1555. dwErrorCode - Supplies the I/O completion status for the
  1556. related I/O. A value of 0 indicates that the I/O was
  1557. successful. Note that end of file is indicated by a
  1558. non-zero dwErrorCode value of ERROR_HANDLE_EOF.
  1559. dwNumberOfBytesTransfered - Supplies the number of bytes
  1560. transfered during the associated I/O. If an error
  1561. occured, a value of 0 is supplied.
  1562. lpOverlapped - Supplies the address of the OVERLAPPED
  1563. structure used to initiate the associated I/O. The
  1564. hEvent field of this structure is not used by the system
  1565. and may be used by the application to provide additional
  1566. I/O context. Once a completion routine is called, the
  1567. system will not use the OVERLAPPED structure. The
  1568. completion routine is free to deallocate the overlapped
  1569. structure.
  1570. Arguments:
  1571. ApcContext - Supplies the users completion routine. The format of
  1572. this routine is an LPOVERLAPPED_COMPLETION_ROUTINE.
  1573. IoStatusBlock - Supplies the address of the IoStatusBlock that
  1574. contains the I/O completion status. The IoStatusBlock is
  1575. contained within the OVERLAPPED structure.
  1576. Reserved - Not used; reserved for future use.
  1577. Return Value:
  1578. None.
  1579. --*/
  1580. {
  1581. PBASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK ActivationBlock;
  1582. LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine;
  1583. DWORD dwErrorCode;
  1584. DWORD dwNumberOfBytesTransfered;
  1585. LPOVERLAPPED lpOverlapped;
  1586. NTSTATUS Status;
  1587. RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME ActivationFrame = { sizeof(ActivationFrame), RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_FORMAT_WHISTLER };
  1588. PACTIVATION_CONTEXT ActivationContext = NULL;
  1589. if ( NT_ERROR(IoStatusBlock->Status) ) {
  1590. dwErrorCode = RtlNtStatusToDosError(IoStatusBlock->Status);
  1591. dwNumberOfBytesTransfered = 0;
  1592. } else {
  1593. dwErrorCode = 0;
  1594. dwNumberOfBytesTransfered = (DWORD)IoStatusBlock->Information;
  1595. }
  1596. ActivationBlock = (PBASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK) ApcContext;
  1597. ActivationContext = ActivationBlock->ActivationContext;
  1598. CompletionRoutine = (LPOVERLAPPED_COMPLETION_ROUTINE) ActivationBlock->CallbackFunction;
  1599. lpOverlapped = (LPOVERLAPPED) CONTAINING_RECORD(IoStatusBlock, OVERLAPPED, Internal);
  1600. if (!(ActivationBlock->Flags & BASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK_FLAG_DO_NOT_FREE_AFTER_CALLBACK))
  1601. BasepFreeActivationContextActivationBlock(ActivationBlock);
  1602. RtlActivateActivationContextUnsafeFast(&ActivationFrame, ActivationContext);
  1603. __try {
  1604. (*CompletionRoutine)(dwErrorCode, dwNumberOfBytesTransfered, lpOverlapped);
  1605. } __finally {
  1606. RtlDeactivateActivationContextUnsafeFast(&ActivationFrame);
  1607. }
  1608. Reserved;
  1609. }
  1610. VOID
  1611. WINAPI
  1612. BasepIoCompletionSimple(
  1613. PVOID ApcContext,
  1614. PIO_STATUS_BLOCK IoStatusBlock,
  1615. DWORD Reserved
  1616. )
  1617. /*++
  1618. Routine Description:
  1619. This procedure is called to complete ReadFileEx and WriteFileEx
  1620. asynchronous I/O. Its primary function is to extract the
  1621. appropriate information from the passed IoStatusBlock and call the
  1622. users completion routine.
  1623. The users completion routine is called as:
  1624. Routine Description:
  1625. When an outstanding I/O completes with a callback, this
  1626. function is called. This function is only called while the
  1627. thread is in an alertable wait (SleepEx,
  1628. WaitForSingleObjectEx, or WaitForMultipleObjectsEx with the
  1629. bAlertable flag set to TRUE). Returning from this function
  1630. allows another pendiong I/O completion callback to be
  1631. processed. If this is the case, this callback is entered
  1632. before the termination of the thread's wait with a return
  1633. code of WAIT_IO_COMPLETION.
  1634. Note that each time your completion routine is called, the
  1635. system uses some of your stack. If you code your completion
  1636. logic to do additional ReadFileEx's and WriteFileEx's within
  1637. your completion routine, AND you do alertable waits in your
  1638. completion routine, you may grow your stack without ever
  1639. trimming it back.
  1640. Arguments:
  1641. dwErrorCode - Supplies the I/O completion status for the
  1642. related I/O. A value of 0 indicates that the I/O was
  1643. successful. Note that end of file is indicated by a
  1644. non-zero dwErrorCode value of ERROR_HANDLE_EOF.
  1645. dwNumberOfBytesTransfered - Supplies the number of bytes
  1646. transfered during the associated I/O. If an error
  1647. occured, a value of 0 is supplied.
  1648. lpOverlapped - Supplies the address of the OVERLAPPED
  1649. structure used to initiate the associated I/O. The
  1650. hEvent field of this structure is not used by the system
  1651. and may be used by the application to provide additional
  1652. I/O context. Once a completion routine is called, the
  1653. system will not use the OVERLAPPED structure. The
  1654. completion routine is free to deallocate the overlapped
  1655. structure.
  1656. Arguments:
  1657. ApcContext - Supplies the users completion routine. The format of
  1658. this routine is an LPOVERLAPPED_COMPLETION_ROUTINE.
  1659. IoStatusBlock - Supplies the address of the IoStatusBlock that
  1660. contains the I/O completion status. The IoStatusBlock is
  1661. contained within the OVERLAPPED structure.
  1662. Reserved - Not used; reserved for future use.
  1663. Return Value:
  1664. None.
  1665. --*/
  1666. {
  1667. LPOVERLAPPED_COMPLETION_ROUTINE CompletionRoutine;
  1668. DWORD dwErrorCode;
  1669. DWORD dwNumberOfBytesTransfered;
  1670. LPOVERLAPPED lpOverlapped;
  1671. dwErrorCode = 0;
  1672. if ( NT_ERROR(IoStatusBlock->Status) ) {
  1673. dwErrorCode = RtlNtStatusToDosError(IoStatusBlock->Status);
  1674. dwNumberOfBytesTransfered = 0;
  1675. }
  1676. else {
  1677. dwErrorCode = 0;
  1678. dwNumberOfBytesTransfered = (DWORD)IoStatusBlock->Information;
  1679. }
  1680. CompletionRoutine = (LPOVERLAPPED_COMPLETION_ROUTINE)ApcContext;
  1681. lpOverlapped = (LPOVERLAPPED)CONTAINING_RECORD(IoStatusBlock,OVERLAPPED,Internal);
  1682. (CompletionRoutine)(dwErrorCode,dwNumberOfBytesTransfered,lpOverlapped);
  1683. Reserved;
  1684. }
  1685. BOOL
  1686. WINAPI
  1687. ReadFileEx(
  1688. HANDLE hFile,
  1689. LPVOID lpBuffer,
  1690. DWORD nNumberOfBytesToRead,
  1691. LPOVERLAPPED lpOverlapped,
  1692. LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  1693. )
  1694. /*++
  1695. Routine Description:
  1696. Data can be read from a file using ReadFileEx.
  1697. This API reports its completion status asynchronously by calling the
  1698. specified lpCompletionRoutine.
  1699. The caller of this routine uses the lpOverlappedStructure to specify
  1700. the byte offset within the file where the read is to begin from.
  1701. For files that do not support this concept (pipes...), the starting
  1702. file offset is ignored.
  1703. Upon successful completion of this API (return value of TRUE), the
  1704. calling thread has an I/O outstanding. When the I/O completes, and
  1705. the thread is blocked in an alertable wait, the lpCompletionRoutine
  1706. will be called and the wait will return with a return code of
  1707. WAIT_IO_COMPLETION. If the I/O completes, but the thread issuing
  1708. the I/O is not in an alertable wait, the call to the completion
  1709. routine is queued until the thread executes an alertable wait.
  1710. If this API fails (by returning FALSE), GetLastError can be used to
  1711. get additional error information. If this call fails because the
  1712. thread issued a read beyond the end of file, GetLastError will
  1713. return a value of ERROR_HANDLE_EOF.
  1714. Arguments:
  1715. hFile - Supplies an open handle to a file that is to be read. The
  1716. file handle must have been created with GENERIC_READ access to
  1717. the file. The file must have been created with the
  1718. FILE_FLAG_OVERLAPPED flag.
  1719. lpBuffer - Supplies the address of a buffer to receive the data read
  1720. from the file.
  1721. nNumberOfBytesToRead - Supplies the number of bytes to read from the
  1722. file.
  1723. lpOverlapped - Supplies the address of an OVERLAPPED structure to be
  1724. used with the request. The caller of this function must specify
  1725. a starting byte offset within the file to start the read from.
  1726. It does this using the Offset and OffsetHigh fields of the
  1727. overlapped structure. This call does not use or modify the
  1728. hEvent field of the overlapped structure. The caller may use
  1729. this field for any purpose. This API does use the Internal and
  1730. InternalHigh fields of the overlapped structure, the thread
  1731. should not manipulate this. The lpOverlapped structure must
  1732. remain valid for the duration of the I/O. It is not a good idea
  1733. to make it a local variable and then possibly returning from the
  1734. routine with the I/O that is using this structure still pending.
  1735. Return Value:
  1736. TRUE - The operation was successul. Completion status will be
  1737. propagated to the caller using the completion callback
  1738. mechanism. Note that this information is only made available to
  1739. the thread that issued the I/O, and only when the I/O completes,
  1740. and the thread is executing in an alertable wait.
  1741. FALSE - The operation failed. Extended error status is available
  1742. using GetLastError. Note that end of file is treated as a failure
  1743. with an error code of ERROR_HANDLE_EOF.
  1744. --*/
  1745. {
  1746. NTSTATUS Status;
  1747. LARGE_INTEGER Li;
  1748. PBASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK ActivationBlock = NULL;
  1749. PIO_APC_ROUTINE IoApcRoutine = &BasepIoCompletionSimple;
  1750. PVOID ApcContext = lpCompletionRoutine;
  1751. Li.LowPart = lpOverlapped->Offset;
  1752. Li.HighPart = lpOverlapped->OffsetHigh;
  1753. // If there's an APC routine to call we need to allocate a little chunk of heap
  1754. // to pass the activation context to the APC callback.
  1755. if (lpCompletionRoutine != NULL) {
  1756. Status = BasepAllocateActivationContextActivationBlock(
  1757. BASEP_ALLOCATE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK_FLAG_DO_NOT_ALLOCATE_IF_PROCESS_DEFAULT,
  1758. lpCompletionRoutine,
  1759. lpOverlapped,
  1760. &ActivationBlock);
  1761. if (!NT_SUCCESS(Status)) {
  1762. BaseSetLastNTError(Status);
  1763. return FALSE;
  1764. }
  1765. // If there's nothing to do, call the simpler one that doesn't try to do activation context stuff
  1766. if (ActivationBlock != NULL) {
  1767. IoApcRoutine = &BasepIoCompletion;
  1768. ApcContext = ActivationBlock;
  1769. }
  1770. }
  1771. Status = NtReadFile(
  1772. hFile,
  1773. NULL,
  1774. IoApcRoutine,
  1775. ApcContext,
  1776. (PIO_STATUS_BLOCK) &lpOverlapped->Internal,
  1777. lpBuffer,
  1778. nNumberOfBytesToRead,
  1779. &Li,
  1780. NULL
  1781. );
  1782. if ( NT_ERROR(Status) ) {
  1783. if (ActivationBlock != NULL)
  1784. BasepFreeActivationContextActivationBlock(ActivationBlock);
  1785. BaseSetLastNTError(Status);
  1786. return FALSE;
  1787. }
  1788. return TRUE;
  1789. }
  1790. BOOL
  1791. WINAPI
  1792. WriteFileEx(
  1793. HANDLE hFile,
  1794. LPCVOID lpBuffer,
  1795. DWORD nNumberOfBytesToWrite,
  1796. LPOVERLAPPED lpOverlapped,
  1797. LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  1798. )
  1799. /*++
  1800. Routine Description:
  1801. Data can be written to a file using WriteFileEx.
  1802. This API reports its completion status asynchronously by calling the
  1803. specified lpCompletionRoutine.
  1804. The caller of this routine uses the lpOverlappedStructure to specify
  1805. the byte offset within the file where the write is to begin.
  1806. For files that do not support this concept (pipes...), the starting
  1807. file offset is ignored.
  1808. Upon successful completion of this API (return value of TRUE), the
  1809. calling thread has an I/O outstanding. When the I/O completes, and
  1810. the thread is blocked in an alertable wait, the lpCompletionRoutine
  1811. will be called and the wait will return with a return code of
  1812. WAIT_IO_COMPLETION. If the I/O completes, but the thread issuing
  1813. the I/O is not in an alertable wait, the call to the completion
  1814. routine is queued until the thread executes an alertable wait.
  1815. If this API fails (by returning FALSE), GetLastError can be used to
  1816. get additional error information.
  1817. Unlike DOS, a NumberOfBytesToWrite value of zero does not truncate
  1818. or extend the file. If this function is required, SetEndOfFile
  1819. should be used.
  1820. Arguments:
  1821. hFile - Supplies an open handle to a file that is to be written. The
  1822. file handle must have been created with GENERIC_WRITE access to
  1823. the file.
  1824. lpBuffer - Supplies the address of the data that is to be written to
  1825. the file.
  1826. nNumberOfBytesToWrite - Supplies the number of bytes to write to the
  1827. file. Unlike DOS, a value of zero is interpreted a null write.
  1828. lpOverlapped - Supplies the address of an OVERLAPPED structure to be
  1829. used with the request. The caller of this function must specify
  1830. a starting byte offset within the file to start the write to.
  1831. It does this using the Offset and OffsetHigh fields of the
  1832. overlapped structure. This call does not use or modify the
  1833. hEvent field of the overlapped structure. The caller may use
  1834. this field for any purpose. This API does use the Internal and
  1835. InternalHigh fields of the overlapped structure, the thread
  1836. should not manipulate this. The lpOverlapped structure must
  1837. remain valid for the duration of the I/O. It is not a good idea
  1838. to make it a local variable and then possibly returning from the
  1839. routine with the I/O that is using this structure still pending.
  1840. Return Value:
  1841. TRUE - The operation was successul. Completion status will be
  1842. propagated to the caller using the completion callback
  1843. mechanism. Note that this information is only made available to
  1844. the thread that issued the I/O, and only when the I/O completes,
  1845. and the thread is executing in an alertable wait.
  1846. FALSE - The operation failed. Extended error status is available
  1847. using GetLastError. Note that end of file is treated as a failure
  1848. with an error code of ERROR_HANDLE_EOF.
  1849. --*/
  1850. {
  1851. NTSTATUS Status;
  1852. LARGE_INTEGER Li;
  1853. PBASE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK ActivationBlock = NULL;
  1854. PIO_APC_ROUTINE IoApcRoutine = &BasepIoCompletionSimple;
  1855. PVOID ApcContext = lpCompletionRoutine;
  1856. Li.LowPart = lpOverlapped->Offset;
  1857. Li.HighPart = lpOverlapped->OffsetHigh;
  1858. // If there's an APC routine to call we may need to allocate a little chunk of heap
  1859. // to pass to the APC callback.
  1860. //
  1861. // we'll replace the parameters to the common NtWriteFile call below so that
  1862. // the control flow is obvious.
  1863. //
  1864. if (lpCompletionRoutine != NULL) {
  1865. Status = BasepAllocateActivationContextActivationBlock(
  1866. BASEP_ALLOCATE_ACTIVATION_CONTEXT_ACTIVATION_BLOCK_FLAG_DO_NOT_ALLOCATE_IF_PROCESS_DEFAULT,
  1867. lpCompletionRoutine,
  1868. lpOverlapped,
  1869. &ActivationBlock);
  1870. if (!NT_SUCCESS(Status)) {
  1871. BaseSetLastNTError(Status);
  1872. return FALSE;
  1873. }
  1874. // If there's nothing to do, call the simpler one that doesn't try to do activation context stuff
  1875. if (ActivationBlock != NULL) {
  1876. IoApcRoutine = &BasepIoCompletion;
  1877. ApcContext = ActivationBlock;
  1878. }
  1879. }
  1880. Status = NtWriteFile(
  1881. hFile,
  1882. NULL,
  1883. IoApcRoutine,
  1884. ApcContext,
  1885. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1886. (LPVOID)lpBuffer,
  1887. nNumberOfBytesToWrite,
  1888. &Li,
  1889. NULL
  1890. );
  1891. if ( NT_ERROR(Status) ) {
  1892. if (ActivationBlock != NULL) {
  1893. BasepFreeActivationContextActivationBlock(ActivationBlock);
  1894. }
  1895. BaseSetLastNTError(Status);
  1896. return FALSE;
  1897. }
  1898. return TRUE;
  1899. }
  1900. BOOL
  1901. WINAPI
  1902. DeviceIoControl(
  1903. HANDLE hDevice,
  1904. DWORD dwIoControlCode,
  1905. LPVOID lpInBuffer,
  1906. DWORD nInBufferSize,
  1907. LPVOID lpOutBuffer,
  1908. DWORD nOutBufferSize,
  1909. LPDWORD lpBytesReturned,
  1910. LPOVERLAPPED lpOverlapped
  1911. )
  1912. /*++
  1913. Routine Description:
  1914. An operation on a device may be performed by calling the device driver
  1915. directly using the DeviceIoContrl function.
  1916. The device driver must first be opened to get a valid handle.
  1917. Arguments:
  1918. hDevice - Supplies an open handle a device on which the operation is to
  1919. be performed.
  1920. dwIoControlCode - Supplies the control code for the operation. This
  1921. control code determines on which type of device the operation must
  1922. be performed and determines exactly what operation is to be
  1923. performed.
  1924. lpInBuffer - Suplies an optional pointer to an input buffer that contains
  1925. the data required to perform the operation. Whether or not the
  1926. buffer is actually optional is dependent on the IoControlCode.
  1927. nInBufferSize - Supplies the length of the input buffer in bytes.
  1928. lpOutBuffer - Suplies an optional pointer to an output buffer into which
  1929. the output data will be copied. Whether or not the buffer is actually
  1930. optional is dependent on the IoControlCode.
  1931. nOutBufferSize - Supplies the length of the output buffer in bytes.
  1932. lpBytesReturned - Supplies a pointer to a dword which will receive the
  1933. actual length of the data returned in the output buffer.
  1934. lpOverlapped - An optional parameter that supplies an overlap structure to
  1935. be used with the request. If NULL or the handle was created without
  1936. FILE_FLAG_OVERLAPPED then the DeviceIoControl will not return until
  1937. the operation completes.
  1938. When lpOverlapped is supplied and FILE_FLAG_OVERLAPPED was specified
  1939. when the handle was created, DeviceIoControl may return
  1940. ERROR_IO_PENDING to allow the caller to continue processing while the
  1941. operation completes. The event (or File handle if hEvent == NULL) will
  1942. be set to the not signalled state before ERROR_IO_PENDING is
  1943. returned. The event will be set to the signalled state upon completion
  1944. of the request. GetOverlappedResult is used to determine the result
  1945. when ERROR_IO_PENDING is returned.
  1946. Return Value:
  1947. TRUE -- The operation was successful.
  1948. FALSE -- The operation failed. Extended error status is available using
  1949. GetLastError.
  1950. --*/
  1951. {
  1952. NTSTATUS Status;
  1953. BOOLEAN DevIoCtl;
  1954. if ( dwIoControlCode >> 16 == FILE_DEVICE_FILE_SYSTEM ) {
  1955. DevIoCtl = FALSE;
  1956. }
  1957. else {
  1958. DevIoCtl = TRUE;
  1959. }
  1960. if ( ARGUMENT_PRESENT( lpOverlapped ) ) {
  1961. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  1962. if ( DevIoCtl ) {
  1963. Status = NtDeviceIoControlFile(
  1964. hDevice,
  1965. lpOverlapped->hEvent,
  1966. NULL, // APC routine
  1967. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  1968. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1969. dwIoControlCode, // IoControlCode
  1970. lpInBuffer, // Buffer for data to the FS
  1971. nInBufferSize,
  1972. lpOutBuffer, // OutputBuffer for data from the FS
  1973. nOutBufferSize // OutputBuffer Length
  1974. );
  1975. }
  1976. else {
  1977. Status = NtFsControlFile(
  1978. hDevice,
  1979. lpOverlapped->hEvent,
  1980. NULL, // APC routine
  1981. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  1982. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  1983. dwIoControlCode, // IoControlCode
  1984. lpInBuffer, // Buffer for data to the FS
  1985. nInBufferSize,
  1986. lpOutBuffer, // OutputBuffer for data from the FS
  1987. nOutBufferSize // OutputBuffer Length
  1988. );
  1989. }
  1990. // handle warning value STATUS_BUFFER_OVERFLOW somewhat correctly
  1991. if ( !NT_ERROR(Status) && ARGUMENT_PRESENT(lpBytesReturned) ) {
  1992. try {
  1993. *lpBytesReturned = (DWORD)lpOverlapped->InternalHigh;
  1994. }
  1995. except(EXCEPTION_EXECUTE_HANDLER) {
  1996. *lpBytesReturned = 0;
  1997. }
  1998. }
  1999. if ( NT_SUCCESS(Status) && Status != STATUS_PENDING) {
  2000. return TRUE;
  2001. }
  2002. else {
  2003. BaseSetLastNTError(Status);
  2004. return FALSE;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. IO_STATUS_BLOCK Iosb;
  2010. if ( DevIoCtl ) {
  2011. Status = NtDeviceIoControlFile(
  2012. hDevice,
  2013. NULL,
  2014. NULL, // APC routine
  2015. NULL, // APC Context
  2016. &Iosb,
  2017. dwIoControlCode, // IoControlCode
  2018. lpInBuffer, // Buffer for data to the FS
  2019. nInBufferSize,
  2020. lpOutBuffer, // OutputBuffer for data from the FS
  2021. nOutBufferSize // OutputBuffer Length
  2022. );
  2023. }
  2024. else {
  2025. Status = NtFsControlFile(
  2026. hDevice,
  2027. NULL,
  2028. NULL, // APC routine
  2029. NULL, // APC Context
  2030. &Iosb,
  2031. dwIoControlCode, // IoControlCode
  2032. lpInBuffer, // Buffer for data to the FS
  2033. nInBufferSize,
  2034. lpOutBuffer, // OutputBuffer for data from the FS
  2035. nOutBufferSize // OutputBuffer Length
  2036. );
  2037. }
  2038. if ( Status == STATUS_PENDING) {
  2039. // Operation must complete before return & Iosb destroyed
  2040. Status = NtWaitForSingleObject( hDevice, FALSE, NULL );
  2041. if ( NT_SUCCESS(Status)) {
  2042. Status = Iosb.Status;
  2043. }
  2044. }
  2045. if ( NT_SUCCESS(Status) ) {
  2046. *lpBytesReturned = (DWORD)Iosb.Information;
  2047. return TRUE;
  2048. }
  2049. else {
  2050. // handle warning value STATUS_BUFFER_OVERFLOW somewhat correctly
  2051. if ( !NT_ERROR(Status) ) {
  2052. *lpBytesReturned = (DWORD)Iosb.Information;
  2053. }
  2054. BaseSetLastNTError(Status);
  2055. return FALSE;
  2056. }
  2057. }
  2058. }
  2059. BOOL
  2060. WINAPI
  2061. CancelIo(
  2062. HANDLE hFile
  2063. )
  2064. /*++
  2065. Routine Description:
  2066. This routine cancels all of the outstanding I/O for the specified handle
  2067. for the specified file.
  2068. Arguments:
  2069. hFile - Supplies the handle to the file whose pending I/O is to be
  2070. canceled.
  2071. Return Value:
  2072. TRUE -- The operation was successful.
  2073. FALSE -- The operation failed. Extended error status is available using
  2074. GetLastError.
  2075. --*/
  2076. {
  2077. NTSTATUS Status;
  2078. IO_STATUS_BLOCK IoStatusBlock;
  2079. //
  2080. // Simply cancel the I/O for the specified file.
  2081. //
  2082. Status = NtCancelIoFile(hFile, &IoStatusBlock);
  2083. if ( NT_SUCCESS(Status) ) {
  2084. return TRUE;
  2085. }
  2086. else {
  2087. BaseSetLastNTError(Status);
  2088. return FALSE;
  2089. }
  2090. }
  2091. BOOL
  2092. WINAPI
  2093. ReadFileScatter(
  2094. HANDLE hFile,
  2095. FILE_SEGMENT_ELEMENT aSegementArray[],
  2096. DWORD nNumberOfBytesToRead,
  2097. LPDWORD lpReserved,
  2098. LPOVERLAPPED lpOverlapped
  2099. )
  2100. /*++
  2101. Routine Description:
  2102. Data can be read from a file using ReadFileScatter. The data
  2103. is then scatter to specified buffer segements.
  2104. This API is used to read data from a file. Data is read from the
  2105. file from the position indicated by the file pointer. After the
  2106. read completes, the file pointer is adjusted by the number of bytes
  2107. actually read. A return value of TRUE coupled with a bytes read of
  2108. 0 indicates that the file pointer was beyond the current end of the
  2109. file at the time of the read.
  2110. Arguments:
  2111. hFile - Supplies an open handle to a file that is to be read. The
  2112. file handle must have been created with GENERIC_READ access to
  2113. the file.
  2114. aSegementArray - Supplies a pointer an array of virtual segments.
  2115. A virtual segment is a memory buffer where part of the transferred data
  2116. should be placed. Segments are have a fix size of PAGE_SIZE
  2117. and must be aligned on a PAGE_SIZE boundary.
  2118. nNumberOfBytesToRead - Supplies the number of bytes to read from the file.
  2119. lpReserved - Reserved for now.
  2120. lpOverlapped - Optionally points to an OVERLAPPED structure to be used with the
  2121. request. If NULL then the transfer starts at the current file position
  2122. and ReadFile will not return until the operation completes.
  2123. If the handle hFile was created without specifying FILE_FLAG_OVERLAPPED
  2124. the file pointer is moved to the specified offset plus
  2125. lpNumberOfBytesRead before ReadFile returns. ReadFile will wait for the
  2126. request to complete before returning (it will not return
  2127. ERROR_IO_PENDING).
  2128. When FILE_FLAG_OVERLAPPED is specified, ReadFile may return
  2129. ERROR_IO_PENDING to allow the calling function to continue processing
  2130. while the operation completes. The event (or hFile if hEvent is NULL) will
  2131. be set to the signalled state upon completion of the request.
  2132. When the handle is created with FILE_FLAG_OVERLAPPED and lpOverlapped
  2133. is set to NULL, ReadFile will return ERROR_INVALID_PARAMTER because
  2134. the file offset is required.
  2135. Return Value:
  2136. TRUE - The operation was successul.
  2137. FALSE - The operation failed. Extended error status is available
  2138. using GetLastError.
  2139. --*/
  2140. {
  2141. NTSTATUS Status;
  2142. IO_STATUS_BLOCK IoStatusBlock;
  2143. LPDWORD lpNumberOfBytesRead = NULL;
  2144. if ( ARGUMENT_PRESENT(lpReserved) ||
  2145. !ARGUMENT_PRESENT( lpOverlapped )) {
  2146. SetLastError(ERROR_INVALID_PARAMETER);
  2147. return FALSE;
  2148. }
  2149. if (CONSOLE_HANDLE(hFile)) {
  2150. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  2151. return FALSE;
  2152. }
  2153. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  2154. *lpNumberOfBytesRead = 0;
  2155. }
  2156. if ( ARGUMENT_PRESENT( lpOverlapped ) ) {
  2157. LARGE_INTEGER Li;
  2158. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  2159. Li.LowPart = lpOverlapped->Offset;
  2160. Li.HighPart = lpOverlapped->OffsetHigh;
  2161. Status = NtReadFileScatter(
  2162. hFile,
  2163. lpOverlapped->hEvent,
  2164. NULL,
  2165. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  2166. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  2167. aSegementArray,
  2168. nNumberOfBytesToRead,
  2169. &Li,
  2170. NULL
  2171. );
  2172. if ( NT_SUCCESS(Status) && Status != STATUS_PENDING) {
  2173. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  2174. try {
  2175. *lpNumberOfBytesRead = (DWORD)lpOverlapped->InternalHigh;
  2176. }
  2177. except(EXCEPTION_EXECUTE_HANDLER) {
  2178. *lpNumberOfBytesRead = 0;
  2179. }
  2180. }
  2181. return TRUE;
  2182. }
  2183. else
  2184. if (Status == STATUS_END_OF_FILE) {
  2185. if ( ARGUMENT_PRESENT(lpNumberOfBytesRead) ) {
  2186. *lpNumberOfBytesRead = 0;
  2187. }
  2188. BaseSetLastNTError(Status);
  2189. return FALSE;
  2190. }
  2191. else {
  2192. BaseSetLastNTError(Status);
  2193. return FALSE;
  2194. }
  2195. }
  2196. else
  2197. {
  2198. Status = NtReadFileScatter(
  2199. hFile,
  2200. NULL,
  2201. NULL,
  2202. NULL,
  2203. &IoStatusBlock,
  2204. aSegementArray,
  2205. nNumberOfBytesToRead,
  2206. NULL,
  2207. NULL
  2208. );
  2209. if ( Status == STATUS_PENDING) {
  2210. // Operation must complete before return & IoStatusBlock destroyed
  2211. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  2212. if ( NT_SUCCESS(Status)) {
  2213. Status = IoStatusBlock.Status;
  2214. }
  2215. }
  2216. if ( NT_SUCCESS(Status) ) {
  2217. *lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;
  2218. return TRUE;
  2219. }
  2220. else
  2221. if (Status == STATUS_END_OF_FILE) {
  2222. *lpNumberOfBytesRead = 0;
  2223. return TRUE;
  2224. }
  2225. else {
  2226. if ( NT_WARNING(Status) ) {
  2227. *lpNumberOfBytesRead = (DWORD)IoStatusBlock.Information;
  2228. }
  2229. BaseSetLastNTError(Status);
  2230. return FALSE;
  2231. }
  2232. }
  2233. }
  2234. BOOL
  2235. WINAPI
  2236. WriteFileGather(
  2237. HANDLE hFile,
  2238. FILE_SEGMENT_ELEMENT aSegementArray[],
  2239. DWORD nNumberOfBytesToWrite,
  2240. LPDWORD lpReserved,
  2241. LPOVERLAPPED lpOverlapped
  2242. )
  2243. /*++
  2244. Routine Description:
  2245. Data can be written to a file using WriteFileGather. The data can
  2246. be in multple file segement buffers.
  2247. This API is used to write data to a file. Data is written to the
  2248. file from the position indicated by the file pointer. After the
  2249. write completes, the file pointer is adjusted by the number of bytes
  2250. actually written.
  2251. Unlike DOS, a NumberOfBytesToWrite value of zero does not truncate
  2252. or extend the file. If this function is required, SetEndOfFile
  2253. should be used.
  2254. Arguments:
  2255. hFile - Supplies an open handle to a file that is to be written. The
  2256. file handle must have been created with GENERIC_WRITE access to
  2257. the file.
  2258. aSegementArray - Supplies a pointer an array of virtual segments.
  2259. A virtual segment is a memory buffer where part of the transferred data
  2260. should be placed. Segments are have a fix size of PAGE_SIZE
  2261. and must be aligned on a PAGE_SIZE boundary. The number of
  2262. entries in the array must be equal to nNumberOfBytesToRead /
  2263. PAGE_SIZE.
  2264. nNumberOfBytesToWrite - Supplies the number of bytes to write to the
  2265. file. Unlike DOS, a value of zero is interpreted a null write.
  2266. lpReserved - Unused for now.
  2267. lpOverlapped - Optionally points to an OVERLAPPED structure to be
  2268. used with the request. If NULL then the transfer starts at the
  2269. current file position and WriteFileGather will not return until the
  2270. operation completes.
  2271. If the handle <hFile> was created without specifying
  2272. FILE_FLAG_OVERLAPPED the file pointer is moved to the specified
  2273. offset plus lpNumberOfBytesWritten before WriteFile returns.
  2274. WriteFile will wait for the request to complete before returning
  2275. (it will not set ERROR_IO_PENDING).
  2276. When FILE_FLAG_OVERLAPPED is specified, WriteFile may return
  2277. ERROR_IO_PENDING to allow the calling function to continue processing
  2278. while the operation completes. The event (or hFile if hEvent is NULL) will
  2279. be set to the signalled state upon completion of the request.
  2280. When the handle is created with FILE_FLAG_OVERLAPPED and lpOverlapped
  2281. is set to NULL, WriteFile will return ERROR_INVALID_PARAMTER because
  2282. the file offset is required.
  2283. Return Value:
  2284. TRUE - The operation was a success.
  2285. FALSE - The operation failed. Extended error status is
  2286. available using GetLastError.
  2287. --*/
  2288. {
  2289. NTSTATUS Status;
  2290. IO_STATUS_BLOCK IoStatusBlock;
  2291. LPDWORD lpNumberOfBytesWritten = NULL;
  2292. if ( ARGUMENT_PRESENT(lpReserved) ||
  2293. !ARGUMENT_PRESENT( lpOverlapped )) {
  2294. SetLastError(ERROR_INVALID_PARAMETER);
  2295. return FALSE;
  2296. }
  2297. if (CONSOLE_HANDLE(hFile)) {
  2298. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  2299. return FALSE;
  2300. }
  2301. if ( ARGUMENT_PRESENT(lpNumberOfBytesWritten) ) {
  2302. *lpNumberOfBytesWritten = 0;
  2303. }
  2304. if ( ARGUMENT_PRESENT( lpOverlapped ) ) {
  2305. LARGE_INTEGER Li;
  2306. lpOverlapped->Internal = (DWORD)STATUS_PENDING;
  2307. Li.LowPart = lpOverlapped->Offset;
  2308. Li.HighPart = lpOverlapped->OffsetHigh;
  2309. Status = NtWriteFileGather(
  2310. hFile,
  2311. lpOverlapped->hEvent,
  2312. NULL,
  2313. (ULONG_PTR)lpOverlapped->hEvent & 1 ? NULL : lpOverlapped,
  2314. (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
  2315. aSegementArray,
  2316. nNumberOfBytesToWrite,
  2317. &Li,
  2318. NULL
  2319. );
  2320. if ( !NT_ERROR(Status) && Status != STATUS_PENDING) {
  2321. if ( ARGUMENT_PRESENT(lpNumberOfBytesWritten) ) {
  2322. try {
  2323. *lpNumberOfBytesWritten = (DWORD)lpOverlapped->InternalHigh;
  2324. }
  2325. except(EXCEPTION_EXECUTE_HANDLER) {
  2326. *lpNumberOfBytesWritten = 0;
  2327. }
  2328. }
  2329. return TRUE;
  2330. }
  2331. else {
  2332. BaseSetLastNTError(Status);
  2333. return FALSE;
  2334. }
  2335. }
  2336. else {
  2337. Status = NtWriteFileGather(
  2338. hFile,
  2339. NULL,
  2340. NULL,
  2341. NULL,
  2342. &IoStatusBlock,
  2343. aSegementArray,
  2344. nNumberOfBytesToWrite,
  2345. NULL,
  2346. NULL
  2347. );
  2348. if ( Status == STATUS_PENDING) {
  2349. // Operation must complete before return & IoStatusBlock destroyed
  2350. Status = NtWaitForSingleObject( hFile, FALSE, NULL );
  2351. if ( NT_SUCCESS(Status)) {
  2352. Status = IoStatusBlock.Status;
  2353. }
  2354. }
  2355. if ( NT_SUCCESS(Status)) {
  2356. *lpNumberOfBytesWritten = (DWORD)IoStatusBlock.Information;
  2357. return TRUE;
  2358. }
  2359. else {
  2360. if ( NT_WARNING(Status) ) {
  2361. *lpNumberOfBytesWritten = (DWORD)IoStatusBlock.Information;
  2362. }
  2363. BaseSetLastNTError(Status);
  2364. return FALSE;
  2365. }
  2366. }
  2367. }
  2368. BOOL
  2369. APIENTRY
  2370. SetFileValidData(
  2371. IN HANDLE hFile,
  2372. IN LONGLONG ValidDataLength
  2373. )
  2374. /*++
  2375. Routine Description:
  2376. SetFileValidData is used to set the valid data length for the given file.
  2377. Arguments:
  2378. hFile - Supplies an open handle to a file whose type valid data
  2379. length is to be set
  2380. ValidDataLength - Supplies the desired valid data length
  2381. Return Value:
  2382. TRUE - The operation was a success.
  2383. FALSE - The operation failed. Extended error status is
  2384. available using GetLastError.
  2385. --*/
  2386. {
  2387. NTSTATUS Status;
  2388. IO_STATUS_BLOCK IoStatusBlock;
  2389. FILE_VALID_DATA_LENGTH_INFORMATION ValidDataInfo;
  2390. if (CONSOLE_HANDLE(hFile)) {
  2391. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  2392. return FALSE;
  2393. }
  2394. ValidDataInfo.ValidDataLength.QuadPart = ValidDataLength;
  2395. Status = NtSetInformationFile(
  2396. hFile,
  2397. &IoStatusBlock,
  2398. &ValidDataInfo,
  2399. sizeof(FILE_VALID_DATA_LENGTH_INFORMATION),
  2400. FileValidDataLengthInformation
  2401. );
  2402. if (!NT_SUCCESS(Status)) {
  2403. BaseSetLastNTError(Status);
  2404. return FALSE;
  2405. }
  2406. return TRUE;
  2407. }
  2408. BOOL
  2409. APIENTRY
  2410. SetFileShortNameW(
  2411. IN HANDLE hFile,
  2412. IN LPCWSTR lpShortName
  2413. )
  2414. /*++
  2415. Routine Description:
  2416. SetFileShortNameW is used to set the short name for the given file.
  2417. Arguments:
  2418. hFile - Supplies an open handle to a file whose short name is to be changed
  2419. lpShortName - Supplies the desired short name
  2420. Return Value:
  2421. TRUE - The operation was a success.
  2422. FALSE - The operation failed. Extended error status is
  2423. available using GetLastError.
  2424. --*/
  2425. {
  2426. NTSTATUS Status;
  2427. IO_STATUS_BLOCK IoStatusBlock;
  2428. PFILE_NAME_INFORMATION FileNameInfo;
  2429. DWORD FileNameInfoSize;
  2430. DWORD FileInformationClass;
  2431. if (CONSOLE_HANDLE(hFile)) {
  2432. BaseSetLastNTError(STATUS_INVALID_HANDLE);
  2433. return FALSE;
  2434. }
  2435. if (!ARGUMENT_PRESENT(lpShortName)) {
  2436. SetLastError(ERROR_INVALID_PARAMETER);
  2437. return FALSE;
  2438. }
  2439. FileNameInfoSize = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName) + ((wcslen(lpShortName)+1)*sizeof(WCHAR));
  2440. FileNameInfo = RtlAllocateHeap( RtlProcessHeap(), MAKE_TAG(TMP_TAG), FileNameInfoSize );
  2441. if (!FileNameInfo) {
  2442. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  2443. return FALSE;
  2444. }
  2445. FileNameInfo->FileNameLength = wcslen(lpShortName) * sizeof(WCHAR);
  2446. wcscpy( FileNameInfo->FileName, lpShortName );
  2447. Status = NtSetInformationFile(
  2448. hFile,
  2449. &IoStatusBlock,
  2450. FileNameInfo,
  2451. FileNameInfoSize,
  2452. FileShortNameInformation
  2453. );
  2454. RtlFreeHeap( RtlProcessHeap(), 0, FileNameInfo );
  2455. if (!NT_SUCCESS(Status)) {
  2456. BaseSetLastNTError(Status);
  2457. return FALSE;
  2458. }
  2459. FileInformationClass = FileShortNameInformation;
  2460. if ((FileInformationClass == FileEndOfFileInformation) ||
  2461. (FileInformationClass == FileAllocationInformation) ||
  2462. (FileInformationClass == FilePositionInformation))
  2463. {
  2464. return FALSE;
  2465. }
  2466. return TRUE;
  2467. }
  2468. BOOL
  2469. APIENTRY
  2470. SetFileShortNameA(
  2471. IN HANDLE hFile,
  2472. IN LPCSTR lpShortName
  2473. )
  2474. /*++
  2475. Routine Description:
  2476. SetFileShortNameW is used to set the short name for the given file.
  2477. Arguments:
  2478. hFile - Supplies an open handle to a file whose short name is to be changed
  2479. lpShortName - Supplies the desired short name
  2480. Return Value:
  2481. TRUE - The operation was a success.
  2482. FALSE - The operation failed. Extended error status is
  2483. available using GetLastError.
  2484. --*/
  2485. {
  2486. PUNICODE_STRING Unicode;
  2487. Unicode = Basep8BitStringToStaticUnicodeString( lpShortName );
  2488. if (Unicode == NULL) {
  2489. return FALSE;
  2490. }
  2491. return ( SetFileShortNameW(
  2492. hFile,
  2493. (LPCWSTR)Unicode->Buffer
  2494. )
  2495. );
  2496. }