[entropy.const] move const_file_readable logic to _const_file_something

This commit is contained in:
Fabio Erculiani
2013-09-17 14:17:33 +02:00
parent 689e96e06f
commit 0c5abdce77
+16 -3
View File
@@ -1101,18 +1101,20 @@ def const_set_chmod(myfile, chmod):
if cur_mod != oct(chmod):
os.chmod(myfile, chmod)
def const_file_readable(path):
def _const_file_something(path, flags):
"""
Return whether path points to a readable file.
Return whether file can be opened and is a regular one.
@param path: path to a file
@type path: string
@param flags: os.open() flags
@type flags: int
@return: True, if file exists and is readable
@rtype: bool
"""
fd = None
try:
fd = os.open(path, os.O_RDONLY)
fd = os.open(path, flags)
mode = os.fstat(fd).st_mode
if stat.S_ISREG(mode):
# this also handles symlinks to files
@@ -1125,6 +1127,17 @@ def const_file_readable(path):
if fd is not None:
os.close(fd)
def const_file_readable(path):
"""
Return whether path points to a readable file.
@param path: path to a file
@type path: string
@return: True, if file exists and is readable
@rtype: bool
"""
return _const_file_something(path, os.O_RDONLY)
def const_dir_readable(dir_path):
"""
Return whether path points to a readable directory.