Canonicalize paths on db history getters 57/73457/3
authorKyungwook Tak <k.tak@samsung.com>
Wed, 8 Jun 2016 07:27:52 +0000 (16:27 +0900)
committerkyungwook tak <k.tak@samsung.com>
Thu, 9 Jun 2016 04:59:24 +0000 (21:59 -0700)
Change-Id: Icdf4a1487df644ac662469d1af4729766c17c739
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/client/content-screening.cpp
src/framework/service/cs-logic.cpp

index 181bbfa..a9fbabc 100644 (file)
@@ -481,7 +481,7 @@ int csr_cs_scan_dirs_async(csr_cs_context_h handle, const char *dir_paths[],
                if (dir_paths[i] == nullptr)
                        return CSR_ERROR_INVALID_PARAMETER;
 
-               dirSet->insert(Client::getAbsolutePath(dir_paths[i]));
+               dirSet->emplace(Client::getAbsolutePath(dir_paths[i]));
        }
 
        auto task = std::make_shared<Task>([hExt, user_data, dirSet] {
@@ -729,7 +729,7 @@ int csr_cs_get_detected_malware(csr_cs_context_h handle, const char *file_path,
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
        auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
-                                  CommandId::GET_DETECTED, std::string(file_path));
+                               CommandId::GET_DETECTED, Client::getAbsolutePath(file_path));
 
        if (ret.second)
                hExt->add(ResultPtr(ret.second));
@@ -761,7 +761,7 @@ int csr_cs_get_detected_malwares(csr_cs_context_h handle,
                if (dir_paths[i] == nullptr || dir_paths[i][0] == '\0')
                        return CSR_ERROR_INVALID_PARAMETER;
 
-               dirSet.emplace(dir_paths[i]);
+               dirSet.emplace(Client::getAbsolutePath(dir_paths[i]));
        }
 
        if (dirSet.size() == 0)
@@ -805,7 +805,7 @@ int csr_cs_get_ignored_malware(csr_cs_context_h handle, const char *file_path,
 
        auto hExt = reinterpret_cast<Client::HandleExt *>(handle);
        auto ret = hExt->dispatch<std::pair<int, CsDetected *>>(
-                                  CommandId::GET_IGNORED, std::string(file_path));
+                               CommandId::GET_IGNORED, Client::getAbsolutePath(file_path));
 
        if (ret.second)
                hExt->add(ResultPtr(ret.second));
@@ -837,7 +837,7 @@ int csr_cs_get_ignored_malwares(csr_cs_context_h handle,
                if (dir_paths[i] == nullptr || dir_paths[i][0] == '\0')
                        return CSR_ERROR_INVALID_PARAMETER;
 
-               dirSet.emplace(dir_paths[i]);
+               dirSet.emplace(Client::getAbsolutePath(dir_paths[i]));
        }
 
        if (dirSet.size() == 0)
index de1b45a..fe39c47 100644 (file)
@@ -22,7 +22,6 @@
 #include "service/cs-logic.h"
 
 #include <utility>
-#include <algorithm>
 #include <climits>
 #include <cerrno>
 #include <unistd.h>
@@ -106,6 +105,26 @@ std::string resolvePath(const std::string &_path)
        return path;
 }
 
