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 | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 29 | |
| 30 | const std::string INIT_DATABASE = "\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 31 | PRAGMA foreign_keys = ON; \n\ |
| 32 | \n\ |
| 33 | CREATE TABLE \n\ |
| 34 | SyncNodes( \n\ |
| 35 | device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
| 36 | device_name TEXT NOT NULL, \n\ |
| 37 | description TEXT, \n\ |
| 38 | seq_no INTEGER NOT NULL, \n\ |
| 39 | last_known_tdi TEXT, \n\ |
| 40 | last_update TIMESTAMP \n\ |
| 41 | ); \n\ |
| 42 | \n\ |
| 43 | CREATE TRIGGER SyncNodesUpdater_trigger \n\ |
| 44 | BEFORE INSERT ON SyncNodes \n\ |
| 45 | FOR EACH ROW \n\ |
| 46 | WHEN (SELECT device_id \n\ |
| 47 | FROM SyncNodes \n\ |
| 48 | WHERE device_name=NEW.device_name) \n\ |
| 49 | IS NOT NULL \n\ |
| 50 | BEGIN \n\ |
| 51 | UPDATE SyncNodes \n\ |
| 52 | SET seq_no=max(seq_no,NEW.seq_no) \n\ |
| 53 | WHERE device_name=NEW.device_name; \n\ |
| 54 | SELECT RAISE(IGNORE); \n\ |
| 55 | END; \n\ |
| 56 | \n\ |
| 57 | CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\ |
| 58 | \n\ |
| 59 | CREATE TABLE SyncLog( \n\ |
| 60 | state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
| 61 | state_hash BLOB NOT NULL UNIQUE, \n\ |
| 62 | last_update TIMESTAMP NOT NULL \n\ |
| 63 | ); \n\ |
| 64 | \n\ |
| 65 | CREATE TABLE \n\ |
| 66 | SyncStateNodes( \n\ |
| 67 | id INTEGER PRIMARY KEY AUTOINCREMENT, \n\ |
| 68 | state_id INTEGER NOT NULL \n\ |
| 69 | REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\ |
| 70 | device_id INTEGER NOT NULL \n\ |
| 71 | REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\ |
| 72 | seq_no INTEGER NOT NULL \n\ |
| 73 | ); \n\ |
| 74 | \n\ |
| 75 | CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\ |
| 76 | CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\ |
| 77 | CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\ |
| 78 | \n\ |
| 79 | CREATE TRIGGER SyncLogGuard_trigger \n\ |
| 80 | BEFORE INSERT ON SyncLog \n\ |
| 81 | FOR EACH ROW \n\ |
| 82 | WHEN (SELECT state_hash \n\ |
| 83 | FROM SyncLog \n\ |
| 84 | WHERE state_hash=NEW.state_hash) \n\ |
| 85 | IS NOT NULL \n\ |
| 86 | BEGIN \n\ |
| 87 | DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\ |
| 88 | END; \n\ |
| 89 | \n\ |
| 90 | CREATE TABLE ActionLog ( \n\ |
| 91 | device_id INTEGER NOT NULL, \n\ |
| 92 | seq_no INTEGER NOT NULL, \n\ |
| 93 | \n\ |
| 94 | action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\ |
| 95 | filename TEXT NOT NULL, \n\ |
| 96 | \n\ |
| 97 | version INTEGER NOT NULL, \n\ |
| 98 | action_timestamp TIMESTAMP NOT NULL, \n\ |
| 99 | \n\ |
| 100 | file_hash BLOB, /* NULL if action is \"delete\" */ \n\ |
| 101 | file_atime TIMESTAMP, \n\ |
| 102 | file_mtime TIMESTAMP, \n\ |
| 103 | file_ctime TIMESTAMP, \n\ |
| 104 | file_chmod INTEGER, \n\ |
| 105 | \n\ |
| 106 | parent_device_id INTEGER, \n\ |
| 107 | parent_seq_no INTEGER, \n\ |
| 108 | \n\ |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame^] | 109 | action_name TEXT, \n\ |
| 110 | action_content_object BLOB, \n\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 111 | \n\ |
| 112 | PRIMARY KEY (device_id, seq_no), \n\ |
| 113 | \n\ |
| 114 | FOREIGN KEY (parent_device_id, parent_seq_no) \n\ |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame^] | 115 | REFERENCES ActionLog (device_id, seq_no) \n\ |
Alexander Afanasyev | 8811b35 | 2013-01-02 12:51:15 -0800 | [diff] [blame] | 116 | ON UPDATE RESTRICT \n\ |
| 117 | ON DELETE SET NULL \n\ |
| 118 | ); \n\ |
| 119 | \n\ |
| 120 | CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\ |
| 121 | CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\ |
| 122 | CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\ |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 123 | "; |
| 124 | |
| 125 | DbHelper::DbHelper (const std::string &path) |
| 126 | { |
| 127 | int res = sqlite3_open((path+"chronoshare.db").c_str (), &m_db); |
| 128 | if (res != SQLITE_OK) |
| 129 | { |
| 130 | BOOST_THROW_EXCEPTION (Error::Db () |
| 131 | << errmsg_info_str ("Cannot open/create dabatabase: [" + path + "chronoshare.db" + "]")); |
| 132 | } |
| 133 | |
| 134 | res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0, |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 135 | DbHelper::hash_xStep, DbHelper::hash_xFinal); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 136 | if (res != SQLITE_OK) |
| 137 | { |
| 138 | BOOST_THROW_EXCEPTION (Error::Db () |
| 139 | << errmsg_info_str ("Cannot create function ``hash''")); |
| 140 | } |
| 141 | |
| 142 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 143 | // for now, just attempt to create everything |
| 144 | |
| 145 | char *errmsg = 0; |
| 146 | res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 147 | if (res != SQLITE_OK && errmsg != 0) |
| 148 | { |
| 149 | std::cerr << "DEBUG: " << errmsg << std::endl; |
| 150 | sqlite3_free (errmsg); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | DbHelper::~DbHelper () |
| 155 | { |
| 156 | int res = sqlite3_close (m_db); |
| 157 | if (res != SQLITE_OK) |
| 158 | { |
| 159 | // complain |
| 160 | } |
| 161 | } |
| 162 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 163 | void |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 164 | DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 165 | { |
| 166 | if (argc != 2) |
| 167 | { |
| 168 | // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function"); |
| 169 | sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1); |
| 170 | return; |
| 171 | } |
| 172 | if (sqlite3_value_type (argv[0]) != SQLITE_TEXT || |
| 173 | sqlite3_value_type (argv[1]) != SQLITE_INTEGER) |
| 174 | { |
| 175 | // _LOG_ERROR ("Hash expects (text,integer) parameters"); |
| 176 | sqlite3_result_error (context, "Hash expects (text,integer) parameters", -1); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 181 | |
| 182 | if (hash_context == 0) |
| 183 | { |
| 184 | sqlite3_result_error_nomem (context); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | if (*hash_context == 0) |
| 189 | { |
| 190 | *hash_context = EVP_MD_CTX_create (); |
| 191 | EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0); |
| 192 | } |
| 193 | |
| 194 | int nameBytes = sqlite3_value_bytes (argv[0]); |
| 195 | const unsigned char *name = sqlite3_value_text (argv[0]); |
| 196 | sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]); |
| 197 | |
| 198 | EVP_DigestUpdate (*hash_context, name, nameBytes); |
| 199 | EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64)); |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | DbHelper::hash_xFinal (sqlite3_context *context) |
| 204 | { |
| 205 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 206 | |
| 207 | if (hash_context == 0) |
| 208 | { |
| 209 | sqlite3_result_error_nomem (context); |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | if (*hash_context == 0) // no rows |
| 214 | { |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 215 | char charNullResult = 0; |
| 216 | 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] | 217 | return; |
| 218 | } |
| 219 | |
| 220 | unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE]; |
| 221 | unsigned int hashLength = 0; |
| 222 | |
| 223 | int ok = EVP_DigestFinal_ex (*hash_context, |
| 224 | hash, &hashLength); |
| 225 | |
| 226 | sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
| 227 | delete [] hash; |
| 228 | |
| 229 | EVP_MD_CTX_destroy (*hash_context); |
| 230 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 231 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 232 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 233 | |