File indexing completed on 2024-05-12 05:48:28

0001 /*
0002     SPDX-FileCopyrightText: 2017-2018 Andrius Štikonas <andrius@stikonas.eu>
0003 
0004     SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "plugins/sfdisk/sfdiskdevice.h"
0008 #include "plugins/sfdisk/sfdiskpartitiontable.h"
0009 
0010 #include "core/partitiontable.h"
0011 
0012 #include "util/externalcommand.h"
0013 #include "util/report.h"
0014 
0015 SfdiskDevice::SfdiskDevice(const Device& d) :
0016     CoreBackendDevice(d.deviceNode()),
0017     m_device(&d)
0018 {
0019 }
0020 
0021 SfdiskDevice::~SfdiskDevice()
0022 {
0023     close();
0024 }
0025 
0026 bool SfdiskDevice::open()
0027 {
0028     return true;
0029 }
0030 
0031 bool SfdiskDevice::openExclusive()
0032 {
0033     setExclusive(true);
0034 
0035     return true;
0036 }
0037 
0038 bool SfdiskDevice::close()
0039 {
0040     if (isExclusive())
0041         setExclusive(false);
0042 
0043     CoreBackendPartitionTable* ptable = new SfdiskPartitionTable(m_device);
0044     ptable->commit();
0045     delete ptable;
0046 
0047     return true;
0048 }
0049 
0050 std::unique_ptr<CoreBackendPartitionTable> SfdiskDevice::openPartitionTable()
0051 {
0052     return std::make_unique<SfdiskPartitionTable>(m_device);
0053 }
0054 
0055 bool SfdiskDevice::createPartitionTable(Report& report, const PartitionTable& ptable)
0056 {
0057     QByteArray tableType;
0058     if (ptable.type() == PartitionTable::msdos || ptable.type() == PartitionTable::msdos_sectorbased)
0059         tableType = QByteArrayLiteral("dos");
0060     else
0061         tableType = ptable.typeName().toLocal8Bit();
0062 
0063     ExternalCommand createCommand(report, QStringLiteral("sfdisk"), { QStringLiteral("--wipe=always"), m_device->deviceNode() } );
0064     if ( createCommand.write(QByteArrayLiteral("label: ") + tableType +
0065                                     QByteArrayLiteral("\nwrite\n")) && createCommand.start(-1) ) {
0066         return createCommand.output().contains(QStringLiteral("Script header accepted."));
0067     }
0068 
0069     return false;
0070 }