Commit Graph

2802 Commits

Author SHA1 Message Date
Mike Fleetwood ad4191475a Rename file system from "crypt-luks" to "luks" (#760080)
The name of the format is Linux Unified Key Setup, or just LUKS.
https://en.wikipedia.org/wiki/Linux_Unified_Key_Setup

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood 2d910494d3 Display messages for encrypted file systems (#760080)
At the moment any messages for an encrypted file system aren't shown,
only messages from the outer PartitionLUKS object are shown.  Also in
Win_GParted::activate_paste() the selected Partition object, possibly
a derived PartitionLUKS, is cloned and the messages cleared.

Therefore a set of accessor methods must be provided to query and modify
partition messages.  Messages will be stored in the Partition object to
which they are added and retrieved from all.  So in the case of a
derived PartitionLUKS they will be retrieved from the messages vector of
the PartitionLUKS object itself and the messages vector for the
encrypted file system it contains.

To replace code like this in GParted_Core:

    partition_temp->messages = messages;

We might naturally provide a set_messages() method which assigns the
messages vector and is used like this:

    partition_temp->set_messages( messages );

However on a PartitionLUKS object what should set_messages() do?  By the
name it will replace any existing messages in the PartitionLUKS object
itself, but what should happen to the messages for the contained
encrypted Partition object?  Should they be cleared or left alone?
Rather than implement set_messages() with unclear semantics implement
append_messages(), which in the PartitionLUKS object case will clearly
leave any messages for the contained encrypted Partition object alone.
Append_messages() is then used to add messages as the Partition or
PartitionLUKS objects when populating the data in GParted_Core.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood f91d7e19dd Add encryption section into the Information dialog (#760080)
For LUKS formatted partitions add an encryption section into the
Information dialog and display the type of encryption, path, UUID and
status of the encryption.

The file system section continues to display appropriate file system
details, including the partition graphic with the file system specific
border colour and correct usage.  The details will either be of a plain
file system, an encrypted file system, or nothing when there is no open
dm-crypt mapping, leaving the encrypted file system inaccessible.
Should there be LUKS encryption directly within LUKS encryption then the
details of the inner encryption will be displayed in the file system
section.  However this configuration will not be further supported by
GParted.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood cb24aa4be1 Display usage of encrypted file systems (#760080)
There is already the set of methods in the Partition class to report the
file system usage.  Virtualise them and provide PartitionLUKS specific
implementations to calculate the usage of a file system wrapped in LUKS
encryption.

See the ascii art and comment in PartitionLUKS.cc for the details of
those calculations.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood 38a790d745 Display the label of encrypted file systems (#760080)
LUKS headers don't provide any concept of label.  Also there is already
the method Partition::get_filesystem_label() for getting the *file
system* label, so virtualise it and provide an appropriate
implementation to get the label of an encrypted file system represented
within a derived PartitionLUKS object.  This causes the label to be
displayed correctly in the main window.

It also happens to display the encrypted file system label in the
Information dialog for a LUKS formatted partition.  However the whole
Information dialog will be addressed differently in a following commit.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood b45af51a92 Display GUI column Mount Point correctly for encrypted file systems (#760080)
For open dm-crypt mappings get the Mount Point value from the encrypted
file system Partition object.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood a58e85d193 Display encrypted file system colour correctly in the disk graphic (#760080)
In the main window disk graphic, when there is an open dm-crypt mapping,
display the colour of the encrypted file system.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood cb3cc505ce Display "[Encrypted] FSTYPE" in the File System column (#760080)
In the File System column in the GUI, when there is an open dm-crypt
mapping, display the colour square for the encrypted file system within
and the text as "[Encrypted] FSTYPE".  For closed mappings nothing can
be known about the encrypted file system within so continue to display a
purple square and the text "[Encrypted]".

Looks like:

    Partition        | File System
      ...
      /dev/sdb3        # ext4
    v /dev/sdb4    *   # extended
        /dev/sdb5      # [Encrypted]
        /dev/sdb6  *   # [Encrypted] unknown
        /dev/sdb7  *   # [Encrypted] ext4

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood 6e294dda05 Remove Partition::color member (#760080)
Partition object represents a region of a disk and the file system
within.  GParted always displays the colour base of the type of the file
system.  Therefore remove the color member and always look it up from
the type of the file system as needed.

This makes one less member that will need virtual accessor methods with
different handling in the derived PartitionLUKS class.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood c9a2986fb9 Populate encrypted Partition member inside PartitionLUKS (#760080)
When there exists an open dm-crypt mapping, populate the encrypted
Partition object representing the encrypted file system.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 99ff0c7628 Add PartitionLUKS class and create objects (#760080)
Absolute minimum implementation of a PartitionLUKS class which can be
constructed, polymorphically copied and destroyed.  Contains an
"encrypted" member of type Partition to represent the encrypted file
system within the LUKS format.

Create PartitionLUKS objects instead of base Partition objects when a
LUKS formatted partition is found.  Only the base Partition object
member values have been populated, and the "encrypted" member remains
blank at this point.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 9fee0c57ea Refactor set_used_sectors() to be called per partition (#760080)
This is the equivalent change as made to set_mountpoints() in an earlier
commit.  Change GParted_Core::set_used_sectors() from being called with
a vector of partitions and processing them all to being called per
partition.  This is in preparation for calling set_used_sectors() on a
single Partition object inside a PartitionLUKS object.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood e9b893b4cb Populate active LUKS mountpoints with /dev/mapper/NAME entry (#760080)
Populate the canonical device name, /dev/mapper/NAME, used to access the
encrypted file system into the mount points of the Partition object.
This is the equivalent of what is already done for the Volume Group name
and SWRaid Array device.

This does get displayed in the Mount Point column in the main window,
which isn't wanted.  However the data will be needed when displaying
details of the encryption mapping in the Information dialog.  Both will
be dealt with in following commits.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 1b731b93d7 Refactor set_mountpoints() to be called per partition (#760080)
Previously GParted_Core::set_mountpoints() was called with a vector of
partitions and processed them all.  Now make set_mountpoints() process a
single partition and push the calls to it down one level from
set_devices_thread() into set_device_partitions() and
set_device_one_partition().  This is in preparation for having an
encrypted file system represented as a Partition object inside a
PartitionLUKS object and needing to call set_mountpoints() for the inner
single Partition object.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 132091269c Implement demand loading of LUKS_Info cache (#760080)
Only load the LUKS_Info cache of active dm-crypt mappings when the first
LUKS partition is encountered.  Not needed from a performance point of
view as the longest that I have ever seen "dmsetup table --target crypt"
take to run is 0.05 seconds.  Just means that the dmsetup command is
only run when there are LUKS partitions and the information is needed.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 317114ffcb Populate LUKS partition usage (#760080)
Populate the used, unused and unallocated figures in the Partition
object for a LUKS formatted partition.  See comment in
luks::set_used_sectors() for the rational of what is used, unused and
unallocated.

As that rational mentions, a LUKS header does not store the size of the
encrypted data and is assumed to extend to the end of the partition by
the tools which start the mapping.

An underlying block device of 128 MiB (131072 KiB).
    # sfdisk -s /dev/sde
    131072

An active LUKS mapping at offset 2 MiB (4096 512-byte sectors) and
length 126 MiB (129024 KiB, 258048 512-byte sectors).
    # sfdisk -s /dev/mapper/sde_crypt
    129024
    # cryptsetup status sde_crypt
    /dev/mapper/sde_crypt is active.
      type:  LUKS1
      cipher:  aes-cbc-essiv:sha256
      keysize: 256 bits
      device:  /dev/sde
      offset:  4096 sectors
      size:    258048 sectors
      mode:    read/write

No size/length reported when dumping the LUKS header, just (payload)
offset.
    # cryptsetup luksDump /dev/sde
    LUKS header information for /dev/sde

    Version:        1
    Cipher name:    aes
    Cipher mode:    cbc-essiv:sha256
    Hash spec:      sha1
    Payload offset: 4096
    MK bits:        256
    MK digest:      7f fb ba 40 7e ba e4 3b 2f c6 d0 93 7b f7 05 49 7b 72 d4 ad
    MK salt:        4a 5b 54 f9 7b 67 af 6e ef 16 31 0a fe d9 7e 5f
                    c3 66 dc 8a ed e0 07 f4 45 c3 7c 1a 8d 7d ac f4
    MK iterations:  37750
    UUID:           0a337705-434a-4994-a842-5b4351cb3778
    ...

Shrink the LUKS mapping to 64 MiB (65536 KiB, 131072 512-byte sectors).
    # cryptsetup resize --size 131072 sde_crypt
    # sfdisk -s /dev/mapper/sde_crypt
    65536
    # cryptsetup status sde_crypt
    /dev/mapper/sde_crypt is active.
      type:  LUKS1
      cipher:  aes-cbc-essiv:sha256
      keysize: 256 bits
      device:  /dev/sde
      offset:  4096 sectors
      size:    131072 sectors
      mode:    read/write

Stop and start the LUKS mapping.
    # cryptsetup luksClose sde_crypt
    # cryptsetup luksOpen /dev/sde sde_crypt

The size of the LUKS mapping is back to 126 MiB (129024 KiB, 258048
512-byte sectors), extending to the end of the partition.
    # sfdisk -s /dev/mapper/sde_crypt
    129024
    # cryptsetup status sde_crypt
    /dev/mapper/sde_crypt is active.
      type:  LUKS1
      cipher:  aes-cbc-essiv:sha256
      keysize: 256 bits
      device:  /dev/sde
      offset:  4096 sectors
      size:    258048 sectors
      mode:    read/write

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 317e444056 Add loading of LUKS mapping offset and length (#760080)
Also load the starting offset and length of the active dm-crypt mapping
into the LUKS_Info module from the dmsetup output.  This provides the
location and size of the encrypted data within the underlying block
device.

Note that dmsetup reports in units of 512 bytes sectors [1], the GParted
LUKS_Info module uses bytes and GParted Partition objects work in device
sector size units.  However the actual sector size of a dm-crypt mapping
[2] is the same as that of the underlying block device [3].

    # modprobe scsi_debug dev_size_mb=128 sector_size=4096
    # fgrep scsi_debug /sys/block/*/device/model
    /sys/block/sdd/device/model:scsi_debug
    # parted /dev/sde print
    Error: /dev/sde: unrecognised disk label
    Model: Linux scsi_debug (scsi)
    Disk /dev/sde: 134MB
[3] Sector size (logical/physical): 4096B/4096B
    Partition Table: unknown

    # cryptsetup luksFormat /dev/sde
    # cryptsetup luksOpen /dev/sde sde_crypt
    # parted /dev/mapper/sde_crypt print
    Error: /dev/mapper/sde_crypt: unrecognised disk label
    Model: Linux device-mapper (crypt) (dm)
    Disk /dev/mapper/sde_crypt: 132MB
[2] Sector size (logical/physical): 4096B/4096B
    Partition Table: unknown

    # cryptsetup status sde_crypt
    /dev/mapper/sde_crypt is active.
      type:  LUKS1
      cipher:  aes-cbc-essiv:sha256
      keysize: 256 bits
      device:  /dev/sde
      offset:  4096 sectors
      size:    258048 sectors
      mode:    read/write
    # dmsetup table --target crypt
    ...
    sde_crypt: 0 258048 crypt aes-cbc-essiv:sha256 0000000000000000000000000000000000000000000000000000000000000000 0 8:64 4096

[1] Both cryptsetup and dmsetup report the offset as 4096 and the size/
length as 258048.  128 MiB / (4096+258048) = 512 byte units, even on a
4096 byte sector size device.

Update debugging of LUKS to this:

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================
    DEBUG: /dev/sdb5: LUKS closed
    DEBUG: /dev/sdb6: LUKS open mapping /dev/mapper/sdb6_crypt, offset=2097152, length=534773760
    /dev/sde: unrecognised disk label
    DEBUG: /dev/sde: LUKS open mapping /dev/mapper/sde_crypt, offset=2097152, length=132120576

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood ae57f3cd4e Prevent Mount/Umount operation being available for LUKS (#760080)
The code currently allows attempting to mount and unmount a LUKS
partition.  It is nonsense to directly try to mount and unmount a LUKS
partition and obviously doesn't work.  For read-only LUKS support there
is no need to attempt to apply this to the encrypted file system within.
Therefore prevent these operations for LUKS partitions.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood e86e7b91b2 Prevent crypt-luks appearing in the Create New dialog and Format menu (#760080)
This patchset is adding read-only LUKS support.  Creation of LUKS is
planned to be a tick box adding encryption in the Create New Partition
dialog.  Therefore remove the greyed out crypt-luks entry in the Create
New Partition dialog and the Format menu.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 070d734e57 Add busy detection of LUKS mapping (#760080)
Provide a minimal implementation of a luks file system class which only
does busy detection.

NOTE:
For now, read-only LUKS support, a LUKS partition will be busy when a
dm-crypt mapping exists.  Later when read-write LUKS support is added
GParted will need to look at the busy status of the encrypted file
system within the open LUKS partition and map LUKS partition busy status
to encryption being open or closed.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood b77a6be76b Add initial loading of LUKS mapping details (#760080)
Load basic details of active Device-mapper encryption mappings from the
kernel.  Use dmsetup active targets.

    # cryptsetup luksFormat /dev/sdb5
    # cryptsetup luksFormat /dev/sdb6
    # cryptsetup luksOpen /dev/sdb6 sdb6_crypt
    # ls -l /dev/mapper/sdb6_crypt /dev/dm-0
    lrwxrwxrwx. 1 root root 7 Nov 15 09:03 /dev/mapper/sdb6_crypt -> ../dm-0
    brw-rw----. 1 root disk 253, 0 Nov 15 09:03 /dev/dm-0
    # ls -l /dev/sdb6
    brw-rw----. 1 root disk 8, 22 Nov 15 09:02 /dev/sdb6
    # dmsetup table --target crypt
    sdb6_crypt: 0 1044480 crypt aes-cbc-essiv:sha256 0000000000000000000000000000000000000000000000000000000000000000 0 8:22 4096

So far just load the mapping name and underlying block device reference
(path or major, minor pair).

Note that all supported kernels appear to report the underlying block
device as major, minor pair in the dmsetup output.  Underlying block
device paths are added to the cache when found during a search to avoid
stat(2) call on subsequent searches for the same path.

Prints debugging to show results, like this:

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================
    DEBUG: /dev/sdb5: LUKS closed
    DEBUG: /dev/sdb6: LUKS open mapping /dev/mapper/sdb6_crypt

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood 1b55dfad5b Move DEV_MAPPER_PATH from DMRaid.h to Utils.h (#760080)
Renamed from DEV_MAP_PATH to DEV_MAPPER_PATH.  Moved so that the
constant is logically intended for use outside of the DMRaid class.

Also specifically make the string constant have external linkage, rather
than the default internal (static) linkage for constants, so that there
is only one copy of the variable in the program, rather than one copy in
each compilation unit which included DMRaid.h.  Namely DMRaid.cc and
GParted_Core.cc.

References:
[1] Proper way to do const std::string in a header file?
    http://stackoverflow.com/questions/10201880/proper-way-to-do-const-stdstring-in-a-header-file
[2] What is external linkage and internal linkage in C++
    http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c/1358796#1358796

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:40 -07:00
Mike Fleetwood cd64d6503b Remove unused Partition(path) constructor
History:

1) The constructor was added by commit:

    6d8b169e73
    2006-03-14 21:37:47
    changed the way devices and partitions store their devicepaths.  Instead of

2) Removed from most of the file system specific ::Copy() methods by
   commit:

    ad9f2126e7
    2006-03-19 15:30:20
    fixed issues with copying (see also #335004) cleanups + added FIXME added

3) Removed from GParted_Core::copy() method by commit:

    7bb7e8a84f
    2006-05-23 22:17:34
    Use ped_device_read and ped_device_write instead of 'dd' to copy

4) Finally removed from the last place in xfs::Copy() method by commit:

    e414b71b73
    2012-01-11 19:49:13
    Update xfs resize and copy to use new helper functions

The Partition(path) constructor is no longer used.  Remove.
2016-01-26 10:11:36 -07:00
Mike Fleetwood 561e2203d5 Add virtual qualifier to derived Operation class destructors
When a base class destructor is virtual, derived class destructors are
also virtual [1] even if they don't have the virtual qualifier.

As the Operation destructor is virtual, derived Operation* classes
destructors are virtual too.  Add virtual qualifier just to reflect what
the C++ language mandates the compiler implement.

[1] Derived class with non-virtual destructor
    http://stackoverflow.com/questions/7403883/derived-class-with-non-virtual-destructor
2016-01-26 10:11:36 -07:00
Mike Fleetwood 24fa553385 Remove unnecessary sector_size parameter from Get_New_Partition methods
The sector_size parameter is unnecessary as the value can be retrieved
from the sector size of the selected Partition object on which the
create new, copy & paste or resize/move operation is being performed.

For the create new and resize/move operations it is trivial as the
existing unallocated or in use Partition object on which the operation
is being perform already contains the correct sector size.  For the copy
& paste operation, which can copy across disk devices of different
sector sizes, we merely have to use the sector size of the existing
selected (destination) Partition object rather than copied (source)
Partition object.  Hence these relevant lines in the new code:

    Dialog_Partition_Copy::set_data(selected_partition, copied_partition)
        new_partition = copied_partition.clone();
        ...
        new_partition->sector_size = selected_partition.sector_size;
2016-01-26 10:11:35 -07:00
Mike Fleetwood 3b3d8e44b6 Stop relying on specific values of PED_PARTITION_* enum
The expressions used in the call to Set() were comparing
lp_partition->type to 0 for the type parameter and passing it as a bool
for the inside_extended parameter.  Libparted lp_partition->type is
actually an enumeration.  The code was only working because of the
specific values assigned to the symbolic names, PED_PARTITION_NORMAL = 0
and PED_PARTITION_EXTENDED is non-zero (true).

Make the code use the symbolic names and not depend on the actual
enumeration values, which should be considered changeable and private to
libparted.
2016-01-26 10:11:35 -07:00
Mike Fleetwood 7870a92b80 Add FILESYSTEM_MAP[FS_UNALLOCATED] entry
When displaying an unallocated partition
Win_GParted::set_valid_operations() calls GParted_Core::get_fs() with
parameter FS_UNALLOCATED.

Before this change, get_fs() would fail to find file system capabilities
set for FS_UNALLOCATED and construct a not supported capabilities set
and return that.

Afterwards, find_supported_filesystems() creates a not supported
capabilities set from the NULL pointer for FS_UNALLOCATED and adds this
entry into the FILESYSTEMS vector.  Then get_fs() finds that not
supported capabilities set for FS_UNALLOCATED in the FILESYSTEMS vector
and returns that.

This makes no functional difference.  It just seems right as other
unsupported but used file system types have entries in FILESYSTEM_MAP.
2016-01-26 10:11:35 -07:00
Mike Fleetwood 1a4cefb960 Initialise all struct FS members
The struct FS constructor initialised every member *except* filesystem
and busy.  Then in *most* cases after declaring struct FS, assignments
followed like this:
    FS fs;
    fs.filesystem = FS_BTRFS;
    fs.busy       = FS::GPARTED;
But member busy wasn't always initialised.

Add initialisation of members filesystem and busy to the struct FS
constructor.  Specify optional parameter to the constructor to set the
filesystem member, or when left off filesystem is initialised to
FS_UNKNOWN.
2016-01-26 10:11:35 -07:00
Mike Fleetwood 49664f3ca3 Simplify GParted_Core::get_fs()
get_fs() used to work by (1) returning the supported capabilities of the
requested file system found in the FILESYSTEMS vector; (2) if not found
return the supported capabilities for file system FS_UNKNOWN; and (3)
if that wasn't found either, create a not supported capabilities set for
FS_UNKNOWN and return that.

This is more complicated that required.  Also the not supported
capabilities set, as created by struct FS() constructor, is the same as
that created in file_supported_filesystems() local variable fs_notsupp.

Simplify get_fs() just using a single not found code path returning a
not supported capabilities set.
2016-01-26 10:11:35 -07:00
Mike Fleetwood 320e166c03 Implement and use virtual Partition copy constructor clone() (#759726)
Final step for full polymorphic handling of Partition objects is to
implement a virtual copy constructor.  C++ doesn't directly support
virtual copy constructors, so instead use the virtual copy constructor
idiom [1].  (Just a virtual method called clone() which is implemented
in every polymorphic class and creates a clone of the current object and
returns a pointer to it).

Then replace all calls to the (monomorphic) Partition object copy
constructor throughout the code, except in the clone() implementation
itself, with calls to the new virtual clone() method "virtual copy
constructor".

Also have to make the Partition destructor virtual too [2][3] so that
the derived class destructor is called when deleting using a base class
pointer.  C++ supports this directly.

[1] Wikibooks: More C++ Idioms / Virtual Constructor
    https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor

[2] When to use virtual destructors?
    http://stackoverflow.com/questions/461203/when-to-use-virtual-destructors

[3] Virtuality
    Guideline #4: A base class destructor should be either public and
    virtual, or protected and nonvirtual
    http://www.gotw.ca/publications/mill18.htm

Bug 759726 - Implement Partition object polymorphism

SQUASH: When first using pointers to Partition and calling delete
2016-01-26 10:11:35 -07:00
Mike Fleetwood 656e1709ff Replace all Partition object copy assignment (#759726)
Copy assignment of Partition objects is now only performed in a few
places in the Operation and OperationResizeMove classes when updating
the displayed PartitionVector.  (From Refresh_Visual() when each
operation is visually applied to the display_partitions vector; the
new_partition from the operation is copy assigned over the top of the
relevant existing partition in the display_partitions vector).

In general polymorphic copy assignment is complicated [1], and is now
unnecessary given the above limited use.  All that is needed is a way to
polymorphically replace one Partition object with another in a
PartitionVector.

First, prevent further use of Partition object copy assignment by
providing a private declaration and no implementation, so the compiler
enforces this.  Second implement and use PartitionVector method
replace_at() which replaces a pointer to one Partition object with
another at the specified index in the PartitionVector.

[1] The Assignment Operator Revisited
    [Section:] Virtual assignment
    http://icu-project.org/docs/papers/cpp_report/the_assignment_operator_revisited.html

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 4a6cbcd0f1 Use pointer to Partition in Dialog_Base_Partition and derived classes (#759726)
Now use a pointer to the Partition object in Dialog_Base_Partition class
and derived classes, Dialog_Partition_{Copy,New,Resize_Move}.  This is
equivalent to how the Partition objects are managed in the Operation and
derived classes.

The Partition object is allocated and copy constructed in each derived
classes' set_data() method, called from each constructor and deallocated
in the destructors.  Considering the remaining Big 3, these classes are
never copy constructed or copy assigned so provide private definitions
and no implementations so the compiler enforces this.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 9f4e6909c5 Stop unnecessarily coping a Partition in Dialog_Rescue_Data (#759726)
The code in on_view_clicked() copy constructed a Partition object and
then in the following 3 lines only read a couple of public member
variables from the new copy.

Making a copy of the partition is unnecessary.  Change to just creating
a constant reference to the Partition instead.

(It would also be an impediment to polymorphically using Partition
objects, except for the fact that gpart doesn't recognise LUKS
signatures so will never have to create a PartitionLUKS object).

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 4d8578646c Change methods to use Partition pointers locally (#759726)
A number of methods in GParted_Core and Win_GParted were using local
Partition objects.  Change them into pointers so that Partition object
polymorphism can be implemented.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood ea8ab702f7 Change copied_partition into a pointer (#759726)
Change Win_GParted::copied_partition from Partition object which is
copied by value into a pointer to a Partition object object which is
allocated, copy constructed and deleted.  Required as part of the
polymorphic implementation of Partitions.

As before when managing the lifetime of pointers to objects in a class
the Big 3 of destructor, copy constructor and copy assignment operator
need to be considered.  A destructor is added to finally delete
copied_partition.  A single Win_GParted object is only ever created and
destroyed in main().  The class is never copy constructed or copy
assigned.  Make the compiler enforce this with private declarations and
no implementations.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood b516b1093c Use pointers to Partitions in Operation classes (#759726)
Operation classes now internally use pointers to Partition objects and
take on management of their lifetimes.  As before, with the
PartitionVector class, when storing pointers in a class the Big 3 of
destructor, copy constructor and copy assignment operator also have to
be considered.

First, all the Partition objects are allocated in the derived Operation*
class parameterised constructors and freed in the associated
destructors.  However the Operation classes are never copy constructed
or copy assigned; they are only ever created and destroyed.  Only
pointers to the derived Operations are copied into the vector of pending
operations.  Therefore the copy construtor and copy assignment operator
aren't needed.  To enforce this provide inaccessible private
declarations without any implementation so that the compiler will
enforce this [1][2].

This example code fragment:
 1  OperationCheck o1( device, partition );
 2  OperationCheck o2 = o1;
 3  o2 = o1;
Does these OperationCheck calls:
 1  Implemented parameterised construtor,
 2  Disallowed copy constructor,
 3  Disallowed copy assignment

Trying to compile the above code would fail with errors like these:
    ../include/OperationCheck.h: In member function 'void GParted::Win_GParted::activate_check()':
    ../include/OperationCheck.h:36:2: error: 'GParted::OperationCheck::OperationCheck(const GParted::OperationCheck&)' is private
      OperationCheck( const OperationCheck & src );              // Not implemented copy constructor
      ^
    test.cc:2:21: error: within this context
      OperationCheck o2 = o1;
                          ^

    ../include/OperationCheck.h:37:19: error: 'GParted::OperationCheck& GParted::OperationCheck::operator=(const GParted::OperationCheck&)' is private
      OperationCheck & operator=( const OperationCheck & rhs );  // Not implemented copy assignment operator
                       ^
    test.cc:3:4: error: within this context
      o2 = o1;
         ^

[1] Disable copy constructor
    http://stackoverflow.com/questions/6077143/disable-copy-constructor
[2] Disable compiler-generated copy-assignment operator [duplicate]
    http://stackoverflow.com/questions/7823845/disable-compiler-generated-copy-assignment-operator

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 6fd37c0745 Protect partition members within Operation classes (#759726)
The Operation classes contain partition objects which are copied by
value.  Need to replace these with pointers to Partition objects instead
and manage their lifetimes so that they can be used polymorphically.

First step is to protect the partition members partition_new,
partition_original, and for OperationCopy class only, partition_copied
within the Operation classes and provide accessor methods.

get_partition_new() and get_partition_original() accessors are
implemented in the Operation base class so all derived classes get an
implementation.  get_partition_new() is also virtual so that
OperationCheck and OperationDelete can override the implementation and
assert that they don't use partition_new.  get_partition_copied() is
provided for the OperationCopy class only so can only be accessed via an
OperationCopy type variable.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 504a2d8393 Remove copy constructing add item methods from PartitionVector (#759726)
Remove PartitionVector push_back() and insert() methods which copy
construct Partitions objects into the vector.  All the code has already
been changed to dynamically allocate Partition objects and use the
adoption variants of these methods named, push_back_adopt() and
insert_adopt().  Remove the no longer used methods.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood f6b45a0429 Use Partition pointer adoption everywhere when adding items into a PartitionVector (#759726)
Replace all the current code which uses push_back() and insert() of a
local Partition object and gets it copy constructed into a
PartitionVector.  Instead allocate a Partition object on the heap and
adopt a pointer into the PartitionVector using push_back_adopt() and
insert_adopt().

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 2a2a99b2bf Consolidate down to a single insert_unallocated() implementation (#759726)
GParted_Core and Operation classes both have an insert_unallocated()
method which do the same thing with very nearly identical code.  Both
methods insert unallocated partitions into the vector of partitions
within the specified range of sectors to fill in any gaps larger than
1 MiB.  The only difference was how the two methods got the device path;
the GParted_Core class method got it via a parameter and the Operation
class method got it by calling get_path() on its device member variable.
The GParted_Core insert_unallocated() method gets called during device
scanning and the Operation one gets called when constructing the visual
for a pending operation.

Consolidate down to a single insert_unallocated() implementation by
making the Operation class method call the GParted_Core class method.
Make the GParted_Core class method static and public so that it can be
called using the class name from outside the class.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood fdbd86f1ea Add adoption methods for adding items into a PartitionVector (#759726)
The current code uses push_back() and insert() to copy Partition objects
into the vector of pointers.  This has a few issues:
1) Unnecessary copying of Partition objects;
2) Hides the nature of the PartitionVector class as a manager of
   pointers to Partition objects by providing copy semantics to add
   items.  It is generally better to be explicit;
3) C++ doesn't provide polymorphic copy construction directly, but this
   is easily worked around by following the Virtual Constructor idiom
   [1], which would allow PartitionLUKS derived class objects to be
   copied into the vector.

Add push_back_adopt() and insert_adopt() methods which add a pointer to
a Partition object into the PartitionVector adopting ownership.

[1] Wikibooks: More C++ Idioms / Virtual Constructor
    https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Virtual_Constructor

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 06b8a3a14a Use pointers to Partitions in PartitionVector class (#759726)
The PartitionVector class is now internally using pointers to Partition
objects and taking on management of their lifetimes.  It therefore has
to implement the Big 3: destructor, copy constructor and copy assignment
operator [1][2].  This is because the implicitly-defined copy
constructor and assignment operator perform memberwise "shallow copying"
and the destructor does nothing.  This not correct for classes which
contain non-class types such as raw pointers.

The semantics of the interface still copies each Partition object into
the PartitionVector when they are added with push_back() and insert().

Note that a PartitionVector object is explicitly copy assigned in
Win_GParted::Refresh_Visual().  They are also implicitly copied when
(1) the implementing vector is resized larger to allow it to hold more
pointers to Partition objects than it previously had capacity for; and
(2) a Partition object is copied including the logicals PartitionVector
member.

[1] The rule of three/five/zero
    http://en.cppreference.com/w/cpp/language/rule_of_three
[2] Rule of Three
    https://en.wikipedia.org/wiki/Rule_of_three_%28C%2B%2B_programming%29

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 48d898ebfd Include Partition.h header everywhere it's used
Lots of files which use the Partition class relied on the declaration
being included via other header files.  This is bad practice.

Add #include "Partition.h" into every file which uses the Partition
class which doesn't already include it.  Header file #include guards are
specifically to allow this.
2016-01-26 10:11:35 -07:00
Mike Fleetwood fae909897e Use PartitionVector class throughout the code (#759726)
Replace all occurrences of std::vector<Partition> with PartitionVector.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:35 -07:00
Mike Fleetwood 81337141d7 Stop returning vector of partitions from Dialog_Rescue_Data class (#759726)
get_partitions() method was returning a vector of partitions.  However
the calling code only needed to know whether any partitions were found
or not.  Replace with found_partitions() method reporting the needed
boolean.

Now use of std::vector<Partition> partitions is hidden within the
Dialog_Rescue_Data class implementation.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:34 -07:00
Mike Fleetwood fa98273445 Create PartitionVector class (#759726)
Just creates PartitionVector class and includes it in partition.h so
that it is built and validated by the compiler.  Not used anywhere yet.

Implementation strategy is to create a PartitionLUKS class derived from
the Partition class.  This implies polymorphism of Partition objects,
which in C++ requires using pointers and references to objects, and not
using objects directly.  (See C++ object slicing).  Later this
PartitionVector class will be modified to use pointers to Partition
objects and act as the owner of the pointed to Partition objects.

Bug 759726 - Implement Partition object polymorphism
2016-01-26 10:11:34 -07:00
Richard Hughes f22eee9e7c Add a missing tag to the AppData file 2016-01-25 16:05:20 +00:00
Aurimas Černius 1c916f9906 Updated Lithuanian translation 2016-01-23 18:19:17 +02:00
Curtis Gedak 15b2e9f9d0 Append -git to version for continuing development 2016-01-18 10:15:02 -07:00
Curtis Gedak 976eea771c ========== gparted-0.25.0 ========== 2016-01-18 09:29:50 -07:00