blob: a9cf576fb4ed90a4b19aec1265332673e4d5c558 [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 Afanasyev71b43e72012-12-27 01:03:43 -080029
30const std::string INIT_DATABASE = "\
Alexander Afanasyev8811b352013-01-02 12:51:15 -080031PRAGMA foreign_keys = ON; \n\
32 \n\
33CREATE TABLE \n\
34 SyncNodes( \n\
35 device_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
36 device_name TEXT NOT NULL, \n\
37 description TEXT, \n\
38 seq_no INTEGER NOT NULL, \n\
39 last_known_tdi TEXT, \n\
40 last_update TIMESTAMP \n\
41 ); \n\
42 \n\
43CREATE TRIGGER SyncNodesUpdater_trigger \n\
44 BEFORE INSERT ON SyncNodes \n\
45 FOR EACH ROW \n\
46 WHEN (SELECT device_id \n\
47 FROM SyncNodes \n\
48 WHERE device_name=NEW.device_name) \n\
49 IS NOT NULL \n\
50 BEGIN \n\
51 UPDATE SyncNodes \n\
52 SET seq_no=max(seq_no,NEW.seq_no) \n\
53 WHERE device_name=NEW.device_name; \n\
54 SELECT RAISE(IGNORE); \n\
55 END; \n\
56 \n\
57CREATE INDEX SyncNodes_device_name ON SyncNodes (device_name); \n\
58 \n\
59CREATE TABLE SyncLog( \n\
60 state_id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
61 state_hash BLOB NOT NULL UNIQUE, \n\
62 last_update TIMESTAMP NOT NULL \n\
63 ); \n\
64 \n\
65CREATE TABLE \n\
66 SyncStateNodes( \n\
67 id INTEGER PRIMARY KEY AUTOINCREMENT, \n\
68 state_id INTEGER NOT NULL \n\
69 REFERENCES SyncLog (state_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
70 device_id INTEGER NOT NULL \n\
71 REFERENCES SyncNodes (device_id) ON UPDATE CASCADE ON DELETE CASCADE, \n\
72 seq_no INTEGER NOT NULL \n\
73 ); \n\
74 \n\
75CREATE INDEX SyncStateNodes_device_id ON SyncStateNodes (device_id); \n\
76CREATE INDEX SyncStateNodes_state_id ON SyncStateNodes (state_id); \n\
77CREATE INDEX SyncStateNodes_seq_no ON SyncStateNodes (seq_no); \n\
78 \n\
79CREATE TRIGGER SyncLogGuard_trigger \n\
80 BEFORE INSERT ON SyncLog \n\
81 FOR EACH ROW \n\
82 WHEN (SELECT state_hash \n\
83 FROM SyncLog \n\
84 WHERE state_hash=NEW.state_hash) \n\
85 IS NOT NULL \n\
86 BEGIN \n\
87 DELETE FROM SyncLog WHERE state_hash=NEW.state_hash; \n\
88 END; \n\
89 \n\
90CREATE TABLE ActionLog ( \n\
91 device_id INTEGER NOT NULL, \n\
92 seq_no INTEGER NOT NULL, \n\
93 \n\
94 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
95 filename TEXT NOT NULL, \n\
96 \n\
97 version INTEGER NOT NULL, \n\
98 action_timestamp TIMESTAMP NOT NULL, \n\
99 \n\
100 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
101 file_atime TIMESTAMP, \n\
102 file_mtime TIMESTAMP, \n\
103 file_ctime TIMESTAMP, \n\
104 file_chmod INTEGER, \n\
105 \n\
106 parent_device_id INTEGER, \n\
107 parent_seq_no INTEGER, \n\
108 \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800109 action_name TEXT, \n\
110 action_content_object BLOB, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800111 \n\
112 PRIMARY KEY (device_id, seq_no), \n\
113 \n\
114 FOREIGN KEY (parent_device_id, parent_seq_no) \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800115 REFERENCES ActionLog (device_id, seq_no) \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800116 ON UPDATE RESTRICT \n\
117 ON DELETE SET NULL \n\
118); \n\
119 \n\
120CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
121CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
122CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800123 \n\
124CREATE TRIGGER ActionLogInsert_trigger \n\
125 AFTER INSERT ON ActionLog \n\
126 FOR EACH ROW \n\
127 WHEN (SELECT device_id \n\
128 FROM ActionLog \n\
129 WHERE filename=NEW.filename AND \n\
130 version > NEW.version) IS NULL AND \n\
131 (SELECT a.device_id \n\
132 FROM ActionLog a \n\
133 LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
134 WHERE filename=NEW.filename AND \n\
135 version = NEW.version AND \n\
136 a.device_id != NEW.device_id AND \n\
137 s.device_name > (SELECT device_name \n\
138 FROM SyncNodes \n\
139 WHERE device_id=NEW.device_id)) IS NULL \n\
140 BEGIN \n\
141 SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800142 NEW.device_id, NEW.seq_no, \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800143 NEW.action,NEW.filename,NEW.file_hash, \
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800144 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800145 NEW.file_chmod); /* function that applies action and adds record the FileState */ \n \
146 END; \n\
147 \n\
148CREATE TABLE FileState ( \n\
149 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
150 filename TEXT NOT NULL, \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800151 device_id INTEGER NOT NULL, \n\
152 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800153 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
154 file_atime TIMESTAMP, \n\
155 file_mtime TIMESTAMP, \n\
156 file_ctime TIMESTAMP, \n\
157 file_chmod INTEGER, \n\
158 \n\
159 PRIMARY KEY (type, filename) \n\
160); \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800161 \n\
162CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800163";
164
165DbHelper::DbHelper (const std::string &path)
166{
167 int res = sqlite3_open((path+"chronoshare.db").c_str (), &m_db);
168 if (res != SQLITE_OK)
169 {
170 BOOST_THROW_EXCEPTION (Error::Db ()
171 << errmsg_info_str ("Cannot open/create dabatabase: [" + path + "chronoshare.db" + "]"));
172 }
173
174 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800175 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800176 if (res != SQLITE_OK)
177 {
178 BOOST_THROW_EXCEPTION (Error::Db ()
179 << errmsg_info_str ("Cannot create function ``hash''"));
180 }
181
182 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
183 // for now, just attempt to create everything
184
185 char *errmsg = 0;
186 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800187 if (res != SQLITE_OK && errmsg != 0)
188 {
189 std::cerr << "DEBUG: " << errmsg << std::endl;
190 sqlite3_free (errmsg);
191 }
192}
193
194DbHelper::~DbHelper ()
195{
196 int res = sqlite3_close (m_db);
197 if (res != SQLITE_OK)
198 {
199 // complain
200 }
201}
202
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800203void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800204DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
205{
206 if (argc != 2)
207 {
208 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
209 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
210 return;
211 }
212 if (sqlite3_value_type (argv[0]) != SQLITE_TEXT ||
213 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
214 {
215 // _LOG_ERROR ("Hash expects (text,integer) parameters");
216 sqlite3_result_error (context, "Hash expects (text,integer) parameters", -1);
217 return;
218 }
219
220 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
221
222 if (hash_context == 0)
223 {
224 sqlite3_result_error_nomem (context);
225 return;
226 }
227
228 if (*hash_context == 0)
229 {
230 *hash_context = EVP_MD_CTX_create ();
231 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
232 }
233
234 int nameBytes = sqlite3_value_bytes (argv[0]);
235 const unsigned char *name = sqlite3_value_text (argv[0]);
236 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
237
238 EVP_DigestUpdate (*hash_context, name, nameBytes);
239 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
240}
241
242void
243DbHelper::hash_xFinal (sqlite3_context *context)
244{
245 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
246
247 if (hash_context == 0)
248 {
249 sqlite3_result_error_nomem (context);
250 return;
251 }
252
253 if (*hash_context == 0) // no rows
254 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800255 char charNullResult = 0;
256 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800257 return;
258 }
259
260 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
261 unsigned int hashLength = 0;
262
263 int ok = EVP_DigestFinal_ex (*hash_context,
264 hash, &hashLength);
265
266 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
267 delete [] hash;
268
269 EVP_MD_CTX_destroy (*hash_context);
270}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800271
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800272
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800273