blob: 82feb890e7de84188a082d6a18a3451a7fa87f00 [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\
Zhenkai Zhue851b952013-01-13 22:29:57 -080040 last_known_locator BLOB, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -080041 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\
Zhenkai Zhub777a4d2013-01-18 09:40:46 -0800106 file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800107 \n\
108 parent_device_id INTEGER, \n\
109 parent_seq_no INTEGER, \n\
110 \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800111 action_name TEXT, \n\
112 action_content_object BLOB, \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800113 \n\
114 PRIMARY KEY (device_id, seq_no), \n\
115 \n\
116 FOREIGN KEY (parent_device_id, parent_seq_no) \n\
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800117 REFERENCES ActionLog (device_id, seq_no) \n\
Alexander Afanasyev8811b352013-01-02 12:51:15 -0800118 ON UPDATE RESTRICT \n\
119 ON DELETE SET NULL \n\
120); \n\
121 \n\
122CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
123CREATE INDEX ActionLog_parent ON ActionLog (parent_device_id, parent_seq_no); \n\
124CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800125 \n\
126CREATE TRIGGER ActionLogInsert_trigger \n\
127 AFTER INSERT ON ActionLog \n\
128 FOR EACH ROW \n\
129 WHEN (SELECT device_id \n\
130 FROM ActionLog \n\
131 WHERE filename=NEW.filename AND \n\
132 version > NEW.version) IS NULL AND \n\
133 (SELECT a.device_id \n\
134 FROM ActionLog a \n\
135 LEFT JOIN SyncNodes s ON s.device_id=a.device_id \n\
136 WHERE filename=NEW.filename AND \n\
137 version = NEW.version AND \n\
138 a.device_id != NEW.device_id AND \n\
139 s.device_name > (SELECT device_name \n\
140 FROM SyncNodes \n\
141 WHERE device_id=NEW.device_id)) IS NULL \n\
142 BEGIN \n\
143 SELECT apply_action ((SELECT device_name FROM SyncNodes where device_id=NEW.device_id), \
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800144 NEW.device_id, NEW.seq_no, \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800145 NEW.action,NEW.filename,NEW.file_hash, \
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800146 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800147 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800148 END; \n\
149 \n\
150CREATE TABLE FileState ( \n\
151 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
152 filename TEXT NOT NULL, \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800153 device_id INTEGER NOT NULL, \n\
154 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800155 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
156 file_atime TIMESTAMP, \n\
157 file_mtime TIMESTAMP, \n\
158 file_ctime TIMESTAMP, \n\
159 file_chmod INTEGER, \n\
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800160 file_seg_num INTEGER, \n\
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800161 \n\
162 PRIMARY KEY (type, filename) \n\
163); \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800164 \n\
165CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800166";
167
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800168DbHelper::DbHelper (const fs::path &path)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800169{
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800170 fs::path chronoshareDirectory = path / ".chronoshare";
171 fs::create_directories (chronoshareDirectory);
172
173 int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800174 if (res != SQLITE_OK)
175 {
176 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800177 << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]"));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800178 }
179
180 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800181 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800182 if (res != SQLITE_OK)
183 {
184 BOOST_THROW_EXCEPTION (Error::Db ()
185 << errmsg_info_str ("Cannot create function ``hash''"));
186 }
187
188 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
189 // for now, just attempt to create everything
190
191 char *errmsg = 0;
192 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800193 if (res != SQLITE_OK && errmsg != 0)
194 {
195 std::cerr << "DEBUG: " << errmsg << std::endl;
196 sqlite3_free (errmsg);
197 }
198}
199
200DbHelper::~DbHelper ()
201{
202 int res = sqlite3_close (m_db);
203 if (res != SQLITE_OK)
204 {
205 // complain
206 }
207}
208
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800209void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800210DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
211{
212 if (argc != 2)
213 {
214 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
215 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
216 return;
217 }
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800218 if (sqlite3_value_type (argv[0]) != SQLITE_BLOB ||
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800219 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
220 {
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800221 // _LOG_ERROR ("Hash expects (blob,integer) parameters");
222 sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800223 return;
224 }
225
226 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
227
228 if (hash_context == 0)
229 {
230 sqlite3_result_error_nomem (context);
231 return;
232 }
233
234 if (*hash_context == 0)
235 {
236 *hash_context = EVP_MD_CTX_create ();
237 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
238 }
239
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800240 int nameBytes = sqlite3_value_bytes (argv[0]);
241 const void *name = sqlite3_value_blob (argv[0]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800242 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
243
244 EVP_DigestUpdate (*hash_context, name, nameBytes);
245 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
246}
247
248void
249DbHelper::hash_xFinal (sqlite3_context *context)
250{
251 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
252
253 if (hash_context == 0)
254 {
255 sqlite3_result_error_nomem (context);
256 return;
257 }
258
259 if (*hash_context == 0) // no rows
260 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800261 char charNullResult = 0;
262 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800263 return;
264 }
265
266 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
267 unsigned int hashLength = 0;
268
269 int ok = EVP_DigestFinal_ex (*hash_context,
270 hash, &hashLength);
271
272 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
273 delete [] hash;
274
275 EVP_MD_CTX_destroy (*hash_context);
276}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800277
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800278
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800279