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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
|
/*
SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
SPDX-FileCopyrightText: 2002 Szombathelyi György <gyurco@users.sourceforge.net>
SPDX-FileCopyrightText: 2003 Leo Savernik <l.savernik@aon.at>
SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
This file is heavily based on ktar from kdelibs
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kiso.h"
// QtCore
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QMimeDatabase>
#include <QMimeType>
#include <qplatformdefs.h>
#include <KCompressionDevice>
#include <KConfig>
#include <KConfigGroup>
#include <KFilterBase>
#include "libisofs/isofs.h"
#include "qfilehack.h"
#ifdef Q_OS_LINUX
#undef __STRICT_ANSI__
#include <linux/cdrom.h>
#define __STRICT_ANSI__
#include <fcntl.h>
#include <sys/ioctl.h>
#endif
////////////////////////////////////////////////////////////////////////
/////////////////////////// KIso ///////////////////////////////////
////////////////////////////////////////////////////////////////////////
/**
* puts the track layout of the device 'fname' into 'tracks'
* tracks structure: start sector, track number, ...
* tracks should be 100*2 entry long (this is the maximum in the CD-ROM standard)
* currently it's linux only, porters are welcome
*/
static int getTracks(const char *fname, int *tracks)
{
KRFUNC;
int ret = 0;
memset(tracks, 0, 200 * sizeof(int));
#ifdef Q_OS_LINUX
int fd, i;
struct cdrom_tochdr tochead;
struct cdrom_tocentry tocentry;
// qDebug() << "getTracks open:" << fname << endl;
fd = QT_OPEN(fname, O_RDONLY | O_NONBLOCK);
if (fd > 0) {
if (ioctl(fd, CDROMREADTOCHDR, &tochead) != -1) {
// qDebug() << "getTracks first track:" << tochead.cdth_trk0
// << " last track " << tochead.cdth_trk1 << endl;
for (i = tochead.cdth_trk0; i <= tochead.cdth_trk1; ++i) {
if (ret > 99)
break;
memset(&tocentry, 0, sizeof(struct cdrom_tocentry));
tocentry.cdte_track = static_cast<__u8>(i);
tocentry.cdte_format = CDROM_LBA;
if (ioctl(fd, CDROMREADTOCENTRY, &tocentry) < 0)
break;
// qDebug() << "getTracks got track " << i << " starting at: " <<
// tocentry.cdte_addr.lba << endl;
if ((tocentry.cdte_ctrl & 0x4) == 0x4) {
tracks[ret << 1] = tocentry.cdte_addr.lba;
tracks[(ret << 1) + 1] = i;
ret++;
}
}
}
close(fd);
}
#endif
return ret;
}
class KIso::KIsoPrivate
{
public:
KIsoPrivate() = default;
QStringList dirList;
};
KIso::KIso(const QString &filename, const QString &_mimetype)
: KArchive(nullptr)
{
KRFUNC;
KRDEBUG("Starting KIso: " << filename << " - type: " << _mimetype);
m_startsec = -1;
m_filename = filename;
d = new KIsoPrivate;
QString mimetype(_mimetype);
bool forced = true;
if (mimetype.isEmpty()) {
QMimeDatabase db;
QMimeType mt = db.mimeTypeForFile(filename, QMimeDatabase::MatchContent);
if (mt.isValid())
mimetype = mt.name();
// qDebug() << "KIso::KIso mimetype=" << mimetype << endl;
// Don't move to prepareDevice - the other constructor theoretically allows ANY filter
if (mimetype == "application/x-tgz" || mimetype == "application/x-targz" || // the latter is deprecated but might still be around
mimetype == "application/x-webarchive")
// that's a gzipped tar file, so ask for gzip filter
mimetype = "application/x-gzip";
else if (mimetype == "application/x-tbz") // that's a bzipped2 tar file, so ask for bz2 filter
mimetype = "application/x-bzip2";
else {
// Something else. Check if it's not really gzip though (e.g. for KOffice docs)
QFile file(filename);
if (file.open(QIODevice::ReadOnly)) {
char firstByte;
char secondByte;
char thirdByte;
file.getChar(&firstByte);
file.getChar(&secondByte);
file.getChar(&thirdByte);
if (firstByte == 0037 && secondByte == (char)0213)
mimetype = "application/x-gzip";
else if (firstByte == 'B' && secondByte == 'Z' && thirdByte == 'h')
mimetype = "application/x-bzip2";
else if (firstByte == 'P' && secondByte == 'K' && thirdByte == 3) {
char fourthByte;
file.getChar(&fourthByte);
if (fourthByte == 4)
mimetype = "application/x-zip";
}
}
}
forced = false;
}
prepareDevice(filename, mimetype, forced);
}
void KIso::prepareDevice(const QString &filename, const QString &mimetype, bool forced)
{
KRFUNC;
KRDEBUG("Preparing: " << filename << " - type: " << mimetype << " - using the force: " << forced);
/* 'hack' for Qt's false assumption that only S_ISREG is seekable */
if ("inode/blockdevice" == mimetype)
setDevice(new QFileHack(filename));
else {
if ("application/x-gzip" == mimetype || "application/x-bzip2" == mimetype)
forced = true;
KCompressionDevice *device;
if (mimetype.isEmpty()) {
device = new KCompressionDevice(filename);
} else {
device = new KCompressionDevice(filename, COMPRESSIONTYPEFORMIMETYPE(mimetype));
}
if (device->compressionType() == KCompressionDevice::None && forced) {
delete device;
} else {
setDevice(device);
}
}
}
KIso::KIso(QIODevice *dev)
: KArchive(dev)
{
d = new KIsoPrivate;
}
KIso::~KIso()
{
// mjarrett: Closes to prevent ~KArchive from aborting w/o device
if (isOpen())
close();
if (!m_filename.isEmpty())
delete device(); // we created it ourselves
delete d;
}
/* callback function for libisofs */
static int readf(char *buf, unsigned int start, unsigned int len, void *udata)
{
KRFUNC;
QIODevice *dev = (static_cast<KIso *>(udata))->device();
// seek(0) ensures integrity with the QIODevice's built-in buffer
// see bug #372023 for details
dev->seek(0);
if (dev->seek((qint64)start << (qint64)11)) {
if ((dev->read(buf, len << 11u)) != -1)
return (len);
}
// qDebug() << "KIso::ReadRequest failed start: " << start << " len: " << len << endl;
return -1;
}
/* callback function for libisofs */
static int mycallb(struct iso_directory_record *idr, void *udata)
{
KRFUNC;
auto *iso = static_cast<KIso *>(udata);
QString path, user, group, symlink;
int i;
int access;
time_t time, cdate, adate;
rr_entry rr;
bool special = false;
KArchiveEntry *entry = nullptr, *oldentry = nullptr;
char z_algo[2], z_params[2];
long long z_size = 0;
if ((idr->flags[0] & 1) && !iso->showhidden)
return 0;
if (iso->level) {
if (isonum_711(idr->name_len) == 1) {
switch (idr->name[0]) {
case 0:
path += (".");
special = true;
break;
case 1:
path += ("..");
special = true;
break;
}
}
if (iso->showrr && ParseRR(idr, &rr) > 0) {
if (!special)
path = rr.name;
symlink = rr.sl;
access = rr.mode;
time = rr.t_mtime;
adate = rr.t_atime;
cdate = rr.t_ctime;
user.setNum(rr.uid);
group.setNum(rr.gid);
z_algo[0] = rr.z_algo[0];
z_algo[1] = rr.z_algo[1];
z_params[0] = rr.z_params[0];
z_params[1] = rr.z_params[1];
z_size = rr.z_size;
} else {
access = iso->dirent->permissions() & ~S_IFMT;
adate = cdate = time = isodate_915(idr->date, 0);
user = iso->dirent->user();
group = iso->dirent->group();
if (idr->flags[0] & 2)
access |= S_IFDIR;
else
access |= S_IFREG;
if (!special) {
if (iso->joliet) {
for (i = 0; i < (isonum_711(idr->name_len) - 1); i += 2) {
void *p = &(idr->name[i]);
QChar ch(be2me_16(*(ushort *)p));
if (ch == ';')
break;
path += ch;
}
} else {
for (i = 0; i < isonum_711(idr->name_len); ++i) {
if (idr->name[i] == ';')
break;
if (idr->name[i])
path += (idr->name[i]);
}
}
if (path.endsWith('.'))
path.resize(path.length() - 1);
}
}
if (iso->showrr)
FreeRR(&rr);
if (idr->flags[0] & 2) {
entry = new KIsoDirectory(iso, path, access | S_IFDIR, time, adate, cdate, user, group, symlink);
} else {
entry = new KIsoFile(iso,
path,
access,
time,
adate,
cdate,
user,
group,
symlink,
(long long)(isonum_733(idr->extent)) << (long long)11,
isonum_733(idr->size));
if (z_size)
(dynamic_cast<KIsoFile *>(entry))->setZF(z_algo, z_params, z_size);
}
iso->dirent->addEntry(entry);
}
if ((idr->flags[0] & 2) && (iso->level == 0 || !special)) {
if (iso->level) {
oldentry = iso->dirent;
iso->dirent = dynamic_cast<KIsoDirectory *>(entry);
}
iso->level++;
ProcessDir(&readf, isonum_733(idr->extent), isonum_733(idr->size), &mycallb, udata);
iso->level--;
if (iso->level)
iso->dirent = dynamic_cast<KIsoDirectory *>(oldentry);
}
return 0;
}
void KIso::addBoot(struct el_torito_boot_descriptor *bootdesc)
{
KRFUNC;
int i;
long long size;
boot_head boot;
boot_entry *be;
QString path;
KIsoFile *entry;
path = "Catalog";
entry = new KIsoFile(this,
path,
dirent->permissions() & ~S_IFDIR,
dirent->date(),
dirent->adate(),
dirent->cdate(),
dirent->user(),
dirent->group(),
QString(),
(long long)isonum_731(bootdesc->boot_catalog) << (long long)11,
(long long)2048);
dirent->addEntry(entry);
if (!ReadBootTable(&readf, isonum_731(bootdesc->boot_catalog), &boot, this)) {
i = 1;
be = boot.defentry;
while (be) {
size = BootImageSize(isonum_711(be->data.d_e.media), isonum_721(be->data.d_e.seccount));
path = "Default Image";
if (i > 1)
path += " (" + QString::number(i) + ')';
entry = new KIsoFile(this,
path,
dirent->permissions() & ~S_IFDIR,
dirent->date(),
dirent->adate(),
dirent->cdate(),
dirent->user(),
dirent->group(),
QString(),
(long long)isonum_731(be->data.d_e.start) << (long long)11,
size << (long long)9);
dirent->addEntry(entry);
be = be->next;
i++;
}
FreeBootTable(&boot);
}
}
void KIso::readParams()
{
KRFUNC;
KConfig *config;
config = new KConfig("kio_isorc");
KConfigGroup group(config, QString());
showhidden = group.readEntry("showhidden", false);
showrr = group.readEntry("showrr", true);
delete config;
}
bool KIso::openArchive(QIODevice::OpenMode mode)
{
KRFUNC;
iso_vol_desc *desc;
QString path, uid, gid;
QT_STATBUF buf;
int tracks[2 * 100], trackno = 0, i, access, c_b, c_i, c_j;
KArchiveDirectory *root;
struct iso_directory_record *idr;
struct el_torito_boot_descriptor *bootdesc;
if (mode == QIODevice::WriteOnly)
return false;
readParams();
d->dirList.clear();
tracks[0] = 0;
if (m_startsec > 0)
tracks[0] = m_startsec;
// qDebug() << " m_startsec: " << m_startsec << endl;
/* We'll use the permission and user/group of the 'host' file except
* in Rock Ridge, where the permissions are stored on the file system
*/
if (QT_STAT(m_filename.toLocal8Bit().data(), &buf) < 0) {
/* defaults, if stat fails */
memset(&buf, 0, sizeof(struct stat));
buf.st_mode = 0777;
} else {
/* If it's a block device, try to query the track layout (for multisession) */
if (m_startsec == -1 && S_ISBLK(buf.st_mode))
trackno = getTracks(m_filename.toLatin1().data(), (int *)&tracks);
}
uid.setNum(buf.st_uid);
gid.setNum(buf.st_gid);
access = buf.st_mode & ~S_IFMT;
root = new KIsoDirectory(this, QStringLiteral("/"), 0777 | S_IFDIR, buf.st_mtime, buf.st_atime, buf.st_ctime, uid, gid, QString());
setRootDir(root);
// qDebug() << "KIso::openArchive number of tracks: " << trackno << endl;
if (trackno == 0)
trackno = 1;
for (i = 0; i < trackno; ++i) {
c_b = 1;
c_i = 1;
c_j = 1;
root = rootDir();
if (trackno > 1) {
path.clear();
QTextStream(&path) << "Track " << tracks[(i << 1) + 1];
root = new KIsoDirectory(this, path, access | S_IFDIR, buf.st_mtime, buf.st_atime, buf.st_ctime, uid, gid, QString());
rootDir()->addEntry(root);
}
desc = ReadISO9660(&readf, tracks[i << 1], this);
if (!desc) {
// qDebug() << "KIso::openArchive no volume descriptors" << endl;
continue;
}
while (desc) {
switch (isonum_711(desc->data.type)) {
case ISO_VD_BOOT:
bootdesc = (struct el_torito_boot_descriptor *)&(desc->data);
if (!memcmp(EL_TORITO_ID, bootdesc->system_id, ISODCL(8, 39))) {
path = "El Torito Boot";
if (c_b > 1)
path += " (" + QString::number(c_b) + ')';
dirent = new KIsoDirectory(this, path, access | S_IFDIR, buf.st_mtime, buf.st_atime, buf.st_ctime, uid, gid, QString());
root->addEntry(dirent);
addBoot(bootdesc);
c_b++;
}
break;
case ISO_VD_PRIMARY:
case ISO_VD_SUPPLEMENTARY:
idr = (struct iso_directory_record *)&(((struct iso_primary_descriptor *)&desc->data)->root_directory_record);
joliet = JolietLevel(&desc->data);
if (joliet) {
QTextStream(&path) << "Joliet level " << joliet;
if (c_j > 1)
path += " (" + QString::number(c_j) + ')';
} else {
path = "ISO9660";
if (c_i > 1)
path += " (" + QString::number(c_i) + ')';
}
dirent = new KIsoDirectory(this, path, access | S_IFDIR, buf.st_mtime, buf.st_atime, buf.st_ctime, uid, gid, QString());
root->addEntry(dirent);
level = 0;
mycallb(idr, this);
if (joliet)
c_j++;
else
c_i++;
break;
}
desc = desc->next;
}
free(desc);
}
device()->close();
return true;
}
bool KIso::closeArchive()
{
KRFUNC;
d->dirList.clear();
return true;
}
bool KIso::writeDir(const QString &, const QString &, const QString &, mode_t, time_t, time_t, time_t)
{
return false;
}
bool KIso::prepareWriting(const QString &, const QString &, const QString &, qint64, mode_t, time_t, time_t, time_t)
{
return false;
}
bool KIso::finishWriting(qint64)
{
return false;
}
bool KIso::writeSymLink(const QString &, const QString &, const QString &, const QString &, mode_t, time_t, time_t, time_t)
{
return false;
}
bool KIso::doWriteDir(const QString &, const QString &, const QString &, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
{
return false;
}
bool KIso::doWriteSymLink(const QString &, const QString &, const QString &, const QString &, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
{
return false;
}
bool KIso::doPrepareWriting(const QString &, const QString &, const QString &, qint64, mode_t, const QDateTime &, const QDateTime &, const QDateTime &)
{
return false;
}
bool KIso::doFinishWriting(qint64)
{
return false;
}
void KIso::virtual_hook(int id, void *data)
{
KArchive::virtual_hook(id, data);
}
|