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