Skip to content

Commit

Permalink
[plugins] fix mangled command filenames collisions
Browse files Browse the repository at this point in the history
- Command filenames collisions must be tested tested against absolute path, not
  relative. Otherwise false positive results about no collisions are returned.

- If collision happens for long filenames, ensure that appending ".1" or similar
does not exceed the max filename length.

Resolves: #1684

Signed-off-by: Pavel Moravec <[email protected]>
  • Loading branch information
pmoravec authored and bmr-cymru committed Aug 15, 2019
1 parent 7b485f0 commit 47dcdb8
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions sos/plugins/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -965,20 +965,27 @@ def _make_command_filename(self, exe, subdir=None):
if subdir:
# only allow a single level of subdir to be created
plugin_dir += "/%s" % subdir.split('/')[0]
outfn = os.path.join(self.commons['cmddir'], plugin_dir,
self._mangle_command(exe))
outdir = os.path.join(self.commons['cmddir'], plugin_dir)
outfn = self._mangle_command(exe)

# check for collisions
if os.path.exists(outfn):
inc = 2
if os.path.exists(os.path.join(self.archive.get_tmp_dir(),
outdir, outfn)):
inc = 1
name_max = self.archive.name_max()
while True:
newfn = "%s_%d" % (outfn, inc)
if not os.path.exists(newfn):
suffix = ".%d" % inc
newfn = outfn
if name_max < len(newfn)+len(suffix):
newfn = newfn[:(name_max-len(newfn)-len(suffix))]
newfn = newfn + suffix
if not os.path.exists(os.path.join(self.archive.get_tmp_dir(),
outdir, newfn)):
outfn = newfn
break
inc += 1

return outfn
return os.path.join(outdir, outfn)

def add_env_var(self, name):
"""Add an environment variable to the list of to-be-collected env vars.
Expand Down

0 comments on commit 47dcdb8

Please sign in to comment.