Skip to content

Commit

Permalink
files: change implementation of write to avoid race conditions
Browse files Browse the repository at this point in the history
try/excep instead of checking for existence with an if is safer

Signed-off-by: Renan Rodrigo <[email protected]>
  • Loading branch information
renanrodrigo committed Oct 18, 2023
1 parent 3097635 commit c512b13
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion uaclient/files/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ def write(self, content: str):
if self.is_private
else defaults.WORLD_READABLE_MODE
)
if not os.path.exists(self._directory):
# try/except-ing here avoids race conditions the best
try:
if os.path.basename(self._directory) == defaults.PRIVATE_SUBDIR:
os.makedirs(self._directory, mode=0o700)
else:
os.makedirs(self._directory)
except OSError:
pass

system.write_file(self.path, content, file_mode)

def read(self) -> Optional[str]:
Expand Down

0 comments on commit c512b13

Please sign in to comment.