Skip to content

Commit

Permalink
fixup! Convert codebase to pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhoyer committed May 20, 2024
1 parent 5586886 commit 075b7e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
10 changes: 5 additions & 5 deletions rtslib/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ def _list_files(self, path, writable=None, readable=None):
return []

if writable is None and readable is None:
names = sorted(path.iterdir())
names = [p.name for p in path.glob('*') if p.is_file()]
else:
names = []
for name in path.iterdir():
if name.is_file():
sres = Path.stat(name)
for p in path.iterdir():
if p.is_file():
sres = Path.stat(p)
if (writable is not None and
writable != ((sres[stat.ST_MODE] & stat.S_IWUSR) == stat.S_IWUSR)):
continue
if (readable is not None and
readable != ((sres[stat.ST_MODE] & stat.S_IRUSR) == stat.S_IRUSR)):
continue
names.append(name)
names.append(p.name)

return sorted(names)

Expand Down
16 changes: 6 additions & 10 deletions rtslib/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,25 +455,21 @@ def save_to_file(self, save_file=None, so_path=None):
umask = 0o777 ^ mode # Prevents always downgrading umask to 0

# For security, remove file with potentially elevated mode
with suppress(OSError):
tmp_file.unlink()
tmp_file.unlink(missing_ok=True)

original_umask = os.umask(umask)
# Even though the old file is first deleted, a race condition is still
# possible. mode='x' opens the file for exclusive creation,
# failing if the file already exists
try:
with tmp_file.open(mode="x"):
pass
with tmp_file.open(mode="x") as f:
json.dump(saveconf, f, sort_keys=True, indent=2)
f.write("\n")
f.flush()
os.fsync(f.fileno())
except OSError as e:
raise RuntimeError(f"Could not open {tmp_file}") from e

with tmp_file.open(mode="w") as f:
json.dump(saveconf, f, sort_keys=True, indent=2)
f.write("\n")
f.flush()
os.fsync(f.fileno())

# move along with permissions
tmp_file.replace(save_file)
save_file.chmod(mode)
Expand Down

0 comments on commit 075b7e6

Please sign in to comment.