source: calamares/trunk/fuentes/src/modules/partition/jobs/ClearTempMountsJob.cpp @ 7538

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

sync with github

File size: 3.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 "ClearTempMountsJob.h"
20
21#include "utils/Logger.h"
22
23#include <algorithm>
24
25// KPMcore
26#include <kpmcore/util/report.h>
27
28#include <QFile>
29#include <QProcess>
30#include <QStringList>
31
32
33ClearTempMountsJob::ClearTempMountsJob()
34    : Calamares::Job()
35{
36}
37
38
39QString
40ClearTempMountsJob::prettyName() const
41{
42    return tr( "Clear all temporary mounts." );
43}
44
45
46QString
47ClearTempMountsJob::prettyStatusMessage() const
48{
49    return tr( "Clearing all temporary mounts." );
50}
51
52
53Calamares::JobResult
54ClearTempMountsJob::exec()
55{
56    // Fetch a list of current mounts to Calamares temporary directories.
57    QList< QPair < QString, QString > > lst;
58    QFile mtab( "/etc/mtab" );
59    if ( !mtab.open( QFile::ReadOnly | QFile::Text ) )
60        return Calamares::JobResult::error( tr( "Cannot get list of temporary mounts." ) );
61
62    cDebug() << "Opened mtab. Lines:";
63    QTextStream in(&mtab);
64    QString lineIn = in.readLine();
65    while ( !lineIn.isNull() )
66    {
67        QStringList line = lineIn.split( ' ', QString::SkipEmptyParts );
68        cDebug() << line.join( ' ' );
69        QString device = line.at( 0 );
70        QString mountPoint = line.at( 1 );
71        if ( mountPoint.startsWith( "/tmp/calamares-" ) )
72        {
73            cDebug() << "INSERTING pair (device, mountPoint)" << device << mountPoint;
74            lst.append( qMakePair( device, mountPoint ) );
75        }
76        lineIn = in.readLine();
77    }
78
79    std::sort ( lst.begin(), lst.end(), []( const QPair< QString, QString >& a,
80                                       const QPair< QString, QString >& b ) -> bool
81    {
82        return a.first > b.first;
83    } );
84
85    QStringList goodNews;
86    QProcess process;
87
88    foreach ( auto line, lst )
89    {
90        QString partPath = line.second;
91        cDebug() << "Will try to umount path" << partPath;
92        process.start( "umount", { "-lv", partPath } );
93        process.waitForFinished();
94        if ( process.exitCode() == 0 )
95            goodNews.append( QString( "Successfully unmounted %1." ).arg( partPath ) );
96    }
97
98    Calamares::JobResult ok = Calamares::JobResult::ok();
99    ok.setMessage( tr( "Cleared all temporary mounts." ) );
100    ok.setDetails( goodNews.join( "\n" ) );
101
102    cDebug() << "ClearTempMountsJob finished. Here's what was done:\n" << goodNews.join( "\n" );
103
104    return ok;
105}
Note: See TracBrowser for help on using the repository browser.