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 | |
| 22 | #include "sqlite-helper.h" |
| 23 | // #include "sync-log.h" |
| 24 | |
| 25 | // Other options: VP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha256, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160 |
| 26 | #define HASH_FUNCTION EVP_sha256 |
| 27 | |
| 28 | #include <boost/throw_exception.hpp> |
| 29 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 30 | // typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 31 | |
| 32 | |
| 33 | const std::string INIT_DATABASE = "\ |
| 34 | CREATE TABLE \ |
| 35 | SyncNodes( \ |
| 36 | device_id INTEGER PRIMARY KEY AUTOINCREMENT, \ |
| 37 | device_name TEXT NOT NULL, \ |
| 38 | description TEXT, \ |
| 39 | seq_no INTEGER NOT NULL, \ |
| 40 | last_known_tdi TEXT, \ |
| 41 | last_update TIMESTAMP \ |
| 42 | ); \ |
| 43 | \ |
| 44 | CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \ |
| 45 | \ |
| 46 | CREATE TABLE SyncLog( \ |
| 47 | state_hash BLOB NOT NULL PRIMARY KEY, \ |
| 48 | last_update TIMESTAMP NOT NULL \ |
| 49 | ); \ |
| 50 | \ |
| 51 | CREATE TABLE \ |
| 52 | SyncStateNodes( \ |
| 53 | id INTEGER PRIMARY KEY AUTOINCREMENT, \ |
| 54 | state_hash BLOB NOT NULL \ |
| 55 | REFERENCES SyncLog (state_hash) ON UPDATE CASCADE ON DELETE CASCADE, \ |
| 56 | device_id INTEGER NOT NULL \ |
| 57 | REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \ |
| 58 | seq_no INTEGER NOT NULL \ |
| 59 | ); \ |
| 60 | \ |
| 61 | CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \ |
| 62 | CREATE INDEX SyncStateNodes_state_hash ON SyncStateNodes (state_hash); \ |
| 63 | CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \ |
| 64 | \ |
| 65 | CREATE TRIGGER SyncLogGuard_trigger \ |
| 66 | BEFORE INSERT ON SyncLog \ |
| 67 | FOR EACH ROW \ |
| 68 | WHEN (SELECT state_hash \ |
| 69 | FROM SyncLog \ |
| 70 | WHERE state_hash=NEW.state_hash) \ |
| 71 | IS NOT NULL \ |
| 72 | BEGIN \ |
| 73 | DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \ |
| 74 | END; \ |
| 75 | "; |
| 76 | |
| 77 | DbHelper::DbHelper (const std::string &path) |
| 78 | { |
| 79 | int res = sqlite3_open((path+"chronoshare.db").c_str (), &m_db); |
| 80 | if (res != SQLITE_OK) |
| 81 | { |
| 82 | BOOST_THROW_EXCEPTION (Error::Db () |
| 83 | << errmsg_info_str ("Cannot open/create dabatabase: [" + path + "chronoshare.db" + "]")); |
| 84 | } |
| 85 | |
| 86 | res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0, |
| 87 | DbHelper::hash_xStep, DbHelper::hash_xFinal); |
| 88 | if (res != SQLITE_OK) |
| 89 | { |
| 90 | BOOST_THROW_EXCEPTION (Error::Db () |
| 91 | << errmsg_info_str ("Cannot create function ``hash''")); |
| 92 | } |
| 93 | |
| 94 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 95 | // for now, just attempt to create everything |
| 96 | |
| 97 | char *errmsg = 0; |
| 98 | res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg); |
| 99 | // if (res != SQLITE_OK && errmsg != 0) |
| 100 | // { |
| 101 | // std::cerr << "DEBUG: " << errmsg << std::endl; |
| 102 | // sqlite3_free (errmsg); |
| 103 | // } |
| 104 | |
| 105 | res = sqlite3_exec (m_db, "INSERT INTO SyncLog (state_hash, last_update) SELECT hash(device_name, seq_no), datetime('now') FROM SyncNodes ORDER BY device_name", |
| 106 | NULL, NULL, &errmsg); |
| 107 | if (res != SQLITE_OK && errmsg != 0) |
| 108 | { |
| 109 | std::cerr << "DEBUG: " << errmsg << std::endl; |
| 110 | sqlite3_free (errmsg); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | DbHelper::~DbHelper () |
| 115 | { |
| 116 | int res = sqlite3_close (m_db); |
| 117 | if (res != SQLITE_OK) |
| 118 | { |
| 119 | // complain |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 125 | { |
| 126 | if (argc != 2) |
| 127 | { |
| 128 | // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function"); |
| 129 | sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1); |
| 130 | return; |
| 131 | } |
| 132 | if (sqlite3_value_type (argv[0]) != SQLITE_TEXT || |
| 133 | sqlite3_value_type (argv[1]) != SQLITE_INTEGER) |
| 134 | { |
| 135 | // _LOG_ERROR ("Hash expects (text,integer) parameters"); |
| 136 | sqlite3_result_error (context, "Hash expects (text,integer) parameters", -1); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 141 | |
| 142 | if (hash_context == 0) |
| 143 | { |
| 144 | sqlite3_result_error_nomem (context); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (*hash_context == 0) |
| 149 | { |
| 150 | *hash_context = EVP_MD_CTX_create (); |
| 151 | EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0); |
| 152 | } |
| 153 | |
| 154 | int nameBytes = sqlite3_value_bytes (argv[0]); |
| 155 | const unsigned char *name = sqlite3_value_text (argv[0]); |
| 156 | sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]); |
| 157 | |
| 158 | EVP_DigestUpdate (*hash_context, name, nameBytes); |
| 159 | EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64)); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | DbHelper::hash_xFinal (sqlite3_context *context) |
| 164 | { |
| 165 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 166 | |
| 167 | if (hash_context == 0) |
| 168 | { |
| 169 | sqlite3_result_error_nomem (context); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (*hash_context == 0) // no rows |
| 174 | { |
| 175 | sqlite3_result_null (context); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE]; |
| 180 | unsigned int hashLength = 0; |
| 181 | |
| 182 | int ok = EVP_DigestFinal_ex (*hash_context, |
| 183 | hash, &hashLength); |
| 184 | |
| 185 | sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
| 186 | delete [] hash; |
| 187 | |
| 188 | EVP_MD_CTX_destroy (*hash_context); |
| 189 | } |