island: Use `Path().touch()` instead of `os.open()` when securely creating a file on Linux

This commit is contained in:
Shreya 2021-06-15 15:51:02 +05:30
parent e01165403a
commit e90bf52674
1 changed files with 2 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import logging
import os
import platform
import stat
from pathlib import Path
LOG = logging.getLogger(__name__)
@ -64,11 +65,8 @@ def create_secure_file(path: str):
def _create_secure_file_linux(path: str):
try:
flags = (
os.O_RDWR | os.O_CREAT | os.O_EXCL
) # read/write, create new, throw error if file exists
mode = stat.S_IRWXU # read/write/execute permissions to owner
os.close(os.open(path, flags, mode))
Path(path).touch(mode=mode, exist_ok=False)
except Exception as ex:
LOG.error(f'Could not create a file at "{path}": {str(ex)}')