From 4782396cc464feb84a1552379e1909a9446178cc Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Mon, 12 Dec 2011 15:00:51 -0200 Subject: [PATCH] Do not stat() dir twice --- libkmod/libkmod-config.c | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/libkmod/libkmod-config.c b/libkmod/libkmod-config.c index bdb290bb..605e0211 100644 --- a/libkmod/libkmod-config.c +++ b/libkmod/libkmod-config.c @@ -382,21 +382,16 @@ static bool conf_files_filter_out(struct kmod_ctx *ctx, const char *path, return 0; } +/* + * Iterate over a directory (given by @path) and save the list of + * configuration files in @list. + */ static DIR *conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list, const char *path) { - struct stat st; DIR *d; int err; - if (stat(path, &st) < 0) - return NULL; - - if (!S_ISDIR(st.st_mode)) { - *list = kmod_list_append(*list, path); - return NULL; - } - d = opendir(path); if (d == NULL) { err = errno; @@ -404,6 +399,8 @@ static DIR *conf_files_list(struct kmod_ctx *ctx, struct kmod_list **list, return NULL; } + *list = NULL; + for (;;) { struct dirent ent, *entp; struct kmod_list *l, *tmp; @@ -470,7 +467,7 @@ int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, for (i = 0; config_paths[i] != NULL; i++) { const char *path = config_paths[i]; - struct kmod_list *list = NULL; + struct kmod_list *list; struct stat st; DIR *d; @@ -494,16 +491,6 @@ int kmod_config_new(struct kmod_ctx *ctx, struct kmod_config **p_config, d = conf_files_list(ctx, &list, path); - if (d == NULL) { - ERR(ctx, "returned list but no directory?\n"); - while (list) { - free(list->data); - kmod_list_remove(list); - } - continue; - } - - /* treat all the entries in that dir */ for (; list != NULL; list = kmod_list_remove(list)) { int fd = openat(dirfd(d), list->data, O_RDONLY); DBG(ctx, "parsing file '%s/%s': %d\n", path, -- 2.47.3