adding openFile method with an exception block around file opening part

This commit is contained in:
Miroslav Stampar 2011-01-08 09:30:10 +00:00
parent e3899f7467
commit 77b51dae57
3 changed files with 44 additions and 5 deletions

View File

@ -1670,6 +1670,10 @@ def runningAsAdmin():
return isAdmin
def logHTTPTraffic(requestLogMsg, responseLogMsg):
"""
Logs HTTP traffic to the output file
"""
kb.locks.logLock.acquire()
dataToTrafficFile("%s\n" % requestLogMsg)
@ -1686,6 +1690,7 @@ def getPublicTypeMembers(type_, onlyValues=False):
"""
Useful for getting members from types (e.g. in enums)
"""
retVal = []
for name, value in getmembers(type_):
@ -1698,6 +1703,10 @@ def getPublicTypeMembers(type_, onlyValues=False):
return retVal
def enumValueToNameLookup(type_, value_):
"""
Returns name of a enum member with a given value
"""
retVal = None
for name, value in getPublicTypeMembers(type_):
@ -1708,6 +1717,11 @@ def enumValueToNameLookup(type_, value_):
return retVal
def extractRegexResult(regex, content, flags=0):
"""
Returns 'result' group value from a possible match with regex on a given
content
"""
retVal = None
if regex and content and '?P<result>' in regex:
@ -1722,6 +1736,7 @@ def trimAlphaNum(value):
"""
Trims alpha numeric characters from start and ending of a given value
"""
while value and value[-1].isalnum():
value = value[:-1]
@ -1731,9 +1746,17 @@ def trimAlphaNum(value):
return value
def isNumPosStrValue(value):
"""
Returns True if value is a string with a positive integer representation
"""
return value and isinstance(value, basestring) and value.isdigit() and value != "0"
def aliasToDbmsEnum(value):
"""
Returns major DBMS name from a given alias
"""
retVal = None
for key, item in dbmsDict.items():
@ -1746,7 +1769,7 @@ def aliasToDbmsEnum(value):
def findDynamicContent(firstPage, secondPage):
"""
This function checks if the provided pages have dynamic content. If they
are dynamic, proper markings will be made.
are dynamic, proper markings will be made
"""
infoMsg = "searching for dynamic content"
@ -2019,3 +2042,17 @@ def getComparePageRatio(firstPage, secondPage, filtered=False):
conf.seqMatcher.set_seq2(secondPage)
return conf.seqMatcher.quick_ratio()
def openFile(filename, mode='r'):
"""
Returns file handle of a given filename
"""
try:
return codecs.open(filename, mode, conf.dataEncoding)
except IOError, e:
errMsg = "there has been a file opening error for filename '%s'. " % filename
errMsg += "Please check %s permissions on a file " % ("write" if mode and\
('w' in mode or 'a' in mode or '+' in mode) else "read")
errMsg += "and that it's not locked by another process."
raise sqlmapFilePathException, errMsg

View File

@ -14,6 +14,7 @@ import os
from lib.core.common import dataToDumpFile
from lib.core.common import dataToStdout
from lib.core.common import getUnicode
from lib.core.common import openFile
from lib.core.common import restoreDumpMarkedChars
from lib.core.data import conf
from lib.core.data import kb
@ -259,7 +260,7 @@ class Dump:
os.makedirs(dumpDbPath, 0755)
dumpFileName = "%s%s%s.csv" % (dumpDbPath, os.sep, table)
dumpFP = codecs.open(dumpFileName, "wb", conf.dataEncoding)
dumpFP = openFile(dumpFileName, "wb")
count = int(tableValues["__infos__"]["count"])
separator = str()

View File

@ -29,6 +29,7 @@ from lib.core.common import getFileItems
from lib.core.common import getFileType
from lib.core.common import normalizePath
from lib.core.common import ntToPosixSlashes
from lib.core.common import openFile
from lib.core.common import parseTargetDirect
from lib.core.common import parseTargetUrl
from lib.core.common import paths
@ -130,7 +131,7 @@ def __urllib2Opener():
urllib2.install_opener(opener)
def __feedTargetsDict(reqFile, addedTargetUrls):
fp = codecs.open(reqFile, "rb")
fp = openFile(reqFile, "rb")
fread = fp.read()
fread = fread.replace("\r", "")
@ -1262,7 +1263,7 @@ def __saveCmdline():
config.set(family, option, value)
confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", conf.dataEncoding)
confFP = openFile(paths.SQLMAP_CONFIG, "wb")
config.write(confFP)
infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG
@ -1317,7 +1318,7 @@ def __mergeOptions(inputOptions):
def __setTrafficOutputFP():
if conf.trafficFile:
conf.trafficFP = codecs.open(conf.trafficFile, "w+", conf.dataEncoding)
conf.trafficFP = openFile(conf.trafficFile, "w+")
def __basicOptionValidation():
if conf.limitStart is not None and not (isinstance(conf.limitStart, int) and conf.limitStart > 0):