blob: 22d18306343386cdd0d0770a9b9a031625083286 [file] [log] [blame]
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
22#include "dispatcher.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080023#include "logging.h"
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080024#include "ccnx-discovery.h"
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080025
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080026#include <boost/make_shared.hpp>
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080027#include <boost/lexical_cast.hpp>
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080028
29using namespace Ccnx;
30using namespace std;
31using namespace boost;
32
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080033INIT_LOGGER ("Dispatcher");
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080034
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080035static const string BROADCAST_DOMAIN = "/ndn/broadcast/chronoshare";
36
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080037Dispatcher::Dispatcher(const std::string &localUserName
38 , const std::string &sharedFolder
39 , const filesystem::path &rootDir
40 , Ccnx::CcnxWrapperPtr ccnx
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080041 , bool enablePrefixDiscovery
42 )
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080043 : m_ccnx(ccnx)
44 , m_core(NULL)
45 , m_rootDir(rootDir)
Zhenkai Zhua7ed62a2013-01-25 13:14:37 -080046 , m_executor(1) // creates problems with file assembly. need to ensure somehow that FinishExectute is called after all Segment_Execute finished
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080047 , m_objectManager(ccnx, rootDir)
48 , m_localUserName(localUserName)
49 , m_sharedFolder(sharedFolder)
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080050 , m_server(NULL)
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080051 , m_enablePrefixDiscovery(enablePrefixDiscovery)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080052{
Zhenkai Zhu3290b8e2013-01-24 15:25:48 -080053 m_syncLog = make_shared<SyncLog>(m_rootDir, localUserName);
54 m_actionLog = make_shared<ActionLog>(m_ccnx, m_rootDir, m_syncLog, sharedFolder,
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080055 // bind (&Dispatcher::Did_ActionLog_ActionApply_AddOrModify, this, _1, _2, _3, _4, _5, _6, _7),
56 ActionLog::OnFileAddedOrChangedCallback (), // don't really need this callback
57 bind (&Dispatcher::Did_ActionLog_ActionApply_Delete, this, _1));
Alexander Afanasyev1b15cc62013-01-22 17:00:40 -080058 Name syncPrefix = Name(BROADCAST_DOMAIN)(sharedFolder);
Alexander Afanasyevcbda9922013-01-22 11:21:12 -080059
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -080060 m_server = new ContentServer(m_ccnx, m_actionLog, rootDir, m_localUserName, m_sharedFolder);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080061 m_server->registerPrefix(Name ("/"));
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080062 m_server->registerPrefix(Name(BROADCAST_DOMAIN));
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080063
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080064 m_core = new SyncCore (m_syncLog, localUserName, Name ("/"), syncPrefix,
Alexander Afanasyevd94a8c62013-01-24 13:53:40 -080065 bind(&Dispatcher::Did_SyncLog_StateChange, this, _1), ccnx);
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080066
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080067 m_actionFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
68 m_fileFetcher = make_shared<FetchManager> (m_ccnx, bind (&SyncLog::LookupLocator, &*m_syncLog, _1), 3);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080069
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080070 if (m_enablePrefixDiscovery)
71 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080072 _LOG_DEBUG("registering prefix discovery in Dispatcher");
73 string tag = "dispatcher" + m_localUserName.toString();
74 Ccnx::CcnxDiscovery::registerCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080075 }
Alexander Afanasyevfc720362013-01-24 21:49:48 -080076
77 m_executor.start ();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080078}
79
80Dispatcher::~Dispatcher()
81{
Alexander Afanasyevfc720362013-01-24 21:49:48 -080082 // _LOG_DEBUG ("Enter destructor of dispatcher");
83 m_executor.shutdown ();
84
85 // _LOG_DEBUG (">>");
86
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080087 if (m_enablePrefixDiscovery)
88 {
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -080089 _LOG_DEBUG("deregistering prefix discovery in Dispatcher");
90 string tag = "dispatcher" + m_localUserName.toString();
91 Ccnx::CcnxDiscovery::deregisterCallback (TaggedFunction (bind (&Dispatcher::Did_LocalPrefix_Updated, this, _1), tag));
Zhenkai Zhufaee2d42013-01-24 17:47:13 -080092 }
Alexander Afanasyev758f51b2013-01-24 13:48:18 -080093
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -080094 if (m_core != NULL)
95 {
96 delete m_core;
97 m_core = NULL;
98 }
Zhenkai Zhu1dcbbab2013-01-22 16:03:20 -080099
100 if (m_server != NULL)
101 {
102 delete m_server;
103 m_server = NULL;
104 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800105}
106
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800107void
108Dispatcher::Did_LocalPrefix_Updated (const Ccnx::Name &prefix)
109{
110 Name oldLocalPrefix = m_syncLog->LookupLocalLocator ();
111 _LOG_DEBUG ("LocalPrefix changed from: " << oldLocalPrefix << " to: " << prefix);
112
Zhenkai Zhu3f64d2d2013-01-25 14:34:59 -0800113 m_server->registerPrefix(prefix);
Alexander Afanasyev758f51b2013-01-24 13:48:18 -0800114 m_syncLog->UpdateLocalLocator (prefix);
115 m_server->deregisterPrefix(oldLocalPrefix);
116}
117
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800118/////////////////////////////////////////////////////////////////////////////////////////////////////
119/////////////////////////////////////////////////////////////////////////////////////////////////////
120/////////////////////////////////////////////////////////////////////////////////////////////////////
121
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800122void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800123Dispatcher::Did_LocalFile_AddOrModify (const filesystem::path &relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800124{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800125 m_executor.execute (bind (&Dispatcher::Did_LocalFile_AddOrModify_Execute, this, relativeFilePath));
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800126}
127
128void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800129Dispatcher::Did_LocalFile_AddOrModify_Execute (filesystem::path relativeFilePath)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800130{
Zhenkai Zhufaee2d42013-01-24 17:47:13 -0800131 _LOG_DEBUG(m_localUserName << " calls LocalFile_AddOrModify_Execute");
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800132 filesystem::path absolutePath = m_rootDir / relativeFilePath;
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800133 if (!filesystem::exists(absolutePath))
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800134 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800135 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Update non exist file: " + absolutePath.string() ));
136 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
137 return;
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800138 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800139
140 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
141 if (currentFile &&
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800142 *Hash::FromFileContent (absolutePath) == Hash (currentFile->file_hash ().c_str (), currentFile->file_hash ().size ())
143 // The following two are commented out to prevent front end from reporting intermediate files
144 // should enable it if there is other way to prevent this
145 // && last_write_time (absolutePath) == currentFile->mtime ()
146 // && status (absolutePath).permissions () == static_cast<filesystem::perms> (currentFile->mode ())
147 )
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800148 {
149 _LOG_ERROR ("Got notification about the same file [" << relativeFilePath << "]");
150 return;
151 }
152
153 int seg_num;
154 HashPtr hash;
155 tie (hash, seg_num) = m_objectManager.localFileToObjects (absolutePath, m_localUserName);
156
157 m_actionLog->AddLocalActionUpdate (relativeFilePath.generic_string(),
158 *hash,
159 last_write_time (absolutePath), status (absolutePath).permissions (), seg_num);
160
161 // notify SyncCore to propagate the change
162 m_core->localStateChanged();
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800163}
164
165void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800166Dispatcher::Did_LocalFile_Delete (const filesystem::path &relativeFilePath)
167{
168 m_executor.execute (bind (&Dispatcher::Did_LocalFile_Delete_Execute, this, relativeFilePath));
169}
170
171void
172Dispatcher::Did_LocalFile_Delete_Execute (filesystem::path relativeFilePath)
173{
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800174 filesystem::path absolutePath = m_rootDir / relativeFilePath;
175 if (!filesystem::exists(absolutePath))
176 {
Zhenkai Zhu5f6181a2013-01-25 17:31:30 -0800177 //BOOST_THROW_EXCEPTION (Error::Dispatcher() << error_info_str("Delete notification but file exists: " + absolutePath.string() ));
178 _LOG_DEBUG("Update non exist file: " << absolutePath.string());
179 return;
180
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800181 }
182
183 FileItemPtr currentFile = m_actionLog->LookupFile (relativeFilePath.generic_string ());
184 if (!currentFile)
185 {
186 _LOG_ERROR ("File already deleted [" << relativeFilePath << "]");
187 return;
188 }
189
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800190 m_actionLog->AddLocalActionDelete (relativeFilePath.generic_string());
191 // notify SyncCore to propagate the change
192 m_core->localStateChanged();
193}
194
195/////////////////////////////////////////////////////////////////////////////////////////////////////
196/////////////////////////////////////////////////////////////////////////////////////////////////////
197/////////////////////////////////////////////////////////////////////////////////////////////////////
198
199/**
200 * Callbacks:
201 *
202 * - from SyncLog: when state changes -> to fetch missing actions
203 *
204 * - from FetchManager/Actions: when action is fetched -> to request a file, specified by the action
205 * -> to add action to the action log
206 *
207 * - from ActionLog/Delete: when action applied (file state changed, file deleted) -> to delete local file
208 *
209 * - from ActionLog/AddOrUpdate: when action applied (file state changes, file added or modified) -> do nothing?
210 *
211 * - from FetchManager/Files: when file segment is retrieved -> save it in ObjectDb
212 * when file fetch is completed -> if file belongs to FileState, then assemble it to filesystem. Don't do anything otherwise
213 */
214
215void
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800216Dispatcher::Did_SyncLog_StateChange (SyncStateMsgPtr stateMsg)
217{
218 m_executor.execute (bind (&Dispatcher::Did_SyncLog_StateChange_Execute, this, stateMsg));
219}
220
221void
222Dispatcher::Did_SyncLog_StateChange_Execute (SyncStateMsgPtr stateMsg)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800223{
224 int size = stateMsg->state_size();
225 int index = 0;
226 // iterate and fetch the actions
Alexander Afanasyeve6f2ae12013-01-24 21:50:00 -0800227 for (; index < size; index++)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800228 {
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800229 SyncState state = stateMsg->state (index);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800230 if (state.has_old_seq() && state.has_seq())
231 {
232 uint64_t oldSeq = state.old_seq();
233 uint64_t newSeq = state.seq();
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800234 Name userName (reinterpret_cast<const unsigned char *> (state.name ().c_str ()), state.name ().size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800235
236 // fetch actions with oldSeq + 1 to newSeq (inclusive)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800237 Name actionNameBase = Name(userName)("action")(m_sharedFolder);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800238
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800239 m_actionFetcher->Enqueue (userName, actionNameBase,
240 bind (&Dispatcher::Did_FetchManager_ActionFetch, this, _1, _2, _3, _4), FetchManager::FinishCallback (),
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800241 std::max<uint64_t> (oldSeq + 1, 1), newSeq, FetchManager::PRIORITY_HIGH);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800242 }
243 }
244}
245
Alexander Afanasyevfc720362013-01-24 21:49:48 -0800246
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800247void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800248Dispatcher::Did_FetchManager_ActionFetch (const Ccnx::Name &deviceName, const Ccnx::Name &actionBaseName, uint32_t seqno, Ccnx::PcoPtr actionPco)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800249{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800250 /// @todo Errors and exception checking
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800251 _LOG_DEBUG ("Received action deviceName: " << deviceName << ", actionBaseName: " << actionBaseName << ", seqno: " << seqno);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800252
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800253 ActionItemPtr action = m_actionLog->AddRemoteAction (deviceName, seqno, actionPco);
254 // trigger may invoke Did_ActionLog_ActionApply_Delete or Did_ActionLog_ActionApply_AddOrModify callbacks
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800255
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800256 if (action->action () == ActionItem::UPDATE)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800257 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800258 Hash hash (action->file_hash ().c_str(), action->file_hash ().size ());
259
260 Name fileNameBase = Name (deviceName)("file")(hash.GetHash (), hash.GetHashBytes ());
261
262 if (m_objectDbMap.find (hash) == m_objectDbMap.end ())
263 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800264 _LOG_DEBUG ("create ObjectDb for " << hash);
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800265 m_objectDbMap [hash] = make_shared<ObjectDb> (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800266 }
267
268 m_fileFetcher->Enqueue (deviceName, fileNameBase,
269 bind (&Dispatcher::Did_FetchManager_FileSegmentFetch, this, _1, _2, _3, _4),
270 bind (&Dispatcher::Did_FetchManager_FileFetchComplete, this, _1, _2),
Zhenkai Zhu1c036bf2013-01-24 15:01:17 -0800271 0, action->seg_num () - 1, FetchManager::PRIORITY_NORMAL);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800272 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800273}
274
275void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800276Dispatcher::Did_ActionLog_ActionApply_Delete (const std::string &filename)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800277{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800278 m_executor.execute (bind (&Dispatcher::Did_ActionLog_ActionApply_Delete_Execute, this, filename));
279}
280
281void
282Dispatcher::Did_ActionLog_ActionApply_Delete_Execute (std::string filename)
283{
284 _LOG_DEBUG ("Action to delete " << filename);
285
286 filesystem::path absolutePath = m_rootDir / filename;
287 if (filesystem::exists(absolutePath))
288 {
289 // need some protection from local detection of removal
290 remove (absolutePath);
291 }
292 // don't exist
293}
294
295void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800296Dispatcher::Did_FetchManager_FileSegmentFetch (const Ccnx::Name &deviceName, const Ccnx::Name &fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800297{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800298 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileSegmentFetch_Execute, this, deviceName, fileSegmentBaseName, segment, fileSegmentPco));
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800299}
300
301void
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800302Dispatcher::Did_FetchManager_FileSegmentFetch_Execute (Ccnx::Name deviceName, Ccnx::Name fileSegmentBaseName, uint32_t segment, Ccnx::PcoPtr fileSegmentPco)
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800303{
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800304 const Bytes &hashBytes = fileSegmentBaseName.getCompFromBack (0);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800305 Hash hash (head(hashBytes), hashBytes.size());
306
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800307 _LOG_DEBUG ("Received segment deviceName: " << deviceName << ", segmentBaseName: " << fileSegmentBaseName << ", segment: " << segment);
308
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800309 // _LOG_DEBUG ("Looking up objectdb for " << hash);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800310
311 map<Hash, ObjectDbPtr>::iterator db = m_objectDbMap.find (hash);
312 if (db != m_objectDbMap.end())
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800313 {
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800314 db->second->saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800315 }
316 else
317 {
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800318 _LOG_ERROR ("no db available for this content object: " << fileSegmentBaseName << ", size: " << fileSegmentPco->buf ().size());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800319 }
Alexander Afanasyev17507ba2013-01-24 23:47:34 -0800320
321 // ObjectDb objectDb (m_rootDir / ".chronoshare", lexical_cast<string> (hash));
322 // objectDb.saveContentObject(deviceName, segment, fileSegmentPco->buf ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800323}
324
325void
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800326Dispatcher::Did_FetchManager_FileFetchComplete (const Ccnx::Name &deviceName, const Ccnx::Name &fileBaseName)
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800327{
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800328 m_executor.execute (bind (&Dispatcher::Did_FetchManager_FileFetchComplete_Execute, this, deviceName, fileBaseName));
329}
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800330
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800331void
332Dispatcher::Did_FetchManager_FileFetchComplete_Execute (Ccnx::Name deviceName, Ccnx::Name fileBaseName)
333{
334 _LOG_DEBUG ("Finished fetching " << deviceName << ", fileBaseName: " << fileBaseName);
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800335
Alexander Afanasyev28ca3ed2013-01-24 23:17:15 -0800336 const Bytes &hashBytes = fileBaseName.getCompFromBack (0);
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800337 Hash hash (head (hashBytes), hashBytes.size ());
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800338
Alexander Afanasyev1807e8d2013-01-24 23:37:32 -0800339 if (m_objectDbMap.find (hash) != m_objectDbMap.end())
340 {
341 // remove the db handle
342 m_objectDbMap.erase (hash); // to commit write
343 }
344 else
345 {
346 _LOG_ERROR ("no db available for this file: " << hash);
347 }
348
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800349 FileItemsPtr filesToAssemble = m_actionLog->LookupFilesForHash (hash);
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800350
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800351 for (FileItems::iterator file = filesToAssemble->begin ();
352 file != filesToAssemble->end ();
353 file++)
354 {
355 boost::filesystem::path filePath = m_rootDir / file->filename ();
356 m_objectManager.objectsToLocalFile (deviceName, hash, filePath);
357
358 last_write_time (filePath, file->mtime ());
359 permissions (filePath, static_cast<filesystem::perms> (file->mode ()));
360 }
Zhenkai Zhuc3fd51e2013-01-22 10:45:54 -0800361}