71 lines
2.9 KiB
Python
71 lines
2.9 KiB
Python
"""Reproduce directory rename Win5/32 using only test-owned native handles."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import time
|
|
|
|
import win32con
|
|
import win32file
|
|
import pywintypes
|
|
|
|
|
|
def probe(path, flags):
|
|
try:
|
|
handle = win32file.CreateFile(str(path), win32con.DELETE, 7, None, 3, flags, None)
|
|
except pywintypes.error as error:
|
|
return error.winerror
|
|
handle.Close()
|
|
return 0
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--root', type=Path, required=True)
|
|
args = parser.parse_args()
|
|
root = args.root.resolve()
|
|
root.mkdir(parents=True, exist_ok=False)
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'src'))
|
|
from dx_productivity.trial_task_center.path_access import _sharing_denial, internal_path
|
|
report = {'native_owned_handles_only': True, 'cases': []}
|
|
for kind in ('directory', 'child', 'nested-child'):
|
|
directory = internal_path(root / kind / ('long-' + 'a' * 90) / ('more-' + 'b' * 90))
|
|
directory.mkdir(parents=True)
|
|
child = directory / 'nested' / 'board.prj' if kind == 'nested-child' else directory / 'board.prj'
|
|
child.parent.mkdir(exist_ok=True)
|
|
child.write_text('disposable original bytes', encoding='utf-8')
|
|
held = directory if kind == 'directory' else child
|
|
flags = win32con.FILE_FLAG_BACKUP_SEMANTICS if kind == 'directory' else 0
|
|
handle = win32file.CreateFile(str(held), win32con.GENERIC_READ, 3, None, 3, flags, None)
|
|
destination = directory.with_name('published')
|
|
try:
|
|
try:
|
|
os.replace(directory, destination)
|
|
except OSError as error:
|
|
row = {'kind': kind, 'rename_winerror': error.winerror,
|
|
'directory_attributes': directory.lstat().st_file_attributes,
|
|
'parent_probe_flags0': probe(directory, 0),
|
|
'parent_probe_backup_semantics': probe(directory, win32con.FILE_FLAG_BACKUP_SEMANTICS),
|
|
'held_path_delete_probe': probe(held, flags),
|
|
'confirmed_sharing': _sharing_denial(error, directory)}
|
|
else:
|
|
raise AssertionError('Owned delete-sharing lock did not block rename')
|
|
finally:
|
|
handle.Close()
|
|
time.sleep(.05)
|
|
os.replace(directory, destination)
|
|
row['rename_after_release_succeeded'] = destination.is_dir()
|
|
report['cases'].append(row)
|
|
report['passed'] = all(row['rename_winerror'] == (32 if row['kind'] == 'directory' else 5)
|
|
and row['held_path_delete_probe'] == 32 and row['rename_after_release_succeeded']
|
|
for row in report['cases'])
|
|
(root / 'report.json').write_text(json.dumps(report, indent=2), encoding='utf-8')
|
|
print(json.dumps(report, indent=2))
|
|
if not report['passed']:
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|