-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
executable file
·197 lines (180 loc) · 5.71 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "cpdetail_form.h"
#include "blkdetail_form.h"
#include <QFile>
#include <QTextStream>
#include <QStandardItemModel>
#include <QDir>
#include <QFileInfo>
#include <QFileInfoList>
#include <QFileDialog>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//init table header
QStandardItemModel *model = new QStandardItemModel();
ui->cpTabView->setModel(model);
ui->cpTabView->setEditTriggers(QAbstractItemView::NoEditTriggers);
//get all formatted volumes
QDir dir("/sys/kernel/debug/hmfs");
if (!dir.exists()) {
qWarning("Cannot find the example directory");
exit(EXIT_FAILURE);
}
QFileInfoList list = dir.entryInfoList();
for (int i = 2; i < list.size(); ++i) { // skip '.' and '..'
QFileInfo fileInfo = list.at(i);
ui->cboxAddr->addItem(fileInfo.fileName());
}
this->errMsg = new QErrorMessage();
}
MainWindow::~MainWindow()
{
delete ui;
free(errMsg);
}
/* on_btnShow_clicked
* -- Show all checkpoints from selected volume
*/
void MainWindow::on_btnShow_clicked()
{
QString path = ui->cboxAddr->currentText();
if(path == NULL) {
errMsg->showMessage("Please choose a volume!");
return;
}
QFile cp_file("/sys/kernel/debug/hmfs/"+path+"/info");
if(!cp_file.open(QIODevice::ReadWrite)){
errMsg->showMessage("Cannot open info file in debugfs!");
return;
}
QTextStream out(&cp_file);
out << "cp a" << endl;
QTextStream in(&cp_file);
QStandardItemModel *model = (QStandardItemModel*)ui->cpTabView->model();
model->clear();
int cnt = 0;
QString line = in.readLine();
while(!in.atEnd()) {
if(line.contains("checkpoint_ver")){
QStringList list = line.split(": ");
model->setItem(cnt,0,new QStandardItem(list[1]));//XXX
}
else if(line.contains("wall_time")){
QStringList list = line.split(": ");
model->setItem(cnt,1,new QStandardItem(list[1]));//XXX
cnt++;
}
line = in.readLine();
}
cp_file.close();
model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("Version Number")));
ui->cpTabView->setColumnWidth(0,140);
model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("Time")));
ui->cpTabView->setColumnWidth(1,300);
//FIXME: repeated strectch cause expansion in first column
//QHeaderView *hv = ui->cpTabView->horizontalHeader();
//hv->setStretchLastSection(true);
}
void MainWindow::refresh()
{
on_btnShow_clicked();
}
void MainWindow::on_pushButton_clicked()
{
QString path = ui->cboxAddr->currentText();
if(path == NULL) {
errMsg->showMessage("Please choose a volume!");
return;
}
Blkdetail_form *cp = new Blkdetail_form(0, path);
cp->show();
}
void MainWindow::on_cpTabView_doubleClicked(const QModelIndex &index)
{
QString path = ui->cboxAddr->currentText();
if(path == NULL) {
errMsg->showMessage("Please choose a volume!");
return;
}
int verNum = index.sibling(index.row(), 0).data().toInt();
cpDetail_form *cp = new cpDetail_form(this, verNum, path);
cp->show();
this->hide();
}
void MainWindow::on_pushButton_2_clicked()
{
int cpCnt = ui->cpTabView->model()->rowCount();
if(cpCnt == 0) {
return;
}
QString path = ui->cboxAddr->currentText();
if(path == NULL) {
errMsg->showMessage("Please choose a volume!");
return;
}
QFile cp_file("/sys/kernel/debug/hmfs/"+path+"/info");
if(!cp_file.open(QIODevice::ReadWrite)){
errMsg->showMessage("Cannot open info file in debugfs!");
return;
}
QTextStream out(&cp_file);
out << "cp t" << endl;
QTextStream in(&cp_file);
bool done = false;
QStandardItemModel *model = (QStandardItemModel*)ui->cpTabView->model();
model->clear();
QString line = in.readLine();
if(line.contains("added")){
done = true;
}
cp_file.close();
if(done) {
on_btnShow_clicked();
} else {
errMsg->showMessage("Failed to add a new checkpoint");
}//TODO else prompt
if(cpCnt == ui->cpTabView->model()->rowCount()) {
errMsg->showMessage("No file change yet!");
}
}
void MainWindow::on_btnRemount_clicked()
{
QString mountPath = QFileDialog::getExistingDirectory(this,"Choose the mounting point",
"/home",
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
QProcess umount;
umount.start("umount", QStringList() << mountPath);
if(!umount.waitForStarted()){
errMsg->showMessage("Start umount failed");
return;
}
umount.waitForFinished();
QString output = QString(umount.readAllStandardError());
if(output != "") {
errMsg->showMessage("Unmount failed!\n\n"+output);
return;
}
QProcess remount;
QString i = ui->cboxAddr->currentText();
bool convertOK;
long long intNum = i.toLongLong(&convertOK, 10);
QString hexStr = QString::number(intNum, 16).prepend("0x");
QString mntCMD="mount -t hmfs -o physaddr=" + hexStr + ",uid=1000,gid=1000 none " + mountPath;
remount.start("sh", QStringList() << "-c" << mntCMD);
if(!remount.waitForStarted()){
errMsg->showMessage("Start remount failed");
return;
}
output = QString(remount.readAllStandardError());
remount.waitForFinished();
if(output!=""){
errMsg->showMessage("Remount failed!\n\n "+output);
return;
}
}