From 409ab6ee868b22f8e42083ce668876d3f5f313e8 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 22 Jun 2020 17:10:03 +0200 Subject: [PATCH] [users] Refactor writing sudoers file - use existing convenience methods --- src/modules/users/CreateUserJob.cpp | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/modules/users/CreateUserJob.cpp b/src/modules/users/CreateUserJob.cpp index 349d6c295..f0b1dca88 100644 --- a/src/modules/users/CreateUserJob.cpp +++ b/src/modules/users/CreateUserJob.cpp @@ -95,26 +95,23 @@ CreateUserJob::exec() { cDebug() << "[CREATEUSER]: preparing sudoers"; - QFileInfo sudoersFi( destDir.absoluteFilePath( "etc/sudoers.d/10-installer" ) ); + QString sudoersLine = QString( "%%1 ALL=(ALL) ALL\n" ).arg( gs->value( "sudoersGroup" ).toString() ); + auto fileResult + = CalamaresUtils::System::instance()->createTargetFile( QStringLiteral( "/etc/sudoers.d/10-installer" ), + sudoersLine.toUtf8().constData(), + CalamaresUtils::System::WriteMode::Overwrite ); - if ( !sudoersFi.absoluteDir().exists() ) + if ( fileResult ) { - return Calamares::JobResult::error( tr( "Sudoers dir is not writable." ) ); + if ( QProcess::execute( "chmod", { "440", fileResult.path() } ) ) + { + return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) ); + } } - - QFile sudoersFile( sudoersFi.absoluteFilePath() ); - if ( !sudoersFile.open( QIODevice::WriteOnly | QIODevice::Text ) ) + else { return Calamares::JobResult::error( tr( "Cannot create sudoers file for writing." ) ); } - - QString sudoersGroup = gs->value( "sudoersGroup" ).toString(); - - QTextStream sudoersOut( &sudoersFile ); - sudoersOut << QString( "%%1 ALL=(ALL) ALL\n" ).arg( sudoersGroup ); - - if ( QProcess::execute( "chmod", { "440", sudoersFi.absoluteFilePath() } ) ) - return Calamares::JobResult::error( tr( "Cannot chmod sudoers file." ) ); } cDebug() << "[CREATEUSER]: preparing groups";