File indexing completed on 2023-12-03 04:55:08

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     QString mountPath = QStorageInfo(path).rootPath();
0023 
0024     // We list volumes to find the one with matching mount path
0025 
0026     for (const auto &d : Solid::Device::allDevices()) {
0027         if (d.is<Solid::StorageAccess>()) {
0028             auto iface = d.as<Solid::StorageAccess>();
0029             if (iface->filePath() == mountPath) {
0030                 auto parent = d.parent();
0031 
0032                 // try to cope with encrypted devices
0033                 if (!parent.isValid() || !parent.is<Solid::StorageDrive>()) {
0034                     if (d.is<Solid::StorageVolume>()) {
0035                         auto volume_iface = d.as<Solid::StorageVolume>();
0036                         auto enc = volume_iface->encryptedContainer();
0037                         if (enc.isValid()) {
0038                             parent = enc.parent();
0039                         }
0040                     }
0041                 }
0042 
0043                 if (parent.isValid() && parent.is<Solid::StorageDrive>()) {
0044                     auto parent_iface = parent.as<Solid::StorageDrive>();
0045                     return parent_iface->isRemovable();
0046                 }
0047             }
0048         }
0049     }
0050     // not found, defaulting to false
0051     return false;
0052 }