blob: d9000988d356c9596b1b8e08e972c222d02032ec [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -08003 * Copyright (c) 2013-2017, Regents of the University of California.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * ChronoShare is free software: you can redistribute it and/or modify it under the terms
8 * of the GNU General Public License as published by the Free Software Foundation, either
9 * version 3 of the License, or (at your option) any later version.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * You should have received copies of the GNU General Public License along with
16 * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * See AUTHORS.md for complete list of ChronoShare authors and contributors.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#include "object-manager.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080022#include "ccnx-common.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080023#include "ccnx-name.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080024#include "ccnx-pco.hpp"
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080025#include "logging.hpp"
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080026#include "object-db.hpp"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080027
28#include <sys/stat.h>
29
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080030#include <boost/filesystem/fstream.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080031#include <boost/lexical_cast.hpp>
32#include <boost/throw_exception.hpp>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080033#include <fstream>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080034
Alexander Afanasyev1cf5c432017-01-13 23:22:15 -080035_LOG_INIT(Object.Manager);
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080036
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070037using namespace Ndnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080038using namespace boost;
39using namespace std;
40namespace fs = boost::filesystem;
41
42const int MAX_FILE_SEGMENT_SIZE = 1024;
43
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080044ObjectManager::ObjectManager(Ccnx::CcnxWrapperPtr ccnx, const fs::path& folder,
45 const std::string& appName)
46 : m_ccnx(ccnx)
47 , m_folder(folder / ".chronoshare")
48 , m_appName(appName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080049{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080050 fs::create_directories(m_folder);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080051}
52
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080053ObjectManager::~ObjectManager()
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080054{
55}
56
Alexander Afanasyev4d086752013-02-07 13:06:04 -080057// /<devicename>/<appname>/file/<hash>/<segment>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080058boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/>
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080059ObjectManager::localFileToObjects(const fs::path& file, const Ccnx::Name& deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080060{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080061 HashPtr fileHash = Hash::FromFileContent(file);
62 ObjectDb fileDb(m_folder, lexical_cast<string>(*fileHash));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080063
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080064 fs::ifstream iff(file, std::ios::in | std::ios::binary);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080065 sqlite3_int64 segment = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080066 while (iff.good() && !iff.eof()) {
67 char buf[MAX_FILE_SEGMENT_SIZE];
68 iff.read(buf, MAX_FILE_SEGMENT_SIZE);
69 if (iff.gcount() == 0) {
70 // stupid streams...
71 break;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080072 }
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080073
74 Name name = Name("/")(deviceName)(m_appName)("file")(fileHash->GetHash(),
75 fileHash->GetHashBytes())(segment);
76
77 // cout << *fileHash << endl;
78 // cout << name << endl;
79 //_LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment);
80
81 Bytes data = m_ccnx->createContentObject(name, buf, iff.gcount());
82 fileDb.saveContentObject(deviceName, segment, data);
83
84 segment++;
85 }
Alexander Afanasyevac704622013-01-25 17:50:34 -080086 if (segment == 0) // handle empty files
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080087 {
88 Name name =
89 Name("/")(m_appName)("file")(fileHash->GetHash(), fileHash->GetHashBytes())(deviceName)(0);
90 Bytes data = m_ccnx->createContentObject(name, 0, 0);
91 fileDb.saveContentObject(deviceName, 0, data);
Alexander Afanasyevac704622013-01-25 17:50:34 -080092
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080093 segment++;
94 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080095
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -080096 return make_tuple(fileHash, segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080097}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080098
99bool
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800100ObjectManager::objectsToLocalFile(/*in*/ const Ccnx::Name& deviceName, /*in*/ const Hash& fileHash,
101 /*out*/ const fs::path& file)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800102{
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800103 string hashStr = lexical_cast<string>(fileHash);
104 if (!ObjectDb::DoesExist(m_folder, deviceName, hashStr)) {
105 _LOG_ERROR("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr
106 << "] does not exist or not all segments are available");
107 return false;
108 }
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800109
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800110 if (!exists(file.parent_path())) {
111 create_directories(file.parent_path());
112 }
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800113
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800114 fs::ofstream off(file, std::ios::out | std::ios::binary);
115 ObjectDb fileDb(m_folder, hashStr);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800116
117 sqlite3_int64 segment = 0;
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800118 BytesPtr bytes = fileDb.fetchSegment(deviceName, 0);
119 while (bytes) {
120 ParsedContentObject obj(*bytes);
121 BytesPtr data = obj.contentPtr();
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800122
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800123 if (data) {
124 off.write(reinterpret_cast<const char*>(head(*data)), data->size());
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800125 }
126
Alexander Afanasyeveda3b7a2016-12-25 11:26:40 -0800127 segment++;
128 bytes = fileDb.fetchSegment(deviceName, segment);
129 }
130
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800131 // permission and timestamp should be assigned somewhere else (ObjectManager has no idea about that)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800132
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800133 return true;
134}