Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

CreateFileTransacted_MiniVersion.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. This demonstrates the creation of miniversions of a file during a transaction.
  3. The FSCTL_TXFS_CREATE_MINIVERSION control code saves any changes to a new
  4. miniversion (effectively a savepoint within a transaction).
  5. """
  6. import os
  7. import struct
  8. import win32api
  9. import win32con
  10. import win32file
  11. import win32transaction
  12. import winerror
  13. import winioctlcon
  14. from pywin32_testutil import str2bytes # py3k-friendly helper
  15. def demo():
  16. """
  17. Definition of buffer used with FSCTL_TXFS_CREATE_MINIVERSION:
  18. typedef struct _TXFS_CREATE_MINIVERSION_INFO{
  19. USHORT StructureVersion;
  20. USHORT StructureLength;
  21. ULONG BaseVersion;
  22. USHORT MiniVersion;}
  23. """
  24. buf_fmt = "HHLH0L" ## buffer size must include struct padding
  25. buf_size = struct.calcsize(buf_fmt)
  26. tempdir = win32api.GetTempPath()
  27. tempfile = win32api.GetTempFileName(tempdir, "cft")[0]
  28. print("Demonstrating transactions on tempfile", tempfile)
  29. f = open(tempfile, "w")
  30. f.write("This is original file.\n")
  31. f.close()
  32. trans = win32transaction.CreateTransaction(
  33. Description="Test creating miniversions of a file"
  34. )
  35. hfile = win32file.CreateFileW(
  36. tempfile,
  37. win32con.GENERIC_READ | win32con.GENERIC_WRITE,
  38. win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  39. None,
  40. win32con.OPEN_EXISTING,
  41. 0,
  42. None,
  43. Transaction=trans,
  44. )
  45. win32file.WriteFile(hfile, str2bytes("This is first miniversion.\n"))
  46. buf = win32file.DeviceIoControl(
  47. hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION, None, buf_size, None
  48. )
  49. struct_ver, struct_len, base_ver, ver_1 = struct.unpack(buf_fmt, buf)
  50. win32file.SetFilePointer(hfile, 0, win32con.FILE_BEGIN)
  51. win32file.WriteFile(hfile, str2bytes("This is second miniversion!\n"))
  52. buf = win32file.DeviceIoControl(
  53. hfile, winioctlcon.FSCTL_TXFS_CREATE_MINIVERSION, None, buf_size, None
  54. )
  55. struct_ver, struct_len, base_ver, ver_2 = struct.unpack(buf_fmt, buf)
  56. hfile.Close()
  57. ## miniversions can't be opened with write access
  58. hfile_0 = win32file.CreateFileW(
  59. tempfile,
  60. win32con.GENERIC_READ,
  61. win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  62. None,
  63. win32con.OPEN_EXISTING,
  64. 0,
  65. None,
  66. Transaction=trans,
  67. MiniVersion=base_ver,
  68. )
  69. print("version:", base_ver, win32file.ReadFile(hfile_0, 100))
  70. hfile_0.Close()
  71. hfile_1 = win32file.CreateFileW(
  72. tempfile,
  73. win32con.GENERIC_READ,
  74. win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  75. None,
  76. win32con.OPEN_EXISTING,
  77. 0,
  78. None,
  79. Transaction=trans,
  80. MiniVersion=ver_1,
  81. )
  82. print("version:", ver_1, win32file.ReadFile(hfile_1, 100))
  83. hfile_1.Close()
  84. hfile_2 = win32file.CreateFileW(
  85. tempfile,
  86. win32con.GENERIC_READ,
  87. win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  88. None,
  89. win32con.OPEN_EXISTING,
  90. 0,
  91. None,
  92. Transaction=trans,
  93. MiniVersion=ver_2,
  94. )
  95. print("version:", ver_2, win32file.ReadFile(hfile_2, 100))
  96. hfile_2.Close()
  97. ## MiniVersions are destroyed when transaction is committed or rolled back
  98. win32transaction.CommitTransaction(trans)
  99. os.unlink(tempfile)
  100. if __name__ == "__main__":
  101. # When run on CI, this fails with NOT_SUPPORTED, so don't have that cause "failure"
  102. try:
  103. demo()
  104. except win32file.error as e:
  105. if e.winerror == winerror.ERROR_NOT_SUPPORTED:
  106. print("These features are not supported by this filesystem.")
  107. else:
  108. raise