Malware data deprecation 08/76708/2
authorKyungwook Tak <k.tak@samsung.com>
Mon, 27 Jun 2016 04:23:55 +0000 (13:23 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Mon, 27 Jun 2016 04:31:12 +0000 (13:31 +0900)
Data deprecation based on engine latest update time which is provided by
engine API(csre_cs_engine_latest_update_time)

Change-Id: I8f2b9a2118a2b575493b786c149e70bd93ba9e48
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
15 files changed:
data/scripts/create_schema.sql
src/framework/db/manager.cpp
src/framework/db/manager.h
src/framework/db/query.h
src/framework/db/row.h
src/framework/service/cs-loader.cpp
src/framework/service/cs-loader.h
src/framework/service/cs-logic.cpp
src/framework/service/em-logic.cpp
src/framework/service/wp-loader.cpp
src/framework/service/wp-loader.h
test/engine/content-screening/sample-engine.cpp
test/internals/test-cs-loader.cpp
test/internals/test-db.cpp
test/internals/test-wp-loader.cpp

index f690928..2755b7b 100644 (file)
@@ -36,7 +36,6 @@ CREATE TABLE IF NOT EXISTS ENGINE_STATE (
 CREATE TABLE IF NOT EXISTS SCAN_REQUEST (
        dir TEXT NOT NULL,
        last_scan INTEGER NOT NULL,
-       data_version TEXT NOT NULL,
 
        PRIMARY KEY(dir)
 );
@@ -53,7 +52,6 @@ CREATE TABLE IF NOT EXISTS DETECTED_MALWARE (
        filepath_idx INTEGER PRIMARY KEY AUTOINCREMENT,
        file_path TEXT NOT NULL,
        idx INTEGER NOT NULL,
-       data_version TEXT NOT NULL,
        malware_name TEXT NOT NULL,
        detailed_url TEXT NOT NULL,
        severity INTEGER NOT NULL,
@@ -66,7 +64,6 @@ CREATE TABLE IF NOT EXISTS DETECTED_MALWARE (
 CREATE TABLE IF NOT EXISTS DETECTED_MALWARE_CLOUD (
        idx INTEGER NOT NULL,
        pkg_id TEXT NOT NULL,
-       data_version TEXT NOT NULL,
        malware_name TEXT NOT NULL,
        detailed_url TEXT NOT NULL,
        severity INTEGER NOT NULL,
@@ -86,32 +83,32 @@ CREATE TABLE IF NOT EXISTS PACKAGE_INFO (
 );
 
 CREATE VIEW IF NOT EXISTS [join_detecteds_cloud_by_name] AS
-       SELECT N.name, D.data_version, D.malware_name, D.detailed_url, D.severity,
+       SELECT N.name, D.malware_name, D.detailed_url, D.severity,
                        D.detected_time, D.pkg_id, N.is_ignored
                FROM NAMES AS N INNER JOIN DETECTED_MALWARE_CLOUD AS D ON N.idx = D.idx;
 
 CREATE VIEW IF NOT EXISTS [join_p_d] AS
-       SELECT N.idx, N.name, D.file_path, D.data_version, D.malware_name, D.detailed_url,
+       SELECT N.idx, N.name, D.file_path, D.malware_name, D.detailed_url,
                        D.severity, D.detected_time, P.pkg_id
                FROM (PACKAGE_INFO AS P INNER JOIN DETECTED_MALWARE AS D ON P.worst_filepath_idx = D.filepath_idx)
                        AS J INNER JOIN NAMES AS N ON J.idx = N.idx;
 
 CREATE VIEW IF NOT EXISTS [join_detecteds_by_name] AS
-       SELECT N.name, J.file_path, J.data_version, J.malware_name, J.detailed_url,
+       SELECT N.name, J.file_path, J.malware_name, J.detailed_url,
                        J.severity, J.detected_time, J.pkg_id, N.is_ignored
                FROM (
-                       SELECT D.idx, D.file_path, D.data_version, D.malware_name, D.detailed_url,
+                       SELECT D.idx, D.file_path, D.malware_name, D.detailed_url,
                                        D.severity, D.detected_time, P.pkg_id
                                FROM DETECTED_MALWARE AS D LEFT JOIN PACKAGE_INFO AS P ON D.idx = P.idx
                                WHERE NOT EXISTS (SELECT * FROM join_p_d WHERE idx = D.idx)
                        UNION
-                       SELECT idx, file_path, data_version, malware_name, detailed_url,
+                       SELECT idx, file_path, malware_name, detailed_url,
                                        severity, detected_time, pkg_id
                                FROM join_p_d)
                        AS J INNER JOIN NAMES AS N ON J.idx = N.idx;
 
 CREATE VIEW IF NOT EXISTS [join_detecteds_by_file_path] AS
-       SELECT N.name, D.file_path, D.data_version, D.malware_name, D.detailed_url,
+       SELECT N.name, D.file_path, D.malware_name, D.detailed_url,
                        D.severity, D.detected_time, P.pkg_id, N.is_ignored
                FROM (DETECTED_MALWARE AS D LEFT JOIN PACKAGE_INFO AS P ON D.idx = P.idx)
                        AS J INNER JOIN NAMES AS N ON J.idx = N.idx;
index 932f690..e9b3a6c 100644 (file)
@@ -53,7 +53,6 @@ RowShPtr extractRow(Statement &stmt)
 
        row->targetName = stmt.getText(); // name.
        row->fileInAppPath = stmt.getText(); // file_path
-       row->dataVersion = stmt.getText(); // data_version
        row->malwareName = stmt.getText(); // malware_name
        row->detailedUrl = stmt.getText(); // detailed_url
        row->severity = static_cast<csr_cs_severity_level_e>(stmt.getInt()); // severity
@@ -71,7 +70,6 @@ RowShPtr extractRowCloud(Statement &stmt)
 
        row->targetName = stmt.getText(); // name.
        row->fileInAppPath.clear();
-       row->dataVersion = stmt.getText(); // data_version
        row->malwareName = stmt.getText(); // malware_name
        row->detailedUrl = stmt.getText(); // detailed_url
        row->severity = static_cast<csr_cs_severity_level_e>(stmt.getInt()); // severity
@@ -239,8 +237,7 @@ void Manager::setEngineState(csr_engine_id_e id, csr_state_e state)
 // SCAN_REQUEST table
 //===========================================================================
 
-time_t Manager::getLastScanTime(const std::string &dir,
-                                                               const std::string &dataVersion)
+time_t Manager::getLastScanTime(const std::string &dir, time_t since)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
@@ -250,11 +247,10 @@ time_t Manager::getLastScanTime(const std::string &dir,
 
        while (true) {
                stmt.bind(current);
-               stmt.bind(dataVersion);
 
                if (stmt.step()) {
                        auto candidate = static_cast<time_t>(stmt.getInt64());
-                       if (latest < candidate)
+                       if ((since < 0 || since < candidate) && latest < candidate)
                                latest = candidate;
                }
 
@@ -270,8 +266,7 @@ time_t Manager::getLastScanTime(const std::string &dir,
        return latest;
 }
 
-void Manager::insertLastScanTime(const std::string &dir, time_t scanTime,
-                                                                const std::string &dataVersion)
+void Manager::insertLastScanTime(const std::string &dir, time_t scanTime)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
@@ -279,7 +274,6 @@ void Manager::insertLastScanTime(const std::string &dir, time_t scanTime,
 
        stmt.bind(dir);
        stmt.bind(static_cast<sqlite3_int64>(scanTime));
-       stmt.bind(dataVersion);
        stmt.exec();
 }
 
@@ -451,7 +445,6 @@ RowShPtr Manager::getWorstByPkgPath(const std::string &pkgPath)
 
        row->targetName = stmt.getText(); // name
        row->fileInAppPath = stmt.getText(); // file_path
-       row->dataVersion = stmt.getText(); // data_version
        row->malwareName = stmt.getText(); // malware_name
        row->detailedUrl = stmt.getText(); // detailed_url
        row->severity = static_cast<csr_cs_severity_level_e>(stmt.getInt()); // severity
@@ -462,31 +455,30 @@ RowShPtr Manager::getWorstByPkgPath(const std::string &pkgPath)
        return row;
 }
 
-void Manager::insertDetectedFile(const std::string &filepath, const CsDetected &d,
-                                                                const std::string &dataVersion)
+void Manager::insertDetectedFile(const std::string &filepath, const CsDetected &d)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
        this->insertName(filepath);
-       this->insertDetected(d, filepath, dataVersion);
+       this->insertDetected(d, filepath);
 }
 
 void Manager::insertDetectedFileInApp(const std::string &pkgpath, const std::string &filepath,
-                                                                         const CsDetected &d, const std::string &dataVersion)
+                                                                         const CsDetected &d)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
        this->insertName(pkgpath);
-       this->insertDetected(d, filepath, dataVersion);
+       this->insertDetected(d, filepath);
 }
 
 void Manager::insertDetectedAppByCloud(const std::string &name, const std::string &pkgId,
-                                                                          const CsDetected &d, const std::string &dataVersion)
+                                                                          const CsDetected &d)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
        this->insertName(name);
-       this->insertDetectedCloud(d, pkgId, name, dataVersion);
+       this->insertDetectedCloud(d, pkgId, name);
 }
 
 void Manager::insertName(const std::string &name)
@@ -497,14 +489,12 @@ void Manager::insertName(const std::string &name)
        stmt.exec();
 }
 
