def demo():
    """
    Definition of buffer used with FSCTL_TXFS_CREATE_MINIVERSION:
    typedef struct _TXFS_CREATE_MINIVERSION_INFO{
        USHORT StructureVersion;
        USHORT StructureLength;
        ULONG BaseVersion;
        USHORT MiniVersion;}
    """
    buf_fmt='HHLH0L'   ## buffer size must include struct padding
    buf_size=struct.calcsize(buf_fmt)

    tempdir=win32api.GetTempPath()
    tempfile=win32api.GetTempFileName(tempdir,'cft')[0]
    print("Demonstrating transactions on tempfile", tempfile)
    f=open(tempfile,'w')
    f.write('This is original file.\n')
    f.close()

    trans=win32transaction.CreateTransaction(Description='Test creating miniversions of a file')
    hfile=win32file.CreateFileW(tempfile, win32con.GENERIC_READ|win32con.GENERIC_WRITE,
        win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,
        None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans)

    win32file.WriteFile(hfile, str2bytes('This is first miniversion.\n'))
    buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,None,buf_size,None)
    struct_ver, struct_len, base_ver, ver_1=struct.unpack(buf_fmt, buf)

    win32file.SetFilePointer(hfile, 0, win32con.FILE_BEGIN)
    win32file.WriteFile(hfile, str2bytes('This is second miniversion!\n'))
    buf=win32file.DeviceIoControl(hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION,None,buf_size,None)
    struct_ver, struct_len, base_ver, ver_2=struct.unpack(buf_fmt, buf)
    hfile.Close()

    ## miniversions can't be opened with write access
    hfile_0=win32file.CreateFileW(tempfile, win32con.GENERIC_READ,
        win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,
        None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans, MiniVersion=base_ver)
    print('version:',base_ver,win32file.ReadFile(hfile_0, 100))
    hfile_0.Close()

    hfile_1=win32file.CreateFileW(tempfile, win32con.GENERIC_READ,
        win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,
        None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans, MiniVersion=ver_1)
    print('version:',ver_1,win32file.ReadFile(hfile_1, 100))
    hfile_1.Close()

    hfile_2=win32file.CreateFileW(tempfile, win32con.GENERIC_READ,
        win32con.FILE_SHARE_READ|win32con.FILE_SHARE_WRITE,
        None, win32con.OPEN_EXISTING, 0 , None, Transaction=trans, MiniVersion=ver_2)
    print('version:',ver_2,win32file.ReadFile(hfile_2, 100))
    hfile_2.Close()

    ## MiniVersions are destroyed when transaction is committed or rolled back
    win32transaction.CommitTransaction(trans)

    os.unlink(tempfile)
	USHORT StructureVersion;  
	USHORT StructureLength;
	ULONG BaseVersion;
	USHORT MiniVersion;}
"""
buf_fmt = 'HHLH0L'  ## buffer size must include struct padding
buf_size = struct.calcsize(buf_fmt)

tempdir = win32api.GetTempPath()
tempfile = win32api.GetTempFileName(tempdir, 'cft')[0]
print tempfile
f = open(tempfile, 'w')
f.write('This is original file.\n')
f.close()

trans = win32transaction.CreateTransaction(
    Description='Test creating miniversions of a file')
hfile = win32file.CreateFileW(tempfile,
                              win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                              win32con.FILE_SHARE_READ
                              | win32con.FILE_SHARE_WRITE,
                              None,
                              win32con.OPEN_EXISTING,
                              0,
                              None,
                              Transaction=trans)

win32file.WriteFile(hfile, 'This is first miniversion.\n')
buf = win32file.DeviceIoControl(hfile,
                                winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION, '',
                                buf_size, None)
struct_ver, struct_len, base_ver, ver_1 = struct.unpack(buf_fmt, buf)
Пример #3
0
import win32api, win32con, win32transaction

keyname='Pywin32 test transacted registry functions'
subkeyname='test transacted subkey'
classname='Transacted Class'

trans=win32transaction.CreateTransaction(Description='test RegCreateKeyTransacted')
key, disp=win32api.RegCreateKeyEx(win32con.HKEY_CURRENT_USER, keyname,
    samDesired=win32con.KEY_ALL_ACCESS, Class=classname)
## clean up any existing keys
for subk in win32api.RegEnumKeyExW(key):
    win32api.RegDeleteKey(key, subk[0])

## reopen key in transacted mode
transacted_key=win32api.RegOpenKeyTransacted(Key=win32con.HKEY_CURRENT_USER, SubKey=keyname,
    Transaction=trans, samDesired=win32con.KEY_ALL_ACCESS)
subkey, disp=win32api.RegCreateKeyEx(transacted_key, subkeyname, Transaction=trans,
    samDesired=win32con.KEY_ALL_ACCESS, Class=classname)

## Newly created key should not be visible from non-transacted handle
subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
assert subkeyname not in subkeys

transacted_subkeys=[s[0] for s in win32api.RegEnumKeyExW(transacted_key)]
assert subkeyname in transacted_subkeys

## Key should be visible to non-transacted handle after commit
win32transaction.CommitTransaction(trans)
subkeys=[s[0] for s in win32api.RegEnumKeyExW(key)]
assert subkeyname in subkeys