source: calamares/trunk/fuentes/src/libcalamares/ProcessJob.cpp @ 7538

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

sync with github

File size: 4.0 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "ProcessJob.h"
20
21#include "utils/CalamaresUtilsSystem.h"
22#include "utils/Logger.h"
23
24#include <QDir>
25#include <QProcess>
26
27namespace Calamares {
28
29
30ProcessJob::ProcessJob( const QString& command,
31                        const QString& workingPath,
32                        bool runInChroot,
33                        int secondsTimeout,
34                        QObject* parent )
35    : Job( parent )
36    , m_command( command )
37    , m_workingPath( workingPath )
38    , m_runInChroot( runInChroot )
39    , m_timeoutSec( secondsTimeout )
40{}
41
42
43ProcessJob::~ProcessJob()
44{}
45
46
47QString
48ProcessJob::prettyName() const
49{
50    //TODO: show something more meaningful
51    return tr( "Run command %1 %2" )
52            .arg( m_command )
53            .arg( m_runInChroot ? "in chroot." : " ." );
54}
55
56
57QString
58ProcessJob::prettyStatusMessage() const
59{
60    return tr( "Running command %1 %2" )
61            .arg( m_command )
62            .arg( m_runInChroot ? "in chroot." : " ." );
63}
64
65
66JobResult
67ProcessJob::exec()
68{
69    int ec = 0;
70    QString output;
71    if ( m_runInChroot )
72        ec = CalamaresUtils::System::instance()->
73             targetEnvOutput( m_command,
74                              output,
75                              m_workingPath,
76                              QString(),
77                              m_timeoutSec );
78    else
79        ec = callOutput( m_command,
80                         output,
81                         m_workingPath,
82                         QString(),
83                         m_timeoutSec );
84
85    return CalamaresUtils::ProcessResult::explainProcess( ec, m_command, output, m_timeoutSec );
86}
87
88
89int
90ProcessJob::callOutput( const QString& command,
91                        QString& output,
92                        const QString& workingPath,
93                        const QString& stdInput,
94                        int timeoutSec )
95{
96    output.clear();
97
98    QProcess process;
99    process.setProgram( "/bin/sh" );
100    process.setArguments( { "-c", command } );
101    process.setProcessChannelMode( QProcess::MergedChannels );
102
103    if ( !workingPath.isEmpty() )
104    {
105        if ( QDir( workingPath ).exists() )
106            process.setWorkingDirectory( QDir( workingPath ).absolutePath() );
107        else
108        {
109            cWarning() << "Invalid working directory:" << workingPath;
110            return -3;
111        }
112    }
113
114    cDebug() << "Running" << command;
115    process.start();
116    if ( !process.waitForStarted() )
117    {
118        cWarning() << "Process failed to start" << process.error();
119        return -2;
120    }
121
122    if ( !stdInput.isEmpty() )
123    {
124        process.write( stdInput.toLocal8Bit() );
125        process.closeWriteChannel();
126    }
127
128    if ( !process.waitForFinished( timeoutSec ? ( timeoutSec * 1000 ) : -1 ) )
129    {
130        cWarning() << "Timed out. output so far:";
131        output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() );
132        cWarning() << output;
133        return -4;
134    }
135
136    output.append( QString::fromLocal8Bit( process.readAllStandardOutput() ).trimmed() );
137
138    if ( process.exitStatus() == QProcess::CrashExit )
139    {
140        cWarning() << "Process crashed";
141        return -1;
142    }
143
144    cDebug() << "Finished. Exit code:" << process.exitCode();
145    return process.exitCode();
146}
147
148} // namespace Calamares
Note: See TracBrowser for help on using the repository browser.