[PATCH 09/19] glob: do not unnecessarily opendir() a directory

Sascha Hauer s.hauer at pengutronix.de
Tue Apr 3 00:48:41 PDT 2018


opendir() can trigger automounts, so do not use it when the
pattern we examine doesn't have any wildcards.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 lib/glob.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/lib/glob.c b/lib/glob.c
index 5a997ca092..32f7afdce8 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -313,7 +313,7 @@ static int prefix_array(const char *dirname, char **array, size_t n,
 static int glob_in_dir(const char *pattern, const char *directory,
 		int flags, int (*errfunc) __P((const char *, int)), glob_t *pglob)
 {
-	__ptr_t stream;
+	__ptr_t stream = NULL;
 
 	struct globlink {
 		struct globlink *next;
@@ -323,7 +323,13 @@ static int glob_in_dir(const char *pattern, const char *directory,
 	size_t nfound = 0;
 	int meta;
 
-	stream = opendir(directory);
+	meta = glob_pattern_p(pattern, !(flags & GLOB_NOESCAPE));
+
+	if (meta)
+		flags |= GLOB_MAGCHAR;
+
+	if (meta)
+		stream = opendir(directory);
 
 	if (stream == NULL) {
 		if ((errfunc != NULL && (*errfunc) (directory, errno)) ||
@@ -331,11 +337,6 @@ static int glob_in_dir(const char *pattern, const char *directory,
 			return GLOB_ABORTED;
 	}
 
-	meta = glob_pattern_p(pattern, !(flags & GLOB_NOESCAPE));
-
-	if (meta)
-		flags |= GLOB_MAGCHAR;
-
 	while (1) {
 		const char *name;
 		size_t len;
-- 
2.16.1




More information about the barebox mailing list