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 "ccnx-discovery.hpp" |
| 23 | #include "fetch-task-db.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 24 | #include "logging.hpp" |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 25 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 29 | using namespace Ndnx; |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 30 | using namespace std; |
| 31 | using namespace boost; |
| 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 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 35 | static const string CHRONOSHARE_APP = "chronoshare"; |
| 36 | static const string BROADCAST_DOMAIN = "/ndn/broadcast"; |
| 37 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 38 | static const int CONTENT_FRESHNESS = 1800; // seconds |
Zhenkai Zhu | 126b21c | 2013-01-28 17:56:19 -0800 | [diff] [blame] | 39 | const static double DEFAULT_SYNC_INTEREST_INTERVAL = 10.0; // seconds; |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 40 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | Dispatcher::Dispatcher(const std::string& localUserName, const std::string& sharedFolder, |
| 42 | const filesystem::path& rootDir, Ccnx::CcnxWrapperPtr ccnx, |
| 43 | bool enablePrefixDiscovery) |
| 44 | : m_ccnx(ccnx) |
| 45 | , m_core(NULL) |
| 46 | , m_rootDir(rootDir) |
| 47 | , m_executor( |
| 48 | 1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished |
| 49 | , m_objectManager(ccnx, rootDir, CHRONOSHARE_APP) |
| 50 | , m_localUserName(localUserName) |
| 51 | , m_sharedFolder(sharedFolder) |
| 52 | , m_server(NULL) |
| 53 | , m_enablePrefixDiscovery(enablePrefixDiscovery) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 54 | { |
Zhenkai Zhu | 3290b8e | 2013-01-24 15:25:48 -0800 | [diff] [blame] | 55 | m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 56 | m_actionLog = |
| 57 | make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder, CHRONOSHARE_APP, |
| 58 | // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7), |
| 59 | ActionLog::OnFileAddedOrChangedCallback(), // don't really need this callback |
| 60 | bind(&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1)); |
| 61 | m_fileState = m_actionLog->GetFileState(); |
Alexander Afanasyev | d6364ef | 2013-02-06 13:13:07 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 63 | Name syncPrefix = Name(BROADCAST_DOMAIN)(CHRONOSHARE_APP)(sharedFolder); |
Alexander Afanasyev | cbda992 | 2013-01-22 11:21:12 -0800 | [diff] [blame] | 64 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 65 | // m_server needs a different ccnx face |
| 66 | m_server = new ContentServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, |
| 67 | m_sharedFolder, CHRONOSHARE_APP, CONTENT_FRESHNESS); |
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 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 71 | m_stateServer = |
| 72 | new StateServer(make_shared<CcnxWrapper>(), m_actionLog, rootDir, m_localUserName, |
| 73 | m_sharedFolder, CHRONOSHARE_APP, m_objectManager, CONTENT_FRESHNESS); |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 74 | // no need to register, right now only listening on localhost prefix |
| 75 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 76 | m_core = new SyncCore(m_syncLog, localUserName, Name("/"), syncPrefix, |
| 77 | bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx, |
| 78 | DEFAULT_SYNC_INTEREST_INTERVAL); |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 79 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 80 | FetchTaskDbPtr actionTaskDb = make_shared<FetchTaskDb>(m_rootDir, "action"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 81 | m_actionFetcher = |
| 82 | make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1), |
| 83 | Name(BROADCAST_DOMAIN), // no appname suffix now |
| 84 | 3, |
| 85 | bind(&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), |
| 86 | FetchManager::FinishCallback(), actionTaskDb); |
Zhenkai Zhu | b8c49af | 2013-01-29 16:03:56 -0800 | [diff] [blame] | 87 | |
Zhenkai Zhu | da68688 | 2013-01-29 22:32:24 -0800 | [diff] [blame] | 88 | FetchTaskDbPtr fileTaskDb = make_shared<FetchTaskDb>(m_rootDir, "file"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | m_fileFetcher = |
| 90 | make_shared<FetchManager>(m_ccnx, bind(&SyncLog::LookupLocator, &*m_syncLog, _1), |
| 91 | Name(BROADCAST_DOMAIN), // no appname suffix now |
| 92 | 3, bind(&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, |
| 93 | _3, _4), |
| 94 | bind(&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2), |
| 95 | fileTaskDb); |
Zhenkai Zhu | b8c49af | 2013-01-29 16:03:56 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 97 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 98 | if (m_enablePrefixDiscovery) { |
Zhenkai Zhu | 3f64d2d | 2013-01-25 14:34:59 -0800 | [diff] [blame] | 99 | _LOG_DEBUG("registering prefix discovery in Dispatcher"); |
| 100 | string tag = "dispatcher" + m_localUserName.toString(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 101 | Ccnx::CcnxDiscovery::registerCallback( |
| 102 | TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag)); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 103 | } |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 105 | m_executor.start(); |
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"); |
| 111 | m_executor.shutdown(); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 112 | |
| 113 | // _LOG_DEBUG (">>"); |
| 114 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 115 | if (m_enablePrefixDiscovery) { |
Zhenkai Zhu | 3f64d2d | 2013-01-25 14:34:59 -0800 | [diff] [blame] | 116 | _LOG_DEBUG("deregistering prefix discovery in Dispatcher"); |
| 117 | string tag = "dispatcher" + m_localUserName.toString(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | Ccnx::CcnxDiscovery::deregisterCallback( |
| 119 | TaggedFunction(bind(&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag)); |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 120 | } |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 122 | if (m_core != NULL) { |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 123 | delete m_core; |
| 124 | m_core = NULL; |
| 125 | } |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 127 | if (m_server != NULL) { |
Zhenkai Zhu | 1dcbbab | 2013-01-22 16:03:20 -0800 | [diff] [blame] | 128 | delete m_server; |
| 129 | m_server = NULL; |
| 130 | } |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 131 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 132 | if (m_stateServer != NULL) { |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 133 | delete m_stateServer; |
| 134 | m_stateServer = NULL; |
| 135 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 138 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 139 | Dispatcher::Did_LocalPrefix_Updated(const Ccnx::Name& forwardingHint) |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 140 | { |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 141 | Name effectiveForwardingHint; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 142 | if (m_localUserName.size() >= forwardingHint.size() && |
| 143 | m_localUserName.getPartialName(0, forwardingHint.size()) == forwardingHint) { |
| 144 | effectiveForwardingHint = Name("/"); // "directly" accesible |
| 145 | } |
| 146 | else { |
| 147 | effectiveForwardingHint = forwardingHint; |
| 148 | } |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 149 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 150 | Name oldLocalPrefix = m_syncLog->LookupLocalLocator(); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 151 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 152 | if (oldLocalPrefix == effectiveForwardingHint) { |
| 153 | _LOG_DEBUG( |
| 154 | "Got notification about prefix change from " << oldLocalPrefix << " to: " << forwardingHint |
| 155 | << ", but effective prefix didn't change"); |
| 156 | return; |
| 157 | } |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 159 | if (effectiveForwardingHint == Name("/") || effectiveForwardingHint == Name(BROADCAST_DOMAIN)) { |
| 160 | _LOG_DEBUG("Basic effective prefix [" << effectiveForwardingHint |
| 161 | << "]. Updating local prefix, but don't reregister"); |
| 162 | m_syncLog->UpdateLocalLocator(effectiveForwardingHint); |
| 163 | return; |
| 164 | } |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 166 | _LOG_DEBUG("LocalPrefix changed from: " << oldLocalPrefix << " to: " << effectiveForwardingHint); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 167 | |
| 168 | m_server->registerPrefix(effectiveForwardingHint); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 169 | m_syncLog->UpdateLocalLocator(effectiveForwardingHint); |
Alexander Afanasyev | 7aced75 | 2013-02-13 09:57:25 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 171 | if (oldLocalPrefix == Name("/") || oldLocalPrefix == Name(BROADCAST_DOMAIN)) { |
| 172 | _LOG_DEBUG("Don't deregister basic prefix: " << oldLocalPrefix); |
| 173 | return; |
| 174 | } |
Alexander Afanasyev | 758f51b | 2013-01-24 13:48:18 -0800 | [diff] [blame] | 175 | m_server->deregisterPrefix(oldLocalPrefix); |
| 176 | } |
| 177 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 178 | // moved to state-server |
| 179 | // void |
| 180 | // Dispatcher::Restore_LocalFile (FileItemPtr file) |
| 181 | // { |
| 182 | // m_executor.execute (bind (&Dispatcher::Restore_LocalFile_Execute, this, file)); |
| 183 | // } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 184 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 185 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 186 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 187 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 188 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 189 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 190 | Dispatcher::Did_LocalFile_AddOrModify(const filesystem::path& relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 191 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 192 | m_executor.execute(bind(&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath)); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 196 | Dispatcher::Did_LocalFile_AddOrModify_Execute(filesystem::path relativeFilePath) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 197 | { |
Zhenkai Zhu | faee2d4 | 2013-01-24 17:47:13 -0800 | [diff] [blame] | 198 | _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute"); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 199 | filesystem::path absolutePath = m_rootDir / relativeFilePath; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 200 | if (!filesystem::exists(absolutePath)) { |
| 201 | //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() )); |
| 202 | _LOG_DEBUG("Update non exist file: " << absolutePath.string()); |
| 203 | return; |
| 204 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 205 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 206 | FileItemPtr currentFile = m_fileState->LookupFile(relativeFilePath.generic_string()); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 207 | if (currentFile && |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 208 | *Hash::FromFileContent(absolutePath) == |
| 209 | Hash(currentFile->file_hash().c_str(), currentFile->file_hash().size()) |
Zhenkai Zhu | 1c036bf | 2013-01-24 15:01:17 -0800 | [diff] [blame] | 210 | // The following two are commented out to prevent front end from reporting intermediate files |
| 211 | // should enable it if there is other way to prevent this |
| 212 | // && last_write_time (absolutePath) == currentFile->mtime () |
| 213 | // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ()) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 214 | ) { |
| 215 | _LOG_ERROR("Got notification about the same file [" << relativeFilePath << "]"); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | if (currentFile && !currentFile->is_complete()) { |
| 220 | _LOG_ERROR("Got notification about incomplete file [" << relativeFilePath << "]"); |
| 221 | return; |
| 222 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 223 | |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 224 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 225 | int seg_num; |
| 226 | HashPtr hash; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 227 | tie(hash, seg_num) = m_objectManager.localFileToObjects(absolutePath, m_localUserName); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 229 | try { |
| 230 | m_actionLog->AddLocalActionUpdate(relativeFilePath.generic_string(), |
| 231 | *hash, |
| 232 | last_write_time(absolutePath), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 233 | #if BOOST_VERSION >= 104900 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 234 | status(absolutePath).permissions(), |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 235 | #else |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 236 | 0, |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 237 | #endif |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 238 | seg_num); |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 239 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 240 | // notify SyncCore to propagate the change |
| 241 | m_core->localStateChangedDelayed(); |
| 242 | } |
| 243 | catch (filesystem::filesystem_error& error) { |
| 244 | _LOG_ERROR("File operations failed on [" << relativeFilePath << "] (ignoring)"); |
| 245 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 249 | Dispatcher::Did_LocalFile_Delete(const filesystem::path& relativeFilePath) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 250 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 251 | m_executor.execute(bind(&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 255 | Dispatcher::Did_LocalFile_Delete_Execute(filesystem::path relativeFilePath) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 256 | { |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 257 | filesystem::path absolutePath = m_rootDir / relativeFilePath; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 258 | if (filesystem::exists(absolutePath)) { |
| 259 | //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() )); |
| 260 | _LOG_ERROR("DELETE command, but file still exists: " << absolutePath.string()); |
| 261 | return; |
| 262 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 264 | m_actionLog->AddLocalActionDelete(relativeFilePath.generic_string()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 265 | // notify SyncCore to propagate the change |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 266 | m_core->localStateChangedDelayed(); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 270 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 271 | ///////////////////////////////////////////////////////////////////////////////////////////////////// |
| 272 | |
| 273 | /** |
| 274 | * Callbacks: |
| 275 | * |
| 276 | * - from SyncLog: when state changes -> to fetch missing actions |
| 277 | * |
| 278 | * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action |
| 279 | * -> to add action to the action log |
| 280 | * |
| 281 | * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file |
| 282 | * |
| 283 | * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing? |
| 284 | * |
| 285 | * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb |
| 286 | * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise |
| 287 | */ |
| 288 | |
| 289 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 290 | Dispatcher::Did_SyncLog_StateChange(SyncStateMsgPtr stateMsg) |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 291 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 292 | m_executor.execute(bind(&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg)); |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 296 | Dispatcher::Did_SyncLog_StateChange_Execute(SyncStateMsgPtr stateMsg) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 297 | { |
| 298 | int size = stateMsg->state_size(); |
| 299 | int index = 0; |
| 300 | // iterate and fetch the actions |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 301 | for (; index < size; index++) { |
| 302 | SyncState state = stateMsg->state(index); |
| 303 | if (state.has_old_seq() && state.has_seq()) { |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 304 | uint64_t oldSeq = state.old_seq(); |
| 305 | uint64_t newSeq = state.seq(); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 306 | Name userName(reinterpret_cast<const unsigned char*>(state.name().c_str()), |
| 307 | state.name().size()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 308 | |
| 309 | // fetch actions with oldSeq + 1 to newSeq (inclusive) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 310 | Name actionNameBase = Name("/")(userName)(CHRONOSHARE_APP)("action")(m_sharedFolder); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 311 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 312 | m_actionFetcher->Enqueue(userName, actionNameBase, std::max<uint64_t>(oldSeq + 1, 1), newSeq, |
| 313 | FetchManager::PRIORITY_HIGH); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | |
Alexander Afanasyev | fc72036 | 2013-01-24 21:49:48 -0800 | [diff] [blame] | 318 | |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 319 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 320 | Dispatcher::Did_FetchManager_ActionFetch(const Ccnx::Name& deviceName, |
| 321 | const Ccnx::Name& actionBaseName, uint32_t seqno, |
| 322 | Ccnx::PcoPtr actionPco) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 323 | { |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 324 | /// @todo Errors and exception checking |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 325 | _LOG_DEBUG("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName |
| 326 | << ", seqno: " |
| 327 | << seqno); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 328 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 329 | ActionItemPtr action = m_actionLog->AddRemoteAction(deviceName, seqno, actionPco); |
| 330 | if (!action) { |
| 331 | _LOG_ERROR("AddRemoteAction did not insert action, ignoring"); |
| 332 | return; |
| 333 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 334 | // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 335 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 336 | if (action->action() == ActionItem::UPDATE) { |
| 337 | Hash hash(action->file_hash().c_str(), action->file_hash().size()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 338 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 339 | Name fileNameBase = |
| 340 | Name("/")(deviceName)(CHRONOSHARE_APP)("file")(hash.GetHash(), hash.GetHashBytes()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 341 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 342 | string hashStr = lexical_cast<string>(hash); |
| 343 | if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName, hashStr)) { |
| 344 | _LOG_DEBUG( |
| 345 | "File already exists in the database. No need to refetch, just directly applying the action"); |
| 346 | Did_FetchManager_FileFetchComplete(deviceName, fileNameBase); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 347 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 348 | else { |
| 349 | if (m_objectDbMap.find(hash) == m_objectDbMap.end()) { |
| 350 | _LOG_DEBUG("create ObjectDb for " << hash); |
| 351 | m_objectDbMap[hash] = make_shared<ObjectDb>(m_rootDir / ".chronoshare", hashStr); |
| 352 | } |
| 353 | |
| 354 | m_fileFetcher->Enqueue(deviceName, fileNameBase, 0, action->seg_num() - 1, |
| 355 | FetchManager::PRIORITY_NORMAL); |
| 356 | } |
| 357 | } |
Alexander Afanasyev | a2fabcf | 2013-02-05 11:26:37 -0800 | [diff] [blame] | 358 | // if necessary (when version number is the highest) delete will be applied through the trigger in m_actionLog->AddRemoteAction call |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 362 | Dispatcher::Did_ActionLog_ActionApply_Delete(const std::string& filename) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 363 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 364 | m_executor.execute(bind(&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 368 | Dispatcher::Did_ActionLog_ActionApply_Delete_Execute(std::string filename) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 369 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 370 | _LOG_DEBUG("Action to delete " << filename); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 371 | |
| 372 | filesystem::path absolutePath = m_rootDir / filename; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 373 | try { |
| 374 | if (filesystem::exists(absolutePath)) { |
| 375 | // need some protection from local detection of removal |
| 376 | remove(absolutePath); |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 377 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 378 | // hack to remove empty parent dirs |
| 379 | filesystem::path parentPath = absolutePath.parent_path(); |
| 380 | while (parentPath > m_rootDir) { |
| 381 | if (filesystem::is_empty(parentPath)) { |
| 382 | filesystem::remove(parentPath); |
| 383 | parentPath = parentPath.parent_path(); |
| 384 | } |
| 385 | else { |
| 386 | break; |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 387 | } |
| 388 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 389 | } |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 390 | // don't exist |
| 391 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 392 | catch (filesystem::filesystem_error& error) { |
| 393 | _LOG_ERROR("File operations failed when removing [" << absolutePath << "] (ignoring)"); |
Zhenkai Zhu | dd4359b | 2013-02-24 11:07:34 -0800 | [diff] [blame] | 394 | } |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 398 | Dispatcher::Did_FetchManager_FileSegmentFetch(const Ccnx::Name& deviceName, |
| 399 | const Ccnx::Name& fileSegmentBaseName, |
| 400 | uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 401 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 402 | m_executor.execute(bind(&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, |
| 403 | fileSegmentBaseName, segment, fileSegmentPco)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 407 | Dispatcher::Did_FetchManager_FileSegmentFetch_Execute(Ccnx::Name deviceName, |
| 408 | Ccnx::Name fileSegmentBaseName, |
| 409 | uint32_t segment, Ccnx::PcoPtr fileSegmentPco) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 410 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 411 | // fileSegmentBaseName: /<device_name>/<appname>/file/<hash> |
| 412 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 413 | const Bytes& hashBytes = fileSegmentBaseName.getCompFromBack(0); |
| 414 | Hash hash(head(hashBytes), hashBytes.size()); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 416 | _LOG_DEBUG("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName |
| 417 | << ", segment: " |
| 418 | << segment); |
Alexander Afanasyev | 28ca3ed | 2013-01-24 23:17:15 -0800 | [diff] [blame] | 419 | |
Alexander Afanasyev | 17507ba | 2013-01-24 23:47:34 -0800 | [diff] [blame] | 420 | // _LOG_DEBUG ("Looking up objectdb for " << hash); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 421 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 422 | map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find(hash); |
| 423 | if (db != m_objectDbMap.end()) { |
| 424 | db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf()); |
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 | else { |
| 427 | _LOG_ERROR("no db available for this content object: " << fileSegmentBaseName << ", size: " |
| 428 | << fileSegmentPco->buf().size()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 429 | } |
Alexander Afanasyev | 17507ba | 2013-01-24 23:47:34 -0800 | [diff] [blame] | 430 | |
| 431 | // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash)); |
| 432 | // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 436 | Dispatcher::Did_FetchManager_FileFetchComplete(const Ccnx::Name& deviceName, |
| 437 | const Ccnx::Name& fileBaseName) |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 438 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 439 | m_executor.execute( |
| 440 | bind(&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName)); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 441 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 442 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 443 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 444 | Dispatcher::Did_FetchManager_FileFetchComplete_Execute(Ccnx::Name deviceName, Ccnx::Name fileBaseName) |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 445 | { |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 446 | // fileBaseName: /<device_name>/<appname>/file/<hash> |
| 447 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 448 | _LOG_DEBUG("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName); |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 449 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 450 | const Bytes& hashBytes = fileBaseName.getCompFromBack(0); |
| 451 | Hash hash(head(hashBytes), hashBytes.size()); |
| 452 | _LOG_DEBUG("Extracted hash: " << hash.shortHash()); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 453 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 454 | if (m_objectDbMap.find(hash) != m_objectDbMap.end()) { |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 455 | // remove the db handle |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 456 | m_objectDbMap.erase(hash); // to commit write |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 457 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 458 | else { |
| 459 | _LOG_ERROR("no db available for this file: " << hash); |
Alexander Afanasyev | 1807e8d | 2013-01-24 23:37:32 -0800 | [diff] [blame] | 460 | } |
| 461 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 462 | FileItemsPtr filesToAssemble = m_fileState->LookupFilesForHash(hash); |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 463 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 464 | for (FileItems::iterator file = filesToAssemble->begin(); file != filesToAssemble->end(); file++) { |
| 465 | boost::filesystem::path filePath = m_rootDir / file->filename(); |
Yingdi Yu | adb54eb | 2013-08-15 10:28:28 -0700 | [diff] [blame] | 466 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 467 | try { |
| 468 | if (filesystem::exists(filePath) && filesystem::last_write_time(filePath) == file->mtime() && |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 469 | #if BOOST_VERSION >= 104900 |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 470 | filesystem::status(filePath).permissions() == static_cast<filesystem::perms>(file->mode()) && |
Alexander Afanasyev | f8ff5e1 | 2013-07-11 13:57:32 -0700 | [diff] [blame] | 471 | #endif |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 472 | *Hash::FromFileContent(filePath) == hash) { |
| 473 | _LOG_DEBUG("Asking to assemble a file, but file already exists on a filesystem"); |
| 474 | continue; |
Zhenkai Zhu | f3a9fa6 | 2013-01-31 17:23:09 -0800 | [diff] [blame] | 475 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 476 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 477 | catch (filesystem::filesystem_error& error) { |
| 478 | _LOG_ERROR("File operations failed on [" << filePath << "] (ignoring)"); |
| 479 | } |
| 480 | |
| 481 | if (ObjectDb::DoesExist(m_rootDir / ".chronoshare", deviceName, |
| 482 | boost::lexical_cast<string>(hash))) { |
| 483 | bool ok = m_objectManager.objectsToLocalFile(deviceName, hash, filePath); |
| 484 | if (ok) { |
| 485 | last_write_time(filePath, file->mtime()); |
| 486 | #if BOOST_VERSION >= 104900 |
| 487 | permissions(filePath, static_cast<filesystem::perms>(file->mode())); |
| 488 | #endif |
| 489 | |
| 490 | m_fileState->SetFileComplete(file->filename()); |
| 491 | } |
| 492 | else { |
| 493 | _LOG_ERROR("Notified about complete fetch, but file cannot be restored from the database: [" |
| 494 | << filePath |
| 495 | << "]"); |
| 496 | } |
| 497 | } |
| 498 | else { |
| 499 | _LOG_ERROR(filePath << " supposed to have all segments, but not"); |
| 500 | // should abort for debugging |
| 501 | } |
| 502 | } |
Zhenkai Zhu | c3fd51e | 2013-01-22 10:45:54 -0800 | [diff] [blame] | 503 | } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 504 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 505 | // moved to state-server |
| 506 | // void |
| 507 | // Dispatcher::Restore_LocalFile_Execute (FileItemPtr file) |
| 508 | // { |
| 509 | // _LOG_DEBUG ("Got request to restore local file [" << file->filename () << "]"); |
| 510 | // // the rest will gracefully fail if object-db is missing or incomplete |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 511 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 512 | // boost::filesystem::path filePath = m_rootDir / file->filename (); |
| 513 | // Name deviceName (file->device_name ().c_str (), file->device_name ().size ()); |
| 514 | // Hash hash (file->file_hash ().c_str (), file->file_hash ().size ()); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 515 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 516 | // try |
| 517 | // { |
| 518 | // if (filesystem::exists (filePath) && |
| 519 | // filesystem::last_write_time (filePath) == file->mtime () && |
| 520 | // filesystem::status (filePath).permissions () == static_cast<filesystem::perms> (file->mode ()) && |
| 521 | // *Hash::FromFileContent (filePath) == hash) |
| 522 | // { |
| 523 | // _LOG_DEBUG ("Asking to assemble a file, but file already exists on a filesystem"); |
| 524 | // return; |
| 525 | // } |
| 526 | // } |
| 527 | // catch (filesystem::filesystem_error &error) |
| 528 | // { |
| 529 | // _LOG_ERROR ("File operations failed on [" << filePath << "] (ignoring)"); |
| 530 | // } |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 531 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 532 | // m_objectManager.objectsToLocalFile (deviceName, hash, filePath); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 533 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 534 | // last_write_time (filePath, file->mtime ()); |
| 535 | // permissions (filePath, static_cast<filesystem::perms> (file->mode ())); |
Alexander Afanasyev | 0a30a0c | 2013-01-29 17:25:42 -0800 | [diff] [blame] | 536 | |
Alexander Afanasyev | 026eaf3 | 2013-02-23 16:37:14 -0800 | [diff] [blame] | 537 | // } |