blob: 292a68c7401b28e74f8da55fa16f10ee6feb5d4e [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\
96 \n\
97 PRIMARY KEY (type, filename) \n\
98); \n\
99 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800100CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800101CREATE INDEX FileState_type_file_hash ON FileState (type, file_hash); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800102";
103
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800104static void xTrace (void*, const char* q)
105{
106 _LOG_TRACE ("SQLITE: " << q);
107}
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800108
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800109ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800110 SyncLogPtr syncLog,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800111 const std::string &sharedFolder,
112 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800113 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800114 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800115 , m_ccnx (ccnx)
116 , m_sharedFolderName (sharedFolder)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800117 , m_onFileAddedOrChanged (onFileAddedOrChanged)
118 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800119{
Alexander Afanasyev785b0a02013-01-30 10:17:47 -0800120 sqlite3_exec (m_db, "PRAGMA foreign_keys = OFF", NULL, NULL, NULL);
121 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
122
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800123 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
124 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
125
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800126 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
127 ActionLog::apply_action_xFun,
128 0, 0);
129 if (res != SQLITE_OK)
130 {
131 BOOST_THROW_EXCEPTION (Error::Db ()
132 << errmsg_info_str ("Cannot create function ``apply_action''"));
133 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800134
135
136 res = sqlite3_create_function (m_db, "directory_name", -1, SQLITE_ANY, 0,
137 ActionLog::directory_name_xFun,
138 0, 0);
139 if (res != SQLITE_OK)
140 {
141 BOOST_THROW_EXCEPTION (Error::Db ()
142 << errmsg_info_str ("Cannot create function ``directory_name''"));
143 }
144
145 // "Upgrading" database
146 sqlite3_exec (m_db, "ALTER TABLE FileState ADD COLUMN directory TEXT", NULL, NULL, NULL);
147 sqlite3_exec (m_db, "CREATE INDEX FileState_directory ON FileState (directory)", NULL, NULL, NULL);
148 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 -0800149}
150
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800151tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
152ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800153{
154 // check if something already exists
155 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800156 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
157 "FROM ActionLog "
158 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800159
160 if (res != SQLITE_OK)
161 {
162 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800163 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800164 }
165
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800166 sqlite3_int64 version = -1;
167 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800168 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800169
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800170 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800171 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800172 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800173 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800174
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800175 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
176 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800177 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800178 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800179 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800180 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800181
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800182 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800183 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800184}
185
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800186// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800187ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800188ActionLog::AddLocalActionUpdate (const std::string &filename,
189 const Hash &hash,
190 time_t wtime,
191 int mode,
192 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800193{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800194 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800195
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800196 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
197 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
198 sqlite3_int64 version;
199 CcnxCharbufPtr parent_device_name;
200 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800201
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800202 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800203
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800204 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
205 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800206
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800207 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800208 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800209 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800210 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800211 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800212 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800213 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800214 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800215 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800216 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800217
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800218 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
219
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800220 if (res != SQLITE_OK)
221 {
222 BOOST_THROW_EXCEPTION (Error::Db ()
223 << errmsg_info_str (sqlite3_errmsg (m_db))
224 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800225 }
226
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800227 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800228 sqlite3_bind_int64 (stmt, 2, seq_no);
229 sqlite3_bind_int (stmt, 3, 0);
230 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800231 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800232 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800233
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800234 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800235
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800236 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
237 sqlite3_bind_int64 (stmt, 9, wtime);
238 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800239 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800240 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800241
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800242 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800243 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800244 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800245 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800246 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800247
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800248 ActionItemPtr item = make_shared<ActionItem> ();
249 item->set_action (ActionItem::UPDATE);
250 item->set_filename (filename);
251 item->set_version (version);
252 item->set_timestamp (action_time);
253 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
254 // item->set_atime (atime);
255 item->set_mtime (wtime);
256 // item->set_ctime (ctime);
257 item->set_mode (mode);
258 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800259
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800260 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800261 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800262 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800263
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800264 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
265 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800266 }
267
268 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800269
270 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800271 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800272 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800273 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800274
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800275 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
276 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800277
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800278 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800279
280 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
281 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800282
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800283 sqlite3_step (stmt);
284
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800285 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800286
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800287 sqlite3_finalize (stmt);
288
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800289 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800290
291 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800292}
293
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800294// void
295// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
296// {
297// // not supported yet
298// BOOST_THROW_EXCEPTION (Error::Db ()
299// << errmsg_info_str ("Move operation is not yet supported"));
300// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800301
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800302ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800303ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800304{
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800305 _LOG_DEBUG ("Adding local action DELETE");
306
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800307 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800308
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800309 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
310 sqlite3_int64 version;
311 CcnxCharbufPtr parent_device_name;
312 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800313
314 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800315
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800316 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800317 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800318 {
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800319 _LOG_DEBUG ("Nothing to delete... [" << filename << "]");
320
321 // just in case, remove data from FileState
322 sqlite3_stmt *stmt;
323 sqlite3_prepare_v2 (m_db, "DELETE FROM FileState WHERE filename = ? ", -1, &stmt, 0);
324 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
325
326 sqlite3_step (stmt);
327
328 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
329
330 sqlite3_finalize (stmt);
331
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800332 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800333 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800334 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800335 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800336
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800337 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800338
339 sqlite3_stmt *stmt;
340 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800341 "(device_name, seq_no, action, filename, version, action_timestamp, "
342 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800343 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800344 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800345 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800346 " ?, ?)", -1, &stmt, 0);
347
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800348 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800349 sqlite3_bind_int64 (stmt, 2, seq_no);
350 sqlite3_bind_int (stmt, 3, 1);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800351 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); // file
352
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800353 sqlite3_bind_int64 (stmt, 5, version);
354 sqlite3_bind_int64 (stmt, 6, action_time);
355
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800356 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800357 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800358
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800359 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800360 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800361 item->set_filename (filename);
362 item->set_version (version);
363 item->set_timestamp (action_time);
364 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
365 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800366
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800367 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800368 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800369 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800370
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800371 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
372 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800373
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800374 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
375 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800376
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800377 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800378
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800379 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800380
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800381 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800382
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800383 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800384
385 sqlite3_finalize (stmt);
386
387 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800388
389 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800390}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800391
392
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800393PcoPtr
394ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
395{
396 sqlite3_stmt *stmt;
397 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
398
399 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
400
401 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
402 sqlite3_bind_int64 (stmt, 2, seqno);
403
404 PcoPtr retval;
405 if (sqlite3_step (stmt) == SQLITE_ROW)
406 {
407 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
408 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
409 }
410 else
411 {
412 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
413 }
414 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
415 sqlite3_finalize (stmt);
416
417 return retval;
418}
419
420ActionItemPtr
421ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
422{
423 PcoPtr pco = LookupActionPco (deviceName, seqno);
424 if (!pco) return ActionItemPtr ();
425
426 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
427
428 return action;
429}
430
431Ccnx::PcoPtr
432ActionLog::LookupActionPco (const Ccnx::Name &actionName)
433{
434 sqlite3_stmt *stmt;
435 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
436
437 _LOG_DEBUG (actionName);
438 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
439
440 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
441
442 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
443
444 PcoPtr retval;
445 if (sqlite3_step (stmt) == SQLITE_ROW)
446 {
447 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
448 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
449 }
450 else
451 {
452 _LOG_TRACE ("No action found for name: " << actionName);
453 }
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800454 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800455 sqlite3_finalize (stmt);
456
457 return retval;
458}
459
460ActionItemPtr
461ActionLog::LookupAction (const Ccnx::Name &actionName)
462{
463 PcoPtr pco = LookupActionPco (actionName);
464 if (!pco) return ActionItemPtr ();
465
466 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
467
468 return action;
469}
470
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800471ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800472ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
473{
474 if (!actionPco)
475 {
476 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
477 }
478 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
479
480 if (!action)
481 {
482 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
483 }
484
485 sqlite3_stmt *stmt;
486 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
487 "(device_name, seq_no, action, filename, version, action_timestamp, "
488 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
489 "parent_device_name, parent_seq_no, "
490 "action_name, action_content_object) "
491 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
492 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
493 " ?, ?, "
494 " ?, ?);", -1, &stmt, 0);
495
496 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
497 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
498 sqlite3_bind_int64 (stmt, 2, seqno);
499
500 sqlite3_bind_int (stmt, 3, action->action ());
501 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
502 sqlite3_bind_int64 (stmt, 5, action->version ());
503 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
504
505 if (action->action () == ActionItem::UPDATE)
506 {
507 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
508
509 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
510 sqlite3_bind_int64 (stmt, 9, action->mtime ());
511 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
512
513 sqlite3_bind_int (stmt, 11, action->mode ());
514 sqlite3_bind_int (stmt, 12, action->seg_num ());
515 }
516
517 if (action->has_parent_device_name ())
518 {
519 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
520 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
521 }
522
523 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
524 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
525
526 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
527 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
528 sqlite3_step (stmt);
529
530 // if action needs to be applied to file state, the trigger will take care of it
531
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800532 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800533
534 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800535
536 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800537}
538
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800539ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800540ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
541{
542 Name name = actionPco->name ();
543 // <device_name>/"action"/<shared_folder_name_one_component>/<seqno>
544
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800545 uint64_t seqno = name.getCompFromBackAsInt (0);
546 string sharedFolder = name.getCompFromBackAsString (1);
547
548 if (sharedFolder != m_sharedFolderName)
549 {
550 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder"));
551 }
552
553 string action = name.getCompFromBackAsString (2);
554
555 if (action != "action")
556 {
557 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action"));
558 }
559 Name deviceName = name.getPartialName (0, name.size ()-3);
560
561 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
562
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800563 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800564}
565
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800566sqlite3_int64
567ActionLog::LogSize ()
568{
569 sqlite3_stmt *stmt;
570 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
571
572 sqlite3_int64 retval = -1;
573 if (sqlite3_step (stmt) == SQLITE_ROW)
574 {
575 retval = sqlite3_column_int64 (stmt, 0);
576 }
577
578 return retval;
579}
580
581
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800582///////////////////////////////////////////////////////////////////////////////////
583// SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY"
584///////////////////////////////////////////////////////////////////////////////////
585
586void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800587ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
588{
589 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
590
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800591 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800592 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800593 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800594 return;
595 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800596
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800597 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
598 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
599 int action = sqlite3_value_int (argv[2]);
600 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800601
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800602 _LOG_TRACE ("apply_function called with " << argc);
603 _LOG_TRACE ("device_name: " << Name (device_name)
604 << ", action: " << action
605 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800606
607 if (action == 0) // update
608 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800609 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
610 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
611 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
612 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
613 int mode = sqlite3_value_int (argv[8]);
614 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800615
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800616 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800617
618 sqlite3_stmt *stmt;
619 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800620 "SET "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800621 "device_name=?, seq_no=?, "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800622 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800623 "file_atime=datetime(?, 'unixepoch'),"
624 "file_mtime=datetime(?, 'unixepoch'),"
625 "file_ctime=datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800626 "file_chmod=?, "
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800627 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800628 "WHERE type=0 AND filename=?", -1, &stmt, 0);
629
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800630 sqlite3_bind_blob (stmt, 1, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800631 sqlite3_bind_int64 (stmt, 2, seq_no);
632 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
633 sqlite3_bind_int64 (stmt, 4, atime);
634 sqlite3_bind_int64 (stmt, 5, mtime);
635 sqlite3_bind_int64 (stmt, 6, ctime);
636 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800637 sqlite3_bind_int (stmt, 8, seg_num);
638 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800639
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800640 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800641
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800642 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_ROW && sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800643 sqlite3_errmsg (the->m_db));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800644
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800645 sqlite3_finalize (stmt);
646
647 int affected_rows = sqlite3_changes (the->m_db);
648 if (affected_rows == 0) // file didn't exist
649 {
650 sqlite3_stmt *stmt;
651 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800652 "(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 -0800653 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800654 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?, directory_name(?))", -1, &stmt, 0);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800655
656 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800657 sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800658 sqlite3_bind_int64 (stmt, 3, seq_no);
659 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
660 sqlite3_bind_int64 (stmt, 5, atime);
661 sqlite3_bind_int64 (stmt, 6, mtime);
662 sqlite3_bind_int64 (stmt, 7, ctime);
663 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800664 sqlite3_bind_int (stmt, 9, seg_num);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800665 sqlite3_bind_text (stmt, 10, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800666
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800667 sqlite3_step (stmt);
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800668 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_DONE,
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800669 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800670 sqlite3_finalize (stmt);
671 }
672 }
673 else if (action == 1) // delete
674 {
675 sqlite3_stmt *stmt;
676 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
677 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
678
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800679 _LOG_DEBUG ("Delete " << filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800680
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800681 sqlite3_step (stmt);
682 sqlite3_finalize (stmt);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800683
684 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800685 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800686
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800687 sqlite3_result_null (context);
688}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800689
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800690/**
691 * @todo Implement checking modification time and permissions
692 */
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800693FileItemPtr
694ActionLog::LookupFile (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800695{
696 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800697 sqlite3_prepare_v2 (m_db,
698 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
699 " FROM FileState "
700 " WHERE type = 0 AND filename = ?", -1, &stmt, 0);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800701 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800702
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800703 FileItemPtr retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800704 if (sqlite3_step (stmt) == SQLITE_ROW)
705 {
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800706 retval = make_shared<FileItem> ();
707 retval->set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
708 retval->set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
709 retval->set_seq_no (sqlite3_column_int64 (stmt, 2));
710 retval->set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
711 retval->set_mtime (sqlite3_column_int (stmt, 4));
712 retval->set_mode (sqlite3_column_int (stmt, 5));
713 retval->set_seg_num (sqlite3_column_int64 (stmt, 6));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800714 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800715 sqlite3_finalize (stmt);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800716
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800717 return retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800718}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800719
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800720FileItemsPtr
721ActionLog::LookupFilesForHash (const Hash &hash)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800722{
723 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800724 sqlite3_prepare_v2 (m_db,
725 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
726 " FROM FileState "
Alexander Afanasyev4a8781c2013-01-29 17:59:44 -0800727 " WHERE type = 0 AND file_hash = ?", -1, &stmt, 0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800728 sqlite3_bind_blob(stmt, 1, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800729
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800730 FileItemsPtr retval = make_shared<FileItems> ();
731 while (sqlite3_step (stmt) == SQLITE_ROW)
732 {
733 FileItem file;
734 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
735 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
736 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
737 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
738 file.set_mtime (sqlite3_column_int (stmt, 4));
739 file.set_mode (sqlite3_column_int (stmt, 5));
740 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
741
742 retval->push_back (file);
743 }
744
745 sqlite3_finalize (stmt);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800746
747 return retval;
748}
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800749
750
751FileItemsPtr
752ActionLog::LookupFilesInFolder (const std::string &folder)
753{
754 sqlite3_stmt *stmt;
755 sqlite3_prepare_v2 (m_db,
756 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
757 " FROM FileState "
758 " WHERE type = 0 AND directory = ?", -1, &stmt, 0);
759 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
760
761 FileItemsPtr retval = make_shared<FileItems> ();
762 while (sqlite3_step (stmt) == SQLITE_ROW)
763 {
764 FileItem file;
765 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
766 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
767 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
768 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
769 file.set_mtime (sqlite3_column_int (stmt, 4));
770 file.set_mode (sqlite3_column_int (stmt, 5));
771 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
772
773 retval->push_back (file);
774 }
775
776 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
777
778 sqlite3_finalize (stmt);
779
780 return retval;
781}
782
783FileItemsPtr
784ActionLog::LookupFilesInFolderRecursively (const std::string &folder)
785{
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800786 _LOG_DEBUG ("LookupFilesInFolderRecursively: [" << folder << "]");
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800787
788 sqlite3_stmt *stmt;
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800789 if (folder != "")
790 {
791 sqlite3_prepare_v2 (m_db,
792 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
793 " FROM FileState "
794 " WHERE type = 0 AND (directory = ? OR directory LIKE ?)", -1, &stmt, 0);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800795 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
796
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800797 sqlite3_bind_text (stmt, 1, folder.c_str (), folder.size (), SQLITE_STATIC);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800798 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800799
800 ostringstream escapedFolder;
801 for (string::const_iterator ch = folder.begin (); ch != folder.end (); ch ++)
802 {
803 if (*ch == '%')
804 escapedFolder << "\\%";
805 else
806 escapedFolder << *ch;
807 }
808 escapedFolder << "/" << "%";
809 string escapedFolderStr = escapedFolder.str ();
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800810
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800811 sqlite3_bind_text (stmt, 2, escapedFolderStr.c_str (), escapedFolderStr.size (), SQLITE_STATIC);
Alexander Afanasyev506ff222013-01-29 18:12:55 -0800812 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -0800813 }
814 else
815 {
816 sqlite3_prepare_v2 (m_db,
817 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
818 " FROM FileState "
819 " WHERE type = 0", -1, &stmt, 0);
820 }
821
822 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
823
824 FileItemsPtr retval = make_shared<FileItems> ();
825 while (sqlite3_step (stmt) == SQLITE_ROW)
826 {
827 FileItem file;
828 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
829 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
830 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
831 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
832 file.set_mtime (sqlite3_column_int (stmt, 4));
833 file.set_mode (sqlite3_column_int (stmt, 5));
834 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
835
836 retval->push_back (file);
837 }
838
839 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_DONE, sqlite3_errmsg (m_db));
840
841 sqlite3_finalize (stmt);
842
843 return retval;
844}
845
846void
847ActionLog::ActionLog::directory_name_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
848{
849 if (argc != 1)
850 {
851 sqlite3_result_error (context, "``directory_name'' expects 1 text argument", -1);
852 sqlite3_result_null (context);
853 return;
854 }
855
856 if (sqlite3_value_bytes (argv[0]) == 0)
857 {
858 sqlite3_result_null (context);
859 return;
860 }
861
862 filesystem::path filePath (string (reinterpret_cast<const char*> (sqlite3_value_text (argv[0])), sqlite3_value_bytes (argv[0])));
863 string dirPath = filePath.parent_path ().generic_string ();
864
865 sqlite3_result_text (context, dirPath.c_str (), dirPath.size (), SQLITE_TRANSIENT);
866}
867