1 /*
2     SPDX-FileCopyrightText: 2008-2011 Volker Lanz <vl@fidra.de>
3     SPDX-FileCopyrightText: 2014-2016 Andrius Štikonas <andrius@stikonas.eu>
4 
5     SPDX-License-Identifier: GPL-3.0-or-later
6 */
7 
8 #include "ops/createfilesystemoperation.h"
9 
10 #include "core/partition.h"
11 #include "core/device.h"
12 
13 #include "jobs/deletefilesystemjob.h"
14 #include "jobs/createfilesystemjob.h"
15 #include "jobs/checkfilesystemjob.h"
16 
17 #include "fs/filesystem.h"
18 #include "fs/filesystemfactory.h"
19 
20 #include <QString>
21 
22 #include <KLocalizedString>
23 
24 /** Creates a new CreateFileSystemOperation.
25     @param d the Device to create the new FileSystem on
26     @param p the Partition to create the new FileSystem in
27     @param newType the type of the new FileSystem
28 */
CreateFileSystemOperation(Device & d,Partition & p,FileSystem::Type newType)29 CreateFileSystemOperation::CreateFileSystemOperation(Device& d, Partition& p, FileSystem::Type newType) :
30     Operation(),
31     m_TargetDevice(d),
32     m_Partition(p),
33     m_NewFileSystem(FileSystemFactory::cloneWithNewType(newType, partition().fileSystem())),
34     m_OldFileSystem(&p.fileSystem()),
35     m_DeleteJob(new DeleteFileSystemJob(targetDevice(), partition())),
36     m_CreateJob(new CreateFileSystemJob(targetDevice(), partition())),
37     m_CheckJob(new CheckFileSystemJob(partition()))
38 {
39     // We never know anything about the number of used sectors on a new file system.
40     newFileSystem()->setSectorsUsed(-1);
41 
42     addJob(deleteJob());
43     addJob(createJob());
44     addJob(checkJob());
45 }
46 
~CreateFileSystemOperation()47 CreateFileSystemOperation::~CreateFileSystemOperation()
48 {
49     if (&partition().fileSystem() == newFileSystem())
50         delete oldFileSystem();
51     else
52         delete newFileSystem();
53 }
54 
targets(const Device & d) const55 bool CreateFileSystemOperation::targets(const Device& d) const
56 {
57     return d == targetDevice();
58 }
59 
targets(const Partition & p) const60 bool CreateFileSystemOperation::targets(const Partition& p) const
61 {
62     return p == partition();
63 }
64 
preview()65 void CreateFileSystemOperation::preview()
66 {
67     partition().setFileSystem(newFileSystem());
68 }
69 
undo()70 void CreateFileSystemOperation::undo()
71 {
72     partition().setFileSystem(oldFileSystem());
73 }
74 
execute(Report & parent)75 bool CreateFileSystemOperation::execute(Report& parent)
76 {
77     preview();
78 
79     return Operation::execute(parent);
80 }
81 
description() const82 QString CreateFileSystemOperation::description() const
83 {
84     return xi18nc("@info:status", "Create filesystem %1 on partition <filename>%2</filename>", newFileSystem()->name(), partition().deviceNode());
85 }
86