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 | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 45 | ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const fs::path &folder, const std::string &appName) |
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") |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 48 | , m_appName (appName) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 49 | { |
| 50 | fs::create_directories (m_folder); |
| 51 | } |
| 52 | |
| 53 | ObjectManager::~ObjectManager () |
| 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 | dbc0671 | 2013-01-08 18:30:28 -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 | { |
| 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 | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 64 | fs::ifstream iff (file, std::ios::in | std::ios::binary); |
| 65 | sqlite3_int64 segment = 0; |
Alexander Afanasyev | 2a6fa2b | 2013-01-23 16:51:55 -0800 | [diff] [blame] | 66 | while (iff.good () && !iff.eof ()) |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 67 | { |
| 68 | char buf[MAX_FILE_SEGMENT_SIZE]; |
| 69 | iff.read (buf, MAX_FILE_SEGMENT_SIZE); |
Alexander Afanasyev | 2a6fa2b | 2013-01-23 16:51:55 -0800 | [diff] [blame] | 70 | if (iff.gcount () == 0) |
| 71 | { |
| 72 | // stupid streams... |
| 73 | break; |
| 74 | } |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | 4d08675 | 2013-02-07 13:06:04 -0800 | [diff] [blame] | 76 | Name name = Name ("/")(deviceName)(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment); |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 78 | // cout << *fileHash << endl; |
| 79 | // cout << name << endl; |
Zhenkai Zhu | 81907f2 | 2013-01-23 17:32:34 -0800 | [diff] [blame] | 80 | //_LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment); |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 81 | |
| 82 | Bytes data = m_ccnx->createContentObject (name, buf, iff.gcount ()); |
| 83 | fileDb.saveContentObject (deviceName, segment, data); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 85 | segment ++; |
| 86 | } |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 87 | if (segment == 0) // handle empty files |
| 88 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 89 | Name name = Name ("/")(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(deviceName)(0); |
Alexander Afanasyev | ac70462 | 2013-01-25 17:50:34 -0800 | [diff] [blame] | 90 | Bytes data = m_ccnx->createContentObject (name, 0, 0); |
| 91 | fileDb.saveContentObject (deviceName, 0, data); |
| 92 | |
| 93 | segment ++; |
| 94 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -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 |
| 100 | ObjectManager::objectsToLocalFile (/*in*/const Ccnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file) |
| 101 | { |
| 102 | string hashStr = lexical_cast<string> (fileHash); |
| 103 | if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr)) |
| 104 | { |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 105 | _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] | 106 | return false; |
| 107 | } |
| 108 | |
Alexander Afanasyev | f2c16e0 | 2013-01-23 18:08:04 -0800 | [diff] [blame] | 109 | if (!exists (file.parent_path ())) |
| 110 | { |
| 111 | create_directories (file.parent_path ()); |
| 112 | } |
| 113 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 114 | fs::ofstream off (file, std::ios::out | std::ios::binary); |
| 115 | ObjectDb fileDb (m_folder, hashStr); |
| 116 | |
| 117 | sqlite3_int64 segment = 0; |
| 118 | BytesPtr bytes = fileDb.fetchSegment (deviceName, 0); |
Alexander Afanasyev | e04712b | 2013-01-25 17:53:26 -0800 | [diff] [blame] | 119 | while (bytes) |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 120 | { |
| 121 | ParsedContentObject obj (*bytes); |
| 122 | BytesPtr data = obj.contentPtr (); |
| 123 | |
Alexander Afanasyev | e04712b | 2013-01-25 17:53:26 -0800 | [diff] [blame] | 124 | if (data) |
| 125 | { |
| 126 | off.write (reinterpret_cast<const char*> (head(*data)), data->size()); |
| 127 | } |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 129 | segment ++; |
| 130 | bytes = fileDb.fetchSegment (deviceName, segment); |
| 131 | } |
| 132 | |
| 133 | // 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] | 134 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 135 | return true; |
| 136 | } |