mirror of
https://github.com/certbot/certbot.git
synced 2026-07-30 16:14:44 +02:00
Refactor le_util, 100% cover
This commit is contained in:
+32
-32
@@ -54,8 +54,25 @@ def check_permissions(filepath, mode, uid=0):
|
|||||||
return stat.S_IMODE(file_stat.st_mode) == mode and file_stat.st_uid == uid
|
return stat.S_IMODE(file_stat.st_mode) == mode and file_stat.st_uid == uid
|
||||||
|
|
||||||
|
|
||||||
|
def _safely_attempt_open(fname, mode):
|
||||||
|
file_d = os.open(fname, os.O_CREAT | os.O_EXCL | os.O_RDWR, mode)
|
||||||
|
return os.fdopen(file_d, "w"), fname
|
||||||
|
|
||||||
|
|
||||||
|
def _unique_file(path, filename_pat, count, mode):
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
return _safely_attempt_open(
|
||||||
|
os.path.join(path, filename_pat(count)), mode)
|
||||||
|
except OSError as err:
|
||||||
|
# "File exists," is okay, try a different name.
|
||||||
|
if err.errno != errno.EEXIST:
|
||||||
|
raise
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
|
||||||
def unique_file(path, mode=0o777):
|
def unique_file(path, mode=0o777):
|
||||||
"""Safely finds a unique file for writing only (by default).
|
"""Safely finds a unique file.
|
||||||
|
|
||||||
:param str path: path/filename.ext
|
:param str path: path/filename.ext
|
||||||
:param int mode: File mode
|
:param int mode: File mode
|
||||||
@@ -64,52 +81,35 @@ def unique_file(path, mode=0o777):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
path, tail = os.path.split(path)
|
path, tail = os.path.split(path)
|
||||||
count = 0
|
return _unique_file(
|
||||||
while True:
|
path, filename_pat=(lambda count: "%04d_%s" % (count, tail)),
|
||||||
fname = os.path.join(path, "%04d_%s" % (count, tail))
|
count=0, mode=mode)
|
||||||
try:
|
|
||||||
file_d = os.open(fname, os.O_CREAT | os.O_EXCL | os.O_RDWR, mode)
|
|
||||||
return os.fdopen(file_d, "w"), fname
|
|
||||||
except OSError as exception:
|
|
||||||
# "File exists," is okay, try a different name.
|
|
||||||
if exception.errno != errno.EEXIST:
|
|
||||||
raise
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
|
|
||||||
def unique_lineage_name(path, filename, mode=0o777):
|
def unique_lineage_name(path, filename, mode=0o777):
|
||||||
"""Safely finds a unique file for writing only (by default). Uses a
|
"""Safely finds a unique file using lineage convention.
|
||||||
file lineage convention.
|
|
||||||
|
|
||||||
:param str path: directory path
|
:param str path: directory path
|
||||||
:param str filename: proposed filename
|
:param str filename: proposed filename
|
||||||
:param int mode: file mode
|
:param int mode: file mode
|
||||||
|
|
||||||
:returns: tuple of file object and file name (which may be modified from
|
:returns: tuple of file object and file name (which may be modified
|
||||||
the requested one by appending digits to ensure uniqueness)
|
from the requested one by appending digits to ensure uniqueness)
|
||||||
|
|
||||||
:raises OSError: if writing files fails for an unanticipated reason,
|
:raises OSError: if writing files fails for an unanticipated reason,
|
||||||
such as a full disk or a lack of permission to write to specified
|
such as a full disk or a lack of permission to write to
|
||||||
location.
|
specified location.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
fname = os.path.join(path, "%s.conf" % (filename))
|
|
||||||
try:
|
try:
|
||||||
file_d = os.open(fname, os.O_CREAT | os.O_EXCL | os.O_RDWR, mode)
|
return _safely_attempt_open(
|
||||||
return os.fdopen(file_d, "w"), fname
|
os.path.join(path, "%s.conf" % (filename)), mode=mode)
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
if err.errno != errno.EEXIST:
|
if err.errno != errno.EEXIST:
|
||||||
raise err
|
raise
|
||||||
count = 1
|
return _unique_file(
|
||||||
while True:
|
path, filename_pat=(lambda count: "%s-%04d.conf" % (filename, count)),
|
||||||
fname = os.path.join(path, "%s-%04d.conf" % (filename, count))
|
count=1, mode=mode)
|
||||||
try:
|
|
||||||
file_d = os.open(fname, os.O_CREAT | os.O_EXCL | os.O_RDWR, mode)
|
|
||||||
return os.fdopen(file_d, "w"), fname
|
|
||||||
except OSError as err:
|
|
||||||
if err.errno != errno.EEXIST:
|
|
||||||
raise err
|
|
||||||
count += 1
|
|
||||||
|
|
||||||
|
|
||||||
def safely_remove(path):
|
def safely_remove(path):
|
||||||
|
|||||||
Reference in New Issue
Block a user