diff options
author | Sebastien Douheret <sebastien.douheret@iot.bzh> | 2018-06-05 22:56:45 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-13 14:12:02 +0100 |
commit | 91866ef916e1371413038013d5780eac3c6254bf (patch) | |
tree | b1ff45009b3cefa931aedef2f9cf5dcf246c9413 /filescan-utils.c | |
parent | 529d3e0b43d161475023437e947624550687f092 (diff) |
Removed anonymous function in ScanDir and fixed warnings
Change-Id: Ie6b67506be077f8fe5c2108e5dd98f9460dda485
Signed-off-by: Sebastien Douheret <sebastien.douheret@iot.bzh>
Diffstat (limited to 'filescan-utils.c')
-rw-r--r-- | filescan-utils.c | 149 |
1 files changed, 78 insertions, 71 deletions
diff --git a/filescan-utils.c b/filescan-utils.c index a15a69b..22af147 100644 --- a/filescan-utils.c +++ b/filescan-utils.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2016 "IoT.bzh" + * Copyright (C) 2016-2018 "IoT.bzh" * Author Fulup Ar Foll <fulup@iot.bzh> * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,83 +16,90 @@ */ #define _GNU_SOURCE +#include <dirent.h> #include <stdio.h> -#include <unistd.h> -#include <sys/stat.h> #include <string.h> -#include <time.h> #include <sys/prctl.h> -#include <dirent.h> +#include <sys/stat.h> +#include <time.h> +#include <unistd.h> #include "filescan-utils.h" -// List Avaliable Configuration Files -json_object* ScanForConfig (const char* searchPath, CtlScanDirModeT mode, const char *prefix, const char *extention) { - json_object *responseJ = json_object_new_array(); - char *dirPath; - char* dirList; - size_t extentionLen=0; - int count=0; +static int ScanDir(char* searchPath, CtlScanDirModeT mode, size_t extentionLen, + const char* prefix, const char* extention, json_object* responseJ) +{ + int found = 0; + DIR* dirHandle; + struct dirent* dirEnt; + dirHandle = opendir(searchPath); + if (!dirHandle) { + AFB_DEBUG("CONFIG-SCANNING dir=%s not readable", searchPath); + return 0; + } - if(!searchPath) - return responseJ; + //AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", searchPath); + while ((dirEnt = readdir(dirHandle)) != NULL) { - dirList = strdup(searchPath); + // recursively search embedded directories ignoring any directory starting by '.' or '_' + if (dirEnt->d_type == DT_DIR && mode == CTL_SCAN_RECURSIVE) { + char newpath[CONTROL_MAXPATH_LEN]; + if (dirEnt->d_name[0] == '.' || dirEnt->d_name[0] == '_') + continue; - int ScanDir (char *searchPath) { - int found=0; - DIR *dirHandle; - struct dirent *dirEnt; - dirHandle = opendir (searchPath); - if (!dirHandle) { - AFB_DEBUG("CONFIG-SCANNING dir=%s not readable", searchPath); - return 0; + strncpy(newpath, searchPath, sizeof(newpath)); + strncat(newpath, "/", sizeof(newpath) - strlen(newpath) - 1); + strncat(newpath, dirEnt->d_name, sizeof(newpath) - strlen(newpath) - 1); + found += ScanDir(newpath, mode, extentionLen, prefix, extention, responseJ); + continue; } - //AFB_NOTICE ("CONFIG-SCANNING:ctl_listconfig scanning: %s", searchPath); - while ((dirEnt = readdir(dirHandle)) != NULL) { + // Unknown type is accepted to support dump filesystems + if (dirEnt->d_type == DT_REG || dirEnt->d_type == DT_UNKNOWN) { - // recursively search embedded directories ignoring any directory starting by '.' or '_' - if (dirEnt->d_type == DT_DIR && mode == CTL_SCAN_RECURSIVE) { - char newpath[CONTROL_MAXPATH_LEN]; - if (dirEnt->d_name[0]=='.' || dirEnt->d_name[0]=='_') continue; - - strncpy(newpath, searchPath, sizeof(newpath)); - strncat(newpath, "/", sizeof(newpath) - strlen(newpath) - 1); - strncat(newpath, dirEnt->d_name, sizeof(newpath) - strlen(newpath) - 1); - found += ScanDir(newpath); + // check prefix and extention + ssize_t extentionIdx = strlen(dirEnt->d_name) - extentionLen; + if (extentionIdx <= 0) + continue; + if (prefix && !strcasestr(dirEnt->d_name, prefix)) + continue; + if (extention && strcasecmp(extention, &dirEnt->d_name[extentionIdx])) continue; - } - - // Unknown type is accepted to support dump filesystems - if (dirEnt->d_type == DT_REG || dirEnt->d_type == DT_UNKNOWN) { - - // check prefix and extention - ssize_t extentionIdx=strlen(dirEnt->d_name)-extentionLen; - if (extentionIdx <= 0) continue; - if (prefix && !strcasestr (dirEnt->d_name, prefix)) continue; - if (extention && strcasecmp (extention, &dirEnt->d_name[extentionIdx])) continue; - struct json_object *pathJ = json_object_new_object(); - json_object_object_add(pathJ, "fullpath", json_object_new_string(searchPath)); - json_object_object_add(pathJ, "filename", json_object_new_string(dirEnt->d_name)); - json_object_array_add(responseJ, pathJ); - found ++; - } + struct json_object* pathJ = json_object_new_object(); + json_object_object_add(pathJ, "fullpath", json_object_new_string(searchPath)); + json_object_object_add(pathJ, "filename", json_object_new_string(dirEnt->d_name)); + json_object_array_add(responseJ, pathJ); + found++; } - closedir(dirHandle); - return found; } + closedir(dirHandle); + return found; +} + +// List Avaliable Configuration Files +json_object* ScanForConfig(const char* searchPath, CtlScanDirModeT mode, const char* prefix, const char* extention) +{ + json_object* responseJ = json_object_new_array(); + char* dirPath; + char* dirList; + size_t extentionLen = 0; + int count = 0; + + if (!searchPath) + return responseJ; + + dirList = strdup(searchPath); if (extention) - extentionLen=strlen(extention); + extentionLen = strlen(extention); // loop recursively on dir - for (dirPath= strtok(dirList, ":"); dirPath && *dirPath; dirPath=strtok(NULL,":")) { - count += ScanDir (dirPath); + for (dirPath = strtok(dirList, ":"); dirPath && *dirPath; dirPath = strtok(NULL, ":")) { + count += ScanDir(dirPath, mode, extentionLen, prefix, extention, responseJ); } if (count == 0) { - json_object_put (responseJ); + json_object_put(responseJ); free(dirList); return NULL; } @@ -100,14 +107,15 @@ json_object* ScanForConfig (const char* searchPath, CtlScanDirModeT mode, const return (responseJ); } -const char *GetMiddleName(const char*name) { - char *fullname = strdup(name); +const char* GetMiddleName(const char* name) +{ + char* fullname = strdup(name); for (int idx = 0; fullname[idx] != '\0'; idx++) { if (fullname[idx] == '-') { int start; start = idx + 1; - for (int jdx = start; ; jdx++) { + for (int jdx = start;; jdx++) { if (fullname[jdx] == '-' || fullname[jdx] == '.' || fullname[jdx] == '\0') { fullname[jdx] = '\0'; return &fullname[start]; @@ -119,23 +127,25 @@ const char *GetMiddleName(const char*name) { return ""; } -const char *GetBinderName() { - static char *binderName=NULL; +const char* GetBinderName() +{ + static char* binderName = NULL; - if (binderName) return binderName; + if (binderName) + return binderName; - binderName= getenv("AFB_BINDER_NAME"); + binderName = getenv("AFB_BINDER_NAME"); if (!binderName) { static char psName[17]; // retrieve binder name from process name afb-name-trailer - prctl(PR_GET_NAME, psName,NULL,NULL,NULL); - binderName=(char*)GetMiddleName(psName); + prctl(PR_GET_NAME, psName, NULL, NULL, NULL); + binderName = (char*)GetMiddleName(psName); } return binderName; } -char *GetBindingDirPath(struct afb_dynapi *dynapi) +char* GetBindingDirPath(struct afb_dynapi* dynapi) { // A file description should not be greater than 999.999.999 char fd_link[CONTROL_MAXPATH_LEN]; @@ -150,13 +160,10 @@ char *GetBindingDirPath(struct afb_dynapi *dynapi) sprintf(fd_link, "/proc/self/fd/%d", afb_daemon_rootdir_get_fd()); #endif - if((len = readlink(fd_link, retdir, sizeof(retdir)-1)) == -1) - { + if ((len = readlink(fd_link, retdir, sizeof(retdir) - 1)) == -1) { perror("lstat"); strncpy(retdir, "/tmp", CONTROL_MAXPATH_LEN - 1); - } - else - { + } else { retdir[len] = '\0'; } |