Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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 | |
| 21 | #include "state-server.h" |
| 22 | #include "logging.h" |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 23 | #include "periodic-task.h" |
| 24 | #include "simple-interval-generator.h" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 25 | #include "task.h" |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 26 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
| 28 | #include <boost/make_shared.hpp> |
| 29 | #include <utility> |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 30 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 31 | INIT_LOGGER("StateServer"); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 33 | using namespace Ndnx; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 34 | using namespace std; |
| 35 | using namespace boost; |
| 36 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 37 | StateServer::StateServer(CcnxWrapperPtr ccnx, ActionLogPtr actionLog, |
| 38 | const boost::filesystem::path& rootDir, const Ccnx::Name& userName, |
| 39 | const std::string& sharedFolderName, const std::string& appName, |
| 40 | ObjectManager& objectManager, int freshness /* = -1*/) |
| 41 | : m_ccnx(ccnx) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 42 | , m_actionLog(actionLog) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | , m_objectManager(objectManager) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 44 | , m_rootDir(rootDir) |
| 45 | , m_freshness(freshness) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | , m_executor(1) |
| 47 | , m_userName(userName) |
| 48 | , m_sharedFolderName(sharedFolderName) |
| 49 | , m_appName(appName) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 50 | { |
| 51 | // may be later /localhost should be replaced with /%C1.M.S.localhost |
| 52 | |
| 53 | // <PREFIX_INFO> = /localhost/<user's-device-name>/"chronoshare"/"info" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 54 | m_PREFIX_INFO = Name("/localhost")(m_userName)("chronoshare")(m_sharedFolderName)("info"); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 55 | |
| 56 | // <PREFIX_CMD> = /localhost/<user's-device-name>/"chronoshare"/"cmd" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 57 | m_PREFIX_CMD = Name("/localhost")(m_userName)("chronoshare")(m_sharedFolderName)("cmd"); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | m_executor.start(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | registerPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | StateServer::~StateServer() |
| 65 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | m_executor.shutdown(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 68 | deregisterPrefixes(); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | StateServer::registerPrefixes() |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 73 | { |
| 74 | // currently supporting limited number of command. |
| 75 | // will be extended to support all planned commands later |
| 76 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 77 | // <PREFIX_INFO>/"actions"/"all"/<segment> get list of all actions |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | m_ccnx->setInterestFilter(Name(m_PREFIX_INFO)("actions")("folder"), |
| 79 | bind(&StateServer::info_actions_folder, this, _1)); |
| 80 | m_ccnx->setInterestFilter(Name(m_PREFIX_INFO)("actions")("file"), |
| 81 | bind(&StateServer::info_actions_file, this, _1)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 83 | // <PREFIX_INFO>/"filestate"/"all"/<segment> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 84 | m_ccnx->setInterestFilter(Name(m_PREFIX_INFO)("files")("folder"), |
| 85 | bind(&StateServer::info_files_folder, this, _1)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 86 | |
| 87 | // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 88 | m_ccnx->setInterestFilter(Name(m_PREFIX_CMD)("restore")("file"), |
| 89 | bind(&StateServer::cmd_restore_file, this, _1)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | StateServer::deregisterPrefixes() |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 94 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 95 | m_ccnx->clearInterestFilter(Name(m_PREFIX_INFO)("actions")("folder")); |
| 96 | m_ccnx->clearInterestFilter(Name(m_PREFIX_INFO)("actions")("file")); |
| 97 | m_ccnx->clearInterestFilter(Name(m_PREFIX_INFO)("files")("folder")); |
| 98 | m_ccnx->clearInterestFilter(Name(m_PREFIX_CMD)("restore")("file")); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 101 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 102 | StateServer::formatActionJson(json_spirit::Array& actions, const Ccnx::Name& name, |
| 103 | sqlite3_int64 seq_no, const ActionItem& action) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 104 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | /* |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 106 | * { |
| 107 | * "id": { |
| 108 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 109 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 110 | * }, |
| 111 | * "timestamp": "<ACTION-TIMESTAMP>", |
| 112 | * "filename": "<FILENAME>", |
| 113 | * |
| 114 | * "action": "UPDATE | DELETE", |
| 115 | * |
| 116 | * // only if update |
| 117 | * "update": { |
| 118 | * "hash": "<FILE-HASH>", |
| 119 | * "timestamp": "<FILE-TIMESTAMP>", |
| 120 | * "chmod": "<FILE-MODE>", |
| 121 | * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>" |
| 122 | * }, |
| 123 | * |
| 124 | * // if parent_device_name is set |
| 125 | * "parentId": { |
| 126 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 127 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 128 | * } |
| 129 | * } |
| 130 | */ |
| 131 | |
| 132 | using namespace json_spirit; |
| 133 | using namespace boost::posix_time; |
| 134 | |
| 135 | Object json; |
| 136 | Object id; |
| 137 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 138 | id.push_back(Pair("userName", boost::lexical_cast<string>(name))); |
| 139 | id.push_back(Pair("seqNo", static_cast<int64_t>(seq_no))); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 140 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 141 | json.push_back(Pair("id", id)); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 143 | json.push_back(Pair("timestamp", to_iso_extended_string(from_time_t(action.timestamp())))); |
| 144 | json.push_back(Pair("filename", action.filename())); |
| 145 | json.push_back(Pair("version", action.version())); |
| 146 | json.push_back(Pair("action", (action.action() == 0) ? "UPDATE" : "DELETE")); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 147 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 148 | if (action.action() == 0) { |
| 149 | Object update; |
| 150 | update.push_back(Pair("hash", boost::lexical_cast<string>( |
| 151 | Hash(action.file_hash().c_str(), action.file_hash().size())))); |
| 152 | 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] | 153 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 154 | ostringstream chmod; |
| 155 | chmod << setbase(8) << setfill('0') << setw(4) << action.mode(); |
| 156 | update.push_back(Pair("chmod", chmod.str())); |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | update.push_back(Pair("segNum", action.seg_num())); |
| 159 | json.push_back(Pair("update", update)); |
| 160 | } |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 161 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 162 | if (action.has_parent_device_name()) { |
| 163 | Object parentId; |
| 164 | Ccnx::Name parent_device_name(action.parent_device_name().c_str(), |
| 165 | action.parent_device_name().size()); |
| 166 | id.push_back(Pair("userName", boost::lexical_cast<string>(parent_device_name))); |
| 167 | id.push_back(Pair("seqNo", action.parent_seq_no())); |
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("parentId", parentId)); |
| 170 | } |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 172 | actions.push_back(json); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 176 | StateServer::info_actions_folder(const Name& interest) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 177 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 178 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 179 | _LOG_DEBUG("Invalid interest: " << interest); |
| 180 | return; |
| 181 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 182 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 183 | _LOG_DEBUG(">> info_actions_folder: " << interest); |
| 184 | m_executor.execute(bind(&StateServer::info_actions_fileOrFolder_Execute, this, interest, true)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 188 | StateServer::info_actions_file(const Name& interest) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 189 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 191 | _LOG_DEBUG("Invalid interest: " << interest); |
| 192 | return; |
| 193 | } |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 195 | _LOG_DEBUG(">> info_actions_file: " << interest); |
| 196 | m_executor.execute(bind(&StateServer::info_actions_fileOrFolder_Execute, this, interest, false)); |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | |
| 200 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 201 | StateServer::info_actions_fileOrFolder_Execute(const Ccnx::Name& interest, bool isFolder /* = true*/) |
Alexander Afanasyev | 39dbc4b | 2013-03-01 10:39:23 -0800 | [diff] [blame] | 202 | { |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 203 | // <PREFIX_INFO>/"actions"/"folder|file"/<folder|file>/<offset> get list of all actions |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 204 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 205 | try { |
| 206 | int offset = interest.getCompFromBackAsInt(0); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 207 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 208 | /// @todo !!! add security checking |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 209 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 210 | string fileOrFolderName; |
| 211 | if (interest.size() - m_PREFIX_INFO.size() == 4) |
| 212 | fileOrFolderName = interest.getCompFromBackAsString(1); |
| 213 | else // == 3 |
| 214 | fileOrFolderName = ""; |
| 215 | /* |
Alexander Afanasyev | e1c9504 | 2013-02-27 01:02:36 -0800 | [diff] [blame] | 216 | * { |
| 217 | * "actions": [ |
| 218 | * ... |
| 219 | * ], |
| 220 | * |
| 221 | * // only if there are more actions available |
| 222 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>" |
| 223 | * } |
| 224 | */ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 225 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 226 | using namespace json_spirit; |
| 227 | Object json; |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | Array actions; |
| 230 | bool more; |
| 231 | if (isFolder) { |
| 232 | more = |
| 233 | m_actionLog->LookupActionsInFolderRecursively(boost::bind(StateServer::formatActionJson, |
| 234 | boost::ref(actions), _1, _2, _3), |
| 235 | fileOrFolderName, offset * 10, 10); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 236 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 237 | else { |
| 238 | more = m_actionLog->LookupActionsForFile(boost::bind(StateServer::formatActionJson, |
| 239 | boost::ref(actions), _1, _2, _3), |
| 240 | fileOrFolderName, offset * 10, 10); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 241 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 242 | |
| 243 | json.push_back(Pair("actions", actions)); |
| 244 | |
| 245 | if (more) { |
| 246 | json.push_back(Pair("more", lexical_cast<string>(offset + 1))); |
| 247 | // Ccnx::Name more = Name (interest.getPartialName (0, interest.size () - 1))(offset + 1); |
| 248 | // json.push_back (Pair ("more", lexical_cast<string> (more))); |
| 249 | } |
| 250 | |
| 251 | ostringstream os; |
| 252 | write_stream(Value(json), os, pretty_print | raw_utf8); |
| 253 | m_ccnx->publishData(interest, os.str(), 1); |
| 254 | } |
| 255 | catch (Ccnx::NameException& ne) { |
| 256 | // ignore any unexpected interests and errors |
| 257 | _LOG_ERROR(*boost::get_error_info<Ccnx::error_info_str>(ne)); |
| 258 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 259 | } |
| 260 | |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 261 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 262 | StateServer::formatFilestateJson(json_spirit::Array& files, const FileItem& file) |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 263 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 264 | /** |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 265 | * { |
| 266 | * "filestate": [ |
| 267 | * { |
| 268 | * "filename": "<FILENAME>", |
| 269 | * "owner": { |
| 270 | * "userName": "<NDN-NAME-OF-THE-USER>", |
| 271 | * "seqNo": "<SEQ_NO_OF_THE_ACTION>" |
| 272 | * }, |
| 273 | * |
| 274 | * "hash": "<FILE-HASH>", |
| 275 | * "timestamp": "<FILE-TIMESTAMP>", |
| 276 | * "chmod": "<FILE-MODE>", |
| 277 | * "segNum": "<NUMBER-OF-SEGMENTS (~file size)>" |
| 278 | * }, ..., |
| 279 | * ] |
| 280 | * |
| 281 | * // only if there are more actions available |
| 282 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-FILESTATE>" |
| 283 | * } |
| 284 | */ |
| 285 | using namespace json_spirit; |
| 286 | using namespace boost::posix_time; |
| 287 | |
| 288 | Object json; |
| 289 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 290 | json.push_back(Pair("filename", file.filename())); |
| 291 | json.push_back(Pair("version", file.version())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 292 | { |
| 293 | Object owner; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 294 | Ccnx::Name device_name(file.device_name().c_str(), file.device_name().size()); |
| 295 | owner.push_back(Pair("userName", boost::lexical_cast<string>(device_name))); |
| 296 | owner.push_back(Pair("seqNo", file.seq_no())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 298 | json.push_back(Pair("owner", owner)); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 301 | json.push_back(Pair("hash", boost::lexical_cast<string>( |
| 302 | Hash(file.file_hash().c_str(), file.file_hash().size())))); |
| 303 | 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] | 304 | |
| 305 | ostringstream chmod; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 306 | chmod << setbase(8) << setfill('0') << setw(4) << file.mode(); |
| 307 | json.push_back(Pair("chmod", chmod.str())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 308 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 309 | json.push_back(Pair("segNum", file.seg_num())); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 310 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 311 | files.push_back(json); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 315 | debugFileState(const FileItem& file) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 316 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 317 | std::cout << file.filename() << std::endl; |
| 318 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 319 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 320 | void |
| 321 | StateServer::info_files_folder(const Ccnx::Name& interest) |
| 322 | { |
| 323 | if (interest.size() - m_PREFIX_INFO.size() != 3 && interest.size() - m_PREFIX_INFO.size() != 4) { |
| 324 | _LOG_DEBUG("Invalid interest: " << interest << ", " << interest.size() - m_PREFIX_INFO.size()); |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | _LOG_DEBUG(">> info_files_folder: " << interest); |
| 329 | m_executor.execute(bind(&StateServer::info_files_folder_Execute, this, interest)); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
| 333 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 334 | StateServer::info_files_folder_Execute(const Ccnx::Name& interest) |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 335 | { |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 336 | // <PREFIX_INFO>/"filestate"/"folder"/<one-component-relative-folder-name>/<offset> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 337 | try { |
| 338 | int offset = interest.getCompFromBackAsInt(0); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 339 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 340 | // /// @todo !!! add security checking |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 341 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 342 | string folder; |
| 343 | if (interest.size() - m_PREFIX_INFO.size() == 4) |
| 344 | folder = interest.getCompFromBackAsString(1); |
| 345 | else // == 3 |
| 346 | folder = ""; |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 347 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 348 | /* |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 349 | * { |
| 350 | * "files": [ |
| 351 | * ... |
| 352 | * ], |
| 353 | * |
| 354 | * // only if there are more actions available |
| 355 | * "more": "<NDN-NAME-OF-NEXT-SEGMENT-OF-ACTION>" |
| 356 | * } |
| 357 | */ |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 358 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 359 | using namespace json_spirit; |
| 360 | Object json; |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 361 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 362 | Array files; |
| 363 | bool more = m_actionLog->GetFileState() |
| 364 | ->LookupFilesInFolderRecursively(boost::bind(StateServer::formatFilestateJson, |
| 365 | boost::ref(files), _1), |
| 366 | folder, offset * 10, 10); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 367 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 368 | json.push_back(Pair("files", files)); |
Alexander Afanasyev | 94240b5 | 2013-02-27 11:57:29 -0800 | [diff] [blame] | 369 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 370 | if (more) { |
| 371 | json.push_back(Pair("more", lexical_cast<string>(offset + 1))); |
| 372 | // Ccnx::Name more = Name (interest.getPartialName (0, interest.size () - 1))(offset + 1); |
| 373 | // json.push_back (Pair ("more", lexical_cast<string> (more))); |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 374 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 375 | |
| 376 | ostringstream os; |
| 377 | write_stream(Value(json), os, pretty_print | raw_utf8); |
| 378 | m_ccnx->publishData(interest, os.str(), 1); |
| 379 | } |
| 380 | catch (Ccnx::NameException& ne) { |
| 381 | // ignore any unexpected interests and errors |
| 382 | _LOG_ERROR(*boost::get_error_info<Ccnx::error_info_str>(ne)); |
| 383 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 387 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 388 | StateServer::cmd_restore_file(const Ccnx::Name& interest) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 389 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 390 | if (interest.size() - m_PREFIX_CMD.size() != 4 && interest.size() - m_PREFIX_CMD.size() != 5) { |
| 391 | _LOG_DEBUG("Invalid interest: " << interest); |
| 392 | return; |
| 393 | } |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 394 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 395 | _LOG_DEBUG(">> cmd_restore_file: " << interest); |
| 396 | m_executor.execute(bind(&StateServer::cmd_restore_file_Execute, this, interest)); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 400 | StateServer::cmd_restore_file_Execute(const Ccnx::Name& interest) |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 401 | { |
| 402 | // <PREFIX_CMD>/"restore"/"file"/<one-component-relative-file-name>/<version>/<file-hash> |
| 403 | |
| 404 | /// @todo !!! add security checking |
| 405 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 406 | try { |
| 407 | FileItemPtr file; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 408 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 409 | if (interest.size() - m_PREFIX_CMD.size() == 5) { |
| 410 | Hash hash(head(interest.getCompFromBack(0)), interest.getCompFromBack(0).size()); |
| 411 | int64_t version = interest.getCompFromBackAsInt(1); |
| 412 | string filename = |
| 413 | interest.getCompFromBackAsString(2); // should be safe even with full relative path |
Alexander Afanasyev | 95f9f55 | 2013-02-26 23:05:20 -0800 | [diff] [blame] | 414 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 415 | file = m_actionLog->LookupAction(filename, version, hash); |
| 416 | if (!file) { |
| 417 | _LOG_ERROR("Requested file is not found: [" << filename << "] version [" << version |
| 418 | << "] hash [" |
| 419 | << hash.shortHash() |
| 420 | << "]"); |
| 421 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 422 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 423 | else { |
| 424 | int64_t version = interest.getCompFromBackAsInt(0); |
| 425 | string filename = |
| 426 | interest.getCompFromBackAsString(1); // should be safe even with full relative path |
| 427 | |
| 428 | file = m_actionLog->LookupAction(filename, version, Hash(0, 0)); |
| 429 | if (!file) { |
| 430 | _LOG_ERROR("Requested file is not found: [" << filename << "] version [" << version << "]"); |
| 431 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 432 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 433 | |
| 434 | if (!file) { |
| 435 | m_ccnx->publishData(interest, "FAIL: Requested file is not found", 1); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | Hash hash = Hash(file->file_hash().c_str(), file->file_hash().size()); |
| 440 | |
| 441 | /////////////////// |
| 442 | // now the magic // |
| 443 | /////////////////// |
| 444 | |
| 445 | boost::filesystem::path filePath = m_rootDir / file->filename(); |
| 446 | Name deviceName(file->device_name().c_str(), file->device_name().size()); |
| 447 | |
| 448 | try { |
| 449 | if (filesystem::exists(filePath) && filesystem::last_write_time(filePath) == file->mtime() && |
| 450 | #if BOOST_VERSION >= 104900 |
| 451 | filesystem::status(filePath).permissions() == static_cast<filesystem::perms>(file->mode()) && |
| 452 | #endif |
| 453 | *Hash::FromFileContent(filePath) == hash) { |
| 454 | m_ccnx->publishData(interest, "OK: File already exists", 1); |
| 455 | _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem"); |
| 456 | return; |
| 457 | } |
| 458 | } |
| 459 | catch (filesystem::filesystem_error& error) { |
| 460 | m_ccnx->publishData(interest, "FAIL: File operation failed", 1); |
| 461 | _LOG_ERROR("File operations failed on [" << filePath << "] (ignoring)"); |
| 462 | } |
| 463 | |
| 464 | _LOG_TRACE("Restoring file [" << filePath << "]"); |
| 465 | if (m_objectManager.objectsToLocalFile(deviceName, hash, filePath)) { |
| 466 | last_write_time(filePath, file->mtime()); |
| 467 | #if BOOST_VERSION >= 104900 |
| 468 | permissions(filePath, static_cast<filesystem::perms>(file->mode())); |
| 469 | #endif |
| 470 | m_ccnx->publishData(interest, "OK", 1); |
| 471 | } |
| 472 | else { |
| 473 | m_ccnx->publishData(interest, "FAIL: Unknown error while restoring file", 1); |
| 474 | } |
| 475 | } |
| 476 | catch (Ccnx::NameException& ne) { |
| 477 | // ignore any unexpected interests and errors |
| 478 | _LOG_ERROR(*boost::get_error_info<Ccnx::error_info_str>(ne)); |
| 479 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 480 | } |