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. |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -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. |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -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. |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -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. |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -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. |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "dispatcher.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 22 | #include "fetch-task-db.hpp" |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 23 | #include "core/logging.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/digest.hpp> |
| 26 | #include <ndn-cxx/util/string-helper.hpp> |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 29 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace chronoshare { |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 33 | _LOG_INIT(Dispatcher); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 34 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 35 | namespace fs = boost::filesystem; |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 36 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 37 | static const name::Component CHRONOSHARE_APP = name::Component("chronoshare"); |
| 38 | static const Name BROADCAST_DOMAIN = "/ndn/multicast"; |
| 39 | |
| 40 | static const time::seconds DEFAULT_SYNC_INTEREST_INTERVAL = time::seconds(10); |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 41 | static const time::seconds DEFAULT_AUTO_DISCOVERY_INTERVAL = time::seconds(60); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 42 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 43 | Dispatcher::Dispatcher(const std::string& localUserName, const std::string& sharedFolder, |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 44 | const fs::path& rootDir, Face& face) |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 45 | : m_face(face) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | , m_rootDir(rootDir) |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 47 | , m_ioService(face.getIoService()) |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 48 | , m_scheduler(m_ioService) |
| 49 | , m_autoDiscovery(m_scheduler) |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 50 | , m_objectManager(face, m_keyChain, rootDir, CHRONOSHARE_APP.toUri()) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 51 | , m_localUserName(localUserName) |
| 52 | , m_sharedFolder(sharedFolder) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 53 | { |
Zhenkai Zhu | 3290b8e | 2013-01-24 15:25:48 -0800 | [diff] [blame] | 54 | m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 55 | m_actionLog = |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 56 | make_shared<ActionLog>(m_face, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP, |
| 57 | // bind(&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 58 | ActionLog::OnFileAddedOrChangedCallback(), // don't really need this callback |
| 59 | bind(&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1)); |
| 60 | m_fileState = m_actionLog->GetFileState(); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 61 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 62 | Name syncPrefix = Name(BROADCAST_DOMAIN); |
| 63 | syncPrefix.append(CHRONOSHARE_APP); |
| 64 | syncPrefix.append(sharedFolder); |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 65 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 66 | m_server = make_unique<ContentServer>(m_face, m_actionLog, rootDir, m_localUserName, |
| 67 | m_sharedFolder, CHRONOSHARE_APP, m_keyChain); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 68 | m_server->registerPrefix(Name("/")); |
Zhenkai Zhu | 3f64d2d | 2013-01-25 14:34:59 -0800 | [diff] [blame] | 69 | m_server->registerPrefix(Name(BROADCAST_DOMAIN)); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 70 | |
Lijing Wang | b95c6a5 | 2016-12-25 14:45:17 -0800 | [diff] [blame] | 71 | m_stateServer = make_unique<StateServer>(m_face, m_actionLog, rootDir, m_localUserName, m_sharedFolder, |
| 72 | CHRONOSHARE_APP, m_objectManager, m_keyChain); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 73 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 74 | m_core = make_unique<SyncCore>(m_face, m_syncLog, localUserName, Name("/"), syncPrefix, |
| 75 | bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), |
| 76 | DEFAULT_SYNC_INTEREST_INTERVAL); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 77 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 78 | FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 79 | m_actionFetcher = |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 80 | make_shared<FetchManager>(m_face, bind(&SyncLog::LookupLocator, &*m_syncLog, _1), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | Name(BROADCAST_DOMAIN), // no appname suffix now |
| 82 | 3, |
| 83 | bind(&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), |
| 84 | FetchManager::FinishCallback(), actionTaskDb); |
Zhenkai Zhu | b8c49af | 2013-01-29 16:03:56 -0800 | [diff] [blame] | 85 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 86 | FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | m_fileFetcher = |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 88 | make_shared<FetchManager>(m_face, bind(&SyncLog::LookupLocator, &*m_syncLog, _1), |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | Name(BROADCAST_DOMAIN), // no appname suffix now |
| 90 | 3, bind(&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, |
| 91 | _3, _4), |
| 92 | bind(&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2), |
| 93 | fileTaskDb); |
Zhenkai Zhu | b8c49af | 2013-01-29 16:03:56 -0800 | [diff] [blame] | 94 | |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 95 | _LOG_DEBUG("registering prefix discovery in Dispatcher"); |
| 96 | m_autoDiscovery = m_scheduler.scheduleEvent(DEFAULT_AUTO_DISCOVERY_INTERVAL, |
| 97 | bind(&Dispatcher::DiscoverPrefix, this)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Dispatcher::~Dispatcher() |
| 101 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 102 | _LOG_DEBUG("Enter destructor of dispatcher"); |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 103 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 104 | |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 105 | void |
| 106 | Dispatcher::DiscoverPrefix() |
| 107 | { |
| 108 | _LOG_DEBUG("Query for local prefix"); |
| 109 | Interest interest(Name("/localhop/nfd/rib/routable-prefixes")); |
| 110 | m_face.expressInterest(interest, |
| 111 | bind(&Dispatcher::Handle_Prefix_Discovery, this, _1, _2), |
| 112 | bind([this] {m_autoDiscovery = m_scheduler.scheduleEvent(DEFAULT_AUTO_DISCOVERY_INTERVAL, |
| 113 | bind(&Dispatcher::DiscoverPrefix, this)); |
| 114 | _LOG_DEBUG("query routable-prefixes, nack received"); }), |
| 115 | bind([this] {m_autoDiscovery = m_scheduler.scheduleEvent(DEFAULT_AUTO_DISCOVERY_INTERVAL, |
| 116 | bind(&Dispatcher::DiscoverPrefix, this)); |
| 117 | _LOG_DEBUG("query routable-prefixes interest timeout"); })); |
| 118 | } |
| 119 | |
| 120 | void |
| 121 | Dispatcher::Handle_Prefix_Discovery(const Interest& interest, const Data& data) |
| 122 | { |
| 123 | const Block& content = data.getContent(); |
| 124 | content.parse(); |
| 125 | |
| 126 | Name prefix; |
| 127 | |
| 128 | for (auto& element : content.elements()) { |
| 129 | prefix = Name(element); |
| 130 | break; |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 131 | } |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 132 | |
| 133 | _LOG_DEBUG("local prefix:" << prefix << prefix.empty()); |
| 134 | if(prefix.empty()) { |
| 135 | _LOG_DEBUG("Local prefix is empty"); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | _LOG_DEBUG("local prefix:" << prefix << "discovered"); |
| 140 | Did_LocalPrefix_Updated(prefix); |
| 141 | |
| 142 | m_autoDiscovery = m_scheduler.scheduleEvent(DEFAULT_AUTO_DISCOVERY_INTERVAL, |
| 143 | bind(&Dispatcher::DiscoverPrefix, this)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 146 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 147 | Dispatcher::Did_LocalPrefix_Updated(const Name& forwardingHint) |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 148 | { |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 149 | Name effectiveForwardingHint; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | if (m_localUserName.size() >= forwardingHint.size() && |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 151 | m_localUserName.getSubName(0, forwardingHint.size()) == forwardingHint) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 152 | effectiveForwardingHint = Name("/"); // "directly" accesible |
| 153 | } |
| 154 | else { |
| 155 | effectiveForwardingHint = forwardingHint; |
| 156 | } |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 157 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 158 | Name oldLocalPrefix = m_syncLog->LookupLocalLocator(); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 159 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 160 | if (oldLocalPrefix == effectiveForwardingHint) { |
| 161 | _LOG_DEBUG( |
| 162 | "Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint |
| 163 | << ", but effective prefix didn't change"); |
| 164 | return; |
| 165 | } |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 166 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 167 | if (effectiveForwardingHint == Name("/") || effectiveForwardingHint == Name(BROADCAST_DOMAIN)) { |
| 168 | _LOG_DEBUG("Basic effective prefix [" << effectiveForwardingHint |
| 169 | << "]. Updating local prefix, but don't reregister"); |
| 170 | m_syncLog->UpdateLocalLocator(effectiveForwardingHint); |
| 171 | return; |
| 172 | } |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 174 | _LOG_DEBUG("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 175 | |
| 176 | m_server->registerPrefix(effectiveForwardingHint); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 177 | m_syncLog->UpdateLocalLocator(effectiveForwardingHint); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 178 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 179 | if (oldLocalPrefix == Name("/") || oldLocalPrefix == Name(BROADCAST_DOMAIN)) { |
| 180 | _LOG_DEBUG("Don't deregister basic prefix: " << oldLocalPrefix); |
| 181 | return; |
| 182 | } |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 183 | m_server->deregisterPrefix(oldLocalPrefix); |
| 184 | } |
| 185 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 186 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 187 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 188 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 189 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 190 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 191 | Dispatcher::Did_LocalFile_AddOrModify(const fs::path& relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 192 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 193 | m_ioService.post(bind(&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 197 | Dispatcher::Did_LocalFile_AddOrModify_Execute(fs::path relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 198 | { |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 199 | _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute"); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 200 | fs::path absolutePath = m_rootDir / relativeFilePath; |
| 201 | _LOG_DEBUG("relativeFilePath : " << relativeFilePath); |
| 202 | _LOG_DEBUG("absolutePath : " << absolutePath); |
| 203 | if (!fs::exists(absolutePath)) { |
| 204 | // BOOST_THROW_EXCEPTION(Error::Dispatcher() << error_info_str("Update non exist file: " + |
| 205 | // absolutePath.string() )); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 206 | _LOG_DEBUG("Update non exist file: " << absolutePath.string()); |
| 207 | return; |
| 208 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 209 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 210 | FileItemPtr currentFile = m_fileState->LookupFile(relativeFilePath.generic_string()); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 211 | |
| 212 | if (currentFile) { |
| 213 | fs::ifstream input(absolutePath); |
| 214 | if (*util::Sha256(input).computeDigest() == |
| 215 | Buffer(currentFile->file_hash().c_str(), currentFile->file_hash().size()) |
| 216 | // The following two are commented out to prevent front end from reporting intermediate files |
| 217 | // should enable it if there is other way to prevent this |
| 218 | // && last_write_time(absolutePath) == currentFile->mtime() |
| 219 | // && status(absolutePath).permissions() == |
| 220 | // static_cast<fs::perms>(currentFile->mode()) |
| 221 | ) { |
| 222 | _LOG_ERROR("Got notification about the same file [" << relativeFilePath << "]"); |
| 223 | return; |
| 224 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | if (currentFile && !currentFile->is_complete()) { |
| 228 | _LOG_ERROR("Got notification about incomplete file [" << relativeFilePath << "]"); |
| 229 | return; |
| 230 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 231 | |
| 232 | int seg_num; |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 233 | ConstBufferPtr hash; |
| 234 | _LOG_DEBUG("absolutePath: " << absolutePath << " m_localUserName: " << m_localUserName); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 235 | tie(hash, seg_num) = m_objectManager.localFileToObjects(absolutePath, m_localUserName); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 236 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 237 | try { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 238 | m_actionLog->AddLocalActionUpdate(relativeFilePath.generic_string(), *hash, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 239 | last_write_time(absolutePath), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 240 | #if BOOST_VERSION >= 104900 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 241 | status(absolutePath).permissions(), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 242 | #else |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 243 | 0, |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 244 | #endif |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 245 | seg_num); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 246 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 247 | // notify SyncCore to propagate the change |
| 248 | m_core->localStateChangedDelayed(); |
| 249 | } |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 250 | catch (const fs::filesystem_error& error) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 251 | _LOG_ERROR("File operations failed on [" << relativeFilePath << "](ignoring)"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 252 | } |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 253 | |
| 254 | _LOG_DEBUG("LocalFile_AddOrModify_Execute Finished!"); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 258 | Dispatcher::Did_LocalFile_Delete(const fs::path& relativeFilePath) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 259 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 260 | m_ioService.post(bind(&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 264 | Dispatcher::Did_LocalFile_Delete_Execute(fs::path relativeFilePath) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 265 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 266 | fs::path absolutePath = m_rootDir / relativeFilePath; |
| 267 | if (fs::exists(absolutePath)) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 268 | _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string()); |
| 269 | return; |
| 270 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 271 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 272 | m_actionLog->AddLocalActionDelete(relativeFilePath.generic_string()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 273 | // notify SyncCore to propagate the change |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 274 | m_core->localStateChangedDelayed(); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 278 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 279 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 280 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 281 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 282 | Dispatcher::Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg) |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 283 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 284 | m_ioService.post(bind(&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg)); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 288 | Dispatcher::Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 289 | { |
| 290 | int size = stateMsg->state_size(); |
| 291 | int index = 0; |
| 292 | // iterate and fetch the actions |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 293 | for (; index < size; index++) { |
| 294 | SyncState state = stateMsg->state(index); |
| 295 | if (state.has_old_seq() && state.has_seq()) { |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 296 | uint64_t oldSeq = state.old_seq(); |
| 297 | uint64_t newSeq = state.seq(); |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 298 | Name userName(Block((const unsigned char*)state.name().c_str(), state.name().size())); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 299 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 300 | // fetch actions with oldSeq + 1 to newSeq(inclusive) |
| 301 | Name actionNameBase = Name("/"); |
| 302 | actionNameBase.append(userName).append(CHRONOSHARE_APP).append("action").append(m_sharedFolder); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 303 | m_actionFetcher->Enqueue(userName, actionNameBase, std::max<uint64_t>(oldSeq + 1, 1), newSeq, |
| 304 | FetchManager::PRIORITY_HIGH); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 310 | Dispatcher::Did_FetchManager_ActionFetch(const Name& deviceName, const Name& actionBaseName, |
| 311 | uint32_t seqno, shared_ptr<Data> actionData) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 312 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 313 | /// @todo Errors and exception checking |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 314 | _LOG_DEBUG("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName |
| 315 | << ", seqno: " |
| 316 | << seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 317 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 318 | ActionItemPtr action = m_actionLog->AddRemoteAction(deviceName, seqno, actionData); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 319 | if (!action) { |
| 320 | _LOG_ERROR("AddRemoteAction did not insert action, ignoring"); |
| 321 | return; |
| 322 | } |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 323 | // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify |
| 324 | // callbacks |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 325 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 326 | if (action->action() == ActionItem::UPDATE) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 327 | ConstBufferPtr hash = |
| 328 | make_shared<Buffer>(action->file_hash().c_str(), action->file_hash().size()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 329 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 330 | Name fileNameBase = Name("/"); |
| 331 | fileNameBase.append(deviceName).append(CHRONOSHARE_APP).append("file"); |
| 332 | // fileNameBase.append(name::Component(hash)); |
| 333 | fileNameBase.appendImplicitSha256Digest(hash); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 334 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 335 | std::string hashStr = toHex(*hash); |
| 336 | if (ObjectDb::doesExist(m_rootDir / ".chronoshare", deviceName, hashStr)) { |
| 337 | _LOG_DEBUG("File already exists in the database. No need to refetch, just directly applying " |
| 338 | "the action"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 339 | Did_FetchManager_FileFetchComplete(deviceName, fileNameBase); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 340 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 341 | else { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 342 | if (m_objectDbMap.find(*hash) == m_objectDbMap.end()) { |
| 343 | _LOG_DEBUG("create ObjectDb for " << toHex(*hash)); |
| 344 | m_objectDbMap[*hash] = make_shared<ObjectDb>(m_rootDir / ".chronoshare", hashStr); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | m_fileFetcher->Enqueue(deviceName, fileNameBase, 0, action->seg_num() - 1, |
| 348 | FetchManager::PRIORITY_NORMAL); |
| 349 | } |
| 350 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 354 | Dispatcher::Did_ActionLog_ActionApply_Delete(const std::string& filename) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 355 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 356 | _LOG_DEBUG("Action to delete " << filename); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 357 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 358 | fs::path absolutePath = m_rootDir / filename; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 359 | try { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 360 | if (fs::exists(absolutePath)) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 361 | // need some protection from local detection of removal |
| 362 | remove(absolutePath); |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 363 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 364 | // hack to remove empty parent dirs |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 365 | fs::path parentPath = absolutePath.parent_path(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 366 | while (parentPath > m_rootDir) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 367 | if (fs::is_empty(parentPath)) { |
| 368 | fs::remove(parentPath); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 369 | parentPath = parentPath.parent_path(); |
| 370 | } |
| 371 | else { |
| 372 | break; |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 373 | } |
| 374 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 375 | } |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 376 | // don't exist |
| 377 | } |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 378 | catch (const fs::filesystem_error& error) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 379 | _LOG_ERROR("File operations failed when removing [" << absolutePath << "](ignoring)"); |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 380 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 384 | Dispatcher::Did_FetchManager_FileSegmentFetch(const Name& deviceName, |
| 385 | const Name& fileSegmentBaseName, |
| 386 | uint32_t segment, |
| 387 | shared_ptr<Data> |
| 388 | fileSegmentData) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 389 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 390 | m_ioService.post(bind(&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, |
| 391 | fileSegmentBaseName, segment, fileSegmentData)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 395 | Dispatcher::Did_FetchManager_FileSegmentFetch_Execute(Name deviceName, |
| 396 | Name fileSegmentBaseName, |
| 397 | uint32_t segment, |
| 398 | shared_ptr<Data> fileSegmentData) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 399 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 400 | // fileSegmentBaseName: /<device_name>/<appname>/file/<hash> |
| 401 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 402 | Buffer hash(fileSegmentBaseName.get(-1).value(), fileSegmentBaseName.get(-1).value_size()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 403 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 404 | _LOG_DEBUG("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName |
| 405 | << ", segment: " |
| 406 | << segment); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 407 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 408 | // _LOG_DEBUG("Looking up objectdb for " << hash); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 409 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 410 | std::map<Buffer, shared_ptr<ObjectDb>>::iterator db = m_objectDbMap.find(hash); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 411 | if (db != m_objectDbMap.end()) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 412 | db->second->saveContentObject(deviceName, segment, *fileSegmentData); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 413 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 414 | else { |
| 415 | _LOG_ERROR("no db available for this content object: " << fileSegmentBaseName << ", size: " |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 416 | << fileSegmentData->getContent().size()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 417 | } |
Alexander Afanasyev | 17507ba | 2013-01-24 23:47:34 -0800 | [diff] [blame] | 418 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 419 | // ObjectDb objectDb(m_rootDir / ".chronoshare", lexical_cast<string>(hash)); |
| 420 | // objectDb.saveContentObject(deviceName, segment, *fileSegmentData); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 424 | Dispatcher::Did_FetchManager_FileFetchComplete(const Name& deviceName, const Name& fileBaseName) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 425 | { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 426 | m_ioService.post( |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 427 | bind(&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 428 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 429 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 430 | void |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 431 | Dispatcher::Did_FetchManager_FileFetchComplete_Execute(Name deviceName, Name fileBaseName) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 432 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 433 | // fileBaseName: /<device_name>/<appname>/file/<hash> |
| 434 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 435 | _LOG_DEBUG("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 436 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 437 | Buffer hash(fileBaseName.get(-1).value(), fileBaseName.get(-1).value_size()); |
| 438 | |
| 439 | _LOG_DEBUG("Extracted hash: " << toHex(hash)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 440 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 441 | if (m_objectDbMap.find(hash) != m_objectDbMap.end()) { |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 442 | // remove the db handle |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 443 | m_objectDbMap.erase(hash); // to commit write |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 444 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 445 | else { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 446 | _LOG_ERROR("no db available for this file: " << toHex(hash)); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 447 | } |
| 448 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 449 | FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash(hash); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 450 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 451 | for (FileItems::iterator file = filesToAssemble->begin(); file != filesToAssemble->end(); file++) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 452 | fs::path filePath = m_rootDir / file->filename(); |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 453 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 454 | try { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 455 | if (fs::exists(filePath) && fs::last_write_time(filePath) == file->mtime() && |
| 456 | fs::status(filePath).permissions() == static_cast<fs::perms>(file->mode())) { |
| 457 | fs::ifstream input(filePath, std::ios::in | std::ios::binary); |
| 458 | if (*util::Sha256(input).computeDigest() == hash) { |
| 459 | _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem"); |
| 460 | continue; |
| 461 | } |
Zhenkai Zhu | f3a9fa6 | 2013-01-31 17:23:09 -0800 | [diff] [blame] | 462 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 463 | } |
Yukai Tu | 729b289 | 2017-02-09 21:15:48 -0800 | [diff] [blame^] | 464 | catch (const fs::filesystem_error& error) { |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 465 | _LOG_ERROR("File operations failed on [" << filePath << "](ignoring)"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 466 | } |
| 467 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 468 | if (ObjectDb::doesExist(m_rootDir / ".chronoshare", deviceName, toHex(hash))) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 469 | bool ok = m_objectManager.objectsToLocalFile(deviceName, hash, filePath); |
| 470 | if (ok) { |
| 471 | last_write_time(filePath, file->mtime()); |
| 472 | #if BOOST_VERSION >= 104900 |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 473 | permissions(filePath, static_cast<fs::perms>(file->mode())); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 474 | #endif |
| 475 | |
| 476 | m_fileState->SetFileComplete(file->filename()); |
| 477 | } |
| 478 | else { |
| 479 | _LOG_ERROR("Notified about complete fetch, but file cannot be restored from the database: [" |
| 480 | << filePath |
| 481 | << "]"); |
| 482 | } |
| 483 | } |
| 484 | else { |
| 485 | _LOG_ERROR(filePath << " supposed to have all segments, but not"); |
| 486 | // should abort for debugging |
| 487 | } |
| 488 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 489 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 490 | |
Lijing Wang | 8e56d08 | 2016-12-25 14:45:23 -0800 | [diff] [blame] | 491 | } // namespace chronoshare |
| 492 | } // namespace ndn |