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 | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 23 | #include "logging.h" |
| 24 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 25 | #include <boost/make_shared.hpp> |
| 26 | #include <boost/ref.hpp> |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 27 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 29 | INIT_LOGGER ("DbHelper"); |
| 30 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 31 | using namespace boost; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 32 | namespace fs = boost::filesystem; |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 33 | |
| 34 | const std::string INIT_DATABASE = "\ |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame^] | 35 | PRAGMA foreign_keys = ON; \ |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 36 | "; |
| 37 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 38 | DbHelper::DbHelper (const fs::path &path) |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 40 | fs::path chronoshareDirectory = path / ".chronoshare"; |
| 41 | fs::create_directories (chronoshareDirectory); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 43 | int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 44 | if (res != SQLITE_OK) |
| 45 | { |
| 46 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 47 | << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]")); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 48 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 50 | res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0, |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 51 | DbHelper::hash_xStep, DbHelper::hash_xFinal); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 52 | if (res != SQLITE_OK) |
| 53 | { |
| 54 | BOOST_THROW_EXCEPTION (Error::Db () |
| 55 | << errmsg_info_str ("Cannot create function ``hash''")); |
| 56 | } |
| 57 | |
| 58 | // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go... |
| 59 | // for now, just attempt to create everything |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 60 | sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL); |
| 61 | _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db)); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | DbHelper::~DbHelper () |
| 65 | { |
| 66 | int res = sqlite3_close (m_db); |
| 67 | if (res != SQLITE_OK) |
| 68 | { |
| 69 | // complain |
| 70 | } |
| 71 | } |
| 72 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 73 | void |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 74 | DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 75 | { |
| 76 | if (argc != 2) |
| 77 | { |
| 78 | // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function"); |
| 79 | sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1); |
| 80 | return; |
| 81 | } |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 82 | if (sqlite3_value_type (argv[0]) != SQLITE_BLOB || |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 83 | sqlite3_value_type (argv[1]) != SQLITE_INTEGER) |
| 84 | { |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 85 | // _LOG_ERROR ("Hash expects (blob,integer) parameters"); |
| 86 | sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 87 | return; |
| 88 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 89 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 90 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 91 | |
| 92 | if (hash_context == 0) |
| 93 | { |
| 94 | sqlite3_result_error_nomem (context); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | if (*hash_context == 0) |
| 99 | { |
| 100 | *hash_context = EVP_MD_CTX_create (); |
| 101 | EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0); |
| 102 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 104 | int nameBytes = sqlite3_value_bytes (argv[0]); |
| 105 | const void *name = sqlite3_value_blob (argv[0]); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 106 | sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]); |
| 107 | |
| 108 | EVP_DigestUpdate (*hash_context, name, nameBytes); |
| 109 | EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64)); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | DbHelper::hash_xFinal (sqlite3_context *context) |
| 114 | { |
| 115 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 116 | |
| 117 | if (hash_context == 0) |
| 118 | { |
| 119 | sqlite3_result_error_nomem (context); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (*hash_context == 0) // no rows |
| 124 | { |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 125 | char charNullResult = 0; |
| 126 | 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] | 127 | return; |
| 128 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 130 | unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE]; |
| 131 | unsigned int hashLength = 0; |
| 132 | |
| 133 | int ok = EVP_DigestFinal_ex (*hash_context, |
| 134 | hash, &hashLength); |
| 135 | |
| 136 | sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
| 137 | delete [] hash; |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 138 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 139 | EVP_MD_CTX_destroy (*hash_context); |
| 140 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 141 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 143 | |