blob: 848929e4496cb2c0a6ce14e0f43dd5be2fe6e7da [file] [log] [blame]
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08001/* -*- 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 Afanasyevdbc06712013-01-08 18:30:28 -080025#include "ccnx-pco.h"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080026#include "object-db.h"
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080027#include "logging.h"
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080028
29#include <sys/stat.h>
30
31#include <fstream>
32#include <boost/lexical_cast.hpp>
33#include <boost/throw_exception.hpp>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080034#include <boost/filesystem/fstream.hpp>
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080035
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080036INIT_LOGGER ("Object.Manager");
37
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080038using namespace Ccnx;
39using namespace boost;
40using namespace std;
41namespace fs = boost::filesystem;
42
43const int MAX_FILE_SEGMENT_SIZE = 1024;
44
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080045ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const fs::path &folder)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080046 : m_ccnx (ccnx)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080047 , m_folder (folder / ".chronoshare")
48{
49 fs::create_directories (m_folder);
50}
51
52ObjectManager::~ObjectManager ()
53{
54}
55
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080056boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080057ObjectManager::localFileToObjects (const fs::path &file, const Ccnx::Name &deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080058{
59 HashPtr fileHash = Hash::FromFileContent (file);
60 ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080061
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080062 fs::ifstream iff (file, std::ios::in | std::ios::binary);
63 sqlite3_int64 segment = 0;
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080064 while (iff.good () && !iff.eof ())
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080065 {
66 char buf[MAX_FILE_SEGMENT_SIZE];
67 iff.read (buf, MAX_FILE_SEGMENT_SIZE);
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080068 if (iff.gcount () == 0)
69 {
70 // stupid streams...
71 break;
72 }
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080073
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080074 Name name = Name (deviceName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080075
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080076 // cout << *fileHash << endl;
77 // cout << name << endl;
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080078 _LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080079
80 Bytes data = m_ccnx->createContentObject (name, buf, iff.gcount ());
81 fileDb.saveContentObject (deviceName, segment, data);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080082
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080083 segment ++;
84 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080085
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080086 return make_tuple (fileHash, segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080087}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080088
89bool
90ObjectManager::objectsToLocalFile (/*in*/const Ccnx::Name &deviceName, /*in*/const Hash &fileHash, /*out*/ const fs::path &file)
91{
92 string hashStr = lexical_cast<string> (fileHash);
93 if (!ObjectDb::DoesExist (m_folder, deviceName, hashStr))
94 {
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080095 _LOG_ERROR ("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr << "] does not exist or not all segments are available");
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080096 return false;
97 }
98
99 fs::ofstream off (file, std::ios::out | std::ios::binary);
100 ObjectDb fileDb (m_folder, hashStr);
101
102 sqlite3_int64 segment = 0;
103 BytesPtr bytes = fileDb.fetchSegment (deviceName, 0);
104 while (bytes != BytesPtr())
105 {
106 ParsedContentObject obj (*bytes);
107 BytesPtr data = obj.contentPtr ();
108
109 off.write (reinterpret_cast<const char*> (head(*data)), data->size());
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800110
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800111 segment ++;
112 bytes = fileDb.fetchSegment (deviceName, segment);
113 }
114
115 // permission and timestamp should be assigned somewhere else (ObjectManager has no idea about that)
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800116
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800117 return true;
118}