|
5 | 5 | from asyncio import get_running_loop |
6 | 6 | from contextlib import suppress |
7 | 7 | import logging |
8 | | -from os import ( |
9 | | - O_RDONLY as os_O_RDONLY, |
10 | | - close as os_close, |
11 | | - fsync as os_fsync, |
12 | | - getenv as os_getenv, |
13 | | - getpid as os_getpid, |
14 | | - name as os_name, |
15 | | - open as os_open, |
16 | | -) |
| 8 | +from os import getenv as os_getenv, getpid as os_getpid, name as os_name |
17 | 9 | from os.path import expanduser as os_path_expand_user, join as os_path_join |
18 | 10 | from pathlib import Path |
19 | 11 | from secrets import token_hex as secrets_token_hex |
|
30 | 22 | _LOGGER = logging.getLogger(__name__) |
31 | 23 |
|
32 | 24 |
|
33 | | -def _fsync_parent_dir(path: Path) -> None: |
34 | | - """Ensure persistence on POSIX.""" |
35 | | - fd = os_open(str(path), os_O_RDONLY) |
36 | | - try: |
37 | | - os_fsync(fd) |
38 | | - finally: |
39 | | - os_close(fd) |
40 | | - |
41 | | - |
42 | 25 | class PlugwiseCache: |
43 | 26 | """Base class to cache plugwise information.""" |
44 | 27 |
|
@@ -144,25 +127,12 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None |
144 | 127 | newline="\n", |
145 | 128 | ) as temp_file: |
146 | 129 | await temp_file.writelines(data_to_write) |
| 130 | + # Ensure buffered data is written |
147 | 131 | await temp_file.flush() |
148 | | - # Ensure data reaches disk before rename |
149 | | - with suppress(OSError, TypeError, AttributeError): |
150 | | - # If fsync fails due to fileno() issues or other problems, |
151 | | - # continue without it. Flush() provides reasonable durability. |
152 | | - loop = get_running_loop() |
153 | | - await loop.run_in_executor(None, os_fsync, temp_file.fileno()) |
154 | 132 |
|
155 | 133 | # Atomic rename (overwrites atomically on all platforms) |
156 | 134 | temp_path.replace(cache_file_path) |
157 | 135 | temp_path = None # Successfully renamed |
158 | | - if os_name != "nt": |
159 | | - # Ensure directory entry is persisted on POSIX |
160 | | - with suppress(OSError, PermissionError): |
161 | | - # Directory fsync may fail on some filesystems |
162 | | - # The atomic replace is still complete |
163 | | - await loop.run_in_executor( |
164 | | - None, _fsync_parent_dir, cache_file_path.parent |
165 | | - ) |
166 | 136 |
|
167 | 137 | if not self._cache_file_exists: |
168 | 138 | self._cache_file_exists = True |
|
0 commit comments