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