blob: e677a75d4492cba187ff6496ca141bc3b7285a4d [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080019 */
20
Alexander Afanasyev242f8772013-01-01 23:26:31 -080021#include "db-helper.h"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080022#include "logging.h"
23
Alexander Afanasyevae43c502012-12-29 17:26:37 -080024#include <boost/make_shared.hpp>
25#include <boost/ref.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080026#include <boost/throw_exception.hpp>
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080027
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080028INIT_LOGGER("DbHelper");
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080029
Alexander Afanasyevae43c502012-12-29 17:26:37 -080030using namespace boost;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080031namespace fs = boost::filesystem;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080032
33const std::string INIT_DATABASE = "\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080034 PRAGMA foreign_keys = ON; \
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080035";
36
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080037DbHelper::DbHelper(const fs::path& path, const std::string& dbname)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080038{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039 fs::create_directories(path);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080040
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041 int res = sqlite3_open((path / dbname).c_str(), &m_db);
42 if (res != SQLITE_OK) {
43 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot open/create dabatabase: [" +
44 (path / dbname).string() + "]"));
45 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080046
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080047 res = sqlite3_create_function(m_db, "hash", 2, SQLITE_ANY, 0, 0, DbHelper::hash_xStep,
48 DbHelper::hash_xFinal);
49 if (res != SQLITE_OK) {
50 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``hash''"));
51 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080052
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053 res = sqlite3_create_function(m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0);
54 if (res != SQLITE_OK) {
55 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``is_prefix''"));
56 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080057
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080058 res = sqlite3_create_function(m_db, "directory_name", -1, SQLITE_ANY, 0,
59 DbHelper::directory_name_xFun, 0, 0);
60 if (res != SQLITE_OK) {
61 BOOST_THROW_EXCEPTION(Error::Db()
62 << errmsg_info_str("Cannot create function ``directory_name''"));
63 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -080064
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080065 res = sqlite3_create_function(m_db, "is_dir_prefix", 2, SQLITE_ANY, 0,
66 DbHelper::is_dir_prefix_xFun, 0, 0);
67 if (res != SQLITE_OK) {
68 BOOST_THROW_EXCEPTION(Error::Db() << errmsg_info_str("Cannot create function ``is_dir_prefix''"));
69 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -080070
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080071 sqlite3_exec(m_db, INIT_DATABASE.c_str(), NULL, NULL, NULL);
72 _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080073}
74
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080075DbHelper::~DbHelper()
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080076{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080077 int res = sqlite3_close(m_db);
78 if (res != SQLITE_OK) {
79 // complain
80 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080081}
82
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -080083void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080084DbHelper::hash_xStep(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080085{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080086 if (argc != 2) {
87 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
88 sqlite3_result_error(context, "Wrong arguments are supplied for ``hash'' function", -1);
89 return;
90 }
91 if (sqlite3_value_type(argv[0]) != SQLITE_BLOB || sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
92 // _LOG_ERROR ("Hash expects (blob,integer) parameters");
93 sqlite3_result_error(context, "Hash expects (blob,integer) parameters", -1);
94 return;
95 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080096
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080097 EVP_MD_CTX** hash_context =
98 reinterpret_cast<EVP_MD_CTX**>(sqlite3_aggregate_context(context, sizeof(EVP_MD_CTX*)));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080099
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100 if (hash_context == 0) {
101 sqlite3_result_error_nomem(context);
102 return;
103 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800104
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800105 if (*hash_context == 0) {
106 *hash_context = EVP_MD_CTX_create();
107 EVP_DigestInit_ex(*hash_context, HASH_FUNCTION(), 0);
108 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800109
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 int nameBytes = sqlite3_value_bytes(argv[0]);
111 const void* name = sqlite3_value_blob(argv[0]);
112 sqlite3_int64 seqno = sqlite3_value_int64(argv[1]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 EVP_DigestUpdate(*hash_context, name, nameBytes);
115 EVP_DigestUpdate(*hash_context, &seqno, sizeof(sqlite3_int64));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800116}
117
118void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800119DbHelper::hash_xFinal(sqlite3_context* context)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800120{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800121 EVP_MD_CTX** hash_context =
122 reinterpret_cast<EVP_MD_CTX**>(sqlite3_aggregate_context(context, sizeof(EVP_MD_CTX*)));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800123
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800124 if (hash_context == 0) {
125 sqlite3_result_error_nomem(context);
126 return;
127 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800128
129 if (*hash_context == 0) // no rows
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 {
131 char charNullResult = 0;
132 sqlite3_result_blob(context, &charNullResult, 1,
133 SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
134 return;
135 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800136
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800137 unsigned char* hash = new unsigned char[EVP_MAX_MD_SIZE];
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800138 unsigned int hashLength = 0;
139
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140 int ok = EVP_DigestFinal_ex(*hash_context, hash, &hashLength);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800141
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 sqlite3_result_blob(context, hash, hashLength,
143 SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
144 delete[] hash;
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800145
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800146 EVP_MD_CTX_destroy(*hash_context);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800147}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800148
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800149void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800150DbHelper::is_prefix_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800151{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800152 int len1 = sqlite3_value_bytes(argv[0]);
153 int len2 = sqlite3_value_bytes(argv[1]);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800154
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800155 if (len1 == 0) {
156 sqlite3_result_int(context, 1);
157 return;
158 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800159
160 if (len1 > len2) // first parameter should be at most equal in length to the second one
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800161 {
162 sqlite3_result_int(context, 0);
163 return;
164 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800165
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800166 if (memcmp(sqlite3_value_blob(argv[0]), sqlite3_value_blob(argv[1]), len1) == 0) {
167 sqlite3_result_int(context, 1);
168 }
169 else {
170 sqlite3_result_int(context, 0);
171 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800172}
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800173
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800174void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800175DbHelper::directory_name_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800176{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800177 if (argc != 1) {
178 sqlite3_result_error(context, "``directory_name'' expects 1 text argument", -1);
179 sqlite3_result_null(context);
180 return;
181 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800182
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800183 if (sqlite3_value_bytes(argv[0]) == 0) {
184 sqlite3_result_null(context);
185 return;
186 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800187
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800188 boost::filesystem::path filePath(
189 std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv[0])),
190 sqlite3_value_bytes(argv[0])));
191 std::string dirPath = filePath.parent_path().generic_string();
192 // _LOG_DEBUG("directory_name FUN: " << dirPath);
193 if (dirPath.size() == 0) {
194 sqlite3_result_null(context);
195 }
196 else {
197 sqlite3_result_text(context, dirPath.c_str(), dirPath.size(), SQLITE_TRANSIENT);
198 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800199}
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800200
201void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800202DbHelper::is_dir_prefix_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800203{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800204 int len1 = sqlite3_value_bytes(argv[0]);
205 int len2 = sqlite3_value_bytes(argv[1]);
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800206
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800207 if (len1 == 0) {
208 sqlite3_result_int(context, 1);
209 return;
210 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800211
212 if (len1 > len2) // first parameter should be at most equal in length to the second one
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800213 {
214 sqlite3_result_int(context, 0);
215 return;
216 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800217
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800218 if (memcmp(sqlite3_value_blob(argv[0]), sqlite3_value_blob(argv[1]), len1) == 0) {
219 if (len1 == len2) {
220 sqlite3_result_int(context, 1);
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800221 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800222 else {
223 if (reinterpret_cast<const char*>(sqlite3_value_blob(argv[1]))[len1] == '/') {
224 sqlite3_result_int(context, 1);
225 }
226 else {
227 sqlite3_result_int(context, 0);
228 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800229 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800230 }
231 else {
232 sqlite3_result_int(context, 0);
233 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800234}