2006-03-24 12:08:41 -07:00
|
|
|
/* Copyright (C) 2004 Bart 'plors' Hakvoort
|
2010-03-23 13:06:26 -06:00
|
|
|
* Copyright (C) 2010 Curtis Gedak
|
2006-03-24 12:08:41 -07:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-01-23 03:59:48 -07:00
|
|
|
* GNU General Public License for more details.
|
2006-03-24 12:08:41 -07:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2014-01-23 03:59:48 -07:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2006-03-24 12:08:41 -07:00
|
|
|
*/
|
|
|
|
|
2016-10-18 16:45:28 -06:00
|
|
|
#include "OperationDelete.h"
|
|
|
|
#include "Partition.h"
|
|
|
|
#include "PartitionVector.h"
|
2006-03-24 12:08:41 -07:00
|
|
|
|
|
|
|
namespace GParted
|
|
|
|
{
|
|
|
|
|
|
|
|
OperationDelete::OperationDelete( const Device & device, const Partition & partition_orig )
|
|
|
|
{
|
2006-07-29 09:20:32 -06:00
|
|
|
type = OPERATION_DELETE ;
|
2006-03-24 12:08:41 -07:00
|
|
|
|
2015-05-24 07:13:37 -06:00
|
|
|
this->device = device.get_copy_without_partitions();
|
2015-12-05 09:26:30 -07:00
|
|
|
this->partition_original = partition_orig.clone();
|
2015-09-20 03:50:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
OperationDelete::~OperationDelete()
|
|
|
|
{
|
|
|
|
delete partition_original;
|
C++11: Convert NULL to nullptr (!117)
In C++11, nullptr [1] is the strongly typed value to use instead of the
macro NULL [2]. Use everywhere [3][4].
[1] nullptr, the pointer literal (since C++11)
https://en.cppreference.com/w/cpp/language/nullptr
[2] NULL
https://en.cppreference.com/w/cpp/types/NULL
[3] Bjarne Stroustrup's C++ Style and Technique FAQ, Should I use NULL
or 0?
https://www.stroustrup.com/bs_faq2.html#null
"In C++, the definition of NULL is 0, so there is only an
aesthetic difference. I prefer to avoid macros, so I use 0.
Another problem with NULL is that people sometimes mistakenly
believe that it is different from 0 and/or not an integer. In
pre-standard code, NULL was/is sometimes defined to something
unsuitable and therefore had/has to be avoided. That's less
common these days.
If you have to name the null pointer, call it nullptr; that's
what it's called in C++11. Then, "nullptr" will be a keyword.
"
[4] What is nullptr in C++? Advantages, Use Cases & Examples
https://favtutor.com/blogs/nullptr-cpp
"Advantages of nullptr
...
Compatible: Null pointers are compatible with null pointer
constants in the C style (such as NULL and 0). This implies
that old C code that uses these constants and null pointers can
communicate with each other in C++.
"
Closes !117 - Require C++11 compilation
2023-08-31 14:08:59 -06:00
|
|
|
partition_original = nullptr;
|
2006-03-24 12:08:41 -07:00
|
|
|
}
|
2015-05-23 13:22:37 -06:00
|
|
|
|
2015-09-19 08:57:39 -06:00
|
|
|
Partition & OperationDelete::get_partition_new()
|
|
|
|
{
|
|
|
|
g_assert( false ); // Bug: OperationDelete class doesn't use partition_new
|
|
|
|
|
|
|
|
// Not reached. Return value to keep compiler quiet.
|
2015-09-20 03:50:57 -06:00
|
|
|
return *partition_new;
|
2015-09-19 08:57:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const Partition & OperationDelete::get_partition_new() const
|
|
|
|
{
|
|
|
|
g_assert( false ); // Bug: OperationDelete class doesn't use partition_new
|
|
|
|
|
|
|
|
// Not reached. Return value to keep compiler quiet.
|
2015-09-20 03:50:57 -06:00
|
|
|
return *partition_new;
|
2015-09-19 08:57:39 -06:00
|
|
|
}
|
|
|
|
|
2015-05-23 13:22:37 -06:00
|
|
|
void OperationDelete::apply_to_visual( PartitionVector & partitions )
|
2006-03-24 12:08:41 -07:00
|
|
|
{
|
C++11: Convert NULL to nullptr (!117)
In C++11, nullptr [1] is the strongly typed value to use instead of the
macro NULL [2]. Use everywhere [3][4].
[1] nullptr, the pointer literal (since C++11)
https://en.cppreference.com/w/cpp/language/nullptr
[2] NULL
https://en.cppreference.com/w/cpp/types/NULL
[3] Bjarne Stroustrup's C++ Style and Technique FAQ, Should I use NULL
or 0?
https://www.stroustrup.com/bs_faq2.html#null
"In C++, the definition of NULL is 0, so there is only an
aesthetic difference. I prefer to avoid macros, so I use 0.
Another problem with NULL is that people sometimes mistakenly
believe that it is different from 0 and/or not an integer. In
pre-standard code, NULL was/is sometimes defined to something
unsuitable and therefore had/has to be avoided. That's less
common these days.
If you have to name the null pointer, call it nullptr; that's
what it's called in C++11. Then, "nullptr" will be a keyword.
"
[4] What is nullptr in C++? Advantages, Use Cases & Examples
https://favtutor.com/blogs/nullptr-cpp
"Advantages of nullptr
...
Compatible: Null pointers are compatible with null pointer
constants in the C style (such as NULL and 0). This implies
that old C code that uses these constants and null pointers can
communicate with each other in C++.
"
Closes !117 - Require C++11 compilation
2023-08-31 14:08:59 -06:00
|
|
|
g_assert(partition_original != nullptr); // Bug: Not initialised by constructor or reset later
|
2015-09-20 03:50:57 -06:00
|
|
|
|
2015-09-27 02:30:13 -06:00
|
|
|
int index_extended;
|
|
|
|
int index;
|
|
|
|
|
2015-09-20 03:50:57 -06:00
|
|
|
if ( partition_original->inside_extended )
|
2006-03-24 12:08:41 -07:00
|
|
|
{
|
2016-08-21 04:56:51 -06:00
|
|
|
index_extended = find_extended_partition( partitions );
|
2006-03-24 12:08:41 -07:00
|
|
|
if ( index_extended >= 0 )
|
2015-10-07 14:25:28 -06:00
|
|
|
{
|
2006-03-24 12:08:41 -07:00
|
|
|
index = find_index_original( partitions[ index_extended ] .logicals ) ;
|
|
|
|
|
2015-10-07 14:25:28 -06:00
|
|
|
if ( index >= 0 )
|
|
|
|
{
|
|
|
|
remove_original_and_adjacent_unallocated( partitions[index_extended].logicals, index );
|
2006-03-24 12:08:41 -07:00
|
|
|
|
2015-10-07 14:25:28 -06:00
|
|
|
insert_unallocated( partitions[index_extended].logicals,
|
|
|
|
partitions[index_extended].sector_start,
|
|
|
|
partitions[index_extended].sector_end,
|
|
|
|
device.sector_size,
|
|
|
|
true );
|
|
|
|
|
|
|
|
// If deleted partition was logical we have to decrease
|
|
|
|
// the partition numbers of the logicals with higher
|
|
|
|
// numbers by one (only if its a real partition)
|
2015-09-20 03:50:57 -06:00
|
|
|
if ( partition_original->status != STAT_NEW )
|
2015-10-07 14:25:28 -06:00
|
|
|
for ( unsigned int t = 0 ; t < partitions[index_extended].logicals .size() ; t++ )
|
|
|
|
if ( partitions[index_extended].logicals[t].partition_number >
|
2015-09-20 03:50:57 -06:00
|
|
|
partition_original->partition_number )
|
2015-10-07 14:25:28 -06:00
|
|
|
partitions[index_extended].logicals[t].Update_Number(
|
|
|
|
partitions[index_extended].logicals[t].partition_number -1 );
|
|
|
|
}
|
2006-03-24 12:08:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
index = find_index_original( partitions ) ;
|
|
|
|
|
|
|
|
if ( index >= 0 )
|
|
|
|
{
|
|
|
|
remove_original_and_adjacent_unallocated( partitions, index ) ;
|
|
|
|
|
2010-04-19 19:22:31 -06:00
|
|
|
insert_unallocated( partitions, 0, device .length -1, device .sector_size, false ) ;
|
2006-03-24 12:08:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OperationDelete::create_description()
|
|
|
|
{
|
C++11: Convert NULL to nullptr (!117)
In C++11, nullptr [1] is the strongly typed value to use instead of the
macro NULL [2]. Use everywhere [3][4].
[1] nullptr, the pointer literal (since C++11)
https://en.cppreference.com/w/cpp/language/nullptr
[2] NULL
https://en.cppreference.com/w/cpp/types/NULL
[3] Bjarne Stroustrup's C++ Style and Technique FAQ, Should I use NULL
or 0?
https://www.stroustrup.com/bs_faq2.html#null
"In C++, the definition of NULL is 0, so there is only an
aesthetic difference. I prefer to avoid macros, so I use 0.
Another problem with NULL is that people sometimes mistakenly
believe that it is different from 0 and/or not an integer. In
pre-standard code, NULL was/is sometimes defined to something
unsuitable and therefore had/has to be avoided. That's less
common these days.
If you have to name the null pointer, call it nullptr; that's
what it's called in C++11. Then, "nullptr" will be a keyword.
"
[4] What is nullptr in C++? Advantages, Use Cases & Examples
https://favtutor.com/blogs/nullptr-cpp
"Advantages of nullptr
...
Compatible: Null pointers are compatible with null pointer
constants in the C style (such as NULL and 0). This implies
that old C code that uses these constants and null pointers can
communicate with each other in C++.
"
Closes !117 - Require C++11 compilation
2023-08-31 14:08:59 -06:00
|
|
|
g_assert(partition_original != nullptr); // Bug: Not initialised by constructor or reset later
|
2015-09-20 03:50:57 -06:00
|
|
|
|
|
|
|
if ( partition_original->type == TYPE_LOGICAL )
|
2006-03-24 12:08:41 -07:00
|
|
|
description = _("Logical Partition") ;
|
|
|
|
else
|
2015-09-20 03:50:57 -06:00
|
|
|
description = partition_original->get_path();
|
2006-03-24 12:08:41 -07:00
|
|
|
|
|
|
|
/*TO TRANSLATORS: looks like Delete /dev/hda2 (ntfs, 345 MiB) from /dev/hda */
|
2019-03-25 14:41:08 -06:00
|
|
|
description = Glib::ustring::compose( _("Delete %1 (%2, %3) from %4"),
|
2015-09-20 03:50:57 -06:00
|
|
|
description,
|
2016-12-15 08:03:18 -07:00
|
|
|
partition_original->get_filesystem_string(),
|
2015-09-20 03:50:57 -06:00
|
|
|
Utils::format_size( partition_original->get_sector_length(),
|
|
|
|
partition_original->sector_size ),
|
|
|
|
partition_original->device_path );
|
2006-03-24 12:08:41 -07:00
|
|
|
}
|
2015-09-12 07:59:40 -06:00
|
|
|
|
|
|
|
bool OperationDelete::merge_operations( const Operation & candidate )
|
|
|
|
{
|
|
|
|
return false; // Can't merge with an already deleted partition
|
|
|
|
}
|
|
|
|
|
2015-05-23 13:22:37 -06:00
|
|
|
void OperationDelete::remove_original_and_adjacent_unallocated( PartitionVector & partitions, int index_orig )
|
2006-03-24 12:08:41 -07:00
|
|
|
{
|
|
|
|
//remove unallocated space following the original partition
|
|
|
|
if ( index_orig +1 < static_cast<int>( partitions .size() ) &&
|
2018-10-14 06:27:58 -06:00
|
|
|
partitions[index_orig+1].type == TYPE_UNALLOCATED )
|
2006-03-24 12:08:41 -07:00
|
|
|
partitions .erase( partitions .begin() + index_orig +1 );
|
|
|
|
|
|
|
|
//remove unallocated space preceding the original partition and the original partition
|
2018-10-14 06:27:58 -06:00
|
|
|
if (index_orig-1 >= 0 && partitions[index_orig-1].type == TYPE_UNALLOCATED)
|
2006-03-24 12:08:41 -07:00
|
|
|
partitions .erase( partitions .begin() + --index_orig ) ;
|
|
|
|
|
|
|
|
//and finally remove the original partition
|
|
|
|
partitions .erase( partitions .begin() + index_orig ) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //GParted
|