+std::string canonicalizePath(const std::string &path, bool checkAccess)
+{
+       auto resolved = resolvePath(path);
+       auto target = File::getPkgPath(resolved);
+
+       if (checkAccess && ::access(target.c_str(), R_OK) != 0) {
+               const int err = errno;
+               if (err == ENOENT)
+                       ThrowExc(CSR_ERROR_FILE_DO_NOT_EXIST, "File do not exist: " << target);
+               else if (err == EACCES)
+                       ThrowExc(CSR_ERROR_PERMISSION_DENIED,
+                                        "Perm denied to get real path: " << target);
+               else
+                       ThrowExc(CSR_ERROR_FILE_SYSTEM, "Failed to get real path: " << target <<
+                                        " with errno: " << err);
+       }
+
+       return target;
+}
+
 } // namespace anonymous
 
 CsLogic::CsLogic(const std::shared_ptr<CsLoader> &loader,
@@ -521,20 +540,7 @@ RawBuffer CsLogic::canonicalizePaths(const StrSet &paths)
        StrSet canonicalized;
 
        for (const auto &path : paths) {
-               auto resolved = resolvePath(path);
-               auto target = File::getPkgPath(resolved);
-
-               if (::access(target.c_str(), R_OK) != 0) {
-                       const int err = errno;
-                       if (err == ENOENT)
-                               ThrowExc(CSR_ERROR_FILE_DO_NOT_EXIST, "File do not exist: " << target);
-                       else if (err == EACCES)
-                               ThrowExc(CSR_ERROR_PERMISSION_DENIED,
-                                                "Perm denied to get real path: " << target);
-                       else
-                               ThrowExc(CSR_ERROR_FILE_SYSTEM, "Failed to get real path: " << target <<
-                                                " with errno: " << err);
-               }
+               auto target = canonicalizePath(path, true);
 
                if (canonicalized.find(target) == canonicalized.end()) {
                        INFO("Insert to canonicalized list: " << target);
@@ -608,7 +614,7 @@ RawBuffer CsLogic::judgeStatus(const std::string &filepath, csr_cs_action_e acti
 
 RawBuffer CsLogic::getDetected(const std::string &filepath)
 {
-       auto row = this->m_db->getDetectedByNameOnPath(File::getPkgPath(filepath));
+       auto row = this->m_db->getDetectedByNameOnPath(canonicalizePath(filepath, false));
 
        if (row && !row->isIgnored)
                return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
@@ -616,15 +622,18 @@ RawBuffer CsLogic::getDetected(const std::string &filepath)
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 }
 
-RawBuffer CsLogic::getDetectedList(const StrSet &dirSet)
+RawBuffer CsLogic::getDetectedList(const StrSet &_dirSet)
 {
+       StrSet dirSet;
+       for (const auto &dir : _dirSet)
+               dirSet.emplace(canonicalizePath(dir, false));
+
        Db::RowShPtrs rows;
-       std::for_each(dirSet.begin(), dirSet.end(),
-       [this, &rows](const std::string & dir) {
-               for (auto &row : this->m_db->getDetectedByNameOnDir(File::getPkgPath(dir)))
+       for (const auto &dir : dirSet) {
+               for (auto &row : this->m_db->getDetectedByNameOnDir(dir))
                        if (!row->isIgnored)
                                rows.emplace_back(std::move(row));
-       });
+       }
 
        if (rows.empty())
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
@@ -632,10 +641,9 @@ RawBuffer CsLogic::getDetectedList(const StrSet &dirSet)
                return BinaryQueue::Serialize(CSR_ERROR_NONE, rows).pop();
 }
 
-// TODO: is this command needed?
 RawBuffer CsLogic::getIgnored(const std::string &filepath)
 {
-       auto row = this->m_db->getDetectedByNameOnPath(File::getPkgPath(filepath));
+       auto row = this->m_db->getDetectedByNameOnPath(canonicalizePath(filepath, false));
 
        if (row && row->isIgnored)
                return BinaryQueue::Serialize(CSR_ERROR_NONE, row).pop();
@@ -643,15 +651,18 @@ RawBuffer CsLogic::getIgnored(const std::string &filepath)
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 }
 
-RawBuffer CsLogic::getIgnoredList(const StrSet &dirSet)
+RawBuffer CsLogic::getIgnoredList(const StrSet &_dirSet)
 {
+       StrSet dirSet;
+       for (const auto &dir : _dirSet)
+               dirSet.emplace(canonicalizePath(dir, false));
+
        Db::RowShPtrs rows;
-       std::for_each(dirSet.begin(), dirSet.end(),
-       [this, &rows](const std::string & dir) {
-               for (auto &row : this->m_db->getDetectedByNameOnDir(File::getPkgPath(dir)))
+       for (const auto &dir : dirSet) {
+               for (auto &row : this->m_db->getDetectedByNameOnDir(dir))
                        if (row->isIgnored)
                                rows.emplace_back(std::move(row));
-       });
+       }
 
        if (rows.empty())
                return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();