blob: fc504881e4a806d820cdfc8ff2de05410e3c397c [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 Afanasyev1d1cc832013-02-05 20:03:36 -080045ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const fs::path &folder, const std::string &appName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080046 : m_ccnx (ccnx)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080047 , m_folder (folder / ".chronoshare")
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080048 , m_appName (appName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080049{
50 fs::create_directories (m_folder);
51}
52
53ObjectManager::~ObjectManager ()
54{
55}
56
Alexander Afanasyev4d086752013-02-07 13:06:04 -080057// /<devicename>/<appname>/file/<hash>/<segment>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080058boost::tuple<HashPtr /*object-db name*/, size_t /* number of segments*/>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080059ObjectManager::localFileToObjects (const fs::path &file, const Ccnx::Name &deviceName)
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080060{
61 HashPtr fileHash = Hash::FromFileContent (file);
62 ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash));
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080063
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080064 fs::ifstream iff (file, std::ios::in | std::ios::binary);
65 sqlite3_int64 segment = 0;
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080066 while (iff.good () && !iff.eof ())
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080067 {
68 char buf[MAX_FILE_SEGMENT_SIZE];
69 iff.read (buf, MAX_FILE_SEGMENT_SIZE);
Alexander Afanasyev2a6fa2b2013-01-23 16:51:55 -080070 if (iff.gcount () == 0)
71 {
72 // stupid streams...
73 break;
74 }
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080075
Alexander Afanasyev4d086752013-02-07 13:06:04 -080076 Name name = Name ("/")(deviceName)(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080077
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080078 // cout << *fileHash << endl;
79 // cout << name << endl;
Zhenkai Zhu81907f22013-01-23 17:32:34 -080080 //_LOG_DEBUG ("Read " << iff.gcount () << " from " << file << " for segment " << segment);
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080081
82 Bytes data = m_ccnx->createContentObject (name, buf, iff.gcount ());
83 fileDb.saveContentObject (deviceName, segment, data);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080084
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080085 segment ++;
86 }
Alexander Afanasyevac704622013-01-25 17:50:34 -080087 if (segment == 0) // handle empty files
88 {
Alexander Afanasyev1d1cc832013-02-05 20:03:36 -080089 Name name = Name ("/")(m_appName)("file")(fileHash->GetHash (), fileHash->GetHashBytes ())(deviceName)(0);
Alexander Afanasyevac704622013-01-25 17:50:34 -080090 Bytes data = m_ccnx->createContentObject (name, 0, 0);
91 fileDb.saveContentObject (deviceName, 0, data);
92
93 segment ++;
94 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080095
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080096 return make_tuple (fileHash, segment);
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080097}
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080098
99bool
100ObjectManager::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 Afanasyeva35756b2013-01-22 16:59:11 -0800105 _LOG_ERROR ("ObjectDb for [" << m_folder << ", " << deviceName << ", " << hashStr << "] does not exist or not all segments are available");
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800106 return false;
107 }
108
Alexander Afanasyevf2c16e02013-01-23 18:08:04 -0800109 if (!exists (file.parent_path ()))
110 {
111 create_directories (file.parent_path ());
112 }
113
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800114 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 Afanasyeve04712b2013-01-25 17:53:26 -0800119 while (bytes)
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800120 {
121 ParsedContentObject obj (*bytes);
122 BytesPtr data = obj.contentPtr ();
123
Alexander Afanasyeve04712b2013-01-25 17:53:26 -0800124 if (data)
125 {
126 off.write (reinterpret_cast<const char*> (head(*data)), data->size());
127 }
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800128
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800129 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 Afanasyev49a30d02013-01-21 21:38:48 -0800134
Alexander Afanasyevdbc06712013-01-08 18:30:28 -0800135 return true;
136}