-void Manager::insertDetected(const CsDetected &d, const std::string &filepath,
-                                                        const std::string &dataVersion)
+void Manager::insertDetected(const CsDetected &d, const std::string &filepath)
 {
        Statement stmt(this->m_conn, Query::INS_DETECTED);
 
        stmt.bind(filepath);
        stmt.bind(d.targetName);
-       stmt.bind(dataVersion);
        stmt.bind(d.malwareName);
        stmt.bind(d.detailedUrl);
        stmt.bind(static_cast<int>(d.severity));
@@ -513,13 +503,12 @@ void Manager::insertDetected(const CsDetected &d, const std::string &filepath,
 }
 
 void Manager::insertDetectedCloud(const CsDetected &d, const std::string &pkgId,
-                                                                 const std::string &name, const std::string &dataVersion)
+                                                                 const std::string &name)
 {
        Statement stmt(this->m_conn, Query::INS_DETECTED_CLOUD);
 
        stmt.bind(name);
        stmt.bind(pkgId);
-       stmt.bind(dataVersion);
        stmt.bind(d.malwareName);
        stmt.bind(d.detailedUrl);
        stmt.bind(static_cast<int>(d.severity));
@@ -571,16 +560,19 @@ void Manager::deleteDetectedByFilepathOnPath(const std::string &path)
        stmt.exec();
 }
 
