server: add a util function to get the list of files in a directory 79/126579/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Apr 2017 08:00:21 +0000 (17:00 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Mon, 24 Apr 2017 08:00:21 +0000 (17:00 +0900)
Change-Id: I4759cdfad87c239062944b1d4fca4dad4b15c3a3
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/ServerUtil.h
src/server/ServerUtil.cpp

index d009293..59d10ae 100644 (file)
@@ -18,6 +18,7 @@
 #define __CONTEXT_SERVER_UTIL_H__
 
 #include <string>
+#include <vector>
 #include <tzplatform_config.h>
 #include <ContextTypes.h>
 
@@ -29,6 +30,8 @@ namespace ctx { namespace util {
 
        std::string getUserPath(uid_t uid, enum tzplatform_variable id, const std::string& path);
 
+       std::vector<std::string> getFiles(const std::string& dir, bool symlink = false);
+
 } }
 
 #endif /* __CONTEXT_SERVER_UTIL_H__ */
index c00eaca..1236612 100644 (file)
  * limitations under the License.
  */
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
 #include <ScopeMutex.h>
 #include <ClientBase.h>
 #include <ServiceBase.h>
@@ -72,3 +76,42 @@ EXPORT_API std::string util::getUserPath(uid_t uid, enum tzplatform_variable id,
        tzplatform_context_destroy(context);
        return outPath;
 }
+
+EXPORT_API std::vector<std::string> util::getFiles(const std::string& dirPath, bool symlink)
+{
+       static GMutex dirMutex;
+
+       DIR* dir = NULL;
+       struct dirent* entry = NULL;
+       struct stat fileStat;
+       std::string filePath;
+       std::vector<std::string> fileNames;
+
+       ScopeMutex sm(&dirMutex);
+
+       dir = opendir(dirPath.c_str());
+       IF_FAIL_RETURN_TAG(dir, fileNames, _E, "Failed to open: %s", dirPath.c_str());
+
+       while (true) {
+               entry = readdir(dir);
+               if (!entry)
+                       break;
+
+               filePath = dirPath + "/" + entry->d_name;
+               if (symlink) {
+                       if (stat(filePath.c_str(), &fileStat) != 0)
+                               continue;
+               } else {
+                       if (lstat(filePath.c_str(), &fileStat) != 0)
+                               continue;
+               }
+
+               if (!S_ISREG(fileStat.st_mode))
+                       continue;
+
+               fileNames.push_back(entry->d_name);
+       }
+
+       closedir(dir);
+       return fileNames;
+}