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 = "\ |
| 34 | CREATE TABLE ActionLog ( \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 35 | device_name BLOB NOT NULL, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 36 | seq_no INTEGER NOT NULL, \n\ |
| 37 | \n\ |
| 38 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 39 | filename TEXT NOT NULL, \n\ |
| 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 | 0995f32 | 2013-01-22 13:16:46 -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\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 59 | FOREIGN KEY (parent_device_name, parent_seq_no) \n\ |
| 60 | REFERENCES ActionLog (device_name, seq_no) \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 61 | ON UPDATE RESTRICT \n\ |
| 62 | ON DELETE SET NULL \n\ |
| 63 | ); \n\ |
| 64 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 65 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 66 | 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] | 67 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
| 68 | \n\ |
| 69 | CREATE TRIGGER ActionLogInsert_trigger \n\ |
| 70 | AFTER INSERT ON ActionLog \n\ |
| 71 | FOR EACH ROW \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 72 | WHEN (SELECT device_name \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 73 | FROM ActionLog \n\ |
| 74 | WHERE filename=NEW.filename AND \n\ |
| 75 | version > NEW.version) IS NULL AND \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 76 | (SELECT device_name \n\ |
| 77 | FROM ActionLog \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 78 | WHERE filename=NEW.filename AND \n\ |
| 79 | version = NEW.version AND \n\ |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 80 | device_name > NEW.device_name) IS NULL \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 81 | BEGIN \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 82 | SELECT apply_action (NEW.device_name, NEW.seq_no, \ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 83 | NEW.action,NEW.filename,NEW.file_hash, \ |
| 84 | strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \ |
| 85 | NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \ |
| 86 | END; \n\ |
| 87 | \n\ |
| 88 | CREATE TABLE FileState ( \n\ |
| 89 | type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\ |
| 90 | filename TEXT NOT NULL, \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 91 | device_name BLOB NOT NULL, \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 92 | seq_no INTEGER NOT NULL, \n\ |
| 93 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 94 | file_atime TIMESTAMP, \n\ |
| 95 | file_mtime TIMESTAMP, \n\ |
| 96 | file_ctime TIMESTAMP, \n\ |
| 97 | file_chmod INTEGER, \n\ |
| 98 | file_seg_num INTEGER, \n\ |
| 99 | \n\ |
| 100 | PRIMARY KEY (type, filename) \n\ |
| 101 | ); \n\ |
| 102 | \n\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 103 | CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\ |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 104 | "; |
| 105 | |
| 106 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 107 | ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path, |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 108 | SyncLogPtr syncLog, |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 109 | const std::string &sharedFolder) |
| 110 | : DbHelper (path / ".chronoshare", "action-log.db") |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 111 | , m_syncLog (syncLog) |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 112 | , m_ccnx (ccnx) |
| 113 | , m_sharedFolderName (sharedFolder) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 114 | { |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 115 | sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL); |
| 116 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 117 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 118 | int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this), |
| 119 | ActionLog::apply_action_xFun, |
| 120 | 0, 0); |
| 121 | if (res != SQLITE_OK) |
| 122 | { |
| 123 | BOOST_THROW_EXCEPTION (Error::Db () |
| 124 | << errmsg_info_str ("Cannot create function ``apply_action''")); |
| 125 | } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 128 | tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/> |
| 129 | ActionLog::GetLatestActionForFile (const std::string &filename) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 130 | { |
| 131 | // check if something already exists |
| 132 | sqlite3_stmt *stmt; |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 133 | int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action " |
| 134 | "FROM ActionLog " |
| 135 | "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 136 | |
| 137 | if (res != SQLITE_OK) |
| 138 | { |
| 139 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 140 | << errmsg_info_str ("Some error with GetExistingRecord")); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 143 | sqlite3_int64 version = -1; |
| 144 | CcnxCharbufPtr parent_device_name; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 145 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 146 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 147 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 148 | if (sqlite3_step (stmt) == SQLITE_ROW) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 149 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 150 | version = sqlite3_column_int64 (stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 152 | if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted |
| 153 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 154 | 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] | 155 | parent_seq_no = sqlite3_column_int64 (stmt, 2); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 156 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 157 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 159 | sqlite3_finalize (stmt); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 160 | return make_tuple (version, parent_device_name, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 163 | // local add action. remote action is extracted from content object |
| 164 | void |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 165 | ActionLog::AddLocalActionUpdate (const std::string &filename, |
| 166 | const Hash &hash, |
| 167 | time_t wtime, |
| 168 | int mode, |
| 169 | int seg_num) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 170 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 171 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 172 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 173 | CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf (); |
| 174 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
| 175 | sqlite3_int64 version; |
| 176 | CcnxCharbufPtr parent_device_name; |
| 177 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 179 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 180 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 181 | tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename); |
| 182 | version ++; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 184 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 185 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 186 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 187 | "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] | 188 | "parent_device_name, parent_seq_no, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 189 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 190 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 191 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 192 | " ?, ?, " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 193 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 195 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
| 196 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 197 | if (res != SQLITE_OK) |
| 198 | { |
| 199 | BOOST_THROW_EXCEPTION (Error::Db () |
| 200 | << errmsg_info_str (sqlite3_errmsg (m_db)) |
| 201 | ); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 204 | 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] | 205 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 206 | sqlite3_bind_int (stmt, 3, 0); |
| 207 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 208 | sqlite3_bind_int64 (stmt, 5, version); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 209 | sqlite3_bind_int64 (stmt, 6, action_time); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 210 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 211 | sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 212 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 213 | // sqlite3_bind_int64 (stmt, 8, atime); // NULL |
| 214 | sqlite3_bind_int64 (stmt, 9, wtime); |
| 215 | // sqlite3_bind_int64 (stmt, 10, ctime); // NULL |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 216 | sqlite3_bind_int (stmt, 11, mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 217 | sqlite3_bind_int (stmt, 12, seg_num); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 218 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 219 | if (parent_device_name && parent_seq_no > 0) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 220 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 221 | 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] | 222 | sqlite3_bind_int64 (stmt, 14, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 223 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 224 | |
| 225 | ActionItem item; |
| 226 | item.set_action (ActionItem::UPDATE); |
| 227 | item.set_filename (filename); |
| 228 | item.set_version (version); |
| 229 | item.set_timestamp (action_time); |
| 230 | item.set_file_hash (hash.GetHash (), hash.GetHashBytes ()); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 231 | // item.set_atime (atime); |
| 232 | item.set_mtime (wtime); |
| 233 | // item.set_ctime (ctime); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 234 | item.set_mode (mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 235 | item.set_seg_num (seg_num); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 236 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 237 | if (parent_device_name && parent_seq_no > 0) |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 238 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 239 | // cout << Name (*parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 240 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 241 | item.set_parent_device_name (parent_device_name->buf (), parent_device_name->length ()); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 242 | item.set_parent_seq_no (parent_seq_no); |
| 243 | } |
| 244 | |
| 245 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 246 | |
| 247 | string item_msg; |
| 248 | item.SerializeToString (&item_msg); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 249 | Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 250 | _LOG_DEBUG ("ActionName: " << actionName); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 251 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 252 | Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 253 | CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 254 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 255 | _LOG_DEBUG (" >>>>>>> " << namePtr->buf () << " " << namePtr->length ()); |
| 256 | |
| 257 | sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT); |
| 258 | sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 260 | sqlite3_step (stmt); |
| 261 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 262 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 263 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 264 | sqlite3_finalize (stmt); |
| 265 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 266 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 269 | // void |
| 270 | // ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile) |
| 271 | // { |
| 272 | // // not supported yet |
| 273 | // BOOST_THROW_EXCEPTION (Error::Db () |
| 274 | // << errmsg_info_str ("Move operation is not yet supported")); |
| 275 | // } |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 276 | |
| 277 | void |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 278 | ActionLog::AddLocalActionDelete (const std::string &filename) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 279 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 280 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 281 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 282 | CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf (); |
| 283 | sqlite3_int64 version; |
| 284 | CcnxCharbufPtr parent_device_name; |
| 285 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 286 | |
| 287 | sqlite3_int64 action_time = time (0); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 288 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 289 | tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 290 | if (!parent_device_name) // no records exist or file was already deleted |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 291 | { |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 292 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 293 | return; |
| 294 | } |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 295 | version ++; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 296 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 297 | sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo (); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 298 | |
| 299 | sqlite3_stmt *stmt; |
| 300 | sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 301 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 302 | "parent_device_name, parent_seq_no, " |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 303 | "action_name, action_content_object) " |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 304 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 305 | " ?, ?," |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 306 | " ?, ?)", -1, &stmt, 0); |
| 307 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 308 | 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] | 309 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 310 | sqlite3_bind_int (stmt, 3, 1); |
| 311 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
| 312 | sqlite3_bind_int64 (stmt, 5, version); |
| 313 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 314 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 315 | 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] | 316 | sqlite3_bind_int64 (stmt, 8, parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 317 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 318 | |
| 319 | ActionItem item; |
| 320 | item.set_action (ActionItem::UPDATE); |
| 321 | item.set_filename (filename); |
| 322 | item.set_version (version); |
| 323 | item.set_timestamp (action_time); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 324 | item.set_parent_device_name (parent_device_name->buf (), parent_device_name->length ()); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 325 | item.set_parent_seq_no (parent_seq_no); |
| 326 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 327 | string item_msg; |
| 328 | item.SerializeToString (&item_msg); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 329 | Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 330 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 331 | Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ()); |
| 332 | CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 333 | |
Alexander Afanasyev | ebf7518 | 2013-01-07 17:00:24 -0800 | [diff] [blame] | 334 | sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT); |
| 335 | sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 336 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 337 | sqlite3_step (stmt); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 339 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 340 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 341 | // cout << Ccnx::Name (parent_device_name) << endl; |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 342 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 343 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 344 | |
| 345 | sqlite3_finalize (stmt); |
| 346 | |
| 347 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 348 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 349 | |
| 350 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 351 | PcoPtr |
| 352 | ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno) |
| 353 | { |
| 354 | sqlite3_stmt *stmt; |
| 355 | sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0); |
| 356 | |
| 357 | CcnxCharbufPtr name = deviceName.toCcnxCharbuf (); |
| 358 | |
| 359 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
| 360 | sqlite3_bind_int64 (stmt, 2, seqno); |
| 361 | |
| 362 | PcoPtr retval; |
| 363 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 364 | { |
| 365 | // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0)); |
| 366 | retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno); |
| 371 | } |
| 372 | // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 373 | sqlite3_finalize (stmt); |
| 374 | |
| 375 | return retval; |
| 376 | } |
| 377 | |
| 378 | ActionItemPtr |
| 379 | ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno) |
| 380 | { |
| 381 | PcoPtr pco = LookupActionPco (deviceName, seqno); |
| 382 | if (!pco) return ActionItemPtr (); |
| 383 | |
| 384 | ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ()); |
| 385 | |
| 386 | return action; |
| 387 | } |
| 388 | |
| 389 | Ccnx::PcoPtr |
| 390 | ActionLog::LookupActionPco (const Ccnx::Name &actionName) |
| 391 | { |
| 392 | sqlite3_stmt *stmt; |
| 393 | sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0); |
| 394 | |
| 395 | _LOG_DEBUG (actionName); |
| 396 | CcnxCharbufPtr name = actionName.toCcnxCharbuf (); |
| 397 | |
| 398 | _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ()); |
| 399 | |
| 400 | sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC); |
| 401 | |
| 402 | PcoPtr retval; |
| 403 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 404 | { |
| 405 | // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0)); |
| 406 | retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0)); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | _LOG_TRACE ("No action found for name: " << actionName); |
| 411 | } |
| 412 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 413 | sqlite3_finalize (stmt); |
| 414 | |
| 415 | return retval; |
| 416 | } |
| 417 | |
| 418 | ActionItemPtr |
| 419 | ActionLog::LookupAction (const Ccnx::Name &actionName) |
| 420 | { |
| 421 | PcoPtr pco = LookupActionPco (actionName); |
| 422 | if (!pco) return ActionItemPtr (); |
| 423 | |
| 424 | ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ()); |
| 425 | |
| 426 | return action; |
| 427 | } |
| 428 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 429 | void |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 430 | ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco) |
| 431 | { |
| 432 | if (!actionPco) |
| 433 | { |
| 434 | BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid")); |
| 435 | } |
| 436 | ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ()); |
| 437 | |
| 438 | if (!action) |
| 439 | { |
| 440 | BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded")); |
| 441 | } |
| 442 | |
| 443 | sqlite3_stmt *stmt; |
| 444 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 445 | "(device_name, seq_no, action, filename, version, action_timestamp, " |
| 446 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, " |
| 447 | "parent_device_name, parent_seq_no, " |
| 448 | "action_name, action_content_object) " |
| 449 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 450 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, " |
| 451 | " ?, ?, " |
| 452 | " ?, ?);", -1, &stmt, 0); |
| 453 | |
| 454 | CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf (); |
| 455 | sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC); |
| 456 | sqlite3_bind_int64 (stmt, 2, seqno); |
| 457 | |
| 458 | sqlite3_bind_int (stmt, 3, action->action ()); |
| 459 | sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC); |
| 460 | sqlite3_bind_int64 (stmt, 5, action->version ()); |
| 461 | sqlite3_bind_int64 (stmt, 6, action->timestamp ()); |
| 462 | |
| 463 | if (action->action () == ActionItem::UPDATE) |
| 464 | { |
| 465 | sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC); |
| 466 | |
| 467 | // sqlite3_bind_int64 (stmt, 8, atime); // NULL |
| 468 | sqlite3_bind_int64 (stmt, 9, action->mtime ()); |
| 469 | // sqlite3_bind_int64 (stmt, 10, ctime); // NULL |
| 470 | |
| 471 | sqlite3_bind_int (stmt, 11, action->mode ()); |
| 472 | sqlite3_bind_int (stmt, 12, action->seg_num ()); |
| 473 | } |
| 474 | |
| 475 | if (action->has_parent_device_name ()) |
| 476 | { |
| 477 | sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT); |
| 478 | sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ()); |
| 479 | } |
| 480 | |
| 481 | Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno); |
| 482 | CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf (); |
| 483 | |
| 484 | sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC); |
| 485 | sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC); |
| 486 | sqlite3_step (stmt); |
| 487 | |
| 488 | // if action needs to be applied to file state, the trigger will take care of it |
| 489 | |
| 490 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db)); |
| 491 | |
| 492 | sqlite3_finalize (stmt); |
| 493 | } |
| 494 | |
| 495 | void |
| 496 | ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco) |
| 497 | { |
| 498 | Name name = actionPco->name (); |
| 499 | // <device_name>/"action"/<shared_folder_name_one_component>/<seqno> |
| 500 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame^] | 501 | uint64_t seqno = name.getCompFromBackAsInt (0); |
| 502 | string sharedFolder = name.getCompFromBackAsString (1); |
| 503 | |
| 504 | if (sharedFolder != m_sharedFolderName) |
| 505 | { |
| 506 | BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder")); |
| 507 | } |
| 508 | |
| 509 | string action = name.getCompFromBackAsString (2); |
| 510 | |
| 511 | if (action != "action") |
| 512 | { |
| 513 | BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action")); |
| 514 | } |
| 515 | Name deviceName = name.getPartialName (0, name.size ()-3); |
| 516 | |
| 517 | _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno); |
| 518 | |
| 519 | AddRemoteAction (deviceName, seqno, actionPco); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /////////////////////////////////////////////////////////////////////////////////// |
| 523 | // SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY" |
| 524 | /////////////////////////////////////////////////////////////////////////////////// |
| 525 | |
| 526 | void |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 527 | ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 528 | { |
| 529 | ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context)); |
| 530 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 531 | if (argc != 10) |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 532 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 533 | sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 534 | return; |
| 535 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 536 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 537 | CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0])); |
| 538 | sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]); |
| 539 | int action = sqlite3_value_int (argv[2]); |
| 540 | string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3])); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 541 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 542 | _LOG_TRACE ("apply_function called with " << argc); |
| 543 | _LOG_TRACE ("device_name: " << Name (device_name) |
| 544 | << ", action: " << action |
| 545 | << ", file: " << filename); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 546 | |
| 547 | if (action == 0) // update |
| 548 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 549 | Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4])); |
| 550 | time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5])); |
| 551 | time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6])); |
| 552 | time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7])); |
| 553 | int mode = sqlite3_value_int (argv[8]); |
| 554 | int seg_num = sqlite3_value_int (argv[9]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 555 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 556 | _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 557 | |
| 558 | sqlite3_stmt *stmt; |
| 559 | sqlite3_prepare_v2 (the->m_db, "UPDATE FileState " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 560 | "SET " |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 561 | "device_name=?, seq_no=?, " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 562 | "file_hash=?," |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 563 | "file_atime=datetime(?, 'unixepoch')," |
| 564 | "file_mtime=datetime(?, 'unixepoch')," |
| 565 | "file_ctime=datetime(?, 'unixepoch')," |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 566 | "file_chmod=?, " |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 567 | "file_seg_num=? " |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 568 | "WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 569 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 570 | 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] | 571 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 572 | sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 573 | sqlite3_bind_int64 (stmt, 4, atime); |
| 574 | sqlite3_bind_int64 (stmt, 5, mtime); |
| 575 | sqlite3_bind_int64 (stmt, 6, ctime); |
| 576 | sqlite3_bind_int (stmt, 7, mode); |
Alexander Afanasyev | 334aac8 | 2013-01-18 14:06:39 -0800 | [diff] [blame] | 577 | sqlite3_bind_int (stmt, 8, seg_num); |
| 578 | sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 579 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 580 | sqlite3_step (stmt); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 581 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 582 | _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK, |
| 583 | sqlite3_errmsg (the->m_db)); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 584 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 585 | sqlite3_finalize (stmt); |
| 586 | |
| 587 | int affected_rows = sqlite3_changes (the->m_db); |
| 588 | if (affected_rows == 0) // file didn't exist |
| 589 | { |
| 590 | sqlite3_stmt *stmt; |
| 591 | sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState " |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 592 | "(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] | 593 | "VALUES (0, ?, ?, ?, ?, " |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 594 | "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?)", -1, &stmt, 0); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 595 | |
| 596 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 597 | sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_TRANSIENT); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 598 | sqlite3_bind_int64 (stmt, 3, seq_no); |
| 599 | sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 600 | sqlite3_bind_int64 (stmt, 5, atime); |
| 601 | sqlite3_bind_int64 (stmt, 6, mtime); |
| 602 | sqlite3_bind_int64 (stmt, 7, ctime); |
| 603 | sqlite3_bind_int (stmt, 8, mode); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 604 | sqlite3_bind_int (stmt, 9, seg_num); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 605 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 606 | sqlite3_step (stmt); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 607 | _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK, |
| 608 | sqlite3_errmsg (the->m_db)); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 609 | sqlite3_finalize (stmt); |
| 610 | } |
| 611 | } |
| 612 | else if (action == 1) // delete |
| 613 | { |
| 614 | sqlite3_stmt *stmt; |
| 615 | sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 616 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC); |
| 617 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 618 | _LOG_DEBUG ("Delete " << filename); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 619 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 620 | sqlite3_step (stmt); |
| 621 | sqlite3_finalize (stmt); |
| 622 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 623 | |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 624 | sqlite3_result_null (context); |
| 625 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 626 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 627 | /** |
| 628 | * @todo Implement checking modification time and permissions |
| 629 | */ |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 630 | bool |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 631 | ActionLog::KnownFileState(const std::string &filename, const Hash &hash |
| 632 | /*, time_t mtime, int chmod*/) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 633 | { |
| 634 | sqlite3_stmt *stmt; |
| 635 | sqlite3_prepare_v2 (m_db, "SELECT * FROM FileState WHERE filename = ? AND file_hash = ?;", -1, &stmt, 0); |
| 636 | sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC); |
| 637 | sqlite3_bind_blob(stmt, 2, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 638 | |
| 639 | bool retval = false; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 640 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 641 | { |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 642 | retval = true; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 643 | } |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 644 | sqlite3_finalize (stmt); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 645 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 646 | return retval; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 647 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 648 | |
| 649 | sqlite3_int64 |
| 650 | ActionLog::LogSize () |
| 651 | { |
| 652 | sqlite3_stmt *stmt; |
| 653 | sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0); |
| 654 | |
| 655 | sqlite3_int64 retval = -1; |
| 656 | if (sqlite3_step (stmt) == SQLITE_ROW) |
| 657 | { |
| 658 | retval = sqlite3_column_int64 (stmt, 0); |
| 659 | } |
| 660 | |
| 661 | return retval; |
| 662 | } |