Merge "findleaves.py: prevent recursion into symlink loops"

This commit is contained in:
Colin Cross 2016-11-04 18:09:36 +00:00 committed by Gerrit Code Review
commit 4d8462417d
1 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import sys
def perform_find(mindepth, prune, dirlist, filenames):
result = []
pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))
seen = set()
for rootdir in dirlist:
rootdepth = rootdir.count("/")
for root, dirs, files in os.walk(rootdir, followlinks=True):
@ -52,6 +53,18 @@ def perform_find(mindepth, prune, dirlist, filenames):
if filename in files:
result.append(os.path.join(root, filename))
del dirs[:]
# filter out inodes that have already been seen due to symlink loops
i = 0
while i < len(dirs):
st = os.stat(os.path.join(root, dirs[i]))
key = (st.st_dev, st.st_ino)
if key in seen:
del dirs[i]
else:
i += 1
seen.add(key)
return result
def usage():