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 | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 22 | #include "object-db.hpp" |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 23 | #include "core/logging.hpp" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 24 | |
| 25 | #include <sys/stat.h> |
| 26 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 27 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 29 | |
| 30 | #include <ndn-cxx/util/string-helper.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace chronoshare { |
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 | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 37 | namespace fs = boost::filesystem; |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 38 | using util::Sha256; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 39 | |
| 40 | const int MAX_FILE_SEGMENT_SIZE = 1024; |
| 41 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 42 | ObjectManager::ObjectManager(Face& face, KeyChain& keyChain, |
| 43 | const fs::path& folder, const std::string& appName) |
| 44 | : m_face(face) |
| 45 | , m_keyChain(keyChain) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 46 | , m_folder(folder / ".chronoshare") |
| 47 | , m_appName(appName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 48 | { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 49 | fs::create_directories(m_folder); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 52 | ObjectManager::~ObjectManager() |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 56 | // /<devicename>/<appname>/file/<hash>/<segment> |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 57 | std::tuple<ConstBufferPtr /*object-db name*/, size_t /* number of segments*/> |
| 58 | ObjectManager::localFileToObjects(const fs::path& file, const Name& deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 59 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 60 | fs::ifstream input1(file, std::ios::in | std::ios::binary); |
| 61 | Sha256 fileHash(input1); |
| 62 | ObjectDb fileDb(m_folder, fileHash.toString()); |
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 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 74 | Name name = Name("/"); |
| 75 | name.append(deviceName) |
| 76 | .append(m_appName) |
| 77 | .append("file") |
| 78 | .appendImplicitSha256Digest(fileHash.computeDigest()) |
| 79 | .appendNumber(segment); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 80 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 81 | shared_ptr<Data> data = make_shared<Data>(); |
| 82 | data->setName(name); |
| 83 | data->setFreshnessPeriod(time::seconds(60)); |
| 84 | data->setContent(reinterpret_cast<const uint8_t*>(&buf), iff.gcount()); |
| 85 | m_keyChain.sign(*data); |
| 86 | m_face.put(*data); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 87 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 88 | fileDb.saveContentObject(deviceName, segment, *data); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 89 | |
| 90 | segment++; |
| 91 | } |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 92 | if (segment == 0) // handle empty files |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 93 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 94 | Name name = Name("/"); |
| 95 | name.append(m_appName) |
| 96 | .append("file") |
| 97 | .appendImplicitSha256Digest(fileHash.computeDigest()) |
| 98 | .append(deviceName) |
| 99 | .appendNumber(0); |
| 100 | |
| 101 | shared_ptr<Data> data = make_shared<Data>(); |
| 102 | data->setName(name); |
| 103 | data->setFreshnessPeriod(time::seconds(0)); |
| 104 | data->setContent(0, 0); |
| 105 | m_keyChain.sign(*data); |
| 106 | m_face.put(*data); |
| 107 | |
| 108 | fileDb.saveContentObject(deviceName, 0, *data); |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 110 | segment++; |
| 111 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 112 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 113 | return std::make_tuple(fileHash.computeDigest(), segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 114 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 115 | |
| 116 | bool |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 117 | ObjectManager::objectsToLocalFile(/*in*/ const Name& deviceName, /*in*/ const Buffer& fileHash, |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 118 | /*out*/ const fs::path& file) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 119 | { |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 120 | std::string hashStr = toHex(fileHash); |
| 121 | if (!ObjectDb::doesExist(m_folder, deviceName, hashStr)) { |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 122 | _LOG_ERROR("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr |
| 123 | << "] does not exist or not all segments are available"); |
| 124 | return false; |
| 125 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 127 | if (!exists(file.parent_path())) { |
| 128 | create_directories(file.parent_path()); |
| 129 | } |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | fs::ofstream off(file, std::ios::out | std::ios::binary); |
| 132 | ObjectDb fileDb(m_folder, hashStr); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 133 | |
| 134 | sqlite3_int64 segment = 0; |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 135 | auto data = fileDb.fetchSegment(deviceName, 0); |
| 136 | while (data != nullptr) { |
| 137 | off.write(reinterpret_cast<const char*>(data->getContent().value()), data->getContent().value_size()); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 138 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 139 | ++segment; |
| 140 | data = fileDb.fetchSegment(deviceName, segment); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 143 | // 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] | 144 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 145 | return true; |
| 146 | } |
Lijing Wang | 9f77a8c | 2016-12-25 14:44:04 -0800 | [diff] [blame^] | 147 | |
| 148 | } // namespace chronoshare |
| 149 | } // namespace ndn |