2017-03-21 01:15:29 +01:00
|
|
|
#include <cctype>
|
|
|
|
#include <QFile>
|
2017-06-25 12:24:05 +02:00
|
|
|
#include <QFileInfo>
|
2017-03-21 01:15:29 +01:00
|
|
|
#include "tar.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define BLOCKSIZE 512
|
|
|
|
|
|
|
|
#define BLOCKCOUNT(size) \
|
|
|
|
((size)/BLOCKSIZE + ((size) % BLOCKSIZE > 0 ? 1 : 0))
|
|
|
|
|
2020-10-12 20:05:17 +02:00
|
|
|
struct TARHeader
|
2017-03-21 01:15:29 +01:00
|
|
|
{
|
|
|
|
char name[100]; /* 0 */
|
|
|
|
char mode[8]; /* 100 */
|
|
|
|
char uid[8]; /* 108 */
|
|
|
|
char gid[8]; /* 116 */
|
|
|
|
char size[12]; /* 124 */
|
|
|
|
char mtime[12]; /* 136 */
|
|
|
|
char chksum[8]; /* 148 */
|
|
|
|
char typeflag; /* 156 */
|
|
|
|
char linkname[100]; /* 157 */
|
|
|
|
char magic[6]; /* 257 */
|
|
|
|
char version[2]; /* 263 */
|
|
|
|
char uname[32]; /* 265 */
|
|
|
|
char gname[32]; /* 297 */
|
|
|
|
char devmajor[8]; /* 329 */
|
|
|
|
char devminor[8]; /* 337 */
|
|
|
|
char prefix[155]; /* 345 */
|
2017-03-26 15:32:55 +02:00
|
|
|
/* 500 */
|
2017-03-21 01:15:29 +01:00
|
|
|
};
|
|
|
|
|
2017-06-25 12:24:05 +02:00
|
|
|
static quint64 number(const char* data, size_t size, int base = 8)
|
2017-03-21 01:15:29 +01:00
|
|
|
{
|
|
|
|
const char *sp;
|
|
|
|
quint64 val = 0;
|
|
|
|
|
|
|
|
for (sp = data; sp < data + size; sp++)
|
|
|
|
if (isdigit(*sp))
|
|
|
|
break;
|
|
|
|
for (; sp < data + size && isdigit(*sp); sp++)
|
2017-06-25 12:24:05 +02:00
|
|
|
val = val * base + *sp - '0';
|
2017-03-21 01:15:29 +01:00
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-03-08 02:24:10 +01:00
|
|
|
bool Tar::open()
|
2017-03-21 01:15:29 +01:00
|
|
|
{
|
|
|
|
if (!_file.open(QIODevice::ReadOnly))
|
|
|
|
return false;
|
|
|
|
|
2018-03-08 02:24:10 +01:00
|
|
|
QFileInfo fi(_file.fileName());
|
2017-06-25 12:24:05 +02:00
|
|
|
QString tmiPath = fi.path() + "/" + fi.completeBaseName() + ".tmi";
|
|
|
|
|
|
|
|
if (loadTmi(tmiPath))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return loadTar();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Tar::loadTar()
|
|
|
|
{
|
|
|
|
char buffer[BLOCKSIZE];
|
2020-10-12 20:05:17 +02:00
|
|
|
TARHeader *hdr = (TARHeader*)&buffer;
|
2017-06-25 12:24:05 +02:00
|
|
|
quint64 size;
|
|
|
|
qint64 ret;
|
|
|
|
|
2017-03-26 18:08:37 +02:00
|
|
|
while ((ret = _file.read(buffer, BLOCKSIZE)) > 0) {
|
2017-04-14 22:39:33 +02:00
|
|
|
if (ret < BLOCKSIZE) {
|
|
|
|
_file.close();
|
2017-06-25 12:34:48 +02:00
|
|
|
_index.clear();
|
2017-03-26 18:08:37 +02:00
|
|
|
return false;
|
2017-04-14 22:39:33 +02:00
|
|
|
}
|
2017-03-26 18:08:37 +02:00
|
|
|
size = number(hdr->size, sizeof(hdr->size));
|
2017-06-25 12:24:05 +02:00
|
|
|
_index.insert(hdr->name, _file.pos() / BLOCKSIZE - 1);
|
2017-04-14 22:39:33 +02:00
|
|
|
if (!_file.seek(_file.pos() + BLOCKCOUNT(size) * BLOCKSIZE)) {
|
|
|
|
_file.close();
|
2017-06-25 12:34:48 +02:00
|
|
|
_index.clear();
|
2017-03-26 18:08:37 +02:00
|
|
|
return false;
|
2017-04-14 22:39:33 +02:00
|
|
|
}
|
2017-03-21 01:15:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-25 12:24:05 +02:00
|
|
|
bool Tar::loadTmi(const QString &path)
|
|
|
|
{
|
|
|
|
quint64 block;
|
|
|
|
int ln = 1;
|
|
|
|
|
|
|
|
QFile file(path);
|
|
|
|
if (!file.open(QIODevice::ReadOnly))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (!file.atEnd()) {
|
|
|
|
QByteArray line = file.readLine();
|
|
|
|
int pos = line.indexOf(':');
|
|
|
|
if (line.size() < 10 || pos < 7 || !line.startsWith("block")) {
|
2018-09-24 23:07:11 +02:00
|
|
|
qWarning("%s:%d: syntax error", qPrintable(path), ln);
|
2017-06-25 12:24:05 +02:00
|
|
|
_index.clear();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
block = number(line.constData() + 6, line.size() - 6, 10);
|
|
|
|
QString file(line.mid(pos + 1).trimmed());
|
|
|
|
|
|
|
|
_index.insert(file, block);
|
|
|
|
ln++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-21 01:15:29 +01:00
|
|
|
QByteArray Tar::file(const QString &name)
|
|
|
|
{
|
2017-06-25 12:24:05 +02:00
|
|
|
char buffer[BLOCKSIZE];
|
2020-10-12 20:05:17 +02:00
|
|
|
TARHeader *hdr = (TARHeader*)&buffer;
|
2017-06-25 12:24:05 +02:00
|
|
|
quint64 size;
|
|
|
|
|
2018-03-29 00:29:08 +02:00
|
|
|
QMap<QString, quint64>::const_iterator it(_index.find(name));
|
|
|
|
if (it == _index.constEnd())
|
2017-03-26 15:32:55 +02:00
|
|
|
return QByteArray();
|
2017-03-21 01:15:29 +01:00
|
|
|
|
2017-04-14 22:39:33 +02:00
|
|
|
Q_ASSERT(_file.isOpen());
|
2017-06-25 12:24:05 +02:00
|
|
|
if (_file.seek(it.value() * BLOCKSIZE)) {
|
|
|
|
if (_file.read(buffer, BLOCKSIZE) < BLOCKSIZE)
|
|
|
|
return QByteArray();
|
|
|
|
size = number(hdr->size, sizeof(hdr->size));
|
|
|
|
return _file.read(size);
|
|
|
|
} else
|
2017-03-21 01:15:29 +01:00
|
|
|
return QByteArray();
|
|
|
|
}
|