-void Manager::deleteDetectedDeprecatedOnDir(const std::string &dir,
-                                                                                       const std::string &dataVersion)
+void Manager::deleteDetectedDeprecated(time_t t)
 {
        std::lock_guard<std::mutex> l(this->m_mutex);
 
-       Statement stmt(this->m_conn, Query::DEL_DETECTED_DEPRECATED_ON_DIR);
+       Statement stmt(this->m_conn, Query::DEL_DETECTED_DEPRECATED);
 
-       stmt.bind(dir);
-       stmt.bind(dataVersion);
+       stmt.bind(static_cast<sqlite3_int64>(t));
        stmt.exec();
+
+       Statement stmt2(this->m_conn, Query::DEL_DETECTED_DEPRECATED_CLOUD);
+
+       stmt2.bind(static_cast<sqlite3_int64>(t));
+       stmt2.exec();
 }
 
 } // namespace Db
index d128d90..8a4a00c 100644 (file)
@@ -26,6 +26,7 @@
 #include <memory>
 #include <map>
 #include <mutex>
+#include <ctime>
 
 #include "db/connection.h"
 #include "db/row.h"
@@ -54,9 +55,8 @@ public:
        void setEngineState(csr_engine_id_e, csr_state_e);
 
        // SCAN_REQUEST
-       time_t getLastScanTime(const std::string &dir, const std::string &dataVersion);
-       void insertLastScanTime(const std::string &dir, time_t scanTime,
-                                                       const std::string &dataVersion);
+       time_t getLastScanTime(const std::string &dir, time_t since);
+       void insertLastScanTime(const std::string &dir, time_t scanTime);
        void deleteLastScanTime(const std::string &dir);
        void cleanLastScanTime();
 
@@ -68,30 +68,27 @@ public:
        RowShPtrs getDetectedByFilepathOnDir(const std::string &dir);
        RowShPtr getWorstByPkgPath(const std::string &pkgPath);
 
-       void insertDetectedFile(const std::string &filepath, const CsDetected &d,
-                                                       const std::string &dataVersion);
+       void insertDetectedFile(const std::string &filepath, const CsDetected &d);
        void insertDetectedFileInApp(const std::string &pkgpath, const std::string &filepath,
-                                                                const CsDetected &d, const std::string &dataVersion);
+                                                                const CsDetected &d);
        void insertDetectedAppByCloud(const std::string &name, const std::string &pkgId,
-                                                        const CsDetected &d, const std::string &dataVersion);
+                                                        const CsDetected &d);
        void insertWorst(const std::string &pkgId, const std::string &name,
                                         const std::string &filepath);
 
        void updateIgnoreFlag(const std::string &name, bool flag);
        void deleteDetectedByNameOnPath(const std::string &path);
        void deleteDetectedByFilepathOnPath(const std::string &path);
