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