blob: 5be87199a778ae3a42990e6dc2048880662d1df8 [file] [log] [blame]
Alexander Afanasyeveb575e02013-01-26 17:14:51 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 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 */
20
21#include "dispatcher.h"
22#include "fs-watcher.h"
23#include "logging.h"
24#include "ccnx-wrapper.h"
25
26#include <boost/make_shared.hpp>
27#include <boost/lexical_cast.hpp>
28
29using namespace boost;
30using namespace std;
31using namespace Ccnx;
32
33class ActionLogDumper : public DbHelper
34{
35public:
36 ActionLogDumper (const filesystem::path &path)
37 : DbHelper (path / ".chronoshare", "action-log.db")
38 {
39 }
40
41 void
42 DumpActionLog ()
43 {
44 sqlite3_stmt *stmt;
45 sqlite3_prepare_v2 (m_db,
46 "SELECT device_name, seq_no, action, filename, version, file_hash, file_seg_num, parent_device_name, parent_seq_no "
47 " FROM ActionLog "
48 " ORDER BY filename,version", -1, &stmt, 0);
49
50 cout.setf(std::ios::left, std::ios::adjustfield);
51 cout << ">> ACTION LOG <<" << endl;
52 cout << "=============================================================================================================================================================================" << endl;
53 cout << setw(30) << "device_name" << " | seq_no | action |" << setw(40) << " filename " << " | version | file_hash | seg_num | parent_device_name | parent_seq_no" << endl;
54 cout << "=============================================================================================================================================================================" << endl;
55
56 FileItemsPtr retval = make_shared<FileItems> ();
57 while (sqlite3_step (stmt) == SQLITE_ROW)
58 {
59 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 0), sqlite3_column_bytes (stmt, 0))) << " | "; // device_name
60 cout << setw (6) << sqlite3_column_int64 (stmt, 1) << " | "; // seq_no
61 cout << setw (6) << (sqlite3_column_int (stmt, 2)==0?"UPDATE":"DELETE") << " | "; // action
62 cout << setw (40) << sqlite3_column_text (stmt, 3) << " | "; // filename
63 cout << setw (7) << sqlite3_column_int64 (stmt, 4) << " | "; // version
64
65 if (sqlite3_column_int (stmt, 2) == 0)
66 {
67 cout << setw (10) << Hash (sqlite3_column_blob (stmt, 5), sqlite3_column_bytes (stmt, 5)).shortHash () << " | ";
68 cout << setw (7) << sqlite3_column_int64 (stmt, 6) << " | "; // seg_num
69 }
70 else
71 cout << " | | ";
72
73 if (sqlite3_column_bytes (stmt, 7) > 0)
74 {
75 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 7), sqlite3_column_bytes (stmt, 7))) << " | "; // parent_device_name
76 cout << setw (5) << sqlite3_column_int64 (stmt, 8); // seq_no
77 }
78 else
79 cout << setw (30) << " " << " | ";
80 cout << endl;
81 }
82
83 sqlite3_finalize (stmt);
84 }
85
86 void
87 DumpFileState ()
88 {
89 sqlite3_stmt *stmt;
90 sqlite3_prepare_v2 (m_db,
91 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num "
92 " FROM FileState "
93 " WHERE type = 0 ORDER BY filename", -1, &stmt, 0);
94
95 cout.setf(std::ios::left, std::ios::adjustfield);
96 cout << ">> FILE STATE <<" << endl;
97 cout << "==============================================================================================================" << endl;
98 cout << "filename | device_name | seq_no | file_hash | file_seg_num" << endl;
99 cout << "==============================================================================================================" << endl;
100
101 FileItemsPtr retval = make_shared<FileItems> ();
102 while (sqlite3_step (stmt) == SQLITE_ROW)
103 {
104 cout << setw (40) << sqlite3_column_text (stmt, 0) << " | ";
105 cout << setw (30) << lexical_cast<string> (Name (sqlite3_column_blob (stmt, 1), sqlite3_column_bytes (stmt, 1))) << " | ";
106 cout << setw (6) << sqlite3_column_int64 (stmt, 2) << " | ";
107 cout << setw (10) << Hash (sqlite3_column_blob (stmt, 3), sqlite3_column_bytes (stmt, 3)).shortHash () << " | ";
108 cout << setw (6) << sqlite3_column_int64 (stmt, 6) << endl;
109 }
110
111 sqlite3_finalize (stmt);
112 }
113};
114
115int main(int argc, char *argv[])
116{
117 INIT_LOGGERS ();
118
119 if (argc != 2)
120 {
121 cerr << "Usage: ./dump-db <path-to-shared-folder>" << endl;
122 return 1;
123 }
124
125 ActionLogDumper dumper (argv[1]);
126 dumper.DumpActionLog ();
127 cout << endl;
128 dumper.DumpFileState ();
129
130 return 0;
131}