blob: 7851c4c93ed4bf31cd62fefc14ef71dee4080964 [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 Afanasyev8e2104a2013-01-22 10:56:18 -080023#include "logging.h"
Alexander Afanasyeva199f972013-01-02 19:37:26 -080024
Alexander Afanasyev0995f322013-01-22 13:16:46 -080025#include <boost/make_shared.hpp>
26
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027using namespace boost;
28using namespace std;
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080029using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080030
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080031INIT_LOGGER ("ActionLog");
32
33const std::string INIT_DATABASE = "\
Alexander Afanasyev785b0a02013-01-30 10:17:47 -080034 PRAGMA foreign_keys = OFF; /* foreign key here are very bad idea...*/ \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080035CREATE TABLE ActionLog ( \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080036 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080037 seq_no INTEGER NOT NULL, \n\
38 \n\
39 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
40 filename TEXT NOT NULL, \n\
41 \n\
42 version INTEGER NOT NULL, \n\
43 action_timestamp TIMESTAMP NOT NULL, \n\
44 \n\
45 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
46 file_atime TIMESTAMP, \n\
47 file_mtime TIMESTAMP, \n\
48 file_ctime TIMESTAMP, \n\
49 file_chmod INTEGER, \n\
50 file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
51 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080052 parent_device_name BLOB, \n\
53 parent_seq_no INTEGER, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080054 \n\
55 action_name TEXT, \n\
56 action_content_object BLOB, \n\
57 \n\
Alexander Afanasyev785b0a02013-01-30 10:17:47 -080058 PRIMARY KEY (device_name, seq_no) \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080059); \n\
60 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080061CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
62CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080063CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
64 \n\
65CREATE TRIGGER ActionLogInsert_trigger \n\
66 AFTER INSERT ON ActionLog \n\
67 FOR EACH ROW \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080068 WHEN (SELECT device_name \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080069 FROM ActionLog \n\
70 WHERE filename=NEW.filename AND \n\
71 version > NEW.version) IS NULL AND \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080072 (SELECT device_name \n\
73 FROM ActionLog \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080074 WHERE filename=NEW.filename AND \n\
75 version = NEW.version AND \n\
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080076 device_name > NEW.device_name) IS NULL \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080077 BEGIN \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080078 SELECT apply_action (NEW.device_name, NEW.seq_no, \
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080079 NEW.action,NEW.filename,NEW.file_hash, \
80 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
81 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
82 END; \n\
83 \n\
84CREATE TABLE FileState ( \n\
85 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
86 filename TEXT NOT NULL, \n\
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080087 directory TEXT, \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080088 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080089 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080090 file_hash BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080091 file_atime TIMESTAMP, \n\
92 file_mtime TIMESTAMP, \n\
93 file_ctime TIMESTAMP, \n\
94 file_chmod INTEGER, \n\
95 file_seg_num INTEGER, \n\
Alexander Afanasyev7a647002013-01-30 11:54:52 -080096 is_complete INTEGER, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080097 \n\
98 PRIMARY KEY (type, filename) \n\
99); \n\
100 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800101CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800102CREATE INDEX FileState_type_file_hash ON FileState (type, file_hash); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800103";
104
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800105static void xTrace (void*, const char* q)
106{
107 _LOG_TRACE ("SQLITE: " << q);
108}
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800109
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800110ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800111 SyncLogPtr syncLog,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800112 const std::string &sharedFolder,
113 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800114 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800115 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800116 , m_ccnx (ccnx)
117 , m_sharedFolderName (sharedFolder)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800118 , m_onFileAddedOrChanged (onFileAddedOrChanged)
119 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800120{
Alexander Afanasyev785b0a02013-01-30 10:17:47 -0800121 sqlite3_exec (m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
122 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
123
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800124 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
125 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
126
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800127 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
128 ActionLog::apply_action_xFun,
129 0, 0);
130 if (res != SQLITE_OK)
131 {
132 BOOST_THROW_EXCEPTION (Error::Db ()
133 << errmsg_info_str ("Cannot create function ``apply_action''"));
134 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800135
136
137 res = sqlite3_create_function (m_db, "directory_name", -1, SQLITE_ANY, 0,
138 ActionLog::directory_name_xFun,
139 0, 0);
140 if (res != SQLITE_OK)
141 {
142 BOOST_THROW_EXCEPTION (Error::Db ()
143 << errmsg_info_str ("Cannot create function ``directory_name''"));
144 }
145
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800146 // // "Upgrading" database
147 // sqlite3_exec (m_db, "ALTER TABLE FileState ADD COLUMN directory TEXT", NULL, NULL, NULL);
148 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800149
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800150 // sqlite3_exec (m_db, "CREATE INDEX FileState_directory ON FileState (directory)", NULL, NULL, NULL);
151 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800152
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800153 // sqlite3_exec (m_db, "UPDATE FileState SET directory = directory_name(filename) WHERE directory IS NULL", NULL, NULL, NULL);
154 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800155
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800156 // // Another "upgrade"
157 // sqlite3_exec (m_db, "BEGIN TRANSACTION; ALTER TABLE FileState ADD COLUMN is_complete INTEGER; UPDATE FileState SET is_complete = 1; END TRANSACTION;", NULL, NULL, NULL);
158 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800159}
160
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800161tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
162ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800163{
164 // check if something already exists
165 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800166 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
167 "FROM ActionLog "
168 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800169
170 if (res != SQLITE_OK)
171 {
172 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800173 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800174 }
175
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800176 sqlite3_int64 version = -1;
177 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800178 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800179
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800180 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800181 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800182 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800183 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800184
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800185 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
186 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800187 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800188 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800189 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800190 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800191
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800192 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800193 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800194}
195
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800196// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800197ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800198ActionLog::AddLocalActionUpdate (const std::string &filename,
199 const Hash &hash,
200 time_t wtime,
201 int mode,
202 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800203{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800204 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800205
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800206 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
207 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
208 sqlite3_int64 version;
209 CcnxCharbufPtr parent_device_name;
210 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800211
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800212 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800213
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800214 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
215 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800216
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800217 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800218 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800219 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800220 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800221 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800222 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800223 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800224 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800225 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800226 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800227
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800228 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
229
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800230 if (res != SQLITE_OK)
231 {
232 BOOST_THROW_EXCEPTION (Error::Db ()
233 << errmsg_info_str (sqlite3_errmsg (m_db))
234 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800235 }
236
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800237 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800238 sqlite3_bind_int64 (stmt, 2, seq_no);
239 sqlite3_bind_int (stmt, 3, 0);
240 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800241 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800242 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800243
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800244 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800245
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800246 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
247 sqlite3_bind_int64 (stmt, 9, wtime);
248 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800249 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800250 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800251
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800252 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800253 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800254 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800255 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800256 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800257
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800258 ActionItemPtr item = make_shared<ActionItem> ();
259 item->set_action (ActionItem::UPDATE);
260 item->set_filename (filename);
261 item->set_version (version);
262 item->set_timestamp (action_time);
263 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
264 // item->set_atime (atime);
265 item->set_mtime (wtime);
266 // item->set_ctime (ctime);
267 item->set_mode (mode);
268 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800269
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800270 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800271 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800272 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800273
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800274 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
275 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800276 }
277
278 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800279
280 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800281 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800282 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800283 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800284
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800285 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
286 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800287
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800288 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800289
290 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
291 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800292
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800293 sqlite3_step (stmt);
294
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800295 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800296
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800297 sqlite3_finalize (stmt);
298
Zhenkai Zhu888be472013-02-01 14:25:33 -0800299 // set complete for local file
300 SetFileComplete(filename);
301
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800302 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800303
304 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800305}
306
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800307// void
308// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
309// {
310// // not supported yet
311// BOOST_THROW_EXCEPTION (Error::Db ()
312// << errmsg_info_str ("Move operation is not yet supported"));
313// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800314
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800315ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800316ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800317{
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800318 _LOG_DEBUG ("Adding local action DELETE");
319
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800320 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800321
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800322 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
323 sqlite3_int64 version;
324 CcnxCharbufPtr parent_device_name;
325 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800326
327 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800328
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800329 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800330 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800331 {
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800332 _LOG_DEBUG ("Nothing to delete... [" << filename << "]");
333
334 // just in case, remove data from FileState
335 sqlite3_stmt *stmt;
336 sqlite3_prepare_v2 (m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0);
337 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
338
339 sqlite3_step (stmt);
340
341 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
342
343 sqlite3_finalize (stmt);
344
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800345 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800346 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800347 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800348 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800349
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800350 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800351
352 sqlite3_stmt *stmt;
353 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800354 "(device_name, seq_no, action, filename, version, action_timestamp, "
355 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800356 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800357 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800358 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800359 " ?, ?)", -1, &stmt, 0);
360
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800361 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800362 sqlite3_bind_int64 (stmt, 2, seq_no);
363 sqlite3_bind_int (stmt, 3, 1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800364 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
365
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800366 sqlite3_bind_int64 (stmt, 5, version);
367 sqlite3_bind_int64 (stmt, 6, action_time);
368
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800369 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800370 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800371
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800372 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800373 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800374 item->set_filename (filename);
375 item->set_version (version);
376 item->set_timestamp (action_time);
377 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
378 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800379
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800380 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800381 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800382 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800383
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800384 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
385 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800386
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800387 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
388 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800389
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800390 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800391
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800392 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800393
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800394 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800395
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800396 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800397
398 sqlite3_finalize (stmt);
399
400 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800401
402 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800403}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800404
405
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800406PcoPtr
407ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
408{
409 sqlite3_stmt *stmt;
410 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
411
412 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
413
414 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
415 sqlite3_bind_int64 (stmt, 2, seqno);
416
417 PcoPtr retval;
418 if (sqlite3_step (stmt) == SQLITE_ROW)
419 {
420 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
421 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
422 }
423 else
424 {
425 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
426 }
427 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
428 sqlite3_finalize (stmt);
429
430 return retval;
431}
432
433ActionItemPtr
434ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
435{
436 PcoPtr pco = LookupActionPco (deviceName, seqno);
437 if (!pco) return ActionItemPtr ();
438
439 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
440
441 return action;
442}
443
444Ccnx::PcoPtr
445ActionLog::LookupActionPco (const Ccnx::Name &actionName)
446{
447 sqlite3_stmt *stmt;
448 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
449
450 _LOG_DEBUG (actionName);
451 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
452
453 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
454
455 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
456
457 PcoPtr retval;
458 if (sqlite3_step (stmt) == SQLITE_ROW)
459 {
460 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
461 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
462 }
463 else
464 {
465 _LOG_TRACE ("No action found for name: " << actionName);
466 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800467 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800468 sqlite3_finalize (stmt);
469
470 return retval;
471}
472
473ActionItemPtr
474ActionLog::LookupAction (const Ccnx::Name &actionName)
475{
476 PcoPtr pco = LookupActionPco (actionName);
477 if (!pco) return ActionItemPtr ();
478
479 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
480
481 return action;
482}
483
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800484ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800485ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
486{
487 if (!actionPco)
488 {
489 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
490 }
491 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
492
493 if (!action)
494 {
495 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
496 }
497
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800498 _LOG_DEBUG ("AddRemoteAction: [" << deviceName << "] seqno: " << seqno);
499
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800500 sqlite3_stmt *stmt;
501 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
502 "(device_name, seq_no, action, filename, version, action_timestamp, "
503 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
504 "parent_device_name, parent_seq_no, "
505 "action_name, action_content_object) "
506 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
507 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
508 " ?, ?, "
509 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyevf695aed2013-01-30 13:28:42 -0800510 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800511
512 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
513 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
514 sqlite3_bind_int64 (stmt, 2, seqno);
515
516 sqlite3_bind_int (stmt, 3, action->action ());
517 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
518 sqlite3_bind_int64 (stmt, 5, action->version ());
519 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
520
521 if (action->action () == ActionItem::UPDATE)
522 {
523 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
524
525 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
526 sqlite3_bind_int64 (stmt, 9, action->mtime ());
527 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
528
529 sqlite3_bind_int (stmt, 11, action->mode ());
530 sqlite3_bind_int (stmt, 12, action->seg_num ());
531 }
532
533 if (action->has_parent_device_name ())
534 {
535 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
536 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
537 }
538
539 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
540 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
541
542 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
543 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
544 sqlite3_step (stmt);
545
546 // if action needs to be applied to file state, the trigger will take care of it
547
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800548 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800549
550 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800551
552 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800553}
554
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800555ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800556ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
557{
558 Name name = actionPco->name ();
559 // <device_name>/"action"/<shared_folder_name_one_component>/<seqno>
560
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800561 uint64_t seqno = name.getCompFromBackAsInt (0);
562 string sharedFolder = name.getCompFromBackAsString (1);
563
564 if (sharedFolder != m_sharedFolderName)
565 {
566 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder"));
567 }
568
569 string action = name.getCompFromBackAsString (2);
570
571 if (action != "action")
572 {
573 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action"));
574 }
575 Name deviceName = name.getPartialName (0, name.size ()-3);
576
577 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
578
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800579 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800580}
581
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800582sqlite3_int64
583ActionLog::LogSize ()
584{
585 sqlite3_stmt *stmt;
586 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
587
588 sqlite3_int64 retval = -1;
589 if (sqlite3_step (stmt) == SQLITE_ROW)
590 {
591 retval = sqlite3_column_int64 (stmt, 0);
592 }
593
594 return retval;
595}
596
597
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800598///////////////////////////////////////////////////////////////////////////////////
599// SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY"
600///////////////////////////////////////////////////////////////////////////////////
601
602void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800603ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
604{
605 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
606
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800607 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800608 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800609 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800610 return;
611 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800612
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800613 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
614 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
615 int action = sqlite3_value_int (argv[2]);
616 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800617
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800618 _LOG_TRACE ("apply_function called with " << argc);
619 _LOG_TRACE ("device_name: " << Name (device_name)
620 << ", action: " << action
621 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800622
623 if (action == 0) // update
624 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800625 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
626 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
627 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
628 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
629 int mode = sqlite3_value_int (argv[8]);
630 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800631
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800632 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800633
634 sqlite3_stmt *stmt;
635 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800636 "SET "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800637 "device_name=?, seq_no=?, "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800638 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800639 "file_atime=datetime(?, 'unixepoch'),"
640 "file_mtime=datetime(?, 'unixepoch'),"
641 "file_ctime=datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800642 "file_chmod=?, "
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800643 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800644 "WHERE type=0 AND filename=?", -1, &stmt, 0);
645
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800646 sqlite3_bind_blob (stmt, 1, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800647 sqlite3_bind_int64 (stmt, 2, seq_no);
648 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
649 sqlite3_bind_int64 (stmt, 4, atime);
650 sqlite3_bind_int64 (stmt, 5, mtime);
651 sqlite3_bind_int64 (stmt, 6, ctime);
652 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800653 sqlite3_bind_int (stmt, 8, seg_num);
654 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800655
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800656 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800657
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800658 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_ROW && sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800659 sqlite3_errmsg (the->m_db));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800660
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800661 sqlite3_finalize (stmt);
662
663 int affected_rows = sqlite3_changes (the->m_db);
664 if (affected_rows == 0) // file didn't exist
665 {
666 sqlite3_stmt *stmt;
667 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800668 "(type,filename,device_name,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod,file_seg_num) "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800669 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800670 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?)", -1, &stmt, 0);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800671
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800672 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK, sqlite3_errmsg (the->m_db));
673
674 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
675 sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_STATIC);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800676 sqlite3_bind_int64 (stmt, 3, seq_no);
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800677 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800678 sqlite3_bind_int64 (stmt, 5, atime);
679 sqlite3_bind_int64 (stmt, 6, mtime);
680 sqlite3_bind_int64 (stmt, 7, ctime);
681 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800682 sqlite3_bind_int (stmt, 9, seg_num);
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800683 // sqlite3_bind_text (stmt, 10, filename.c_str (), -1, SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800684
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800685 sqlite3_step (stmt);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800686 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800687 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800688 sqlite3_finalize (stmt);
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800689
690 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState SET directory=directory_name(filename) WHERE filename=?", -1, &stmt, 0);
691 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK, sqlite3_errmsg (the->m_db));
692
693 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
694 sqlite3_step (stmt);
695 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_DONE,
696 sqlite3_errmsg (the->m_db));
697 sqlite3_finalize (stmt);
698
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800699 }
700 }
701 else if (action == 1) // delete
702 {
703 sqlite3_stmt *stmt;
704 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
705 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
706
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800707 _LOG_DEBUG ("Delete " << filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800708
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800709 sqlite3_step (stmt);
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800710 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_DONE,
711 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800712 sqlite3_finalize (stmt);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800713
714 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800715 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800716
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800717 sqlite3_result_null (context);
718}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800719
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800720
721void
722ActionLog::SetFileComplete (const std::string &filename)
723{
724 sqlite3_stmt *stmt;
725 sqlite3_prepare_v2 (m_db,
726 "UPDATE FileState SET is_complete=1 WHERE type = 0 AND filename = ?", -1, &stmt, 0);
727 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
728 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
729
730 sqlite3_step (stmt);
731 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
732
733 sqlite3_finalize (stmt);
734}
735
736
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800737/**
738 * @todo Implement checking modification time and permissions
739 */
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800740FileItemPtr
741ActionLog::LookupFile (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800742{
743 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800744 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800745 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete "
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800746 " FROM FileState "
747 " WHERE type = 0 AND filename = ?", -1, &stmt, 0);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800748 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800749 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800750
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800751 FileItemPtr retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800752 if (sqlite3_step (stmt) == SQLITE_ROW)
753 {
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800754 retval = make_shared<FileItem> ();
755 retval->set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
756 retval->set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
757 retval->set_seq_no (sqlite3_column_int64 (stmt, 2));
758 retval->set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
759 retval->set_mtime (sqlite3_column_int (stmt, 4));
760 retval->set_mode (sqlite3_column_int (stmt, 5));
761 retval->set_seg_num (sqlite3_column_int64 (stmt, 6));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800762 retval->set_is_complete (sqlite3_column_int (stmt, 7));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800763 }
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800764 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800765 sqlite3_finalize (stmt);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800766
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800767 return retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800768}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800769
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800770FileItemsPtr
771ActionLog::LookupFilesForHash (const Hash &hash)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800772{
773 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800774 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800775 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete "
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800776 " FROM FileState "
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800777 " WHERE type = 0 AND file_hash = ?", -1, &stmt, 0);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800778 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800779 sqlite3_bind_blob(stmt, 1, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800780 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800781
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800782 FileItemsPtr retval = make_shared<FileItems> ();
783 while (sqlite3_step (stmt) == SQLITE_ROW)
784 {
785 FileItem file;
786 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
787 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
788 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
789 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
790 file.set_mtime (sqlite3_column_int (stmt, 4));
791 file.set_mode (sqlite3_column_int (stmt, 5));
792 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800793 file.set_is_complete (sqlite3_column_int (stmt, 7));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800794
795 retval->push_back (file);
796 }
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800797 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800798
799 sqlite3_finalize (stmt);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800800
801 return retval;
802}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800803
804
805FileItemsPtr
806ActionLog::LookupFilesInFolder (const std::string &folder)
807{
808 sqlite3_stmt *stmt;
809 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800810 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800811 " FROM FileState "
812 " WHERE type = 0 AND directory = ?", -1, &stmt, 0);
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800813 if (folder.size () == 0)
814 sqlite3_bind_null (stmt, 1);
815 else
816 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800817
818 FileItemsPtr retval = make_shared<FileItems> ();
819 while (sqlite3_step (stmt) == SQLITE_ROW)
820 {
821 FileItem file;
822 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
823 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
824 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
825 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
826 file.set_mtime (sqlite3_column_int (stmt, 4));
827 file.set_mode (sqlite3_column_int (stmt, 5));
828 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800829 file.set_is_complete (sqlite3_column_int (stmt, 7));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800830
831 retval->push_back (file);
832 }
833
834 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
835
836 sqlite3_finalize (stmt);
837
838 return retval;
839}
840
841FileItemsPtr
842ActionLog::LookupFilesInFolderRecursively (const std::string &folder)
843{
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800844 _LOG_DEBUG ("LookupFilesInFolderRecursively: [" << folder << "]");
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800845
846 sqlite3_stmt *stmt;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800847 if (folder != "")
848 {
849 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800850 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800851 " FROM FileState "
852 " WHERE type = 0 AND (directory = ? OR directory LIKE ?)", -1, &stmt, 0);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800853 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
854
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800855 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800856 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800857
858 ostringstream escapedFolder;
859 for (string::const_iterator ch = folder.begin (); ch != folder.end (); ch ++)
860 {
861 if (*ch == '%')
862 escapedFolder << "\\%";
863 else
864 escapedFolder << *ch;
865 }
866 escapedFolder << "/" << "%";
867 string escapedFolderStr = escapedFolder.str ();
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800868
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800869 sqlite3_bind_text (stmt, 2, escapedFolderStr.c_str (), escapedFolderStr.size (), SQLITE_STATIC);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800870 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800871 }
872 else
873 {
874 sqlite3_prepare_v2 (m_db,
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800875 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,is_complete "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800876 " FROM FileState "
877 " WHERE type = 0", -1, &stmt, 0);
878 }
879
880 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
881
882 FileItemsPtr retval = make_shared<FileItems> ();
883 while (sqlite3_step (stmt) == SQLITE_ROW)
884 {
885 FileItem file;
886 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
887 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
888 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
889 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
890 file.set_mtime (sqlite3_column_int (stmt, 4));
891 file.set_mode (sqlite3_column_int (stmt, 5));
892 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
Alexander Afanasyev7a647002013-01-30 11:54:52 -0800893 file.set_is_complete (sqlite3_column_int (stmt, 7));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800894
895 retval->push_back (file);
896 }
897
898 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
899
900 sqlite3_finalize (stmt);
901
902 return retval;
903}
904
905void
906ActionLog::ActionLog::directory_name_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
907{
908 if (argc != 1)
909 {
910 sqlite3_result_error (context, "``directory_name'' expects 1 text argument", -1);
911 sqlite3_result_null (context);
912 return;
913 }
914
915 if (sqlite3_value_bytes (argv[0]) == 0)
916 {
917 sqlite3_result_null (context);
918 return;
919 }
920
921 filesystem::path filePath (string (reinterpret_cast<const char*> (sqlite3_value_text (argv[0])), sqlite3_value_bytes (argv[0])));
922 string dirPath = filePath.parent_path ().generic_string ();
Alexander Afanasyev83906d32013-01-31 16:32:37 -0800923 // _LOG_DEBUG ("directory_name FUN: " << dirPath);
924 if (dirPath.size () == 0)
925 {
926 sqlite3_result_null (context);
927 }
928 else
929 {
930 sqlite3_result_text (context, dirPath.c_str (), dirPath.size (), SQLITE_TRANSIENT);
931 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800932}