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. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -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. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -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. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -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. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -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. |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 19 | */ |
| 20 | |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 21 | #include "object-manager.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 22 | #include "ccnx-common.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 23 | #include "ccnx-name.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 24 | #include "ccnx-pco.hpp" |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 25 | #include "logging.hpp" |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 26 | #include "object-db.hpp" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 27 | |
| 28 | #include <sys/stat.h> |
| 29 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 30 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
| 32 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 33 | #include <fstream> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 35 | _LOG_INIT(Object.Manager); |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 37 | using namespace Ndnx; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 38 | using namespace boost; |
| 39 | using namespace std; |
| 40 | namespace fs = boost::filesystem; |
| 41 | |
| 42 | const int MAX_FILE_SEGMENT_SIZE = 1024; |
| 43 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 44 | ObjectManager::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 Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 49 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 50 | fs::create_directories(m_folder); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 53 | ObjectManager::~ObjectManager() |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 54 | { |
| 55 | } |
| 56 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 57 | // /<devicename>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 58 | boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/> |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | ObjectManager::localFileToObjects(const fs::path& file, const Ccnx::Name& deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 61 | HashPtr fileHash = Hash::FromFileContent(file); |
| 62 | ObjectDb fileDb(m_folder, lexical_cast<string>(*fileHash)); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 63 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 64 | fs::ifstream iff(file, std::ios::in | std::ios::binary); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 65 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 66 | 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 Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 72 | } |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 73 | |
| 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 Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 86 | if (segment == 0) // handle empty files |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | { |
| 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 Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | segment++; |
| 94 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 96 | return make_tuple(fileHash, segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 97 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 98 | |
| 99 | bool |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 100 | ObjectManager::objectsToLocalFile(/*in*/ const Ccnx::Name& deviceName, /*in*/ const Hash& fileHash, |
| 101 | /*out*/ const fs::path& file) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 102 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 103 | 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 Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | if (!exists(file.parent_path())) { |
| 111 | create_directories(file.parent_path()); |
| 112 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 113 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 114 | fs::ofstream off(file, std::ios::out | std::ios::binary); |
| 115 | ObjectDb fileDb(m_folder, hashStr); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 116 | |
| 117 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | BytesPtr bytes = fileDb.fetchSegment(deviceName, 0); |
| 119 | while (bytes) { |
| 120 | ParsedContentObject obj(*bytes); |
| 121 | BytesPtr data = obj.contentPtr(); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 123 | if (data) { |
| 124 | off.write(reinterpret_cast<const char*>(head(*data)), data->size()); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 127 | segment++; |
| 128 | bytes = fileDb.fetchSegment(deviceName, segment); |
| 129 | } |
| 130 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 131 | // permission and timestamp should be assigned somewhere else (ObjectManager has no idea about that) |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 132 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 133 | return true; |
| 134 | } |