-       void deleteDetectedDeprecatedOnDir(const std::string &dir,
-                                                                          const std::string &dataVersion);
+       void deleteDetectedDeprecated(time_t t);
 
 private:
        RowShPtrs getDetectedByNameOnDir(const std::string &dir);
        RowShPtrs getDetectedCloudByNameOnDir(const std::string &dir);
 
        void insertName(const std::string &name);
-       void insertDetected(const CsDetected &d, const std::string &filename,
-                                               const std::string &dataVersion);
+       void insertDetected(const CsDetected &d, const std::string &filename);
        void insertDetectedCloud(const CsDetected &d, const std::string &pkgId,
-                                                        const std::string &name, const std::string &dataVersion);
+                                                        const std::string &name);
 
        void resetDatabase();
        bool isTableExist(const std::string &name);
index bab73b1..d782452 100644 (file)
@@ -38,11 +38,11 @@ const std::string SEL_ENGINE_STATE_ALL =
        "select id, state from ENGINE_STATE";
 
 const std::string SEL_SCAN_REQUEST =
-       "select last_scan from SCAN_REQUEST where dir = ? and data_version = ?";
+       "select last_scan from SCAN_REQUEST where dir = ?";
 
 const std::string INS_SCAN_REQUEST =
-       "insert or replace into SCAN_REQUEST (dir, last_scan, data_version)"
-       " values (?, ?, ?)";
+       "insert or replace into SCAN_REQUEST (dir, last_scan)"
+       " values (?, ?)";
 
 const std::string DEL_SCAN_REQUEST_BY_DIR =
        "delete from SCAN_REQUEST where dir = ?";
@@ -51,37 +51,37 @@ const std::string DEL_SCAN_REQUEST =
        "delete from SCAN_REQUEST";
 
 const std::string SEL_DETECTED_CLOUD_BY_NAME_ON_PATH =
-       "select name, data_version, malware_name, detailed_url, severity, detected_time,"
+       "select name, malware_name, detailed_url, severity, detected_time,"
        "       pkg_id, is_ignored"
        " from join_detecteds_cloud_by_name"
        " where name = ?";
 
 const std::string SEL_DETECTED_BY_NAME_ON_PATH =
-       "select name, file_path, data_version, malware_name, detailed_url, severity,"
+       "select name, file_path, malware_name, detailed_url, severity,"
        "       detected_time, pkg_id, is_ignored"
        " from join_detecteds_by_name"
        " where name = ?";
 
 const std::string SEL_DETECTED_CLOUD_BY_NAME_ON_DIR =
-       "select name, data_version, malware_name, detailed_url, severity, detected_time,"
+       "select name, malware_name, detailed_url, severity, detected_time,"
        "       pkg_id, is_ignored"
        " from join_detecteds_cloud_by_name"
        " where name like ? || '%'";
 
 const std::string SEL_DETECTED_BY_NAME_ON_DIR =
-       "select name, file_path, data_version, malware_name, detailed_url, severity,"
+       "select name, file_path, malware_name, detailed_url, severity,"
        "       detected_time, pkg_id, is_ignored"
        " from join_detecteds_by_name"
        " where name like ? || '%'";
 
 const std::string SEL_DETECTED_BY_FILEPATH_ON_DIR =
-       "select name, file_path, data_version, malware_name, detailed_url, severity,"
+       "select name, file_path, malware_name, detailed_url, severity,"
        "       detected_time, pkg_id, is_ignored"
        " from join_detecteds_by_file_path"
        " where file_path like ? || '%'";
 
 const std::string SEL_WORST_BY_PKGPATH =
-       "select name, file_path, data_version, malware_name, detailed_url, severity,"
+       "select name, file_path, malware_name, detailed_url, severity,"
        "       detected_time, pkg_id"
        " from join_p_d"
        " where name = ?";
@@ -90,15 +90,15 @@ const std::string INS_NAME =
        "insert or replace into NAMES(name) values(?)";
 
 const std::string INS_DETECTED_CLOUD =
-       "insert or replace into DETECTED_MALWARE_CLOUD(idx, pkg_id, data_version,"
+       "insert or replace into DETECTED_MALWARE_CLOUD(idx, pkg_id,"
        "                                              malware_name, detailed_url, severity,"
        "                                              detected_time)"
-       " values((select idx from NAMES where name = ?), ?, ?, ?, ?, ?, ?)";
+       " values((select idx from NAMES where name = ?), ?, ?, ?, ?, ?)";
 
 const std::string INS_DETECTED =
-       "insert or replace into DETECTED_MALWARE(file_path, idx, data_version, malware_name,"
+       "insert or replace into DETECTED_MALWARE(file_path, idx, malware_name,"
        "                                        detailed_url, severity, detected_time)"
