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 return isAdmin
def logHTTPTraffic(requestLogMsg, responseLogMsg): def logHTTPTraffic(requestLogMsg, responseLogMsg):
"""
Logs HTTP traffic to the output file
"""
kb.locks.logLock.acquire() kb.locks.logLock.acquire()
dataToTrafficFile("%s\n" % requestLogMsg) dataToTrafficFile("%s\n" % requestLogMsg)
@ -1686,6 +1690,7 @@ def getPublicTypeMembers(type_, onlyValues=False):
""" """
Useful for getting members from types (e.g. in enums) Useful for getting members from types (e.g. in enums)
""" """
retVal = [] retVal = []
for name, value in getmembers(type_): for name, value in getmembers(type_):
@ -1698,6 +1703,10 @@ def getPublicTypeMembers(type_, onlyValues=False):
return retVal return retVal
def enumValueToNameLookup(type_, value_): def enumValueToNameLookup(type_, value_):
"""
Returns name of a enum member with a given value
"""
retVal = None retVal = None
for name, value in getPublicTypeMembers(type_): for name, value in getPublicTypeMembers(type_):
@ -1708,6 +1717,11 @@ def enumValueToNameLookup(type_, value_):
return retVal return retVal
def extractRegexResult(regex, content, flags=0): def extractRegexResult(regex, content, flags=0):
"""
Returns 'result' group value from a possible match with regex on a given
content
"""
retVal = None retVal = None
if regex and content and '?P<result>' in regex: 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 Trims alpha numeric characters from start and ending of a given value
""" """
while value and value[-1].isalnum(): while value and value[-1].isalnum():
value = value[:-1] value = value[:-1]
@ -1731,9 +1746,17 @@ def trimAlphaNum(value):
return value return value
def isNumPosStrValue(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" return value and isinstance(value, basestring) and value.isdigit() and value != "0"
def aliasToDbmsEnum(value): def aliasToDbmsEnum(value):
"""
Returns major DBMS name from a given alias
"""
retVal = None retVal = None
for key, item in dbmsDict.items(): for key, item in dbmsDict.items():
@ -1746,7 +1769,7 @@ def aliasToDbmsEnum(value):
def findDynamicContent(firstPage, secondPage): def findDynamicContent(firstPage, secondPage):
""" """
This function checks if the provided pages have dynamic content. If they 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" infoMsg = "searching for dynamic content"
@ -2019,3 +2042,17 @@ def getComparePageRatio(firstPage, secondPage, filtered=False):
conf.seqMatcher.set_seq2(secondPage) conf.seqMatcher.set_seq2(secondPage)
return conf.seqMatcher.quick_ratio() 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 dataToDumpFile
from lib.core.common import dataToStdout from lib.core.common import dataToStdout
from lib.core.common import getUnicode from lib.core.common import getUnicode
from lib.core.common import openFile
from lib.core.common import restoreDumpMarkedChars from lib.core.common import restoreDumpMarkedChars
from lib.core.data import conf from lib.core.data import conf
from lib.core.data import kb from lib.core.data import kb
@ -259,7 +260,7 @@ class Dump:
os.makedirs(dumpDbPath, 0755) os.makedirs(dumpDbPath, 0755)
dumpFileName = "%s%s%s.csv" % (dumpDbPath, os.sep, table) 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"]) count = int(tableValues["__infos__"]["count"])
separator = str() 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 getFileType
from lib.core.common import normalizePath from lib.core.common import normalizePath
from lib.core.common import ntToPosixSlashes from lib.core.common import ntToPosixSlashes
from lib.core.common import openFile
from lib.core.common import parseTargetDirect from lib.core.common import parseTargetDirect
from lib.core.common import parseTargetUrl from lib.core.common import parseTargetUrl
from lib.core.common import paths from lib.core.common import paths
@ -130,7 +131,7 @@ def __urllib2Opener():
urllib2.install_opener(opener) urllib2.install_opener(opener)
def __feedTargetsDict(reqFile, addedTargetUrls): def __feedTargetsDict(reqFile, addedTargetUrls):
fp = codecs.open(reqFile, "rb") fp = openFile(reqFile, "rb")
fread = fp.read() fread = fp.read()
fread = fread.replace("\r", "") fread = fread.replace("\r", "")
@ -1262,7 +1263,7 @@ def __saveCmdline():
config.set(family, option, value) config.set(family, option, value)
confFP = codecs.open(paths.SQLMAP_CONFIG, "wb", conf.dataEncoding) confFP = openFile(paths.SQLMAP_CONFIG, "wb")
config.write(confFP) config.write(confFP)
infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG infoMsg = "saved command line options on '%s' configuration file" % paths.SQLMAP_CONFIG
@ -1317,7 +1318,7 @@ def __mergeOptions(inputOptions):
def __setTrafficOutputFP(): def __setTrafficOutputFP():
if conf.trafficFile: if conf.trafficFile:
conf.trafficFP = codecs.open(conf.trafficFile, "w+", conf.dataEncoding) conf.trafficFP = openFile(conf.trafficFile, "w+")
def __basicOptionValidation(): def __basicOptionValidation():
if conf.limitStart is not None and not (isinstance(conf.limitStart, int) and conf.limitStart > 0): if conf.limitStart is not None and not (isinstance(conf.limitStart, int) and conf.limitStart > 0):