MCPcopy Create free account
hub / github.com/tox-dev/filelock / hold

Function hold

tests/conftest.py:362–390  ·  view source on GitHub ↗
(path: Path, *, read_handle: bool = False)

Source from the content-addressed store, hash-verified

360 answers: list[ctypes.Array[ctypes.c_char]] = [] # Windows writes one when its request completes
361
362 def hold(path: Path, *, read_handle: bool = False) -> Callable[[], None]:
363 generic_read, share_all, open_existing, flag_overlapped = 0x80000000, 0x1 | 0x2 | 0x4, 3, 0x40000000
364 handle = kernel32.CreateFileW(str(path), generic_read, share_all, None, open_existing, flag_overlapped, None)
365 overlapped = Overlapped(hEvent=kernel32.CreateEventW(None, True, False, None))
366 # FSCTL_REQUEST_OPLOCK_LEVEL_1 stays pending while granted; a read handle suffices to make other opens wait.
367 request: tuple[int, bytes | None, int, ctypes.Array[ctypes.c_char] | None, int] = (0x00090000, None, 0, None, 0)
368 if read_handle:
369 # FSCTL_REQUEST_OPLOCK with a version 1 REQUEST_OPLOCK_INPUT_BUFFER of 12 bytes that requests
370 # OPLOCK_LEVEL_CACHE_READ | OPLOCK_LEVEL_CACHE_HANDLE. Opens that share everything leave this oplock alone,
371 # and a rename waits for its holder. The answer is a REQUEST_OPLOCK_OUTPUT_BUFFER.
372 asked = struct.pack("<HHII", 1, 12, 0x1 | 0x2, 0x1)
373 answers.append(answer := ctypes.create_string_buffer(24))
374 request = (0x00090240, asked, len(asked), answer, len(answer))
375 assert not kernel32.DeviceIoControl(handle, *request, None, ctypes.byref(overlapped))
376 assert ctypes.get_last_error() == 997 # ERROR_IO_PENDING
377 closed = False
378
379 def close() -> None:
380 nonlocal closed
381 if closed:
382 return
383 closed = True
384 kernel32.CloseHandle(handle)
385 # The request completes on close and writes overlapped, so wait before freeing it.
386 kernel32.WaitForSingleObject(overlapped.hEvent, 5000)
387 kernel32.CloseHandle(overlapped.hEvent)
388
389 closers.append(close)
390 return close
391
392 yield hold
393 for close in closers:

Callers

nothing calls this directly

Calls 1

OverlappedClass · 0.85

Tested by

no test coverage detected