-       " values(?, (select idx from NAMES where name = ?), ?, ?, ?, ?, ?)";
+       " values(?, (select idx from NAMES where name = ?), ?, ?, ?, ?)";
 
 const std::string INS_WORST =
        "insert or replace into PACKAGE_INFO(pkg_id, idx, worst_filepath_idx)"
@@ -115,9 +115,11 @@ const std::string DEL_DETECTED_BY_NAME_ON_PATH =
 const std::string DEL_DETECTED_BY_FILEPATH_ON_PATH =
        "delete from DETECTED_MALWARE where file_path = ?";
 
-const std::string DEL_DETECTED_DEPRECATED_ON_DIR =
-       "delete from DETECTED_MALWARE where file_path like ? || '%' "
-       " and data_version != ?";
+const std::string DEL_DETECTED_DEPRECATED =
+       "delete from DETECTED_MALWARE where detected_time < ?";
+
+const std::string DEL_DETECTED_DEPRECATED_CLOUD =
+       "delete from DETECTED_MALWARE_CLOUD where detected_time < ?";
 
 } // namespace Query
 } // namespace Db
index c6f23ab..fa1a521 100644 (file)
@@ -34,7 +34,6 @@ using RowShPtrs = std::vector<RowShPtr>;
 
 struct Row : public Csr::CsDetected {
        std::string fileInAppPath; // for case of file in app
-       std::string dataVersion; // engine's data version
        bool isIgnored;
 
        Row() : isIgnored(false) {}
index 524b0b7..487e6cb 100644 (file)
@@ -207,12 +207,16 @@ void CsLoader::getEngineDataVersion(csre_cs_context_h c, std::string &value)
        }));
 }
 
-void CsLoader::getEngineLatestUpdateTime(csre_cs_context_h c, time_t *ptime)
+time_t CsLoader::getEngineLatestUpdateTime(csre_cs_context_h c)
 {
-       if (c == nullptr || ptime == nullptr)
+       if (c == nullptr)
                ThrowExc(CSR_ERROR_INVALID_PARAMETER, "cs loader get latest update time");
 
-       this->toException(this->m_pc.fpGetEngineLatestUpdateTime(c, ptime));
+       time_t t;
+
+       this->toException(this->m_pc.fpGetEngineLatestUpdateTime(c, &t));
+
+       return t;
 }
 
 void CsLoader::getEngineActivated(csre_cs_context_h c, csre_cs_activated_e *pactivated)
index 924243b..23088c8 100644 (file)
@@ -56,7 +56,7 @@ public:
        void getEngineName(csre_cs_context_h, std::string &);
        void getEngineVersion(csre_cs_context_h, std::string &);
        void getEngineDataVersion(csre_cs_context_h, std::string &);
-       void getEngineLatestUpdateTime(csre_cs_context_h, time_t *);
+       time_t getEngineLatestUpdateTime(csre_cs_context_h);
        void getEngineActivated(csre_cs_context_h, csre_cs_activated_e *);
        void getEngineVendorLogo(csre_cs_context_h, std::vector<unsigned char> &);
 
index 7b926e3..2a0c4df 100644 (file)
@@ -139,6 +139,8 @@ CsLogic::CsLogic(const std::shared_ptr<CsLoader> &loader,
        if (this->m_loader) {
                CsEngineContext csEngineContext(this->m_loader);
                this->m_loader->getEngineDataVersion(csEngineContext.get(), this->m_dataVersion);
+               this->m_db->deleteDetectedDeprecated(
+                               this->m_loader->getEngineLatestUpdateTime(csEngineContext.get()));
        }
 }
 
@@ -186,7 +188,7 @@ RawBuffer CsLogic::scanAppOnCloud(const CsContext &context,
        detected.isApp = true;
        detected.pkgId = pkgId;
 
-       this->m_db->insertDetectedAppByCloud(pkgPath, pkgId, detected, this->m_dataVersion);
+       this->m_db->insertDetectedAppByCloud(pkgPath, pkgId, detected);
 
        return this->handleAskUser(context, detected);
 }
@@ -199,7 +201,8 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
        CsEngineContext engineContext(this->m_loader);
        auto &c = engineContext.get();
 
-       auto lastScanTime = this->m_db->getLastScanTime(pkgPath, this->m_dataVersion);
+       auto t = this->m_loader->getEngineLatestUpdateTime(c);
+       auto lastScanTime = this->m_db->getLastScanTime(pkgPath, t);
 
        // traverse files in app and take which is more danger than riskiest
        auto visitor = FsVisitor::create(pkgPath, lastScanTime);
@@ -227,8 +230,7 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
                candidate.isApp = true;
                candidate.pkgId = pkgId;
 
