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 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
|
/*
* SPDX-FileCopyrightText: 2022 Kai Uwe Broulik <kde@broulik.de>
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "kio_afc.h"
#include "afc_debug.h"
#include "afcapp.h"
#include "afcdevice.h"
#include "afcfile.h"
#include "afcfilereader.h"
#include "afcurl.h"
#include "afcutils.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QMimeDatabase>
#include <QMimeType>
#include <QMutexLocker>
#include <QScopeGuard>
#include <KFileUtils>
#include <KLocalizedString>
#include <algorithm>
using namespace KIO;
// Pseudo plugin class to embed meta data
class KIOPluginForMetaData : public QObject
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.kde.kio.worker.afc" FILE "afc.json")
};
using namespace KIO;
extern "C" {
int Q_DECL_EXPORT kdemain(int argc, char **argv)
{
QCoreApplication app(argc, argv);
app.setApplicationName(QStringLiteral("kio_afc"));
qCDebug(KIO_AFC_LOG) << "*** Starting kio_afc";
if (argc != 4) {
qCDebug(KIO_AFC_LOG) << "Usage: kio_afc protocol domain-socket1 domain-socket2";
exit(-1);
}
AfcWorker worker(argv[2], argv[3]);
worker.dispatchLoop();
return 0;
}
}
AfcWorker::AfcWorker(const QByteArray &poolSocket, const QByteArray &appSocket)
: WorkerBase(QByteArrayLiteral("kio_afc"), poolSocket, appSocket)
{
const auto result = init();
Q_ASSERT(result.success());
}
AfcWorker::~AfcWorker()
{
idevice_event_unsubscribe();
qDeleteAll(m_devices);
m_devices.clear();
}
Result AfcWorker::init()
{
bool ok;
const int logLevel = qEnvironmentVariableIntValue("KIO_AFC_LOG_VERBOSITY", &ok);
if (ok) {
idevice_set_debug_level(logLevel);
}
idevice_event_subscribe(
[](const idevice_event_t *event, void *user_data) {
// NOTE this is executed in a different thread!
static_cast<AfcWorker *>(user_data)->onDeviceEvent(event);
},
this);
updateDeviceList();
return Result::pass();
}
void AfcWorker::onDeviceEvent(const idevice_event_t *event)
{
// NOTE this is executed in a different thread!
switch (event->event) {
case IDEVICE_DEVICE_ADD:
qCDebug(KIO_AFC_LOG) << "idevice event ADD for" << event->udid;
addDevice(QString::fromLatin1(event->udid));
return;
case IDEVICE_DEVICE_REMOVE:
qCDebug(KIO_AFC_LOG) << "idevice event REMOVE for" << event->udid;
removeDevice(QString::fromLatin1(event->udid));
return;
#if IMOBILEDEVICE_API >= QT_VERSION_CHECK(1, 3, 0)
case IDEVICE_DEVICE_PAIRED:
qCDebug(KIO_AFC_LOG) << "idevice event PAIRED for" << event->udid;
return;
#endif
}
qCWarning(KIO_AFC_LOG) << "Unhandled idevice event" << event->event << "for" << event->udid;
}
AfcDevice *AfcWorker::deviceForUrl(const AfcUrl &afcUrl) const
{
return m_devices.value(afcUrl.device());
}
Result AfcWorker::clientForUrl(const AfcUrl &afcUrl, AfcClient::Ptr &client) const
{
AfcDevice *device = deviceForUrl(afcUrl);
if (!device) {
return Result::fail(ERR_DOES_NOT_EXIST, afcUrl.url().toDisplayString());
}
return device->client(afcUrl.appId(), client);
}
UDSEntry AfcWorker::overviewEntry(const QString &fileName) const
{
UDSEntry entry;
entry.fastInsert(UDSEntry::UDS_NAME, !fileName.isEmpty() ? fileName : i18n("Apple Devices"));
entry.fastInsert(UDSEntry::UDS_ICON_NAME, QStringLiteral("phone-apple-iphone"));
entry.fastInsert(UDSEntry::UDS_FILE_TYPE, S_IFDIR);
entry.fastInsert(UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory"));
return entry;
}
UDSEntry AfcWorker::deviceEntry(const AfcDevice *device, const QString &fileName, bool asLink) const
{
const QString deviceId = device->id();
const QString deviceClass = device->deviceClass();
UDSEntry entry;
entry.fastInsert(UDSEntry::UDS_NAME, !fileName.isEmpty() ? fileName : deviceId);
if (!device->name().isEmpty()) {
entry.fastInsert(UDSEntry::UDS_DISPLAY_NAME, device->name());
}
// TODO prettier
entry.fastInsert(UDSEntry::UDS_DISPLAY_TYPE, deviceClass);
entry.fastInsert(UDSEntry::UDS_FILE_TYPE, S_IFDIR);
entry.fastInsert(UDSEntry::UDS_MIME_TYPE, QStringLiteral("inode/directory"));
QString iconName;
// We can assume iPod running iOS/supporting imobiledevice is an iPod touch?
if (deviceClass.contains(QLatin1String("iPad"))) {
iconName = QStringLiteral("computer-apple-ipad");
} else if (deviceClass.contains(QLatin1String("iPod"))) {
iconName = QStringLiteral("multimedia-player-apple-ipod-touch");
} else {
iconName = QStringLiteral("phone-apple-iphone");
}
entry.fastInsert(UDSEntry::UDS_ICON_NAME, iconName);
if (asLink) {
const QString contentsUrl = QStringLiteral("afc://%1/").arg(deviceId);
entry.fastInsert(UDSEntry::UDS_LINK_DEST, contentsUrl);
entry.fastInsert(UDSEntry::UDS_TARGET_URL, contentsUrl);
}
return entry;
}
UDSEntry AfcWorker::appsOverviewEntry(const AfcDevice *device, const QString &fileName) const
{
UDSEntry entry;
entry.fastInsert(UDSEntry::UDS_NAME, !fileName.isEmpty() ? fileName : AfcUrl::appsTag());
entry.fastInsert(UDSEntry::UDS_DISPLAY_NAME, i18nc("Link to folder with files stored inside apps", "Apps"));
entry.fastInsert(UDSEntry::UDS_ICON_NAME, QStringLiteral("folder-documents"));
entry.fastInsert(UDSEntry::UDS_FILE_TYPE, S_IFDIR);
const QString appsUrl = QStringLiteral("afc://%1/%2/").arg(device->id(), AfcUrl::appsTag());
entry.fastInsert(UDSEntry::UDS_LINK_DEST, appsUrl);
entry.fastInsert(UDSEntry::UDS_TARGET_URL, appsUrl);
return entry;
}
void AfcWorker::updateDeviceList()
{
char **devices = nullptr;
int count = 0;
idevice_get_device_list(&devices, &count);
qCDebug(KIO_AFC_LOG) << "Update device list, found" << count << "devices";
for (int i = 0; i < count; ++i) {
const QString id = QString::fromLatin1(devices[i]);
addDevice(id);
}
if (devices) {
idevice_device_list_free(devices);
}
}
bool AfcWorker::addDevice(const QString &id)
{
// NOTE this may be executed in a different thread when called from device idevice_event callback
QMutexLocker locker(&m_mutex);
const QString upperCaseId = id.toUpper();
if (m_devices.contains(upperCaseId)) {
return false;
}
auto *device = new AfcDevice(id);
qCDebug(KIO_AFC_LOG) << "Created device:" << id;
if (!device->isValid()) {
qCInfo(KIO_AFC_LOG) << "Device" << id << "is not valid";
delete device;
return false;
}
m_devices.insert(upperCaseId, device);
Q_ASSERT(!device->name().isEmpty());
return true;
}
void AfcWorker::removeDevice(const QString &id)
{
// NOTE this may be executed in a different thread when called from device idevice_event callback
QMutexLocker locker(&m_mutex);
auto *device = m_devices.take(id.toUpper());
if (device) {
qCDebug(KIO_AFC_LOG) << "Removed device" << id;
if (m_openFile && m_openFile->client()->device() == device) {
m_openFile.reset();
}
delete device;
}
}
Result AfcWorker::listDir(const QUrl &url)
{
qCDebug(KIO_AFC_LOG) << "list directory:" << url;
const AfcUrl afcUrl(url);
if (!afcUrl.isValid()) {
return Result::fail(ERR_MALFORMED_URL, url.toDisplayString());
}
// Don't have an empty path.
// Otherwise we get "invalid URL" errors when trying to enter a subfolder.
if (!url.host().isEmpty() && url.path().isEmpty()) {
QUrl newUrl = url;
newUrl.setPath(QStringLiteral("/"));
redirection(newUrl);
return Result::pass();
}
if (afcUrl.device().isEmpty()) {
updateDeviceList();
for (AfcDevice *device : m_devices) {
UDSEntry entry = deviceEntry(device, QString(), true /*asLink*/);
// When there is only one device, redirect to it right away
if (m_devices.count() == 1) {
const QUrl redirectUrl(entry.stringValue(UDSEntry::UDS_TARGET_URL));
qCDebug(KIO_AFC_LOG) << "There is only one device, redirecting to" << redirectUrl;
redirection(redirectUrl);
return Result::pass();
}
listEntry(entry);
}
// We cannot just list that at the beginning because we might do a redirect
listEntry(overviewEntry(QStringLiteral(".")));
return Result::pass();
}
AfcDevice *device = deviceForUrl(afcUrl);
if (!device) {
return Result::fail(ERR_DOES_NOT_EXIST, afcUrl.device());
}
const QString appId = afcUrl.appId();
if (afcUrl.browseMode() == AfcUrl::BrowseMode::Apps && appId.isEmpty()) {
QList<AfcApp> apps;
const auto result = device->apps(apps);
if (!result.success()) {
return result;
}
// Cannot browse apps without sharing enabled, don't list them.
apps.erase(std::remove_if(apps.begin(),
apps.end(),
[](const AfcApp &app) {
return !app.sharingEnabled();
}),
apps.end());
device->fetchAppIcons(apps);
for (const auto &app : apps) {
listEntry(app.entry());
}
listEntry(appsOverviewEntry(device, QStringLiteral(".")));
return Result::pass();
}
AfcClient::Ptr client;
Result result = device->client(appId, client);
if (!result.success()) {
return result;
}
// Ourself must be "."
UDSEntry rootEntry;
result = client->entry(afcUrl.path(), rootEntry);
if (!result.success()) {
return result;
}
// NOTE this must not be "fastInsert" as AfcDevice::entry already sets a name
rootEntry.replace(UDSEntry::UDS_NAME, QStringLiteral("."));
listEntry(rootEntry);
QStringList files;
result = client->entryList(afcUrl.path(), files);
if (!result.success()) {
// One can only access the "Documents" folder within an app, redirect to it if applicable
if (result.error() == KIO::ERR_ACCESS_DENIED && !afcUrl.appId().isEmpty() && afcUrl.path().isEmpty()) {
QUrl newUrl = url;
newUrl.setPath(newUrl.path() + QLatin1String("/Documents"));
qCDebug(KIO_AFC_LOG) << "Got access denied on app root folder, redirecting to Documents folder";
redirection(newUrl);
return Result::pass();
}
return result;
}
for (const QString &file : files) {
QString absolutePath = afcUrl.path();
if (!absolutePath.endsWith(QLatin1Char('/')) && !file.startsWith(QLatin1Char('/'))) {
absolutePath.append(QLatin1Char('/'));
}
absolutePath.append(file);
UDSEntry entry;
result = client->entry(absolutePath, entry);
if (!result.success()) {
qCWarning(KIO_AFC_LOG) << "Failed to list" << absolutePath << result.error() << result.errorString();
continue;
}
listEntry(entry);
}
// Add link to "Apps documents" to device root folder
if (afcUrl.path().isEmpty()) {
listEntry(appsOverviewEntry(device));
}
return Result::pass();
}
Result AfcWorker::stat(const QUrl &url)
{
const AfcUrl afcUrl(url);
if (!afcUrl.isValid()) {
return Result::fail(ERR_MALFORMED_URL, url.toDisplayString());
}
// Device overview page afc:/
if (afcUrl.device().isEmpty()) {
statEntry(overviewEntry());
return Result::pass();
}
AfcDevice *device = deviceForUrl(afcUrl);
if (!device) {
return Result::fail(ERR_DOES_NOT_EXIST, url.toDisplayString());
}
if (afcUrl.path().isEmpty()) {
// Device file system or device app overview
if (afcUrl.appId().isEmpty()) {
UDSEntry rootEntry = deviceEntry(device);
if (afcUrl.browseMode() == AfcUrl::BrowseMode::Apps) {
rootEntry.replace(UDSEntry::UDS_DISPLAY_NAME, i18nc("Link to folder with files stored inside apps", "Apps"));
}
statEntry(rootEntry);
return Result::pass();
}
// App folder
AfcApp app = device->app(afcUrl.appId());
if (!app.isValid()) {
return Result::fail(KIO::ERR_DOES_NOT_EXIST, afcUrl.appId());
}
device->fetchAppIcon(app);
UDSEntry appEntry = app.entry();
statEntry(appEntry);
return Result::pass();
}
AfcClient::Ptr client;
auto result = device->client(afcUrl.appId(), client);
if (!result.success()) {
return result;
}
UDSEntry entry;
result = client->entry(afcUrl.path(), entry);
if (!result.success()) {
return result;
}
statEntry(entry);
return Result::pass();
}
void AfcWorker::guessMimeType(AfcFile &file, const QString &path)
{
// Determine the mimetype of the file to be retrieved, and emit it.
// This is mandatory in all workers...
AfcFileReader reader = file.reader();
reader.setSize(1024);
const Result result = reader.read();
if (result.success()) {
QMimeDatabase db;
const QString fileName = path.section(QLatin1Char('/'), -1, -1);
QMimeType mime = db.mimeTypeForFileNameAndData(fileName, reader.data());
mimeType(mime.name());
}
file.seek(0);
}
Result AfcWorker::get(const QUrl &url)
{
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
auto result = clientForUrl(afcUrl, client);
if (!result.success()) {
return result;
}
UDSEntry entry;
result = client->entry(afcUrl.path(), entry);
if (!result.success()) {
return result;
}
AfcFile file(client, afcUrl.path());
result = file.open(QIODevice::ReadOnly);
if (!result.success()) {
return result;
}
const auto size = entry.numberValue(UDSEntry::UDS_SIZE, 0);
totalSize(size);
guessMimeType(file, afcUrl.path());
position(0);
AfcFileReader reader = file.reader();
reader.setSize(size);
while (reader.hasMore()) {
const auto result = reader.read();
if (!result.success()) {
return result;
}
data(reader.data());
}
return Result::pass();
}
Result AfcWorker::put(const QUrl &url, int permissions, JobFlags flags)
{
Q_UNUSED(permissions);
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
Result result = clientForUrl(afcUrl, client);
if (!result.success()) {
return result;
}
UDSEntry entry;
result = client->entry(afcUrl.path(), entry);
const bool exists = result.error() != ERR_DOES_NOT_EXIST;
if (exists && !flags.testFlag(Overwrite) && !flags.testFlag(Resume)) {
if (S_ISDIR(entry.numberValue(UDSEntry::UDS_FILE_TYPE))) {
return Result::fail(ERR_DIR_ALREADY_EXIST, afcUrl.path());
}
return Result::fail(ERR_FILE_ALREADY_EXIST, afcUrl.path());
}
AfcFile file(client, afcUrl.path());
if (flags.testFlag(Resume)) {
result = file.open(QIODevice::Append);
} else {
result = file.open(QIODevice::WriteOnly);
}
if (!result.success()) {
return result;
}
int readDataResult = 0;
do {
QByteArray buffer;
dataReq();
readDataResult = readData(buffer);
if (readDataResult < 0) {
return Result::fail(ERR_CANNOT_READ, QStringLiteral("readData result was %1").arg(readDataResult));
}
uint32_t bytesWritten = 0;
const auto result = file.write(buffer, bytesWritten);
if (!result.success()) {
return result;
}
} while (readDataResult > 0);
const QString modifiedMeta = metaData(QStringLiteral("modified"));
if (!modifiedMeta.isEmpty()) {
const QDateTime mtime = QDateTime::fromString(modifiedMeta, Qt::ISODate);
if (mtime.isValid() && !client->setModificationTime(afcUrl.path(), mtime).success()) {
qCWarning(KIO_AFC_LOG) << "Failed to set mtime for" << afcUrl.path() << "in put";
}
}
return Result::pass();
}
Result AfcWorker::open(const QUrl &url, QIODevice::OpenMode mode)
{
// TODO fail if already open?
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
Result result = clientForUrl(afcUrl, client);
if (!result.success()) {
return result;
}
UDSEntry entry;
result = client->entry(afcUrl.path(), entry);
if (!result.success()) {
return result;
}
auto file = std::make_unique<AfcFile>(client, afcUrl.path());
result = file->open(mode);
if (!result.success()) {
return result;
}
if (mode.testFlag(QIODevice::ReadOnly) && !mode.testFlag(QIODevice::Append)) {
guessMimeType(*file, afcUrl.path());
}
m_openFile = std::move(file);
totalSize(entry.numberValue(UDSEntry::UDS_SIZE, 0));
position(0);
return Result::pass();
}
Result AfcWorker::read(filesize_t bytesRequested)
{
if (!m_openFile) {
return Result::fail(ERR_CANNOT_READ, i18n("Cannot read without opening file first"));
}
AfcFileReader reader = m_openFile->reader();
reader.setSize(bytesRequested);
while (reader.hasMore()) {
const Result result = reader.read();
if (!result.success()) {
return result;
}
data(reader.data());
}
return Result::pass();
}
Result AfcWorker::seek(filesize_t offset)
{
if (!m_openFile) {
return Result::fail(ERR_CANNOT_SEEK, i18n("Cannot seek without opening file first"));
}
const Result result = m_openFile->seek(offset);
if (result.success()) {
position(offset);
}
return result;
}
Result AfcWorker::truncate(filesize_t length)
{
if (!m_openFile) {
return Result::fail(ERR_CANNOT_TRUNCATE, QStringLiteral("Cannot truncate without opening file first"));
}
Result result = m_openFile->truncate(length);
if (result.success()) {
truncated(length);
}
return result;
}
Result AfcWorker::write(const QByteArray &data)
{
if (!m_openFile) {
return Result::fail(ERR_CANNOT_WRITE, i18n("Cannot write without opening file first"));
}
uint32_t bytesWritten = 0;
const Result result = m_openFile->write(data, bytesWritten);
if (result.success()) {
written(bytesWritten);
}
return result;
}
Result AfcWorker::close()
{
if (!m_openFile) {
return Result::fail(ERR_INTERNAL, QStringLiteral("Cannot close what is not open"));
}
const Result result = m_openFile->close();
if (result.success()) {
m_openFile.reset();
}
return result;
}
Result AfcWorker::copy(const QUrl &src, const QUrl &dest, int permissions, JobFlags flags)
{
Q_UNUSED(permissions);
const AfcUrl srcAfcUrl(src);
const AfcUrl destAfcUrl(dest);
if (srcAfcUrl.device() != destAfcUrl.device()) {
// Let KIO handle copying onto, off the, and between devices
return Result::fail(ERR_UNSUPPORTED_ACTION);
}
AfcClient::Ptr client;
Result result = clientForUrl(srcAfcUrl, client);
if (!result.success()) {
return result;
}
UDSEntry srcEntry;
result = client->entry(srcAfcUrl.path(), srcEntry);
if (!result.success()) {
return result;
}
UDSEntry destEntry;
result = client->entry(destAfcUrl.path(), destEntry);
const bool exists = result.error() != ERR_DOES_NOT_EXIST;
if (exists && !flags.testFlag(Overwrite)) {
if (S_ISDIR(destEntry.numberValue(UDSEntry::UDS_FILE_TYPE))) {
return Result::fail(ERR_DIR_ALREADY_EXIST, destAfcUrl.path());
}
return Result::fail(ERR_FILE_ALREADY_EXIST, destAfcUrl.path());
}
AfcFile srcFile(client, srcAfcUrl.path());
result = srcFile.open(QIODevice::ReadOnly);
if (!result.success()) {
return result;
}
AfcFile destFile(client, destAfcUrl.path());
if (flags.testFlag(Resume)) {
result = destFile.open(QIODevice::Append);
} else {
result = destFile.open(QIODevice::WriteOnly);
}
if (!result.success()) {
return result;
}
auto cleanup = qScopeGuard([&client, &destAfcUrl] {
qCInfo(KIO_AFC_LOG) << "Cleaning up leftover file" << destAfcUrl.path();
// NOTE cannot emit failure here because emitResult
// will already have been called before
auto result = client->del(destAfcUrl.path());
if (!result.success()) {
qCWarning(KIO_AFC_LOG) << "Failed to clean up" << result.error() << result.errorString();
}
});
const auto size = srcEntry.numberValue(UDSEntry::UDS_SIZE, 0);
totalSize(size);
AfcFileReader reader = srcFile.reader();
reader.setSize(size);
KIO::filesize_t copied = 0;
while (!wasKilled() && reader.hasMore()) {
auto result = reader.read();
if (!result.success()) {
return result;
}
const QByteArray chunk = reader.data();
uint32_t bytesWritten = 0;
result = destFile.write(chunk, bytesWritten);
if (!result.success()) {
return result;
}
// TODO check if bytesWritten matches reader.data().size()?
copied += chunk.size();
processedSize(copied);
}
cleanup.dismiss();
destFile.close();
// TODO check if conversion back and forth QDateTime is too expensive when copying many files
const QDateTime mtime = QDateTime::fromSecsSinceEpoch(srcEntry.numberValue(KIO::UDSEntry::UDS_MODIFICATION_TIME, 0));
if (mtime.isValid()) {
client->setModificationTime(destAfcUrl.path(), mtime);
}
return Result::pass();
}
Result AfcWorker::del(const QUrl &url, bool isFile)
{
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
Result result = clientForUrl(afcUrl, client);
if (result.success()) {
if (isFile) {
result = client->del(afcUrl.path());
} else {
result = client->delRecursively(afcUrl.path());
}
}
return result;
}
Result AfcWorker::rename(const QUrl &url, const QUrl &dest, JobFlags flags)
{
const AfcUrl srcAfcUrl(url);
const AfcUrl destAfcUrl(dest);
if (srcAfcUrl.device() != destAfcUrl.device()) {
return Result::fail(ERR_CANNOT_RENAME, i18n("Cannot rename between devices."));
}
AfcClient::Ptr client;
Result result = clientForUrl(srcAfcUrl, client);
if (result.success()) {
result = client->rename(srcAfcUrl.path(), destAfcUrl.path(), flags);
}
return result;
}
Result AfcWorker::symlink(const QString &target, const QUrl &dest, JobFlags flags)
{
const AfcUrl destAfcUrl(dest);
AfcClient::Ptr client;
Result result = clientForUrl(destAfcUrl, client);
if (result.success()) {
result = client->symlink(target, destAfcUrl.path(), flags);
}
return result;
}
Result AfcWorker::mkdir(const QUrl &url, int permissions)
{
Q_UNUSED(permissions)
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
Result result = clientForUrl(afcUrl, client);
if (result.success()) {
result = client->mkdir(afcUrl.path());
}
return result;
}
Result AfcWorker::setModificationTime(const QUrl &url, const QDateTime &mtime)
{
const AfcUrl afcUrl(url);
AfcClient::Ptr client;
Result result = clientForUrl(afcUrl, client);
if (result.success()) {
result = client->setModificationTime(afcUrl.path(), mtime);
}
return result;
}
Result AfcWorker::fileSystemFreeSpace(const QUrl &url)
{
// TODO FileSystemFreeSpaceJob does not follow redirects!
const AfcUrl afcUrl(url);
if (afcUrl.device().isEmpty() && m_devices.count() == 1) {
return fileSystemFreeSpace(QUrl(QStringLiteral("afc://%1/").arg((*m_devices.constBegin())->id())));
}
AfcClient::Ptr client;
const Result result = clientForUrl(afcUrl, client);
if (!result.success()) {
return result;
}
const AfcDiskUsage diskUsage(client);
if (!diskUsage.isValid()) {
return Result::fail(ERR_CANNOT_STAT, url.toDisplayString());
}
setMetaData(QStringLiteral("total"), QString::number(diskUsage.total()));
setMetaData(QStringLiteral("available"), QString::number(diskUsage.free()));
return Result::pass();
}
#include "kio_afc.moc"
|