Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "dispatcher.hpp" |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 22 | #include "sync-core.hpp" |
| 23 | |
| 24 | #include "core/logging.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/util/string-helper.hpp> |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
| 29 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace chronoshare { |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 33 | _LOG_INIT(DumpDb); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 35 | using std::cout; |
| 36 | using std::cerr; |
| 37 | using std::endl; |
| 38 | using std::setw; |
| 39 | |
| 40 | namespace fs = boost::filesystem; |
| 41 | |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 42 | class StateLogDumper : public DbHelper |
| 43 | { |
| 44 | public: |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 45 | StateLogDumper(const fs::path& path) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | : DbHelper(path / ".chronoshare", "sync-log.db") |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 51 | DumpState() |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 52 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | sqlite3_stmt* stmt; |
| 54 | sqlite3_prepare_v2(m_db, |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 55 | "SELECT hash(device_name, seq_no) FROM(SELECT * FROM SyncNodes ORDER BY device_name)", |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | -1, &stmt, 0); |
| 57 | sqlite3_step(stmt); |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 58 | ndn::Buffer hash(sqlite3_column_blob(stmt, 0), sqlite3_column_bytes(stmt, 0)); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | sqlite3_prepare_v2(m_db, "SELECT device_name, seq_no, last_known_locator, last_update " |
| 62 | " FROM SyncNodes " |
| 63 | " ORDER BY device_name", |
| 64 | -1, &stmt, 0); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 65 | |
| 66 | cout.setf(std::ios::left, std::ios::adjustfield); |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 67 | cout << ">> SYNC NODES(" << toHex(hash).substr(0, 8) << ") <<" << endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | cout << "====================================================================================" |
| 69 | << endl; |
| 70 | cout << setw(30) << "device_name" |
| 71 | << " | seq_no | " << setw(20) << "locator" |
| 72 | << " | last_update " << endl; |
| 73 | cout << "====================================================================================" |
| 74 | << endl; |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 77 | cout << setw(30) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 78 | << (Name(Block(sqlite3_column_blob(stmt, 0), (sqlite3_column_bytes(stmt, 0))))).toUri() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | << " | "; // device_name |
| 80 | cout << setw(6) << sqlite3_column_int64(stmt, 1) << " | "; // seq_no |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 81 | cout << setw(20); |
| 82 | if (sqlite3_column_bytes(stmt, 2) > 0) { |
| 83 | cout << (Name(Block(sqlite3_column_blob(stmt, 2), (sqlite3_column_bytes(stmt, 2))))).toUri(); |
| 84 | } |
| 85 | else { |
| 86 | cout << "NULL"; |
| 87 | } |
| 88 | cout << " | "; // locator |
| 89 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 90 | if (sqlite3_column_bytes(stmt, 3) > 0) { |
| 91 | cout << setw(10) << sqlite3_column_text(stmt, 3) << endl; |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 92 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | else { |
| 94 | cout << "unknown" << endl; |
| 95 | } |
| 96 | } |
| 97 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | DumpLog() |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | sqlite3_stmt* stmt; |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 104 | sqlite3_prepare_v2(m_db, "SELECT state_hash, last_update, state_id, last_update " |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | " FROM SyncLog " |
| 106 | " ORDER BY last_update", |
| 107 | -1, &stmt, 0); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 108 | |
| 109 | cout.setf(std::ios::left, std::ios::adjustfield); |
| 110 | cout << ">> SYNC LOG <<" << endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 111 | cout << "====================================================================================" |
| 112 | << endl; |
| 113 | cout << setw(10) << "state_hash" |
| 114 | << " | state details " << endl; |
| 115 | cout << "====================================================================================" |
| 116 | << endl; |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 119 | cout << setw(10) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 120 | << toHex(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 121 | sqlite3_column_bytes(stmt, 0)) |
| 122 | .substr(0, 8) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 123 | << " | "; // state hash |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 125 | sqlite3_stmt* stmt2; |
| 126 | sqlite3_prepare_v2(m_db, |
| 127 | "SELECT device_name, ss.seq_no " |
| 128 | " FROM SyncStateNodes ss JOIN SyncNodes sn ON ss.device_id = sn.device_id " |
| 129 | " WHERE state_id=? " |
| 130 | " ORDER BY device_name", |
| 131 | -1, &stmt2, 0); |
| 132 | _LOG_DEBUG_COND(sqlite3_errcode(m_db) != SQLITE_OK, sqlite3_errmsg(m_db)); |
| 133 | sqlite3_bind_int64(stmt2, 1, sqlite3_column_int64(stmt, 2)); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 134 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 135 | while (sqlite3_step(stmt2) == SQLITE_ROW) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 136 | cout << (Name(Block(sqlite3_column_blob(stmt2, 0), (sqlite3_column_bytes(stmt2, 0))))).toUri() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 137 | << "(" << sqlite3_column_int64(stmt2, 1) << "); "; |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 138 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 139 | |
| 140 | sqlite3_finalize(stmt2); |
| 141 | |
| 142 | cout << endl; |
| 143 | } |
| 144 | sqlite3_finalize(stmt); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 145 | } |
| 146 | }; |
| 147 | |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 148 | class ActionLogDumper : public DbHelper |
| 149 | { |
| 150 | public: |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 151 | ActionLogDumper(const fs::path& path) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 152 | : DbHelper(path / ".chronoshare", "action-log.db") |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 153 | { |
| 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 157 | Dump() |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 158 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 159 | sqlite3_stmt* stmt; |
| 160 | sqlite3_prepare_v2(m_db, |
| 161 | "SELECT device_name, seq_no, action, filename, version, file_hash, file_seg_num, parent_device_name, parent_seq_no " |
| 162 | " FROM ActionLog " |
| 163 | " ORDER BY action_timestamp", |
| 164 | -1, &stmt, 0); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 165 | |
| 166 | cout.setf(std::ios::left, std::ios::adjustfield); |
| 167 | cout << ">> ACTION LOG <<" << endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 168 | cout << "=============================================================================================================================================================================" |
| 169 | << endl; |
| 170 | cout << setw(30) << "device_name" |
| 171 | << " | seq_no | action |" << setw(40) << " filename " |
| 172 | << " | version | file_hash | seg_num | parent_device_name | parent_seq_no" |
| 173 | << endl; |
| 174 | cout << "=============================================================================================================================================================================" |
| 175 | << endl; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 176 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 177 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 178 | cout << setw(30) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 179 | << (Name(Block(sqlite3_column_blob(stmt, 0), (sqlite3_column_bytes(stmt, 0))))).toUri() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 180 | << " | "; // device_name |
| 181 | cout << setw(6) << sqlite3_column_int64(stmt, 1) << " | "; // seq_no |
| 182 | cout << setw(6) << (sqlite3_column_int(stmt, 2) == 0 ? "UPDATE" : "DELETE") << " | "; // action |
| 183 | cout << setw(40) << sqlite3_column_text(stmt, 3) << " | "; // filename |
| 184 | cout << setw(7) << sqlite3_column_int64(stmt, 4) << " | "; // version |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 185 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 186 | if (sqlite3_column_int(stmt, 2) == 0) { |
| 187 | cout << setw(10) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 188 | << toHex(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 5)), |
| 189 | sqlite3_column_bytes(stmt, 5)) |
| 190 | .substr(0, 8) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 191 | << " | "; |
| 192 | cout << setw(7) << sqlite3_column_int64(stmt, 6) << " | "; // seg_num |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 193 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 194 | else |
| 195 | cout << " | | "; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 197 | if (sqlite3_column_bytes(stmt, 7) > 0) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 198 | cout << setw(30) |
| 199 | << (Name(Block(sqlite3_column_blob(stmt, 7), (sqlite3_column_bytes(stmt, 7))))).toUri() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 200 | << " | "; // parent_device_name |
| 201 | cout << setw(5) << sqlite3_column_int64(stmt, 8); // seq_no |
| 202 | } |
| 203 | else |
| 204 | cout << setw(30) << " " |
| 205 | << " | "; |
| 206 | cout << endl; |
| 207 | } |
| 208 | |
| 209 | sqlite3_finalize(stmt); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 210 | } |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 211 | |
| 212 | void |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 213 | DumpActionData(const ndn::Name& deviceName, int64_t seqno) |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 214 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 215 | sqlite3_stmt* stmt; |
| 216 | sqlite3_prepare_v2(m_db, |
| 217 | "SELECT action_content_object, action_name FROM ActionLog WHERE device_name = ? and seq_no = ?", |
| 218 | -1, &stmt, 0); |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 219 | const ndn::Block nameBlock = deviceName.wireEncode(); |
| 220 | sqlite3_bind_blob(stmt, 1, nameBlock.wire(), nameBlock.size(), SQLITE_STATIC); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 221 | sqlite3_bind_int64(stmt, 2, seqno); |
| 222 | cout << "Dumping action data for: [" << deviceName << ", " << seqno << "]" << endl; |
| 223 | if (sqlite3_step(stmt) == SQLITE_ROW) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 224 | ndn::shared_ptr<ndn::Data> data = ndn::make_shared<ndn::Data>( |
| 225 | ndn::Block(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 0)), |
| 226 | sqlite3_column_bytes(stmt, 0))); |
| 227 | ndn::Name actionName = |
| 228 | Name(Block(sqlite3_column_blob(stmt, 1), (sqlite3_column_bytes(stmt, 1)))); |
| 229 | if (data) { |
| 230 | ActionItemPtr action = deserializeMsg<ActionItem>( |
| 231 | ndn::Buffer(data->getContent().value(), data->getContent().value_size())); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 232 | if (action) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 233 | cout << "Action data size : " << data->getContent().size() << endl; |
Zhenkai Zhu | 555c4f1 | 2013-02-13 16:57:36 -0800 | [diff] [blame] | 234 | cout << "Action data name : " << actionName << endl; |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 235 | std::string type = action->action() == ActionItem::UPDATE ? "UPDATE" : "DELETE"; |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 236 | cout << "Action Type = " << type << endl; |
| 237 | cout << "Timestamp = " << action->timestamp() << endl; |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 238 | std::string filename = action->filename(); |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 239 | cout << "Filename = " << filename << endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 240 | if (action->has_seg_num()) { |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 241 | cout << "Segment number = " << action->seg_num() << endl; |
| 242 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 243 | if (action->has_file_hash()) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 244 | cout << "File hash = " |
| 245 | << toHex(reinterpret_cast<const uint8_t*>(action->file_hash().data()), |
| 246 | action->file_hash().size()) |
| 247 | .substr(0, 8) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 248 | << endl; |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 249 | } |
| 250 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 251 | else { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 252 | cerr << "Error! Failed to parse action from data! " << endl; |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 255 | else { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 256 | cerr << "Error! Invalid data! " << endl; |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 257 | } |
| 258 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 259 | else { |
Zhenkai Zhu | ed457c3 | 2013-02-13 15:51:48 -0800 | [diff] [blame] | 260 | cerr << "Error! Can not find the requested action" << endl; |
| 261 | } |
| 262 | sqlite3_finalize(stmt); |
| 263 | } |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | class FileStateDumper : public DbHelper |
| 267 | { |
| 268 | public: |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 269 | FileStateDumper(const fs::path& path) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 270 | : DbHelper(path / ".chronoshare", "file-state.db") |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 271 | { |
| 272 | } |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 273 | |
| 274 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 275 | Dump() |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 276 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 277 | sqlite3_stmt* stmt; |
| 278 | sqlite3_prepare_v2(m_db, |
| 279 | "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,directory,is_complete " |
| 280 | " FROM FileState " |
| 281 | " WHERE type = 0 ORDER BY filename", |
| 282 | -1, &stmt, 0); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 283 | |
| 284 | cout.setf(std::ios::left, std::ios::adjustfield); |
| 285 | cout << ">> FILE STATE <<" << endl; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 286 | cout << "===================================================================================================================================" |
| 287 | << endl; |
| 288 | cout << "filename | device_name | seq_no | file_hash | seg_num | directory | is_complete" |
| 289 | << endl; |
| 290 | cout << "===================================================================================================================================" |
| 291 | << endl; |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 292 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 293 | while (sqlite3_step(stmt) == SQLITE_ROW) { |
| 294 | cout << setw(40) << sqlite3_column_text(stmt, 0) << " | "; |
| 295 | cout << setw(30) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 296 | << Name(Block(sqlite3_column_blob(stmt, 1), (sqlite3_column_bytes(stmt, 1)))).toUri() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 297 | << " | "; |
| 298 | cout << setw(6) << sqlite3_column_int64(stmt, 2) << " | "; |
| 299 | cout << setw(10) |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 300 | << toHex(reinterpret_cast<const uint8_t*>(sqlite3_column_blob(stmt, 3)), |
| 301 | sqlite3_column_bytes(stmt, 3)) |
| 302 | .substr(0, 8) |
| 303 | << " | "; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 304 | cout << setw(6) << sqlite3_column_int64(stmt, 6) << " | "; |
| 305 | if (sqlite3_column_bytes(stmt, 7) == 0) |
| 306 | cout << setw(20) << "<NULL>" |
| 307 | << " | "; |
| 308 | else |
| 309 | cout << setw(20) << sqlite3_column_text(stmt, 7) << " | "; |
Alexander Afanasyev | 38826ce | 2013-01-31 16:31:42 -0800 | [diff] [blame] | 310 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 311 | if (sqlite3_column_int(stmt, 8) == 0) |
| 312 | cout << setw(20) << "no" << endl; |
| 313 | else |
| 314 | cout << setw(20) << "yes" << endl; |
| 315 | } |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 316 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 317 | sqlite3_finalize(stmt); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 318 | } |
| 319 | }; |
| 320 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 321 | int |
| 322 | main(int argc, char* argv[]) |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 323 | { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 324 | if (argc != 3 && !(argc == 5 && std::string(argv[1]) == "action")) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 325 | cerr << "Usage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl; |
| 326 | cerr << " or: ./dump-db action <path-to-shared-folder> <device-name> <seq-no>" << endl; |
| 327 | return 1; |
| 328 | } |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 329 | |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 330 | std::string type = argv[1]; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 331 | if (type == "state") { |
| 332 | StateLogDumper dumper(argv[2]); |
| 333 | dumper.DumpState(); |
| 334 | dumper.DumpLog(); |
| 335 | } |
| 336 | else if (type == "action") { |
| 337 | ActionLogDumper dumper(argv[2]); |
| 338 | if (argc == 5) { |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 339 | dumper.DumpActionData(std::string(argv[3]), boost::lexical_cast<int64_t>(argv[4])); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 340 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 341 | else { |
| 342 | dumper.Dump(); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 343 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 344 | } |
| 345 | else if (type == "file") { |
| 346 | FileStateDumper dumper(argv[2]); |
| 347 | dumper.Dump(); |
| 348 | } |
| 349 | else if (type == "all") { |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 350 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 351 | StateLogDumper dumper(argv[2]); |
| 352 | dumper.DumpState(); |
| 353 | dumper.DumpLog(); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 354 | } |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 355 | |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 356 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 357 | ActionLogDumper dumper(argv[2]); |
| 358 | dumper.Dump(); |
Alexander Afanasyev | 24b449f | 2013-02-06 13:27:59 -0800 | [diff] [blame] | 359 | } |
| 360 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 361 | { |
| 362 | FileStateDumper dumper(argv[2]); |
| 363 | dumper.Dump(); |
| 364 | } |
| 365 | } |
| 366 | else { |
| 367 | cerr << "ERROR: Wrong database type" << endl; |
| 368 | cerr << "\nUsage: ./dump-db state|action|file|all <path-to-shared-folder>" << endl; |
| 369 | return 1; |
| 370 | } |
| 371 | |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 372 | return 0; |
| 373 | } |
Alexander Afanasyev | 61ced27 | 2015-05-31 16:25:16 -0700 | [diff] [blame] | 374 | |
| 375 | } // namespace chronoshare |
| 376 | } // namespace ndn |
| 377 | |
| 378 | int |
| 379 | main(int argc, char* argv[]) |
| 380 | { |
| 381 | return ndn::chronoshare::main(argc, argv); |
| 382 | } |