2020-09-29 23:55:11 +01:00
/*
SPDX - FileCopyrightText : 2008 - 2010 Volker Lanz < vl @ fidra . de >
SPDX - FileCopyrightText : 2014 - 2018 Andrius Š tikonas < andrius @ stikonas . eu >
SPDX - License - Identifier : GPL - 3.0 - or - later
*/
2015-06-04 01:29:22 +01:00
# include "jobs/setfilesystemlabeljob.h"
2018-04-13 13:54:51 +01:00
# include "backend/corebackend.h"
# include "backend/corebackendmanager.h"
# include "backend/corebackenddevice.h"
# include "backend/corebackendpartitiontable.h"
# include "core/device_p.h"
# include "core/operationstack.h"
2015-06-04 01:29:22 +01:00
# include "core/partition.h"
# include "fs/filesystem.h"
# include "util/report.h"
# include <KLocalizedString>
2018-04-13 13:54:51 +01:00
# include <memory>
2015-06-04 01:29:22 +01:00
/** Creates a new SetFileSystemLabelJob
2015-07-13 15:16:36 +01:00
@ param p the Partition the FileSystem whose label is to be set is on
@ param newlabel the new label
2015-06-04 01:29:22 +01:00
*/
SetFileSystemLabelJob : : SetFileSystemLabelJob ( Partition & p , const QString & newlabel ) :
2015-07-13 15:16:36 +01:00
Job ( ) ,
m_Partition ( p ) ,
m_Label ( newlabel )
2015-06-04 01:29:22 +01:00
{
}
bool SetFileSystemLabelJob : : run ( Report & parent )
{
2015-07-13 15:16:36 +01:00
bool rval = true ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
Report * report = jobStarted ( parent ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
// If there's no support for file system label setting for this file system,
// just ignore the request and say all is well. This helps in operations because
// we don't have to check for support to avoid having a failed job.
if ( partition ( ) . fileSystem ( ) . supportSetLabel ( ) = = FileSystem : : cmdSupportNone )
2016-07-17 23:41:00 +01:00
report - > line ( ) < < xi18nc ( " @info:progress " , " File system on partition <filename>%1</filename> does not support setting labels. Job ignored. " , partition ( ) . deviceNode ( ) ) ;
2017-04-09 17:49:55 +01:00
else if ( partition ( ) . fileSystem ( ) . supportSetLabel ( ) = = FileSystem : : cmdSupportFileSystem & & ! partition ( ) . isMounted ( ) ) {
2015-07-13 15:16:36 +01:00
rval = partition ( ) . fileSystem ( ) . writeLabel ( * report , partition ( ) . deviceNode ( ) , label ( ) ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
if ( rval )
partition ( ) . fileSystem ( ) . setLabel ( label ( ) ) ;
}
2017-04-09 17:49:55 +01:00
else if ( partition ( ) . fileSystem ( ) . supportSetLabelOnline ( ) = = FileSystem : : cmdSupportFileSystem & & partition ( ) . isMounted ( ) ) {
rval = partition ( ) . fileSystem ( ) . writeLabelOnline ( * report , partition ( ) . deviceNode ( ) , partition ( ) . mountPoint ( ) , label ( ) ) ;
if ( rval )
partition ( ) . fileSystem ( ) . setLabel ( label ( ) ) ;
}
2018-04-13 18:21:48 +01:00
else
rval = false ;
2015-06-04 01:29:22 +01:00
2018-04-13 13:54:51 +01:00
// A hack to reread partition table (commit() should be called even on non DiskDevices)
Device dev ( std : : make_shared < DevicePrivate > ( ) , QString ( ) , QString ( ) , 0 , 0 , QString ( ) , Device : : Type : : Unknown_Device ) ;
std : : unique_ptr < CoreBackendDevice > backendDevice = CoreBackendManager : : self ( ) - > backend ( ) - > openDevice ( dev ) ;
if ( backendDevice ) {
std : : unique_ptr < CoreBackendPartitionTable > backendPartitionTable = backendDevice - > openPartitionTable ( ) ;
if ( backendPartitionTable )
backendPartitionTable - > commit ( ) ;
}
2018-04-13 11:08:43 +01:00
2015-07-13 15:16:36 +01:00
jobFinished ( * report , rval ) ;
2015-06-04 01:29:22 +01:00
2015-07-13 15:16:36 +01:00
return rval ;
2015-06-04 01:29:22 +01:00
}
QString SetFileSystemLabelJob : : description ( ) const
{
2016-07-17 23:41:00 +01:00
return xi18nc ( " @info:progress " , " Set the file system label on partition <filename>%1</filename> to \" %2 \" " , partition ( ) . deviceNode ( ) , label ( ) ) ;
2015-06-04 01:29:22 +01:00
}