blob: 8adf5a03b9210f1897821f7a9809d93a569356c6 [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 Afanasyevc7170bc2016-12-25 16:17:26 -080021#include "db-helper.hpp"
22#include "core/logging.hpp"
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080023
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080024#include <ndn-cxx/util/digest.hpp>
25
26namespace ndn {
27namespace chronoshare {
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080028
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080029INIT_LOGGER("DbHelper");
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080030
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080031namespace fs = boost::filesystem;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080032
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080033using util::Sha256;
34
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080035const std::string INIT_DATABASE = "\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080036 PRAGMA foreign_keys = ON; \
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080037";
38
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080039DbHelper::DbHelper(const fs::path& path, const std::string& dbname)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080040{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080041 fs::create_directories(path);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080042
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080043 int res = sqlite3_open((path / dbname).c_str(), &m_db);
44 if (res != SQLITE_OK) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080045 BOOST_THROW_EXCEPTION(Error("Cannot open/create database: [" + (path / dbname).string() + "]"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080046 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080047
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080048 res = sqlite3_create_function(m_db, "hash", 2, SQLITE_ANY, 0, 0, DbHelper::hash_xStep,
49 DbHelper::hash_xFinal);
50 if (res != SQLITE_OK) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080051 BOOST_THROW_EXCEPTION(Error("Cannot create function ``hash''"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080052 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080053
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080054 res = sqlite3_create_function(m_db, "is_prefix", 2, SQLITE_ANY, 0, DbHelper::is_prefix_xFun, 0, 0);
55 if (res != SQLITE_OK) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080056 BOOST_THROW_EXCEPTION(Error("Cannot create function ``is_prefix''"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080057 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -080058
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059 res = sqlite3_create_function(m_db, "directory_name", -1, SQLITE_ANY, 0,
60 DbHelper::directory_name_xFun, 0, 0);
61 if (res != SQLITE_OK) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080062 BOOST_THROW_EXCEPTION(Error("Cannot create function ``directory_name''"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080063 }
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) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080068 BOOST_THROW_EXCEPTION(Error("Cannot create function ``is_dir_prefix''"));
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080069 }
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) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080087 // _LOG_ERROR("Wrong arguments are supplied for ``hash'' function");
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080088 sqlite3_result_error(context, "Wrong arguments are supplied for ``hash'' function", -1);
89 return;
90 }
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080091
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080092 if (sqlite3_value_type(argv[0]) != SQLITE_BLOB || sqlite3_value_type(argv[1]) != SQLITE_INTEGER) {
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080093 // _LOG_ERROR("Hash expects(blob,integer) parameters");
94 sqlite3_result_error(context, "Hash expects(blob,integer) parameters", -1);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080095 return;
96 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080097
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -080098 Sha256** digest = reinterpret_cast<Sha256**>(sqlite3_aggregate_context(context, sizeof(Sha256*)));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080099
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800100 if (digest == nullptr) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800101 sqlite3_result_error_nomem(context);
102 return;
103 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800104
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800105 if (*digest == nullptr) {
106 *digest = new Sha256();
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800107 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800108
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800109 int nameBytes = sqlite3_value_bytes(argv[0]);
110 const void* name = sqlite3_value_blob(argv[0]);
111 sqlite3_int64 seqno = sqlite3_value_int64(argv[1]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800112
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800113 (*digest)->update(reinterpret_cast<const uint8_t*>(name), nameBytes);
114 (*digest)->update(reinterpret_cast<const uint8_t*>(&seqno), sizeof(sqlite3_int64));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800115}
116
117void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118DbHelper::hash_xFinal(sqlite3_context* context)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800119{
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800120 Sha256** digest = reinterpret_cast<Sha256**>(sqlite3_aggregate_context(context, sizeof(Sha256*)));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800121
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800122 if (digest == nullptr) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800123 sqlite3_result_error_nomem(context);
124 return;
125 }
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800126
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800127 if (*digest == nullptr) {
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800128 char charNullResult = 0;
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800129 sqlite3_result_blob(context, &charNullResult, 1, SQLITE_TRANSIENT);
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800130 return;
131 }
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800132
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800133 shared_ptr<const Buffer> hash = (*digest)->computeDigest();
134 sqlite3_result_blob(context, hash->buf(), hash->size(), SQLITE_TRANSIENT);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800135
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800136 delete *digest;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800137}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800138
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800139void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800140DbHelper::is_prefix_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800141{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800142 int len1 = sqlite3_value_bytes(argv[0]);
143 int len2 = sqlite3_value_bytes(argv[1]);
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800144
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800145 if (len1 == 0) {
146 sqlite3_result_int(context, 1);
147 return;
148 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800149
150 if (len1 > len2) // first parameter should be at most equal in length to the second one
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800151 {
152 sqlite3_result_int(context, 0);
153 return;
154 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800155
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800156 if (memcmp(sqlite3_value_blob(argv[0]), sqlite3_value_blob(argv[1]), len1) == 0) {
157 sqlite3_result_int(context, 1);
158 }
159 else {
160 sqlite3_result_int(context, 0);
161 }
Alexander Afanasyev026eaf32013-02-23 16:37:14 -0800162}
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800163
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800164void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800165DbHelper::directory_name_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800166{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800167 if (argc != 1) {
168 sqlite3_result_error(context, "``directory_name'' expects 1 text argument", -1);
169 sqlite3_result_null(context);
170 return;
171 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800172
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800173 if (sqlite3_value_bytes(argv[0]) == 0) {
174 sqlite3_result_null(context);
175 return;
176 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800177
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800178 boost::filesystem::path filePath(
179 std::string(reinterpret_cast<const char*>(sqlite3_value_text(argv[0])),
180 sqlite3_value_bytes(argv[0])));
181 std::string dirPath = filePath.parent_path().generic_string();
182 // _LOG_DEBUG("directory_name FUN: " << dirPath);
183 if (dirPath.size() == 0) {
184 sqlite3_result_null(context);
185 }
186 else {
187 sqlite3_result_text(context, dirPath.c_str(), dirPath.size(), SQLITE_TRANSIENT);
188 }
Alexander Afanasyev95f9f552013-02-26 23:05:20 -0800189}
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800190
191void
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800192DbHelper::is_dir_prefix_xFun(sqlite3_context* context, int argc, sqlite3_value** argv)
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800193{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800194 int len1 = sqlite3_value_bytes(argv[0]);
195 int len2 = sqlite3_value_bytes(argv[1]);
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800196
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800197 if (len1 == 0) {
198 sqlite3_result_int(context, 1);
199 return;
200 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800201
202 if (len1 > len2) // first parameter should be at most equal in length to the second one
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800203 {
204 sqlite3_result_int(context, 0);
205 return;
206 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800207
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800208 if (memcmp(sqlite3_value_blob(argv[0]), sqlite3_value_blob(argv[1]), len1) == 0) {
209 if (len1 == len2) {
210 sqlite3_result_int(context, 1);
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800211 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800212 else {
213 if (reinterpret_cast<const char*>(sqlite3_value_blob(argv[1]))[len1] == '/') {
214 sqlite3_result_int(context, 1);
215 }
216 else {
217 sqlite3_result_int(context, 0);
218 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800219 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800220 }
221 else {
222 sqlite3_result_int(context, 0);
223 }
Alexander Afanasyev20fd84a2013-02-27 12:17:00 -0800224}
Alexander Afanasyevc7170bc2016-12-25 16:17:26 -0800225
226} // chronoshare
227} // ndn