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