-               this->m_db->insertDetectedFileInApp(pkgPath, file->getPath(), candidate,
-                                                                                       this->m_dataVersion);
+               this->m_db->insertDetectedFileInApp(pkgPath, file->getPath(), candidate);
 
                if (!riskiest) {
                        riskiest.reset(new CsDetected(std::move(candidate)));
@@ -239,7 +241,7 @@ CsDetectedPtr CsLogic::scanAppDelta(const std::string &pkgPath, const std::strin
                }
        }
 
-       this->m_db->insertLastScanTime(pkgPath, starttime, this->m_dataVersion);
+       this->m_db->insertLastScanTime(pkgPath, starttime);
 
        return riskiest;
 }
@@ -390,7 +392,7 @@ RawBuffer CsLogic::scanFileWithoutDelta(const CsContext &context,
 
        auto d = this->convert(result, filepath, timestamp);
 
-       this->m_db->insertDetectedFile(d.targetName, d, this->m_dataVersion);
+       this->m_db->insertDetectedFile(d.targetName, d);
 
        return this->handleAskUser(context, d, std::forward<FilePtr>(fileptr));
 }
@@ -415,10 +417,16 @@ RawBuffer CsLogic::scanFile(const CsContext &context, const std::string &filepat
 
        // if history exist, fileptr can be null because of modified since value
        // from history.
-       if (history)
-               fileptr = File::createIfModified(target, static_cast<time_t>(history->ts));
-       else
+       if (history) {
+               CsEngineContext engineContext(this->m_loader);
+               auto t = this->m_loader->getEngineLatestUpdateTime(engineContext.get());
+               if (t < history->ts)
+                       fileptr = File::createIfModified(target, history->ts);
+               else
+                       fileptr = File::create(target);
+       } else {
                fileptr = File::create(target);
+       }
 
        // non-null fileptr means the file is modified since the last history
        // OR there's no history at all.
@@ -474,7 +482,10 @@ RawBuffer CsLogic::getScannableFiles(const std::string &dir, const std::function
        if (this->m_db->getEngineState(CSR_ENGINE_CS) != CSR_STATE_ENABLE)
                ThrowExc(CSR_ERROR_ENGINE_DISABLED, "engine is disabled");
 
-       auto lastScanTime = this->m_db->getLastScanTime(dir, this->m_dataVersion);
+       CsEngineContext engineContext(this->m_loader);
+
+       auto t = this->m_loader->getEngineLatestUpdateTime(engineContext.get());
+       auto lastScanTime = this->m_db->getLastScanTime(dir, t);
 
        auto visitor = FsVisitor::create(dir, lastScanTime);
 
@@ -538,7 +549,7 @@ RawBuffer CsLogic::canonicalizePaths(const StrSet &paths)
 
 RawBuffer CsLogic::setDirTimestamp(const std::string &dir, time_t ts)
 {
-       this->m_db->insertLastScanTime(dir, ts, this->m_dataVersion);
+       this->m_db->insertLastScanTime(dir, ts);
 
        return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
 }
index be636e4..9043450 100644 (file)
@@ -157,20 +157,14 @@ RawBuffer EmLogic::getEngineUpdatedTime(const EmContext &context)
                CsEngineContext engineContext(this->m_cs);
                auto &c = engineContext.get();
 
-               time_t value;
-               this->m_cs->getEngineLatestUpdateTime(c, &value);
-
-               int64_t ts64 = static_cast<int64_t>(value);
+               auto ts64 = static_cast<int64_t>(this->m_cs->getEngineLatestUpdateTime(c));
 
                return BinaryQueue::Serialize(CSR_ERROR_NONE, ts64).pop();
        } else {
                WpEngineContext engineContext(this->m_wp);
                auto &c = engineContext.get();
 
-               time_t value;
-               this->m_wp->getEngineLatestUpdateTime(c, &value);
-
-               int64_t ts64 = static_cast<int64_t>(value);
+               auto ts64 = static_cast<int64_t>(this->m_wp->getEngineLatestUpdateTime(c));
 
                return BinaryQueue::Serialize(CSR_ERROR_NONE, ts64).pop();
        }
index 755ae59..d1021b6 100644 (file)
@@ -158,12 +158,16 @@ void WpLoader::getEngineDataVersion(csre_wp_context_h c, std::string &value)
        }));
 }
 
