Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 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 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 20 | */ |
| 21 | |
| 22 | #include "object-manager.h" |
| 23 | #include "ccnx-name.h" |
| 24 | #include "ccnx-common.h" |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 25 | #include "ccnx-pco.h" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 26 | #include "object-db.h" |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 27 | #include "logging.h" |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 28 | |
| 29 | #include <sys/stat.h> |
| 30 | |
| 31 | #include <fstream> |
| 32 | #include <boost/lexical_cast.hpp> |
| 33 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 34 | #include <boost/filesystem/fstream.hpp> |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 36 | INIT_LOGGER ("Object.Manager"); |
| 37 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 38 | using namespace Ccnx; |
| 39 | using namespace boost; |
| 40 | using namespace std; |
| 41 | namespace fs = boost::filesystem; |
| 42 | |
| 43 | const int MAX_FILE_SEGMENT_SIZE = 1024; |
| 44 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 45 | ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const fs::path &folder) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 46 | : m_ccnx (ccnx) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 47 | , m_folder (folder / ".chronoshare") |
| 48 | { |
| 49 | fs::create_directories (m_folder); |
| 50 | } |
| 51 | |
| 52 | ObjectManager::~ObjectManager () |
| 53 | { |
| 54 | } |
| 55 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 56 | boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/> |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 57 | ObjectManager::localFileToObjects (const fs::path &file, const Ccnx::Name &deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 58 | { |
| 59 | HashPtr fileHash = Hash::FromFileContent (file); |
| 60 | ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash)); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 62 | fs::ifstream iff (file, std::ios::in | std::ios::binary); |
| 63 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 64 | while (iff.good ()) |
| 65 | { |
| 66 | char buf[MAX_FILE_SEGMENT_SIZE]; |
| 67 | iff.read (buf, MAX_FILE_SEGMENT_SIZE); |
| 68 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 69 | Name name = Name (deviceName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 70 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 71 | // cout << *fileHash << endl; |
| 72 | // cout << name << endl; |
| 73 | |
| 74 | Bytes data = m_ccnx->createContentObject (name, buf, iff.gcount ()); |
| 75 | fileDb.saveContentObject (deviceName, segment, data); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 77 | segment ++; |
| 78 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 80 | return make_tuple (fileHash, segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 81 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 82 | |
| 83 | bool |
| 84 | ObjectManager::objectsToLocalFile (/*in*/const Ccnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file) |
| 85 | { |
| 86 | string hashStr = lexical_cast<string> (fileHash); |
| 87 | if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr)) |
| 88 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 89 | _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] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | fs::ofstream off (file, std::ios::out | std::ios::binary); |
| 94 | ObjectDb fileDb (m_folder, hashStr); |
| 95 | |
| 96 | sqlite3_int64 segment = 0; |
| 97 | BytesPtr bytes = fileDb.fetchSegment (deviceName, 0); |
| 98 | while (bytes != BytesPtr()) |
| 99 | { |
| 100 | ParsedContentObject obj (*bytes); |
| 101 | BytesPtr data = obj.contentPtr (); |
| 102 | |
| 103 | off.write (reinterpret_cast<const char*> (head(*data)), data->size()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 104 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 105 | segment ++; |
| 106 | bytes = fileDb.fetchSegment (deviceName, segment); |
| 107 | } |
| 108 | |
| 109 | // 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] | 110 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 111 | return true; |
| 112 | } |