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