diff options
author | Romain Forlot <romain.forlot@iot.bzh> | 2017-09-08 11:53:44 +0200 |
---|---|---|
committer | Romain Forlot <romain.forlot@iot.bzh> | 2018-12-13 14:12:02 +0100 |
commit | 0d3acdd1ac876a1de4893d534ba6fc438045c93d (patch) | |
tree | a38db43e495634ee58f42dec139dd26cdd6e1bc0 | |
parent | 7624af807d9c3574f36d015fc7b894e0e321f884 (diff) |
More reliable GetBindingDirsPath function.
Only return a wanted directory path not a complex
structure.
Change-Id: I5b5a1dea2c55ee6a76dbbbd52c3ebfdd684255e5
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
-rw-r--r-- | filescan-utils.c | 50 | ||||
-rw-r--r-- | filescan-utils.h | 18 |
2 files changed, 38 insertions, 30 deletions
diff --git a/filescan-utils.c b/filescan-utils.c index 830e233..fe01bdc 100644 --- a/filescan-utils.c +++ b/filescan-utils.c @@ -123,37 +123,45 @@ PUBLIC const char *GetBinderName() { return binderName; } -PUBLIC BPaths GetBindingDirsPath() +PUBLIC const char *GetBindingDirPath(BindingDirsT dir) { - BPaths BindingPaths; - - void initBindingPaths(char* bdir, const char* dir) - { - strcpy(bdir, BindingPaths.rootdir); - strcat(bdir, dir); - } - // A file description should not be greater than 999.999.999 char fd[10]; + static char retdir[CONTROL_MAXPATH_LEN]; sprintf(fd, "%d", afb_daemon_rootdir_get_fd()); - char* fd_link = malloc(strlen("/proc/self/fd/") + strlen(&fd)); + char* fd_link = malloc(strlen("/proc/self/fd/") + strlen(fd)); strcpy(fd_link, "/proc/self/fd/"); - strcat(fd_link, &fd); + strcat(fd_link, fd); ssize_t len; - if((len = readlink(fd_link, &BindingPaths.rootdir, sizeof(BindingPaths)-1)) == -1) + if((len = readlink(fd_link, retdir, sizeof(retdir)-1)) == -1) { perror("lstat"); AFB_ERROR("Error reading stat of link: %s", fd_link); - return BindingPaths; + strcpy(retdir, "/tmp"); + } + else + { + retdir[len] = '\0'; + switch (dir) { + case BIN_DIR: + strcat(retdir, "/bin"); + break; + case ETC_DIR: + strcat(retdir, "/etc"); + break; + case LIB_DIR: + strcat(retdir, "/lib"); + break; + case DATA_DIR: + strcat(retdir, "/data"); + break; + case HTTP_DIR: + strcat(retdir, "/http"); + break; + } } - BindingPaths.rootdir[len] = '\0'; - free(fd_link); - initBindingPaths(BindingPaths.bindir, "/bin"); - initBindingPaths(BindingPaths.etcdir, "/etc"); - initBindingPaths(BindingPaths.libdir, "/lib"); - initBindingPaths(BindingPaths.datadir, "/data"); - initBindingPaths(BindingPaths.httpdir, "/http"); - return BindingPaths; + free(fd_link); + return retdir; } diff --git a/filescan-utils.h b/filescan-utils.h index e038330..738be27 100644 --- a/filescan-utils.h +++ b/filescan-utils.h @@ -44,19 +44,19 @@ typedef enum { CTL_SCAN_RECURSIVE=1, } CtlScanDirModeT; -typedef struct bpath { - char rootdir[CONTROL_MAXPATH_LEN]; - char bindir[CONTROL_MAXPATH_LEN]; - char etcdir[CONTROL_MAXPATH_LEN]; - char datadir[CONTROL_MAXPATH_LEN]; - char libdir[CONTROL_MAXPATH_LEN]; - char httpdir[CONTROL_MAXPATH_LEN]; -} BPaths; +typedef enum { + ROOT_DIR=0, + BIN_DIR=1, + ETC_DIR=2, + DATA_DIR=3, + LIB_DIR=4, + HTTP_DIR=5 +} BindingDirsT PUBLIC const char *GetMidleName(const char*name); PUBLIC const char *GetBinderName(); PUBLIC json_object* ScanForConfig (const char* searchPath, CtlScanDirModeT mode, const char *pre, const char *ext); -PUBLIC BPaths GetBindingDirsPath(); +PUBLIC const char *GetBindingDirPath(BindingDirsT dir); #ifdef __cplusplus |