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 | a9369b4 | 2017-01-11 11:58:00 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -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 | 026eaf3 | 2013-02-23 16:37:14 -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 | 026eaf3 | 2013-02-23 16:37:14 -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 | 026eaf3 | 2013-02-23 16:37:14 -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 | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 21 | #include "state-server.hpp" |
| 22 | #include "core/logging.hpp" |
| 23 | |
| 24 | #include <ndn-cxx/util/digest.hpp> |
| 25 | #include <ndn-cxx/util/string-helper.hpp> |
| 26 | |
| 27 | #include <boost/asio/io_service.hpp> |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 28 | #include <boost/date_time/posix_time/posix_time.hpp> |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 29 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <boost/lexical_cast.hpp> |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace chronoshare { |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 35 | _LOG_INIT(StateServer); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 36 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 37 | namespace fs = boost::filesystem; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 38 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 39 | StateServer::StateServer(Face& face, ActionLogPtr actionLog, const fs::path& rootDir, |
| 40 | const Name& userName, const std::string& sharedFolderName, |
| 41 | const name::Component& appName, ObjectManager& objectManager, |
| 42 | KeyChain& keyChain, time::milliseconds freshness) |
| 43 | : m_face(face) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 44 | , m_actionLog(actionLog) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 45 | , m_objectManager(objectManager) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 46 | , m_rootDir(rootDir) |
| 47 | , m_freshness(freshness) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 48 | , m_userName(userName) |
| 49 | , m_sharedFolderName(sharedFolderName) |
| 50 | , m_appName(appName) |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 51 | , m_keyChain(keyChain) |
| 52 | , m_ioService(m_face.getIoService()) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 53 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 54 | // may be later /localhop should be replaced with /%C1.M.S.localhost |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 55 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 56 | // <PREFIX_INFO> = /localhop/<user's-device-name>/"chronoshare"/"info" |
| 57 | m_PREFIX_INFO = Name("/localhop"); |
| 58 | m_PREFIX_INFO.append(m_userName).append(m_appName).append(m_sharedFolderName).append("info"); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 59 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 60 | // <PREFIX_CMD> = /localhop/<user's-device-name>/"chronoshare"/"cmd" |
| 61 | m_PREFIX_CMD = Name("/localhop"); |
| 62 | m_PREFIX_CMD.append(m_userName).append(m_appName).append(m_sharedFolderName).append("cmd"); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | registerPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | StateServer::~StateServer() |
| 68 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 69 | deregisterPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | StateServer::registerPrefixes() |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 74 | { |
| 75 | // currently supporting limited number of command. |
| 76 | // will be extended to support all planned commands later |
| 77 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 78 | // <PREFIX_INFO>/"actions"/"all"/<segment> get list of all actions |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 79 | Name actionsFolder = Name(m_PREFIX_INFO); |
| 80 | actionsFolder.append("actions").append("folder"); |
| 81 | actionsFolderId = |
| 82 | m_face.setInterestFilter(InterestFilter(actionsFolder), |
| 83 | bind(&StateServer::info_actions_folder, this, _1, _2), |
| 84 | RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback()); |
| 85 | |
| 86 | _LOG_DEBUG("Register Prefix: " << actionsFolder); |
| 87 | |
| 88 | Name actionsFile = Name(m_PREFIX_INFO); |
| 89 | actionsFile.append("actions").append("file"); |
| 90 | actionsFileId = |
| 91 | m_face.setInterestFilter(InterestFilter(actionsFile), |
| 92 | bind(&StateServer::info_actions_file, this, _1, _2), |
| 93 | RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback()); |
| 94 | |
| 95 | _LOG_DEBUG("Register Prefix: " << actionsFile); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 97 | // <PREFIX_INFO>/"filestate"/"all"/<segment> |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 98 | Name filesFolder = Name(m_PREFIX_INFO); |
| 99 | filesFolder.append("files").append("folder"); |
| 100 | filesFolderId = |
| 101 | m_face.setInterestFilter(InterestFilter(filesFolder), |
| 102 | bind(&StateServer::info_files_folder, this, _1, _2), |
| 103 | RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback()); |
| 104 | |
| 105 | _LOG_DEBUG("Register Prefix: " << filesFolder); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 106 | |
| 107 | // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash> |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 108 | Name restoreFile = Name(m_PREFIX_CMD); |
| 109 | restoreFile.append("restore").append("file"); |
| 110 | restoreFileId = |
| 111 | m_face.setInterestFilter(InterestFilter(restoreFile), |
| 112 | bind(&StateServer::cmd_restore_file, this, _1, _2), |
| 113 | RegisterPrefixSuccessCallback(), RegisterPrefixFailureCallback()); |
| 114 | |
| 115 | _LOG_DEBUG("Register Prefix: " << restoreFile); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 119 | StateServer::deregisterPrefixes() |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 120 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 121 | m_face.unsetInterestFilter(actionsFolderId); |
| 122 | m_face.unsetInterestFilter(actionsFileId); |
| 123 | m_face.unsetInterestFilter(filesFolderId); |
| 124 | m_face.unsetInterestFilter(restoreFileId); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 127 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 128 | StateServer::formatActionJson(json_spirit::Array& actions, const Name& name, sqlite3_int64 seq_no, |
| 129 | const ActionItem& action) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 130 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | /* |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 132 | * { |
| 133 | * "id": { |
| 134 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 135 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 136 | * }, |
| 137 | * "timestamp": "<ACTION-TIMESTAMP>", |
| 138 | * "filename": "<FILENAME>", |
| 139 | * |
| 140 | * "action": "UPDATE | DELETE", |
| 141 | * |
| 142 | * // only if update |
| 143 | * "update": { |
| 144 | * "hash": "<FILE-HASH>", |
| 145 | * "timestamp": "<FILE-TIMESTAMP>", |
| 146 | * "chmod": "<FILE-MODE>", |
| 147 | * "segNum": "<NUMBER-OF-SEGMENTS(~file size)>" |
| 148 | * }, |
| 149 | * |
| 150 | * // if parent_device_name is set |
| 151 | * "parentId": { |
| 152 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 153 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 154 | * } |
| 155 | * } |
| 156 | */ |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 157 | |
| 158 | using namespace json_spirit; |
| 159 | using namespace boost::posix_time; |
| 160 | |
| 161 | Object json; |
| 162 | Object id; |
| 163 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 164 | id.push_back(Pair("userName", boost::lexical_cast<std::string>(name))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 165 | id.push_back(Pair("seqNo", static_cast<int64_t>(seq_no))); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 167 | json.push_back(Pair("id", id)); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 169 | json.push_back(Pair("timestamp", to_iso_extended_string(from_time_t(action.timestamp())))); |
| 170 | json.push_back(Pair("filename", action.filename())); |
| 171 | json.push_back(Pair("version", action.version())); |
| 172 | json.push_back(Pair("action", (action.action() == 0) ? "UPDATE" : "DELETE")); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 174 | if (action.action() == 0) { |
| 175 | Object update; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 176 | const Buffer hash(action.file_hash().c_str(), action.file_hash().size()); |
| 177 | update.push_back(Pair("hash", toHex(hash))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 178 | update.push_back(Pair("timestamp", to_iso_extended_string(from_time_t(action.mtime())))); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 179 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 180 | std::ostringstream chmod; |
| 181 | chmod << std::setbase(8) << std::setfill('0') << std::setw(4) << action.mode(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 182 | update.push_back(Pair("chmod", chmod.str())); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 183 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 184 | update.push_back(Pair("segNum", action.seg_num())); |
| 185 | json.push_back(Pair("update", update)); |
| 186 | } |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 187 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 188 | if (action.has_parent_device_name()) { |
| 189 | Object parentId; |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 190 | Name parent_device_name(action.parent_device_name()); |
| 191 | id.push_back(Pair("userName", boost::lexical_cast<std::string>(parent_device_name))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 192 | id.push_back(Pair("seqNo", action.parent_seq_no())); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 194 | json.push_back(Pair("parentId", parentId)); |
| 195 | } |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 196 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 197 | actions.push_back(json); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 201 | StateServer::info_actions_folder(const InterestFilter& interesFilter, const Interest& interestTrue) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 202 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 203 | Name interest = interestTrue.getName(); |
| 204 | _LOG_DEBUG(">> info_actions_folder: " << interest); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 205 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 206 | _LOG_DEBUG("Invalid interest: " << interest); |
| 207 | return; |
| 208 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 209 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 210 | m_ioService.post(bind(&StateServer::info_actions_fileOrFolder_Execute, this, interest, true)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 214 | StateServer::info_actions_file(const InterestFilter& interesFilter, const Interest& interestTrue) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 215 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 216 | Name interest = interestTrue.getName(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 217 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 218 | _LOG_DEBUG("Invalid interest: " << interest); |
| 219 | return; |
| 220 | } |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 221 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 222 | _LOG_DEBUG(">> info_actions_file: " << interest); |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 223 | m_ioService.post(bind(&StateServer::info_actions_fileOrFolder_Execute, this, interest, false)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 226 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 227 | StateServer::info_actions_fileOrFolder_Execute(const Name& interest, bool isFolder /* = true*/) |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 228 | { |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 229 | // <PREFIX_INFO>/"actions"/"folder|file"/<folder|file>/<offset> get list of all actions |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 230 | if (interest.size() < 1) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 231 | // ignore any unexpected interests and errors |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 232 | _LOG_ERROR("empty interest name"); |
| 233 | return; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 234 | } |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 235 | uint64_t offset = interest.get(-1).toNumber(); |
| 236 | |
| 237 | /// @todo !!! add security checking |
| 238 | |
| 239 | std::string fileOrFolderName; |
| 240 | if (interest.size() - m_PREFIX_INFO.size() == 4) |
| 241 | fileOrFolderName = interest.get(-2).toUri(); |
| 242 | else // == 3 |
| 243 | fileOrFolderName = ""; |
| 244 | /* |
| 245 | * { |
| 246 | * "actions": [ |
| 247 | * ... |
| 248 | * ], |
| 249 | * |
| 250 | * // only if there are more actions available |
| 251 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>" |
| 252 | * } |
| 253 | */ |
| 254 | |
| 255 | _LOG_DEBUG("info_actions_fileOrFolder_Execute! offset: " << offset); |
| 256 | using namespace json_spirit; |
| 257 | Object json; |
| 258 | |
| 259 | Array actions; |
| 260 | bool more; |
| 261 | if (isFolder) { |
| 262 | more = m_actionLog->LookupActionsInFolderRecursively(bind(StateServer::formatActionJson, |
| 263 | boost::ref(actions), _1, _2, _3), |
| 264 | fileOrFolderName, offset * 10, 10); |
| 265 | } |
| 266 | else { |
| 267 | more = m_actionLog->LookupActionsForFile(bind(StateServer::formatActionJson, |
| 268 | boost::ref(actions), _1, _2, _3), |
| 269 | fileOrFolderName, offset * 10, 10); |
| 270 | } |
| 271 | |
| 272 | json.push_back(Pair("actions", actions)); |
| 273 | |
| 274 | if (more) { |
| 275 | json.push_back(Pair("more", boost::lexical_cast<std::string>(offset + 1))); |
| 276 | // Name more = Name(interest.getPartialName(0, interest.size() - 1))(offset + 1); |
| 277 | // json.push_back(Pair("more", boost::lexical_cast<std::string>(more))); |
| 278 | } |
| 279 | |
| 280 | std::ostringstream os; |
| 281 | write_stream(Value(json), os, pretty_print | raw_utf8); |
| 282 | |
| 283 | shared_ptr<Data> data = make_shared<Data>(); |
| 284 | data->setName(interest); |
| 285 | data->setFreshnessPeriod(m_freshness); |
| 286 | data->setContent(reinterpret_cast<const uint8_t*>(os.str().c_str()), os.str().size()); |
| 287 | m_keyChain.sign(*data); |
| 288 | m_face.put(*data); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 289 | } |
| 290 | |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 291 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 292 | StateServer::formatFilestateJson(json_spirit::Array& files, const FileItem& file) |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 293 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 294 | /** |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 295 | * { |
| 296 | * "filestate": [ |
| 297 | * { |
| 298 | * "filename": "<FILENAME>", |
| 299 | * "owner": { |
| 300 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 301 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 302 | * }, |
| 303 | * |
| 304 | * "hash": "<FILE-HASH>", |
| 305 | * "timestamp": "<FILE-TIMESTAMP>", |
| 306 | * "chmod": "<FILE-MODE>", |
| 307 | * "segNum": "<NUMBER-OF-SEGMENTS(~file size)>" |
| 308 | * }, ..., |
| 309 | * ] |
| 310 | * |
| 311 | * // only if there are more actions available |
| 312 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-FILESTATE>" |
| 313 | * } |
| 314 | */ |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 315 | using namespace json_spirit; |
| 316 | using namespace boost::posix_time; |
| 317 | |
| 318 | Object json; |
| 319 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 320 | json.push_back(Pair("filename", file.filename())); |
| 321 | json.push_back(Pair("version", file.version())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 322 | { |
| 323 | Object owner; |
Yukai Tu | 40f184f | 2017-01-26 09:00:43 -0800 | [diff] [blame] | 324 | Name device_name(Block(file.device_name().data(), file.device_name().size())); |
| 325 | owner.push_back(Pair("userName", device_name.toUri())); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 326 | owner.push_back(Pair("seqNo", file.seq_no())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 327 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 328 | json.push_back(Pair("owner", owner)); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 331 | json.push_back(Pair("hash", toHex(reinterpret_cast<const uint8_t*>(file.file_hash().data()), |
| 332 | file.file_hash().size()))); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 333 | json.push_back(Pair("timestamp", to_iso_extended_string(from_time_t(file.mtime())))); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 334 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 335 | std::ostringstream chmod; |
| 336 | chmod << std::setbase(8) << std::setfill('0') << std::setw(4) << file.mode(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 337 | json.push_back(Pair("chmod", chmod.str())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 339 | json.push_back(Pair("segNum", file.seg_num())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 340 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 341 | files.push_back(json); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 345 | debugFileState(const FileItem& file) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 346 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 347 | std::cout << file.filename() << std::endl; |
| 348 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 349 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 350 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 351 | StateServer::info_files_folder(const InterestFilter& interesFilter, const Interest& interestTrue) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 352 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 353 | Name interest = interestTrue.getName(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 354 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 355 | _LOG_DEBUG("Invalid interest: " << interest << ", " << interest.size() - m_PREFIX_INFO.size()); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | _LOG_DEBUG(">> info_files_folder: " << interest); |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 360 | m_ioService.post(bind(&StateServer::info_files_folder_Execute, this, interest)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 361 | } |
| 362 | |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 363 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 364 | StateServer::info_files_folder_Execute(const Name& interest) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 365 | { |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 366 | // <PREFIX_INFO>/"filestate"/"folder"/<one-component-relative-folder-name>/<offset> |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 367 | if (interest.size() < 1) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 368 | // ignore any unexpected interests and errors |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 369 | _LOG_ERROR("empty interest name"); |
| 370 | return; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 371 | } |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 372 | uint64_t offset = interest.get(-1).toNumber(); |
| 373 | |
| 374 | // /// @todo !!! add security checking |
| 375 | |
| 376 | std::string folder; |
| 377 | if (interest.size() - m_PREFIX_INFO.size() == 4) |
| 378 | folder = interest.get(-2).toUri(); |
| 379 | else // == 3 |
| 380 | folder = ""; |
| 381 | |
| 382 | /* |
| 383 | *{ |
| 384 | * "files": [ |
| 385 | * ... |
| 386 | * ], |
| 387 | * |
| 388 | * // only if there are more actions available |
| 389 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>" |
| 390 | *} |
| 391 | */ |
| 392 | |
| 393 | using namespace json_spirit; |
| 394 | Object json; |
| 395 | |
| 396 | Array files; |
| 397 | bool more = |
| 398 | m_actionLog->GetFileState()->LookupFilesInFolderRecursively(bind(StateServer::formatFilestateJson, |
| 399 | boost::ref(files), _1), |
| 400 | folder, offset * 10, 10); |
| 401 | |
| 402 | json.push_back(Pair("files", files)); |
| 403 | |
| 404 | if (more) { |
| 405 | json.push_back(Pair("more", boost::lexical_cast<std::string>(offset + 1))); |
| 406 | // Name more = Name(interest.getPartialName(0, interest.size() - 1))(offset + 1); |
| 407 | // json.push_back(Pair("more", boost::lexical_cast<std::string>(more))); |
| 408 | } |
| 409 | |
| 410 | std::ostringstream os; |
| 411 | write_stream(Value(json), os, pretty_print | raw_utf8); |
| 412 | |
| 413 | shared_ptr<Data> data = make_shared<Data>(); |
| 414 | data->setName(interest); |
| 415 | data->setFreshnessPeriod(m_freshness); |
| 416 | data->setContent(reinterpret_cast<const uint8_t*>(os.str().c_str()), os.str().size()); |
| 417 | m_keyChain.sign(*data); |
| 418 | m_face.put(*data); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 419 | } |
| 420 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 421 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 422 | StateServer::cmd_restore_file(const InterestFilter& interesFilter, const Interest& interestTrue) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 423 | { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 424 | Name interest = interestTrue.getName(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 425 | if (interest.size() - m_PREFIX_CMD.size() != 4 && interest.size() - m_PREFIX_CMD.size() != 5) { |
| 426 | _LOG_DEBUG("Invalid interest: " << interest); |
| 427 | return; |
| 428 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 429 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 430 | _LOG_DEBUG(">> cmd_restore_file: " << interest); |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 431 | m_ioService.post(bind(&StateServer::cmd_restore_file_Execute, this, interest)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | void |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 435 | StateServer::cmd_restore_file_Execute(const Name& interest) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 436 | { |
| 437 | // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash> |
| 438 | |
| 439 | /// @todo !!! add security checking |
| 440 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 441 | FileItemPtr file; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 442 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 443 | if (interest.size() - m_PREFIX_CMD.size() == 5) { |
| 444 | const Buffer hash(interest.get(-1).value(), interest.get(-1).value_size()); |
| 445 | uint64_t version = interest.get(-2).toNumber(); |
| 446 | std::string filename = interest.get(-3).toUri(); // should be safe even with full relative path |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 447 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 448 | _LOG_DEBUG("filename: " << filename << " version: " << version); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 449 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 450 | file = m_actionLog->LookupAction(filename, version, hash); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 451 | |
| 452 | if (!file) { |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 453 | _LOG_ERROR("Requested file is not found: [" << filename << "] version [" << version << "] hash [" |
| 454 | << toHex(hash) |
| 455 | << "]"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 456 | } |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 457 | } |
| 458 | else { |
| 459 | uint64_t version = interest.get(-1).toNumber(); |
| 460 | std::string filename = interest.get(-2).toUri(); |
| 461 | file = m_actionLog->LookupAction(filename, version, Buffer(0, 0)); |
| 462 | if (!file) { |
| 463 | _LOG_ERROR("Requested file is not found: [" << filename << "] version [" << version << "]"); |
| 464 | } |
| 465 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 466 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 467 | if (!file) { |
| 468 | shared_ptr<Data> data = make_shared<Data>(); |
| 469 | data->setName(interest); |
| 470 | data->setFreshnessPeriod(m_freshness); |
| 471 | std::string msg = "FAIL: Requested file is not found"; |
| 472 | data->setContent(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 473 | m_keyChain.sign(*data); |
| 474 | m_face.put(*data); |
| 475 | return; |
| 476 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 477 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 478 | const Buffer hash(file->file_hash().c_str(), file->file_hash().size()); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 479 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 480 | /////////////////// |
| 481 | // now the magic // |
| 482 | /////////////////// |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 483 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 484 | fs::path filePath = m_rootDir / file->filename(); |
| 485 | Name deviceName( |
| 486 | Block((const unsigned char*)file->device_name().c_str(), file->device_name().size())); |
| 487 | |
| 488 | _LOG_DEBUG("filePath" << filePath << " deviceName " << deviceName); |
| 489 | |
| 490 | try { |
| 491 | if (fs::exists(filePath) && fs::last_write_time(filePath) == file->mtime() |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 492 | #if BOOST_VERSION >= 104900 |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 493 | && |
| 494 | fs::status(filePath).permissions() == static_cast<fs::perms>(file->mode()) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 495 | #endif |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 496 | ) { |
| 497 | fs::ifstream input(filePath, std::ios::in | std::ios::binary); |
| 498 | if (*util::Sha256(input).computeDigest() == hash) { |
| 499 | shared_ptr<Data> data = make_shared<Data>(); |
| 500 | data->setName(interest); |
| 501 | data->setFreshnessPeriod(m_freshness); |
| 502 | std::string msg = "OK: File already exists"; |
| 503 | data->setContent(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 504 | m_keyChain.sign(*data); |
| 505 | m_face.put(*data); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 506 | _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem"); |
| 507 | return; |
| 508 | } |
| 509 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 510 | } |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 511 | catch (fs::filesystem_error& error) { |
| 512 | shared_ptr<Data> data = make_shared<Data>(); |
| 513 | data->setName(interest); |
| 514 | data->setFreshnessPeriod(m_freshness); |
| 515 | std::string msg = "FAIL: File operation failed"; |
| 516 | data->setContent(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 517 | m_keyChain.sign(*data); |
| 518 | m_face.put(*data); |
| 519 | _LOG_ERROR("File operations failed on [" << filePath << "](ignoring)"); |
| 520 | } |
| 521 | |
| 522 | _LOG_TRACE("Restoring file [" << filePath << "]" |
| 523 | << " deviceName " |
| 524 | << deviceName); |
| 525 | if (m_objectManager.objectsToLocalFile(deviceName, hash, filePath)) { |
| 526 | last_write_time(filePath, file->mtime()); |
| 527 | #if BOOST_VERSION >= 104900 |
| 528 | permissions(filePath, static_cast<fs::perms>(file->mode())); |
| 529 | #endif |
| 530 | shared_ptr<Data> data = make_shared<Data>(); |
| 531 | data->setName(interest); |
| 532 | data->setFreshnessPeriod(m_freshness); |
| 533 | std::string msg = "OK"; |
| 534 | data->setContent(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 535 | m_keyChain.sign(*data); |
| 536 | m_face.put(*data); |
| 537 | _LOG_DEBUG("Restoring file successfully!"); |
| 538 | } |
| 539 | else { |
| 540 | shared_ptr<Data> data = make_shared<Data>(); |
| 541 | data->setName(interest); |
| 542 | data->setFreshnessPeriod(m_freshness); |
| 543 | std::string msg = "FAIL: Unknown error while restoring file"; |
| 544 | data->setContent(reinterpret_cast<const uint8_t*>(msg.c_str()), msg.size()); |
| 545 | m_keyChain.sign(*data); |
| 546 | m_face.put(*data); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 547 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 548 | } |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 549 | |
| 550 | } // namespace chronoshare |
| 551 | } // namespace ndn |