Add cancel checker in scan app delta 92/78092/1
authorKyungwook Tak <k.tak@samsung.com>
Mon, 4 Jul 2016 08:45:48 +0000 (17:45 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 4 Jul 2016 08:45:48 +0000 (17:45 +0900)
For canceling faster while big app scanning

Change-Id: I935e2fa79c4ce213b2679aef4024c2bc0fc1109d
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
src/framework/service/cs-logic.cpp
src/framework/service/cs-logic.h

index 6218478..efd0e10 100644 (file)
@@ -236,7 +236,7 @@ int CsLogic::scanAppOnCloud(const CsContext &context, const FilePtr &pkgPtr,
        return this->handleAskUser(context, *malware, pkgPtr);
 }
 
-CsDetectedPtr CsLogic::scanAppDelta(const FilePtr &pkgPtr, std::string &riskiestPath)
+CsDetectedPtr CsLogic::scanAppDelta(const FilePtr &pkgPtr, std::string &riskiestPath, const std::function<void()> &isCancelled)
 {
        const auto &pkgPath = pkgPtr->getName();
        const auto &pkgId = pkgPtr->getAppPkgId();
@@ -252,6 +252,9 @@ CsDetectedPtr CsLogic::scanAppDelta(const FilePtr &pkgPtr, std::string &riskiest
        CsDetectedPtr riskiest;
        // traverse files in app and take which is more danger than riskiest
        auto visitor = FsVisitor::create([&](const FilePtr &file) {
+               if (isCancelled != nullptr)
+                       isCancelled();
+
                DEBUG("Scan file by engine: " << file->getPath());
 
                auto timestamp = ::time(nullptr);
@@ -288,7 +291,7 @@ CsDetectedPtr CsLogic::scanAppDelta(const FilePtr &pkgPtr, std::string &riskiest
 }
 
 int CsLogic::scanApp(const CsContext &context, const FilePtr &pkgPtr,
-                                        CsDetectedPtr &malware)
+                                        CsDetectedPtr &malware, const std::function<void()> &isCancelled)
 {
        const auto &pkgPath = pkgPtr->getName();
        const auto &pkgId = pkgPtr->getAppPkgId();
@@ -303,7 +306,7 @@ int CsLogic::scanApp(const CsContext &context, const FilePtr &pkgPtr,
        auto history = this->m_db->getWorstByPkgPath(pkgPath, since);
        // riskiest detected among newly scanned files
        std::string riskiestPath;
-       auto riskiest = this->scanAppDelta(pkgPtr, riskiestPath);
+       auto riskiest = this->scanAppDelta(pkgPtr, riskiestPath, isCancelled);
        // history after delta scan. if worst file is changed, it's rescanned in scanAppDelta
        // and deleted from db if it's cured. if history != nullptr && after == nullptr,
        // it means worst detected item is cured anyway.
@@ -426,10 +429,10 @@ int CsLogic::scanApp(const CsContext &context, const FilePtr &pkgPtr,
 }
 
 int CsLogic::scanFileInternal(const CsContext &context, const FilePtr &target,
-                                                         CsDetectedPtr &malware)
+                                                         CsDetectedPtr &malware, const std::function<void()> &isCancelled)
 {
        if (target->isInApp())
-               return this->scanApp(context, target, malware);
+               return this->scanApp(context, target, malware, isCancelled);
 
        const auto &name = target->getName();
 
@@ -524,7 +527,7 @@ RawBuffer CsLogic::scanFilesAsync(const ConnShPtr &conn, const CsContext &contex
                }
 
                CsDetectedPtr malware;
-               auto retcode = this->scanFileInternal(context, target, malware);
+               auto retcode = this->scanFileInternal(context, target, malware, isCancelled);
 
                switch (retcode) {
                case CSR_ERROR_NONE:
index 3399fbc..1cc4cc7 100644 (file)
@@ -40,6 +40,8 @@ namespace Csr {
 
 class CsLogic : public Logic {
 public:
+       using CancelChecker = std::function<void()>;
+
        CsLogic(const std::shared_ptr<CsLoader> &loader,
                        const std::shared_ptr<Db::Manager> &db);
        virtual ~CsLogic() = default;
@@ -47,9 +49,9 @@ public:
        RawBuffer scanData(const CsContext &context, const RawBuffer &data);
        RawBuffer scanFile(const CsContext &context, const std::string &filepath);
        RawBuffer scanFilesAsync(const ConnShPtr &conn, const CsContext &context,
-                                                        StrSet &paths, const std::function<void()> &isCancelled);
+                                                        StrSet &paths, const CancelChecker &isCancelled);
        RawBuffer scanDirsAsync(const ConnShPtr &conn, const CsContext &context,
-                                                       StrSet &paths, const std::function<void()> &isCancelled);
+                                                       StrSet &paths, const CancelChecker &isCancelled);
        RawBuffer judgeStatus(const std::string &filepath, csr_cs_action_e action);
        RawBuffer getDetected(const std::string &filepath);
        RawBuffer getDetectedList(const StrSet &dirSet);
@@ -57,10 +59,10 @@ public:
        RawBuffer getIgnoredList(const StrSet &dirSet);
 
 private:
-       int scanFileInternal(const CsContext &context, const FilePtr &target, CsDetectedPtr &malware);
-       int scanApp(const CsContext &context, const FilePtr &pkgPtr, CsDetectedPtr &malware);
+       int scanFileInternal(const CsContext &context, const FilePtr &target, CsDetectedPtr &malware, const CancelChecker &isCancelled = nullptr);
+       int scanApp(const CsContext &context, const FilePtr &pkgPtr, CsDetectedPtr &malware, const CancelChecker &isCancelled = nullptr);
        int scanAppOnCloud(const CsContext &context, const FilePtr &pkgPtr, CsDetectedPtr &malware);
-       CsDetectedPtr scanAppDelta(const FilePtr &pkgPtr, std::string &riskiestPath);
+       CsDetectedPtr scanAppDelta(const FilePtr &pkgPtr, std::string &riskiestPath, const CancelChecker &isCancelled = nullptr);
 
        CsDetectedPtr convert(csre_cs_detected_h &result, const std::string &targetName,
                                           time_t timestamp);