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 | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 38 | DbHelper::DbHelper (const fs::path &path, const std::string &dbname) |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 39 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 40 | fs::create_directories (path); |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 42 | int res = sqlite3_open((path / dbname).c_str (), &m_db); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 43 | if (res != SQLITE_OK) |
| 44 | { |
| 45 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 46 | << errmsg_info_str ("Cannot open/create dabatabase: [" + (path / dbname).string () + "]")); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 47 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 49 | res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0, |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 50 | DbHelper::hash_xStep, DbHelper::hash_xFinal); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 51 | if (res != SQLITE_OK) |
| 52 | { |
| 53 | BOOST_THROW_EXCEPTION (Error::Db () |
| 54 | << errmsg_info_str ("Cannot create function ``hash''")); |
| 55 | } |
| 56 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 57 | res = sqlite3_create_function (m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0); |
| 58 | if (res != SQLITE_OK) |
| 59 | { |
| 60 | BOOST_THROW_EXCEPTION (Error::Db () |
| 61 | << errmsg_info_str ("Cannot create function ``is_prefix''")); |
| 62 | } |
| 63 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 64 | res = sqlite3_create_function (m_db, "directory_name", -1, SQLITE_ANY, 0, DbHelper::directory_name_xFun, 0, 0); |
| 65 | if (res != SQLITE_OK) |
| 66 | { |
| 67 | BOOST_THROW_EXCEPTION (Error::Db () |
| 68 | << errmsg_info_str ("Cannot create function ``directory_name''")); |
| 69 | } |
| 70 | |
Alexander Afanasyev | 20fd84a | 2013-02-27 12:17:00 -0800 | [diff] [blame^] | 71 | res = sqlite3_create_function (m_db, "is_dir_prefix", 2, SQLITE_ANY, 0, DbHelper::is_dir_prefix_xFun, 0, 0); |
| 72 | if (res != SQLITE_OK) |
| 73 | { |
| 74 | BOOST_THROW_EXCEPTION (Error::Db () |
| 75 | << errmsg_info_str ("Cannot create function ``is_dir_prefix''")); |
| 76 | } |
| 77 | |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 78 | sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL); |
| 79 | _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] | 80 | } |
| 81 | |
| 82 | DbHelper::~DbHelper () |
| 83 | { |
| 84 | int res = sqlite3_close (m_db); |
| 85 | if (res != SQLITE_OK) |
| 86 | { |
| 87 | // complain |
| 88 | } |
| 89 | } |
| 90 | |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 91 | void |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 92 | DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 93 | { |
| 94 | if (argc != 2) |
| 95 | { |
| 96 | // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function"); |
| 97 | sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1); |
| 98 | return; |
| 99 | } |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 100 | if (sqlite3_value_type (argv[0]) != SQLITE_BLOB || |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 101 | sqlite3_value_type (argv[1]) != SQLITE_INTEGER) |
| 102 | { |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 103 | // _LOG_ERROR ("Hash expects (blob,integer) parameters"); |
| 104 | sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 108 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 109 | |
| 110 | if (hash_context == 0) |
| 111 | { |
| 112 | sqlite3_result_error_nomem (context); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | if (*hash_context == 0) |
| 117 | { |
| 118 | *hash_context = EVP_MD_CTX_create (); |
| 119 | EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0); |
| 120 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 122 | int nameBytes = sqlite3_value_bytes (argv[0]); |
| 123 | const void *name = sqlite3_value_blob (argv[0]); |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 124 | sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]); |
| 125 | |
| 126 | EVP_DigestUpdate (*hash_context, name, nameBytes); |
| 127 | EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64)); |
| 128 | } |
| 129 | |
| 130 | void |
| 131 | DbHelper::hash_xFinal (sqlite3_context *context) |
| 132 | { |
| 133 | EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *))); |
| 134 | |
| 135 | if (hash_context == 0) |
| 136 | { |
| 137 | sqlite3_result_error_nomem (context); |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | if (*hash_context == 0) // no rows |
| 142 | { |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 143 | char charNullResult = 0; |
| 144 | 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] | 145 | return; |
| 146 | } |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 148 | unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE]; |
| 149 | unsigned int hashLength = 0; |
| 150 | |
| 151 | int ok = EVP_DigestFinal_ex (*hash_context, |
| 152 | hash, &hashLength); |
| 153 | |
| 154 | sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy |
| 155 | delete [] hash; |
Alexander Afanasyev | 8e2104a | 2013-01-22 10:56:18 -0800 | [diff] [blame] | 156 | |
Alexander Afanasyev | 71b43e7 | 2012-12-27 01:03:43 -0800 | [diff] [blame] | 157 | EVP_MD_CTX_destroy (*hash_context); |
| 158 | } |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 160 | void |
| 161 | DbHelper::is_prefix_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 162 | { |
| 163 | int len1 = sqlite3_value_bytes (argv[0]); |
| 164 | int len2 = sqlite3_value_bytes (argv[1]); |
Alexander Afanasyev | de1cdd0 | 2012-12-29 14:41:46 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 166 | if (len1 == 0) |
| 167 | { |
| 168 | sqlite3_result_int (context, 1); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | if (len1 > len2) // first parameter should be at most equal in length to the second one |
| 173 | { |
| 174 | sqlite3_result_int (context, 0); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | if (memcmp (sqlite3_value_blob (argv[0]), sqlite3_value_blob (argv[1]), len1) == 0) |
| 179 | { |
| 180 | sqlite3_result_int (context, 1); |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | sqlite3_result_int (context, 0); |
| 185 | } |
| 186 | } |
Alexander Afanasyev | ae43c50 | 2012-12-29 17:26:37 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 188 | void |
| 189 | DbHelper::directory_name_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 190 | { |
| 191 | if (argc != 1) |
| 192 | { |
| 193 | sqlite3_result_error (context, "``directory_name'' expects 1 text argument", -1); |
| 194 | sqlite3_result_null (context); |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | if (sqlite3_value_bytes (argv[0]) == 0) |
| 199 | { |
| 200 | sqlite3_result_null (context); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | boost::filesystem::path filePath (std::string (reinterpret_cast<const char*> (sqlite3_value_text (argv[0])), sqlite3_value_bytes (argv[0]))); |
| 205 | std::string dirPath = filePath.parent_path ().generic_string (); |
| 206 | // _LOG_DEBUG ("directory_name FUN: " << dirPath); |
| 207 | if (dirPath.size () == 0) |
| 208 | { |
| 209 | sqlite3_result_null (context); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | sqlite3_result_text (context, dirPath.c_str (), dirPath.size (), SQLITE_TRANSIENT); |
| 214 | } |
| 215 | } |
Alexander Afanasyev | 20fd84a | 2013-02-27 12:17:00 -0800 | [diff] [blame^] | 216 | |
| 217 | void |
| 218 | DbHelper::is_dir_prefix_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 219 | { |
| 220 | int len1 = sqlite3_value_bytes (argv[0]); |
| 221 | int len2 = sqlite3_value_bytes (argv[1]); |
| 222 | |
| 223 | if (len1 == 0) |
| 224 | { |
| 225 | sqlite3_result_int (context, 1); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (len1 > len2) // first parameter should be at most equal in length to the second one |
| 230 | { |
| 231 | sqlite3_result_int (context, 0); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | if (memcmp (sqlite3_value_blob (argv[0]), sqlite3_value_blob (argv[1]), len1) == 0) |
| 236 | { |
| 237 | if (len1 == len2) |
| 238 | { |
| 239 | sqlite3_result_int (context, 1); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | if (reinterpret_cast<const char*> (sqlite3_value_blob (argv[1]))[len1] == '/') |
| 244 | { |
| 245 | sqlite3_result_int (context, 1); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | sqlite3_result_int (context, 0); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | { |
| 255 | sqlite3_result_int (context, 0); |
| 256 | } |
| 257 | } |