blob: 4574a8a0f59388fad7b55d2a1e5b1eb962425e2d [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 "
Alexander Afanasyev0a30a0c2013-01-29 17:25:42 -080048 " ORDER BY action_timestamp", -1, &stmt, 0);
Alexander Afanasyeveb575e02013-01-26 17:14:51 -080049
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,
Alexander Afanasyev38826ce2013-01-31 16:31:42 -080091 "SELECT filename,device_name,seq_no,file_hash,strftime('%s', file_mtime),file_chmod,file_seg_num,directory,is_complete "
Alexander Afanasyeveb575e02013-01-26 17:14:51 -080092 " 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;
Alexander Afanasyev38826ce2013-01-31 16:31:42 -080097 cout << "===================================================================================================================================" << endl;
98 cout << "filename | device_name | seq_no | file_hash | seg_num | directory | is_complete" << endl;
99 cout << "===================================================================================================================================" << endl;
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800100
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 () << " | ";
Alexander Afanasyev38826ce2013-01-31 16:31:42 -0800108 cout << setw (6) << sqlite3_column_int64 (stmt, 6) << " | ";
109 if (sqlite3_column_bytes (stmt, 7) == 0)
110 cout << setw (20) << "<NULL>" << " | ";
111 else
112 cout << setw (20) << sqlite3_column_text (stmt, 7) << " | ";
113
114 if (sqlite3_column_int (stmt, 8) == 0)
115 cout << setw (20) << "no" << endl;
116 else
117 cout << setw (20) << "yes" << endl;
Alexander Afanasyeveb575e02013-01-26 17:14:51 -0800118 }
119
120 sqlite3_finalize (stmt);
121 }
122};
123
124int main(int argc, char *argv[])
125{
126 INIT_LOGGERS ();
127
128 if (argc != 2)
129 {
130 cerr << "Usage: ./dump-db <path-to-shared-folder>" << endl;
131 return 1;
132 }
133
134 ActionLogDumper dumper (argv[1]);
135 dumper.DumpActionLog ();
136 cout << endl;
137 dumper.DumpFileState ();
138
139 return 0;
140}