Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2016, 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" |
| 22 | #include "ccnx-name.hpp" |
| 23 | #include "ccnx-common.hpp" |
| 24 | #include "ccnx-pco.hpp" |
| 25 | #include "object-db.hpp" |
| 26 | #include "logging.hpp" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 27 | |
| 28 | #include <sys/stat.h> |
| 29 | |
| 30 | #include <fstream> |
| 31 | #include <boost/lexical_cast.hpp> |
| 32 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 33 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 34 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 35 | INIT_LOGGER ("Object.Manager"); |
| 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 | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 44 | ObjectManager::ObjectManager (Ndnx::NdnxWrapperPtr ndnx, const fs::path &folder, const std::string &appName) |
| 45 | : m_ndnx (ndnx) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 46 | , m_folder (folder / ".chronoshare") |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 47 | , m_appName (appName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 48 | { |
| 49 | fs::create_directories (m_folder); |
| 50 | } |
| 51 | |
| 52 | ObjectManager::~ObjectManager () |
| 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 56 | // /<devicename>/<appname>/file/<hash>/<segment> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 57 | boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/> |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 58 | ObjectManager::localFileToObjects (const fs::path &file, const Ndnx::Name &deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 59 | { |
| 60 | HashPtr fileHash = Hash::FromFileContent (file); |
| 61 | ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash)); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 63 | fs::ifstream iff (file, std::ios::in | std::ios::binary); |
| 64 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | 2a6fa2b | 2013-01-23 16:51:55 -0800 | [diff] [blame] | 65 | while (iff.good () && !iff.eof ()) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 66 | { |
| 67 | char buf[MAX_FILE_SEGMENT_SIZE]; |
| 68 | iff.read (buf, MAX_FILE_SEGMENT_SIZE); |
Alexander Afanasyev | 2a6fa2b | 2013-01-23 16:51:55 -0800 | [diff] [blame] | 69 | if (iff.gcount () == 0) |
| 70 | { |
| 71 | // stupid streams... |
| 72 | break; |
| 73 | } |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 74 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 75 | Name name = Name ("/")(deviceName)(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 77 | // cout << *fileHash << endl; |
| 78 | // cout << name << endl; |
Zhenkai Zhu | 81907f2 | 2013-01-23 17:32:34 -0800 | [diff] [blame] | 79 | //_LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 80 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 81 | Bytes data = m_ndnx->createContentObject (name, buf, iff.gcount ()); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 82 | fileDb.saveContentObject (deviceName, segment, data); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 84 | segment ++; |
| 85 | } |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 86 | if (segment == 0) // handle empty files |
| 87 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 88 | Name name = Name ("/")(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(deviceName)(0); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 89 | Bytes data = m_ndnx->createContentObject (name, 0, 0); |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 90 | fileDb.saveContentObject (deviceName, 0, data); |
| 91 | |
| 92 | segment ++; |
| 93 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 95 | return make_tuple (fileHash, segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 96 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 97 | |
| 98 | bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 99 | ObjectManager::objectsToLocalFile (/*in*/const Ndnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 100 | { |
| 101 | string hashStr = lexical_cast<string> (fileHash); |
| 102 | if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr)) |
| 103 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 104 | _LOG_ERROR ("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr << "] does not exist or not all segments are available"); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 105 | return false; |
| 106 | } |
| 107 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 108 | if (!exists (file.parent_path ())) |
| 109 | { |
| 110 | create_directories (file.parent_path ()); |
| 111 | } |
| 112 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 113 | fs::ofstream off (file, std::ios::out | std::ios::binary); |
| 114 | ObjectDb fileDb (m_folder, hashStr); |
| 115 | |
| 116 | sqlite3_int64 segment = 0; |
| 117 | BytesPtr bytes = fileDb.fetchSegment (deviceName, 0); |
Alexander Afanasyev | e04712b | 2013-01-25 17:53:26 -0800 | [diff] [blame] | 118 | while (bytes) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 119 | { |
| 120 | ParsedContentObject obj (*bytes); |
| 121 | BytesPtr data = obj.contentPtr (); |
| 122 | |
Alexander Afanasyev | e04712b | 2013-01-25 17:53:26 -0800 | [diff] [blame] | 123 | if (data) |
| 124 | { |
| 125 | off.write (reinterpret_cast<const char*> (head(*data)), data->size()); |
| 126 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 128 | segment ++; |
| 129 | bytes = fileDb.fetchSegment (deviceName, segment); |
| 130 | } |
| 131 | |
| 132 | // 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] | 133 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 134 | return true; |
| 135 | } |