blob: 32820b6b534f37609f586a4a49edd43cb85c0ee6 [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 Afanasyev0995f322013-01-22 13:16:46 -080091 device_name BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080092 seq_no INTEGER NOT NULL, \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -080093 file_hash BLOB NOT NULL, \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -080094 file_atime TIMESTAMP, \n\
95 file_mtime TIMESTAMP, \n\
96 file_ctime TIMESTAMP, \n\
97 file_chmod INTEGER, \n\
98 file_seg_num INTEGER, \n\
99 \n\
100 PRIMARY KEY (type, filename) \n\
101); \n\
102 \n\
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800103CREATE INDEX FileState_device_name_seq_no ON FileState (device_name, seq_no); \n\
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800104CREATE INDEX FileState_type_file_hash ON FileState (type, file_hash); \n\
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800105";
106
107
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800108ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800109 SyncLogPtr syncLog,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800110 const std::string &sharedFolder,
111 OnFileAddedOrChangedCallback onFileAddedOrChanged, OnFileRemovedCallback onFileRemoved)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800112 : DbHelper (path / ".chronoshare", "action-log.db")
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800113 , m_syncLog (syncLog)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800114 , m_ccnx (ccnx)
115 , m_sharedFolderName (sharedFolder)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800116 , m_onFileAddedOrChanged (onFileAddedOrChanged)
117 , m_onFileRemoved (onFileRemoved)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800118{
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800119 sqlite3_exec (m_db, INIT_DATABASE.c_str (), NULL, NULL, NULL);
120 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
121
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800122 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
123 ActionLog::apply_action_xFun,
124 0, 0);
125 if (res != SQLITE_OK)
126 {
127 BOOST_THROW_EXCEPTION (Error::Db ()
128 << errmsg_info_str ("Cannot create function ``apply_action''"));
129 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800130}
131
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800132tuple<sqlite3_int64 /*version*/, Ccnx::CcnxCharbufPtr /*device name*/, sqlite3_int64 /*seq_no*/>
133ActionLog::GetLatestActionForFile (const std::string &filename)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800134{
135 // check if something already exists
136 sqlite3_stmt *stmt;
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800137 int res = sqlite3_prepare_v2 (m_db, "SELECT version,device_name,seq_no,action "
138 "FROM ActionLog "
139 "WHERE filename=? ORDER BY version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800140
141 if (res != SQLITE_OK)
142 {
143 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800144 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800145 }
146
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800147 sqlite3_int64 version = -1;
148 CcnxCharbufPtr parent_device_name;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800149 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800150
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800151 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800152 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800153 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800154 version = sqlite3_column_int64 (stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800155
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800156 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
157 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800158 parent_device_name = make_shared<CcnxCharbuf> (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800159 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800160 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800161 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800162
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800163 sqlite3_finalize (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800164 return make_tuple (version, parent_device_name, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800165}
166
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800167// local add action. remote action is extracted from content object
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800168ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800169ActionLog::AddLocalActionUpdate (const std::string &filename,
170 const Hash &hash,
171 time_t wtime,
172 int mode,
173 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800174{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800175 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800176
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800177 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
178 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
179 sqlite3_int64 version;
180 CcnxCharbufPtr parent_device_name;
181 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800182
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800183 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800184
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800185 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
186 version ++;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800187
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800188 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800189 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800190 "(device_name, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800191 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800192 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800193 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800194 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800195 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800196 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800197 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800198
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800199 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK, sqlite3_errmsg (m_db));
200
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800201 if (res != SQLITE_OK)
202 {
203 BOOST_THROW_EXCEPTION (Error::Db ()
204 << errmsg_info_str (sqlite3_errmsg (m_db))
205 );
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800206 }
207
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800208 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800209 sqlite3_bind_int64 (stmt, 2, seq_no);
210 sqlite3_bind_int (stmt, 3, 0);
211 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800212 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800213 sqlite3_bind_int64 (stmt, 6, action_time);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800214
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800215 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800216
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800217 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
218 sqlite3_bind_int64 (stmt, 9, wtime);
219 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800220 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800221 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800222
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800223 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800224 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800225 sqlite3_bind_blob (stmt, 13, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800226 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800227 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800228
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800229 ActionItemPtr item = make_shared<ActionItem> ();
230 item->set_action (ActionItem::UPDATE);
231 item->set_filename (filename);
232 item->set_version (version);
233 item->set_timestamp (action_time);
234 item->set_file_hash (hash.GetHash (), hash.GetHashBytes ());
235 // item->set_atime (atime);
236 item->set_mtime (wtime);
237 // item->set_ctime (ctime);
238 item->set_mode (mode);
239 item->set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800240
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800241 if (parent_device_name && parent_seq_no > 0)
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800242 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800243 // cout << Name (*parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800244
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800245 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
246 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800247 }
248
249 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800250
251 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800252 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800253 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800254 _LOG_DEBUG ("ActionName: " << actionName);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800255
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800256 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
257 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800258
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800259 // _LOG_DEBUG (" >>>>>>> " << Name (namePtr->buf () << " " << namePtr->length ());
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800260
261 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
262 sqlite3_bind_blob (stmt, 16, head (actionData), actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800263
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800264 sqlite3_step (stmt);
265
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800266 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
267
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800268 sqlite3_finalize (stmt);
269
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800270 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800271
272 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800273}
274
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800275// void
276// ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
277// {
278// // not supported yet
279// BOOST_THROW_EXCEPTION (Error::Db ()
280// << errmsg_info_str ("Move operation is not yet supported"));
281// }
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800282
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800283ActionItemPtr
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800284ActionLog::AddLocalActionDelete (const std::string &filename)
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800285{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800286 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800287
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800288 CcnxCharbufPtr device_name = m_syncLog->GetLocalName ().toCcnxCharbuf ();
289 sqlite3_int64 version;
290 CcnxCharbufPtr parent_device_name;
291 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800292
293 sqlite3_int64 action_time = time (0);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800294
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800295 tie (version, parent_device_name, parent_seq_no) = GetLatestActionForFile (filename);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800296 if (!parent_device_name) // no records exist or file was already deleted
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800297 {
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800298 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800299 return ActionItemPtr ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800300 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800301 version ++;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800302
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800303 sqlite3_int64 seq_no = m_syncLog->GetNextLocalSeqNo ();
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800304
305 sqlite3_stmt *stmt;
306 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800307 "(device_name, seq_no, action, filename, version, action_timestamp, "
308 "parent_device_name, parent_seq_no, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800309 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800310 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800311 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800312 " ?, ?)", -1, &stmt, 0);
313
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800314 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800315 sqlite3_bind_int64 (stmt, 2, seq_no);
316 sqlite3_bind_int (stmt, 3, 1);
317 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
318 sqlite3_bind_int64 (stmt, 5, version);
319 sqlite3_bind_int64 (stmt, 6, action_time);
320
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800321 sqlite3_bind_blob (stmt, 7, parent_device_name->buf (), parent_device_name->length (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800322 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800323
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800324
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800325 ActionItemPtr item = make_shared<ActionItem> ();
Alexander Afanasyev6d4befc2013-01-26 00:01:28 -0800326 item->set_action (ActionItem::DELETE);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800327 item->set_filename (filename);
328 item->set_version (version);
329 item->set_timestamp (action_time);
330 item->set_parent_device_name (parent_device_name->buf (), parent_device_name->length ());
331 item->set_parent_seq_no (parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800332
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800333 string item_msg;
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800334 item->SerializeToString (&item_msg);
Alexander Afanasyev8e2104a2013-01-22 10:56:18 -0800335 Name actionName = Name (m_syncLog->GetLocalName ())("action")(m_sharedFolderName)(seq_no);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800336
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800337 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
338 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800339
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800340 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
341 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800342
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800343 sqlite3_step (stmt);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800344
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800345 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
346
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800347 // cout << Ccnx::Name (parent_device_name) << endl;
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800348
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800349 // assign name to the action, serialize action, and create content object
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800350
351 sqlite3_finalize (stmt);
352
353 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800354
355 return item;
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800356}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800357
358
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800359PcoPtr
360ActionLog::LookupActionPco (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
361{
362 sqlite3_stmt *stmt;
363 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE device_name=? AND seq_no=?", -1, &stmt, 0);
364
365 CcnxCharbufPtr name = deviceName.toCcnxCharbuf ();
366
367 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
368 sqlite3_bind_int64 (stmt, 2, seqno);
369
370 PcoPtr retval;
371 if (sqlite3_step (stmt) == SQLITE_ROW)
372 {
373 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
374 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
375 }
376 else
377 {
378 _LOG_TRACE ("No action found for deviceName [" << deviceName << "] and seqno:" << seqno);
379 }
380 // _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
381 sqlite3_finalize (stmt);
382
383 return retval;
384}
385
386ActionItemPtr
387ActionLog::LookupAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno)
388{
389 PcoPtr pco = LookupActionPco (deviceName, seqno);
390 if (!pco) return ActionItemPtr ();
391
392 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
393
394 return action;
395}
396
397Ccnx::PcoPtr
398ActionLog::LookupActionPco (const Ccnx::Name &actionName)
399{
400 sqlite3_stmt *stmt;
401 sqlite3_prepare_v2 (m_db, "SELECT action_content_object FROM ActionLog WHERE action_name=?", -1, &stmt, 0);
402
403 _LOG_DEBUG (actionName);
404 CcnxCharbufPtr name = actionName.toCcnxCharbuf ();
405
406 _LOG_DEBUG (" <<<<<<< " << name->buf () << " " << name->length ());
407
408 sqlite3_bind_blob (stmt, 1, name->buf (), name->length (), SQLITE_STATIC);
409
410 PcoPtr retval;
411 if (sqlite3_step (stmt) == SQLITE_ROW)
412 {
413 // _LOG_DEBUG (sqlite3_column_blob (stmt, 0) << ", " << sqlite3_column_bytes (stmt, 0));
414 retval = make_shared<ParsedContentObject> (reinterpret_cast<const unsigned char *> (sqlite3_column_blob (stmt, 0)), sqlite3_column_bytes (stmt, 0));
415 }
416 else
417 {
418 _LOG_TRACE ("No action found for name: " << actionName);
419 }
420 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
421 sqlite3_finalize (stmt);
422
423 return retval;
424}
425
426ActionItemPtr
427ActionLog::LookupAction (const Ccnx::Name &actionName)
428{
429 PcoPtr pco = LookupActionPco (actionName);
430 if (!pco) return ActionItemPtr ();
431
432 ActionItemPtr action = deserializeMsg<ActionItem> (pco->content ());
433
434 return action;
435}
436
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800437ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800438ActionLog::AddRemoteAction (const Ccnx::Name &deviceName, sqlite3_int64 seqno, Ccnx::PcoPtr actionPco)
439{
440 if (!actionPco)
441 {
442 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("actionPco is not valid"));
443 }
444 ActionItemPtr action = deserializeMsg<ActionItem> (actionPco->content ());
445
446 if (!action)
447 {
448 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("action cannot be decoded"));
449 }
450
451 sqlite3_stmt *stmt;
452 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
453 "(device_name, seq_no, action, filename, version, action_timestamp, "
454 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
455 "parent_device_name, parent_seq_no, "
456 "action_name, action_content_object) "
457 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
458 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,?, "
459 " ?, ?, "
460 " ?, ?);", -1, &stmt, 0);
461
462 CcnxCharbufPtr device_name = deviceName.toCcnxCharbuf ();
463 sqlite3_bind_blob (stmt, 1, device_name->buf (), device_name->length (), SQLITE_STATIC);
464 sqlite3_bind_int64 (stmt, 2, seqno);
465
466 sqlite3_bind_int (stmt, 3, action->action ());
467 sqlite3_bind_text (stmt, 4, action->filename ().c_str (), action->filename ().size (), SQLITE_STATIC);
468 sqlite3_bind_int64 (stmt, 5, action->version ());
469 sqlite3_bind_int64 (stmt, 6, action->timestamp ());
470
471 if (action->action () == ActionItem::UPDATE)
472 {
473 sqlite3_bind_blob (stmt, 7, action->file_hash ().c_str (), action->file_hash ().size (), SQLITE_STATIC);
474
475 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
476 sqlite3_bind_int64 (stmt, 9, action->mtime ());
477 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
478
479 sqlite3_bind_int (stmt, 11, action->mode ());
480 sqlite3_bind_int (stmt, 12, action->seg_num ());
481 }
482
483 if (action->has_parent_device_name ())
484 {
485 sqlite3_bind_blob (stmt, 13, action->parent_device_name ().c_str (), action->parent_device_name ().size (), SQLITE_TRANSIENT);
486 sqlite3_bind_int64 (stmt, 14, action->parent_seq_no ());
487 }
488
489 Name actionName = Name (deviceName)("action")(m_sharedFolderName)(seqno);
490 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
491
492 sqlite3_bind_blob (stmt, 15, namePtr->buf (), namePtr->length (), SQLITE_STATIC);
493 sqlite3_bind_blob (stmt, 16, head (actionPco->buf ()), actionPco->buf ().size (), SQLITE_STATIC);
494 sqlite3_step (stmt);
495
496 // if action needs to be applied to file state, the trigger will take care of it
497
498 _LOG_DEBUG_COND (sqlite3_errcode (m_db) != SQLITE_OK && sqlite3_errcode (m_db) != SQLITE_ROW, sqlite3_errmsg (m_db));
499
500 sqlite3_finalize (stmt);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800501
502 return action;
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800503}
504
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800505ActionItemPtr
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800506ActionLog::AddRemoteAction (Ccnx::PcoPtr actionPco)
507{
508 Name name = actionPco->name ();
509 // <device_name>/"action"/<shared_folder_name_one_component>/<seqno>
510
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800511 uint64_t seqno = name.getCompFromBackAsInt (0);
512 string sharedFolder = name.getCompFromBackAsString (1);
513
514 if (sharedFolder != m_sharedFolderName)
515 {
516 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("Action doesn't belong to this shared folder"));
517 }
518
519 string action = name.getCompFromBackAsString (2);
520
521 if (action != "action")
522 {
523 BOOST_THROW_EXCEPTION (Error::ActionLog () << errmsg_info_str ("not an action"));
524 }
525 Name deviceName = name.getPartialName (0, name.size ()-3);
526
527 _LOG_DEBUG ("From [" << name << "] extracted deviceName: " << deviceName << ", sharedFolder: " << sharedFolder << ", seqno: " << seqno);
528
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800529 return AddRemoteAction (deviceName, seqno, actionPco);
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800530}
531
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800532sqlite3_int64
533ActionLog::LogSize ()
534{
535 sqlite3_stmt *stmt;
536 sqlite3_prepare_v2 (m_db, "SELECT count(*) FROM ActionLog", -1, &stmt, 0);
537
538 sqlite3_int64 retval = -1;
539 if (sqlite3_step (stmt) == SQLITE_ROW)
540 {
541 retval = sqlite3_column_int64 (stmt, 0);
542 }
543
544 return retval;
545}
546
547
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800548///////////////////////////////////////////////////////////////////////////////////
549// SHOULD BE MOVED TO SEPARATE FILESTATE CLASS "EVENTUALLY"
550///////////////////////////////////////////////////////////////////////////////////
551
552void
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800553ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
554{
555 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
556
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800557 if (argc != 10)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800558 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800559 sqlite3_result_error (context, "``apply_action'' expects 10 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800560 return;
561 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800562
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800563 CcnxCharbuf device_name (sqlite3_value_blob (argv[0]), sqlite3_value_bytes (argv[0]));
564 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[1]);
565 int action = sqlite3_value_int (argv[2]);
566 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[3]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800567
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800568 _LOG_TRACE ("apply_function called with " << argc);
569 _LOG_TRACE ("device_name: " << Name (device_name)
570 << ", action: " << action
571 << ", file: " << filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800572
573 if (action == 0) // update
574 {
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800575 Hash hash (sqlite3_value_blob (argv[4]), sqlite3_value_bytes (argv[4]));
576 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[5]));
577 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
578 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
579 int mode = sqlite3_value_int (argv[8]);
580 int seg_num = sqlite3_value_int (argv[9]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800581
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800582 _LOG_DEBUG ("Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800583
584 sqlite3_stmt *stmt;
585 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800586 "SET "
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800587 "device_name=?, seq_no=?, "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800588 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800589 "file_atime=datetime(?, 'unixepoch'),"
590 "file_mtime=datetime(?, 'unixepoch'),"
591 "file_ctime=datetime(?, 'unixepoch'),"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800592 "file_chmod=?, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800593 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800594 "WHERE type=0 AND filename=?", -1, &stmt, 0);
595
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800596 sqlite3_bind_blob (stmt, 1, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800597 sqlite3_bind_int64 (stmt, 2, seq_no);
598 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
599 sqlite3_bind_int64 (stmt, 4, atime);
600 sqlite3_bind_int64 (stmt, 5, mtime);
601 sqlite3_bind_int64 (stmt, 6, ctime);
602 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800603 sqlite3_bind_int (stmt, 8, seg_num);
604 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800605
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800606 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800607
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800608 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK,
609 sqlite3_errmsg (the->m_db));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800610
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800611 sqlite3_finalize (stmt);
612
613 int affected_rows = sqlite3_changes (the->m_db);
614 if (affected_rows == 0) // file didn't exist
615 {
616 sqlite3_stmt *stmt;
617 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800618 "(type,filename,device_name,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod,file_seg_num) "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800619 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800620 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?, ?)", -1, &stmt, 0);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800621
622 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800623 sqlite3_bind_blob (stmt, 2, device_name.buf (), device_name.length (), SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800624 sqlite3_bind_int64 (stmt, 3, seq_no);
625 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
626 sqlite3_bind_int64 (stmt, 5, atime);
627 sqlite3_bind_int64 (stmt, 6, mtime);
628 sqlite3_bind_int64 (stmt, 7, ctime);
629 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800630 sqlite3_bind_int (stmt, 9, seg_num);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800631
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800632 sqlite3_step (stmt);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800633 _LOG_DEBUG_COND (sqlite3_errcode (the->m_db) != SQLITE_OK,
634 sqlite3_errmsg (the->m_db));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800635 sqlite3_finalize (stmt);
636 }
637 }
638 else if (action == 1) // delete
639 {
640 sqlite3_stmt *stmt;
641 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
642 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
643
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800644 _LOG_DEBUG ("Delete " << filename);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800645
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800646 sqlite3_step (stmt);
647 sqlite3_finalize (stmt);
Alexander Afanasyev0c545ed2013-01-26 00:12:32 -0800648
649 the->m_onFileRemoved (filename);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800650 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800651
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800652 sqlite3_result_null (context);
653}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800654
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800655/**
656 * @todo Implement checking modification time and permissions
657 */
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800658FileItemPtr
659ActionLog::LookupFile (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800660{
661 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800662 sqlite3_prepare_v2 (m_db,
663 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
664 " FROM FileState "
665 " WHERE type = 0 AND filename = ?", -1, &stmt, 0);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800666 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800667
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800668 FileItemPtr retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800669 if (sqlite3_step (stmt) == SQLITE_ROW)
670 {
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800671 retval = make_shared<FileItem> ();
672 retval->set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
673 retval->set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
674 retval->set_seq_no (sqlite3_column_int64 (stmt, 2));
675 retval->set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
676 retval->set_mtime (sqlite3_column_int (stmt, 4));
677 retval->set_mode (sqlite3_column_int (stmt, 5));
678 retval->set_seg_num (sqlite3_column_int64 (stmt, 6));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800679 }
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800680 sqlite3_finalize (stmt);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800681
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800682 return retval;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800683}
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800684
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800685FileItemsPtr
686ActionLog::LookupFilesForHash (const Hash &hash)
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800687{
688 sqlite3_stmt *stmt;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800689 sqlite3_prepare_v2 (m_db,
690 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
691 " FROM FileState "
692 " WHERE type = 0 AND file_hash = ?;", -1, &stmt, 0);
693 sqlite3_bind_blob(stmt, 1, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800694
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800695 FileItemsPtr retval = make_shared<FileItems> ();
696 while (sqlite3_step (stmt) == SQLITE_ROW)
697 {
698 FileItem file;
699 file.set_filename (reinterpret_cast<const char *> (sqlite3_column_text (stmt, 0)), sqlite3_column_bytes (stmt, 0));
700 file.set_device_name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1));
701 file.set_seq_no (sqlite3_column_int64 (stmt, 2));
702 file.set_file_hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3));
703 file.set_mtime (sqlite3_column_int (stmt, 4));
704 file.set_mode (sqlite3_column_int (stmt, 5));
705 file.set_seg_num (sqlite3_column_int64 (stmt, 6));
706
707 retval->push_back (file);
708 }
709
710 sqlite3_finalize (stmt);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800711
712 return retval;
713}