Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 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 | |
Alexander Afanasyev | 242f877 | 2013-01-01 23:26:31 -0800 | [diff] [blame] | 22 | #include "db-helper.h" |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 23 | // #include "sync-log.h" |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 24 | #include <boost/make_shared.hpp> |
| 25 | #include <boost/ref.hpp> |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 26 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 28 | using namespace boost; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame^] | 29 | namespace fs = boost::filesystem; |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 30 | |
| 31 | const std::string INIT_DATABASE = "\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 32 | PRAGMA foreign_keys = ON; \n\ |
| 33 | \n\ |
| 34 | CREATE TABLE \n\ |
| 35 | SyncNodes( \n\ |
| 36 | device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 37 | device_name BLOB NOT NULL, \n\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 38 | description TEXT, \n\ |
| 39 | seq_no INTEGER NOT NULL, \n\ |
| 40 | last_known_tdi TEXT, \n\ |
| 41 | last_update TIMESTAMP \n\ |
| 42 | ); \n\ |
| 43 | \n\ |
| 44 | CREATE TRIGGER SyncNodesUpdater_trigger \n\ |
| 45 | BEFORE INSERT ON SyncNodes \n\ |
| 46 | FOR EACH ROW \n\ |
| 47 | WHEN (SELECT device_id \n\ |
| 48 | FROM SyncNodes \n\ |
| 49 | WHERE device_name=NEW.device_name) \n\ |
| 50 | IS NOT NULL \n\ |
| 51 | BEGIN \n\ |
| 52 | UPDATE SyncNodes \n\ |
| 53 | SET seq_no=max(seq_no,NEW.seq_no) \n\ |
| 54 | WHERE device_name=NEW.device_name; \n\ |
| 55 | SELECT RAISE(IGNORE); \n\ |
| 56 | END; \n\ |
| 57 | \n\ |
| 58 | CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\ |
| 59 | \n\ |
| 60 | CREATE TABLE SyncLog( \n\ |
| 61 | state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
| 62 | state_hash BLOB NOT NULL UNIQUE, \n\ |
| 63 | last_update TIMESTAMP NOT NULL \n\ |
| 64 | ); \n\ |
| 65 | \n\ |
| 66 | CREATE TABLE \n\ |
| 67 | SyncStateNodes( \n\ |
| 68 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
| 69 | state_id INTEGER NOT NULL \n\ |
| 70 | REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\ |
| 71 | device_id INTEGER NOT NULL \n\ |
| 72 | REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\ |
| 73 | seq_no INTEGER NOT NULL \n\ |
| 74 | ); \n\ |
| 75 | \n\ |
| 76 | CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\ |
| 77 | CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\ |
| 78 | CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\ |
| 79 | \n\ |
| 80 | CREATE TRIGGER SyncLogGuard_trigger \n\ |
| 81 | BEFORE INSERT ON SyncLog \n\ |
| 82 | FOR EACH ROW \n\ |
| 83 | WHEN (SELECT state_hash \n\ |
| 84 | FROM SyncLog \n\ |
| 85 | WHERE state_hash=NEW.state_hash) \n\ |
| 86 | IS NOT NULL \n\ |
| 87 | BEGIN \n\ |
| 88 | DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\ |
| 89 | END; \n\ |
| 90 | \n\ |
| 91 | CREATE TABLE ActionLog ( \n\ |
| 92 | device_id INTEGER NOT NULL, \n\ |
| 93 | seq_no INTEGER NOT NULL, \n\ |
| 94 | \n\ |
| 95 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 96 | filename TEXT NOT NULL, \n\ |
| 97 | \n\ |
| 98 | version INTEGER NOT NULL, \n\ |
| 99 | action_timestamp TIMESTAMP NOT NULL, \n\ |
| 100 | \n\ |
| 101 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 102 | file_atime TIMESTAMP, \n\ |
| 103 | file_mtime TIMESTAMP, \n\ |
| 104 | file_ctime TIMESTAMP, \n\ |
| 105 | file_chmod INTEGER, \n\ |
| 106 | \n\ |
| 107 | parent_device_id INTEGER, \n\ |
| 108 | parent_seq_no INTEGER, \n\ |
| 109 | \n\ |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 110 | action_name TEXT, \n\ |
| 111 | action_content_object BLOB, \n\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 112 | \n\ |
| 113 | PRIMARY KEY (device_id, seq_no), \n\ |
| 114 | \n\ |
| 115 | FOREIGN KEY (parent_device_id, parent_seq_no) \n\ |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 116 | REFERENCES ActionLog (device_id, seq_no) \n\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 117 | ON UPDATE RESTRICT \n\ |
| 118 | ON DELETE SET NULL \n\ |
| 119 | ); \n\ |
| 120 | \n\ |
| 121 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 122 | CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\ |
| 123 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 124 | \n\ |
| 125 | CREATE TRIGGER ActionLogInsert_trigger \n\ |
| 126 | AFTER INSERT ON ActionLog \n\ |
| 127 | FOR EACH ROW \n\ |
| 128 | WHEN (SELECT device_id \n\ |
| 129 | FROM ActionLog \n\ |
| 130 | WHERE filename=NEW.filename AND \n\ |
| 131 | version > NEW.version) IS NULL AND \n\ |
| 132 | (SELECT a.device_id \n\ |
| 133 | FROM ActionLog a \n\ |
| 134 | LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\ |
| 135 | WHERE filename=NEW.filename AND \n\ |
| 136 | version = NEW.version AND \n\ |
| 137 | a.device_id != NEW.device_id AND \n\ |
| 138 | s.device_name > (SELECT device_name \n\ |
| 139 | FROM SyncNodes \n\ |
| 140 | WHERE device_id=NEW.device_id)) IS NULL \n\ |
| 141 | BEGIN \n\ |
| 142 | SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \ |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 143 | NEW.device_id, NEW.seq_no, \ |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 144 | NEW.action,NEW.filename,NEW.file_hash, \ |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 145 | strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \ |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 146 | NEW.file_chmod); /* function that applies action and adds record the FileState */ \n \ |
| 147 | END; \n\ |
| 148 | \n\ |
| 149 | CREATE TABLE FileState ( \n\ |
| 150 | type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\ |
| 151 | filename TEXT NOT NULL, \n\ |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 152 | device_id INTEGER NOT NULL, \n\ |
| 153 | seq_no INTEGER NOT NULL, \n\ |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 154 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 155 | file_atime TIMESTAMP, \n\ |
| 156 | file_mtime TIMESTAMP, \n\ |
| 157 | file_ctime TIMESTAMP, \n\ |
| 158 | file_chmod INTEGER, \n\ |
| 159 | \n\ |
| 160 | PRIMARY KEY (type, filename) \n\ |
| 161 | ); \n\ |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 162 | \n\ |
| 163 | CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\ |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 164 | "; |
| 165 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame^] | 166 | DbHelper::DbHelper (const fs::path &path) |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 167 | { |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame^] | 168 | fs::path chronoshareDirectory = path / ".chronoshare"; |
| 169 | fs::create_directories (chronoshareDirectory); |
| 170 | |
| 171 | int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 172 | if (res != SQLITE_OK) |
| 173 | { |
| 174 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame^] | 175 | << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]")); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0, |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 179 | DbHelper::hash_xStep, DbHelper::hash_xFinal); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 180 | if (res != SQLITE_OK) |
| 181 | { |
| 182 | BOOST_THROW_EXCEPTION (Error::Db () |
| 183 | << errmsg_info_str ("Cannot create function ``hash''")); |
| 184 | } |
| 185 | |
| 186 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 187 | // for now, just attempt to create everything |
| 188 | |
| 189 | char *errmsg = 0; |
| 190 | res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 191 | if (res != SQLITE_OK && errmsg != 0) |
| 192 | { |
| 193 | std::cerr << "DEBUG: " << errmsg << std::endl; |
| 194 | sqlite3_free (errmsg); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | DbHelper::~DbHelper () |
| 199 | { |
| 200 | int res = sqlite3_close (m_db); |
| 201 | if (res != SQLITE_OK) |
| 202 | { |
| 203 | // complain |
| 204 | } |
| 205 | } |
| 206 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 207 | void |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 208 | DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 209 | { |
| 210 | if (argc != 2) |
| 211 | { |
| 212 | // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function"); |
| 213 | sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1); |
| 214 | return; |
| 215 | } |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 216 | if (sqlite3_value_type (argv[0]) != SQLITE_BLOB || |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 217 | sqlite3_value_type (argv[1]) != SQLITE_INTEGER) |
| 218 | { |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 219 | // _LOG_ERROR ("Hash expects (blob,integer) parameters"); |
| 220 | sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 221 | return; |
| 222 | } |
| 223 | |
| 224 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 225 | |
| 226 | if (hash_context == 0) |
| 227 | { |
| 228 | sqlite3_result_error_nomem (context); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | if (*hash_context == 0) |
| 233 | { |
| 234 | *hash_context = EVP_MD_CTX_create (); |
| 235 | EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0); |
| 236 | } |
| 237 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 238 | int nameBytes = sqlite3_value_bytes (argv[0]); |
| 239 | const void *name = sqlite3_value_blob (argv[0]); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 240 | sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]); |
| 241 | |
| 242 | EVP_DigestUpdate (*hash_context, name, nameBytes); |
| 243 | EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64)); |
| 244 | } |
| 245 | |
| 246 | void |
| 247 | DbHelper::hash_xFinal (sqlite3_context *context) |
| 248 | { |
| 249 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 250 | |
| 251 | if (hash_context == 0) |
| 252 | { |
| 253 | sqlite3_result_error_nomem (context); |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (*hash_context == 0) // no rows |
| 258 | { |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 259 | char charNullResult = 0; |
| 260 | sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 261 | return; |
| 262 | } |
| 263 | |
| 264 | unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE]; |
| 265 | unsigned int hashLength = 0; |
| 266 | |
| 267 | int ok = EVP_DigestFinal_ex (*hash_context, |
| 268 | hash, &hashLength); |
| 269 | |
| 270 | sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
| 271 | delete [] hash; |
| 272 | |
| 273 | EVP_MD_CTX_destroy (*hash_context); |
| 274 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 275 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 276 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 277 | |