Читаем Windows® Internals, Sixth Edition, Part 2 полностью

In this experiment we’ll use the Transactdemo.exe tool to create a new file, add some data to it as part of a transaction, and see how nontransacted clients interact with the file while the transaction is active. First, open a Command Prompt window and run Transactdemo.exe:C:\>Transactdemo.exe Transaction Demo v1.0 by Mark Russinovich Transaction created: {5CD5E900-9DA8-11DD-8379-005056C00008} Created C:\TransactionDemo.txt. Pass TransDemo the GUID listed above to see the transacted file. Rollback or commit transaction? (r/c):

Transactdemo creates C:\TransactionDemo.txt within a transaction that it has not committed. Open a second Command Prompt window, and use the dir command to look for the presence of the TransactionDemo.txt file:C:\>dir transactiondemo.txt Volume in drive C is OS Volume Serial Number is 0C30-686E Directory of C:\ File Not Found

According to this second command prompt, the file doesn’t even exist. Now simulate a nontransacted writer by trying to add data to the file via the echo command:C:\>echo Hello > TransactionDemo.txt The function attempted to use a name that is reserved for use by another transaction.

As expected, nontransacted writers are blocked from modifying the file.

The %SystemRoot%\System32\Ktmutil.exe and %SystemRoot%\System32\Fsutil.exe built-in applications can be very useful for dealing with transactional operations on the file system. For example, you can get a list of all current transactions on the system with the following command:C:\>ktmutil tx list TxGuid Description -------------------------------------- ----------------------------------------------- {5cd5e900-9da8-11dd-8379-005056c00008} Demo Transaction?

Note that the GUID matches what Transactdemo returned. With the GUID, you can now use the Fsutil command to query information about the transaction and to commit it or roll it back. For example, here’s how to list the files part of the transaction and the owner account:C:\>fsutil transaction query all {5cd5e900-9da8-11dd-8379-005056c00008} dwOutcome: 1 dwIsolationLevel: 0 dwIsolationFlags: 0 dwTimeout: -1 Owner: BUILTIN\Administrators Number of Files: 1 ---- \TransactionDemo.txt

Although the Transactdemo tool presents you with the option to roll back or commit the current transaction, the Fsutil utility allows commits or rollbacks to any ongoing transaction your account has access to. Go back to the command prompt where you ran Transactdemo and press C to commit the transaction, after which the file becomes a standard nontransacted file.

Transactional APIs

TxF implements transacted versions of the Windows file I/O APIs, which use the suffix Transacted:

Create APIsCreateDirectoryTransacted, CreateFileTransacted, CreateHardLinkTransacted, CreateSymbolicLinkTransacted

Find APIsFindFirstFileNameTransacted, FindFirstFileTransacted, FindFirstStreamTransacted

Query APIsGetCompressedFileSizeTransacted, GetFileAttributesTransacted, GetFullPathNameTransacted, GetLongPathNameTransacted

Delete APIsDeleteFileTransacted, RemoveDirectoryTransacted

Copy and Move/Rename APIsCopyFileTransacted, MoveFileTransacted

Set APIsSetFileAttributesTransacted

In addition, some APIs automatically participate in transacted operations when the file handle they are passed is part of a transaction, like one created by the CreateFileTransacted API. Table 12-9 lists Windows APIs that have modified behavior when dealing with a transacted file handle.

Table 12-9. API Behavior Changed by TxF

API Name

Change

CloseHandle

Transactions will not be committed until all applications close transacted handles to the file.

CreateFileMapping, MapViewOfFile

Modifications to mapped views of a file part of a transaction will be associated with the transaction themselves.

FindNextFile, ReadDirectoryChanges, GetInformationByHandle, GetFileSize

If the file handle is part of a transaction, read-isolation rules will be applied to these operations.

GetVolumeInformation

Function will return FILE_SUPPORTS_TRANSACTIONS if the volume supports TxF.

ReadFile, WriteFile

Read and write operations to a transacted file handle will be part of the transaction.

SetFileInformationByHandle

Перейти на страницу:

Похожие книги