Skip to content

Commit

Permalink
MIB loader ignores file and directory access errors
Browse files Browse the repository at this point in the history
Also fixed crash on MIB load failure in case of directory
access error
  • Loading branch information
etingof committed Jan 8, 2019
1 parent 2b3dcaf commit fd7f6a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
Revision 4.4.9, released 2019-01-XX
-----------------------------------

No changes yet
- Made MIB loader ignoring file and directory access errors
- Fixed crash on MIB load failure in case of directory access error

Revision 4.4.8, released 2018-12-30
-----------------------------------
Expand Down
22 changes: 13 additions & 9 deletions pysnmp/smi/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self._srcName)

def _uniqNames(self, files):
u = {}
u = set()
for f in files:
if f[:9] == '__init__.':
if f.startswith('__init__.'):
continue
for typ in (imp.PY_SOURCE, imp.PY_COMPILED):
for sfx, sfxLen, mode in self.__sfx[typ]:
if f[-sfxLen:] == sfx:
u[f[:-sfxLen]] = None
u.add(f[:-sfxLen])
return tuple(u)

# MibSource API follows
Expand Down Expand Up @@ -201,7 +201,8 @@ def _getData(self, f, mode=None):
return self.__loader.get_data(p), p

except Exception: # ZIP code seems to return all kinds of errors
raise IOError(ENOENT, 'No such file in ZIP archive: %s' % sys.exc_info()[1], p)
why = sys.exc_info()
raise IOError(ENOENT, 'File or ZIP archive %s access error: %s' % (p, why[1]))


class DirMibSource(__AbstractMibSource):
Expand All @@ -213,8 +214,9 @@ def _listdir(self):
try:
return self._uniqNames(os.listdir(self._srcName))
except OSError:
why = sys.exc_info()
debug.logger & debug.flagBld and debug.logger(
'listdir() failed for %s: %s' % (self._srcName, sys.exc_info()[1]))
'listdir() failed for %s: %s' % (self._srcName, why[1]))
return ()

def _getTimestamp(self, f):
Expand All @@ -225,6 +227,7 @@ def _getTimestamp(self, f):
raise IOError(ENOENT, 'No such file: %s' % sys.exc_info()[1], p)

def _getData(self, f, mode):
p = os.path.join(self._srcName, '*')
try:
if f in os.listdir(self._srcName): # make FS case-sensitive
p = os.path.join(self._srcName, f)
Expand All @@ -234,12 +237,13 @@ def _getData(self, f, mode):
return data, p

except (IOError, OSError):
why = sys.exc_info()[1]
if why.errno != ENOENT and ENOENT != -1:
raise error.MibLoadError('MIB file %s access error: %s' % (p, why))
why = sys.exc_info()
msg = 'File or directory %s access error: %s' % (p, why[1])

raise IOError(ENOENT, 'No such file: %s' % sys.exc_info()[1], f)
else:
msg = 'No such file or directory: %s' % p

raise IOError(ENOENT, msg)

class MibBuilder(object):
defaultCoreMibs = os.pathsep.join(
Expand Down

0 comments on commit fd7f6a7

Please sign in to comment.