From 6b4e6d0da9db9829455431059878e9eaae2cfa03 Mon Sep 17 00:00:00 2001
From: Romain Forlot <romain.forlot@iot.bzh>
Date: Mon, 4 Sep 2017 14:37:09 +0200
Subject: Retrieve binder rootdir and subfolder paths

Change-Id: I064bf4831be40de70dab68b72489c4fdfc69e9c4
Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
---
 filescan-utils.c | 37 +++++++++++++++++++++++++++++++++++++
 filescan-utils.h | 12 +++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/filescan-utils.c b/filescan-utils.c
index dbbccc3..830e233 100644
--- a/filescan-utils.c
+++ b/filescan-utils.c
@@ -17,6 +17,8 @@
 
 #define _GNU_SOURCE
 #include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
 #include <string.h>
 #include <time.h>
 #include <sys/prctl.h>
@@ -120,3 +122,38 @@ PUBLIC const char *GetBinderName() {
 
     return binderName;
 }
+
+PUBLIC BPaths GetBindingDirsPath()
+{
+    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];
+    sprintf(fd, "%d", afb_daemon_rootdir_get_fd());
+    char* fd_link = malloc(strlen("/proc/self/fd/") + strlen(&fd));
+    strcpy(fd_link, "/proc/self/fd/");
+    strcat(fd_link, &fd);
+
+    ssize_t len;
+    if((len = readlink(fd_link, &BindingPaths.rootdir, sizeof(BindingPaths)-1)) == -1)
+    {
+        perror("lstat");
+        AFB_ERROR("Error reading stat of link: %s", fd_link);
+        return BindingPaths;
+    }
+    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;
+}
diff --git a/filescan-utils.h b/filescan-utils.h
index b6111e2..e038330 100644
--- a/filescan-utils.h
+++ b/filescan-utils.h
@@ -39,15 +39,25 @@
   #define CONTROL_MAXPATH_LEN 255
 #endif
 
-// ctl-misc.c
 typedef enum {
   CTL_SCAN_FLAT=0,
   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;
+
 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();
+
 
 #ifdef __cplusplus
     }
-- 
cgit