Skip to content

Commit

Permalink
chore: 関数の形を変更
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba committed Jan 15, 2025
1 parent 346c2d6 commit 1bd3d55
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions voicevox_engine/user_dict/user_dict_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,42 @@ def func(*args: Any, **kw: Any) -> Any:

_save_format_dict_adapter = TypeAdapter(dict[str, SaveFormatUserDictWord])

if sys.platform == "win32":
import ctypes
from ctypes.wintypes import DWORD, HANDLE, LPCWSTR

_CreateFileW = ctypes.windll.kernel32.CreateFileW
_CreateFileW.argtypes = [
LPCWSTR,
DWORD,
DWORD,
ctypes.c_void_p,
DWORD,
DWORD,
HANDLE,
]
_CreateFileW.restype = HANDLE
_CloseHandle = ctypes.windll.kernel32.CloseHandle
_CloseHandle.argtypes = [HANDLE]

_FILE_SHARE_DELETE = 0x00000004
_FILE_SHARE_READ = 0x00000001
_OPEN_EXISTING = 3
_FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
_INVALID_HANDLE_VALUE = HANDLE(-1).value

def _unlink_file_on_close(filename: str) -> None:
"""
CreateFileW関数で`FILE_FLAG_DELETE_ON_CLOSE`付けてすぐに閉じる
これにより`FILE_SHARE_DELETE`を付けて開かれているファイルのハンドルが
全て閉じた時に自動的に削除することができる
"""

def _delete_file_on_close(file_path: Path) -> None:
"""
ファイルのハンドルが全て閉じたときにファイルを削除する。OpenJTalk用のカスタム辞書用。
WindowsではCreateFileW関数で`FILE_FLAG_DELETE_ON_CLOSE`を付けてすぐに閉じることで、
`FILE_SHARE_DELETE`を付けて開かれているファイルのハンドルが全て閉じた時に削除されるようにする。
Windows以外では即座にファイルを削除する。
"""
if sys.platform == "win32":
import ctypes
from ctypes.wintypes import DWORD, HANDLE, LPCWSTR

_CreateFileW = ctypes.windll.kernel32.CreateFileW
_CreateFileW.argtypes = [
LPCWSTR,
DWORD,
DWORD,
ctypes.c_void_p,
DWORD,
DWORD,
HANDLE,
]
_CreateFileW.restype = HANDLE
_CloseHandle = ctypes.windll.kernel32.CloseHandle
_CloseHandle.argtypes = [HANDLE]

_FILE_SHARE_DELETE = 0x00000004
_FILE_SHARE_READ = 0x00000001
_OPEN_EXISTING = 3
_FILE_FLAG_DELETE_ON_CLOSE = 0x04000000
_INVALID_HANDLE_VALUE = HANDLE(-1).value

h_file = _CreateFileW(
filename,
str(file_path),
0,
_FILE_SHARE_DELETE | _FILE_SHARE_READ,
None,
Expand All @@ -99,12 +103,13 @@ def _unlink_file_on_close(filename: str) -> None:
None,
)
if h_file == _INVALID_HANDLE_VALUE:
error = ctypes.WinError()
error.filename = filename
raise error
raise RuntimeError(f"Failed to CreateFileW for {file_path}")

result = _CloseHandle(h_file)
if result == 0:
raise ctypes.WinError()
raise RuntimeError(f"Failed to CloseHandle for {file_path}")
else:
file_path.unlink()


class UserDictionary:
Expand Down Expand Up @@ -211,10 +216,7 @@ def update_dict(self) -> None:
if tmp_csv_path.exists():
tmp_csv_path.unlink()
if tmp_compiled_path.exists():
if sys.platform == "win32":
_unlink_file_on_close(str(tmp_compiled_path))
else:
tmp_compiled_path.unlink()
_delete_file_on_close(tmp_compiled_path)

@mutex_wrapper(mutex_user_dict)
def read_dict(self) -> dict[str, UserDictWord]:
Expand Down

0 comments on commit 1bd3d55

Please sign in to comment.