-void WpLoader::getEngineLatestUpdateTime(csre_wp_context_h c, time_t *ptime)
+time_t WpLoader::getEngineLatestUpdateTime(csre_wp_context_h c)
 {
-       if (c == nullptr || ptime == nullptr)
+       if (c == nullptr)
                ThrowExc(CSR_ERROR_INVALID_PARAMETER, "wp loader get latest update time");
 
-       this->toException(this->m_pc.fpGetEngineLatestUpdateTime(c, ptime));
+       time_t t;
+
+       this->toException(this->m_pc.fpGetEngineLatestUpdateTime(c, &t));
+
+       return t;
 }
 
 void WpLoader::getEngineActivated(csre_wp_context_h c, csre_wp_activated_e *pactivated)
index 22835e8..b926922 100644 (file)
@@ -50,7 +50,7 @@ public:
        void getEngineName(csre_wp_context_h, std::string &);
        void getEngineVersion(csre_wp_context_h, std::string &);
        void getEngineDataVersion(csre_wp_context_h, std::string &);
-       void getEngineLatestUpdateTime(csre_wp_context_h, time_t *);
+       time_t getEngineLatestUpdateTime(csre_wp_context_h);
        void getEngineActivated(csre_wp_context_h, csre_wp_activated_e *);
        void getEngineVendorLogo(csre_wp_context_h, std::vector<unsigned char> &);
 
index a97b5b4..11063ca 100644 (file)
@@ -30,6 +30,8 @@
 #include <fstream>
 #include <iostream>
 #include <climits>
+#include <ctime>
+#include <cstring>
 #include <sys/time.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -88,8 +90,8 @@ enum csret_cs_internal_error_e {
 //==============================================================================
 // static variables
 //==============================================================================
-static std::string g_resdir;
-static std::string g_workingdir;
+static std::string g_private_db_path;
+static std::string g_private_logo_path;
 static std::list<csret_cs_malware_s> g_virus_sig;
 
 //==============================================================================
@@ -126,7 +128,7 @@ int csret_cs_read_virus_signatures(const std::string &path)
        // threat_type=RISKY
        // detailed_url=http://medium.malware.com
        // signature=RISKY_MALWARE
-       std::ifstream f(path.c_str());
+       std::ifstream f(path.c_str(), std::ifstream::in);
 
        if (!f.is_open())
                return CSRET_CS_ERROR_NO_SIGNATURE_FILE;
@@ -243,7 +245,7 @@ int csret_cs_init_engine(csret_cs_engine_s **pengine)
        ptr->engineVersion = ENGINE_VERSION;
        ptr->dataVersion = ENGINE_VERSION;
 
-       int ret = csret_cs_read_binary(g_resdir + "/" PRIVATE_LOGO_FILE, ptr->logoImage);
+       int ret = csret_cs_read_binary(g_private_logo_path, ptr->logoImage);
 
        if (ret != CSRE_ERROR_NONE) {
                delete ptr;
@@ -251,8 +253,12 @@ int csret_cs_init_engine(csret_cs_engine_s **pengine)
        }
 
        struct stat attrib;
-
-       stat(PRIVATE_DB_NAME, &attrib);
+       ::memset(&attrib, 0x00, sizeof(attrib));
+       ret = ::stat(g_private_db_path.c_str(), &attrib);
+       if (ret != 0) {
+               delete ptr;
+               return CSRET_CS_ERROR_NO_SIGNATURE_FILE;
+       }
 
        ptr->latestUpdate = attrib.st_mtime;
 
@@ -323,12 +329,12 @@ int csre_cs_global_initialize(const char *ro_res_dir, const char *rw_working_dir
        if (ro_res_dir == nullptr || rw_working_dir == nullptr)
                return CSRE_ERROR_INVALID_PARAMETER;
 
-       g_resdir = ro_res_dir;
-       g_workingdir = rw_working_dir;
+       g_private_db_path = std::string(rw_working_dir) + "/" PRIVATE_DB_NAME;
+       g_private_logo_path = std::string(ro_res_dir) + "/" PRIVATE_LOGO_FILE;
 
        g_virus_sig.clear();
 
-       return csret_cs_read_virus_signatures(g_workingdir + "/" PRIVATE_DB_NAME);
+       return csret_cs_read_virus_signatures(g_private_db_path);
 }
 
 API
index ed8aec0..e6ed0df 100644 (file)
@@ -321,8 +321,8 @@ BOOST_AUTO_TEST_CASE(get_latest_update_time)
 
        Handle h;
 
-       time_t time = 0;
-       h.loader.getEngineLatestUpdateTime(h.context, &time);
+       auto time = h.loader.getEngineLatestUpdateTime(h.context);
+       BOOST_MESSAGE("engine latest update time: " << time);
 
        EXCEPTION_GUARD_END
 }
index 41611a3..f09b5ec 100644 (file)
@@ -89,13 +89,13 @@ BOOST_AUTO_TEST_CASE(scan_time)
        std::string dataVersion = "1.0.0";
 
        db.cleanLastScanTime();
