def secure_file(fname, remove_existing=False):
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL # Refer to "man 2 open".
mode = stat.S_IRUSR | stat.S_IWUSR # This is 0o600 in octal and 384 in decimal.
if remove_existing:
# For security, remove file with potentially elevated mode
try:
os.remove(fname)
except OSError:
pass
# Open file descriptor
umask_original = os.umask(0)
try:
fdesc = os.open(fname, flags, mode)
finally:
os.umask(umask_original)
return fdesc
评论列表
文章目录