diff --git a/README.md b/README.md index 3e82d9c..2fd29bd 100644 --- a/README.md +++ b/README.md @@ -1,102 +1,102 @@ # KPMcore > KPMcore, the KDE Partition Manager core, is a library for examining > and modifying partitions, disk devices, and filesystems on a > Linux system. It provides a unified programming interface over > top of (external) system-manipulation tools. KPMcore is a library for examining and manipulating all facets of storage devices on a system: * raw disk devices * partition tables on a device * filesystems within a partition There are multiple backends so that KPMcore can support different operating systems, although the only functional backend is the one for Linux systems: * libparted backend (Linux) * null backend ## Using KPMcore Most of the usage information on KPMcore is included in the API documentation; this section contains only high-level usage information. ### Finding KPMcore with CMake KPMcore supports CMake as (meta-)build system and installs suitable CMake support files. Typical use of of KPMcore in a `CMakeLists.txt` looks like this: ``` find_package( KPMcore 3.2 REQUIRED ) include_directories( ${KPMCORE_INCLUDE_DIR} ) target_link_libraries( target kpmcore ) ``` There are no imported targets defined for KPMcore. ### Initialization An application must initialize the library and load a suitable backend before using KPMcore functions. By convention, the environment variable `KPMCORE_BACKEND` names a backend, and typical initialization code will look like this (or use the class `KPMCoreInitializer` from `test/helpers.h`): ``` #include #include bool initKPMcore() { static bool inited = false; if ( inited ) return true; QByteArray env = qgetenv( "KPMCORE_BACKEND" ); auto backendName = env.isEmpty() ? CoreBackendManager::defaultBackendName() : env; if ( !CoreBackendManager::self()->load( backendName ) { qWarning() << "Failed to load backend plugin" << backendName; return false; } inited = true; return true; } ``` This code uses the environment variable if set, and otherwise falls back to a default backend suitable for the current platform. Calling KPMcore functions before the library is initialized will result in undefined behavior. ### Devices [FIXME: WIP] #### Backend device scanner After the backend is initialized you can scan for available devices. If you only want devices from the loaded backend you can call ``` QList devices = backend->scanDevices( excludeReadOnly ); ``` -where bool option `excludeReadOnly` specifies whether to exclude +where `bool` option `excludeReadOnly` specifies whether to exclude read only devices. #### KPMcore device scanner Alternatively, you can use KPMcore device scanner which also finds LVM Volume Groups. ``` // First create operationStack with another QObject as parent. OperationStack *operationStack = new OperationStack(parent); DeviceScanner *deviceScanner = new DeviceScanner(parent, *operationStack)); deviceScanner->start(); ``` The `deviceScanner` scans for the devices in a background thread. After scanning is complete `DeviceScanner::finished()` signal will be emitted. Then the devices can accessed using `operationStack->previewDevices()`. diff --git a/src/util/externalcommand.cpp b/src/util/externalcommand.cpp index 6e3607a..b38b1f2 100644 --- a/src/util/externalcommand.cpp +++ b/src/util/externalcommand.cpp @@ -1,137 +1,137 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * Copyright (C) 2016 by Andrius Štikonas * * * * 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 .* *************************************************************************/ #include "util/externalcommand.h" #include "util/report.h" #include #include #include /** Creates a new ExternalCommand instance without Report. @param cmd the command to run @param args the arguments to pass to the command */ ExternalCommand::ExternalCommand(const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : QProcess(), m_Report(nullptr), m_Command(cmd), m_Args(args), m_ExitCode(-1), m_Output() { setup(processChannelMode); } /** Creates a new ExternalCommand instance with Report. @param report the Report to write output to. @param cmd the command to run @param args the arguments to pass to the command */ ExternalCommand::ExternalCommand(Report& report, const QString& cmd, const QStringList& args, const QProcess::ProcessChannelMode processChannelMode) : QProcess(), m_Report(report.newChild()), m_Command(cmd), m_Args(args), m_ExitCode(-1), m_Output() { setup(processChannelMode); } void ExternalCommand::setup(const QProcess::ProcessChannelMode processChannelMode) { setEnvironment(QStringList() << QStringLiteral("LC_ALL=C") << QStringLiteral("PATH=") + QString::fromUtf8(getenv("PATH")) << QStringLiteral("LVM_SUPPRESS_FD_WARNINGS=1")); setProcessChannelMode(processChannelMode); connect(this, static_cast(&QProcess::finished), this, &ExternalCommand::onFinished); connect(this, &ExternalCommand::readyReadStandardOutput, this, &ExternalCommand::onReadOutput); } /** Starts the external command. @param timeout timeout to wait for the process to start @return true on success */ bool ExternalCommand::start(int timeout) { QProcess::start(command(), args()); if (report()) { report()->setCommand(xi18nc("@info:status", "Command: %1 %2", command(), args().join(QStringLiteral(" ")))); } if (!waitForStarted(timeout)) { if (report()) report()->line() << xi18nc("@info:status", "(Command timeout while starting)"); return false; } return true; } /** Waits for the external command to finish. @param timeout timeout to wait until the process finishes. @return true on success */ bool ExternalCommand::waitFor(int timeout) { closeWriteChannel(); if (!waitForFinished(timeout)) { if (report()) report()->line() << xi18nc("@info:status", "(Command timeout while running)"); return false; } onReadOutput(); return true; } /** Runs the command. @param timeout timeout to use for waiting when starting and when waiting for the process to finish @return true on success */ bool ExternalCommand::run(int timeout) { return start(timeout) && waitFor(timeout) && exitStatus() == 0; } void ExternalCommand::onReadOutput() { - const QString s = QString::fromUtf8(readAllStandardOutput()); + const QByteArray s = readAllStandardOutput(); if(m_Output.length() > 10*1024*1024) { // prevent memory overflow for badly corrupted file systems if (report()) report()->line() << xi18nc("@info:status", "(Command is printing too much output)"); return; } m_Output += s; if (report()) - *report() << s; + *report() << QString::fromLocal8Bit(s); } void ExternalCommand::onFinished(int exitCode, QProcess::ExitStatus exitStatus) { Q_UNUSED(exitStatus) setExitCode(exitCode); } diff --git a/src/util/externalcommand.h b/src/util/externalcommand.h index f9a665f..f854ee0 100644 --- a/src/util/externalcommand.h +++ b/src/util/externalcommand.h @@ -1,89 +1,93 @@ /************************************************************************* * Copyright (C) 2008 by Volker Lanz * * * * 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 .* *************************************************************************/ #if !defined(KPMCORE_EXTERNALCOMMAND_H) #define KPMCORE_EXTERNALCOMMAND_H #include "util/libpartitionmanagerexport.h" #include #include #include #include #include class Report; /** An external command. Runs an external command as a child process. @author Volker Lanz @author Andrius Štikonas */ class LIBKPMCORE_EXPORT ExternalCommand : public QProcess { Q_DISABLE_COPY(ExternalCommand) public: explicit ExternalCommand(const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = MergedChannels); explicit ExternalCommand(Report& report, const QString& cmd = QString(), const QStringList& args = QStringList(), const QProcess::ProcessChannelMode processChannelMode = MergedChannels); public: void setCommand(const QString& cmd) { m_Command = cmd; } /**< @param cmd the command to run */ const QString& command() const { return m_Command; } /**< @return the command to run */ void addArg(const QString& s) { m_Args << s; } /**< @param s the argument to add */ const QStringList& args() const { return m_Args; } /**< @return the arguments */ void setArgs(const QStringList& args) { m_Args = args; } /**< @param args the new arguments */ bool start(int timeout = 30000); bool waitFor(int timeout = 30000); bool run(int timeout = 30000); int exitCode() const { return m_ExitCode; /**< @return the exit code */ } - const QString& output() const { + const QString output() const { + return QString::fromLocal8Bit(m_Output); /**< @return the command output */ + } + + const QByteArray& rawOutput() const { return m_Output; /**< @return the command output */ } Report* report() { return m_Report; /**< @return pointer to the Report or nullptr */ } protected: void setExitCode(int i) { m_ExitCode = i; } void setup(const QProcess::ProcessChannelMode processChannelMode); void onFinished(int exitCode, QProcess::ExitStatus exitStatus); void onReadOutput(); private: Report *m_Report; QString m_Command; QStringList m_Args; int m_ExitCode; - QString m_Output; + QByteArray m_Output; }; #endif