Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 23 | #include <action-item.pb.h> |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 24 | |
| 25 | using namespace boost; |
| 26 | using namespace std; |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame^] | 27 | using namespace Ccnx; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame^] | 29 | ActionLog::ActionLog (Ccnx::CcnxWrapperPtr ccnx, const std::string &path, const std::string &localName, const std::string &sharedFolder) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 30 | : SyncLog (path, localName) |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame^] | 31 | , m_ccnx (ccnx) |
| 32 | , m_sharedFolderName (sharedFolder) |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 33 | { |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 34 | 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 Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 42 | } |
| 43 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 44 | tuple<sqlite3_int64, sqlite3_int64, sqlite3_int64, string> |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 45 | ActionLog::GetExistingRecord (const std::string &filename) |
| 46 | { |
| 47 | // check if something already exists |
| 48 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 49 | 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 Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 51 | "WHERE filename=? ORDER BY a.version DESC LIMIT 1", -1, &stmt, 0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 52 | |
| 53 | if (res != SQLITE_OK) |
| 54 | { |
| 55 | BOOST_THROW_EXCEPTION (Error::Db () |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 56 | << errmsg_info_str ("Some error with GetExistingRecord")); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 59 | // 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 Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 65 | sqlite3_bind_text (stmt, 1, filename.c_str (), filename.size (), SQLITE_STATIC); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 66 | if (sqlite3_step (stmt) == SQLITE_ROW) |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 67 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 68 | version = sqlite3_column_int64 (stmt, 0) + 1; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 70 | 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 Afanasyev | 707f6df | 2013-01-04 22:46:15 -0800 | [diff] [blame] | 74 | parent_device_name = string(reinterpret_cast<const char*> (sqlite3_column_blob (stmt, 4)), sqlite3_column_bytes (stmt, 4)); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 76 | } |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 78 | sqlite3_finalize (stmt); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 79 | return make_tuple (version, parent_device_id, parent_seq_no, parent_device_name); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 82 | // local add action. remote action is extracted from content object |
| 83 | void |
| 84 | ActionLog::AddActionUpdate (const std::string &filename, |
| 85 | const Hash &hash, |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 86 | time_t atime, time_t mtime, time_t ctime, |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 87 | int mode) |
| 88 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 89 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 90 | |
| 91 | sqlite3_int64 seq_no = GetNextLocalSeqNo (); |
| 92 | sqlite3_int64 version = 0; |
| 93 | sqlite3_int64 parent_device_id = -1; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 94 | string parent_device_name; |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 95 | sqlite3_int64 parent_seq_no = -1; |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 97 | sqlite3_int64 action_time = time (0); |
| 98 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 99 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 100 | |
| 101 | sqlite3_stmt *stmt; |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 102 | int res = sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 103 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 104 | "file_hash, file_atime, file_mtime, file_ctime, file_chmod, " |
| 105 | "parent_device_id, parent_seq_no) " |
| 106 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 107 | " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
| 108 | " ?, ?);", -1, &stmt, 0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 110 | // cout << "INSERT INTO ActionLog " |
| 111 | // "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 112 | // "file_hash, file_atime, file_mtime, file_ctime, file_chmod, " |
| 113 | // "parent_device_id, parent_seq_no) " |
| 114 | // "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 115 | // " ?, datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?," |
| 116 | // " ?, ?)" << endl; |
| 117 | |
| 118 | if (res != SQLITE_OK) |
| 119 | { |
| 120 | BOOST_THROW_EXCEPTION (Error::Db () |
| 121 | << errmsg_info_str (sqlite3_errmsg (m_db)) |
| 122 | ); |
| 123 | // << errmsg_info_str ("Some error with prepare AddActionUpdate")); |
| 124 | } |
| 125 | |
| 126 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 127 | sqlite3_bind_int64 (stmt, 1, m_localDeviceId); |
| 128 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 129 | sqlite3_bind_int (stmt, 3, 0); |
| 130 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 131 | sqlite3_bind_int64 (stmt, 5, version); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 132 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 133 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 134 | sqlite3_bind_blob (stmt, 7, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 135 | |
| 136 | sqlite3_bind_int64 (stmt, 8, atime); |
| 137 | sqlite3_bind_int64 (stmt, 9, mtime); |
| 138 | sqlite3_bind_int64 (stmt, 10, ctime); |
| 139 | sqlite3_bind_int (stmt, 11, mode); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 141 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 142 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 143 | sqlite3_bind_int64 (stmt, 12, parent_device_id); |
| 144 | sqlite3_bind_int64 (stmt, 13, parent_seq_no); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 145 | } |
| 146 | else |
| 147 | { |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 148 | sqlite3_bind_null (stmt, 12); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 149 | sqlite3_bind_null (stmt, 13); |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | sqlite3_step (stmt); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 153 | |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 154 | // missing part: creating ContentObject for the action !!! |
| 155 | |
| 156 | ActionItem item; |
| 157 | item.set_action (ActionItem::UPDATE); |
| 158 | item.set_filename (filename); |
| 159 | item.set_version (version); |
| 160 | item.set_timestamp (action_time); |
| 161 | item.set_file_hash (hash.GetHash (), hash.GetHashBytes ()); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 162 | item.set_atime (atime); |
| 163 | item.set_mtime (mtime); |
| 164 | item.set_ctime (ctime); |
| 165 | item.set_mode (mode); |
| 166 | |
| 167 | if (parent_device_id > 0 && parent_seq_no > 0) |
| 168 | { |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame^] | 169 | cout << Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()), |
| 170 | parent_device_name.size ()) << endl; |
Alexander Afanasyev | 707f6df | 2013-01-04 22:46:15 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 172 | item.set_parent_device_name (parent_device_name); |
| 173 | item.set_parent_seq_no (parent_seq_no); |
| 174 | } |
| 175 | |
| 176 | // assign name to the action, serialize action, and create content object |
Alexander Afanasyev | 433ecda | 2013-01-02 22:13:45 -0800 | [diff] [blame] | 177 | |
| 178 | sqlite3_finalize (stmt); |
| 179 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 180 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void |
| 184 | ActionLog::AddActionMove (const std::string &oldFile, const std::string &newFile) |
| 185 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 186 | // not supported yet |
| 187 | BOOST_THROW_EXCEPTION (Error::Db () |
| 188 | << errmsg_info_str ("Move operation is not yet supported")); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | void |
| 192 | ActionLog::AddActionDelete (const std::string &filename) |
| 193 | { |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 194 | sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0); |
| 195 | |
| 196 | sqlite3_int64 version = 0; |
| 197 | sqlite3_int64 parent_device_id = -1; |
| 198 | string parent_device_name; |
| 199 | sqlite3_int64 parent_seq_no = -1; |
| 200 | |
| 201 | sqlite3_int64 action_time = time (0); |
| 202 | |
| 203 | tie (version, parent_device_id, parent_seq_no, parent_device_name) = GetExistingRecord (filename); |
| 204 | if (parent_device_id < 0) // no records exist or file was already deleted |
| 205 | { |
| 206 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | sqlite3_int64 seq_no = GetNextLocalSeqNo (); |
| 211 | |
| 212 | sqlite3_stmt *stmt; |
| 213 | sqlite3_prepare_v2 (m_db, "INSERT INTO ActionLog " |
| 214 | "(device_id, seq_no, action, filename, version, action_timestamp, " |
| 215 | "parent_device_id, parent_seq_no) " |
| 216 | "VALUES (?, ?, ?, ?, ?, datetime(?, 'unixepoch')," |
| 217 | " ?, ?)", -1, &stmt, 0); |
| 218 | |
| 219 | sqlite3_bind_int64 (stmt, 1, m_localDeviceId); |
| 220 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 221 | sqlite3_bind_int (stmt, 3, 1); |
| 222 | sqlite3_bind_text (stmt, 4, filename.c_str (), filename.size (), SQLITE_TRANSIENT); |
| 223 | sqlite3_bind_int64 (stmt, 5, version); |
| 224 | sqlite3_bind_int64 (stmt, 6, action_time); |
| 225 | |
| 226 | sqlite3_bind_int64 (stmt, 7, parent_device_id); |
| 227 | sqlite3_bind_int64 (stmt, 8, parent_seq_no); |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 229 | |
| 230 | ActionItem item; |
| 231 | item.set_action (ActionItem::UPDATE); |
| 232 | item.set_filename (filename); |
| 233 | item.set_version (version); |
| 234 | item.set_timestamp (action_time); |
| 235 | item.set_parent_device_name (parent_device_name); |
| 236 | item.set_parent_seq_no (parent_seq_no); |
| 237 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame^] | 238 | string item_msg; |
| 239 | item.SerializeToString (&item_msg); |
| 240 | Name actionName (m_localName); |
| 241 | actionName |
| 242 | .appendComp ("action") |
| 243 | .appendComp (m_sharedFolderName) |
| 244 | .appendComp (reinterpret_cast<void*> (seq_no), sizeof (seq_no)); |
| 245 | |
| 246 | // Bytes actionData = m_ccnx->createContentObject (?name, item_msg.c_str (), item_msg.size ()); |
| 247 | |
| 248 | |
| 249 | // sqlite3_bind_blob (stmt, 10, item_msg.c_str (), item_msg.size (), SQLITE_TRANSIENT); |
| 250 | |
| 251 | sqlite3_step (stmt); |
| 252 | |
| 253 | |
| 254 | |
| 255 | // cout << Ccnx::Name (reinterpret_cast<const unsigned char *> (parent_device_name.c_str ()), |
| 256 | // parent_device_name.size ()) << endl; |
Alexander Afanasyev | 707f6df | 2013-01-04 22:46:15 -0800 | [diff] [blame] | 257 | |
Alexander Afanasyev | b6bc01a | 2013-01-02 23:34:20 -0800 | [diff] [blame] | 258 | // assign name to the action, serialize action, and create content object |
| 259 | |
| 260 | sqlite3_finalize (stmt); |
| 261 | |
| 262 | sqlite3_exec (m_db, "END TRANSACTION;", 0,0,0); |
Alexander Afanasyev | a199f97 | 2013-01-02 19:37:26 -0800 | [diff] [blame] | 263 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 264 | |
| 265 | |
| 266 | void |
| 267 | ActionLog::apply_action_xFun (sqlite3_context *context, int argc, sqlite3_value **argv) |
| 268 | { |
| 269 | ActionLog *the = reinterpret_cast<ActionLog*> (sqlite3_user_data (context)); |
| 270 | |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 271 | if (argc != 10) |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 272 | { |
| 273 | sqlite3_result_error (context, "``apply_action'' expects 8 arguments", -1); |
| 274 | return; |
| 275 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 276 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 277 | // cout << "apply_function called with " << argc << endl; |
| 278 | |
| 279 | // cout << "device_name: " << sqlite3_value_text (argv[0]) << endl; |
| 280 | // cout << "action: " << sqlite3_value_int (argv[1]) << endl; |
| 281 | // cout << "filename: " << sqlite3_value_text (argv[2]) << endl; |
| 282 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 283 | string device_name (reinterpret_cast<const char*> (sqlite3_value_blob (argv[0])), sqlite3_value_bytes (argv[0])); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 284 | sqlite3_int64 device_id = sqlite3_value_int64 (argv[1]); |
| 285 | sqlite3_int64 seq_no = sqlite3_value_int64 (argv[2]); |
| 286 | int action = sqlite3_value_int (argv[3]); |
| 287 | string filename = reinterpret_cast<const char*> (sqlite3_value_text (argv[4])); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 288 | |
| 289 | if (action == 0) // update |
| 290 | { |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 291 | Hash hash (sqlite3_value_blob (argv[5]), sqlite3_value_bytes (argv[5])); |
| 292 | time_t atime = static_cast<time_t> (sqlite3_value_int64 (argv[6])); |
| 293 | time_t mtime = static_cast<time_t> (sqlite3_value_int64 (argv[7])); |
| 294 | time_t ctime = static_cast<time_t> (sqlite3_value_int64 (argv[8])); |
| 295 | int mode = sqlite3_value_int (argv[9]); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 296 | |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 297 | cout << "Update " << filename << " " << atime << " " << mtime << " " << ctime << " " << hash << endl; |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 298 | |
| 299 | sqlite3_stmt *stmt; |
| 300 | sqlite3_prepare_v2 (the->m_db, "UPDATE FileState " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 301 | "SET " |
| 302 | "device_id=?, seq_no=?, " |
| 303 | "file_hash=?," |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 304 | "file_atime=datetime(?, 'unixepoch')," |
| 305 | "file_mtime=datetime(?, 'unixepoch')," |
| 306 | "file_ctime=datetime(?, 'unixepoch')," |
| 307 | "file_chmod=? " |
| 308 | "WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 309 | |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 310 | sqlite3_bind_int64 (stmt, 1, device_id); |
| 311 | sqlite3_bind_int64 (stmt, 2, seq_no); |
| 312 | sqlite3_bind_blob (stmt, 3, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 313 | sqlite3_bind_int64 (stmt, 4, atime); |
| 314 | sqlite3_bind_int64 (stmt, 5, mtime); |
| 315 | sqlite3_bind_int64 (stmt, 6, ctime); |
| 316 | sqlite3_bind_int (stmt, 7, mode); |
| 317 | sqlite3_bind_text (stmt, 8, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 318 | |
| 319 | sqlite3_step (stmt); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 320 | |
| 321 | // cout << sqlite3_errmsg (the->m_db) << endl; |
| 322 | |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 323 | sqlite3_finalize (stmt); |
| 324 | |
| 325 | int affected_rows = sqlite3_changes (the->m_db); |
| 326 | if (affected_rows == 0) // file didn't exist |
| 327 | { |
| 328 | sqlite3_stmt *stmt; |
| 329 | sqlite3_prepare_v2 (the->m_db, "INSERT INTO FileState " |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 330 | "(type,filename,device_id,seq_no,file_hash,file_atime,file_mtime,file_ctime,file_chmod) " |
| 331 | "VALUES (0, ?, ?, ?, ?, " |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 332 | "datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), datetime(?, 'unixepoch'), ?)", -1, &stmt, 0); |
| 333 | |
| 334 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_TRANSIENT); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 335 | sqlite3_bind_int64 (stmt, 2, device_id); |
| 336 | sqlite3_bind_int64 (stmt, 3, seq_no); |
| 337 | sqlite3_bind_blob (stmt, 4, hash.GetHash (), hash.GetHashBytes (), SQLITE_TRANSIENT); |
| 338 | sqlite3_bind_int64 (stmt, 5, atime); |
| 339 | sqlite3_bind_int64 (stmt, 6, mtime); |
| 340 | sqlite3_bind_int64 (stmt, 7, ctime); |
| 341 | sqlite3_bind_int (stmt, 8, mode); |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 342 | |
| 343 | sqlite3_step (stmt); |
Alexander Afanasyev | 3c2b728 | 2013-01-04 20:34:51 -0800 | [diff] [blame] | 344 | // cout << sqlite3_errmsg (the->m_db) << endl; |
Alexander Afanasyev | a2f77e9 | 2013-01-03 22:46:52 -0800 | [diff] [blame] | 345 | sqlite3_finalize (stmt); |
| 346 | } |
| 347 | } |
| 348 | else if (action == 1) // delete |
| 349 | { |
| 350 | sqlite3_stmt *stmt; |
| 351 | sqlite3_prepare_v2 (the->m_db, "DELETE FROM FileState WHERE type=0 AND filename=?", -1, &stmt, 0); |
| 352 | sqlite3_bind_text (stmt, 1, filename.c_str (), -1, SQLITE_STATIC); |
| 353 | |
| 354 | cout << "Delete " << filename << endl; |
| 355 | |
| 356 | sqlite3_step (stmt); |
| 357 | sqlite3_finalize (stmt); |
| 358 | } |
Alexander Afanasyev | ee7e613 | 2013-01-03 20:03:14 -0800 | [diff] [blame] | 359 | |
| 360 | sqlite3_result_null (context); |
| 361 | } |