blob: 610a2fb98232b55109efbced56095f9d78a282ae [file] [log] [blame]
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2016, Regents of the University of California.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08004 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08005 * This file is part of ChronoShare, a decentralized file sharing application over NDN.
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08006 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -08007 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080010 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080011 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080014 *
Alexander Afanasyevfa2f6622016-12-25 12:28:00 -080015 * 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 Afanasyev68f2a952013-01-08 14:34:16 -080019 */
20
Alexander Afanasyevf4cde4e2016-12-25 13:42:57 -080021#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 Afanasyev68f2a952013-01-08 14:34:16 -080027
28#include <sys/stat.h>
29
30#include <fstream>
31#include <boost/lexical_cast.hpp>
32#include <boost/throw_exception.hpp>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080033#include <boost/filesystem/fstream.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080034
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080035INIT_LOGGER ("Object.Manager");
36
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070037using namespace Ndnx;
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080038using namespace boost;
39using namespace std;
40namespace fs = boost::filesystem;
41
42const int MAX_FILE_SEGMENT_SIZE = 1024;
43
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070044ObjectManager::ObjectManager (Ndnx::NdnxWrapperPtr ndnx, const fs::path &folder, const std::string &appName)
45 : m_ndnx (ndnx)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080046 , m_folder (folder / ".chronoshare")
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080047 , m_appName (appName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080048{
49 fs::create_directories (m_folder);
50}
51
52ObjectManager::~ObjectManager ()
53{
54}
55
Alexander Afanasyev4d086752013-02-07 13:06:04 -080056// /<devicename>/<appname>/file/<hash>/<segment>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080057boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/>
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070058ObjectManager::localFileToObjects (const fs::path &file, const Ndnx::Name &deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080059{
60 HashPtr fileHash = Hash::FromFileContent (file);
61 ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080062
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080063 fs::ifstream iff (file, std::ios::in | std::ios::binary);
64 sqlite3_int64 segment = 0;
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080065 while (iff.good () && !iff.eof ())
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080066 {
67 char buf[MAX_FILE_SEGMENT_SIZE];
68 iff.read (buf, MAX_FILE_SEGMENT_SIZE);
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080069 if (iff.gcount () == 0)
70 {
71 // stupid streams...
72 break;
73 }
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080074
Alexander Afanasyev4d086752013-02-07 13:06:04 -080075 Name name = Name ("/")(deviceName)(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080076
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080077 // cout << *fileHash << endl;
78 // cout << name << endl;
Zhenkai Zhu81907f22013-01-23 17:32:34 -080079 //_LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080080
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070081 Bytes data = m_ndnx->createContentObject (name, buf, iff.gcount ());
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080082 fileDb.saveContentObject (deviceName, segment, data);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080083
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080084 segment ++;
85 }
Alexander Afanasyevac704622013-01-25 17:50:34 -080086 if (segment == 0) // handle empty files
87 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080088 Name name = Name ("/")(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(deviceName)(0);
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070089 Bytes data = m_ndnx->createContentObject (name, 0, 0);
Alexander Afanasyevac704622013-01-25 17:50:34 -080090 fileDb.saveContentObject (deviceName, 0, data);
91
92 segment ++;
93 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080094
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080095 return make_tuple (fileHash, segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080096}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080097
98bool
Alexander Afanasyev1dd37ed2013-08-14 18:08:09 -070099ObjectManager::objectsToLocalFile (/*in*/const Ndnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800100{
101 string hashStr = lexical_cast<string> (fileHash);
102 if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr))
103 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800104 _LOG_ERROR ("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr << "] does not exist or not all segments are available");
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800105 return false;
106 }
107
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800108 if (!exists (file.parent_path ()))
109 {
110 create_directories (file.parent_path ());
111 }
112
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800113 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 Afanasyeve04712b2013-01-25 17:53:26 -0800118 while (bytes)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800119 {
120 ParsedContentObject obj (*bytes);
121 BytesPtr data = obj.contentPtr ();
122
Alexander Afanasyeve04712b2013-01-25 17:53:26 -0800123 if (data)
124 {
125 off.write (reinterpret_cast<const char*> (head(*data)), data->size());
126 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800127
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800128 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 Afanasyev49a30d02013-01-21 21:38:48 -0800133
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800134 return true;
135}