Handle symbolic links in app directory 28/107728/2
authorKyungwook Tak <k.tak@samsung.com>
Thu, 29 Dec 2016 10:44:13 +0000 (19:44 +0900)
committerkyungwook tak <k.tak@samsung.com>
Thu, 29 Dec 2016 11:11:57 +0000 (03:11 -0800)
App directory hierarchy is changed.
Non global apps have symbolic link which points under
TZ_SYS_RW_APPS(/opt/usr/globalapps/...) because non global app's binary
and resources are duplicated if such app installed by multiple users.

So FsVisitor traverses through symlink either only in app directory
which is defined in AppDir class in regular expression.

Change-Id: I3049fcb92258fc8d8b4123d74856a4d584ebcdfe
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/service/file-system.cpp
src/framework/service/file-system.h
test/engine/content-screening/sample-engine.cpp

index 2422a4d..8458052 100644 (file)
@@ -301,54 +301,73 @@ void FsVisitor::run(const DirPtr &dirptr, const FilePtr &currentdir)
                auto fullpath = (parent_dirpath.back() == '/') ?
                                (parent_dirpath + name) : (parent_dirpath + "/" + name);
 
-               if (result->d_type == DT_DIR) {
+               INFO("start to traverse file: " << fullpath);
+
+               if (result->d_type == DT_LNK) {
+                       if (!currentdir->isInApp())
+                               continue;
+
+                       auto stat = getStat(fullpath);
+                       if (S_ISREG(stat->st_mode))
+                               this->visitFile(fullpath, currentdir);
+                       else if (S_ISDIR(stat->st_mode))
+                               this->visitDir(fullpath, currentdir);
+
+               } else if (result->d_type == DT_DIR) {
                        if ((name_size == 1 && name[0] == '.') ||
                                (name_size == 2 && name[0] == '.' && name[1] == '.'))
                                continue;
 
-                       FilePtr ncurrentdir;
-                       try {
-                               ncurrentdir = File::create(fullpath, currentdir);
-                       } catch (const Exception &e) {
-                               if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST) {
-                                       WARN("Perm denied to create file on pkg path: " << fullpath);
-                                       continue;
-                               } else {
-                                       throw;
-                               }
-                       }
-
-                       if (this->m_isBasedOnName && ncurrentdir->isInApp()) {
-                               this->m_targetHandler(ncurrentdir);
-                       } else {
-                               auto ndirptr = openDir(fullpath);
-                               if (ndirptr == nullptr) {
-                                       WARN("Failed to open dir: " << fullpath << " with errno: " << errno);
-                                       continue;
-                               }
-
-                               DEBUG("recurse dir : " << fullpath);
-                               this->run(ndirptr, ncurrentdir);
-                       }
+                       this->visitDir(fullpath, currentdir);
                } else if (result->d_type == DT_REG) {
-                       try {
-                               auto fileptr = File::createIfModified(fullpath, currentdir, this->m_since);
-
-                               if (fileptr)
-                                       this->m_targetHandler(fileptr);
-                       } catch (const Exception &e) {
-                               if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST)
-                                       WARN("file not exist: " << fullpath << " msg: " << e.what());
-                               else if (e.error() == CSR_ERROR_FILE_SYSTEM)
-                                       WARN("file type is not regular...? can it be happened?"
-                                                " :" << fullpath << " msg: " << e.what());
-                               else
-                                       throw;
-                       }
+                       this->visitFile(fullpath, currentdir);
                }
        }
 }
 
+void FsVisitor::visitDir(const std::string &fullpath, const FilePtr &currentdir)
+{
+       FilePtr ncurrentdir;
+
+       try {
+               ncurrentdir = File::create(fullpath, currentdir);
+       } catch (const Exception &e) {
+               if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST) {
+                       WARN("Perm denied to create file on pkg path: " << fullpath);
+                       return;
+               } else {
+                       throw;
+               }
+       }
+
+       if (this->m_isBasedOnName && ncurrentdir->isInApp()) {
+               this->m_targetHandler(ncurrentdir);
+       } else {
+               if (auto ndirptr = openDir(fullpath)) {
+                       DEBUG("recurse dir: " << fullpath);
+                       this->run(ndirptr, ncurrentdir);
+               } else {
+                       WARN("Failed to open dir: " << fullpath << " with errno: " << errno);
+               }
+       }
+}
+
+void FsVisitor::visitFile(const std::string &fullpath, const FilePtr &currentdir)
+{
+       try {
+               if (auto fileptr = File::createIfModified(fullpath, currentdir, this->m_since))
+                       this->m_targetHandler(fileptr);
+       } catch (const Exception &e) {
+               if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST)
+                       WARN("file not exist: " << fullpath << " msg: " << e.what());
+               else if (e.error() == CSR_ERROR_FILE_SYSTEM)
+                       WARN("file type is not regular...? can it be happened?"
+                                " :" << fullpath << " msg: " << e.what());
+               else
+                       throw;
+       }
+}
+
 void FsVisitor::run()
 {
        auto currentdir = File::create(this->m_path, nullptr);
index ee73279..7166c47 100644 (file)
@@ -170,6 +170,8 @@ private:
        using DirPtr = std::unique_ptr<Dir>;
 
        void run(const DirPtr &dirptr, const FilePtr &currentdir);
+       void visitDir(const std::string &fullpath, const FilePtr &currentdir);
+       void visitFile(const std::string &fullpath, const FilePtr &currentdir);
 
        static DirPtr openDir(const std::string &);
 
index 34afff7..29634e8 100644 (file)
@@ -477,14 +477,28 @@ int csre_cs_scan_app_on_cloud(csre_cs_context_h handle,
 
                int ret = CSRE_ERROR_NONE;
 
-               if (result->d_type & (DT_REG | DT_LNK))
+               if (result->d_type == DT_LNK) {
+                       // when the file type is link, check the real file's type(dir or reg file)
+                       // and go ahead to traverse
+                       struct stat s;
+                       if (stat(fullpath.c_str(), &s) != 0)
+                               continue; // failed to get stat
+
+                       if (S_ISREG(s.st_mode))
+                               ret = csre_cs_scan_file(handle, fullpath.c_str(), &detected);
+                       else if (S_ISDIR(s.st_mode))
+                               ret = csre_cs_scan_app_on_cloud(handle, fullpath.c_str(), &detected);
+                       else
+                               continue;
+               } else if (result->d_type == DT_REG) {
                        ret = csre_cs_scan_file(handle, fullpath.c_str(), &detected);
-               else if ((result->d_type & DT_DIR)
-                                && filename.compare("..") != 0
-                                && filename.compare(".") != 0)
+               } else if ((result->d_type & DT_DIR) &&
+                                  filename.compare("..") != 0 &&
+                                  filename.compare(".") != 0) {
                        ret = csre_cs_scan_app_on_cloud(handle, fullpath.c_str(), &detected);
-               else
+               } else {
                        continue;
+               }
 
                if (ret != CSRE_ERROR_NONE)
                        return ret;