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

Unified · Split

scripts/dtc/dt-extract-compatibles+12−2
@@ -1,8 +1,8 @@
1#!/usr/bin/env python31#!/usr/bin/env python3
2# SPDX-License-Identifier: GPL-2.0-only2# SPDX-License-Identifier: GPL-2.0-only
3 3
4import fnmatch
4import os5import os
5import glob
6import re6import re
7import argparse7import argparse
8 8
@@ -81,10 +81,20 @@ def print_compat(filename, compatibles): · 72 unmodified lines
81 else:81 else:
82 print(*compatibles, sep='\n')82 print(*compatibles, sep='\n')
83 83
84def 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
84def files_to_parse(path_args):94def files_to_parse(path_args):
85 for f in path_args:95 for f in path_args:
86 if os.path.isdir(f):96 if os.path.isdir(f):
87 for filename in glob.iglob(f + "/**/*.c", recursive=True):97 for filename in glob_without_symlinks(f, "*.c"):
88 yield filename98 yield filename
89 else:99 else:
90 yield f100 yield f