2015-06-04 01:29:22 +01:00
|
|
|
|
/*************************************************************************
|
|
|
|
|
* Copyright (C) 2008 by Volker Lanz <vl@fidra.de> *
|
2016-03-02 19:00:31 +00:00
|
|
|
|
* Copyright (C) 2016 by Andrius Štikonas <andrius@stikonas.eu> *
|
2015-06-04 01:29:22 +01:00
|
|
|
|
* *
|
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
|
* modify it under the terms of the GNU General Public License as *
|
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
|
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.*
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "core/device.h"
|
|
|
|
|
|
|
|
|
|
#include "core/partitiontable.h"
|
|
|
|
|
#include "core/smartstatus.h"
|
|
|
|
|
|
|
|
|
|
#include "util/capacity.h"
|
|
|
|
|
|
|
|
|
|
#include <KLocalizedString>
|
|
|
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
|
#include <QByteArray>
|
|
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
|
|
#if !defined(BLKPBSZGET)
|
|
|
|
|
#define BLKPBSZGET _IO(0x12,123)/* get block physical sector size */
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static qint32 getPhysicalSectorSize(const QString& device_node)
|
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
|
/*
|
|
|
|
|
* possible ways of getting the physical sector size for a drive:
|
|
|
|
|
* - ioctl(BLKPBSZGET) -- supported with Linux 2.6.32 and later
|
|
|
|
|
* - /sys/block/sda/queue/physical_block_size
|
|
|
|
|
* - libblkid from util-linux-ng 2.17 or later
|
|
|
|
|
* TODO: implement the blkid method
|
|
|
|
|
*/
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
|
|
|
|
#if defined(BLKPBSZGET)
|
2015-07-13 15:16:36 +01:00
|
|
|
|
int phSectorSize = -1;
|
|
|
|
|
int fd = open(device_node.toLocal8Bit().constData(), O_RDONLY);
|
|
|
|
|
if (fd != -1) {
|
|
|
|
|
if (ioctl(fd, BLKPBSZGET, &phSectorSize) >= 0) {
|
|
|
|
|
close(fd);
|
|
|
|
|
return phSectorSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
#endif
|
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
|
QFile f(QStringLiteral("/sys/block/%1/queue/physical_block_size").arg(QString(device_node).remove(QStringLiteral("/dev/"))));
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
|
QByteArray a = f.readLine();
|
|
|
|
|
return a.simplified().toInt();
|
|
|
|
|
}
|
2015-06-04 01:29:22 +01:00
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
|
return -1;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Constructs a Device with an empty PartitionTable.
|
2015-07-13 15:16:36 +01:00
|
|
|
|
@param name the Device's name, usually some string defined by the manufacturer
|
|
|
|
|
@param devicenode the Device's node, for example "/dev/sda"
|
|
|
|
|
@param heads the number of heads in CHS notation
|
|
|
|
|
@param numSectors the number of sectors in CHS notation
|
|
|
|
|
@param cylinders the number of cylinders in CHS notation
|
|
|
|
|
@param sectorSize the size of a sector in bytes
|
2015-06-04 01:29:22 +01:00
|
|
|
|
*/
|
|
|
|
|
Device::Device(const QString& name, const QString& devicenode, qint32 heads, qint32 numSectors, qint32 cylinders, qint64 sectorSize, const QString& iconname) :
|
2015-07-13 15:16:36 +01:00
|
|
|
|
QObject(),
|
|
|
|
|
m_Name(name.length() > 0 ? name : i18n("Unknown Device")),
|
|
|
|
|
m_DeviceNode(devicenode),
|
2015-07-22 14:48:03 +01:00
|
|
|
|
m_PartitionTable(nullptr),
|
2015-07-13 15:16:36 +01:00
|
|
|
|
m_Heads(heads),
|
|
|
|
|
m_SectorsPerTrack(numSectors),
|
|
|
|
|
m_Cylinders(cylinders),
|
|
|
|
|
m_LogicalSectorSize(sectorSize),
|
|
|
|
|
m_PhysicalSectorSize(getPhysicalSectorSize(devicenode)),
|
|
|
|
|
m_IconName(iconname.isEmpty() ? QStringLiteral("drive-harddisk") : iconname),
|
|
|
|
|
m_SmartStatus(new SmartStatus(devicenode))
|
2015-06-04 01:29:22 +01:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Destructs a Device. */
|
|
|
|
|
Device::~Device()
|
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
|
delete m_PartitionTable;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Device::operator==(const Device& other) const
|
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
|
return m_DeviceNode == other.m_DeviceNode;
|
2015-06-04 01:29:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Device::operator!=(const Device& other) const
|
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
|
return !(other == *this);
|
2015-06-04 01:29:22 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString Device::prettyName() const
|
|
|
|
|
{
|
2016-02-25 22:24:46 +00:00
|
|
|
|
return i18nc("Device name – Capacity (device node)", "%1 – %2 (%3)", name(), Capacity::formatByteSize(capacity()), deviceNode());
|
2015-06-04 01:29:22 +01:00
|
|
|
|
}
|