File indexing completed on 2024-12-01 04:29:20

0001 /*
0002     SPDX-FileCopyrightText: 2017 Nicolas Carion
0003     SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0004 */
0005 
0006 #include "devices.hpp"
0007 
0008 #include <QStorageInfo>
0009 #include <solid/block.h>
0010 #include <solid/device.h>
0011 #include <solid/storageaccess.h>
0012 #include <solid/storagedrive.h>
0013 #include <solid/storagevolume.h>
0014 
0015 bool isOnRemovableDevice(const QUrl &file)
0016 {
0017     return isOnRemovableDevice(file.path());
0018 }
0019 
0020 bool isOnRemovableDevice(const QString &path)
0021 {
0022 #ifdef Q_OS_MAC
0023     // Parsing Solid devices seems to crash on Mac
0024     return false;
0025 #endif
0026     const QString mountPath = QStorageInfo(path).rootPath();
0027     // We list volumes to find the one with matching mount path
0028 
0029     for (const auto &d : Solid::Device::allDevices()) {
0030         if (d.is<Solid::StorageAccess>()) {
0031             auto iface = d.as<Solid::StorageAccess>();
0032             if (iface->filePath() == mountPath) {
0033                 auto parent = d.parent();
0034 
0035                 // try to cope with encrypted devices
0036                 if (!parent.isValid() || !parent.is<Solid::StorageDrive>()) {
0037                     if (d.is<Solid::StorageVolume>()) {
0038                         auto volume_iface = d.as<Solid::StorageVolume>();
0039                         auto enc = volume_iface->encryptedContainer();
0040                         if (enc.isValid()) {
0041                             parent = enc.parent();
0042                         }
0043                     }
0044                 }
0045 
0046                 if (parent.isValid() && parent.is<Solid::StorageDrive>()) {
0047                     auto parent_iface = parent.as<Solid::StorageDrive>();
0048                     return parent_iface->isRemovable();
0049                 }
0050             }
0051         }
0052     }
0053     // not found, defaulting to false
0054     return false;
0055 }