-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYaffsManager.cpp
138 lines (118 loc) · 4.37 KB
/
YaffsManager.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
/*
* yaffey: Utility for reading, editing and writing YAFFS2 images
* Copyright (C) 2012 David Place <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include <QDir>
#include "YaffsManager.h"
#include "YaffsControl.h"
YaffsManager* YaffsManager::mSelf = new YaffsManager();
YaffsManager* YaffsManager::getInstance() {
return mSelf;
}
YaffsManager::YaffsManager() {
mYaffsModel = NULL;
mYaffsExportInfo = NULL;
}
YaffsManager::~YaffsManager() {
delete mYaffsModel;
}
YaffsModel* YaffsManager::newModel() {
delete mYaffsModel;
mYaffsModel = new YaffsModel();
connect(mYaffsModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), SLOT(on_model_DataChanged(QModelIndex, QModelIndex)));
connect(mYaffsModel, SIGNAL(layoutChanged()), SLOT(on_model_LayoutChanged()));
return mYaffsModel;
}
YaffsExportInfo* YaffsManager::exportItems(QModelIndexList itemIndices, const QString& path) {
mYaffsExportInfo = new YaffsExportInfo();
mYaffsExportInfo->numDirsExported = 0;
mYaffsExportInfo->numFilesExported = 0;
foreach (QModelIndex index, itemIndices) {
YaffsItem* item = static_cast<YaffsItem*>(index.internalPointer());
exportItem(item, path);
}
return mYaffsExportInfo;
}
void YaffsManager::on_model_DataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) {
emit modelChanged();
}
void YaffsManager::on_model_LayoutChanged() {
emit modelChanged();
}
void YaffsManager::exportItem(const YaffsItem* item, const QString& path) {
if (item) {
if (item->isFile()) {
exportFile(item, path);
} else if (item->isDir()) {
exportDirectory(item, path);
}
}
}
void YaffsManager::exportFile(const YaffsItem* item, const QString& path) {
bool result = false;
if (item->isFile() && item->getCondition() != YaffsItem::NEW) {
int headerPosition = item->getHeaderPosition();
int filesize = item->getFileSize();
QString imageFilename = mYaffsModel->getImageFilename();
YaffsControl yaffsControl(imageFilename.toStdString().c_str(), NULL);
if (yaffsControl.open(YaffsControl::OPEN_READ)) {
char* data = yaffsControl.extractFile(headerPosition);
if (data != NULL) {
QDir().mkpath(path);
result = saveDataToFile(path + QDir::separator() + item->getName(), data, filesize);
delete data;
}
}
}
if (result) {
mYaffsExportInfo->numFilesExported++;
} else {
mYaffsExportInfo->listFileExportFailures.append(item);
}
}
void YaffsManager::exportDirectory(const YaffsItem* item, const QString& path) {
bool result = false;
if (item->isDir() && item->getCondition() != YaffsItem::NEW) {
result = true;
QString dir(path);
if (!item->isRoot()) {
dir += QDir::separator() + item->getName();
result = QDir().mkdir(dir);
}
if (result) {
int childCount = item->childCount();
for (int i = 0; i < childCount; ++i) {
const YaffsItem* childItem = item->child(i);
exportItem(childItem, dir);
}
}
}
if (result) {
mYaffsExportInfo->numDirsExported++;
} else {
mYaffsExportInfo->listDirExportFailures.append(item);
}
}
bool YaffsManager::saveDataToFile(const QString& filename, const char* data, int length) {
bool result = false;
QFile file(filename);
bool open = file.open(QIODevice::WriteOnly);
if (open) {
result = (file.write(data, length) != -1);
file.close();
}
return result;
}