source: calamares/trunk/fuentes/src/modules/locale/SetTimezoneJob.cpp @ 7538

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

sync with github

File size: 3.8 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014, Teo Mrnjavac <teo@kde.org>
4 *   Copyright 2015, Rohan Garg <rohan@garg.io>
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
20#include <SetTimezoneJob.h>
21
22#include "JobQueue.h"
23#include "GlobalStorage.h"
24#include "Settings.h"
25#include "utils/Logger.h"
26#include "utils/CalamaresUtilsSystem.h"
27
28#include <QDir>
29#include <QFileInfo>
30
31
32SetTimezoneJob::SetTimezoneJob( const QString& region, const QString& zone )
33    : Calamares::Job()
34    , m_region( region )
35    , m_zone( zone )
36{
37}
38
39
40QString
41SetTimezoneJob::prettyName() const
42{
43    return tr( "Set timezone to %1/%2" ).arg( m_region ).arg( m_zone );
44}
45
46
47Calamares::JobResult
48SetTimezoneJob::exec()
49{
50    // do not call timedatectl in a chroot, it is not safe (timedatectl talks
51    // to a running timedated over D-Bus), and we have code that works
52    if ( !Calamares::Settings::instance()->doChroot() )
53    {
54        int ec = CalamaresUtils::System::instance()->
55                              targetEnvCall( { "timedatectl",
56                                               "set-timezone",
57                                               m_region + '/' + m_zone } );
58
59        if ( !ec )
60            return Calamares::JobResult::ok();
61    }
62
63    QString localtimeSlink( "/etc/localtime" );
64    QString zoneinfoPath( "/usr/share/zoneinfo" );
65    zoneinfoPath.append( QDir::separator() + m_region );
66    zoneinfoPath.append( QDir::separator() + m_zone );
67
68    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
69    QFileInfo zoneFile( gs->value( "rootMountPoint" ).toString() + zoneinfoPath );
70    if ( !zoneFile.exists() || !zoneFile.isReadable() )
71        return Calamares::JobResult::error( tr( "Cannot access selected timezone path." ),
72                                            tr( "Bad path: %1" ).arg( zoneFile.absolutePath() ) );
73
74    // Make sure /etc/localtime doesn't exist, otherwise symlinking will fail
75    CalamaresUtils::System::instance()->
76                 targetEnvCall( { "rm",
77                                  "-f",
78                                  localtimeSlink } );
79
80    int ec = CalamaresUtils::System::instance()->
81                          targetEnvCall( { "ln",
82                                           "-s",
83                                           zoneinfoPath,
84                                           localtimeSlink } );
85    if ( ec )
86        return Calamares::JobResult::error( tr( "Cannot set timezone." ),
87                                            tr( "Link creation failed, target: %1; link name: %2" )
88                                                .arg( zoneinfoPath )
89                                                .arg( "/etc/localtime" ) );
90
91    QFile timezoneFile( gs->value( "rootMountPoint" ).toString() + "/etc/timezone" );
92
93    if ( !timezoneFile.open( QIODevice::WriteOnly |
94                             QIODevice::Text |
95                             QIODevice::Truncate ) )
96        return Calamares::JobResult::error( tr( "Cannot set timezone,"),
97                                            tr( "Cannot open /etc/timezone for writing"));
98
99    QTextStream out(&timezoneFile);
100    out << m_region << '/' << m_zone << "\n";
101    timezoneFile.close();
102
103    return Calamares::JobResult::ok();
104}
Note: See TracBrowser for help on using the repository browser.