7768
|
1 |
/* borrowed from https://github.com/skhaz/qt-physfs-wrapper
|
|
2 |
* TODO: add copyright header, determine license
|
|
3 |
*/
|
|
4 |
|
7955
|
5 |
|
8052
|
6 |
#include "hwpacksmounter.h"
|
7768
|
7 |
#include "FileEngine.h"
|
|
8 |
|
7770
|
9 |
|
|
10 |
const QString FileEngineHandler::scheme = "physfs:/";
|
|
11 |
|
7768
|
12 |
FileEngine::FileEngine(const QString& filename)
|
|
13 |
: _handler(NULL)
|
|
14 |
, _flags(0)
|
|
15 |
{
|
|
16 |
setFileName(filename);
|
|
17 |
}
|
|
18 |
|
|
19 |
FileEngine::~FileEngine()
|
|
20 |
{
|
|
21 |
close();
|
|
22 |
}
|
|
23 |
|
|
24 |
bool FileEngine::open(QIODevice::OpenMode openMode)
|
|
25 |
{
|
|
26 |
close();
|
|
27 |
|
|
28 |
if (openMode & QIODevice::WriteOnly) {
|
|
29 |
_handler = PHYSFS_openWrite(_filename.toUtf8().constData());
|
|
30 |
_flags = QAbstractFileEngine::WriteOwnerPerm | QAbstractFileEngine::WriteUserPerm | QAbstractFileEngine::FileType;
|
|
31 |
}
|
|
32 |
|
|
33 |
else if (openMode & QIODevice::ReadOnly) {
|
|
34 |
_handler = PHYSFS_openRead(_filename.toUtf8().constData());
|
|
35 |
}
|
|
36 |
|
|
37 |
else if (openMode & QIODevice::Append) {
|
|
38 |
_handler = PHYSFS_openAppend(_filename.toUtf8().constData());
|
|
39 |
}
|
|
40 |
|
|
41 |
else {
|
|
42 |
qWarning("Bad file open mode: %d", (int)openMode);
|
|
43 |
}
|
|
44 |
|
|
45 |
if (!_handler) {
|
|
46 |
qWarning("Failed to open %s, reason: %s", _filename.toUtf8().constData(), PHYSFS_getLastError());
|
|
47 |
return false;
|
|
48 |
}
|
|
49 |
|
|
50 |
return true;
|
|
51 |
}
|
|
52 |
|
|
53 |
bool FileEngine::close()
|
|
54 |
{
|
|
55 |
if (isOpened()) {
|
|
56 |
int result = PHYSFS_close(_handler);
|
|
57 |
_handler = NULL;
|
|
58 |
return result != 0;
|
|
59 |
}
|
|
60 |
|
|
61 |
return true;
|
|
62 |
}
|
|
63 |
|
|
64 |
bool FileEngine::flush()
|
|
65 |
{
|
|
66 |
return PHYSFS_flush(_handler) != 0;
|
|
67 |
}
|
|
68 |
|
|
69 |
qint64 FileEngine::size() const
|
|
70 |
{
|
|
71 |
return _size;
|
|
72 |
}
|
|
73 |
|
|
74 |
qint64 FileEngine::pos() const
|
|
75 |
{
|
|
76 |
return PHYSFS_tell(_handler);
|
|
77 |
}
|
|
78 |
|
|
79 |
bool FileEngine::seek(qint64 pos)
|
|
80 |
{
|
|
81 |
return PHYSFS_seek(_handler, pos) != 0;
|
|
82 |
}
|
|
83 |
|
|
84 |
bool FileEngine::isSequential() const
|
|
85 |
{
|
7770
|
86 |
return false;
|
7768
|
87 |
}
|
|
88 |
|
|
89 |
bool FileEngine::remove()
|
|
90 |
{
|
|
91 |
return PHYSFS_delete(_filename.toUtf8().constData()) != 0;
|
|
92 |
}
|
|
93 |
|
|
94 |
bool FileEngine::mkdir(const QString &dirName, bool createParentDirectories) const
|
|
95 |
{
|
|
96 |
Q_UNUSED(createParentDirectories);
|
|
97 |
return PHYSFS_mkdir(dirName.toUtf8().constData()) != 0;
|
|
98 |
}
|
|
99 |
|
|
100 |
bool FileEngine::rmdir(const QString &dirName, bool recurseParentDirectories) const
|
|
101 |
{
|
|
102 |
Q_UNUSED(recurseParentDirectories);
|
|
103 |
return PHYSFS_delete(dirName.toUtf8().constData()) != 0;
|
|
104 |
}
|
|
105 |
|
|
106 |
bool FileEngine::caseSensitive() const
|
|
107 |
{
|
|
108 |
return true;
|
|
109 |
}
|
|
110 |
|
|
111 |
bool FileEngine::isRelativePath() const
|
|
112 |
{
|
|
113 |
return true;
|
|
114 |
}
|
|
115 |
|
7770
|
116 |
QAbstractFileEngineIterator * FileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
|
|
117 |
{
|
|
118 |
return new FileEngineIterator(filters, filterNames, entryList(filters, filterNames));
|
|
119 |
}
|
|
120 |
|
7768
|
121 |
QStringList FileEngine::entryList(QDir::Filters filters, const QStringList &filterNames) const
|
|
122 |
{
|
|
123 |
Q_UNUSED(filters);
|
|
124 |
|
|
125 |
QString file;
|
|
126 |
QStringList result;
|
7770
|
127 |
char **files = PHYSFS_enumerateFiles(_filename.toUtf8().constData());
|
7768
|
128 |
|
|
129 |
for (char **i = files; *i != NULL; i++) {
|
7770
|
130 |
file = QString::fromUtf8(*i);
|
|
131 |
|
|
132 |
if (filterNames.isEmpty() || QDir::match(filterNames, file)) {
|
7768
|
133 |
result << file;
|
|
134 |
}
|
|
135 |
}
|
|
136 |
|
|
137 |
PHYSFS_freeList(files);
|
7770
|
138 |
|
7768
|
139 |
return result;
|
|
140 |
}
|
|
141 |
|
|
142 |
QAbstractFileEngine::FileFlags FileEngine::fileFlags(FileFlags type) const
|
|
143 |
{
|
|
144 |
return type & _flags;
|
|
145 |
}
|
|
146 |
|
|
147 |
QString FileEngine::fileName(FileName file) const
|
|
148 |
{
|
|
149 |
if (file == QAbstractFileEngine::AbsolutePathName)
|
|
150 |
return PHYSFS_getWriteDir();
|
|
151 |
|
7955
|
152 |
return QString("physfs://%1").arg(_filename);
|
7768
|
153 |
}
|
|
154 |
|
|
155 |
QDateTime FileEngine::fileTime(FileTime time) const
|
|
156 |
{
|
7955
|
157 |
|
7768
|
158 |
switch (time)
|
|
159 |
{
|
|
160 |
case QAbstractFileEngine::ModificationTime:
|
|
161 |
default:
|
|
162 |
return _datetime;
|
|
163 |
break;
|
|
164 |
};
|
|
165 |
}
|
|
166 |
|
|
167 |
void FileEngine::setFileName(const QString &file)
|
|
168 |
{
|
7770
|
169 |
if(file.startsWith(FileEngineHandler::scheme))
|
|
170 |
_filename = file.mid(FileEngineHandler::scheme.size());
|
|
171 |
else
|
|
172 |
_filename = file;
|
7931
|
173 |
|
7768
|
174 |
PHYSFS_Stat stat;
|
|
175 |
if (PHYSFS_stat(_filename.toUtf8().constData(), &stat) != 0) {
|
|
176 |
_size = stat.filesize;
|
|
177 |
_datetime = QDateTime::fromTime_t(stat.modtime);
|
7955
|
178 |
// _flags |= QAbstractFileEngine::WriteUserPerm;
|
7931
|
179 |
_flags |= QAbstractFileEngine::ReadUserPerm;
|
7768
|
180 |
_flags |= QAbstractFileEngine::ExistsFlag;
|
|
181 |
|
|
182 |
switch (stat.filetype)
|
|
183 |
{
|
|
184 |
case PHYSFS_FILETYPE_REGULAR:
|
|
185 |
_flags |= QAbstractFileEngine::FileType;
|
|
186 |
break;
|
|
187 |
|
|
188 |
case PHYSFS_FILETYPE_DIRECTORY:
|
|
189 |
_flags |= QAbstractFileEngine::DirectoryType;
|
|
190 |
break;
|
|
191 |
case PHYSFS_FILETYPE_SYMLINK:
|
|
192 |
_flags |= QAbstractFileEngine::LinkType;
|
|
193 |
break;
|
|
194 |
default: ;
|
7955
|
195 |
}
|
7768
|
196 |
}
|
|
197 |
}
|
|
198 |
|
|
199 |
bool FileEngine::atEnd() const
|
|
200 |
{
|
|
201 |
return PHYSFS_eof(_handler) != 0;
|
|
202 |
}
|
|
203 |
|
|
204 |
qint64 FileEngine::read(char *data, qint64 maxlen)
|
|
205 |
{
|
|
206 |
return PHYSFS_readBytes(_handler, data, maxlen);
|
|
207 |
}
|
|
208 |
|
|
209 |
qint64 FileEngine::write(const char *data, qint64 len)
|
|
210 |
{
|
|
211 |
return PHYSFS_writeBytes(_handler, data, len);
|
|
212 |
}
|
|
213 |
|
|
214 |
bool FileEngine::isOpened() const
|
|
215 |
{
|
|
216 |
return _handler != NULL;
|
|
217 |
}
|
|
218 |
|
|
219 |
QFile::FileError FileEngine::error() const
|
|
220 |
{
|
|
221 |
return QFile::UnspecifiedError;
|
|
222 |
}
|
|
223 |
|
|
224 |
QString FileEngine::errorString() const
|
|
225 |
{
|
|
226 |
return PHYSFS_getLastError();
|
|
227 |
}
|
|
228 |
|
|
229 |
bool FileEngine::supportsExtension(Extension extension) const
|
|
230 |
{
|
|
231 |
return extension == QAbstractFileEngine::AtEndExtension;
|
|
232 |
}
|
|
233 |
|
7770
|
234 |
|
|
235 |
|
|
236 |
FileEngineHandler::FileEngineHandler(char *argv0)
|
|
237 |
{
|
|
238 |
PHYSFS_init(argv0);
|
|
239 |
}
|
|
240 |
|
|
241 |
FileEngineHandler::~FileEngineHandler()
|
|
242 |
{
|
|
243 |
PHYSFS_deinit();
|
|
244 |
}
|
|
245 |
|
7768
|
246 |
QAbstractFileEngine* FileEngineHandler::create(const QString &filename) const
|
|
247 |
{
|
7770
|
248 |
if (filename.startsWith(scheme))
|
|
249 |
return new FileEngine(filename.mid(scheme.size()));
|
|
250 |
else
|
|
251 |
return NULL;
|
|
252 |
}
|
|
253 |
|
|
254 |
void FileEngineHandler::mount(const QString &path)
|
|
255 |
{
|
|
256 |
PHYSFS_mount(path.toUtf8().constData(), NULL, 1);
|
|
257 |
}
|
|
258 |
|
7955
|
259 |
void FileEngineHandler::mount(const QString & path, const QString & mountPoint)
|
|
260 |
{
|
|
261 |
PHYSFS_mount(path.toUtf8().constData(), mountPoint.toUtf8().constData(), 1);
|
|
262 |
}
|
|
263 |
|
7770
|
264 |
void FileEngineHandler::setWriteDir(const QString &path)
|
|
265 |
{
|
|
266 |
PHYSFS_setWriteDir(path.toUtf8().constData());
|
7768
|
267 |
}
|
7770
|
268 |
|
8052
|
269 |
void FileEngineHandler::mountPacks()
|
|
270 |
{
|
|
271 |
hedgewarsMountPackages();
|
|
272 |
}
|
7770
|
273 |
|
|
274 |
|
|
275 |
FileEngineIterator::FileEngineIterator(QDir::Filters filters, const QStringList &nameFilters, const QStringList &entries)
|
|
276 |
: QAbstractFileEngineIterator(filters, nameFilters)
|
|
277 |
{
|
|
278 |
m_entries = entries;
|
|
279 |
|
|
280 |
/* heck.. docs are unclear on this
|
|
281 |
* QDirIterator puts iterator before first entry
|
|
282 |
* but QAbstractFileEngineIterator example puts iterator on first entry
|
|
283 |
* though QDirIterator approach seems to be the right one
|
|
284 |
*/
|
|
285 |
|
|
286 |
m_index = -1;
|
|
287 |
}
|
|
288 |
|
|
289 |
bool FileEngineIterator::hasNext() const
|
|
290 |
{
|
|
291 |
return m_index < m_entries.size() - 1;
|
|
292 |
}
|
|
293 |
|
|
294 |
QString FileEngineIterator::next()
|
|
295 |
{
|
|
296 |
if (!hasNext())
|
|
297 |
return QString();
|
|
298 |
|
|
299 |
++m_index;
|
|
300 |
return currentFilePath();
|
|
301 |
}
|
|
302 |
|
|
303 |
QString FileEngineIterator::currentFileName() const
|
|
304 |
{
|
|
305 |
return m_entries.at(m_index);
|
|
306 |
}
|