blob: c5455e87ba2a3fae8b81074e0226a99948ba8362 [file] [log] [blame]
Alexander Afanasyev71b43e72012-12-27 01:03:43 -08001/* -*- 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 Afanasyev242f8772013-01-01 23:26:31 -080022#include "db-helper.h"
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080023// #include "sync-log.h"
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 Afanasyevae43c502012-12-29 17:26:37 -080028using namespace boost;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080029namespace fs = boost::filesystem;
Alexander Afanasyev71b43e72012-12-27 01:03:43 -080030
31const std::string INIT_DATABASE = "\
Alexander Afanasyev8811b352013-01-02 12:51:15 -080032PRAGMA foreign_keys = ON; \n\
33 \n\
34CREATE TABLE \n\
35 SyncNodes( \n\
36 device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080037 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -080038 description TEXT, \n\
39 seq_no INTEGER NOT NULL, \n\
40 last_known_tdi TEXT, \n\
41 last_update TIMESTAMP \n\
42 ); \n\
43 \n\
44CREATE TRIGGER SyncNodesUpdater_trigger \n\
45 BEFORE INSERT ON SyncNodes \n\
46 FOR EACH ROW \n\
47 WHEN (SELECT device_id \n\
48 FROM SyncNodes \n\
49 WHERE device_name=NEW.device_name) \n\
50 IS NOT NULL \n\
51 BEGIN \n\
52 UPDATE SyncNodes \n\
53 SET seq_no=max(seq_no,NEW.seq_no) \n\
54 WHERE device_name=NEW.device_name; \n\
55 SELECT RAISE(IGNORE); \n\
56 END; \n\
57 \n\
58CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
59 \n\
60CREATE TABLE SyncLog( \n\
61 state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
62 state_hash BLOB NOT NULL UNIQUE, \n\
63 last_update TIMESTAMP NOT NULL \n\
64 ); \n\
65 \n\
66CREATE TABLE \n\
67 SyncStateNodes( \n\
68 id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
69 state_id INTEGER NOT NULL \n\
70 REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
71 device_id INTEGER NOT NULL \n\
72 REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
73 seq_no INTEGER NOT NULL \n\
74 ); \n\
75 \n\
76CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
77CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
78CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
79 \n\
80CREATE TRIGGER SyncLogGuard_trigger \n\
81 BEFORE INSERT ON SyncLog \n\
82 FOR EACH ROW \n\
83 WHEN (SELECT state_hash \n\
84 FROM SyncLog \n\
85 WHERE state_hash=NEW.state_hash) \n\
86 IS NOT NULL \n\
87 BEGIN \n\
88 DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
89 END; \n\
90 \n\
91CREATE TABLE ActionLog ( \n\
92 device_id INTEGER NOT NULL, \n\
93 seq_no INTEGER NOT NULL, \n\
94 \n\
95 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
96 filename TEXT NOT NULL, \n\
97 \n\
98 version INTEGER NOT NULL, \n\
99 action_timestamp TIMESTAMP NOT NULL, \n\
100 \n\
101 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
102 file_atime TIMESTAMP, \n\
103 file_mtime TIMESTAMP, \n\
104 file_ctime TIMESTAMP, \n\
105 file_chmod INTEGER, \n\
106 \n\
107 parent_device_id INTEGER, \n\
108 parent_seq_no INTEGER, \n\
109 \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800110 action_name TEXT, \n\
111 action_content_object BLOB, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800112 \n\
113 PRIMARY KEY (device_id, seq_no), \n\
114 \n\
115 FOREIGN KEY (parent_device_id, parent_seq_no) \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800116 REFERENCES ActionLog (device_id, seq_no) \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800117 ON UPDATE RESTRICT \n\
118 ON DELETE SET NULL \n\
119); \n\
120 \n\
121CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
122CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
123CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800124 \n\
125CREATE TRIGGER ActionLogInsert_trigger \n\
126 AFTER INSERT ON ActionLog \n\
127 FOR EACH ROW \n\
128 WHEN (SELECT device_id \n\
129 FROM ActionLog \n\
130 WHERE filename=NEW.filename AND \n\
131 version > NEW.version) IS NULL AND \n\
132 (SELECT a.device_id \n\
133 FROM ActionLog a \n\
134 LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
135 WHERE filename=NEW.filename AND \n\
136 version = NEW.version AND \n\
137 a.device_id != NEW.device_id AND \n\
138 s.device_name > (SELECT device_name \n\
139 FROM SyncNodes \n\
140 WHERE device_id=NEW.device_id)) IS NULL \n\
141 BEGIN \n\
142 SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800143 NEW.device_id, NEW.seq_no, \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800144 NEW.action,NEW.filename,NEW.file_hash, \
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800145 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800146 NEW.file_chmod); /* function that applies action and adds record the FileState */ \n \
147 END; \n\
148 \n\
149CREATE TABLE FileState ( \n\
150 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
151 filename TEXT NOT NULL, \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800152 device_id INTEGER NOT NULL, \n\
153 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800154 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
155 file_atime TIMESTAMP, \n\
156 file_mtime TIMESTAMP, \n\
157 file_ctime TIMESTAMP, \n\
158 file_chmod INTEGER, \n\
159 \n\
160 PRIMARY KEY (type, filename) \n\
161); \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800162 \n\
163CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800164";
165
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800166DbHelper::DbHelper (const fs::path &path)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800167{
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800168 fs::path chronoshareDirectory = path / ".chronoshare";
169 fs::create_directories (chronoshareDirectory);
170
171 int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800172 if (res != SQLITE_OK)
173 {
174 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800175 << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]"));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800176 }
177
178 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800179 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800180 if (res != SQLITE_OK)
181 {
182 BOOST_THROW_EXCEPTION (Error::Db ()
183 << errmsg_info_str ("Cannot create function ``hash''"));
184 }
185
186 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
187 // for now, just attempt to create everything
188
189 char *errmsg = 0;
190 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800191 if (res != SQLITE_OK && errmsg != 0)
192 {
193 std::cerr << "DEBUG: " << errmsg << std::endl;
194 sqlite3_free (errmsg);
195 }
196}
197
198DbHelper::~DbHelper ()
199{
200 int res = sqlite3_close (m_db);
201 if (res != SQLITE_OK)
202 {
203 // complain
204 }
205}
206
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800207void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800208DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
209{
210 if (argc != 2)
211 {
212 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
213 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
214 return;
215 }
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800216 if (sqlite3_value_type (argv[0]) != SQLITE_BLOB ||
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800217 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
218 {
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800219 // _LOG_ERROR ("Hash expects (blob,integer) parameters");
220 sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800221 return;
222 }
223
224 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
225
226 if (hash_context == 0)
227 {
228 sqlite3_result_error_nomem (context);
229 return;
230 }
231
232 if (*hash_context == 0)
233 {
234 *hash_context = EVP_MD_CTX_create ();
235 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
236 }
237
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800238 int nameBytes = sqlite3_value_bytes (argv[0]);
239 const void *name = sqlite3_value_blob (argv[0]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800240 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
241
242 EVP_DigestUpdate (*hash_context, name, nameBytes);
243 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
244}
245
246void
247DbHelper::hash_xFinal (sqlite3_context *context)
248{
249 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
250
251 if (hash_context == 0)
252 {
253 sqlite3_result_error_nomem (context);
254 return;
255 }
256
257 if (*hash_context == 0) // no rows
258 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800259 char charNullResult = 0;
260 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800261 return;
262 }
263
264 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
265 unsigned int hashLength = 0;
266
267 int ok = EVP_DigestFinal_ex (*hash_context,
268 hash, &hashLength);
269
270 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
271 delete [] hash;
272
273 EVP_MD_CTX_destroy (*hash_context);
274}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800275
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800276
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800277