source: calamares/trunk/fuentes/src/libcalamares/utils/CalamaresUtilsSystem.h @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 17 months ago

sync with github

File size: 9.2 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2017-2018, Adriaan de Groot <groot@kde.org>
5 *
6 *   Calamares is free software: you can redistribute it and/or modify
7 *   it under the terms of the GNU General Public License as published by
8 *   the Free Software Foundation, either version 3 of the License, or
9 *   (at your option) any later version.
10 *
11 *   Calamares is distributed in the hope that it will be useful,
12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 *   GNU General Public License for more details.
15 *
16 *   You should have received a copy of the GNU General Public License
17 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
18 */
19#ifndef CALAMARESUTILSSYSTEM_H
20#define CALAMARESUTILSSYSTEM_H
21
22#include "DllMacro.h"
23
24#include "Job.h"
25
26#include <QObject>
27#include <QPair>
28#include <QString>
29
30namespace CalamaresUtils
31{
32class ProcessResult : public QPair< int, QString >
33{
34public:
35    /** @brief Implicit one-argument constructor has no output, only a return code */
36    ProcessResult( int r ) : QPair< int, QString >( r, QString() ) {}
37    ProcessResult( int r, QString s ) : QPair< int, QString >( r, s ) {}
38
39    int getExitCode() const { return first; }
40    QString getOutput() const { return second; }
41
42    /** @brief Explain a typical external process failure.
43     *
44     * @param errorCode Return code from runCommand() or similar
45     *                  (negative values get special explanation). The member
46     *                  function uses the exit code stored in the ProcessResult
47     * @param output    (error) output from the command, used when there is
48     *                  an error to report (exit code > 0). The member
49     *                  function uses the output stored in the ProcessResult.
50     * @param command   String or split-up string of the command
51     *                  that was invoked.
52     * @param timeout   Timeout passed to the process runner, for explaining
53     *                  error code -4 (timeout).
54     */
55    static Calamares::JobResult explainProcess( int errorCode, const QString& command, const QString& output, int timeout );
56
57    /// @brief Convenience wrapper for explainProcess()
58    inline Calamares::JobResult explainProcess( const QString& command, int timeout ) const
59    {
60        return explainProcess( getExitCode(), command, getOutput(), timeout );
61    }
62
63    /// @brief Convenience wrapper for explainProcess()
64    inline Calamares::JobResult explainProcess( const QStringList& command, int timeout ) const
65    {
66        return explainProcess( getExitCode(), command.join( ' ' ), getOutput(), timeout );
67    }
68} ;
69
70/**
71 * @brief The System class is a singleton with utility functions that perform
72 * system-specific operations.
73 */
74class DLLEXPORT System : public QObject
75{
76    Q_OBJECT
77public:
78    /**
79     * @brief System the constructor. Only call this once in a Calamares instance.
80     * @param doChroot set to true if all external commands should run in the
81     * target system chroot, otherwise false to run everything on the current system.
82     * @param parent the QObject parent.
83     */
84    explicit System( bool doChroot, QObject* parent = nullptr );
85    virtual ~System();
86
87    static System* instance();
88
89    /**
90      * Runs the mount utility with the specified parameters.
91      * @param devicePath the path of the partition to mount.
92      * @param mountPoint the full path of the target mount point.
93      * @param filesystemName the name of the filesystem (optional).
94      * @param options any additional options as passed to mount -o (optional).
95      * @returns the program's exit code, or:
96      *             -1 = QProcess crash
97      *             -2 = QProcess cannot start
98      *             -3 = bad arguments
99      */
100    DLLEXPORT int mount( const QString& devicePath,
101                         const QString& mountPoint,
102                         const QString& filesystemName = QString(),
103                         const QString& options = QString() );
104
105
106    /** (Typed) Boolean describing where a particular command should be run,
107     *  whether in the host (live) system or in the (chroot) target system.
108     */
109    enum class RunLocation { RunInHost, RunInTarget };
110
111    /**
112      * Runs the specified command in the chroot of the target system.
113      * @param args the command with arguments, as a string list.
114      * @param workingPath the current working directory for the QProcess
115      *        call (optional).
116      * @param stdInput the input string to send to the running process as
117      *        standard input (optional).
118      * @param timeoutSec the timeout after which the process will be
119      *        killed (optional, default is 0 i.e. no timeout).
120      *
121      * @returns the program's exit code and its output (if any). Special
122      *     exit codes (which will never have any output) are:
123      *             -1 = QProcess crash
124      *             -2 = QProcess cannot start
125      *             -3 = bad arguments
126      *             -4 = QProcess timeout
127      */
128    static DLLEXPORT ProcessResult runCommand(
129        RunLocation location,
130        const QStringList &args,
131        const QString& workingPath = QString(),
132        const QString& stdInput = QString(),
133        int timeoutSec = 0 );
134
135    /** @brief Convenience wrapper for runCommand().
136     *  Runs the command in the location specified through the boolean
137     *  doChroot(), which is what you usually want for running commands
138     *  during installation.
139      */
140    inline ProcessResult targetEnvCommand(
141        const QStringList &args,
142        const QString& workingPath = QString(),
143        const QString& stdInput = QString(),
144        int timeoutSec = 0 )
145    {
146        return runCommand(
147            m_doChroot ? RunLocation::RunInTarget : RunLocation::RunInHost,
148            args,
149            workingPath,
150            stdInput,
151            timeoutSec );
152    }
153
154    /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */
155    inline int targetEnvCall( const QStringList& args,
156                              const QString& workingPath = QString(),
157                              const QString& stdInput = QString(),
158                              int timeoutSec = 0 )
159    {
160        return targetEnvCommand( args, workingPath, stdInput, timeoutSec ).first;
161    }
162
163    /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code */
164    inline int targetEnvCall( const QString& command,
165                              const QString& workingPath = QString(),
166                              const QString& stdInput = QString(),
167                              int timeoutSec = 0 )
168    {
169        return targetEnvCall( QStringList{ command }, workingPath, stdInput, timeoutSec );
170    }
171
172    /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code
173     *
174     * Places the called program's output in the @p output string.
175     */
176    int targetEnvOutput( const QStringList& args,
177                                QString& output,
178                                const QString& workingPath = QString(),
179                                const QString& stdInput = QString(),
180                                int timeoutSec = 0 )
181    {
182        auto r = targetEnvCommand( args, workingPath, stdInput, timeoutSec );
183        output = r.second;
184        return r.first;
185    }
186
187    /** @brief Convenience wrapper for targetEnvCommand() which returns only the exit code
188     *
189     * Places the called program's output in the @p output string.
190     */
191    inline int targetEnvOutput( const QString& command,
192                                QString& output,
193                                const QString& workingPath = QString(),
194                                const QString& stdInput = QString(),
195                                int timeoutSec = 0 )
196    {
197        return targetEnvOutput( QStringList{ command }, output, workingPath, stdInput, timeoutSec );
198    }
199
200    /**
201     * @brief getTotalMemoryB returns the total main memory, in bytes.
202     *
203     * Since it is difficult to get the RAM memory size exactly -- either
204     * by reading information from the DIMMs, which may fail on virtual hosts
205     * or from asking the kernel, which doesn't report some memory areas --
206     * this returns a pair of guessed-size (in bytes) and a "guesstimate factor"
207     * which says how good the guess is. Generally, assume the *real* memory
208     * available is size * guesstimate.
209     *
210     * If nothing can be found, returns a 0 size and 0 guesstimate.
211     *
212     * @return size, guesstimate-factor
213     */
214    DLLEXPORT QPair<quint64, float> getTotalMemoryB() const;
215
216    /**
217     * @brief getCpuDescription returns a string describing the CPU.
218     *
219     * Returns the value of the "model name" line in /proc/cpuinfo.
220     */
221    DLLEXPORT QString getCpuDescription() const;
222
223    /**
224     * @brief getTotalDiskB returns the total disk attached, in bytes.
225     *
226     * If nothing can be found, returns a 0.
227     */
228    DLLEXPORT quint64 getTotalDiskB() const;
229
230    DLLEXPORT bool doChroot() const;
231
232private:
233    static System* s_instance;
234
235    bool m_doChroot;
236};
237
238}
239
240#endif // CALAMARESUTILSSYSTEM_H
Note: See TracBrowser for help on using the repository browser.