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.

BackupRead_BackupWrite.py 3.7KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ## demonstrates using BackupRead and BackupWrite to copy all of a file's data streams
  2. import ntsecuritycon
  3. import pythoncom
  4. import pywintypes
  5. import win32api
  6. import win32con
  7. import win32file
  8. import win32security
  9. from pywin32_testutil import ob2memory, str2bytes
  10. from win32com import storagecon
  11. all_sd_info = (
  12. win32security.DACL_SECURITY_INFORMATION
  13. | win32security.DACL_SECURITY_INFORMATION
  14. | win32security.OWNER_SECURITY_INFORMATION
  15. | win32security.GROUP_SECURITY_INFORMATION
  16. )
  17. tempdir = win32api.GetTempPath()
  18. tempfile = win32api.GetTempFileName(tempdir, "bkr")[0]
  19. outfile = win32api.GetTempFileName(tempdir, "out")[0]
  20. print("Filename:", tempfile, "Output file:", outfile)
  21. f = open(tempfile, "w")
  22. f.write("some random junk" + "x" * 100)
  23. f.close()
  24. ## add a couple of alternate data streams
  25. f = open(tempfile + ":streamdata", "w")
  26. f.write("data written to alternate stream" + "y" * 100)
  27. f.close()
  28. f = open(tempfile + ":anotherstream", "w")
  29. f.write("z" * 100)
  30. f.close()
  31. ## add Summary Information, which is stored as a separate stream
  32. m = storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE | storagecon.STGM_DIRECT
  33. pss = pythoncom.StgOpenStorageEx(
  34. tempfile, m, storagecon.STGFMT_FILE, 0, pythoncom.IID_IPropertySetStorage, None
  35. )
  36. ps = pss.Create(
  37. pythoncom.FMTID_SummaryInformation,
  38. pythoncom.IID_IPropertyStorage,
  39. 0,
  40. storagecon.STGM_READWRITE | storagecon.STGM_SHARE_EXCLUSIVE,
  41. )
  42. ps.WriteMultiple(
  43. (storagecon.PIDSI_KEYWORDS, storagecon.PIDSI_COMMENTS), ("keywords", "comments")
  44. )
  45. ps = None
  46. pss = None
  47. ## add a custom security descriptor to make sure we don't
  48. ## get a default that would always be the same for both files in temp dir
  49. new_sd = pywintypes.SECURITY_DESCRIPTOR()
  50. sid = win32security.LookupAccountName("", "EveryOne")[0]
  51. acl = pywintypes.ACL()
  52. acl.AddAccessAllowedAce(1, win32con.GENERIC_READ, sid)
  53. acl.AddAccessAllowedAce(1, ntsecuritycon.FILE_APPEND_DATA, sid)
  54. acl.AddAccessAllowedAce(1, win32con.GENERIC_WRITE, sid)
  55. acl.AddAccessAllowedAce(1, ntsecuritycon.FILE_ALL_ACCESS, sid)
  56. new_sd.SetSecurityDescriptorDacl(True, acl, False)
  57. win32security.SetFileSecurity(tempfile, win32security.DACL_SECURITY_INFORMATION, new_sd)
  58. sa = pywintypes.SECURITY_ATTRIBUTES()
  59. sa.bInheritHandle = True
  60. h = win32file.CreateFile(
  61. tempfile,
  62. win32con.GENERIC_ALL,
  63. win32con.FILE_SHARE_READ,
  64. sa,
  65. win32con.OPEN_EXISTING,
  66. win32file.FILE_FLAG_BACKUP_SEMANTICS,
  67. None,
  68. )
  69. outh = win32file.CreateFile(
  70. outfile,
  71. win32con.GENERIC_ALL,
  72. win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
  73. sa,
  74. win32con.OPEN_EXISTING,
  75. win32file.FILE_FLAG_BACKUP_SEMANTICS,
  76. None,
  77. )
  78. ctxt = 0
  79. outctxt = 0
  80. buf = None
  81. readsize = 100
  82. while 1:
  83. bytes_read, buf, ctxt = win32file.BackupRead(h, readsize, buf, False, True, ctxt)
  84. if bytes_read == 0:
  85. break
  86. bytes_written, outctxt = win32file.BackupWrite(
  87. outh, bytes_read, buf, False, True, outctxt
  88. )
  89. print("Written:", bytes_written, "Context:", outctxt)
  90. win32file.BackupRead(h, 0, buf, True, True, ctxt)
  91. win32file.BackupWrite(outh, 0, str2bytes(""), True, True, outctxt)
  92. win32file.CloseHandle(h)
  93. win32file.CloseHandle(outh)
  94. assert open(tempfile).read() == open(outfile).read(), "File contents differ !"
  95. assert (
  96. open(tempfile + ":streamdata").read() == open(outfile + ":streamdata").read()
  97. ), "streamdata contents differ !"
  98. assert (
  99. open(tempfile + ":anotherstream").read() == open(outfile + ":anotherstream").read()
  100. ), "anotherstream contents differ !"
  101. assert (
  102. ob2memory(win32security.GetFileSecurity(tempfile, all_sd_info))[:]
  103. == ob2memory(win32security.GetFileSecurity(outfile, all_sd_info))[:]
  104. ), "Security descriptors are different !"
  105. ## also should check Summary Info programatically