History

dt: dt-extract-compatibles: Don't follow symlinks when walking tree

The iglob function, which we use to find C source files in the kernel tree, always follows symbolic links. This can cause unintentional recursions whenever a symbolic link points to a parent directory. A common scenario is building the kernel with the output set to a directory inside the kernel tree, which will contain such a symlink. Instead of using the iglob function, use os.walk to traverse the directory tree, which by default doesn't follow symbolic links. fnmatch is then used to match the glob on the filename, as well as ignore hidden files (which were ignored by default with iglob). This approach runs just as fast as using iglob.

Nícolas F. R. A. Prado committed 3 years ago

Commit 8f515931 parent fe612621 file · +12 −2Browse files

Showing the changes to scripts (1 of 1 file). Show all

Unified · Split

scripts/dtc/dt-extract-compatibles+12−2
@@ -1,8 +1,8 @@
11 #!/usr/bin/env python3
22 # SPDX-License-Identifier: GPL-2.0-only
33
4+import fnmatch
45 import os
5−import glob
66 import re
77 import argparse
88
@@ -81,10 +81,20 @@ def print_compat(filename, compatibles): · 72 unmodified lines
8181 else:
8282 print(*compatibles, sep='\n')
8383
84+def glob_without_symlinks(root, glob):
85+ for path, dirs, files in os.walk(root):
86+ # Ignore hidden directories
87+ for d in dirs:
88+ if fnmatch.fnmatch(d, ".*"):
89+ dirs.remove(d)
90+ for f in files:
91+ if fnmatch.fnmatch(f, glob):
92+ yield os.path.join(path, f)
93+
8494 def files_to_parse(path_args):
8595 for f in path_args:
8696 if os.path.isdir(f):
87− for filename in glob.iglob(f + "/**/*.c", recursive=True):
97+ for filename in glob_without_symlinks(f, "*.c"):
8898 yield filename
8999 else:
90100 yield f