blob: 9c8f50da10ef174aea9b6d0d829dae014db7c357 [file] [log] [blame]
Alexander Afanasyeva199f972013-01-02 19:37:26 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012-2013 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
22#include "action-log.h"
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080023#include <action-item.pb.h>
Alexander Afanasyeva199f972013-01-02 19:37:26 -080024
25using namespace boost;
26using namespace std;
27
28ActionLog::ActionLog (const std::string &path, const std::string &localName)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080029 : SyncLog (path, localName)
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030{
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080031 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
32 ActionLog::apply_action_xFun,
33 0, 0);
34 if (res != SQLITE_OK)
35 {
36 BOOST_THROW_EXCEPTION (Error::Db ()
37 << errmsg_info_str ("Cannot create function ``apply_action''"));
38 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -080039}
40
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080041tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080042ActionLog::GetExistingRecord (const std::string &filename)
43{
44 // check if something already exists
45 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080046 int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
47 "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080048 "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080049
50 if (res != SQLITE_OK)
51 {
52 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080053 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080054 }
55
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080056 // with parent with version number + 1
57 sqlite3_int64 version = 0;
58 sqlite3_int64 parent_device_id = -1;
59 sqlite3_int64 parent_seq_no = -1;
60 string parent_device_name;
61
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080062 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080063 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080064 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080065 version = sqlite3_column_int64 (stmt, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080066
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080067 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
68 {
69 parent_device_id = sqlite3_column_int64 (stmt, 1);
70 parent_seq_no = sqlite3_column_int64 (stmt, 2);
71 parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_text (stmt, 4)));
72 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080073 }
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080074
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080075 sqlite3_finalize (stmt);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080076 return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080077}
78
Alexander Afanasyeva199f972013-01-02 19:37:26 -080079// local add action. remote action is extracted from content object
80void
81ActionLog::AddActionUpdate (const std::string &filename,
82 const Hash &hash,
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080083 time_t atime, time_t mtime, time_t ctime,
Alexander Afanasyeva199f972013-01-02 19:37:26 -080084 int mode)
85{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080086 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080087
88 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
89 sqlite3_int64 version = 0;
90 sqlite3_int64 parent_device_id = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080091 string parent_device_name;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080092 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080093
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080094 sqlite3_int64 action_time = time (0);
95
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080096 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080097
98 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080099 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
100 "(device_id, seq_no, action, filename, version, action_timestamp, "
101 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
102 "parent_device_id, parent_seq_no) "
103 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
104 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
105 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800106
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800107 // cout << "INSERT INTO ActionLog "
108 // "(device_id, seq_no, action, filename, version, action_timestamp, "
109 // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
110 // "parent_device_id, parent_seq_no) "
111 // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
112 // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
113 // " ?, ?)" << endl;
114
115 if (res != SQLITE_OK)
116 {
117 BOOST_THROW_EXCEPTION (Error::Db ()
118 << errmsg_info_str (sqlite3_errmsg (m_db))
119 );
120 // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
121 }
122
123
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800124 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
125 sqlite3_bind_int64 (stmt, 2, seq_no);
126 sqlite3_bind_int (stmt, 3, 0);
127 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800128 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800129 sqlite3_bind_int64 (stmt, 6, action_time);
130
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800131 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
132
133 sqlite3_bind_int64 (stmt, 8, atime);
134 sqlite3_bind_int64 (stmt, 9, mtime);
135 sqlite3_bind_int64 (stmt, 10, ctime);
136 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800137
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800138 if (parent_device_id > 0 && parent_seq_no > 0)
139 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800140 sqlite3_bind_int64 (stmt, 12, parent_device_id);
141 sqlite3_bind_int64 (stmt, 13, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800142 }
143 else
144 {
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800145 sqlite3_bind_null (stmt, 12);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800146 sqlite3_bind_null (stmt, 13);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800147 }
148
149 sqlite3_step (stmt);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800151 // missing part: creating ContentObject for the action !!!
152
153 ActionItem item;
154 item.set_action (ActionItem::UPDATE);
155 item.set_filename (filename);
156 item.set_version (version);
157 item.set_timestamp (action_time);
158 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800159 item.set_atime (atime);
160 item.set_mtime (mtime);
161 item.set_ctime (ctime);
162 item.set_mode (mode);
163
164 if (parent_device_id > 0 && parent_seq_no > 0)
165 {
166 item.set_parent_device_name (parent_device_name);
167 item.set_parent_seq_no (parent_seq_no);
168 }
169
170 // assign name to the action, serialize action, and create content object
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800171
172 sqlite3_finalize (stmt);
173
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800174 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800175}
176
177void
178ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
179{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800180 // not supported yet
181 BOOST_THROW_EXCEPTION (Error::Db ()
182 << errmsg_info_str ("Move operation is not yet supported"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800183}
184
185void
186ActionLog::AddActionDelete (const std::string &filename)
187{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800188 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
189
190 sqlite3_int64 version = 0;
191 sqlite3_int64 parent_device_id = -1;
192 string parent_device_name;
193 sqlite3_int64 parent_seq_no = -1;
194
195 sqlite3_int64 action_time = time (0);
196
197 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
198 if (parent_device_id < 0) // no records exist or file was already deleted
199 {
200 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
201 return;
202 }
203
204 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
205
206 sqlite3_stmt *stmt;
207 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
208 "(device_id, seq_no, action, filename, version, action_timestamp, "
209 "parent_device_id, parent_seq_no) "
210 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
211 " ?, ?)", -1, &stmt, 0);
212
213 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
214 sqlite3_bind_int64 (stmt, 2, seq_no);
215 sqlite3_bind_int (stmt, 3, 1);
216 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
217 sqlite3_bind_int64 (stmt, 5, version);
218 sqlite3_bind_int64 (stmt, 6, action_time);
219
220 sqlite3_bind_int64 (stmt, 7, parent_device_id);
221 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
222
223 sqlite3_step (stmt);
224
225 // missing part: creating ContentObject for the action !!!
226
227 ActionItem item;
228 item.set_action (ActionItem::UPDATE);
229 item.set_filename (filename);
230 item.set_version (version);
231 item.set_timestamp (action_time);
232 item.set_parent_device_name (parent_device_name);
233 item.set_parent_seq_no (parent_seq_no);
234
235 // assign name to the action, serialize action, and create content object
236
237 sqlite3_finalize (stmt);
238
239 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800240}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800241
242
243void
244ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
245{
246 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
247
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800248 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800249 {
250 sqlite3_result_error (context, "``apply_action'' expects 8 arguments", -1);
251 return;
252 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800253
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800254 // cout << "apply_function called with " << argc << endl;
255
256 // cout << "device_name: " << sqlite3_value_text (argv[0]) << endl;
257 // cout << "action: " << sqlite3_value_int (argv[1]) << endl;
258 // cout << "filename: " << sqlite3_value_text (argv[2]) << endl;
259
260 string device_name = reinterpret_cast<const char*> (sqlite3_value_text (argv[0]));
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800261 sqlite3_int64 device_id = sqlite3_value_int64 (argv[1]);
262 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[2]);
263 int action = sqlite3_value_int (argv[3]);
264 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[4]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800265
266 if (action == 0) // update
267 {
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800268 Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5]));
269 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
270 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
271 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8]));
272 int mode = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800273
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800274 cout << "Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800275
276 sqlite3_stmt *stmt;
277 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800278 "SET "
279 "device_id=?, seq_no=?, "
280 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800281 "file_atime=datetime(?, 'unixepoch'),"
282 "file_mtime=datetime(?, 'unixepoch'),"
283 "file_ctime=datetime(?, 'unixepoch'),"
284 "file_chmod=? "
285 "WHERE type=0 AND filename=?", -1, &stmt, 0);
286
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800287 sqlite3_bind_int64 (stmt, 1, device_id);
288 sqlite3_bind_int64 (stmt, 2, seq_no);
289 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
290 sqlite3_bind_int64 (stmt, 4, atime);
291 sqlite3_bind_int64 (stmt, 5, mtime);
292 sqlite3_bind_int64 (stmt, 6, ctime);
293 sqlite3_bind_int (stmt, 7, mode);
294 sqlite3_bind_text (stmt, 8, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800295
296 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800297
298 // cout << sqlite3_errmsg (the->m_db) << endl;
299
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800300 sqlite3_finalize (stmt);
301
302 int affected_rows = sqlite3_changes (the->m_db);
303 if (affected_rows == 0) // file didn't exist
304 {
305 sqlite3_stmt *stmt;
306 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800307 "(type,filename,device_id,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod) "
308 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800309 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", -1, &stmt, 0);
310
311 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800312 sqlite3_bind_int64 (stmt, 2, device_id);
313 sqlite3_bind_int64 (stmt, 3, seq_no);
314 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
315 sqlite3_bind_int64 (stmt, 5, atime);
316 sqlite3_bind_int64 (stmt, 6, mtime);
317 sqlite3_bind_int64 (stmt, 7, ctime);
318 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800319
320 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800321 // cout << sqlite3_errmsg (the->m_db) << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800322 sqlite3_finalize (stmt);
323 }
324 }
325 else if (action == 1) // delete
326 {
327 sqlite3_stmt *stmt;
328 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
329 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
330
331 cout << "Delete " << filename << endl;
332
333 sqlite3_step (stmt);
334 sqlite3_finalize (stmt);
335 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800336
337 sqlite3_result_null (context);
338}