blob: 4313dfdb0b52bd03e6ad6e5ff4b38a316433ba23 [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 Afanasyevee7e6132013-01-03 20:03:14 -0800147 NEW.file_chmod); /* function that applies action and adds record the FileState */ \n \
148 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\
160 \n\
161 PRIMARY KEY (type, filename) \n\
162); \n\
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800163 \n\
164CREATE INDEX FileState_device_id_seq_no ON FileState (device_id, seq_no); \n\
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800165";
166
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800167DbHelper::DbHelper (const fs::path &path)
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800168{
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800169 fs::path chronoshareDirectory = path / ".chronoshare";
170 fs::create_directories (chronoshareDirectory);
171
172 int res = sqlite3_open((chronoshareDirectory / "state.db").c_str (), &m_db);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800173 if (res != SQLITE_OK)
174 {
175 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800176 << errmsg_info_str ("Cannot open/create dabatabase: [" + (chronoshareDirectory / "state.db").string () + "]"));
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800177 }
178
179 res = sqlite3_create_function (m_db, "hash", 2, SQLITE_ANY, 0, 0,
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800180 DbHelper::hash_xStep, DbHelper::hash_xFinal);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800181 if (res != SQLITE_OK)
182 {
183 BOOST_THROW_EXCEPTION (Error::Db ()
184 << errmsg_info_str ("Cannot create function ``hash''"));
185 }
186
187 // Alex: determine if tables initialized. if not, initialize... not sure what is the best way to go...
188 // for now, just attempt to create everything
189
190 char *errmsg = 0;
191 res = sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, &errmsg);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800192 if (res != SQLITE_OK && errmsg != 0)
193 {
194 std::cerr << "DEBUG: " << errmsg << std::endl;
195 sqlite3_free (errmsg);
196 }
197}
198
199DbHelper::~DbHelper ()
200{
201 int res = sqlite3_close (m_db);
202 if (res != SQLITE_OK)
203 {
204 // complain
205 }
206}
207
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800208void
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800209DbHelper::hash_xStep (sqlite3_context *context, int argc, sqlite3_value **argv)
210{
211 if (argc != 2)
212 {
213 // _LOG_ERROR ("Wrong arguments are supplied for ``hash'' function");
214 sqlite3_result_error (context, "Wrong arguments are supplied for ``hash'' function", -1);
215 return;
216 }
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800217 if (sqlite3_value_type (argv[0]) != SQLITE_BLOB ||
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800218 sqlite3_value_type (argv[1]) != SQLITE_INTEGER)
219 {
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800220 // _LOG_ERROR ("Hash expects (blob,integer) parameters");
221 sqlite3_result_error (context, "Hash expects (blob,integer) parameters", -1);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800222 return;
223 }
224
225 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
226
227 if (hash_context == 0)
228 {
229 sqlite3_result_error_nomem (context);
230 return;
231 }
232
233 if (*hash_context == 0)
234 {
235 *hash_context = EVP_MD_CTX_create ();
236 EVP_DigestInit_ex (*hash_context, HASH_FUNCTION (), 0);
237 }
238
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800239 int nameBytes = sqlite3_value_bytes (argv[0]);
240 const void *name = sqlite3_value_blob (argv[0]);
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800241 sqlite3_int64 seqno = sqlite3_value_int64 (argv[1]);
242
243 EVP_DigestUpdate (*hash_context, name, nameBytes);
244 EVP_DigestUpdate (*hash_context, &seqno, sizeof(sqlite3_int64));
245}
246
247void
248DbHelper::hash_xFinal (sqlite3_context *context)
249{
250 EVP_MD_CTX **hash_context = reinterpret_cast<EVP_MD_CTX **> (sqlite3_aggregate_context (context, sizeof (EVP_MD_CTX *)));
251
252 if (hash_context == 0)
253 {
254 sqlite3_result_error_nomem (context);
255 return;
256 }
257
258 if (*hash_context == 0) // no rows
259 {
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800260 char charNullResult = 0;
261 sqlite3_result_blob (context, &charNullResult, 1, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
Alexander Afanasyev71b43e72012-12-27 01:03:43 -0800262 return;
263 }
264
265 unsigned char *hash = new unsigned char [EVP_MAX_MD_SIZE];
266 unsigned int hashLength = 0;
267
268 int ok = EVP_DigestFinal_ex (*hash_context,
269 hash, &hashLength);
270
271 sqlite3_result_blob (context, hash, hashLength, SQLITE_TRANSIENT); //SQLITE_TRANSIENT forces to make a copy
272 delete [] hash;
273
274 EVP_MD_CTX_destroy (*hash_context);
275}
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800276
Alexander Afanasyevde1cdd02012-12-29 14:41:46 -0800277
Alexander Afanasyevae43c502012-12-29 17:26:37 -0800278