blob: 7c5934e7a0a2c79223c943f2757af62cfda55ec9 [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"
23
24using namespace boost;
25using namespace std;
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080026using namespace Ccnx;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080027
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080028ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const boost::filesystem::path &path,
29 const std::string &localName, const std::string &sharedFolder)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080030 : SyncLog (path, localName)
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080031 , m_ccnx (ccnx)
32 , m_sharedFolderName (sharedFolder)
Alexander Afanasyeva199f972013-01-02 19:37:26 -080033{
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080034 int res = sqlite3_create_function (m_db, "apply_action", -1, SQLITE_ANY, reinterpret_cast<void*> (this),
35 ActionLog::apply_action_xFun,
36 0, 0);
37 if (res != SQLITE_OK)
38 {
39 BOOST_THROW_EXCEPTION (Error::Db ()
40 << errmsg_info_str ("Cannot create function ``apply_action''"));
41 }
Alexander Afanasyeva199f972013-01-02 19:37:26 -080042}
43
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080044tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string>
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080045ActionLog::GetExistingRecord (const std::string &filename)
46{
47 // check if something already exists
48 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080049 int res = sqlite3_prepare_v2 (m_db, "SELECT a.version,a.device_id,a.seq_no,a.action,s.device_name "
50 "FROM ActionLog a JOIN SyncNodes s ON s.device_id = a.device_id "
Alexander Afanasyevee7e6132013-01-03 20:03:14 -080051 "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080052
53 if (res != SQLITE_OK)
54 {
55 BOOST_THROW_EXCEPTION (Error::Db ()
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080056 << errmsg_info_str ("Some error with GetExistingRecord"));
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080057 }
58
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080059 // with parent with version number + 1
60 sqlite3_int64 version = 0;
61 sqlite3_int64 parent_device_id = -1;
62 sqlite3_int64 parent_seq_no = -1;
63 string parent_device_name;
64
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080065 sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080066 if (sqlite3_step (stmt) == SQLITE_ROW)
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080067 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080068 version = sqlite3_column_int64 (stmt, 0) + 1;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080069
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080070 if (sqlite3_column_int (stmt, 3) == 0) // prevent "linking" if the file was previously deleted
71 {
72 parent_device_id = sqlite3_column_int64 (stmt, 1);
73 parent_seq_no = sqlite3_column_int64 (stmt, 2);
Alexander Afanasyev707f6df2013-01-04 22:46:15 -080074 parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 4)), sqlite3_column_bytes (stmt, 4));
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080075 }
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080076 }
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080077
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080078 sqlite3_finalize (stmt);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080079 return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080080}
81
Alexander Afanasyeva199f972013-01-02 19:37:26 -080082// local add action. remote action is extracted from content object
83void
84ActionLog::AddActionUpdate (const std::string &filename,
85 const Hash &hash,
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080086 time_t wtime,
Alexander Afanasyev334aac82013-01-18 14:06:39 -080087 int mode,
88 int seg_num)
Alexander Afanasyeva199f972013-01-02 19:37:26 -080089{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080090 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080091
92 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
93 sqlite3_int64 version = 0;
94 sqlite3_int64 parent_device_id = -1;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -080095 string parent_device_name;
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080096 sqlite3_int64 parent_seq_no = -1;
Alexander Afanasyeva199f972013-01-02 19:37:26 -080097
Alexander Afanasyev433ecda2013-01-02 22:13:45 -080098 sqlite3_int64 action_time = time (0);
99
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800100 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800101
102 sqlite3_stmt *stmt;
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800103 int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
104 "(device_id, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800105 "file_hash, file_atime, file_mtime, file_ctime, file_chmod, file_seg_num, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800106 "parent_device_id, parent_seq_no, "
107 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800108 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
109 " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800110 " ?, ?, "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800111 " ?, ?);", -1, &stmt, 0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800112
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800113 // cout << "INSERT INTO ActionLog "
114 // "(device_id, seq_no, action, filename, version, action_timestamp, "
115 // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, "
116 // "parent_device_id, parent_seq_no) "
117 // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
118 // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?,"
119 // " ?, ?)" << endl;
120
121 if (res != SQLITE_OK)
122 {
123 BOOST_THROW_EXCEPTION (Error::Db ()
124 << errmsg_info_str (sqlite3_errmsg (m_db))
125 );
126 // << errmsg_info_str ("Some error with prepare AddActionUpdate"));
127 }
128
129
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800130 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
131 sqlite3_bind_int64 (stmt, 2, seq_no);
132 sqlite3_bind_int (stmt, 3, 0);
133 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800134 sqlite3_bind_int64 (stmt, 5, version);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800135 sqlite3_bind_int64 (stmt, 6, action_time);
136
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800137 sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
138
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800139 // sqlite3_bind_int64 (stmt, 8, atime); // NULL
140 sqlite3_bind_int64 (stmt, 9, wtime);
141 // sqlite3_bind_int64 (stmt, 10, ctime); // NULL
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800142 sqlite3_bind_int (stmt, 11, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800143 sqlite3_bind_int (stmt, 12, seg_num);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800144
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800145 if (parent_device_id > 0 && parent_seq_no > 0)
146 {
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800147 sqlite3_bind_int64 (stmt, 13, parent_device_id);
148 sqlite3_bind_int64 (stmt, 14, parent_seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800149 }
150 else
151 {
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800152 sqlite3_bind_null (stmt, 13);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800153 sqlite3_bind_null (stmt, 14);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800154 }
155
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800156 // missing part: creating ContentObject for the action !!!
157
158 ActionItem item;
159 item.set_action (ActionItem::UPDATE);
160 item.set_filename (filename);
161 item.set_version (version);
162 item.set_timestamp (action_time);
163 item.set_file_hash (hash.GetHash (), hash.GetHashBytes ());
Alexander Afanasyev68f2a952013-01-08 14:34:16 -0800164 // item.set_atime (atime);
165 item.set_mtime (wtime);
166 // item.set_ctime (ctime);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800167 item.set_mode (mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800168 item.set_seg_num (seg_num);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800169
170 if (parent_device_id > 0 && parent_seq_no > 0)
171 {
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800172 cout << Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
173 parent_device_name.size ()) << endl;
Alexander Afanasyev707f6df2013-01-04 22:46:15 -0800174
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800175 item.set_parent_device_name (parent_device_name);
176 item.set_parent_seq_no (parent_seq_no);
177 }
178
179 // assign name to the action, serialize action, and create content object
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800180
181 string item_msg;
182 item.SerializeToString (&item_msg);
183 Name actionName (m_localName);
184 actionName
185 .appendComp ("action")
186 .appendComp (m_sharedFolderName)
187 .appendComp (seq_no);
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800188
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800189 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
190 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
191
192 sqlite3_bind_blob (stmt, 14, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
193 sqlite3_bind_blob (stmt, 15, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
194
195 sqlite3_step (stmt);
196
Alexander Afanasyev433ecda2013-01-02 22:13:45 -0800197 sqlite3_finalize (stmt);
198
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800199 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800200}
201
202void
203ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile)
204{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800205 // not supported yet
206 BOOST_THROW_EXCEPTION (Error::Db ()
207 << errmsg_info_str ("Move operation is not yet supported"));
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800208}
209
210void
211ActionLog::AddActionDelete (const std::string &filename)
212{
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800213 sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
214
215 sqlite3_int64 version = 0;
216 sqlite3_int64 parent_device_id = -1;
217 string parent_device_name;
218 sqlite3_int64 parent_seq_no = -1;
219
220 sqlite3_int64 action_time = time (0);
221
222 tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename);
223 if (parent_device_id < 0) // no records exist or file was already deleted
224 {
225 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
226 return;
227 }
228
229 sqlite3_int64 seq_no = GetNextLocalSeqNo ();
230
231 sqlite3_stmt *stmt;
232 sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog "
233 "(device_id, seq_no, action, filename, version, action_timestamp, "
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800234 "parent_device_id, parent_seq_no, "
235 "action_name, action_content_object) "
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800236 "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch'),"
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800237 " ?, ?,"
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800238 " ?, ?)", -1, &stmt, 0);
239
240 sqlite3_bind_int64 (stmt, 1, m_localDeviceId);
241 sqlite3_bind_int64 (stmt, 2, seq_no);
242 sqlite3_bind_int (stmt, 3, 1);
243 sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT);
244 sqlite3_bind_int64 (stmt, 5, version);
245 sqlite3_bind_int64 (stmt, 6, action_time);
246
247 sqlite3_bind_int64 (stmt, 7, parent_device_id);
248 sqlite3_bind_int64 (stmt, 8, parent_seq_no);
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800249
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800250
251 ActionItem item;
252 item.set_action (ActionItem::UPDATE);
253 item.set_filename (filename);
254 item.set_version (version);
255 item.set_timestamp (action_time);
256 item.set_parent_device_name (parent_device_name);
257 item.set_parent_seq_no (parent_seq_no);
258
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800259 string item_msg;
260 item.SerializeToString (&item_msg);
261 Name actionName (m_localName);
262 actionName
263 .appendComp ("action")
264 .appendComp (m_sharedFolderName)
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800265 .appendComp (seq_no);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800266
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800267 Bytes actionData = m_ccnx->createContentObject (actionName, item_msg.c_str (), item_msg.size ());
268 CcnxCharbufPtr namePtr = actionName.toCcnxCharbuf ();
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800269
Alexander Afanasyevebf75182013-01-07 17:00:24 -0800270 sqlite3_bind_blob (stmt, 9, namePtr->buf (), namePtr->length (), SQLITE_TRANSIENT);
271 sqlite3_bind_blob (stmt, 10, &actionData[0], actionData.size (), SQLITE_TRANSIENT);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800272
273 sqlite3_step (stmt);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800274
275 // cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()),
276 // parent_device_name.size ()) << endl;
Alexander Afanasyev707f6df2013-01-04 22:46:15 -0800277
Alexander Afanasyevb6bc01a2013-01-02 23:34:20 -0800278 // assign name to the action, serialize action, and create content object
279
280 sqlite3_finalize (stmt);
281
282 sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0);
Alexander Afanasyeva199f972013-01-02 19:37:26 -0800283}
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800284
285
286void
287ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv)
288{
289 ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context));
290
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800291 if (argc != 11)
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800292 {
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800293 sqlite3_result_error (context, "``apply_action'' expects 11 arguments", -1);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800294 return;
295 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800296
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800297 // cout << "apply_function called with " << argc << endl;
298
299 // cout << "device_name: " << sqlite3_value_text (argv[0]) << endl;
300 // cout << "action: " << sqlite3_value_int (argv[1]) << endl;
301 // cout << "filename: " << sqlite3_value_text (argv[2]) << endl;
302
Alexander Afanasyevd09871f2013-01-04 22:36:37 -0800303 string device_name (reinterpret_cast<const char*> (sqlite3_value_blob (argv[0])), sqlite3_value_bytes (argv[0]));
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800304 sqlite3_int64 device_id = sqlite3_value_int64 (argv[1]);
305 sqlite3_int64 seq_no = sqlite3_value_int64 (argv[2]);
306 int action = sqlite3_value_int (argv[3]);
307 string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[4]));
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800308
309 if (action == 0) // update
310 {
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800311 Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5]));
312 time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6]));
313 time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7]));
314 time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8]));
315 int mode = sqlite3_value_int (argv[9]);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800316 int seg_num = sqlite3_value_int (argv[10]);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800317
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800318 cout << "Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800319
320 sqlite3_stmt *stmt;
321 sqlite3_prepare_v2 (the->m_db, "UPDATE FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800322 "SET "
323 "device_id=?, seq_no=?, "
324 "file_hash=?,"
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800325 "file_atime=datetime(?, 'unixepoch'),"
326 "file_mtime=datetime(?, 'unixepoch'),"
327 "file_ctime=datetime(?, 'unixepoch'),"
328 "file_chmod=? "
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800329 "file_seg_num=? "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800330 "WHERE type=0 AND filename=?", -1, &stmt, 0);
331
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800332 sqlite3_bind_int64 (stmt, 1, device_id);
333 sqlite3_bind_int64 (stmt, 2, seq_no);
334 sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
335 sqlite3_bind_int64 (stmt, 4, atime);
336 sqlite3_bind_int64 (stmt, 5, mtime);
337 sqlite3_bind_int64 (stmt, 6, ctime);
338 sqlite3_bind_int (stmt, 7, mode);
Alexander Afanasyev334aac82013-01-18 14:06:39 -0800339 sqlite3_bind_int (stmt, 8, seg_num);
340 sqlite3_bind_text (stmt, 9, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800341
342 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800343
344 // cout << sqlite3_errmsg (the->m_db) << endl;
345
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800346 sqlite3_finalize (stmt);
347
348 int affected_rows = sqlite3_changes (the->m_db);
349 if (affected_rows == 0) // file didn't exist
350 {
351 sqlite3_stmt *stmt;
352 sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState "
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800353 "(type,filename,device_id,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod) "
354 "VALUES (0, ?, ?, ?, ?, "
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800355 "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", -1, &stmt, 0);
356
357 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800358 sqlite3_bind_int64 (stmt, 2, device_id);
359 sqlite3_bind_int64 (stmt, 3, seq_no);
360 sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT);
361 sqlite3_bind_int64 (stmt, 5, atime);
362 sqlite3_bind_int64 (stmt, 6, mtime);
363 sqlite3_bind_int64 (stmt, 7, ctime);
364 sqlite3_bind_int (stmt, 8, mode);
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800365
366 sqlite3_step (stmt);
Alexander Afanasyev3c2b7282013-01-04 20:34:51 -0800367 // cout << sqlite3_errmsg (the->m_db) << endl;
Alexander Afanasyeva2f77e92013-01-03 22:46:52 -0800368 sqlite3_finalize (stmt);
369 }
370 }
371 else if (action == 1) // delete
372 {
373 sqlite3_stmt *stmt;
374 sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0);
375 sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC);
376
377 cout << "Delete " << filename << endl;
378
379 sqlite3_step (stmt);
380 sqlite3_finalize (stmt);
381 }
Alexander Afanasyevee7e6132013-01-03 20:03:14 -0800382
383 sqlite3_result_null (context);
384}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800385
386bool
387ActionLog::KnownFileState(const std::string &filename, const Hash &hash)
388{
389 sqlite3_stmt *stmt;
390 sqlite3_prepare_v2 (m_db, "SELECT * FROM FileState WHERE filename = ? AND file_hash = ?;", -1, &stmt, 0);
391 sqlite3_bind_text(stmt, 1, filename.c_str(), -1, SQLITE_STATIC);
392 sqlite3_bind_blob(stmt, 2, hash.GetHash (), hash.GetHashBytes (), SQLITE_STATIC);
393 if (sqlite3_step (stmt) == SQLITE_ROW)
394 {
395 return true;
396 }
397
398 return false;
399}