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