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/copysourcedevice.h"
|
|
|
|
|
|
|
|
#include "backend/corebackend.h"
|
|
|
|
#include "backend/corebackendmanager.h"
|
|
|
|
#include "backend/corebackenddevice.h"
|
|
|
|
|
|
|
|
#include "core/copytarget.h"
|
|
|
|
#include "core/copytargetdevice.h"
|
|
|
|
#include "core/device.h"
|
|
|
|
|
|
|
|
/** Constructs a CopySource on the given Device
|
2015-07-13 15:16:36 +01:00
|
|
|
@param d Device from which to copy
|
|
|
|
@param firstsector First sector that will be copied
|
|
|
|
@param lastsector Last sector that will be copied
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
CopySourceDevice::CopySourceDevice(Device& d, qint64 firstsector, qint64 lastsector) :
|
2015-07-13 15:16:36 +01:00
|
|
|
CopySource(),
|
|
|
|
m_Device(d),
|
|
|
|
m_FirstSector(firstsector),
|
|
|
|
m_LastSector(lastsector),
|
2015-07-22 14:48:03 +01:00
|
|
|
m_BackendDevice(nullptr)
|
2015-06-04 01:29:22 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Destructs a CopySourceDevice */
|
|
|
|
CopySourceDevice::~CopySourceDevice()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
delete m_BackendDevice;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Opens the Device
|
2015-07-13 15:16:36 +01:00
|
|
|
@return true if the Device could be successfully opened
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool CopySourceDevice::open()
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
m_BackendDevice = CoreBackendManager::self()->backend()->openDeviceExclusive(device().deviceNode());
|
2015-07-22 14:48:03 +01:00
|
|
|
return m_BackendDevice != nullptr;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the Device's sector size
|
2015-07-13 15:16:36 +01:00
|
|
|
@return the sector size
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
qint32 CopySourceDevice::sectorSize() const
|
|
|
|
{
|
2016-05-31 19:28:31 +01:00
|
|
|
return device().logicalSize();
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the length of this CopySource
|
2015-07-13 15:16:36 +01:00
|
|
|
@return length of the copy source
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
qint64 CopySourceDevice::length() const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
return lastSector() - firstSector() + 1;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Reads a given number of sectors from the Device into the given buffer.
|
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
Note that @p readOffset must be greater or equal than zero.
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
@param buffer the buffer to store the read sectors in
|
|
|
|
@param readOffset the offset to begin reading
|
|
|
|
@param numSectors the number of sector to read
|
2015-06-04 01:29:22 +01:00
|
|
|
|
2015-07-13 15:16:36 +01:00
|
|
|
@return true if successful
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool CopySourceDevice::readSectors(void* buffer, qint64 readOffset, qint64 numSectors)
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
Q_ASSERT(readOffset >= 0);
|
|
|
|
return m_BackendDevice->readSectors(buffer, readOffset, numSectors);
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Checks if this CopySourceDevice overlaps with the given CopyTarget
|
2015-07-13 15:16:36 +01:00
|
|
|
@param target the CopyTarget to check overlapping with
|
|
|
|
@return true if overlaps
|
2015-06-04 01:29:22 +01:00
|
|
|
*/
|
|
|
|
bool CopySourceDevice::overlaps(const CopyTarget& target) const
|
|
|
|
{
|
2015-07-13 15:16:36 +01:00
|
|
|
try {
|
|
|
|
const CopyTargetDevice& t = dynamic_cast<const CopyTargetDevice&>(target);
|
|
|
|
|
|
|
|
if (device().deviceNode() != t.device().deviceNode())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// overlapping at the front?
|
|
|
|
if (firstSector() <= t.firstSector() && lastSector() >= t.firstSector())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// overlapping at the back?
|
|
|
|
if (firstSector() <= t.lastSector() && lastSector() >= t.lastSector())
|
|
|
|
return true;
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-06-04 01:29:22 +01:00
|
|
|
}
|