|
6 | 6 | from contextlib import suppress |
7 | 7 | import logging |
8 | 8 | from os import ( |
9 | | - O_RDONLY as os_O_RDONLY, |
10 | | - close as os_close, |
11 | | - fsync as os_fsync, |
12 | 9 | getenv as os_getenv, |
13 | 10 | getpid as os_getpid, |
14 | 11 | name as os_name, |
15 | | - open as os_open, |
16 | 12 | ) |
17 | 13 | from os.path import expanduser as os_path_expand_user, join as os_path_join |
18 | 14 | from pathlib import Path |
|
30 | 26 | _LOGGER = logging.getLogger(__name__) |
31 | 27 |
|
32 | 28 |
|
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 | 29 | class PlugwiseCache: |
43 | 30 | """Base class to cache plugwise information.""" |
44 | 31 |
|
@@ -144,25 +131,12 @@ async def write_cache(self, data: dict[str, str], rewrite: bool = False) -> None |
144 | 131 | newline="\n", |
145 | 132 | ) as temp_file: |
146 | 133 | await temp_file.writelines(data_to_write) |
| 134 | + # Ensure buffered data is written |
147 | 135 | 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 | 136 |
|
155 | 137 | # Atomic rename (overwrites atomically on all platforms) |
156 | 138 | temp_path.replace(cache_file_path) |
157 | 139 | 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 | 140 |
|
167 | 141 | if not self._cache_file_exists: |
168 | 142 | self._cache_file_exists = True |
|
0 commit comments