blob: a2de9f16c9f68c3f31f0c7cbf320f1d5a96c1c22 [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"
25#include "object-db.h"
26
27#include <sys/stat.h>
28
29#include <fstream>
30#include <boost/lexical_cast.hpp>
31#include <boost/throw_exception.hpp>
32
33using namespace Ccnx;
34using namespace boost;
35using namespace std;
36namespace fs = boost::filesystem;
37
38const int MAX_FILE_SEGMENT_SIZE = 1024;
39
40ObjectManager::ObjectManager (Ccnx::CcnxWrapperPtr ccnx, const Ccnx::Name &localDeviceName, const fs::path &folder)
41 : m_ccnx (ccnx)
42 , m_localDeviceName (localDeviceName)
43 , m_folder (folder / ".chronoshare")
44{
45 fs::create_directories (m_folder);
46}
47
48ObjectManager::~ObjectManager ()
49{
50}
51
52HashPtr
53ObjectManager::storeLocalFile (const fs::path &file)
54{
55 HashPtr fileHash = Hash::FromFileContent (file);
56 ObjectDb fileDb (m_folder, lexical_cast<string> (*fileHash));
57
58 ifstream iff (file.c_str (), std::ios::in | std::ios::binary);
59 int segment = 0;
60 while (iff.good ())
61 {
62 char buf[MAX_FILE_SEGMENT_SIZE];
63 iff.read (buf, MAX_FILE_SEGMENT_SIZE);
64
65
66 Name name (m_localDeviceName);
67 name
68 .appendComp ("file")
69 .appendComp (fileHash->GetHash (), fileHash->GetHashBytes ())
70 .appendComp (segment);
71
72 cout << *fileHash << endl;
73 cout << name << endl;
74
75 segment ++;
76 }
77
78 return fileHash;
79}