- Catch IOError when file can not be read

- Do not convert line to latin-1 but to utf8

git-svn-id: https://fail2ban.svn.sourceforge.net/svnroot/fail2ban/trunk@374 a942ae1a-1317-0410-a47c-b1dcaea8d605
0.x
Cyril Jaquier 2006-09-20 22:39:16 +00:00
parent f12d0fab61
commit 3ca397922d
1 changed files with 15 additions and 5 deletions

View File

@ -276,8 +276,14 @@ class Filter(JailThread):
try:
self.crtFilename = filename
self.crtHandler = open(filename)
logSys.debug("Opened " + filename)
return True
except OSError:
logSys.error("Unable to open " + filename)
except IOError:
logSys.error("Unable to read " + filename +
". Please check permissions")
return False
##
# Close the log file.
@ -321,18 +327,21 @@ class Filter(JailThread):
def getFailures(self, filename):
ipList = dict()
logSys.debug(filename)
self.openLogFile(filename)
ret = self.openLogFile(filename)
if not ret:
logSys.error("Unable to get failures in " + filename)
return False
self.__setFilePos()
lastLine = None
for line in self.crtHandler:
try:
# Try to convert UTF-8 string to Latin-1
line = line.decode('utf-8').encode('latin-1')
#line = line.decode('utf-8').encode('latin-1')
line = line.decode('utf-8')
except UnicodeDecodeError:
pass
except UnicodeEncodeError:
logSys.warn("Mmhh... Are you sure you watch the correct file?")
#except UnicodeEncodeError:
# logSys.warn("Mmhh... Are you sure you watch the correct file?")
if not self.dateDetector.matchTime(line):
# There is no valid time in this line
continue
@ -351,6 +360,7 @@ class Filter(JailThread):
if lastLine:
self.lastDate[filename] = self.dateDetector.getTime(lastLine)
self.__closeLogFile()
return True
##
# Finds the failure in a line.