blob: 2bab7ca6c450e5235cc71984332d1e20a9363720 [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 = "\
34CREATE TABLE ActionLog ( \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080035 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080036 seq_no INTEGER NOT NULL, \n\
37 \n\
38 action CHAR(1) NOT NULL, /* 0 for \"update\", 1 for \"delete\". */ \n\
39 filename TEXT NOT NULL, \n\
40 \n\
41 version INTEGER NOT NULL, \n\
42 action_timestamp TIMESTAMP NOT NULL, \n\
43 \n\
44 file_hash BLOB, /* NULL if action is \"delete\" */ \n\
45 file_atime TIMESTAMP, \n\
46 file_mtime TIMESTAMP, \n\
47 file_ctime TIMESTAMP, \n\
48 file_chmod INTEGER, \n\
49 file_seg_num INTEGER, /* NULL if action is \"delete\" */ \n\
50 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080051 parent_device_name BLOB, \n\
52 parent_seq_no INTEGER, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080053 \n\
54 action_name TEXT, \n\
55 action_content_object BLOB, \n\
56 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080057 PRIMARY KEY (device_name, seq_no), \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080058 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080059 FOREIGN KEY (parent_device_name, parent_seq_no) \n\
60 REFERENCES ActionLog (device_name, seq_no) \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080061 ON UPDATE RESTRICT \n\
62 ON DELETE SET NULL \n\
63); \n\
64 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080065CREATE INDEX ActionLog_filename_version ON ActionLog (filename,version); \n\
66CREATE INDEX ActionLog_parent ON ActionLog (parent_device_name, parent_seq_no); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080067CREATE INDEX ActionLog_action_name ON ActionLog (action_name); \n\
68 \n\
69CREATE TRIGGER ActionLogInsert_trigger \n\
70 AFTER INSERT ON ActionLog \n\
71 FOR EACH ROW \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080072 WHEN (SELECT device_name \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080073 FROM ActionLog \n\
74 WHERE filename=NEW.filename AND \n\
75 version > NEW.version) IS NULL AND \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080076 (SELECT device_name \n\
77 FROM ActionLog \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080078 WHERE filename=NEW.filename AND \n\
79 version = NEW.version AND \n\
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080080 device_name > NEW.device_name) IS NULL \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080081 BEGIN \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080082 SELECT apply_action (NEW.device_name, NEW.seq_no, \
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080083 NEW.action,NEW.filename,NEW.file_hash, \
84 strftime('%s', NEW.file_atime),strftime('%s', NEW.file_mtime),strftime('%s', NEW.file_ctime), \
85 NEW.file_chmod, NEW.file_seg_num); /* function that applies action and adds record the FileState */ \n \
86 END; \n\
87 \n\
88CREATE TABLE FileState ( \n\
89 type INTEGER NOT NULL, /* 0 - newest, 1 - oldest */ \n\
90 filename TEXT NOT NULL, \n\
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080091 directory TEXT, \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -080092 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080093 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080094 file_hash BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080095 file_atime TIMESTAMP, \n\
96 file_mtime TIMESTAMP, \n\
97 file_ctime TIMESTAMP, \n\
98 file_chmod INTEGER, \n\
99 file_seg_num INTEGER, \n\
100 \n\
101 PRIMARY KEY (type, filename) \n\
102); \n\
103 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800104CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800105CREATE INDEX FileState_type_file_hash ON FileState (type, file_hash); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800106";
107
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800108static void xTrace (void*, const char* q)
109{
110 _LOG_TRACE ("SQLITE: " << q);
111}
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800112
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800113ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800114 SyncLogPtr syncLog,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800115 const std::string &sharedFolder,
116 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800117 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800118 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800119 , m_ccnx (ccnx)
120 , m_sharedFolderName (sharedFolder)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800121 , m_onFileAddedOrChanged (onFileAddedOrChanged)
122 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800123{
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
146 // "Upgrading" database
147 sqlite3_exec (m_db, "ALTER TABLE FileState ADD COLUMN directory TEXT", NULL, NULL, NULL);
148 sqlite3_exec (m_db, "CREATE INDEX FileState_directory ON FileState (directory)", NULL, NULL, NULL);
149 sqlite3_exec (m_db, "UPDATE FileState SET directory = directory_name(filename) WHERE directory IS NULL", NULL, NULL, NULL);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800150}
151
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800152tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
153ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800154{
155 // check if something already exists
156 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800157 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
158 "FROM ActionLog "
159 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800160
161 if (res != SQLITE_OK)
162 {
163 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800164 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800165 }
166
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800167 sqlite3_int64 version = -1;
168 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800169 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800170
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800171 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800172 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800173 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800174 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800175
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800176 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
177 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800178 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800179 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800180 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800181 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800182
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800183 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800184 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800185}
186
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800187// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800188ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800189ActionLog::AddLocalActionUpdate (const std::string &filename,
190 const Hash &hash,
191 time_t wtime,
192 int mode,
193 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800194{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800195 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800196
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800197 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
198 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
199 sqlite3_int64 version;
200 CcnxCharbufPtr parent_device_name;
201 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800202
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800203 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800204
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800205 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
206 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800207
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800208 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800209 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800210 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800211 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800212 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800213 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800214 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800215 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800216 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800217 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800218
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800219 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
220
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800221 if (res != SQLITE_OK)
222 {
223 BOOST_THROW_EXCEPTION (Error::Db ()
224 << errmsg_info_str (sqlite3_errmsg (m_db))
225 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800226 }
227
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800228 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800229 sqlite3_bind_int64 (stmt, 2, seq_no);
230 sqlite3_bind_int (stmt, 3, 0);
231 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800232 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800233 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800234
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800235 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800236
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800237 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
238 sqlite3_bind_int64 (stmt, 9, wtime);
239 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800240 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800241 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800242
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800243 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800244 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800245 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800246 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800247 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800248
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800249 ActionItemPtr item = make_shared<ActionItem> ();
250 item->set_action (ActionItem::UPDATE);
251 item->set_filename (filename);
252 item->set_version (version);
253 item->set_timestamp (action_time);
254 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
255 // item->set_atime (atime);
256 item->set_mtime (wtime);
257 // item->set_ctime (ctime);
258 item->set_mode (mode);
259 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800260
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800261 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800262 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800263 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800264
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800265 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
266 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800267 }
268
269 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800270
271 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800272 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800273 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800274 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800275
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800276 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
277 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800278
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800279 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800280
281 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
282 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800283
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800284 sqlite3_step (stmt);
285
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800286 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800287
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800288 sqlite3_finalize (stmt);
289
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800290 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800291
292 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800293}
294
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800295// void
296// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
297// {
298// // not supported yet
299// BOOST_THROW_EXCEPTION (Error::Db ()
300// << errmsg_info_str ("Move operation is not yet supported"));
301// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800302
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800303ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800304ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800305{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800306 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800307
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800308 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
309 sqlite3_int64 version;
310 CcnxCharbufPtr parent_device_name;
311 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800312
313 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800314
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800315 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800316 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800317 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800318 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800319 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800320 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800321 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800322
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800323 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800324
325 sqlite3_stmt *stmt;
326 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800327 "(device_name, seq_no, action, filename, version, action_timestamp, "
328 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800329 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800330 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800331 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800332 " ?, ?)", -1, &stmt, 0);
333
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800334 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800335 sqlite3_bind_int64 (stmt, 2, seq_no);
336 sqlite3_bind_int (stmt, 3, 1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800337 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
338
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800339 sqlite3_bind_int64 (stmt, 5, version);
340 sqlite3_bind_int64 (stmt, 6, action_time);
341
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800342 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800343 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800344
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800345 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800346 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800347 item->set_filename (filename);
348 item->set_version (version);
349 item->set_timestamp (action_time);
350 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
351 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800352
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800353 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800354 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800355 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800356
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800357 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
358 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800359
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800360 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
361 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800362
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800363 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800364
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800365 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800366
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800367 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800368
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800369 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800370
371 sqlite3_finalize (stmt);
372
373 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800374
375 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800376}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800377
378
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800379PcoPtr
380ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
381{
382 sqlite3_stmt *stmt;
383 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
384
385 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
386
387 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
388 sqlite3_bind_int64 (stmt, 2, seqno);
389
390 PcoPtr retval;
391 if (sqlite3_step (stmt) == SQLITE_ROW)
392 {
393 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
394 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
395 }
396 else
397 {
398 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
399 }
400 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
401 sqlite3_finalize (stmt);
402
403 return retval;
404}
405
406ActionItemPtr
407ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
408{
409 PcoPtr pco = LookupActionPco (deviceName, seqno);
410 if (!pco) return ActionItemPtr ();
411
412 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
413
414 return action;
415}
416
417Ccnx::PcoPtr
418ActionLog::LookupActionPco (const Ccnx::Name &actionName)
419{
420 sqlite3_stmt *stmt;
421 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
422
423 _LOG_DEBUG (actionName);
424 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
425
426 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
427
428 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
429
430 PcoPtr retval;
431 if (sqlite3_step (stmt) == SQLITE_ROW)
432 {
433 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
434 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
435 }
436 else
437 {
438 _LOG_TRACE ("No action found for name: " << actionName);
439 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800440 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800441 sqlite3_finalize (stmt);
442
443 return retval;
444}
445
446ActionItemPtr
447ActionLog::LookupAction (const Ccnx::Name &actionName)
448{
449 PcoPtr pco = LookupActionPco (actionName);
450 if (!pco) return ActionItemPtr ();
451
452 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
453
454 return action;
455}
456
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800457ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800458ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
459{
460 if (!actionPco)
461 {
462 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
463 }
464 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
465
466 if (!action)
467 {
468 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
469 }
470
471 sqlite3_stmt *stmt;
472 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
473 "(device_name, seq_no, action, filename, version, action_timestamp, "
474 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
475 "parent_device_name, parent_seq_no, "
476 "action_name, action_content_object) "
477 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
478 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
479 " ?, ?, "
480 " ?, ?);", -1, &stmt, 0);
481
482 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
483 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
484 sqlite3_bind_int64 (stmt, 2, seqno);
485
486 sqlite3_bind_int (stmt, 3, action->action ());
487 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
488 sqlite3_bind_int64 (stmt, 5, action->version ());
489 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
490
491 if (action->action () == ActionItem::UPDATE)
492 {
493 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
494
495 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
496 sqlite3_bind_int64 (stmt, 9, action->mtime ());
497 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
498
499 sqlite3_bind_int (stmt, 11, action->mode ());
500 sqlite3_bind_int (stmt, 12, action->seg_num ());
501 }
502
503 if (action->has_parent_device_name ())
504 {
505 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
506 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
507 }
508
509 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
510 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
511
512 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
513 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
514 sqlite3_step (stmt);
515
516 // if action needs to be applied to file state, the trigger will take care of it
517
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800518 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800519
520 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800521
522 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800523}
524
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800525ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800526ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
527{
528 Name name = actionPco->name ();
529 // <device_name>/"action"/<shared_folder_name_one_component>/<seqno>
530
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800531 uint64_t seqno = name.getCompFromBackAsInt (0);
532 string sharedFolder = name.getCompFromBackAsString (1);
533
534 if (sharedFolder != m_sharedFolderName)
535 {
536 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder"));
537 }
538
539 string action = name.getCompFromBackAsString (2);
540
541 if (action != "action")
542 {
543 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action"));
544 }
545 Name deviceName = name.getPartialName (0, name.size ()-3);
546
547 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
548
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800549 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800550}
551
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800552sqlite3_int64
553ActionLog::LogSize ()
554{
555 sqlite3_stmt *stmt;
556 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
557
558 sqlite3_int64 retval = -1;
559 if (sqlite3_step (stmt) == SQLITE_ROW)
560 {
561 retval = sqlite3_column_int64 (stmt, 0);
562 }
563
564 return retval;
565}
566
567
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800568///////////////////////////////////////////////////////////////////////////////////
569// SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY"
570///////////////////////////////////////////////////////////////////////////////////
571
572void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800573ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
574{
575 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
576
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800577 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800578 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800579 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800580 return;
581 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800582
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800583 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
584 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
585 int action = sqlite3_value_int (argv[2]);
586 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800587
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800588 _LOG_TRACE ("apply_function called with " << argc);
589 _LOG_TRACE ("device_name: " << Name (device_name)
590 << ", action: " << action
591 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800592
593 if (action == 0) // update
594 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800595 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
596 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
597 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
598 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
599 int mode = sqlite3_value_int (argv[8]);
600 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800601
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800602 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800603
604 sqlite3_stmt *stmt;
605 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800606 "SET "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800607 "device_name=?, seq_no=?, "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800608 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800609 "file_atime=datetime(?, 'unixepoch'),"
610 "file_mtime=datetime(?, 'unixepoch'),"
611 "file_ctime=datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800612 "file_chmod=?, "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800613 "file_seg_num=?, "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800614 "WHERE type=0 AND filename=?", -1, &stmt, 0);
615
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800616 sqlite3_bind_blob (stmt, 1, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800617 sqlite3_bind_int64 (stmt, 2, seq_no);
618 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
619 sqlite3_bind_int64 (stmt, 4, atime);
620 sqlite3_bind_int64 (stmt, 5, mtime);
621 sqlite3_bind_int64 (stmt, 6, ctime);
622 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800623 sqlite3_bind_int (stmt, 8, seg_num);
624 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800625
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800626 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800627
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800628 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_ROW && sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800629 sqlite3_errmsg (the->m_db));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800630
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800631 sqlite3_finalize (stmt);
632
633 int affected_rows = sqlite3_changes (the->m_db);
634 if (affected_rows == 0) // file didn't exist
635 {
636 sqlite3_stmt *stmt;
637 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800638 "(type,filename,device_name,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod,file_seg_num,directory) "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800639 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800640 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?, directory_name(?))", -1, &stmt, 0);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800641
642 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800643 sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800644 sqlite3_bind_int64 (stmt, 3, seq_no);
645 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
646 sqlite3_bind_int64 (stmt, 5, atime);
647 sqlite3_bind_int64 (stmt, 6, mtime);
648 sqlite3_bind_int64 (stmt, 7, ctime);
649 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800650 sqlite3_bind_int (stmt, 9, seg_num);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800651 sqlite3_bind_text (stmt, 10, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800652
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800653 sqlite3_step (stmt);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800654 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800655 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800656 sqlite3_finalize (stmt);
657 }
658 }
659 else if (action == 1) // delete
660 {
661 sqlite3_stmt *stmt;
662 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
663 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
664
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800665 _LOG_DEBUG ("Delete " << filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800666
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800667 sqlite3_step (stmt);
668 sqlite3_finalize (stmt);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800669
670 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800671 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800672
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800673 sqlite3_result_null (context);
674}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800675
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800676/**
677 * @todo Implement checking modification time and permissions
678 */
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800679FileItemPtr
680ActionLog::LookupFile (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800681{
682 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800683 sqlite3_prepare_v2 (m_db,
684 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
685 " FROM FileState "
686 " WHERE type = 0 AND filename = ?", -1, &stmt, 0);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800687 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800688
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800689 FileItemPtr retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800690 if (sqlite3_step (stmt) == SQLITE_ROW)
691 {
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800692 retval = make_shared<FileItem> ();
693 retval->set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
694 retval->set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
695 retval->set_seq_no (sqlite3_column_int64 (stmt, 2));
696 retval->set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
697 retval->set_mtime (sqlite3_column_int (stmt, 4));
698 retval->set_mode (sqlite3_column_int (stmt, 5));
699 retval->set_seg_num (sqlite3_column_int64 (stmt, 6));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800700 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800701 sqlite3_finalize (stmt);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800702
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800703 return retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800704}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800705
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800706FileItemsPtr
707ActionLog::LookupFilesForHash (const Hash &hash)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800708{
709 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800710 sqlite3_prepare_v2 (m_db,
711 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
712 " FROM FileState "
713 " WHERE type = 0 AND file_hash = ?;", -1, &stmt, 0);
714 sqlite3_bind_blob(stmt, 1, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800715
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800716 FileItemsPtr retval = make_shared<FileItems> ();
717 while (sqlite3_step (stmt) == SQLITE_ROW)
718 {
719 FileItem file;
720 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
721 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
722 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
723 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
724 file.set_mtime (sqlite3_column_int (stmt, 4));
725 file.set_mode (sqlite3_column_int (stmt, 5));
726 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
727
728 retval->push_back (file);
729 }
730
731 sqlite3_finalize (stmt);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800732
733 return retval;
734}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800735
736
737FileItemsPtr
738ActionLog::LookupFilesInFolder (const std::string &folder)
739{
740 sqlite3_stmt *stmt;
741 sqlite3_prepare_v2 (m_db,
742 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
743 " FROM FileState "
744 " WHERE type = 0 AND directory = ?", -1, &stmt, 0);
745 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
746
747 FileItemsPtr retval = make_shared<FileItems> ();
748 while (sqlite3_step (stmt) == SQLITE_ROW)
749 {
750 FileItem file;
751 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
752 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
753 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
754 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
755 file.set_mtime (sqlite3_column_int (stmt, 4));
756 file.set_mode (sqlite3_column_int (stmt, 5));
757 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
758
759 retval->push_back (file);
760 }
761
762 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
763
764 sqlite3_finalize (stmt);
765
766 return retval;
767}
768
769FileItemsPtr
770ActionLog::LookupFilesInFolderRecursively (const std::string &folder)
771{
772 // sqlite3_trace(m_db, xTrace, NULL);
773
774 sqlite3_stmt *stmt;
775
776 if (folder != "")
777 {
778 sqlite3_prepare_v2 (m_db,
779 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
780 " FROM FileState "
781 " WHERE type = 0 AND (directory = ? OR directory LIKE ?)", -1, &stmt, 0);
782 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
783
784 ostringstream escapedFolder;
785 for (string::const_iterator ch = folder.begin (); ch != folder.end (); ch ++)
786 {
787 if (*ch == '%')
788 escapedFolder << "\\%";
789 else
790 escapedFolder << *ch;
791 }
792 escapedFolder << "/" << "%";
793 string escapedFolderStr = escapedFolder.str ();
794 sqlite3_bind_text (stmt, 2, escapedFolderStr.c_str (), escapedFolderStr.size (), SQLITE_STATIC);
795 }
796 else
797 {
798 sqlite3_prepare_v2 (m_db,
799 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
800 " FROM FileState "
801 " WHERE type = 0", -1, &stmt, 0);
802 }
803
804 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
805
806 FileItemsPtr retval = make_shared<FileItems> ();
807 while (sqlite3_step (stmt) == SQLITE_ROW)
808 {
809 FileItem file;
810 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
811 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
812 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
813 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
814 file.set_mtime (sqlite3_column_int (stmt, 4));
815 file.set_mode (sqlite3_column_int (stmt, 5));
816 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
817
818 retval->push_back (file);
819 }
820
821 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
822
823 sqlite3_finalize (stmt);
824
825 return retval;
826}
827
828void
829ActionLog::ActionLog::directory_name_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
830{
831 if (argc != 1)
832 {
833 sqlite3_result_error (context, "``directory_name'' expects 1 text argument", -1);
834 sqlite3_result_null (context);
835 return;
836 }
837
838 if (sqlite3_value_bytes (argv[0]) == 0)
839 {
840 sqlite3_result_null (context);
841 return;
842 }
843
844 filesystem::path filePath (string (reinterpret_cast<const char*> (sqlite3_value_text (argv[0])), sqlite3_value_bytes (argv[0])));
845 string dirPath = filePath.parent_path ().generic_string ();
846
847 sqlite3_result_text (context, dirPath.c_str (), dirPath.size (), SQLITE_TRANSIENT);
848}
849