fixed is_audio_file to handle extensionless files. made sure is_user_readable closes opened filehandle

This commit is contained in:
Rudi Grinberg 2012-07-05 15:34:53 -04:00
parent 1916ea8b0e
commit 7f4e6f98f5
1 changed files with 4 additions and 6 deletions

View File

@ -43,7 +43,6 @@ class MediaMonitorCommon:
def is_temp_file(self, filename): def is_temp_file(self, filename):
info = filename.split(".") info = filename.split(".")
# if file doesn't have any extension, info[-2] throws exception # if file doesn't have any extension, info[-2] throws exception
# Hence, checking length of info before we do anything # Hence, checking length of info before we do anything
if(len(info) >= 2): if(len(info) >= 2):
@ -53,20 +52,19 @@ class MediaMonitorCommon:
def is_audio_file(self, filename): def is_audio_file(self, filename):
info = filename.split(".") info = filename.split(".")
if len(info) < 2: return false # handle cases like filename="mp3"
return info[-1].lower() in self.supported_file_formats return info[-1].lower() in self.supported_file_formats
#check if file is readable by "nobody" #check if file is readable by "nobody"
def is_user_readable(self, filepath, euid='nobody', egid='nogroup'): def is_user_readable(self, filepath, euid='nobody', egid='nogroup'):
f = None
try: try:
uid = pwd.getpwnam(euid)[2] uid = pwd.getpwnam(euid)[2]
gid = grp.getgrnam(egid)[2] gid = grp.getgrnam(egid)[2]
#drop root permissions and become "nobody" #drop root permissions and become "nobody"
os.setegid(gid) os.setegid(gid)
os.seteuid(uid) os.seteuid(uid)
f = open(filepath)
open(filepath)
readable = True readable = True
except IOError: except IOError:
self.logger.warn("File does not have correct permissions: '%s'", filepath) self.logger.warn("File does not have correct permissions: '%s'", filepath)
@ -77,9 +75,9 @@ class MediaMonitorCommon:
self.logger.error("traceback: %s", traceback.format_exc()) self.logger.error("traceback: %s", traceback.format_exc())
finally: finally:
#reset effective user to root #reset effective user to root
if f: f.close()
os.seteuid(0) os.seteuid(0)
os.setegid(0) os.setegid(0)
return readable return readable
# the function only changes the permission if its not readable by www-data # the function only changes the permission if its not readable by www-data