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" |
| 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 | |
| 35 | using namespace Ccnx; |
| 36 | using namespace boost; |
| 37 | using namespace std; |
| 38 | namespace fs = boost::filesystem; |
| 39 | |
| 40 | const int MAX_FILE_SEGMENT_SIZE = 1024; |
| 41 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 42 | ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const fs::path &folder) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 43 | : m_ccnx (ccnx) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 44 | , m_folder (folder / ".chronoshare") |
| 45 | { |
| 46 | fs::create_directories (m_folder); |
| 47 | } |
| 48 | |
| 49 | ObjectManager::~ObjectManager () |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | HashPtr |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 54 | ObjectManager::localFileToObjects (const fs::path &file, const Ccnx::Name &deviceName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 55 | { |
| 56 | HashPtr fileHash = Hash::FromFileContent (file); |
| 57 | ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash)); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 58 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 59 | fs::ifstream iff (file, std::ios::in | std::ios::binary); |
| 60 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 61 | while (iff.good ()) |
| 62 | { |
| 63 | char buf[MAX_FILE_SEGMENT_SIZE]; |
| 64 | iff.read (buf, MAX_FILE_SEGMENT_SIZE); |
| 65 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 66 | Name name = Name (deviceName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 68 | // cout << *fileHash << endl; |
| 69 | // cout << name << endl; |
| 70 | |
| 71 | Bytes data = m_ccnx->createContentObject (name, buf, iff.gcount ()); |
| 72 | fileDb.saveContentObject (deviceName, segment, data); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 73 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 74 | segment ++; |
| 75 | } |
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 | return fileHash; |
| 78 | } |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 79 | |
| 80 | bool |
| 81 | ObjectManager::objectsToLocalFile (/*in*/const Ccnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file) |
| 82 | { |
| 83 | string hashStr = lexical_cast<string> (fileHash); |
| 84 | if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr)) |
| 85 | { |
| 86 | cout << "Brr" << endl; |
| 87 | // file does not exist or not all segments are available |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | fs::ofstream off (file, std::ios::out | std::ios::binary); |
| 92 | ObjectDb fileDb (m_folder, hashStr); |
| 93 | |
| 94 | sqlite3_int64 segment = 0; |
| 95 | BytesPtr bytes = fileDb.fetchSegment (deviceName, 0); |
| 96 | while (bytes != BytesPtr()) |
| 97 | { |
| 98 | ParsedContentObject obj (*bytes); |
| 99 | BytesPtr data = obj.contentPtr (); |
| 100 | |
| 101 | off.write (reinterpret_cast<const char*> (head(*data)), data->size()); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame^] | 102 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 103 | segment ++; |
| 104 | bytes = fileDb.fetchSegment (deviceName, segment); |
| 105 | } |
| 106 | |
| 107 | // 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^] | 108 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 109 | return true; |
| 110 | } |