-       ASSERT_IF(db.getLastScanTime(dir, dataVersion), -1);
-       db.insertLastScanTime(dir, scantime, dataVersion);
+       ASSERT_IF(db.getLastScanTime(dir, -1), -1);
+       db.insertLastScanTime(dir, scantime);
 
-       db.insertLastScanTime("/opt/data", scantime + 100, dataVersion);
-       db.insertLastScanTime("/opt/data/etc", scantime + 200, dataVersion);
+       db.insertLastScanTime("/opt/data", scantime + 100);
+       db.insertLastScanTime("/opt/data/etc", scantime + 200);
 
-       ASSERT_IF(db.getLastScanTime(dir, dataVersion), scantime);
+       ASSERT_IF(db.getLastScanTime(dir, -1), scantime);
        db.cleanLastScanTime();
 
        EXCEPTION_GUARD_END
@@ -110,27 +110,29 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
        std::string initDataVersion = "1.0.0";
        std::string changedDataVersion = "2.0.0";
 
+       auto starttime = ::time(nullptr);
+
        // insert
        CsDetected malware1;
        malware1.targetName = "/opt/testmalware1";
        malware1.severity = CSR_CS_SEVERITY_MEDIUM;
        malware1.malwareName = "testmalware1";
        malware1.detailedUrl = "http://detailed.malware.com";
-       malware1.ts = 100;
+       malware1.ts = 1;
 
        CsDetected malware2;
        malware2.targetName = "/opt/testmalware2";
        malware2.severity = CSR_CS_SEVERITY_HIGH;
        malware2.malwareName = "testmalware2";
        malware2.detailedUrl = "http://detailed2.malware.com";
-       malware2.ts = 210;
+       malware2.ts = 2;
 
        CsDetected malware3;
        malware3.targetName = "/opt/testmalware3";
        malware3.severity = CSR_CS_SEVERITY_LOW;
        malware3.malwareName = "testmalware3";
        malware3.detailedUrl = "http://detailed3.malware.com";
-       malware3.ts = 310;
+       malware3.ts = starttime;
 
        // select test with vacant data
        auto detected = db.getDetectedByNameOnPath(malware1.targetName);
@@ -139,17 +141,15 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
        auto detectedList = db.getDetectedAllByNameOnDir("/opt");
        ASSERT_IF(detectedList.empty(), true);
 
-       db.insertDetectedFile(malware1.targetName, malware1, initDataVersion);
+       db.insertDetectedFile(malware1.targetName, malware1);
        detected = db.getDetectedByNameOnPath(malware1.targetName);
        checkSameMalware(malware1, *detected);
-       ASSERT_IF(detected->dataVersion, initDataVersion);
        ASSERT_IF(detected->isIgnored, false);
 
-       db.insertDetectedFile(malware2.targetName, malware2, initDataVersion);
+       db.insertDetectedFile(malware2.targetName, malware2);
        db.updateIgnoreFlag(malware2.targetName, true);
        detected = db.getDetectedByNameOnPath(malware2.targetName);
        checkSameMalware(malware2, *detected);
-       ASSERT_IF(detected->dataVersion, initDataVersion);
        ASSERT_IF(detected->isIgnored, true);
 
        // getDetectedMalwares test
@@ -172,11 +172,10 @@ BOOST_AUTO_TEST_CASE(detected_malware_file)
        ASSERT_IF(detected->isIgnored, true);
 
        // deleteDeprecatedDetectedMalwares test
-       db.insertDetectedFile(malware3.targetName, malware3, changedDataVersion);
-       db.deleteDetectedDeprecatedOnDir("/opt", changedDataVersion);
+       db.insertDetectedFile(malware3.targetName, malware3);
+       db.deleteDetectedDeprecated(3);
        detected = db.getDetectedByNameOnPath(malware3.targetName);
        checkSameMalware(malware3, *detected);
-       ASSERT_IF(detected->dataVersion, changedDataVersion);
        ASSERT_IF(detected->isIgnored, false);
 
        CHECK_IS_NULL(db.getDetectedByNameOnPath(malware1.targetName));
index 8007943..2a318db 100755 (executable)
@@ -189,8 +189,8 @@ BOOST_AUTO_TEST_CASE(get_latest_update_time)
 
        Handle h;
 
-       time_t time = 0;
-       h.loader.getEngineLatestUpdateTime(h.context, &time);
+       auto time = h.loader.getEngineLatestUpdateTime(h.context);
+       BOOST_MESSAGE("engine latest update time: " << time);
 
        EXCEPTION_GUARD_END
 }