Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid symlink infinite loops #44

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions distutils/filelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,22 @@ def _find_all_simple(path):
"""
Find all files under 'path'
"""
results = (
os.path.join(base, file)
for base, dirs, files in os.walk(path, followlinks=True)
for file in files
)
return filter(os.path.isfile, results)
# https://bugs.python.org/issue44497
dirs = set()
files = set()
for dirpath, dirnames, filenames in os.walk(path, followlinks=True):
st = os.stat(dirpath)
scandirs = []
for dirname in dirnames:
st = os.stat(os.path.join(dirpath, dirname))
dirkey = st.st_dev, st.st_ino
if dirkey not in dirs:
dirs.add(dirkey)
scandirs.append(dirname)
dirnames[:] = scandirs
for f in filenames:
files.add(os.path.join(dirpath, f))
Comment on lines +251 to +264
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's a common pattern, I'm very much a detractor to the init/loop/append pattern (mostly because of how it mutates a local variable, making state analysis much more complicated).

The proposed approach entwines all of the logic of path traversal, link resolution, and deduplication into several loops.

Additionally, this change removes the check for os.path.isfile. I'm not sure that check is important, but it seems to be important enough that it was included previously.

I'd much rather see a solution that separates the concern of walking a tree while avoiding loops and assembling a list of files in that walk. Since distutils is only needed for Python 3.6+, perhaps a pathlib-based solution could help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got an alternative solution I'll present.

return files


def findall(dir=os.curdir):
Expand Down