Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "action-log.hpp" |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 22 | #include "sync-core.hpp" |
| 23 | #include "core/logging.hpp" |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 24 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 25 | #include <ndn-cxx/util/string-helper.hpp> |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 26 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 27 | namespace ndn { |
| 28 | namespace chronoshare { |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 30 | _LOG_INIT(ActionLog); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 31 | |
| 32 | const std::string INIT_DATABASE = "\ |
| 33 | CREATE TABLE ActionLog ( \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 34 | device_name BLOB NOT NULL, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 35 | seq_no INTEGER NOT NULL, \n\ |
| 36 | \n\ |
| 37 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 38 | filename TEXT NOT NULL, \n\ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 39 | directory TEXT, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 40 | \n\ |
| 41 | version INTEGER NOT NULL, \n\ |
| 42 | action_timestamp TIMESTAMP NOT NULL, \n\ |
| 43 | \n\ |
| 44 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 45 | file_atime TIMESTAMP, \n\ |
| 46 | file_mtime TIMESTAMP, \n\ |
| 47 | file_ctime TIMESTAMP, \n\ |
| 48 | file_chmod INTEGER, \n\ |
| 49 | file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\ |
| 50 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 51 | parent_device_name BLOB, \n\ |
| 52 | parent_seq_no INTEGER, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 53 | \n\ |
| 54 | action_name TEXT, \n\ |
| 55 | action_content_object BLOB, \n\ |
| 56 | \n\ |
Alexander Afanasyev | 785b0a0 | 2013-01-30 10:17:47 -0800 | [diff] [blame] | 57 | PRIMARY KEY (device_name, seq_no) \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 58 | ); \n\ |
| 59 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 60 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 61 | CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 62 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 63 | CREATE INDEX ActionLog_filename_version_hash ON ActionLog (filename,version,file_hash); \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 64 | \n\ |
| 65 | CREATE TRIGGER ActionLogInsert_trigger \n\ |
| 66 | AFTER INSERT ON ActionLog \n\ |
| 67 | FOR EACH ROW \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 68 | WHEN (SELECT device_name \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 69 | FROM ActionLog \n\ |
| 70 | WHERE filename=NEW.filename AND \n\ |
| 71 | version > NEW.version) IS NULL AND \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 72 | (SELECT device_name \n\ |
| 73 | FROM ActionLog \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 74 | WHERE filename=NEW.filename AND \n\ |
| 75 | version = NEW.version AND \n\ |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 76 | device_name > NEW.device_name) IS NULL \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 77 | BEGIN \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 78 | SELECT apply_action (NEW.device_name, NEW.seq_no, \ |
Alexander Afanasyev | 6fb5dc6 | 2013-02-27 11:34:52 -0800 | [diff] [blame] | 79 | NEW.action,NEW.filename,NEW.version,NEW.file_hash, \ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 80 | strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \ |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 81 | NEW.file_chmod, NEW.file_seg_num); \n\ |
| 82 | /* function that applies action and adds record the FileState */ \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 83 | END; \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 84 | "; |
| 85 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 86 | // static void |
| 87 | // xTrace(void*, const char* q) |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 88 | // { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | // _LOG_TRACE("SQLITE: " << q); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 90 | // } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 91 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 92 | ActionLog::ActionLog(Face& face, const boost::filesystem::path& path, SyncLogPtr syncLog, |
| 93 | const std::string& sharedFolder, const std::string& appName, |
| 94 | OnFileAddedOrChangedCallback onFileAddedOrChanged, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | OnFileRemovedCallback onFileRemoved) |
| 96 | : DbHelper(path / ".chronoshare", "action-log.db") |
| 97 | , m_syncLog(syncLog) |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 98 | // , m_face(face) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 99 | , m_sharedFolderName(sharedFolder) |
| 100 | , m_appName(appName) |
| 101 | , m_onFileAddedOrChanged(onFileAddedOrChanged) |
| 102 | , m_onFileRemoved(onFileRemoved) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 103 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 104 | sqlite3_exec(m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL); |
| 105 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 785b0a0 | 2013-01-30 10:17:47 -0800 | [diff] [blame] | 106 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 107 | sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, NULL); |
| 108 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | int res = |
| 111 | sqlite3_create_function(m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*>(this), |
| 112 | ActionLog::apply_action_xFun, 0, 0); |
| 113 | if (res != SQLITE_OK) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 114 | BOOST_THROW_EXCEPTION(Error("Cannot create function ``apply_action''")); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 115 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 117 | m_fileState = make_shared<FileState>(path); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 120 | std::tuple<sqlite3_int64 /*version*/, BufferPtr /*device name*/, sqlite3_int64 /*seq_no*/> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 121 | ActionLog::GetLatestActionForFile(const std::string& filename) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 122 | { |
| 123 | // check if something already exists |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 124 | sqlite3_stmt* stmt; |
| 125 | int res = sqlite3_prepare_v2(m_db, "SELECT version,device_name,seq_no,action " |
| 126 | "FROM ActionLog " |
| 127 | "WHERE filename=? ORDER BY version DESC LIMIT 1", |
| 128 | -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 130 | if (res != SQLITE_OK) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 131 | BOOST_THROW_EXCEPTION(Error("Some error with GetExistingRecord")); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 132 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 133 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 134 | sqlite3_int64 version = -1; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 135 | BufferPtr parent_device_name; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 136 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 137 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 138 | sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); |
| 139 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 140 | version = sqlite3_column_int64(stmt, 0); |
| 141 | |
| 142 | if (sqlite3_column_int(stmt, 3) == 0) // prevent "linking" if the file was previously deleted |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 143 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 144 | parent_device_name = |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 145 | std::make_shared<Buffer>(sqlite3_column_blob(stmt, 1), sqlite3_column_bytes(stmt, 1)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | parent_seq_no = sqlite3_column_int64(stmt, 2); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 147 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 148 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | sqlite3_finalize(stmt); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 151 | return std::make_tuple(version, parent_device_name, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 154 | // local add action. remote action is extracted from content object |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 155 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 156 | ActionLog::AddLocalActionUpdate(const std::string& filename, const Buffer& hash, time_t wtime, |
| 157 | int mode, int seg_num) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 158 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 159 | sqlite3_exec(m_db, "BEGIN TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 160 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 161 | Block device_name = m_syncLog->GetLocalName().wireEncode(); |
| 162 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 163 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo(); |
| 164 | sqlite3_int64 version; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 165 | BufferPtr parent_device_name; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 166 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 167 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 168 | sqlite3_int64 action_time = std::time(0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 169 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 170 | tie(version, parent_device_name, parent_seq_no) = GetLatestActionForFile(filename); |
| 171 | version++; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 173 | sqlite3_stmt* stmt; |
| 174 | int res = |
| 175 | sqlite3_prepare_v2(m_db, |
| 176 | "INSERT INTO ActionLog " |
| 177 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 178 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
| 179 | "parent_device_name, parent_seq_no, " |
| 180 | "action_name, action_content_object) " |
| 181 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 182 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
| 183 | " ?, ?, " |
| 184 | " ?, ?);", |
| 185 | -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 186 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 187 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 189 | if (res != SQLITE_OK) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 190 | BOOST_THROW_EXCEPTION(Error(sqlite3_errmsg(m_db))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 191 | } |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 192 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 193 | sqlite3_bind_blob(stmt, 1, device_name.wire(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 194 | sqlite3_bind_int64(stmt, 2, seq_no); |
| 195 | sqlite3_bind_int(stmt, 3, 0); |
| 196 | sqlite3_bind_text(stmt, 4, filename.c_str(), filename.size(), SQLITE_STATIC); |
| 197 | sqlite3_bind_int64(stmt, 5, version); |
| 198 | sqlite3_bind_int64(stmt, 6, action_time); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 199 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 200 | sqlite3_bind_blob(stmt, 7, hash.buf(), hash.size(), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 201 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 202 | // sqlite3_bind_int64(stmt, 8, atime); // NULL |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 203 | sqlite3_bind_int64(stmt, 9, wtime); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 204 | // sqlite3_bind_int64(stmt, 10, ctime); // NULL |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 205 | sqlite3_bind_int(stmt, 11, mode); |
| 206 | sqlite3_bind_int(stmt, 12, seg_num); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 208 | if (parent_device_name && parent_seq_no > 0) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 209 | sqlite3_bind_blob(stmt, 13, parent_device_name->buf(), parent_device_name->size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 210 | sqlite3_bind_int64(stmt, 14, parent_seq_no); |
| 211 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 212 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 213 | ActionItemPtr item = make_shared<ActionItem>(); |
| 214 | item->set_action(ActionItem::UPDATE); |
| 215 | item->set_filename(filename); |
| 216 | item->set_version(version); |
| 217 | item->set_timestamp(action_time); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 218 | item->set_file_hash(hash.buf(), hash.size()); |
| 219 | // item->set_atime(atime); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 220 | item->set_mtime(wtime); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 221 | // item->set_ctime(ctime); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 222 | item->set_mode(mode); |
| 223 | item->set_seg_num(seg_num); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 224 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 225 | if (parent_device_name && parent_seq_no > 0) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 226 | // cout << Name(*parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 227 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 228 | item->set_parent_device_name(parent_device_name->buf(), parent_device_name->size()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | item->set_parent_seq_no(parent_seq_no); |
| 230 | } |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 231 | |
| 232 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 233 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 234 | std::string item_msg; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 235 | item->SerializeToString(&item_msg); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 236 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 237 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 238 | |
| 239 | Name actionName = Name("/"); |
| 240 | actionName.append(m_syncLog->GetLocalName()).append(m_appName).append("action"); |
| 241 | actionName.append(m_sharedFolderName).appendNumber(seq_no); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 242 | _LOG_DEBUG("ActionName: " << actionName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 243 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 244 | shared_ptr<Data> actionData = make_shared<Data>(); |
| 245 | actionData->setName(actionName); |
| 246 | actionData->setFreshnessPeriod(time::seconds(60)); |
| 247 | actionData->setContent(reinterpret_cast<const uint8_t*>(item_msg.c_str()), item_msg.size()); |
| 248 | m_keyChain.sign(*actionData); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 249 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 250 | // _LOG_DEBUG(" >>>>>>> " << Name(namePtr->buf() << " " << namePtr->length()); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 251 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 252 | sqlite3_bind_blob(stmt, 15, actionName.wireEncode().wire(), actionName.wireEncode().size(), |
| 253 | SQLITE_STATIC); |
| 254 | sqlite3_bind_blob(stmt, 16, actionData->wireEncode().wire(), actionData->wireEncode().size(), |
| 255 | SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 256 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 257 | sqlite3_step(stmt); |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 258 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 259 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 260 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 261 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 262 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 263 | // I had a problem including directory_name assignment as part of the initial insert. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 264 | sqlite3_prepare_v2(m_db, |
| 265 | "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", |
| 266 | -1, &stmt, 0); |
| 267 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 268 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 269 | sqlite3_bind_blob(stmt, 1, device_name.wire(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 270 | sqlite3_bind_int64(stmt, 2, seq_no); |
| 271 | sqlite3_step(stmt); |
| 272 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 273 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 274 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 275 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 276 | sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 277 | |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 278 | // set complete for local file |
| 279 | m_fileState->SetFileComplete(filename); |
| 280 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 281 | return item; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 284 | // void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 285 | // ActionLog::AddActionMove(const std::string &oldFile, const std::string &newFile) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 286 | // { |
| 287 | // // not supported yet |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 288 | // BOOST_THROW_EXCEPTION(Error("Move operation is not yet supported")); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 289 | // } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 290 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 291 | ActionItemPtr |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 292 | ActionLog::AddLocalActionDelete(const std::string& filename) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 293 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 294 | _LOG_DEBUG("Adding local action DELETE"); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 295 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 296 | sqlite3_exec(m_db, "BEGIN TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 297 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 298 | const Block device_name = m_syncLog->GetLocalName().wireEncode(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 299 | sqlite3_int64 version; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 300 | BufferPtr parent_device_name; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 301 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 302 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 303 | sqlite3_int64 action_time = std::time(0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 304 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 305 | tie(version, parent_device_name, parent_seq_no) = GetLatestActionForFile(filename); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 306 | if (!parent_device_name) // no records exist or file was already deleted |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 307 | { |
| 308 | _LOG_DEBUG("Nothing to delete... [" << filename << "]"); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 309 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 310 | // just in case, remove data from FileState |
| 311 | sqlite3_stmt* stmt; |
| 312 | sqlite3_prepare_v2(m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0); |
| 313 | sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); // file |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 314 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 315 | sqlite3_step(stmt); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 316 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 317 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 318 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 319 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 4a8781c | 2013-01-29 17:59:44 -0800 | [diff] [blame] | 320 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 321 | sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0); |
| 322 | return ActionItemPtr(); |
| 323 | } |
| 324 | version++; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 325 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 326 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo(); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 327 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 328 | sqlite3_stmt* stmt; |
| 329 | sqlite3_prepare_v2(m_db, "INSERT INTO ActionLog " |
| 330 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 331 | "parent_device_name, parent_seq_no, " |
| 332 | "action_name, action_content_object) " |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 333 | "VALUES(?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 334 | " ?, ?," |
| 335 | " ?, ?)", |
| 336 | -1, &stmt, 0); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 337 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 338 | sqlite3_bind_blob(stmt, 1, device_name.wire(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 339 | sqlite3_bind_int64(stmt, 2, seq_no); |
| 340 | sqlite3_bind_int(stmt, 3, 1); |
| 341 | sqlite3_bind_text(stmt, 4, filename.c_str(), filename.size(), SQLITE_STATIC); // file |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 342 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 343 | sqlite3_bind_int64(stmt, 5, version); |
| 344 | sqlite3_bind_int64(stmt, 6, action_time); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 345 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 346 | sqlite3_bind_blob(stmt, 7, parent_device_name->buf(), parent_device_name->size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 347 | sqlite3_bind_int64(stmt, 8, parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 348 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 349 | ActionItemPtr item = make_shared<ActionItem>(); |
| 350 | item->set_action(ActionItem::DELETE); |
| 351 | item->set_filename(filename); |
| 352 | item->set_version(version); |
| 353 | item->set_timestamp(action_time); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 354 | item->set_parent_device_name(parent_device_name->buf(), parent_device_name->size()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 355 | item->set_parent_seq_no(parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 356 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 357 | std::string item_msg; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 358 | item->SerializeToString(&item_msg); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 359 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 360 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 361 | Name actionName = Name("/"); |
| 362 | actionName.append(m_syncLog->GetLocalName()).append(m_appName).append("action"); |
| 363 | actionName.append(m_sharedFolderName).appendNumber(seq_no); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 364 | _LOG_DEBUG("ActionName: " << actionName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 365 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 366 | shared_ptr<Data> actionData = make_shared<Data>(); |
| 367 | actionData->setName(actionName); |
| 368 | actionData->setFreshnessPeriod(time::seconds(60)); |
| 369 | actionData->setContent(reinterpret_cast<const uint8_t*>(item_msg.c_str()), item_msg.size()); |
| 370 | m_keyChain.sign(*actionData); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 371 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 372 | sqlite3_bind_blob(stmt, 9, actionName.wireEncode().wire(), actionName.wireEncode().size(), |
| 373 | SQLITE_STATIC); |
| 374 | sqlite3_bind_blob(stmt, 10, actionData->wireEncode().wire(), actionData->wireEncode().size(), |
| 375 | SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 376 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 377 | sqlite3_step(stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 378 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 379 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 380 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 381 | // cout << Name(parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 382 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 383 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 384 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 385 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 386 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 387 | // I had a problem including directory_name assignment as part of the initial insert. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 388 | sqlite3_prepare_v2(m_db, |
| 389 | "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", |
| 390 | -1, &stmt, 0); |
| 391 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 392 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 393 | sqlite3_bind_blob(stmt, 1, device_name.wire(), device_name.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 394 | sqlite3_bind_int64(stmt, 2, seq_no); |
| 395 | sqlite3_step(stmt); |
| 396 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 397 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 398 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 399 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 400 | sqlite3_exec(m_db, "END TRANSACTION;", 0, 0, 0); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 401 | |
| 402 | return item; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 403 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 404 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 405 | shared_ptr<Data> |
| 406 | ActionLog::LookupActionData(const Name& deviceName, sqlite3_int64 seqno) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 407 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 408 | sqlite3_stmt* stmt; |
| 409 | sqlite3_prepare_v2(m_db, |
| 410 | "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", |
| 411 | -1, &stmt, 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 412 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 413 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 414 | SQLITE_STATIC); // ndn version |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 415 | sqlite3_bind_int64(stmt, 2, seqno); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 416 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 417 | shared_ptr<Data> retval; |
| 418 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 419 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 420 | // _LOG_DEBUG(sqlite3_column_blob(stmt, 0) << ", " << sqlite3_column_bytes(stmt, 0)); |
| 421 | retval = make_shared<Data>(); |
| 422 | retval->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 423 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 424 | } |
| 425 | else { |
| 426 | _LOG_TRACE("No action found for deviceName [" << deviceName << "] and seqno:" << seqno); |
| 427 | } |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 428 | // _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK && sqlite3_errcode(m_db) != SQLITE_ROW, |
| 429 | // sqlite3_errmsg(m_db)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 430 | sqlite3_finalize(stmt); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 431 | |
| 432 | return retval; |
| 433 | } |
| 434 | |
| 435 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 436 | ActionLog::LookupAction(const Name& deviceName, sqlite3_int64 seqno) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 437 | { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 438 | shared_ptr<Data> data = LookupActionData(deviceName, seqno); |
| 439 | if (!data) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 440 | return ActionItemPtr(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 441 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 442 | ActionItemPtr action = |
| 443 | deserializeMsg<ActionItem>(Buffer(data->getContent().value(), data->getContent().value_size())); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 444 | return action; |
| 445 | } |
| 446 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 447 | shared_ptr<Data> |
| 448 | ActionLog::LookupActionData(const Name& actionName) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 449 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 450 | sqlite3_stmt* stmt; |
| 451 | sqlite3_prepare_v2(m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, |
| 452 | &stmt, 0); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 453 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 454 | _LOG_DEBUG(actionName); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 455 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 456 | _LOG_DEBUG(" LookActionData <<<<<<< " << actionName << " " << actionName.wireEncode().size()); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 457 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 458 | sqlite3_bind_blob(stmt, 1, actionName.wireEncode().wire(), actionName.wireEncode().size(), |
| 459 | SQLITE_STATIC); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 460 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 461 | shared_ptr<Data> retval; // = make_shared<Data>(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 462 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 463 | // _LOG_DEBUG(sqlite3_column_blob(stmt, 0) << ", " << sqlite3_column_bytes(stmt, 0)); |
| 464 | retval = make_shared<Data>(); |
| 465 | retval->wireDecode(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 466 | sqlite3_column_bytes(stmt, 0))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 467 | } |
| 468 | else { |
| 469 | _LOG_TRACE("No action found for name: " << actionName); |
| 470 | } |
| 471 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_ROW, sqlite3_errmsg(m_db)); |
| 472 | sqlite3_finalize(stmt); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 473 | |
| 474 | return retval; |
| 475 | } |
| 476 | |
| 477 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 478 | ActionLog::LookupAction(const Name& actionName) |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 479 | { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 480 | shared_ptr<Data> data = LookupActionData(actionName); |
| 481 | if (!data) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 482 | return ActionItemPtr(); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 483 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 484 | ActionItemPtr action = |
| 485 | deserializeMsg<ActionItem>(Buffer(data->getContent().value(), data->getContent().value_size())); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 486 | |
| 487 | return action; |
| 488 | } |
| 489 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 490 | FileItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 491 | ActionLog::LookupAction(const std::string& filename, sqlite3_int64 version, const Buffer& filehash) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 492 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 493 | sqlite3_stmt* stmt; |
| 494 | sqlite3_prepare_v2(m_db, |
| 495 | "SELECT device_name, seq_no, strftime('%s', file_mtime), file_chmod, file_seg_num, file_hash " |
| 496 | " FROM ActionLog " |
| 497 | " WHERE action = 0 AND " |
| 498 | " filename=? AND " |
| 499 | " version=? AND " |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 500 | " is_prefix(?, file_hash)=1", |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 501 | -1, &stmt, 0); |
| 502 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 503 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 504 | sqlite3_bind_text(stmt, 1, filename.c_str(), filename.size(), SQLITE_STATIC); |
| 505 | sqlite3_bind_int64(stmt, 2, version); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 506 | sqlite3_bind_blob(stmt, 3, filehash.buf(), filehash.size(), SQLITE_STATIC); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 507 | |
| 508 | FileItemPtr fileItem; |
| 509 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 510 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 511 | fileItem = make_shared<FileItem>(); |
| 512 | fileItem->set_filename(filename); |
| 513 | fileItem->set_device_name(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
| 514 | fileItem->set_seq_no(sqlite3_column_int64(stmt, 1)); |
| 515 | fileItem->set_mtime(sqlite3_column_int64(stmt, 2)); |
| 516 | fileItem->set_mode(sqlite3_column_int64(stmt, 3)); |
| 517 | fileItem->set_seg_num(sqlite3_column_int64(stmt, 4)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 518 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 519 | fileItem->set_file_hash(sqlite3_column_blob(stmt, 5), sqlite3_column_bytes(stmt, 5)); |
| 520 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 521 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 522 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE || sqlite3_errcode(m_db) != SQLITE_ROW || |
| 523 | sqlite3_errcode(m_db) != SQLITE_OK, |
| 524 | sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 525 | |
| 526 | return fileItem; |
| 527 | } |
| 528 | |
| 529 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 530 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 531 | ActionLog::AddRemoteAction(const Name& deviceName, sqlite3_int64 seqno, shared_ptr<Data> actionData) |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 532 | { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 533 | if (!actionData) { |
| 534 | _LOG_ERROR("actionData is not valid"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 535 | return ActionItemPtr(); |
| 536 | } |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 537 | ActionItemPtr action = deserializeMsg<ActionItem>( |
| 538 | Buffer(actionData->getContent().value(), actionData->getContent().value_size())); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 539 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 540 | if (!action) { |
| 541 | _LOG_ERROR("action cannot be decoded"); |
| 542 | return ActionItemPtr(); |
| 543 | } |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 544 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 545 | _LOG_DEBUG("AddRemoteAction: [" << deviceName.toUri() << "] seqno: " << seqno); |
Alexander Afanasyev | f695aed | 2013-01-30 13:28:42 -0800 | [diff] [blame] | 546 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 547 | sqlite3_stmt* stmt; |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 548 | sqlite3_prepare_v2(m_db, |
| 549 | "INSERT INTO ActionLog " |
| 550 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 551 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
| 552 | "parent_device_name, parent_seq_no, " |
| 553 | "action_name, action_content_object) " |
| 554 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 555 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
| 556 | " ?, ?, " |
| 557 | " ?, ?);", |
| 558 | -1, &stmt, 0); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 559 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 560 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 561 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 562 | SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 563 | sqlite3_bind_int64(stmt, 2, seqno); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 564 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 565 | sqlite3_bind_int(stmt, 3, action->action()); |
| 566 | sqlite3_bind_text(stmt, 4, action->filename().c_str(), action->filename().size(), SQLITE_STATIC); |
| 567 | sqlite3_bind_int64(stmt, 5, action->version()); |
| 568 | sqlite3_bind_int64(stmt, 6, action->timestamp()); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 569 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 570 | if (action->action() == ActionItem::UPDATE) { |
| 571 | sqlite3_bind_blob(stmt, 7, action->file_hash().c_str(), action->file_hash().size(), |
| 572 | SQLITE_STATIC); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 573 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 574 | // sqlite3_bind_int64(stmt, 8, atime); // NULL |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 575 | sqlite3_bind_int64(stmt, 9, action->mtime()); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 576 | // sqlite3_bind_int64(stmt, 10, ctime); // NULL |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 577 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 578 | sqlite3_bind_int(stmt, 11, action->mode()); |
| 579 | sqlite3_bind_int(stmt, 12, action->seg_num()); |
| 580 | } |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 581 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 582 | if (action->has_parent_device_name()) { |
| 583 | sqlite3_bind_blob(stmt, 13, action->parent_device_name().c_str(), |
| 584 | action->parent_device_name().size(), SQLITE_STATIC); |
| 585 | sqlite3_bind_int64(stmt, 14, action->parent_seq_no()); |
| 586 | } |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 587 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 588 | Name actionName = Name(deviceName); |
| 589 | actionName.append("action").append(m_sharedFolderName).appendNumber(seqno); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 590 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 591 | sqlite3_bind_blob(stmt, 15, actionName.wireEncode().wire(), actionName.wireEncode().size(), |
| 592 | SQLITE_STATIC); |
| 593 | sqlite3_bind_blob(stmt, 16, actionData->wireEncode().wire(), actionData->wireEncode().size(), |
| 594 | SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 595 | sqlite3_step(stmt); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 596 | |
| 597 | // if action needs to be applied to file state, the trigger will take care of it |
| 598 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 599 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 600 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 601 | sqlite3_finalize(stmt); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 602 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 603 | // I had a problem including directory_name assignment as part of the initial insert. |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 604 | sqlite3_prepare_v2(m_db, |
| 605 | "UPDATE ActionLog SET directory=directory_name(filename) WHERE device_name=? AND seq_no=?", |
| 606 | -1, &stmt, 0); |
| 607 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 608 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 609 | sqlite3_bind_blob(stmt, 1, deviceName.wireEncode().wire(), deviceName.wireEncode().size(), |
| 610 | SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 611 | sqlite3_bind_int64(stmt, 2, seqno); |
| 612 | sqlite3_step(stmt); |
| 613 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 614 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 615 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 616 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 617 | return action; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 618 | } |
| 619 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 620 | ActionItemPtr |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 621 | ActionLog::AddRemoteAction(shared_ptr<Data> actionData) |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 622 | { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 623 | Name name = actionData->getName(); |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 624 | // action name: /<device_name>/<appname>/action/<shared-folder>/<action-seq> |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 625 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 626 | uint64_t seqno = name.get(-1).toNumber(); |
| 627 | std::string sharedFolder = name.get(-2).toUri(); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 628 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 629 | if (sharedFolder != m_sharedFolderName) { |
| 630 | _LOG_ERROR("Action doesn't belong to this shared folder"); |
| 631 | return ActionItemPtr(); |
| 632 | } |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 633 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 634 | std::string action = name.get(-3).toUri(); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 635 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 636 | if (action != "action") { |
| 637 | _LOG_ERROR("not an action"); |
| 638 | return ActionItemPtr(); |
| 639 | } |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 640 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 641 | std::string appName = name.get(-4).toUri(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 642 | if (appName != m_appName) { |
| 643 | _LOG_ERROR("Action doesn't belong to this application"); |
| 644 | return ActionItemPtr(); |
| 645 | } |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 646 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 647 | Name deviceName = name.getSubName(0, name.size() - 4); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 648 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 649 | _LOG_DEBUG("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " |
| 650 | << sharedFolder |
| 651 | << ", seqno: " |
| 652 | << seqno); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 653 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 654 | return AddRemoteAction(deviceName, seqno, actionData); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 657 | sqlite3_int64 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 658 | ActionLog::LogSize() |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 659 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 660 | sqlite3_stmt* stmt; |
| 661 | sqlite3_prepare_v2(m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 662 | |
| 663 | sqlite3_int64 retval = -1; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 664 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
| 665 | retval = sqlite3_column_int64(stmt, 0); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | return retval; |
| 669 | } |
| 670 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 671 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 672 | ActionLog::LookupActionsInFolderRecursively( |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 673 | const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 674 | const std::string& folder, int offset /*=0*/, int limit /*=-1*/) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 675 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 676 | _LOG_DEBUG("LookupActionsInFolderRecursively: [" << folder << "]"); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 677 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 678 | if (limit >= 0) |
| 679 | limit += 1; // to check if there is more data |
| 680 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 681 | sqlite3_stmt* stmt; |
| 682 | if (folder != "") { |
| 683 | /// @todo Do something to improve efficiency of this query. Right now it is basically scanning the whole database |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 684 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 685 | sqlite3_prepare_v2(m_db, |
| 686 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 687 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
| 688 | " parent_device_name,parent_seq_no " |
| 689 | " FROM ActionLog " |
| 690 | " WHERE is_dir_prefix (?, directory)=1 " |
| 691 | " ORDER BY action_timestamp DESC " |
| 692 | " LIMIT ? OFFSET ?", |
| 693 | -1, &stmt, |
| 694 | 0); // there is a small ambiguity with is_prefix matching, but should be ok for now |
| 695 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 696 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 697 | sqlite3_bind_text(stmt, 1, folder.c_str(), folder.size(), SQLITE_STATIC); |
| 698 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 699 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 700 | sqlite3_bind_int(stmt, 2, limit); |
| 701 | sqlite3_bind_int(stmt, 3, offset); |
| 702 | } |
| 703 | else { |
| 704 | sqlite3_prepare_v2(m_db, |
| 705 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 706 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
| 707 | " parent_device_name,parent_seq_no " |
| 708 | " FROM ActionLog " |
| 709 | " ORDER BY action_timestamp DESC " |
| 710 | " LIMIT ? OFFSET ?", |
| 711 | -1, &stmt, 0); |
| 712 | sqlite3_bind_int(stmt, 1, limit); |
| 713 | sqlite3_bind_int(stmt, 2, offset); |
| 714 | } |
| 715 | |
| 716 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
| 717 | |
| 718 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 719 | if (limit == 1) |
| 720 | break; |
| 721 | |
| 722 | ActionItem action; |
| 723 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 724 | Name device_name(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 725 | sqlite3_column_bytes(stmt, 0))); |
| 726 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 727 | sqlite3_int64 seq_no = sqlite3_column_int64(stmt, 1); |
| 728 | action.set_action(static_cast<ActionItem_ActionType>(sqlite3_column_int(stmt, 2))); |
| 729 | action.set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)), |
| 730 | sqlite3_column_bytes(stmt, 3)); |
| 731 | std::string directory(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)), |
| 732 | sqlite3_column_bytes(stmt, 4)); |
| 733 | action.set_version(sqlite3_column_int64(stmt, 5)); |
| 734 | action.set_timestamp(sqlite3_column_int64(stmt, 6)); |
| 735 | |
| 736 | if (action.action() == 0) { |
| 737 | action.set_file_hash(sqlite3_column_blob(stmt, 7), sqlite3_column_bytes(stmt, 7)); |
| 738 | action.set_mtime(sqlite3_column_int(stmt, 8)); |
| 739 | action.set_mode(sqlite3_column_int(stmt, 9)); |
| 740 | action.set_seg_num(sqlite3_column_int64(stmt, 10)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 741 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 742 | if (sqlite3_column_bytes(stmt, 11) > 0) { |
| 743 | action.set_parent_device_name(sqlite3_column_blob(stmt, 11), sqlite3_column_bytes(stmt, 11)); |
| 744 | action.set_parent_seq_no(sqlite3_column_int64(stmt, 12)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 745 | } |
| 746 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 747 | visitor(device_name, seq_no, action); |
| 748 | limit--; |
| 749 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 750 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 751 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 752 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 753 | sqlite3_finalize(stmt); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 754 | |
| 755 | return (limit == 1); // more data is available |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 756 | } |
| 757 | |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 758 | /** |
| 759 | * @todo Figure out the way to minimize code duplication |
| 760 | */ |
| 761 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 762 | ActionLog::LookupActionsForFile( |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 763 | const function<void(const Name& name, sqlite3_int64 seq_no, const ActionItem&)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 764 | const std::string& file, int offset /*=0*/, int limit /*=-1*/) |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 765 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 766 | _LOG_DEBUG("LookupActionsInFolderRecursively: [" << file << "]"); |
| 767 | if (file.empty()) |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 768 | return false; |
| 769 | |
| 770 | if (limit >= 0) |
| 771 | limit += 1; // to check if there is more data |
| 772 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 773 | sqlite3_stmt* stmt; |
| 774 | sqlite3_prepare_v2(m_db, |
| 775 | "SELECT device_name,seq_no,action,filename,directory,version,strftime('%s', action_timestamp), " |
| 776 | " file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num, " |
| 777 | " parent_device_name,parent_seq_no " |
| 778 | " FROM ActionLog " |
| 779 | " WHERE filename=? " |
| 780 | " ORDER BY action_timestamp DESC " |
| 781 | " LIMIT ? OFFSET ?", |
| 782 | -1, &stmt, |
| 783 | 0); // there is a small ambiguity with is_prefix matching, but should be ok for now |
| 784 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 785 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 786 | sqlite3_bind_text(stmt, 1, file.c_str(), file.size(), SQLITE_STATIC); |
| 787 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 788 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 789 | sqlite3_bind_int(stmt, 2, limit); |
| 790 | sqlite3_bind_int(stmt, 3, offset); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 791 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 792 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 793 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 794 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 795 | if (limit == 1) |
| 796 | break; |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 797 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 798 | ActionItem action; |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 799 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 800 | Name device_name(Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 801 | sqlite3_column_bytes(stmt, 0))); |
| 802 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 803 | sqlite3_int64 seq_no = sqlite3_column_int64(stmt, 1); |
| 804 | action.set_action(static_cast<ActionItem_ActionType>(sqlite3_column_int(stmt, 2))); |
| 805 | action.set_filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 3)), |
| 806 | sqlite3_column_bytes(stmt, 3)); |
| 807 | std::string directory(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 4)), |
| 808 | sqlite3_column_bytes(stmt, 4)); |
| 809 | action.set_version(sqlite3_column_int64(stmt, 5)); |
| 810 | action.set_timestamp(sqlite3_column_int64(stmt, 6)); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 811 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 812 | if (action.action() == 0) { |
| 813 | action.set_file_hash(sqlite3_column_blob(stmt, 7), sqlite3_column_bytes(stmt, 7)); |
| 814 | action.set_mtime(sqlite3_column_int(stmt, 8)); |
| 815 | action.set_mode(sqlite3_column_int(stmt, 9)); |
| 816 | action.set_seg_num(sqlite3_column_int64(stmt, 10)); |
| 817 | } |
| 818 | if (sqlite3_column_bytes(stmt, 11) > 0) { |
| 819 | action.set_parent_device_name(sqlite3_column_blob(stmt, 11), sqlite3_column_bytes(stmt, 11)); |
| 820 | action.set_parent_seq_no(sqlite3_column_int64(stmt, 12)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 821 | } |
| 822 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 823 | visitor(device_name, seq_no, action); |
| 824 | limit--; |
| 825 | } |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 826 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 827 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
| 828 | |
| 829 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 830 | |
| 831 | return (limit == 1); // more data is available |
| 832 | } |
| 833 | |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 834 | void |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 835 | ActionLog::LookupRecentFileActions(const function<void(const std::string&, int, int)>& visitor, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 836 | int limit) |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 837 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 838 | sqlite3_stmt* stmt; |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 839 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 840 | sqlite3_prepare_v2(m_db, |
| 841 | "SELECT AL.filename, AL.action" |
| 842 | " FROM ActionLog AL" |
| 843 | " JOIN " |
| 844 | " (SELECT filename, MAX(action_timestamp) AS action_timestamp " |
| 845 | " FROM ActionLog " |
| 846 | " GROUP BY filename ) AS GAL" |
| 847 | " ON AL.filename = GAL.filename AND AL.action_timestamp = GAL.action_timestamp " |
| 848 | " ORDER BY AL.action_timestamp DESC " |
| 849 | " LIMIT ?;", |
| 850 | -1, &stmt, 0); |
| 851 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 852 | sqlite3_bind_int(stmt, 1, limit); |
| 853 | int index = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 854 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 855 | std::string filename(reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0)), |
| 856 | sqlite3_column_bytes(stmt, 0)); |
| 857 | int action = sqlite3_column_int(stmt, 1); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 858 | visitor(filename, action, index); |
| 859 | index++; |
| 860 | } |
| 861 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 862 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_DONE, sqlite3_errmsg(m_db)); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 863 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 864 | sqlite3_finalize(stmt); |
Zhenkai Zhu | 25e1358 | 2013-02-27 15:33:01 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 867 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 868 | /////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 869 | /////////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 870 | /////////////////////////////////////////////////////////////////////////////////// |
| 871 | |
| 872 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 873 | ActionLog::apply_action_xFun(sqlite3_context* context, int argc, sqlite3_value** argv) |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 874 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 875 | ActionLog* the = reinterpret_cast<ActionLog*>(sqlite3_user_data(context)); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 876 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 877 | if (argc != 11) { |
| 878 | sqlite3_result_error(context, "``apply_action'' expects 10 arguments", -1); |
| 879 | return; |
| 880 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 881 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 882 | Buffer device_name(sqlite3_value_blob(argv[0]), sqlite3_value_bytes(argv[0])); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 883 | sqlite3_int64 seq_no = sqlite3_value_int64(argv[1]); |
| 884 | int action = sqlite3_value_int(argv[2]); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 885 | std::string filename = reinterpret_cast<const char*>(sqlite3_value_text(argv[3])); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 886 | sqlite3_int64 version = sqlite3_value_int64(argv[4]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 887 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 888 | _LOG_TRACE("apply_function called with " << argc); |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 889 | _LOG_TRACE("device_name: " << Name(Block(reinterpret_cast<const char*>(device_name.buf()), |
| 890 | device_name.size())) |
| 891 | << ", action: " |
| 892 | << action |
| 893 | << ", file: " |
| 894 | << filename); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 895 | |
| 896 | if (action == 0) // update |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 897 | { |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 898 | Buffer hash(sqlite3_value_blob(argv[5]), sqlite3_value_bytes(argv[5])); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 899 | time_t atime = static_cast<time_t>(sqlite3_value_int64(argv[6])); |
| 900 | time_t mtime = static_cast<time_t>(sqlite3_value_int64(argv[7])); |
| 901 | time_t ctime = static_cast<time_t>(sqlite3_value_int64(argv[8])); |
| 902 | int mode = sqlite3_value_int(argv[9]); |
| 903 | int seg_num = sqlite3_value_int(argv[10]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 904 | |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 905 | _LOG_DEBUG("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " |
| 906 | << toHex(hash)); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 907 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 908 | the->m_fileState->UpdateFile(filename, version, hash, device_name, seq_no, atime, mtime, ctime, |
| 909 | mode, seg_num); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 910 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 911 | // no callback here |
| 912 | } |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 913 | else if (action == 1) // delete |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 914 | { |
| 915 | the->m_fileState->DeleteFile(filename); |
Alexander Afanasyev | 0c545ed | 2013-01-26 00:12:32 -0800 | [diff] [blame] | 916 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 917 | the->m_onFileRemoved(filename); |
| 918 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 919 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 920 | sqlite3_result_null(context); |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 921 | } |
Lijing Wang | 51837ad | 2016-12-25 14:43:53 -0800 | [diff] [blame^] | 922 | |
| 923 | } // namespace chronoshare |
| 924 | } // namespace ndn |