Skip to content

Commit a24d4de

Browse files
sambacc: generalize dir fd sync'ing - apply it to xattr set
Adapt and continue Shachar's previous patch to ensure that after setting xattrs on the dir we try to commit it to permanent storage. Signed-off-by: John Mulligan <[email protected]>
1 parent dc76109 commit a24d4de

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

sambacc/permissions.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from __future__ import annotations
2020

21+
import contextlib
2122
import datetime
2223
import errno
2324
import logging
@@ -47,6 +48,19 @@ def path(self) -> str:
4748
... # pragma: no cover
4849

4950

51+
@contextlib.contextmanager
52+
def _opendir(path: str) -> typing.Iterator[int]:
53+
dfd: int = os.open(path, os.O_DIRECTORY)
54+
try:
55+
yield dfd
56+
os.fsync(dfd)
57+
except OSError:
58+
os.sync()
59+
raise
60+
finally:
61+
os.close(dfd)
62+
63+
5064
class NoopPermsHandler:
5165
def __init__(
5266
self,
@@ -145,14 +159,8 @@ def _set_perms(self) -> None:
145159
# yeah, this is really simple compared to all the state management
146160
# stuff.
147161
path = self._full_path()
148-
dfd = os.open(path, os.O_DIRECTORY)
149-
try:
162+
with _opendir(path) as dfd:
150163
os.fchmod(dfd, self._mode)
151-
os.fsync(dfd)
152-
except OSError:
153-
os.sync()
154-
finally:
155-
os.close(dfd)
156164

157165
def _timestamp(self) -> str:
158166
return datetime.datetime.now().strftime("%s")
@@ -163,7 +171,8 @@ def _set_status(self) -> None:
163171
val = f"{self._prefix}/{ts}"
164172
path = self._full_path()
165173
_logger.debug("setting xattr %r=%r: %r", self._xattr, val, self._path)
166-
return xattr.set(path, self._xattr, val, nofollow=True)
174+
with _opendir(path) as dfd:
175+
xattr.set(dfd, self._xattr, val, nofollow=True)
167176

168177

169178
class AlwaysPosixPermsHandler(InitPosixPermsHandler):

0 commit comments

Comments
 (0)