Commit Graph

1864 Commits

Author SHA1 Message Date
Mike Fleetwood fe7b734792 Switch to using struct FS_Limits inside Dialog_Partition_Resize_Move (#787204)
Changes the internal code in Dialog_Partition_Resize_Move to use
fs_limits instead of fs.MIN and fs.MAX.  The limits are still passed
into the constructor via struct FS and it's members .MIN and .MAX but
immediately used to assign to fs_limits.

Bug 787204 - Minimum and maximum size of the UDF partition/disk
2018-01-28 10:09:35 -07:00
Mike Fleetwood 285c24a82a Query and pass struct FS_Limits into Dialog_Partition_Copy (#787204)
Refactor Win_GParted::activate_paste() to query the file system size
limits using the new get_filesystem_limits() method and pass those
limits into the the dialog class as struct FS_Limits.

Bug 787204 - Minimum and maximum size of the UDF partition/disk
2018-01-28 10:09:35 -07:00
Mike Fleetwood fc436595fd Switch to using struct FS_Limits inside Dialog_Partition_Copy (#787204)
Adds working copy fs_limits member into common Dialog_Base_Partition
class.  Changes the internal code in Dialog_Partition_Copy class to use
fs_limits instead of fs.MIN and fs.MAX.  The limits are still passed
into the constructor via object of struct FS and it's members .MIN and
.MAX but immediately used to assign to the fs_limits member.

Bug 787204 - Minimum and maximum size of the UDF partition/disk
2018-01-28 10:09:35 -07:00
Mike Fleetwood aea0070799 Assign to duplicate FS_Limits (#787204)
Duplicate the assignment of file system size limits into
struct FS_Limits, matching the fixed values currently assigned to
struct FS members .MIN and .MAX.

Bug 787204 - Minimum and maximum size of the UDF partition/disk
2018-01-28 10:09:35 -07:00
Pali Rohár 2f532be9f9 Use external tools udfinfo and udflabel for UDF file system (#792052)
Those external tools were introduced in version 2.0 of udftools package
and can show or change UDF label, UDF uuid and can provide information
needed for counting total/free sectors.

Bug 792052 - Add support for changing UDF label/uuid and show disk usage
2018-01-04 17:02:53 +00:00
Mike Fleetwood b04dbbc357 Rename function and reword text for rollback of failed file system move
To better reflect specifically that it is a failed (internally
implemented) file system move which is being rolled back.
2018-01-02 10:34:52 -07:00
Mike Fleetwood f54dd10707 Fix rollback when growing a partition by more than twice fails (#791875)
Attempt to grow a partition to more than twice it's size.  If committing
that change to the partition fails in such a way that the new larger
partition boundaries are not written to the disk drive then rolling back
will fail with libparted error:
    Can't have overlapping partitions.

Example operation details:

    Grow /dev/sdb8 from 1.00 GiB to 2.20 GiB
    * calibrate /dev/sdb8                                      (SUCCESS)
    * check file system on /dev/sdb8 for errors and (if poss...(SUCCESS)
    * grow partition from 1.00 GiB to 2.20 GiB                 (ERROR)
    * attempt to rollback failed change to the partition       (ERROR)
        original start: 7350272
        original end:   9447423
        original size:  2097152 (1.00 GiB)
      * libparted messages                                     (ERROR)
          Can't have overlapping partitions.

What happened is that resize_move_partition() passed the new Partition
object to resize_move_partition_implement() as the source partition for
the rollback, and than called ped_disk_partition_by_sector() with a
sector in the middle to identify the partition to be changed.  However
the new partition was never written to the drive so in the middle was
outside the old smaller partition.  Therefore libparted identified empty
space after the partition, rather than the partition itself, as the
intended target so when ped_disk_set_partition_geom() was called it
reported error "Can't have overlapping partitions" because it thought
another partition was being created with the same boundaries as the old
partition, rather than the boundaries of the old partition being
updated.

The same error also occurs when rolling back a failed partition change
as part of a move operation when the middle of the new partition falls
outside of the boundaries of the old partition.

Fix by making a temporary Partition object from the intersection of the
old and new partition boundaries just to be used to identify the
partition being changed to libparted.  As this is only rolling back a
single step adjusting the partition boundaries as part of a resize
and/or move operation, the old and new partition boundaries must
intersect (and in fact that intersection contains the file system data).

Bug 791875 - Rollback specific failed partition change steps
2018-01-02 10:34:52 -07:00
Mike Fleetwood 0b5bf83b22 Enable failed partition change rollback for selected steps (#791875)
The general rule is that:
1) For a partition change step BEFORE a file system change step,
   rollback on failure;
2) For a partition change step AFTER a file system change step, don't
   rollback on failure.

Examining every case where resize_move_partition() is called and whether
rollback on failure is wanted or not:

* In resize_move()
    Resize / move extended partition.  No associated file system change.
  NO ROLLBACK
    Just to keep possibly applied operation.

* #1 in move()
    Making all encompassing partition before moving file system.
  ROLLBACK
    To restore partition boundaries back to those of the file system.

* #2 in move()
    Recreating original partition boundaries after file system move
    failed or was cancelled and has been rolled back.
  NO ROLLBACK
    To keep updated partition boundaries to match restored file system
    data.

* #3 in move()
    Replacing all encompassing partition with final partition after
    successful file system move.
  NO ROLLBACK
    Keep new partition boundaries to match moved file system.

* #1 in resize_encryption()
    Making the partition larger before growing closed LUKS encrypted
    data.
  ROLLBACK
    Restore partition boundaries back to those of the closed LUKS
    encrypted data.

* #2 in resize_encryption()
    Shrinking the partition after open LUKS mapping has been shrunk, but
    before swap is re-created (smaller).
  NO ROLLBACK
    Difficult case because the partition shrink is in the middle of a
    LUKS shrink and a swap shrink (re-create).  If swap was actually
    shrunk like other types of file system, rather than re-created, then
    the operation sequence would be (1) shrink swap, (2) shrink LUKS
    encryption, (3) shrink partition.  In this hypothetical case and the
    actual case no rollback is preferred to try to keep the new
    partition boundaries match the shrunk open LUKS encryption mapping.

* #3 in resize_encryption()
    Grow the partition before growing open LUKS mapping and re-creating
    swap larger.
  ROLLBACK
    Restore partition boundaries back to those of the smaller open LUKS
    encryption mapping.

* #4 in resize_encryption()
    Shrink the partition after shrinking the file system and open LUKS
    encryption mapping.
  NO ROLLBACK
    Keep new smaller partition boundaries to match shrunk encrypted file
    system.

* #5 in resize_encryption()
    Grow the partition before growing the open LUKS encryption mapping
    and file system.
  ROLLBACK
    Restore partition boundaries back to those of the not yet grown
    encrypted file system.

* #1 in resize_plain()
    Resize partition before re-creating swap a different size.
  ROLLBACK
    Restore partition boundaries back to those of the not yet resized
    (re-created) swap space.

* #2 in resize_plain()
    Shrink partition after shrinking the file system.
  NO ROLLBACK
    Keep new smaller partition boundaries to match shrunk file system.

* #3 in resize_plain()
    Grow partition before growing the file system.
  ROLLBACK
    Restore partition boundaries back to those of the not yet grown
    file system.

Removes the default value from the rollback_on_fail parameter so
rollback or not has to be explicitly specified for every call of
resize_move_partition().

Bug 791875 - Rollback specific failed partition change steps
2018-01-02 10:34:52 -07:00
Mike Fleetwood 0f16703bbb Implement rollback of failed partition resize/move steps (#791875)
Even after implementing a fix for bug 790418 "Unable to inform the
kernel of the change message may lead to corrupted partition table"
GParted/libparted can still encounter errors informing the kernel of the
new partition layout.  This has been seen with GParted on CentOS 7 with
libparted 3.1.

In such a case the partition has been successfully written to the disk
but just informing the kernel failed.  This is a problem because when a
partition is being moved in advance of a file system move step, failure
to inform the kernel leaves the partition boundaries not matching the on
disk limits of the file system.  For a move to the left this leaves the
partition reported as unknown, apparently losing the user's data.

For example start with a 512 MiB partition containing an XFS file
system.  This is recognised by blkid and parted, hence also by GParted.

    # blkid /dev/sdb1
    /dev/sdb1: UUID=... TYPE="xfs" PARTUUID="37965980-01"
    # parted /dev/sdb unit s print
    Model: ATA VBOX HARDDISK (scsi)
    Disk /dev/sdb: 16777216s
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    Number  Start     End       Size      Type     File system  Flags
     1      1048576s  2097151s  1048576s  primary  xfs

Now move the partition 100 MiB to the left and have it fail to inform
the kernel after the first partition change step.  Operation details:

    Move /dev/sdb1 to the left                                 (ERROR)
    * calibrate /dev/sdb1                                      (SUCCESS)
    * check file system on /dev/sdb1 for errors and (if poss...(SUCCESS)
    * grow partition from 512.00 MiB to 612.00 MiB             (ERROR)
        old start: 1048576
        old end: 2097151
        old size: 1048576 (512.00 MiB)
        requested start: 843776
        requested end: 2097151
        requested size: 1253376 (612.00 MiB)
      * libparted messages                                     (ERROR)
          Error informing the kernel about modifications to partition
          /dev/sdb1 -- Device or resource busy.  This means Linux won't
          know about any changes you made to /dev/sdb1 until you reboot
          -- so you shouldn't mount it or use it in any way before
          rebooting.  Failed to add partition 1 (resource temporarily
          unavailable)

Now because the start of the partition is 100 MiB before the start of
the file system, the file system is no longer recognised, and apparently
the user's data has been lost.

    # blkid /dev/sdb1
    /dev/sdb1: PARTUUID="37965980-01"
    # parted /dev/sdb unit s print
    ...
    Number  Start    End       Size      Type     File system  Flags
     1      843776s  2097151s  1253376s  primary

It doesn't matter why updating the partition failed, even if it was
because of an error writing to the disk.  Rollback of the change to the
partition should be attempted.  The worst case scenario is that rollback
of the change fails, which is the equivalent to how the code worked
before this patch set.

However in other cases where the partition boundaries are being updated
after a file system move or shrink step then the partition should be
updated to match the new location of the file system itself.  And no
rollback is wanted.  If the failure was only informing the kernel then
in fact the partition has actually been updated on disk after all.

So each partition resize/move step needs examining on a case by case
basis to decide if rolling back the change to the partition is wanted or
not.

This patch only adds partition change rollback into
resize_move_partition().  Rollback remains disabled until all cases are
examined in the following patch.

Bug 791875 - Rollback specific failed partition change steps
2018-01-02 10:34:52 -07:00
Mike Fleetwood 93ccc79e3a Extract common code into update_dmraid_entry() (#791875)
Extract common code which updates a DMRaid device mapper entry into a
sub-function.  This will also be needed when adding rollback of a
partition change on failure.

Bug 791875 - Rollback specific failed partition change steps
2018-01-02 10:34:52 -07:00
Mike Fleetwood 890d5a93a7 Extract code into resize_move_partition_implement() (#791875)
Extract the code which actually implements the partition change into a
sub-function ready for adding rollback of the change on failure.

Bug 791875 - Rollback specific failed partition change steps
2018-01-02 10:34:52 -07:00
Mike Fleetwood ff0360eb4f Match up OperationDetail creation and status setting for internal copy (#790842)
This is not required, but it is more logical to have an OperationDetail
object created and it's final status set in the same function rather
than split between caller and callee.  So move creation of
"copy %1 using a block size of %2" OperationDetail objects into
GParted_Core::copy().

Also introduces a couple of variables to remove some recomputation:
benchmark_od & remaining_length.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-12-11 13:53:09 -07:00
Mike Fleetwood e5ccb3b8bc Set OperationDetail status last during internal copy benchmarking (#790842)
Performing a copy or move operation which uses GParted's internal copy
routine triggered the new GParted bug message.  Example operation
details:

  Copy /dev/sdb8 to /dev/sdb (start at 4.51 GiB)               (SUCESSS)
  * calibrate /dev/sdb8                                        (SUCCESS)
  * check file system on /dev/sdb8 for errors and (if possib...(SUCCESS)
  * create empty partition                                     (SUCCESS)
  * set partition type on /dev/sdb9                            (SUCCESS)
  * copy file system from /dev/sdb8 to /dev/sdb9               (SUCCESS)
      using internal algorithm
      copy 1.00 GiB
    * finding optimal block size
      * copy 16.00 MiB using a block size of 1.00 MiB          (SUCCESS)
          16.00 MiB of 16.00 MiB copied
          GParted Bug: Adding more information to the result...(WARNING)
          0.797269 seconds
      * copy 16.00 MiB using a block size of 2.00 MiB          (SUCCESS)
      * copy 16.00 MiB using a block size of 4.00 MiB          (SUCCESS)
      * copy 16.00 MiB using a block size of 8.00 MiB          (SUCCESS)
      * copy 16.00 MiB using a block size of 16.00 MiB         (SUCCESS)
        optimal block size is 1.00 MiB
    * copy 944.00 MiB using a block size of 1.00 MiB           (SUCCESS)

This is because when performing the initial benchmarking copies the time
taken by each copy is added to the operation detail results in the
calling GParted_Core::copy_blocks() after the final status was set in
CopyBlocks::copy() with set_success_and_capture_errors().  Fix by
setting the final status in the parent function after adding the time to
the benchmark copies.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-12-11 13:53:09 -07:00
Mike Fleetwood c7ee9934f1 Make OperationDetail no_more_children bug message translatable (#790842)
To be consistent with all previous bug messages being translatable.

Also only mark the bug as a warning instead of an error because the bug
doesn't cause any disk drive operations to fail.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-12-04 13:32:21 -07:00
Mike Fleetwood 2f53876c0f Wait for the kernel and udev to settle partitions for a second time (#790418)
There is still another subtle issue.  When GParted_Core::commit() closes
the device, the kernel initiates a second set of events which removes
and re-adds the partitions again.  Need to wait for these to complete
to prevent any following step failing with missing partition device
nodes.

Bug 790418 - "Unable to inform the kernel of the change" message may
             lead to corrupted partition table
2017-11-29 12:58:48 -07:00
Mike Fleetwood f49f0bb2b8 Avoid libparted failing to inform the kernel about partition changes (#790418)
Operations involving modifications to a partition are sometimes failing
with a libparted error informing the kernel about modifications to
partitions.  For example I encountered these errors when just creating a
fourth partition on CentOS 7 in a VirtualBox VM.  Operation results:

    Create Primary Partition #1 (ext4, 4.73 GiB) on /dev/sdb   (ERROR)
    * create empty partition                                   (ERROR)
      * libparted messages                                     (ERROR)
        * Error informing the kernel about modification to partition
          /dev/sdb1 -- Device or resource busy.  This means Linux won't
          know about any changes you made to /dev/sdb1 until you reboot
          -- so you shouldn't mount it or use it in any way before
          rebooting.
        * Failed to add partition 1 (Resource temporarily unavailable)

Those two libparted messages were presented in "Libparted Error" dialogs
and [Cancel] was selected both times.

        Libparted Error
    (-) Error informing the kernel about modifications to partition
        /dev/sdb1 -- Device or resource busy.  This means Linux won't
        know about any changes you made to /dev/sdb1 until you reboot --
        so you shouldn't mount it or use it in any way before rebooting.
                                                   [ Cancel ] [ Ignore ]

        Libparted Error
    (-) Failed to add partition 1 (Resource temporarily unavailable)
                                                    [ Retry ] [ Cancel ]

This is the edited output showing GParted print debugging, stracing of
GParted and monitoring of udev events for this case.

    # ./gpartedbin /dev/sdb
    ======================
    libparted : 3.1
    ======================
    ...
     24.541604 +23.923435 create_partition()   start (new_partition, optdet, min_size=0)  new_partition.device_path="/dev/sdb"
     24.556101 +0.014497 create_partition()   type=PED_PARTITION_NORMAL
     24.556354 +0.000253 commit()             start (lp_disk)  lp_disk->dev->path="/dev/sdb"
    D: strace pid 18054.  Press [Return] to continue.
    ^Z
    [1]+  Stopped          ./gpartedbin /dev/sdb

    # udevadm monitor &
    [2] 18124
    monitor will print the received events for:
    UDEV - the event which udev sends out after rule processing
    KERNEL - the kernel uevent

    # strace -p 18054 -e open,ioctl,write,close &
    [3] 18129
    strace: Process 18054 attached

    # fg %1
    ./gpartedbin /dev/sdb
    128.175811 +103.619457 commit()             calling ped_disk_commit_to_dev(lp_disk) ...
    open("/dev/sdb", O_RDWR)                = 6
    ioctl(6, BLKFLSBUF)                     = 0
    write(6, "\372\270\0\20\216\320\274\0\260\270\0\0\216\330\216\300\373\276\0|\277\0\6\271\0\2\363\244\352!\6\0"..., 512) = 512
    ioctl(6, BLKFLSBUF)                     = 0
    close(6)
    128.181352 +0.005542 commit()             ped_disk_commit_to_dev(lp_disk) returned true
    128.181475 +0.000122 commit_to_os()       start (lp_disk, timeout=10)  lp_disk->dev->path="/dev/sdb"
    128.181527 +0.000052 commit_to_os()       calling ped_disk_commit_to_os(lp_disk) ...
    open("/dev/sdb", O_RDWR)                = 6
    ioctl(6, BLKFLSBUF)                     = 0
    open("/sys/block/sdb/ext_range", O_RDONLY) = 7
    close(7)                                = 0

    KERNEL[1158935.380543] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    KERNEL[1158935.380565] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[1158935.380578] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)

    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=1, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=2, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=3, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ...

    KERNEL[1158935.380977] change   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb (block)
    KERNEL[1158935.381296] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    KERNEL[1158935.381367] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[1158935.381432] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    KERNEL[1158935.382992] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)

    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=62, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=63, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ioctl(6, BLKPG, {BLKPG_DEL_PARTITION, flags=0, datalen=152, data={start=0, length=0, pno=64, devname="", volname=""}}) = -1 ENXIO (No such device or address)
    ioctl(6, BLKPG, {BLKPG_ADD_PARTITION, flags=0, datalen=152, data={start=1048576, length=1073741824, pno=1, devname="/dev/sdb1", volname=""}}) = -1 EBUSY (Device or resource busy)
    write(2, "Error informing the kernel about"..., 251) = 251
    Error informing the kernel about modifications to partition
    /dev/sdb1 -- Device or resource busy.  This means Linux won't know
    about any changes you made to /dev/sdb1 until you reboot -- so you
    shouldn't mount it or use it in any way before rebooting.

    UDEV  [1158935.384641] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    UDEV  [1158935.390203] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    UDEV  [1158935.390243] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [1158935.462866] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [1158935.469207] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [1158935.471512] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    UDEV  [1158935.492173] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)

    write(2, "Failed to add partition 1 (Resou"..., 60) = 60
    Failed to add partition 1 (Resource temporarily unavailable)
    close(6)

    KERNEL[1158955.730960] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    KERNEL[1158955.731095] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[1158955.731314] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    KERNEL[1158955.731397] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    KERNEL[1158955.731817] change   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb (block)
    KERNEL[1158955.731981] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    KERNEL[1158955.732166] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[1158955.732232] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    KERNEL[1158955.733955] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)

    148.533154 +20.351627 commit_to_os()       ped_disk_commit_to_os(lp_disk) returned false

    UDEV  [1158955.738262] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    UDEV  [1158955.738460] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [1158955.738525] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)

    148.537648 +0.004494 execute_command()    udevadm settle --timeout=10

    UDEV  [1158955.740864] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [1158955.760192] change   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb (block)
    UDEV  [1158955.801211] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [1158955.815262] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [1158955.815314] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    UDEV  [1158955.828134] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)

    148.630797 +0.093149 execute_command()              exit status 0
    148.630882 +0.000085 commit_to_os()                 return false
    D: stop strace pid 18054.  Press [Return] to continue.
    ^Z
    [1]+  Stopped          ./gpartedbin /dev/sdb

    # kill %3
    strace: Process 18054 detached
    [3]-  Done             strace -p 18054 -e open,ioctl,write,close

    # kill %2
    [2]   Done             udevadm monitor

    # fg %1
    ./gpartedbin /dev/sdb
    173.700143 +25.069261 commit()             return false
    173.700470 +0.000327 create_partition()   return false

What happens is that GParted calls ped_disk_commit_to_dev() which opens
the device, writes the updated partition table and closes the device.
When the device closes the kernel initiates asynchronous uevents and
user space udev rules which remove and re-add all the partitions.  In
the mean time GParted calls ped_disk_commit_to_os() to inform the kernel
of the changes to the partition table.  This involves opening the
device, using ioctl() to remove all possible partitions [1] and re-add
needed partitions.  It finds partitions 1 to 3 already removed and
accepts this along with all other non-existent partitions up to 64.
When it tries to re-add partition 1 the ioctl() BLKPG_ADD_PARTITION call
returns EBUSY.  Presumably because the partition is in use by udev which
is in the process of running the user space rules associated with
removing and re-adding it.  Then ped_disk_commit_to_os() closes the
device which initiates a second round of asynchronous uevents and user
space udev rules removing and re-adding all the partitions again.

So in summary the kernel and udev are removing and re-adding the
partitions exactly when libparted is trying to do exactly the same
thing!

[1] The algorithm in libparted 3.1 is to try to remove all possible
    partitions, 64 for this kernel, followed by re-adding the needed
    partitions.

    parted/libparted/arch/linux.c:_disk_sync_part_table()
    http://git.savannah.gnu.org/cgit/parted.git/tree/libparted/arch/linux.c?h=v3.1#n2541

Partprobe has had exactly the same issue with failing to inform the
kernel about modifications to the partition table [2].  This was fixed
in libparted post v3.2 release by this commit [3].

[2] rhbz#1339705 - ceph-disk prepare: Error: partprobe /dev/vdb failed :
    Error: Error informing the kernel about modifications to partition
    /dev/vdb1 -- Device or resource busy.

    https://bugzilla.redhat.com/show_bug.cgi?id=1339705

[3] partprobe: Open the device once for probing

    Previously there were 3 open/close pairs for the device, which may
    result in triggering extra udev actions. Instead, open it once at
    the start of process_dev and close it at the end.

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=cfafa4394998a11f871a0f8d172b13314f9062c2

Implement the same fix as implemented for partprobe.  Hold a file handle
open which libparted can use internally to avoid having to open() and
close() the device itself twice, once for each of the calls
ped_disk_commit_to_dev() and ped_disk_commit_to_os().  This avoids the
first close() initiating the kernel and udev to remove and re-add the
partitions exactly when ped_disk_commit_to_os() is trying to do the same
thing.

Bug 790418 - "Unable to inform the kernel of the change" message may
             lead to corrupted partition table
2017-11-29 11:37:26 -07:00
Mike Fleetwood f9ae35584b Remove left behind #include "ProgressBar.h"
The includes were missed being removed by this earlier refactoring
commit which reduced direct access to the single ProgressBar object:
    b1313281bd
    Simplify use of the progress bar via OperationDetail (#760709)
2017-11-26 10:53:52 -07:00
Mike Fleetwood 8c5c13d613 Rename OperationDetailStatus STATUS_N_A to STATUS_WARNING
Make the enumeration name match it's use as indicating a warning.  Also
spell SUCCESS correctly.  Follow on to icon variable names too.
2017-11-26 10:53:52 -07:00
Mike Fleetwood 0688662055 Identify libparted messages as either success or error (#790842)
All libparted messages were reported as informational, even for a step
which failed.  Instead identify libparted messages as either
informational or errors depending on whether this step was successful
or not respectively.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:24 -07:00
Mike Fleetwood b4cd5da4f7 Set the status last for the OperationDetails in mk/rm_temp_dir() (#790842)
Resizing any unmounted file system which has to be mounted to be resized
triggered the new GParted bug message.  However the operation did
complete successfully.  Example operation details:

  Grow /dev/sdb8 from 1.00 GiB to 1.50 GiB                     (SUCCESS)
  * calibrate /dev/sdb
  * check file system on /dev/sdb8 for errors and (if possib...(SUCCESS)
  * grow partition from 1.00 GiB to 1.50 GiB                   (SUCCESS)
  * grow file system to fill the partition                     (SUCCESS)
    * mkdir -v /tmp/gparted-wvH0Ez                             (SUCCESS)
      * GParted Bug: Adding another child after no_more_chil...(ERROR)
      * Created directory /tmp/gparted-wvH0Ez
    * mount -v -t btrfs '/dev/sdb8' '/tmp/gparted-wvH0Ez'      (SUCCESS)
    * btrfs filesystem resize 1:max '/tmp/gparted-wvH0Ez'      (SUCCESS)
    * umount -v '/tmp/gparted-wvH0Ez'                          (SUCCESS)
    * rmdir -v /tmp/gparted-wvH0Ez                             (SUCCESS)
      * GParted Bug: Adding another child after no_more_chil...(ERROR)
      * Removed directory /tmp/gparted-wvH0Ez

This is because set_success_and_capture_errors() was called first and
the child details added after.  Reverse this ordering to fix.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:19 -07:00
Mike Fleetwood 2040aabd22 Transition other code to callback error collection (#790842)
Transition all remaining code, DMRaid and file system code, to use the
new method of reporting success of a step and automatic error
collection.  None of this code calls libparted so can't generate any
libparted exceptions.  This is just for consistency so all code follows
the same pattern using set_success_and_capture_errors().

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:18 -07:00
Mike Fleetwood bf1e87ecf3 Refactor ntfs resize code (#790842)
Refactor nested if-then-else into a sequence of if fail return early.
Makes the code simpler to understand and converts separate
OperationDetail::set_status() calls for success or error into a single
call using ternary conditional matching how it is or was done everywhere
else.  This is also ready for status and error capture refactoring.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:18 -07:00
Mike Fleetwood 76088a39de Transition code using libparted to callback error collection (#790842)
Transition GParted block copying code and partition manipulation code,
which uses libparted API, to the new method of reporting success of a
step and automatic error collection.  Libparted exceptions are now
reported with the step at which they occurred.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:18 -07:00
Mike Fleetwood 0a78c39856 Allow child OperationDetails to emit error capture callback too (#790842)
Just copies the callback into each newly added child detail.  As there
are no more uses of set_success_and_capture_errors() yet, libparted
errors are still only captured once at the top-level of each operation.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:18 -07:00
Mike Fleetwood 23939331f6 Capture libparted messages via callback at top-level only (#790842)
Replace the explicit adding of libparted exception messages with a
callback to do it instead, and fire the callback just once per operation
by only changing the very top-level OperationDetail to use the new
set_success_and_capture_errors().  Therefore this still produces exactly
the same operation details with libparted messages at the end of each
operation.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:53:18 -07:00
Mike Fleetwood 2ed7feb2f5 Add mechanism to capture exception messages into an OperationDetail (#790842)
All code implementing a step of an operation follows this pattern:

    od.add_child(OperationDetail("Step heading"));
    od.get_last_child().add_child(OperationDetail("More details"));
    // Do step
    success = ...
    od.get_last_child().set_status(success ? STATUS_SUCCESS
                                           : STATUS_ERROR);

At this point any libparted messages reported via exceptions need to be
added into the OperationDetail tree.  Also adding further children into
the tree after collecting those errors needs to be prohibited (as much
as the previous patch prohibited it).

Add a new method which will replace the final set_status() call above
like this which set the status, captures the errors and flags that
further children shouldn't be added:

    ...
    od.get_last_child().set_success_and_capture_errors(status);

It emits a callback to capture the errors to provide flexibility and so
that the OperationDetail class doesn't have to get into the details of
how GParted_Core saves libparted exception messages.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:52:54 -07:00
Mike Fleetwood f8f9a72de6 Add mechanism to stop adding more child OperationDetails (#790842)
Want functionality to prevent further child details being added to an
OperationDetail.  This is so that the captured libparted error messages
are always the last child in the list, and more details (at that point
in the tree) can't be added.

For example we want GParted to report like this:

  Move /dev/sdb3 to the right and shrink it from 1.14 GiB to...(SUCCESS)
  ...
  * shrink partition from 1.14 GiB to 1.00 GiB                 (SUCCESS)
    * old start: 4464640
      old end: 6856703
      old size: 2392064 (1.14 GiB)
    * new start: 4464640
      new end: 6561791
      new size: 2097152 (1.00 GiB)
    * libparted messages                                       (INFO)
      * DEBUG: GParted generated synthetic libparted excepti...

and not like this:

  Move /dev/sdb3 to the right and shrink it from 1.14 GiB to...(SUCCESS)
  ...
  * shrink partition from 1.14 GiB to 1.00 GiB                 (SUCCESS)
    * old start: 4464640
      old end: 6856703
      old size: 2392064 (1.14 GiB)
    * libparted messages                                       (INFO)
      * DEBUG: GParted generated synthetic libparted excepti...
    * new start: 4464640
      new end: 6561791
      new size: 2097152 (1.00 GiB)

So actually preventing the addition of more child details would stop
users seeing information they should see.  So instead just report a bug
message into the operation details.  This doesn't stop anything, but the
bug message will be seen and allow us to fix GParted.

So far nothing is enforced.  This patch just adds the mechanism to
report a bug when a new child detail is added when prohibited.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:06:37 -07:00
Mike Fleetwood 3c1fdd14f4 Also stop OperationDetail execution timer when setting status to N/A (#790842)
PATCH SET SUMMARY:

Libparted exception messages are reported into the operation details at
the end of each separate operation.  For operations which involve
multiple steps of partition manipulation there is no way to identify
which exceptions occurred with which steps.

Example resize/move operation in which multiple libparted exceptions
were raised:

  Move /dev/sdb to the right and shrink it from 1.15 GiB to ...(ERROR)
  * calibrate /dev/sdb3                                        (SUCCESS)
  * check file system on /dev/sdb3 for errors and (if possib...(SUCCESS)
    * e2fsck -f -y -v -C 0 '/dev/sdb3'                         (SUCCESS)
  * shrink file system                                         (SUCCESS)
    * resize2fs -p 'dev/sdb3' 1048576K                         (SUCCESS)
  * shrink partition from 1.14 GiB to 1.00 GiB                 (SUCCESS)
  * check file system on /dev/sdb3 for errors and (if possib...(SUCCESS)
    * e2fsck -f -y -v -C 0 '/dev/sdb3'                         (SUCCESS)
  * grow partition from 1.00 GiB to 1.12 GiB                   (SUCCESS)
  * move file system to the right                              (SUCCESS)
    * e2image -ra -p -O 134217728 '/dev/sdb3'                  (SUCCESS)
  * shrink partition from 1.12 GiB to 1.00 GiB                 (ERROR)
  * libparted messages                                         (INFO)
    * DEBUG: GParted generated synthetic libparted exception...
    * Error informing the kernel about modifications to part...
    * Error informing the kernel about modifications to part...
    * DEBUG: GParted generated synthetic libparted exception...
    * DEBUG: GParted generated synthetic libparted exception...

But there is no way to know which of the libparted steps: 1 calibrate or
3 partition resize steps encountered which exceptions.

Fix this by reporting the libparted messages into the operation details
at the point at which they occur.  Then the above example would become:

  Move /dev/sdb to the right and shrink it from 1.15 GiB to ...(ERROR)
  * calibrate /dev/sdb3                                        (SUCCESS)
  * check file system on /dev/sdb3 for errors and (if possib...(SUCCESS)
    * e2fsck -f -y -v -C 0 '/dev/sdb3'                         (SUCCESS)
  * shrink file system                                         (SUCCESS)
    * resize2fs -p 'dev/sdb3' 1048576K                         (SUCCESS)
  * shrink partition from 1.14 GiB to 1.00 GiB                 (SUCCESS)
    * libparted messages                                       (INFO)
      * DEBUG: GParted generated synthetic libparted excepti...
  * check file system on /dev/sdb3 for errors and (if possib...(SUCCESS)
    * e2fsck -f -y -v -C 0 '/dev/sdb3'                         (SUCCESS)
  * grow partition from 1.00 GiB to 1.12 GiB                   (SUCCESS)
    * libparted messages                                       (INFO)
      * Error informing the kernel about modifications to pa...
      * Error informing the kernel about modifications to pa...
      * DEBUG: GParted generated synthetic libparted excepti...
  * move file system to the right                              (SUCCESS)
    * e2image -ra -p -O 134217728 '/dev/sdb3'                  (SUCCESS)
  * shrink partition from 1.12 GiB to 1.00 GiB                 (ERROR)
    * libparted messages                                       (ERROR)
      * DEBUG: GParted generated synthetic libparted excepti...

THIS PATCH:

Small change so that setting the status of an OperationDetail to N/A,
warning, also stops the execution timer if it was running.  Matching
what happens when the status is set to either success or error.

This is to avoid having to set status twice, first time just to stop the
timer, and second time to set it to the desired status when reporting a
warning.

Bug 790842 - Report libparted messages into operation details at the
             point at which they occur
2017-11-26 10:06:37 -07:00
Mike Fleetwood 33a535390e Extract common code into new method get_lp_partition() 2017-10-03 08:22:19 -06:00
Mike Fleetwood 12dcebcb47 Calibrate whole disk device partitions again (#788308)
Fix up following switch from whole_device flag to TYPE_UNPARTITIONED.

Also calibrate the type for whole disk device partitions.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood bcd03e5c41 Restore Information dialog display of whole disk devices (#788308)
So they display as previously; as all grey in the graphic and with only
the correct attributes shown.

Fix up following switch from whole_device flag to TYPE_UNPARTITIONED.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood c1807ca504 Disallow formatting of unrecognised whole disk devices again (#788308)
Fix up following switch from whole_device flag to TYPE_UNPARTITIONED.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood b1967e7211 Allow Partition > New on unallocated whole disk devices again (#788308)
Fix up following switch from whole_device flag to TYPE_UNPARTITIONED.

Again use FS_UNALLOCATED to determine if a partition represents
unallocated space and creation of a new partition should be allowed.
This is so that trying to create a new partition on a whole disk device
shows the "No partition table found on device /dev/DEV" warning again.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood fa5ed1fb90 Select unallocated whole disk devices again (#788308)
After the change from whole_device flag to TYPE_UNPARTITIONED,
unallocated whole disk devices are no longer automatically selected
because the partition type is no longer TYPE_UNALLOCATED.  Fix by
checking for file system type FS_UNALLOCATED when identifying the
largest unallocated space.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood d5a20697c8 Display unrecognised whole disk devices as all grey again (#788308)
Following the switch from whole_device flag to TYPE_UNPARTITIONED,
unallocated space can be found in two partition types:
    TYPE_UNALLOCATED
    TYPE_UNPARTITIONED  (only when filesystem == FS_UNALLOCATED)

As the file system in both cases is FS_UNALLOCATED check for that when
determining whether a partition is unallocated or not.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood ea269cc929 Switch from whole_device flag to TYPE_UNPARTITIONED (#788308)
Remove whole_device flag and replace with new partition type
TYPE_UNPARTITIONED.  Minimally adapt the remaining code to compile and
run.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood 24123c4298 Update partition related checks in set_valid_operations() (#788308)
Update the partition can be named and partition flags can be managed
checks from being disallowed on unallocated partition types, to being
allowed on primary, logical and extended partition types.  This is in
preparation for the introduction of new unallocated partition type.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood 59185b133c Add FILESYSTEM_MAP[FS_EXTENDED] entry
Now when refreshing the devices, GParted_Core::read_label() calls
GParted_Core::get_fs() with parameter FS_EXTENDED.

Before this change, get_fs() would fail to find file system capabilities
set for FS_EXTENDED 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_EXTENDED and adds this
entry into the FILESYSTEMS vector.  Then get_fs() finds that not
supported capabilities set for FS_EXTENDED 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.

See this earlier commit doing the same thing:
    7870a92b80
    Add FILESYSTEM_MAP[FS_UNALLOCATED] entry
2017-10-03 08:22:19 -06:00
Mike Fleetwood 326df46af2 Remove some unnecessary extended partition checks from GParted_Core (#788308)
For example GParted_Core::read_label() is already called for empty
partitions where .filesystem = FS_UNKNOWN.  In that case get_fs()
returns an unsupported capability set with all capabilities set to
FS::NONE.  The same would be true extended partitions where .filesystem
= FS_EXTENDED too; get_fs() would return an unsupported capability set
with all capabilities set to FS::NONE.

Therefore the if not extended partition condition around the switch
statements is not necessary in GParted_Core::read_label(), read_uuid(),
label_filesystem() and change_uuid().  Remove.

This also achieves removal of some uses of partition type enumerations
in advance of the introduction of new partition type TYPE_UNPARTITIONED.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:19 -06:00
Mike Fleetwood 400864a65e Add and use Partition::set_unpartitioned() method (#788308)
PATCHSET OVERVIEW:

When unpartitioned drive read-write support was added this commit added
a whole_device flag:
   5098744f9a
   Add whole_device flag to the partition object (#743181)

Using a whole_device flags now seems not the correct way to model
unpartitioned drives.  GParted models an uninitialised drive as:
    .path         = _("uninitialized")
    .type         = TYPE_UNALLOCATED
    .whole_device = true
    .filesystem   = FS_UNALLOCATED
and a whole drive file system, using ext4 for example, as:
    .path         = "/dev/sdb"
    .type         = TYPE_PRIMARY
    .whole_device = true
    .filesystem   = FS_EXT4
No partitioning changed yet the type of the partition in the model
changed between TYPE_UNALLOCATED and TYPE_PRIMARY depending on whether
the whole drive contains a recognised file system or not.

The partition object describing a file system within a LUKS encryption
mapping is another case of the model not matching reality.
    .path         = /dev/mapper/crypt_sdb1_crypt
    .type         = TYPE_PRIMARY
    .whole_device = true
    .filesystem   = FS_EXT4
There is no partition table within the encryption mapping, the file
system fills it, but GParted records it as a primary partition.

Make TYPE_UNALLOCATED and TYPE_PRIMARY be reserved for representing
unallocated space and primary partitions within a partitioned disk drive
and introduce new TYPE_UNPARTITIONED for all cases of an unpartitioned
whole disk drive.

The GParted UI does differentiate between an unallocated whole disk
device and anything else by requiring a partition table to be created
first, even if that is just the loop partition table.  That
determination can simply look for the partition object containing file
system type FS_UNALLOCATED instead.

THIS PATCH:

Create set_unpartitioned() helper method to set a partition object to
represent a whole disk drive and use everywhere such an object is
modelled.  This matches what existing methods Set_Unallocated() and
indeed Set() do for unallocated space and any type of partition
respectively.

For now the partition type is still set to either TYPE_UNALLOCATED or
TYPE_PRIMARY so the rest of the code base remains the same.
TYPE_UNPARTITIONED will be introduced later.

Bug 788308 - Remove whole_device partition flag
2017-10-03 08:22:06 -06:00
Mike Fleetwood f5d3f97c7a Pass string literals directly to execute_command()
There were a few cases of creating a local string variable from a
literal and then passing the variable to execute_command() like this:
    Glib::ustring cmd = "whatever";
    Utils::execute_command( cmd, ... );

This creates an unnecessary local variable.  Instead pass the string
literal directly to Utils::execute_command() like this:
    Utils::execute_command( "whatever", ... );
This also make the code a little bit more grep friendly.
2017-09-25 09:25:17 -06:00
Mike Fleetwood 25d2aa341c Stop nicing external commands run by the DMRaid module (#788007)
No other commands run by GParted are niced, so stop nicing commands run
from the DMRaid module.

I think nicing of possibly long running file system modification
commands would have made virtually no difference because "nice -n 19"
lowered the CPU priority, but such command would be I/O bound.

History:

Nicing of file system modification commands was added by this commit
from 2006-08-21:
    82e6f6b132
    added nice -n 19, so that all extensive filesystem operations will be

Nicing of DMRaid operations was copied into the DMRaid module when it
was added here in 2009-03-14:
    5865c92dc0
    Added new class for dmraid support

Nicing was removed from file system modification commands with this
commit from 2013-02-22:
    52a2a9b00a
    Reduce threading (#685740)

Bug 788007 - Remove minor bits of legacy from DMRaid module
2017-09-25 09:25:17 -06:00
Mike Fleetwood a89d55a6f0 Remove backward compatibility for dmraid without -P option (#788007)
The -P option was first added to dmraid-1.0.0.rc15 released 2006-09-17
[1].  dmraid-1.0.0.rc16 (a later release) was included in Debian 6 and
Ubunbu 12.04 LTS.  dmraid-1.0.0.rc13 was included in RedHat/CentOS 5
however the -P option was back ported into RedHat/CentOS 5.1.

All of these distributions are so old as to be no longer supported and
yet they provided the dmraid -P option.  Therefore backward
compatibility for dmraid without the -P option is no longer required.
So remove it.

[1] old dmraid source code releases
    http://people.redhat.com/heinzm/sw/dmraid/src/old/

Bug 788007 - Remove minor bits of legacy from DMRaid module
2017-09-25 09:25:17 -06:00
Pali Rohár 8fdc2c21a6 Correctly quote and escape arguments passed to external commands (#787203)
Trying to set a file system label to (including the double quotes):
    " --help "
fails.  For example labelling an ext4 file system would try to run this
command:
    # e2label /dev/sdb1 "" --help ""
    Usage: e2label device [newlabel]
    # echo $?
    1

Alternatively trying to create a file system with a label of just a
double quote also fails.  The Applying Pending Operations dialog waits
forever and won't cancel or force cancel.  Have to use the window
manager close window button to close the dialog.  Also GParted reports
this error to the console:
    (gpartedbin:9648): glibmm-CRITICAL **:
    unhandled exception (type Glib::Error) in signal handler:
    domain: g-shell-error-quark
    code  : 0
    what  : Text ended before matching quote was found for ". (The text was 'mkfs.xfs -f -L """ /dev/sdb2')

Command strings are parsed and split into argv array by function
Glib::shell_parse_argv() which calls internal glib function
tokenize_command_line() for shell tokenization.  It expects the command
string to be properly quoted and escaped and after tokenization, calls
g_shell_unquote() on every parsed argument.  So to prevent constructing
incorrect commands, every non-static string needs to be properly quoted.

GParted only puts labels and mount points into double quotes, but has
not escaped special characters in those values itself.  This patch
fixes all these problems by using Glib::shell_quote() on all variable
values.  Labels, mount points, paths and all others too.

Probably a better solution would be to use a new function which takes
argv array instead of one string with all the, correctly quoted and
escaped, arguments concatenated together.

Bug 787203 - Correctly quote and escape arguments of external programs
             passed to execute_command()
2017-09-21 20:00:26 +01:00
Pali Rohár f422052356 Correctly quote and escape command line argument passed to "sh -c" (#787203)
Shell fragments must be properly quoted and escaped to prevent execution
of unintended commands derived from user controllable data.  For
example:

    $ printf '#!/bin/sh\necho test > out\n' > script.sh
    $ chmod +x script.sh
    $ truncate -s 20M 'jfs;script.sh'
    $ mkfs.jfs -q 'jfs;script.sh'
    $ ls -l out
    ls: cannot access out: No such file or directory

    $ sudo PATH=$PWD:$PATH /usr/local/bin/gparted 'jfs;script.sh'
    $ sudo PATH=$PWD:$PATH /usr/local/bin/gparted 'jfs;script.sh'

    $ ls -l out
    -rw-r--r-- 1 root root 5 Sep 12 23:11 out
    $ cat out
    test

What is happening is that jfs::set_used_sectors() is using the device
name 'jfs;script.sh' without quoting it and passing it to the shell to
execute like this:
    sh -c 'echo dm | jfs_debugfs jfs;script.sh'
which the shell duly executes as these two commands:
    echo dm | jfs_debugfs jfs
    script.sh

This could be a security related issue as "sh -c" is able to execute
arbitrary shell commands from the argument if if contains shell special
characters.  Use Glib::shell_quote() [1] to quote and escape file names
and whole commands passed to the shell.

[1] Glib::shell_quote(const std::string & unquoted_string)
    https://developer.gnome.org/glibmm/stable/group__ShellUtils.html
    "Quotes a string so that the shell (/bin/sh) will interpret the
    quoted string to mean unquoted_string.

    If you pass a filename to the shell, for example, you should first
    quote it with this function."

Bug 787203 - Correctly quote and escape arguments of external programs
             passed to execute_command()
2017-09-21 20:00:26 +01:00
Pali Rohár e8f0504b13 Make sure that FS_Info cache is loaded for all named paths (#787181)
Naming a file system image file on the command line is shown by GParted
as unknown.

    $ truncate -s 100M /tmp/fat.img
    $ mkfs.vfat /tmp/fat.img
    $ sudo ./gpartedbin /tmp/fat.img

Currently the FS_Info cache is loaded for all devices reported by
blkid (plus all whole disk devices identified from /proc/partitions even
if blkid reports nothing).  However file system images named on the
command line are not queried so GParted can't identify them.

Fix by ensuring that the FS_Info blkid cache is loaded for all named
devices, including named file system image files.

Note that Mount_Info::load_cache() depends on the contents of the
FS_Info cache to lookup UUID= and LABEL= device names from /etc/fstab.
However only file systems in block devices can be mounted like this, and
never file system image files, so the fact that the cache may be
extended afterwards by FS_Info::load_cache_for_paths() does not matter.

History

Prior to version 0.22.0, when unpartitioned drive support was added,
GParted could recognise some file system image files using loop
partition handling in libparted.  However libparted before version 3.2
reported the loop partition name as the whole disk device name appended
with "1" so all the query commands were provided a non-existent name to
use.  Therefore no file system usage or the label was displayed.

Bug 787181 - Fix detection of file system images
2017-09-12 19:10:20 +01:00
Pali Rohár c3ad49d9da Update list of prohibited fat label characters (#787202)
Add double quote (") to the list of prohibited FAT label characters,
previously missed [1][2].

Also add single quote (') because mlabel encoded it in a way that both
Windows and blkid don't understand, although mlabel can correctly decode
it itself.

    # export MTOOLS_SKIP_CHECK=1
    # mlabel ::"MIKE'S" -i /dev/sdf1
    # mlabel -s :: -i /dev/sdf1
     Volume label is MIKE'S (abbr=MIKE_S~1???)
    # blkid -o value -s LABEL /dev/sdf1
    MIKE_S~1???

    (8-bit characters in the above output have been replaced with
    question marks (?) just to keep this commit message as 7-bit ASCII).

Finally exclude ASCII control characters below SPACE (0x00 to 0x1F) as
they also cause mlabel to ask a question and wait for input in the same
way that prohibited characters do.  As discussed in the previous commit
[1] the only way to stop GParted waiting forever is to manually kill
mlabel with signal 9 (KILL).

    # mlabel ::"^A" -i /dev/sdf1
    Long file name "^A" contains illegal character(s).
    a)utorename A)utorename-all r)ename R)ename-all
    s)kip S)kip-all q)uit (aArRsSq):

[1] 584137b32b
    Remove prohibited characters from FAT16/32 labels (#755608)

[2] Microsoft TechNet: Label
    https://technet.microsoft.com/en-us/library/bb490925.aspx

Bug 787202 - Update list of prohibited fat label characters
2017-09-07 13:23:59 +01:00
Mike Fleetwood 73fe1dbf5c Support /etc/fstab using Unicode labels as mount points (#786502)
So far GParted is still loading the default non-reversible encoded
labels from blkid in the initial loading of the FS_Info module cache.
This encoded label is used to match LABEL=<label> when reading
/etc/fstab, via the get_path_by_label() call, so works for ASCII only
labels.  This prevents GParted enabling the "mount on >" partition menu
item when non-ASCII labels are used.

To fix this:
1) Stop reading the labels the wrong way.
   Via the blkid command used to initially load the FS_Info module cache
   and is subject to default non-reversible encoding of non-printable
   ASCII bytes.
2) Read all the labels the right way, but only when needed.
   Only when /etc/fstab file contains LABEL=<label> and
   get_path_by_label() is called, read all the labels from blkid without
   encoding them via run_blkid_update_cache_one_label().
3) Return label from the cache.
   get_label() returns the cached label, loading it into the cache first
   if needed with run_blkid_update_cache_one_label().

In the worst case scenario of having a LABEL=<label> in /etc/fstab blkid
will be run for every partition containing a recognised file system to
read the label.  On my desktop with 5 hard drives, 4  SWRaid arrays and
31 recognised file systems running 'blkid -o value -s LABEL ...' 31
times took 0.074 seconds of a total scan time of 9.072 seconds.  Less
that 1% of the total scanning time.  When LABEL=<label> is not used in
/etc/fstab individual blkid executions are only used to read labels for
file systems where there is no file system specific tool available
reducing the impact further.  Blkid itself caches the data in it's
blkid.tab cache file rather than reading all file systems on each
invocation.  Also the Linux file system cache will already contain the
blkid executable file, needed libraries files and the blkid.tab cache
file itself.  Hence why repeated execution of blkid is so fast.

Further to the updated comment in set_partition_label_and_uuid().
Matching LABEL=<label> from /etc/fstab uses the label obtained from
blkid run in the C locale so this kind of assumes it returns the label
correctly and it does for my limited testing on Unicode enabled
desktops.  Just not sure if it would be true for all cases in all
locales compared to the FS specific command run in the users default
locale.

Bug 786502 - Support reading Unicode labels when file system specific
             tools aren't available
2017-09-02 13:25:26 -06:00
Mike Fleetwood b022c1e3a9 Update FS_Info cache with Unicode safe labels read from blkid (#786502)
Move the code which reads the Unicode label from FS_Info::get_label()
into new function run_blkid_update_cache_one_label() which also replaces
the non-reversibly encoded copy loaded during the initial cache load.

This is mainly a bit of code refactoring ready for the following change.
It deliberately keeps the initial loaded labels so that reading
/etc/fstab and decoding LABEL=<label> to block special device names via
FS_Info::get_path_by_label() continues to works, at least for ASCII only
labels.

Bug 786502 - Support reading Unicode labels when file system specific
             tools aren't available
2017-09-02 13:25:26 -06:00
Mike Fleetwood 1c984ae06d Support reading Unicode character labels via blkid (#786502)
For file systems which don't provide a specific tool to read labels
(exfat, f2fs, hfs, hfsplus, udf and ufs) or when the tools aren't
installed for any other file system, reading labels falls back to using
the blkid command.  Blkid encodes non-ASCII bytes in it's output [1].
For example blkid reports a short Unicode label like this:

    # blkid /dev/sdb12
    /dev/sdb12: LABEL="M-PM-^ZM-PM->M-QM-^HM-PM-:M-PM-0" TYPE="hfsplus"

This shows encoding using 'M-' for bytes above 127 and caret '^' for
control characters.  Unfortunately neither 'M-' or '^' are encoded by
blkid so it is impossible to distinguish between the original label
containing either of these sequences or encoded non-printable bytes.
See this Bugzilla's bug 786502 entry for more details.  Unfortunately
this makes the default output format from blkid unsuitable for reading
Unicode character labels.

Instead instruct blkid to print values without encoding them using the
'-o value' option.  This just produces a list of new line separated
values without being able to identify which TYPE, UUID or LABEL belongs
to which device.  So query just the label for each block device one at
a time.  This method has the advantage of also working with all versions
of blkid, from version 1.0.0 (12-Feb-2003) in CentOS 5 to the latest
version 2.30.1 (20-Jul-2017) in Fedora 26.

    # blkid -o value -s LABEL /dev/sdb12 | hexdump -C
    00000000  d0 9a d0 be d1 88 d0 ba  d0 b0 0a           |...........|
    0000000b

(Using hexdump -C just so that this commit message only contains ASCII
characters).

Therefore this commit changes FS_Info::get_label() to query blkid as
shown above.  Note that GParted_Core::set_partition_label_and_uuid()
only calls get_label() when there is no file system specific tool
available (or the tool failed).

The FS_Info cache still contains copies of the labels subject to blkid
encoding, and that will be addressed in following commits.

[1] blkid.c v2.30.1 safe_print()
    https://git.kernel.org/pub/scm/utils/util-linux/util-linux.git/tree/misc-utils/blkid.c?h=v2.30.1#n111

Bug 786502 - Support reading Unicode labels when file system specific
             tools aren't available
2017-09-02 13:25:26 -06:00
Mike Fleetwood c353630312 Only execute mkudffs during detection when it is known to exist (#786050)
When udftools are not installed and the mkudffs program isn't found
GParted would report this error during startup:

    # ./gpartedbin
    ======================
    libparted : 3.1
    ======================
    Failed to execute child process "mkudffs" (No such file or directory)

Only run mkudffs to check for an old version when the program is found.

Bug 786050 - GParted reports failed to execute child process "mkudffs"
             when it is not installed
2017-08-10 10:04:20 -06:00
Mike Fleetwood a0c44d766d Refactor UDF label manipulation code (#784533)
Make especially the Volume Identifier length limit code simpler to
understand and therefore easier to maintain.

Bug 784533 - Add support for UDF file system
2017-07-30 09:50:09 +01:00
Pali Rohár 861bc8df5d Add support for long UDF labels and check for old versions of mkudffs (#784533)
UDF label is stored in the Logical Volume Identifier which has space for
either 126 Latin1 or 63 UCS-2 characters.  For compatibility reasons
with older versions of blkid, the possibly truncated UDF label is also
stored in the Volume Identifier which only has space for 30 Latin1 or 15
UCS-2 characters.

Because versions of mkudffs prior to 1.1 damage the label if it contains
non-ASCII characters, make sure GParted does not call such versions of
mkudffs with a non-ASCII character label.

Bug 784533 - Add support for UDF file system
2017-07-30 09:50:09 +01:00
Mike Fleetwood 62a7465fd2 Update AUTHORS file and Help > About > Credits
Provide credit for patch by Pali Rohár.
2017-07-10 19:17:05 +01:00
Mike Fleetwood 7feb84a219 Minor update of colour for exFAT
Colour for exFAT was a Sea Green.  This is close to Accent Green Dark
from the GNOME colour palette.  Use that instead.
2017-07-10 19:17:05 +01:00
Pali Rohár 5f327feb25 Add support for UDF file system (#784533)
Add support for detecting UDF file systems and formatting hard disks
with revision 2.01 UDF file systems using udftools.  Formatting optical
disks or any other media types is not supported yet.  Changing label or
UUID after formatting is not supported as the tools do not yet exist.

Bug 784533 - Add support for UDF file system
2017-07-10 19:17:05 +01:00
Pali Rohár 77ef2be089 Fix setting empty label when creating FAT16/32 file systems (#784564)
A FAT file system label in the partition boot sector should be set to
"NO NAME    " when the label is empty [1][2].  mkdosfs/mkfs.fat always
sets this to the label specified on the command line, even when the
label is blank [3].  Fix by not specifying the label when it is blank.

[1] The FAT File System, BIOS Parameter Block and Extended BIOS
    Parameter Block
    https://social.technet.microsoft.com/wiki/contents/articles/6771.the-fat-file-system.aspx#BPB_and_EBPB
[2] FAT16 File System, Volume Label
    http://www.maverick-os.dk/FileSystemFormats/FAT16_FileSystem.html#VolumeLabel
[3] mkfs.fat -n "" generates file system with invalid label
    https://github.com/dosfstools/dosfstools/issues/54

Bug 784564 - GParted calls mkfs.fat incorrectly when user does not specify label
2017-07-07 13:23:27 +01:00
Mike Fleetwood 59c8a7d6df Detect btrfstune change UUID capability again (#784467)
Btrfstune changed it's command line help output from being written on
stderr to being written on stdout in the following commit first included
in btrfs-progs 4.7.2 released 05-Sep-2016.  This breaks GParted
detection of the change UUID capability.  Found on the very latest
distributions: up to date Arch Linux and Fedora 26 Beta.  Fix this.

    https://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git/commit/?id=57d1cbd867311d99a2ae5e3cdcffd04
    btrfs-progs: print help test to stdout

Bug 784467 - No longer detecting btrfs change UUID capability
2017-07-03 09:21:50 -06:00
Mike Fleetwood 87ccf060ce Ensure internal detection requires second magic when needed (#783997)
Internal file system detection is broken for detection of LVM2 PVs
because it reports finding LVM2 PV even when only the first magic is
found.  Prepare a partition like this:
    # lvm pvcreate /dev/sdb1
    # hexdump -C /dev/sdb1 > hexdump-1.txt

Clear the second magic:
    # python
    f = open("/dev/sdb1","w")
    f.seek(0x218)
    f.write("\x00"*4)
    f.close()
    quit()

    # hexdump -C /dev/sdb1 > hexdump-2.txt
    # diff -u hexdump-[12].txt
     00000200  4c 41 42 45 4c 4f 4e 45  01 00 00 00 00 00 00 00  |LABELONE........|
    -00000210  58 69 83 e1 20 00 00 00  4c 56 4d 32 20 30 30 31  |Xi.. ...LVM2 001|
    +00000210  58 69 83 e1 20 00 00 00  00 00 00 00 20 30 30 31  |Xi.. ....... 001|
                                        ^^ ^^ ^^ ^^                       ^^^^
     00000220  52 4b 31 73 50 77 49 66  6a 72 55 4c 6a 4d 30 58  |RK1sPwIfjrULjM0X|

GParted still detects this as an LVM2 PV even though lvm and blkid do
not.

Correct logic error and only perform memcmp() with the second magic when
the second signature is non-NULL.

Bug 783997 - GParted detecting LVM2 PV regardless whether second magic
             matches or not
2017-06-20 10:03:31 -06:00
Mike Fleetwood 25780c611b Further improve speed of PipeCapture for non-watched output (#777973)
For large output a lot of time is used copying capturebuf to callerbuf
to provide a Glib::ustring copy of the buffer for the update callback.
However update callbacks are only used when commands are run to apply
operations by FileSystem::execute_command() and their output is
incrementally displayed in the UI.  Whereas update callbacks are never
used when commands are used to query information via
Utils::execute_command().

Stop performing interim copying of capturebuf to callerbuf when there
are no update callbacks registered as it is unnecessary.

Time to read portions of the recorded fsck.fat output via
fat16::set_used_sectors() and intermediate copies aren't required:

                     1 MiB     10 MiB   122 MiB
    old code :   0.074 sec   1.41 sec   210 sec [3:30]
    new code :   0.063 sec   0.56 sec     6.57 sec

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood 6f49f3049d Increase PipeCapture maximum read size to 64K (#777973)
For large output PipeCapture spends all it's time copying the capturebuf
to callerbuf to provide a consistent view for any registered update
callbacks.  This overhead is dependant of the size of the ever growing
captured output and the number of times OnReadable() is called.
Therefore increase the maximum read size to exponentially reduce this
overhead.

Time taken to read varying amounts of fsck.fat output with various
read buffer sizes:

                 1 MiB   10 MiB            122 MiB
    512b  :   0.60 sec   65 sec [1:05]   17262 sec [4:47:42]
    4096b :   0.19 sec   13 sec           2157 sec [  35:57]
    64K   :   0.07 sec    1.4 sec          210 sec [   3:30]

Note that this is only increasing the maximum size that can be read from
the output of the external command.  If the command produces it's output
slowly, such as the with progress reporting commands like mkfs.ext4,
then only the available number of bytes is read reporting the next
progress increment.  However if the command produces it's output
quickly, such as when testing this bug using a modified fsck.fat
concatenating the 122 MiB of pre-recorded output, then full buffer reads
are performed.

To ensure that a single call to OnReadable() couldn't block the UI too
long, the time taken for OnReadable() to process a full buffer of
various sizes was recorded as:

    512b  :   0.031 milliseconds
    4096b :   0.188 milliseconds
    64K   :   3.576 milliseconds

Adding this amount of processing time in the UI under normal
circumstances is not a problem.

As the captured output increases, the time taken by OnReadable() becomes
dominated by the time taken to copy the ever increasing capture buffer
to handle it's expansion and to copy it to the caller buffer for the
update callback.  At the end of the 122M captured fsck.fat output
OnReadable() takes 350 milliseconds per call.  This is not a problem
because this is an extreme case in which GParted is already hung and
increasing the buffer size is reducing the overall hang time from over
4 hours to a few minutes.

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood 8dbbb47ce2 Workaround g_utf8_get_char_validate() bug with embedded NUL bytes (#777973)
If PipeCapture reads a NUL byte in the middle of what is expected to be
a multi-byte UTF-8 character then PipeCapture either returns the
captured characters to the previous update or loops forever depending on
whether the end of the stream is encountered before the read buffer is
full or not.  This is equivalent to saying whether the NUL byte occurs
within the last 512 bytes of the output or not.

This is caused by a bug in g_utf8_get_char_validated() reporting that a
partial UTF-8 character has been found when the NUL byte is encountered
in the middle of a multi-byte character even though more bytes are
available in the length specified buffer.  g_utf8_get_char_validated()
is always stopping at the NUL byte assuming it is working with a NUL
terminated string.

Workaround this by checking for g_utf8_get_char_validated() claiming a
partial UTF-8 character has been found when in fact there are at least
enough bytes in the read buffer to instead determine that it is really
an invalid UTF-8 character.

Reference:
    Bug 780095 - g_utf8_get_char_validated() stopping at nul byte even
                 for length specified buffers
    https://bugzilla.gnome.org/show_bug.cgi?id=780095

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood 3a6a304c64 Workaround g_utf8_find_next_char() not incrementing past NUL char (#777973)
If PipeCapture reads a NUL character, a valid UTF-8 character, it causes
GParted to allocate all available memory and crash.  The while loop in
PipeCapture::OnReadable() loops forever reading the same NUL character
from readbuf because g_utf8_find_next_char() doesn't advance past it.
Hence an infinite number of NUL characters are added to the current
line, linevec.

Workaround this by checking for this failure case of
g_utf8_find_next_char() and increment past the NUL character.

This is actually a bug recently fixed in glib 2.49.3 released
2016-07-17.  References:

*   Bug 547200 - g_utf8_find_next_char() issues
    https://bugzilla.gnome.org/show_bug.cgi?id=547200

*   https://git.gnome.org/browse/glib/commit/?id=e0e652e4032a181d4f0b0a12aeddf0678b7a3c04
    Fix a corner-case in g_utf8_find_next_char

    In the case that *p is '\0', we should return p + 1, not p.
    This change allows to simplify g_utf8_find_next_char a bit.

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood b5b3d199f8 Improve the performance of PipeCapture for large output (#777973)
A user had a very corrupted FAT file system such that fsck.fat produced
122 MiB of output.  GParted has to read this output to get the file
system usage information.  However GParted takes more than 48 hours to
read the 122 MiB of output, while using 100% CPU time and is
unresponsive for the duration.

Modified fsck.fat to output just the first 1 MiB of output and used perf
to capture performance data from GParted reading that output:
    # perf -g -F 1999 -- ./gpartedbin
    # perf report --stdio
      67.84% Glib::ustring::replace      [4.23s]
      17.67% g_utf8_pointer_to_offset    [1.10s]
       8.48% g_utf8_offset_to_pointer    [0.53s]
    [  6.01% (everything else)       ]   [0.38s]
    [100.00% TOTAL                   ]   [6.24s]

And to read the first 10 MiB of output the performance figures are:
      92.95% Glib::ustring::replace      [257.44s]
       4.35% g_utf8_pointer_to_offset    [ 12.05s]
       2.13% g_utf8_offset_to_pointer    [  5.90s]
    [  0.58% (everything else)       ]   [  1.61s]
    [100.00% TOTAL                   ]   [277.00s]

See how the total time is increasing non-linearly, 44 times longer for
only 10 times as much data.  This is because of the exponential increase
in time spent in Glib::ustring::replace.

After a lot of experimentation I came to the conclusion that
Glib::ustrings are not appropriate for storing and editing large buffers
of data, sizes megabytes and above.  The issues are that iterators are
invalid after the content changes and replacing UTF-8 characters by
index gets exponentially slower as the size of the string increases.
Hence the > 48 hours of 100% CPU time to read and apply the line
discipline to the 122 MiB of fsck.fat output.  See code comment for a
more detailed description of the issues found.

Rewrote OnReadable() to use Glib::ustrings as little as possible.
Instead using buffers and vectors of fixed width data types allowing for
fast access using pointers and indexes (converted to pointers by the
compiler with simple arithmetic).  Again see code comment for a more
detailed description of the implementation.

Repeating the performance capture with the new code for the first 1 MiB
of fsck.fat output:
      63.34% memcpy                      [0.35s]
    [ 36.66% (everything else)       ]   [0.21s]
    [100.00% TOTAL                   ]   [0.56s]

And for the first 10 MiB of fsck.fat output:
      96.66% memcpy                      [63.60s]
    [  3.34% (everything else)       ]   [ 2.20s]
    [100.00% TOTAL                   ]   [65.80s]

Simple timings taken to read portions of the fsck.fat output (when not
using perf):

                   1 MiB    10 MiB      122 MiB
    old code :   6.2 sec   277 sec   > 48 hours
                            (4:37)
    new code :   0.6 sec    66 sec    17262 sec
                            (1:06)    (4:47:42)

Performance of the code is still non-linear because of the assignment
of the ever growing capturebuf to callerbuf for every block of input
read.  This is required to generate a consistent Glib::ustring copy of
the input for the update callback.  However this is much faster than
before, and I have a plan for further improvements.

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood a233e30efe Move initial clearing of output capture buffers into PipeCapture class (#777973)
Seems more logical to initially clear the output capture buffer in a
single location in the PipeCapture class which reads the command output
into said buffer, rather than each calling site before the PipeCapture
objects are constructed.

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood a03735ac6f Prevent core dump reading none UTF-8 data from external command (#777973)
A user had a very corrupted FAT file system and when GParted was reading
the file system usage it would core dump.  The tip of the stack trace
was:

    #0 Glib::ustring_Iterator<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::operator++()
       at /usr/include/glibmm-2.4/glibmm/ustring.h line 957
    #1 Glib::ustring_Iterator<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::operator++(int)
       at /usr/include/glibmm-2.4/glibmm/ustring.h line 965
    #2 GParted::PipeCapture::OnReadable(Glib::IOCondition)
       at PipeCapture.cc line 63
    #3 GParted::PipeCapture::_OnReadable(_GIOChannel*, GIOCondition, void*)
       at PipeCapture.cc line 45

GParted uses 'fsck.fat -n -v /dev/PTN' to get the file system usage.
However because the file system was so corrupted it was reporting every
file name as being damaged, including the file names being reported as
binary data.  PipeCapture::OnReadable() reads up to 512 bytes at a time
and then uses a Glib::ustring iterator to loop over the UTF-8
characters, but a Glib::ustring iterator is explicitly not capable of
reading binary data [1].  With invalid UTF-8 bytes the code continued to
read beyond the end of the string until GParted crashed with a
segmentation violation.

Fix by accessing the read string by index instead of by iterator.

[1] Quote from the Glib::ustring_Iterator<T> Class Template Reference:
    https://developer.gnome.org/glibmm/stable/classGlib_1_1ustring__Iterator.html

    "The Glib::ustring iterated over must contain only valid UTF-8 data.
    If it does not, operator++(), operator--() and operator*() may make
    accesses outside the bounds of the string."

Bug 777973 - Segmentation fault on bad disk
2017-06-02 11:47:35 -06:00
Mike Fleetwood 7c51b62958 Remove unused build definition GPARTED_DATADIR
Definition has been unused since this commit first released in GParted
0.5.0:
    c2d19a8ab4
    Replace gnome-open with gtk_show_uri (#600046)
2017-06-02 10:47:35 -06:00
Mike Fleetwood 69d1bbcf8f Remove old .cvsignore files
As the source code is managed in GIT and there is a .gitignore file in
the top level directory specifying file names to exclude from version
control, then the old per-directory .cvsignore files for CVS are
redundant.

Add the only missing and applicable entry from src/.cvsignore of '.libs'
to .gitignore and remove all the .cvsignore files.
2017-06-02 10:47:35 -06:00
Mike Fleetwood e0390d9cd8 Update comment with example LUKS_Info cache entry
Reflect the change made to the data model in commit:
    Use BlockSpecial in LUKS_Info module cache (#767842)
    7cd574cac5
2017-05-21 09:44:20 -06:00
Mike Fleetwood 6798c271c7 Quote mount point in Data Rescue dialog (#782681)
As before always quote use of mount points in command lines.

Bug 782681 - btrfs partitions mounted with whitespace cannot be resized
2017-05-21 09:44:20 -06:00
Mike Fleetwood 2025581029 Quote mount point when copying and resizing xfs (#782681)
Attempting to grow an already mounted xfs where the mount point
contained spaces failed like this:

    Grow /dev/sdb4 from 1.00 GiB to 1.50 GiB
    + calibrate /dev/sdb4
    + grow partition from 1.00 GiB to 1.50 GiB
    + grow file system to fill the partition
      + xfs_growfs /tmp/File System Label
        Usage: xfs_growfs [options] mountpoint
        ...

Apply the rule and quote all uses of mount points within command lines.
This is also applied to copying xfs file systems even though it was safe
because it only ever used GParted generated mount points.

Also for the xfs copy operation switch unmounting of partitions to
specify mount points instead of partitions.  This is just to be
consistent with how it is done in all the online file system resizing
code.

Bug 782681 - btrfs partitions mounted with whitespace cannot be resized
2017-05-21 09:44:20 -06:00
Mike Fleetwood 988dacfb1b Quote mount point when resizing nilfs2 (#782681)
The current nilfs2 resizing code is safe because it never passes a user
influenced mount point into a command line.  Regardless, apply the same
simple rule to always quote mount points when used in command lines.

WARNING:
Nilfs-resize is broken and can't actually resize a file system mounted
with spaces in the mount point anyway!

    # mkdir "/tmp/File System Label"
    # mount -v -t nilfs2 /dev/sdb3 "/tmp/File System Label"
    mount.nilfs2: started nilfs_cleanerd
    # nilfs-resize -v -y /dev/sdb3
    Error: cannot open NILFS on /dev/sdb3.
    # echo $?
    1
    # strace nilfs-resize -v -y /dev/sdb3
    ...
    stat("/dev/sdb3", {st_mode=S_IFBLK|0660, st_rdev=makedev(8, 19), ...}) = 0
    open("/dev/sdb3", O_RDONLY)             = 3
    ...
    open("/proc/mounts", O_RDONLY)          = 4
    read(4, "sysfs /sys sysfs rw,seclabel,nos"..., 1024) = 1024
    ...
    close(4)                                = 0
    open("/tmp/File\\040System\\040Label", O_RDONLY) = -1 ENOENT (No such file or directory)
    ...
    # fgrep /dev/sd /proc/mounts
    /dev/sda3 / ext4 rw,seclabel,relatime,data=ordered 0 0
    /dev/sda1 /boot ext4 rw,seclabel,relatime,data=ordered 0 0
    /dev/sdb3 /tmp/File\040System\040Label nilfs2 rw,relatime 0 0

So it looks like nilfs-resize (or a library it uses) can't decode the
octal characters '\040' used to encode spaces in the mount point as
reported in /proc/mounts.

Bug 782681 - btrfs partitions mounted with whitespace cannot be resized
2017-05-21 09:44:20 -06:00
Mike Fleetwood 3a10e30c3a Quote mount point when resizing jfs (#782681)
Attempting to grow an already mounted jfs where the mount point
contained spaces failed like this:

    Grow /dev/sdb2 from 1.00 GiB to 1.50 GiB
    + calibrate /dev/sdb2
    + grow partition from 1.00 GiB to 1.50 GiB
    + grow file system to fill the partition
      + mount -v -t jfs -o remount,resize /dev/sdb2 /tmp/File System Label
        Usage:
        mount [-lhV]
        mount -a [options]
        mount [options] [--source[ <source> | [--target] <directory>
        mount [options] <source> <directory>
        mount <operation> <mountpoint> [<target>]
        ...

Apply the rule to always enclose mount points within double quotes
within command lines.

Bug 782681 - btrfs partitions mounted with whitespace cannot be resized
2017-05-21 09:44:20 -06:00
Mike Fleetwood 618c1a202d Quote mount point when resizing btrfs (#782681)
A user had a btrfs file system mounted by automounter on a mount point
like "/mount/$USER/File System Label" which included white space
characters.  Resizing the file system while online failed like this:

    Grow /dev/sdb1 from 1.00 GiB to 1.50 GiB
    + calibrate /dev/sdb1
    + grow partition from 1.00 GiB to 1.50 GiB
    + grow file system to fill the partition
      + btrfs filesystem resize 1:max /mount/USER/File System Label
          btrfs filesystem resize: too many arguments
          usage: btrfs filesystem resize [devid:][+/-]<newsize>[kKmMgGtTpPeE]|[devid:]max <path>

So mount points not created by GParted should be considered under user
control and need quoting when used as parameters in command lines.
Strictly speaking, mount points created by GParted itself, by
FileSystem::mk_temp_dir(), are safe and don't need quoting.  However it
is simpler and safer just to quote all uses of mount points in command
lines, rather than risk missing some.

Bug 782681 - btrfs partitions mounted with whitespace cannot be resized
2017-05-21 09:44:20 -06:00
Mike Fleetwood 75131d85a5 Fix snap-to-alignment of operations creating partitions (#779339)
Using the default MiB alignment, creating an MSDOS logical partition
between two other existing logical partitions fails with this error
dialog:

    (-) <b>An error occurred while applying the operations</b>
        See the details for more information.
        <b>IMPORTANT</b>
        If you want support, you need to provide the saved details!
        See http://gparted.org/save-details.htm for more information.
                                                               [ OK ]

and these operation details:

    + libparted messages
      - Unable to satisfy all constraints on the partition.

This bug was introduced by this commit included in GParted 0.23.0:
    90e3ed68fc
    Shallow copy Device object into Operation object (#750168)

The commit message claimed that the deep copied Partition objects inside
the Device inside the Operation object are never accessed.  This turned
out not to be true.  Win_GParted::Add_Operation() uses them as part of
snap_to_alignment() which updates requested partition boundaries to
account for alignment requirements and the space needed for EBR
(Extended Boot Record) preceding logical partitions.

In this case the new logical partition was trying to be created over the
top of the EBR for the following logical partition because
snap_to_alignment() wasn't aware of its existence.

Fix by making Add_Operation() and snap_to_alignment() refer to the
current device, as displayed in the UI, rather than the shallow copy
included in the Operation object.  Hopefully now it is true that the
not copied vector of Partition objects in the Device object in each
Operation object are never accessed.

Bug 779339 - enforce at least 1 MiB "free space following"
2017-04-23 08:57:25 -06:00
Curtis Gedak 4a0931c50d Restore ability to grow primary w/unallocated space before extended (#778700)
A regression which prevented growing a primary partition that had
unallocated space between it and the following extended partition was
introduced with the following commit:

  Create and use general find_extended_partition() function
  aa98107706

To fix the regression, restore the logic that checked for a logical
partition before seeking the index of the extended partition.

Bug 778700 - Unable to grow partition even though unallocated space is
             adjacent
2017-02-17 12:40:46 +00:00
Refael Sheinker 925e505f77 Make the Name Partition dialog a bit bigger (#778003)
Increase the size of the Name Partition dialog, matching the change made
to the Label File System dialog in the previous commit.  The code for
the Name Partition dialog was basically copied from the Label File
System dialog.

Bug 778003 - The "Label File System" dialog is too small
2017-02-14 18:59:23 +00:00
Refael Sheinker d1ce653d1b Make "Label File System" dialog a bit bigger (#778003)
On Arch Linux with XFCE 4.12 and Fedora 24 with GNOME 3.20 and later;
the Label File System dialog is too small.  The problem is that the
label entry box clips the Cancel and OK buttons.

Stop specifying the dialog height, instead letting it fit the combined
height of all the widgets automatically.

Also make the dialog wider and the label entry box wider so that longer
device names can be shown in the title before they are truncated.

Bug 778003 - The "Label File System" dialog is too small
2017-02-14 18:59:23 +00:00
Curtis Gedak ad9aaa1dc3 Update copyright year 2017-02-14 10:00:41 -07:00
Mike Fleetwood 38857e09c7 Improve error message in check_repair_filesystem() (#774818)
Change the error message after it raised a question with the translators
and to better align with the operation only being described as a check
in the GParted Manual and in the application UI.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-02-07 12:31:49 -07:00
Piotr Drąg 2258b70cd9 Fix translator comments
They need to be exactly one line above a string to show up in .po files.
2017-01-28 01:04:42 +01:00
Mike Fleetwood aa98107706 Create and use general find_extended_partition() function
The Operation class already provided find_index_extended() method and
 was used in the Operation and derived classes where required.  It
returns the index to the extended partition in the PartitionVector
object, or -1 when no extended partition exists.

There were several cases of the same functionality being open coded in
GParted_Core and Win_GParted.  Therefore move the implementation to
find_extended_partition() in PartitionVector compilation unit and use
this implementation everywhere.
2017-01-14 08:49:58 -07:00
Mike Fleetwood 786a53b43c Replace 2 Win_GParted member variables with local variables
Member variables fs and gpart_output were used in just one method each
as local variables.  Replace them with local variables.
2017-01-14 08:49:58 -07:00
Mike Fleetwood fbd39b81e3 Fix for loop limit in fat16::sanitize_label()
Use the same uppercase_label string to determine the limit of the for
loop as is actually accessed.
2017-01-14 08:49:58 -07:00
Mike Fleetwood 0f76b8f8ff Remove unused clear_mountpoints parameter from add_mountpoint*()
The clear_mountpoints parameter has never been used since
add_mountpoint*() were first added [1][2].  clear_mountpoints() method
[3] is available to provide this functionality and used.  Therefore
removed unused parameter and code.

[1] add_mountpoints() added 2006-03-15
        9532c3cad1
        Made Partition::mountpoints private

[2] add_mountpoint() added 2011-12-16
        208083f11d84dbd4f186271a3cdbf5170db259f8b8
        Display LVM2 VGNAME as the PV's mount point (#160787)

[3] clear_mountpoint() added 2006-03-19
        ad9f2126e7
        fixed issues with copying (see also #335004) cleanups + added FIXME added
2017-01-14 08:49:58 -07:00
Mike Fleetwood 34185afbf1 Document new requirement on the cryptsetup command (#774818)
Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 5cd1f718a1 Add "Available online only" to the Supported Actions legend (#774818)
Shrinking LUKS encryption is only possible while the mapping is open and
active.  Therefore the File System Support dialog shows Cross + Tick for
this operation.  Add this new combination to the legend.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 86597b8672 Use virtual get_filessytem_string() in remaining operation descriptions (#774818)
Switch the remaining create and delete operation description generation
to use the virtual Partition get_filesystem_string() method.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood f1e3d42b56 Prevent deletion of open LUKS mappings (#774818)
At least until closing LUKS mappings is supported.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 36804b9634 Implement maximize encryption volume as part of check repair operation (#774818)
Now that resizing of encrypted file systems is implemented add growing
of the open LUKS mapping as part of the check repair operation.

Resizing an encrypted file system requires the LUKS mapping to be open
to access the file system within; therefore it also requires libparted
and kernel support for online partition resizing.  This limits resizing
to the latest distributions with libparted >= 3.2 and kernel >= 3.6.
However growing an open LUKS mapping as part of a check repair operation
doesn't require resizing the partition.  Therefore route via offline
grow of LUKS to avoid those extra, unnecessary requirement.  This does
mean that offline LUKS grow artificially requires cryptsetup, but that is
not really significant as even opening LUKS requires cryptsetup.

So now checking an encrypted file system on even the oldest
distributions does:
1) runs FSCK on the encrypted file system;
2) grows the encryption volume to fill the partition;
3) grows the file system to fill the encryption mapping.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood e2aff7ba66 Implement offline grow of encryption volumes (#774818)
While a device-mapper encryption mapping can only be resized while
active, a LUKS volume can inherently be grown while offline because it
doesn't store a size and when started fills the partition.  This doesn't
even need the cryptsetup command to do the resizing (just to open the
LUKS volume afterwards which GParted doesn't yet support).  Implement
offline growing of LUKS volumes.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood a1c1401285 Add bug checks into resize/move operation methods (#774818)
Ensure pre-conditions of always or never being passed a Partition object
containing an open LUKS encryption are met for resizing file system and
LUKS encryption methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 828f0d8ab3 Implement resize/move operation of encrypted file systems (#774818)
Moving of closed LUKS is simply enabled by luks .move capability being
set and requires no further coding.

Resizing of encrypted file systems requires both the LUKS mapping and
encrypted file system within to be resized in the right order for both
shrinking and growing.  To keep the code simple split resizing of plain
and encrypted into separate functions.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood ee1b2257d2 Preview resize/move operation of encrypted file systems (#774818)
Changing the Resize/Move dialog code to also handle PartitionLUKS
objects was considered too complicated.  Instead create an unencrypted
equivalent using clone_as_plain(), pass that to the Resize/Move dialog
and finally apply the change back using Partition*::resize().

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 08e4ba4eca Add Partition object resizing method Partition*::resize() (#774818)
Add a resize() method to both Partition and PartitionLUKS classes.  They
take a reference Partition object, and update the position, size and
file system usage of *this Partition to match.  This is ready for taking
a partition returned from Resize/Move dialog and applying the change.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 30a0f4506c Add specialist clone method PartitionLUKS::clone_as_plain() (#774818)
Implement a specialist PartitionLUKS clone method.  Creates a new
Partition object which has the same space usage as the source encrypted
file system, but is a plain file system.  Namely, the overhead of the
LUKS header has been added to the file system usage.  This is ready for
feeding this representation of the partition to the Resize/Move dialog.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood e2c70d5639 Enable resize/move for encrypted file systems (#774818)
A partition containing LUKS encryption can only be moved when closed and
the Device Mapper encryption mapping only exists to be resized when
open.  As GParted can't yet open or close LUKS encryption these
restrictions have to be adhered to when composing operations.  Also as
encrypted partitions are only being resized when open, additionally
libparted and the kernel have to both be capable of resizing a partition
while in use.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 89540fedd8 Add bug checks into copy operation methods (#774818)
Ensure pre-condition of never being passed a Partition object containing
an open LUKS encryption mapping is met for copy operation related
methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood e31fa783a8 Display LUKS copy capability (#774818)
Users will expect to see that copying of LUKS is available in the File
System Support dialog, even if technically what is implemented is
copying of the file system within an open encryption mapping.  There is
no other reason to do this change as these two previous commits have
fully enabled copying of encrypted content:
    Implement copy operation of encrypted file systems (#774814)
    Preview copy operation of encrypted file systems (#774818)

Set LUKS .copy capability so that the dialog shows copying availability,
but then disallow copying of closed LUKS.  (Checking for the capability
and performing copying the content of an open LUKS encryption mapping is
inherent in Win_GParted::set_valid_operations() and GParted_Core::copy()
in the way that they access the block device containing the file system,
whether encrypted or not).

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 56859e1d6d Implement copy operation of encrypted file systems (#774814)
Implement the copy operation by making the copy code work with the
Partition object directly containing the file system, instead of the
enclosing PartitionLUKS object containing the LUKS encryption mapping.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 4c70ec3aee Preview copy operation of encrypted file systems (#774818)
Implement composing of the copy paste operation for encrypted file
systems.

Copying a closed LUKS partition would duplicate the LUKS header
containing the UUID, passphrase and master encryption key.  From a
security point of view having additional copies of encrypted data with
the same master key is an extra risk, but it all depends on what is
going to happen with that copy.  The Cryptsetup FAQ [1] talks about how
to make a backup at the file system level and block level, preferring
file system level with separate encryption if needed.  It strongly
recommends separate encryption if the copy is removable or going
off-site [2].  Also in the case of cloning the data, cloning the LUKS
container is strongly discouraged [3].

Therefore copying of encrypted file systems will be implemented by
copying the file system inside an open LUKS encryption mapping and not
by copying a closed LUKS partition.

Also, while creating new LUKS encryption is not yet supported, copying
an encrypted file system into a new partition will not be permitted as
that will always decrypt the data.  An encrypted file system will be
allowed to be copied into an existing plain partition, decrypting the
data, or into an existing open encrypted partition, keeping it
encrypted.  Pasting over the top of a closed encrypted partition will
remove the LUKS encryption.  (This is planned to be removed when
creating and removing LUKS encryption is implemented as part of full
LUKS read-write support).

Remember that when pasting into an existing partition the file system
must fit within the available space and that encryption has overhead
from the LUKS header.  Therefore copying from a plain partition into a
partition of the same size with open an encryption mapping will not fit
for space reasons.

[1] The Cryptsetup FAQ, Backup and data Recovery section
https://gitlab.com/cryptsetup/cryptsetup/wikis/FrequentlyAskedQuestions

[2] 6.7 Does a backup compromise security?
"If you do network-backup or tape-backup, I strongly recommend to go the
filesystem backup path with independent encryption, as you typically
cannot reliably delete data in these scenarios, especially in a cloud
setting."

[3] 6.15 Can I clone a LUKS container?
"You can, but it breaks security, because the cloned container has the
same header and hence the same master key.  You cannot change the master
key on a LUKS container, even if you change the passphrase(s), the
master key stays the same. That means whoever has access to one of the
clones can decrypt them all, completely bypassing the passphrases.

The right way to do this is to first luksFormat the target container,
then to clone the contents of the source container, with both containers
mapped, i.e. decrypted.  You can clone the decrypted contents of a LUKS
container in binary mode, although you may run into secondary issues
with GUIDs in filesystems, partition tables, RAID-components and the
like. These are just the normal problems binary cloning causes.

Note that if you need to ship (e.g.) cloned LUKS containers with a
default passphrase, that is fine as long as each container was
individually created (and hence has its own master key). In this case,
changing the default passphrase will make it secure again."

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 35d57011e9 Add bug checks into format operation methods (#774818)
Ensure pre-condition of never being passed a Partition object containing
an open LUKS encryption mapping is met for format operation related
methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood a568e5365a Implement format operation on encrypted file systems (#774818)
Formatting an existing encrypted partition will format the file system
within the encrypted mapping.  Formatting over the top of a closed
encrypted partition will remove the encryption.  (The latter is planned
to be prevented when creating and removing LUKS encryption is
implemented as part of full LUKS read-write support).

Composing the format operation inside an open LUKS encryption mapping
also has to account for the size of that mapping and construct a
PartitionLUKS object containing the new file system.  Implementing the
operation itself is as simple as passing the Partition object directly
containing the file system, instead of the outer PartitionLUKS object.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 88136c96d7 Extend functions generating encrypted file system string (#774818)
Already have:
    Utils::get_filesystem_string(FS_EXT2)          -> "ext2"
    virtual Partition::get_filesystem_string()     -> "ext2"
    virtual PartitionLUKS::get_filesystem_string() -> "[Encrypted] ext2"
Add these:
    Utils::get_encrypted_string()                  -> "[Encrypted]"
    Utils::get_filesystem_string(false, FS_EXT2)   -> "ext2"
    Utils::get_filesystem_string(true, FS_EXT2)    -> "[Encrypted] ext2"

This is ready for use of Utils::get_filesystem_string(true, FS_EXT2)
when composing the preview of a format of an encrypted file system by
Win_GParted::activate_format().

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 112ddef9df Implement mount/umount of encrypted file systems (#774818)
Adapts the swapon/swapoff, activate/deactivate Volume Group and mount/
unmount file system code to work with the Partition object directly
containing the file system.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 313c5fcb1c Add bug checks into check operation methods (#774818)
Ensure pre-condition of never being passed a Partition object containing
an open LUKS encryption mapping is met for check operation related
methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood df05a9785f Implement check operation on encrypted file systems (#774818)
Just update the code describing and implementing the check operation.
No need to update the code composing the operation in
Win_GParted::activate_check() as that is incredibly simple, just cloning
the Partition object and doesn't need changing.

Note that for encrypted file systems this does:
1) runs FSCK on the encrypted file system;
2) grows the file system to fill the encrypted mapping.
At this time it does not grow the encryption mapping to fill the
partition.  That will be added after resize/move has been implemented.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 49c0564601 Add bug checks into label file system operation methods (#774818)
Ensure pre-condition of never being passed a Partition object containing
an open LUKS encryption mapping is met for label file system operation
related methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood ff4ac89ba2 Implement label operation on encrypted file systems (#774818)
Again, just need to change the code when composing, describing and
implementing the operation to query and set the Partition object
directly containing the file system, instead of the enclosing encryption
mapping to make it work.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 8e41a5f712 Display encrypted path in the calibration step (#774818)
For an encrypted file system the calibrate step now looks like:

    + calibrate /dev/sdb4
        path: /dev/sdb4 (partition)
        start: 6293504
        end: 8390655
        size: 2097152 (1.00 GiB)
        encryption path: /dev/mapper/sdb4_crypt

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 8984ccaf23 Add bug checks into change UUID operation methods (#774818)
Ensure pre-condition of never being passed a Partition object containing
an open LUKS encryption mapping is met for change UUID operation related
methods.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 3ba7128d55 Implement new UUID operation on encrypted file systems (#774818)
When composing, describing and implementing the operation just need the
code to query and set the Partition object directly containing the file
system, instead of the enclosing encryption mapping to make it work.

The operation details for setting a new UUID on an encrypted ext4 file
system become:

    Set a new random UUID on [Encrypted] ext4 file system on /dev/sdb4
    + calibrate /dev/sdb4
    + Set UUID on /dev/mapper/sdb4_crypt to a new, random value
      + tune2fs -U random /dev/mapper/sdb4_crypt
          tune2fs 1.41.12 (17-May-2010)

Also note the now documented rule in apply_operation_to_disk() which
says each operation must leave the status of the encryption mapping and
file system as it found it.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood bd6fc67afb Provide virtual Partition::get_filesystem_string() method (#774818)
Provide and use a single interface for getting the file system string
for display, regardless of whether the partition is encrypted or the
encryption mapping is active or not.

Example return values for get_filesystem_string() for different types
and states of Partition objects:
1)  Plain ext4 file system:          -> "ext4"
2)  Closed encrypted:                -> "[Encrypted]"
3)  Open encrypted ext4 file system: -> "[Encrypted] ext4"

This simplifies the code in TreeView_Detail::create_row() which sets the
file system type displayed in the main window.  The same method will
then also be used when setting the operation description as each
operation is updated to handle encrypted file systems.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood aa49b763e4 Remove virtual PartitionLUKS::get_filesystem_label() (#774818)
The previous commit changed how the code behind the main window
retrieved the file system label for display.  This is the relevant
changes in TreeView_Detail::create_row():

  + const Partition & filesystem_ptn = partition.get_filesystem_partition();
    ...
  - Glib::ustring temp_filesystem_label = partition.get_filesystem_label();
  + Glib::ustring temp_filesystem_label = filesystem_ptn.get_filesystem_label();
    treerow[treeview_detail_columns.label] = temp_filesystem_label;

In the case of an encrypted file system get_filesystem_label() is now
called on the Partition object directly rather than on the outer
Partition object containing the LUKS encryption.

The code behind the Information dialog always obtained and used the
Partition object directly containing the file system to call
get_filesystem_label() since read-only LUKS support was added.
Therefore the virtualised PartitionLUKS::get_filesystem_label() is no
longer needed, so remove it.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 53fd80e6ca Provide virtual Partition::get_filesystem_partition() method (#774818)
There are multiple cases of code wanting to work with the Partition
object directly containing the file system, regardless of whether it is
within a PartitionLUKS object or not.  The code had to do something
similar to this to access it:

    const Partition * filesystem_ptn = &partition;
    if ( partition.filesystem == FS_LUKS && partition.busy )
            filesystem_ptn = &dynamic_cast<const PartitionLUKS *>( &partition )->get_encrypted();
    ...
    // Access Partition object directly containing the file system
    filesystem_ptn-> ...

Implement and use virtual accessor get_filesystem_partition() which
allows the code to be simplified like this:

    const Partition & filesystem_ptn = partition.get_filesystem_partition();
    ...
    // Access Partition object directly containing the file system
    filesystem_ptn. ...

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood 2be136b9fd Display busy status of the file system within LUKS encryption (#774818)
In the main window, display the busy status of the file system only
according to whether it is mounted or not, ignoring the status of the
encryption mapping.

Bug 774818 - Implement LUKS read-write actions NOT requiring a
             passphrase
2017-01-14 08:49:58 -07:00
Mike Fleetwood b2190372d0 Ensure blkid FS_Info cache has entries for all whole disk devices (#771244)
More recent versions of blkid don't report an ISO9660 file system on the
whole disk device if partitions can be reports for embedded partitions.
However when querying the whole disk device directly then the expected
ISO9660 file system is reported.  For example on CentOS 7 with the
previous ISO images:

    # wget http://git.kernel.org/cgit/utils/util-linux/util-linux.git/plain/tests/ts/isosize/sample.iso.gz
    # dd if=/dev/zero bs=1M of=/dev/sdc
    # zcat sample.iso.gz | dd of=/dev/sdc

    # blkid -v
    blkid from util-linux 2.23.2  (libblkid 2.23.0, 25-Apr-2013)
    # blkid | fgrep /dev/sdc
    /dev/sdc1: UUID="2013-01-04-22-05-45-00" LABEL="ARCH_201301" TYPE="iso9660" PTTYPE="dos"
    # blkid /dev/sdc
    /dev/sdc: UUID="2013-01-04-22-05-45-00" LABEL="ARCH_201301" TYPE="iso9660" PTTYPE="dos"

    # wget http://cdimage.debian.org/debian-cd/8.6.0/amd64/iso-cd/debian-8.6.0-amd64-netinst.iso
    # dd if=/dev/zero bs=1M of=/dev/sdc
    # dd if=debian-8.6.0-amd64-netinst.iso bs=1M of=/dev/sdc

    # blkid | fgrep /dev/sdc
    /dev/sdc1: UUID="2016-09-17-14-23-48-00" LABEL="Debian 8.6.0 amd64 1" TYPE="iso9660" PTTYPE="dos"
    /dev/sdc2: SEC_TYPE="msdos" UUID="17F3-1162" TYPE="vfat"
    # blkid /dev/sdc
    /dev/sdc: UUID="2016-09-17-14-23-48-00" LABEL="Debian 8.6.0 amd64 1" TYPE="iso9660" PTTYPE="dos"

This behavioural difference with blkid is probably as a result of newer
versions of udev informing the kernel of the partitions embedded within
the ISO9660 image, and not directly as a result of a change in blkid
itself.  Older distributions don't have partition entries for the above
ISO images, but CentOS 7 (with udev 219) and later distributions do have
partition entries:

    # fgrep sdc /proc/partitions
       8       16    8388608 sdc
       8       17     252928 sdc1
       8       18        416 sdc2

Fix by ensuring that the blkid FS_Info cache has entries for whole disk
devices, even if each entry has all empty attributes because there is a
partition table and not a recognised file system.

Calling blkid on whole disk devices containing partition tables produces
output like this, with newer versions of blkid:

    # blkid /dev/sda
    /dev/sda: PTTYPE="dos"
    # blkid /dev/sdb
    /dev/sdb: PTTYPE="gpt"

This will be loaded into the FS_Info cache as a blank entry for the
device by run_blkid_load_cache().  There will be a path name but all the
other attributes will be blank because there are no TYPE, SEC_TYPE, UUID
or LABEL name value pairs.  With older versions of blkid no output is
produced at all.  In that case load_fs_info_cache_extra_for_path() will
create the same blank entry with just the path name defined.

Bug 771244 - gparted does not recognize the iso9660 file system in
             cloned Ubuntu USB boot drives
2017-01-06 12:21:50 -07:00
Mike Fleetwood 683dc9d1ab Only read partition table after not finding a whole disk file system (#771244)
ISO9660 images can contain embedded partitions which when encountered on
whole disk drives causes libparted to report various warnings and
errors.  For example on CentOS 6 with upgraded libparted 2.4 the
following errors and warnings are encountered with various ISO images.
(Deliberately using an older distribution with older blkid to avoid
another issue with blkid addressed in the following patch).

Libparted error message 1:
    # wget http://git.kernel.org/cgit/utils/util-linux/util-linux.git/plain/tests/ts/isosize/sample.iso.gz
    # dd if=/dev/zero bs=1M of=/dev/sdc
    # zcat sample.iso.gz | dd of=/dev/sdc

    # blkid -v
    blkid from util-linux-ng 2.17.2 (libblkid 2.17.0, 22-Mar-2010)
    # blkid | fgrep /dev/sdc
    /dev/sdc: LABEL="ARCH_201301" TYPE="iso9660"

    # ./gpartedbin /dev/sdc
    ======================
    libparted : 2.4
    ======================
    Invalid partition table - recursive partition on /dev/sdc.

    Libparted Error
    (-) Invalid partition table - recursive partition on /dev/sdc.
                                             [ Cancel ] [ Ignore ]

Libparted error message 2:
    # wget http://cdimage.debian.org/debian-cd/8.6.0/amd64/iso-cd/debian-8.6.0-amd64-netinst.iso
    # dd if=/dev/zero bs=1M of=/dev/sdc
    # dd if=debian-8.6.0-amd64-netinst.iso bs=1M of=/dev/sdc

    # blkid | fgrep /dev/sdc
    /dev/sdc: LABEL="Debian 8.6.0 amd64 1" TYPE="iso9660"

    # ./gpartedbin /dev/sdc
    ======================
    libparted : 2.4
    ======================
    /dev/sdc contains GPT signatures, indicating that it has a GPT
    table.  However, it does not have a valid fake msdos partition
    table, as it should.  Perhaps it was corrupted -- possibly by a
    program that doesn't understand GPT partition tables.  Or perhaps
    you deleted the GPT table, and are now using an msdos partition
    table.  Is this a GPT partition table?

    Libparted Warning
    /!\ /dev/sdc contains GPT signatures, indicating that it has a GPT
        table.  However, it does not have a valid fake msdos partition
        table, as it should.  Perhaps it was corrupted -- possibly by a
        program that doesn't understand GPT partition tables.  Or
        perhaps you deleted the GPT table, and are now using an msdos
        partition table.  Is this a GPT partition table?
                                                         [ Yes ] [ No ]

These messages are because GParted is calling ped_disk_new() to attempt
to read the partition table even before it has tried to recognise the
ISO9660 file system on the whole disk drive.  Full call chain is:
    set_devices_thread()
        set_device_from_disk()
            get_device_and_disk()
                get_disk()
                    ped_disk_new()

Fix this by delaying the call to ped_disk_new() until after whole disk
drive recognition has been performed.  Replace combined
get_device_and_disk() with separate get_device() and only call
get_disk() after no whole disk drive file system has been recognised.
This is similar to how calibrate_partition() and
erase_filesystem_signatures() are structured to also handle whole disk
drive file systems.

Bug 771244 - gparted does not recognize the iso9660 file system in
             cloned Ubuntu USB boot drives
2017-01-06 12:21:40 -07:00
Mike Fleetwood fd426cf36e Create new method GParted_Core::set_device_from_disk() (#771244)
Move code from GParted_Core::set_devices_thread() performing top level
population of each Device object during the scan of the drives into new
set_device_from_disk() method.

Bug 771244 - gparted does not recognize the iso9660 file system in
             cloned Ubuntu USB boot drives
2017-01-06 10:47:11 -07:00
Curtis Gedak f762c0cc34 Add detection of iso9660 file system (#771244)
Requires blkid.

Note that FS_LUKS was also moved to more closely match the order in
include/Utils.h

Bug 771244 - gparted does not recognize the iso9660 file system in
             cloned Ubuntu USB boot drives
2017-01-06 10:47:11 -07:00
Michał Górny e4819fdd45 Include sys/sysmacros.h for major and minor macros (#776173)
Glibc 2.25 is deprecating <sys/types.h> including <sys/sysmacros.h>.
    https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=dbab6577c6684c62bd2521c1c29dc25c3cac966f
    Deprecate inclusion of <sys/sysmacros.h> by <sys/types.h>

Building on Fedora Rawhide with Glibc 2.24.90 produces these warnings:

    BlockSpecial.cc:64:13: warning: In the GNU C Library, "major" is defined
     by <sys/sysmacros.h>. For historical compatibility, it is
     currently defined by <sys/types.h> as well, but we plan to
     remove this soon. To use "major", include <sys/sysmacros.h>
     directly. If you did not intend to use a system-defined macro
     "major", you should undefine it after including <sys/types.h>.
       m_major = major( sb.st_rdev );
                 ^~~~~~~~~~~~~~~~~~~~

    BlockSpecial.cc:65:13: warning: In the GNU C Library, "minor" is defined
     by <sys/sysmacros.h>. For historical compatibility, it is
     currently defined by <sys/types.h> as well, but we plan to
     remove this soon. To use "minor", include <sys/sysmacros.h>
     directly. If you did not intend to use a system-defined macro
     "minor", you should undefine it after including <sys/types.h>.
       m_minor = minor( sb.st_rdev );
                 ^~~~~~~~~~~~~~~~~~~~

Code needing major and minor macros should include <sys/sysmacros.h>
directly.  As both Glibc and musl-libc have always provided this header
and GParted is a Linux only application, just always include the header
and don't bother with an autoconf check for its existence.

Bug 776173 - Missing sys/sysmacros.h #include, needed for experimental
             glibc versions
2017-01-04 10:58:44 +00:00
Mike Fleetwood c03c5d11dc Add lost "shrink file system" operation detail message (#775932)
The details for a file system shrink operation look like this with the
"shrink file system" message being missing:

    Shrink /dev/sdb1 from 128.00 MiB to 100.00 MiB
    + calibrate /dev/sdb1
    + check file system on /dev/sdb1 for errors and (if possible) fix them
      + e2fsck -f -y -v -C 0 /dev/sdb1
      + resize2fs -p /dev/sdb1 102400K
    + shrink partition from 128.00 MiB to 100.00 MiB

This earlier commit [1] in the series dropped the message while moving
code from resize_filesystem() to shrink_filesystem().  Re-add the
message back.

[1] a0158abbeb
    Refactor resizing file system apply methods (#775932)

Bug 775932 - Refactor mostly applying of operations
2016-12-16 14:22:54 -07:00
Mike Fleetwood 8373d36ed2 Improve comment about disallowing shrinking when the FS usage is unknown
Explain when and why shrinking file systems is disallowed.

Unnecessary history.  Check added in 2004-12-15:
    d100935b55
    :Set_Valid_Operations()

Comment later added in 2006-07-30:
    677a21f50a
    improved errorhandling a bit. At the initialscan we store errors/warnings
2016-12-12 13:15:34 -07:00
Mike Fleetwood 8979913a3f Remove "../include/" from GParted header #includes
It made the code look a little messy, is easily resolved in the build
system and made the dependencies more complicated than needed.  Each
GParted header was tracked via multiple different names (different
numbers of "../include/" prefixes).  For example just looking at how
DialogFeatures.o depends on Utils.h:

    $ cd src
    $ make DialogFeatures.o
    $ egrep ' [^ ]*Utils.h' .deps/DialogFeatures.Po
     ../include/DialogFeatures.h ../include/../include/Utils.h \
     ../include/../include/../include/../include/../include/../include/Utils.h \
     ../include/../include/../include/Utils.h \

After removing "../include/" from the GParted header #includes, just
need to add "-I../include" to the compile command via the AM_CPPFLAGS in
src/Makefile.am.  Now the dependencies on GParted header files are
tracked under a single name (with a single "../include/" prefix).  Now
DialogFeatures.o only depends on a single name to Utils.h:

    $ make DialogFeatures.o
    $ egrep ' [^ ]*Utils.h' .deps/DialogFeatures.Po
     ../include/DialogFeatures.h ../include/Utils.h ../include/i18n.h \
2016-12-12 13:15:34 -07:00
Mike Fleetwood 683b4da0e4 Update LVM2 PV activate/deactivate check in set_valid_operations()
Simplify conditions checking whether activate/deactivate of an LVM2 PV
is possible.  Excluding extended partition type was unnecessary as it
only matters that the file system type is LVM2 PV or not.

Also remove activate/deactivate from the comment above as that check
only determines if the busy state of file systems and swap space can be
toggled.
2016-12-12 13:15:34 -07:00
Mike Fleetwood 2740113dcb Refactor linux-swap recreation (#775932)
Linux-swap is recreated as part of copy, resize and move operations and
the code was special cased to implement that by calling the linux-swap
specific resize method.  However the displayed text always said "growing
file system" and then proceeded to recreate linux swap.  Example
operation:

    Copy /dev/sdb1 to /dev/sdb2
    ...
    + copy file system from /dev/sdb1 to /dev/sdb2
        Partition copy action skipped because linux-swap file system does not contain data
    + grow file system to fill the partition
      + create new linux-swap file system
        + mkswap -L"" -U "77d939ef-54d6-427a-a2bf-a053da7eed4c" /dev/sdb2
            Setting up swapspace version 1, size = 262140 KiB
            LABEL=, UUID=77d939ef-54d6-427a-a2bf-a053da7eed4c

Fix by writing recreate_linux_swap_filesystem() method with better
messaging and use everywhere maximise_filesystem() was previously used
to recreate linux-swap.  Also as this is a create step, erase the
partition first to prevent the possibility of any other file system
signatures being found afterwards.  Now the operation steps are more
reflective of what is actually being performed.

    Copy /dev/sdb1 to /dev/sdb2
    ...
    + copy file system from /dev/sdb1 to /dev/sdb2
        Partition copy action skipped because linux-swap file system does not contain data
    + clear old file system signatures in /dev/sdb2
    + create new linux-swap file system
      + mkswap -L"" -U "77d939ef-54d6-427a-a2bf-a053da7eed4c" /dev/sdb2
          Setting up swapspace version 1, size = 262140 KiB
          LABEL=, UUID=77d939ef-54d6-427a-a2bf-a053da7eed4c

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood a1c938b30d Properly check for file system online resize capabilities (#775932)
Resizing a file system only checked the .grow and .shrink support
capabilities of the file system, even when perform online resizing.
This wasn't a issue because .online_grow and .online_shrink were always
set from the offline .grow and .shrink capabilities respectively, but
only when online resizing was possible.

However the Device Mapper encryption mapping used for LUKS can only be
resized when online, and not when offline.  Therefore the correct
.online_grow and .online_shrink capabilities needs to be checked to
prevent a LUKS resize step failing as not implemented.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood a0158abbeb Refactor resizing file system apply methods (#775932)
resize_filesystem() was meeting two different needs:
1) when called with fill_partition = false it generated operation
   details;
2) when called from maximize_filesystem() with fill_partition = true it
   skipped generating any operation details;
then ran the switch statement to select the resize implementation.  So
extract the common switch statement into new method
resize_filesystem_implement().

Then observe that the only time resize_filesystem() was called to grow
the file system was when re-creating linux-swap.  Therefore change that
call to use maximize_filesystem() and rename to shrink_filesystem() and
modify the operation detail messages to match.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood a1c2454856 Make copying and moving swap depend on mkswap availability (#775932)
Attempting to copy or move linux-swap partition was possible when the
mkswap command was unavailable.  However as linux-swap is re-created as
part of these operations the mkswap command is a mandatory requirement.
Example copy operation results:

    Copy /dev/sdb1 to /dev/sdb2
    + calibrate /dev/sdb1
    + check file system on /dev/sdb1 for errors and (if possible) fix them
    + create empty partition
    + set partition type on /dev/sdb2
    + copy file system from /dev/sdb1 to /dev/sdb2           [SUCCESS]
        Partition copy action skipped because linux-swap file system does not contain data
    + grow file system to fill the partition                 [WARNING]
        growing is not available for this file system

Because mkswap was not available the grow action didn't re-create
the linux-swap so the newly copied partition is left without any content
and therefore reported as an unknown partition.

Fix by making copying and moving linux-swap depend on mkswap being
available.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 0420159c1d Rename a few GParted_Core apply related methods (#775932)
Make the methods called below apply_operation_to_disk() follow a
standard naming convention:

 *  Contains "_partition"
    Uses libparted to query or change the partition in the disk label
    (partition table).
    E.g.:
        calibrate_partition()
        create_partition()
        delete_partition()
        name_partition()
        resize_move_partition()
        set_partition_type()

 *  Contains "_filesystem"
    Manipulates the file system within the partition, mostly using the
    FileSystem and derived class methods.
    E.g.:
        create_filesystem()
        remove_filesystem()
        label_filesystem()
        copy_filesystem()
        erase_filesystem_signatures()
        check_repair_filesystem()
        resize_filesystem()
        maximize_filesystem()

 *  Other
    Compound method calling multiple partition and file system related
    apply methods.
    E.g.:
        create()
        format()
        copy()
        resize_move()
        resize()
        move()

Rename:
    Delete()      -> delete_partition()
    change_uuid() -> change_filesystem_uuid()

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 9a1841caaa Properly report specific move and resize errors as bugs (#775932)
The initial check in each of GParted_Core::resize() and move() check for
impossible conditions.  (See resize_move() where both functions are
called for confirmation).  The old messages could have suggested the
user somehow composed the operation incorrectly if they had ever been
seen.

Make it clear these failures are programming bugs, thus expecting the
user to raise a bug report should they ever be seen.  While for now the
checks are obviously impossible they document preconditions for the
functions.  More precondition checks are expected to be added later.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood d2fad35407 Refactor GParted_Core::move() (#775932)
Simplify the move() function.  Change it into an if-operation-fails-
return-false style and re-write the final overly complicated conditional
check repair and maximise file system.

The final condition said if the file system was linux-swap or the new
partition was larger, perform a check repair and maximize file system.
However this is a move only step with a check at the top of the move()
function ensuring the Partition objects are the same size.  So that
simplifies to only checking for linux-swap.  As the context is a move
and linux-swap is recreated, performing a check repair first is
unnecessary, so remove that too.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 7caebd2515 Make resize_move_partition() skip whole disk device partitions (#775932)
Make the logic at the resize_move_partition() call sites a little
simpler by not having to avoid calling it for whole disk device
partitions.  Make it a successful non-operation in that case.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 84acec3f91 Make check_repair_filesystem() skip mounted file systems (#775932)
Make the logic at the check_repair_filesystem() call sites a little
easier by not having to avoid calling it for online file systems.  The
function handles that itself as a silent non-operation.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 829bf0ccc1 Make set_partition_type() skip whole disk device partitions (#775932)
Make the logic at the set_partition_type() call sites a little simpler
by not having to avoid calling it for whole disk device Partition
objects.  Make set_partition_type() handle those itself as a silent
non-operation.

This is similar to how calibrate_partition() could be called on an
UNALLOCATED Partition object, and update_bootsector() is called on
non-NTFS file system Partition objects.  Both are successful and silent
non-operations.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 9f08875997 Make encrypted Partition objects look like whole disk device ones (#775932)
Until now an encryption mapping has been modelled as a Partition object
similar to a partition like this:
    .encrypted.device_path  = "/dev/sdb1"
    .encrypted.path         = "/dev/mapper/sdb1_crypt"
    .encrypted.whole_device = false
    .encrypted.sector_start = // start of the mapping in the partition
    .encrypted.sector_end   = // end of the mapping in the partition
However accessing device_path in the start to end sector range is not
equivalent to accessing the partition path as it doesn't provide access
to the encrypted data.  Therefore existing functions which read and
write partition data (GParted file system copying and signature erasure)
via libparted using the device_path won't work and will in fact destroy
the encrypted data.  This could be coded around with an extra case in
the device opening code, however it is not necessary.

An encrypted block special device /dev/mapper/CRYPTNAME looks just like
a whole disk device because it doesn't contain a partition and the file
system it contains starts at sector 0 and goes to the end.  Therefore
model an encryption mapping in the same way a whole disk device is
modelled as a Partition object like this:
    .encrypted.device_path  = "/dev/mapper/sdb1_crypt"
    .encrypted.path         = "/dev/mapper/sdb1_crypt"
    .encrypted.whole_device = true
    .encrypted.sector_start = 0
    .encrypted.sector_end   = // size of the encryption mapping - 1
Now GParted file system copy and erasure will just work without any
change.  Just need to additionally store the LUKS header size, which was
previous stored in the sector_start, for use in the
get_sectors_{used,unused,unallocated}() calculations.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood 0b359a5490 Refactor GParted_Core::copy() (#775932)
Split out the switch statement selecting the copy implementation and
associated copy file system operation detail message into a separate
copy_filesystem() method, matching how a number of other operations are
coded.  This is why the previous copy_filesystem() methods needed
renaming.

Re-write the remaining copy() into if-operation-fails-return-false style
to simplify it.  Re-write final complicated conditional check repair and
maximise file system into separate positive if conditions for swap and
larger partition to make it understandable.

The min_size parameter to copy() was queried from the partition_src
parameter also passed to copy().  Drop the parameter and query inside
copy() instead.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood fd56d7be57 Rename copy_filesystem() methods (#775932)
Rename GParted_Core methods:
    copy_filesystem(4 params)  -> copy_filesystem_internal()
    copy_filesystem(5 params)  -> copy_filesystem_internal()
    copy_filesystem(10 params) -> copy_blocks()

See the following commit for the desire to do this.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood fed2595d6d Rename file and class to CopyBlocks (#775932)
Files were named Block_Copy and the class was named block_copy.  Change
to the primary naming convention of CamelCase class name and matching
file names.

Also make CopyBlocks::copy_block() a private method as it is only used
internally within the class.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood a5f2c9937b Refactor activate_mount_partition() and toggle_busy_state() (#775932)
These two methods had a lot of repeated and common code.  Both determine
if the partition has any pending operations, notify the user that
changing the busy status can not be performed, and report any errors
when changing the status.

Extract the common code into sub-functions check_toggle_busy_allowed()
and show_toggle_failure_dialog() to handle showing the message dialogs.
Also refactor toggle_busy_state() to make it clear that it handles 5
cases of swapon, swapoff, activate VG, deactivate VG and unmount file
system.

Also remove out of date comment near the top of toggle_busy_state()
stating there can only be pending operations for inactive partitions is
out of date.  Some file systems can be resized while online and
partition naming is allowed whatever the busy status of the file system.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:34 -07:00
Mike Fleetwood bfc16bd4a6 Refactor Win_GParted::unmount_partition() (#775932)
The primary reason to refactor unmount_partition() is to pass the
Partition object to be unmounted, rather than use member variable
selected_partition_ptr so that it doesn't have to handle the differences
between encrypted and non-encrypted Partition objects.  The calling
function can deal with that instead.  Then there were lots of small
reasons to change almost every other line too:
* Return success or failure rather than updating a passed pointer with
  the result.  Leftover from when the function used to be a separate
  thread:
      commit 52a2a9b00a
      Reduce threading (#685740)
* Pass updated error string by reference rather than by pointer.  Likely
  another leftover.
* Stop borrowing the updated error string as a local variable for the
  error output from the umount command.  Use new umount_error local
  variable instead.  Was bad practice making the code harder to
  understand.
* Rename failed_mountpoints to skipped_mountpoints to better reflect
  that it contains the mount points not attempted to be unmounted
  because two or more file systems are mounted at that point.
* Rename errors to umount_errors to better reflect it contains the
  errors from each failed umount command.
* Document the reason for mount points being skipped.
* Update the skipped mount points message to state definitely why they
  could not be unmounted rather than stating most likely.
* Simplify logic processing the error cases and return value.
* Made static because it no longer accesses any class members.
* Remove out dated "//threads.." comment from the header.  Another
  leftover from when the function use to be a separate thread.

Bug 775932 - Refactor mostly applying of operations
2016-12-12 13:15:16 -07:00
Mike Fleetwood 43f2a4bd08 Reorder and rename params to TreeView_Detail::load_partitions()
Reorder the parameters into the same order in which they occur in the
row, i.e. Name first, then Mount Point and finally Label.  Rename local
variables in load_partitions(1 param) and parameters of
load_partitions(5 params) prefixing with "show_" to make it clearer the
variables track if that column will be displayed or not.
2016-12-02 10:06:01 -07:00
Mike Fleetwood 9e932ec7d0 Fix detection of empty mount point column for encrypted file systems (#775475)
create_row() populates the values for each row to be displayed in the UI
from the relevant Partition object.  However load_partitions(5 params)
independently decided if the Name, Mount Point and Label columns were
empty and should be displayed.

Getting the mount point value is more complex for encrypted file systems
because it has to call get_mountpoints() on the inner encrypted
Partition object.  load_partitions(5 params) didn't account for this.

Fix by making create_row() both copy the values into each row and at the
same time check if they are empty to decide if they should be displayed
or not.

Bug 775475 - Mount Point column displayed for encrypted file systems
             even when empty
2016-12-02 09:56:29 -07:00
Mike Fleetwood 6bacd6ae4d Fix slightly messed up comment in Win_GParted::on_show() (#771816)
Bug 771816 - GParted never exits if the main window is closed before the
             initial device load completes
2016-10-10 14:07:54 -06:00
Mike Fleetwood aeebee9c12 Prevent the UI hanging while gpart data rescue is running (#772123)
Running Device > Attempt Data Rescue... hangs the GParted UI for as long
as gpart takes to scan the whole disk device looking for file system
signatures.

Originally when gpart support was added by commit [1] a separate thread
was created to run gpart.  Then most threading was removed by commit [2]
which left gpart running in the main thread blocking the UI.

[1] ef37bdb7de
    Added support to lost data recovery using gpart

[2] 52a2a9b00a
    Reduce threading (#685740)

guess_partition_table() hand codes using Glib to run the gpart command
asynchronously reading standard output, but it just doesn't run the Gtk
main loop to process events, hence the UI hangs.  Instead just use
Utils::execute_command() which handles everything already.  It runs the
commands asynchronously, reading output and if being run in the main
thread also calls the Gtk main loop to keep the UI responsive.

Bug 772123 - GParted is unresponsive while gpart is running
2016-10-06 10:07:23 -06:00
Mike Fleetwood 7ea22f1190 Make GParted exit when closed before the initial load completes (#771816)
If the GParted main window is closed before the initial device load
completed gpartedbin never exits.  The main window closes but the
process sits there idle forever.  Subsequently running GParted reports
this error:
    # gparted
    The process gpartedbin is already running.
    Only one gpartedbin process is permitted.

If the user is running GParted from a desktop menu they will never see
this error so they will never know why GParted won't start any more.

More technically, it is if the main window is closed before the
Win_GParted::on_show() callback completes.

I assume the Gtk main loop doesn't setup the normal quit handling until
the on_show() callback finishes drawing the main window for the first
time.  Following this hint [1], move the initial device load from the
on_show() callback to immediately after it completes by using a run once
idle callback setup in on_show().

This looks exactly the same to the user except now gpartedbin exits when
the main window is closed during the initial device load.  Note that
GParted finished the device load before exiting.  This is exactly the
same as happens when exiting during subsequent device refreshes.

[1] How to know when a Gtk Window is fully shown?
    http://stackoverflow.com/questions/14663212/how-to-know-when-a-gtk-window-is-fully-shown

    "If you want to know when your main window appears the first time,
    it is far easier (and saner) add a g_idle_add after your show_all
    call."

Bug 771816 - GParted never exits if the main window is closed before the
             initial device load completes
2016-10-05 11:28:36 -06:00
Mike Fleetwood 770ce9a9e1 Line wrap file system status value in the Information dialog (#771693)
Again this is to handle a long list of mount points for a single
partition.  To prevent the file system status value of "Mounted on [list
of 20 mount points]" causing the dialog to become wider than the screen,
line wrap the text.  This instead makes the dialog taller, which already
automatically scrolls vertically as needed.

Bug 771693 - Mount Point column is wider than the screen on openSUSE
             using btrfs root with lots of mounted subvolumes
2016-10-05 10:53:21 -06:00
Mike Fleetwood 1f4f0a4b67 Show too wide Mount Point column with ellipsis (#771693)
openSUSE 42.1 with default btrfs root installation is heavily using
subvolumes.  (Think of btrfs as a storage pool and subvolumes as
individual file systems within the pool for a rough approximation).
Thus the root partition is mounted on many mount points:

    # df -k
    Filesystem     1K-blocks    Used Available Use% Mounted on
    ...
    /dev/sda2       19445760 5820080  13157104  31% /
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/libvirt/images
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/mysql
    /dev/sda2       19445760 5820080  13157104  31% /.snapshots
    /dev/sda2       19445760 5820080  13157104  31% /home
    /dev/sda2       19445760 5820080  13157104  31% /var/opt
    /dev/sda2       19445760 5820080  13157104  31% /usr/local
    /dev/sda2       19445760 5820080  13157104  31% /var/tmp
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/named
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/mariadb
    /dev/sda2       19445760 5820080  13157104  31% /var/spool
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/pgsql
    /dev/sda2       19445760 5820080  13157104  31% /var/lib/mailman
    /dev/sda2       19445760 5820080  13157104  31% /srv
    /dev/sda2       19445760 5820080  13157104  31% /opt
    /dev/sda2       19445760 5820080  13157104  31% /var/crash
    /dev/sda2       19445760 5820080  13157104  31% /tmp
    /dev/sda2       19445760 5820080  13157104  31% /boot/grub2/i386-pc
    /dev/sda2       19445760 5820080  13157104  31% /var/log
    /dev/sda2       19445760 5820080  13157104  31% /boot/grub2/x86_64-efi

As the Mount Point column contains all 20 mount points it makes the
column wider than the screen, requiring lots of horizontal scrolling to
see the following columns.  (Truncated to just 7 mounts in this
example here).

    Partition     | File System | Mount Point
      /dev/sda1 * | # swap      |
      /dev/sda2 * | # btrfs     | /, /.snapshots, /boot/grub2/i386-pc, /boot/grub2/x86_64-efi, /home, /opt, /srv

Fix by making the Mount Point column resizable and display truncated
text with ellipsis.  The column now takes the initial and minimum width
from the width of the "Mount Point" column header text.

    Partition     | File System | Mount Point     |   Size
      /dev/sda1 * | # swap      |                 |  1.45 GiB
      /dev/sda2 * | # btrfs     | /, /.snapsho... | 18.54 GiB

Bug 771693 - Mount Point column is wider than the screen on openSUSE
             using btrfs root with lots of mounted subvolumes
2016-10-05 10:53:21 -06:00
Mike Fleetwood 85b6858702 Remove remaining vestiges of coloured text from the partition list
This commit stopped setting the text colours in the Partition, File
System and Mount Point columns to avoid hard coding text colours making
them impossible to read when using GNOME's High Contrast Inverse theme:
    ff2a6c00dd
    Changes post gparted-0.3.6 - code recreation from Source Forge

    * src/TreeView_Detail.cc: Removed text_color hard coding
      - Removed hard coding of Partition and Filesystem text_color
        which was based on if partition was TYPE_UNALLOCATED.
      - Removed hard coding of Mount text_color which was based
        on if partition was busy.  Lock symbol provides this indicator.
      - Closes GParted bug #413810 - Don't hardcode text colour in
        partition list

Now remove the remaining vestiges left behind.  Remove the unused color
text and mount_text_color columns from the tree model.  Also remove
setting of the column attributes which set the colour of the text in the
tree view from those unused columns in the tree model.

Unnecessary history.  Added by:
    b179990dc9
    show greyed-out mountpoint of unmounted partitions in the treeview
    as an improved way to identify partitions
    Bug #333027 -  Displaying unmounted partitions' default mount points
    in grey

and by commit only in CVS history:
    Bart Hakvoort <...> 2004-08-22 15:06:45
    Made text in Partition column darkgrey for unallocated. this offers
    more visual difference between partitions and unallocated space
2016-10-05 10:53:21 -06:00
Mike Fleetwood c6d29aa7e8 Include extended partitions in the count of active partitions
Trying to create a new partition table on a device with active
partitions reports the number of active partitions in the error dialog.
However when there is a busy logical partition the number of reported
busy partitions will be one less than the number of partitions in the
main UI showing the busy symbol.

GParted considers extended partitions as busy when any of the logical
partitions it contains as busy.  Display in the main UI reflects this.

Fix Win_GParted::active_partitions_on_device_count() to not exclude
extended partitions from the count.
2016-10-05 09:59:15 -06:00
Mike Fleetwood 253c4d6416 Fix BlockSpecial comparison (#771670)
Found that in some cases usage of active encrypted swap was not working,
but only for the first encrypted swap partition.  This only failed on
the first Device Mapper device, dm-0:

    # ls -l /dev/mapper/ /dev/dm-*
    brw-rw---- 1 root disk 254, 0 Oct  4 20:58 /dev/dm-0
    brw-rw---- 1 root disk 254, 1 Oct  4 20:58 /dev/dm-1

    /dev/mapper/:
    total 0
    crw------- 1 root root 10,236 Oct  4 19:48 control
    lrwxrwxrwx 1 root root      7 Oct  4 20:58 sdb1_crypt -> ../dm-0
    lrwxrwxrwx 1 root root      7 Oct  4 20:58 sdb2_crypt -> ../dm-1

    # cat /proc/swaps
    Filename                        Type        Size    Used    Priority
    /dev/sda1                       partition   1524732 92356   -1
    /dev/dm-0                       partition   1046524 0       -2
    /dev/dm-1                       partition   1046524 0       -3

Was failing because the minor number of dm-0 was 0, causing BlockSpecial
operator==() to fall back to name comparison rather than major, minor
number, and GParted name /dev/mapper/sdb1_crypt doesn't match /dev/dm-0.

Found on openSUSE and Ubuntu which don't use LVM by default and don't
already have dm-0 used as an LVM Logical Volume which GParted doesn't
support.

The LINUX ALLOCATED DEVICES document [1] says block special device 0, 0
(major, minor) is not used "reserved as null device number".   (Not to
be confused with 1, 3 /dev/null the Null device).  All other
non-negative pairs are valid block special device numbers.  Therefore
update BlockSpecial operator==() accordingly; compare by major, minor
number when either is greater than 0 falling back to string compare
otherwise.  This still fits in with the BlockSpecial() constructor using
major, minor numbers 0, 0 to represent plain files.

[1] LINUX ALLOCATED DEVICES
    https://www.kernel.org/doc/Documentation/devices.txt

Bug 771670 - Usage of active encrypted swap is not shown
2016-10-05 09:59:15 -06:00
Mike Fleetwood 3966cc3e6f Implement usage reporting of active encrypted swap partitions (#771670)
GParted does not show the usage of active encrypted swap partitions,
instead showing partition warning "Unable to read the contents of this
file system! ...".  OS setup:

    # ls -l /dev/mapper/sdb4_crypt /dev/dm-3
    brw-rw----. 1 root disk 253, 3 Sep 14 07:26 /dev/dm-3
    lrwxrwxrwx. 1 root root      7 Sep 14 07:26 /dev/mapper/sdb4_crypt -> ../dm-3
    # mkswap -L encrypted_swap /dev/mapper/sdb4_crypt
    # swapon /dev/mapper/sdb4_crypt
    # cat /proc/swaps
    Filename                        Type        Size    Used    Priority
    /dev/sda2                       partition   2097148 237632  -1
    /dev/dm-3                       partition   1046524 0       -2

This is because the code was performing a string compare between the
canonical /dev/mapper/sdb4_crypt name GParted is using and the /dev/dm-3
name reported by the kernel via /proc/swaps.  Fix by creating
BlockSpecial objects from the names and compare those so that comparison
is done correctly using major, minor numbers.

Bug 771670 - Usage of active encrypted swap is not shown
2016-10-05 09:59:15 -06:00
Mike Fleetwood ab4040c547 Remove out of date comment from the end of set_devices_thread()
The comment became completely unnecessary with the transfer of
mount_info and fstab_info into separate Mount_Info module by commit:
    63ec73dfda
    Split mount_info and fstab_info maps into separate Mount_Info module

It was never necessary to clear one of the mappings at the end of the
device refresh because it was reloaded at the start of the next device
refresh anyway and it is only a small amount of memory.
2016-09-14 09:48:33 -06:00
Mike Fleetwood 8c870cf72f Stop showing duplicate mount points for unmounted encrypted file systems (#771323)
Have an unmounted file system within an open encrypted mapping and an
entry in /etc/fstab for the file system like this:

    # lsblk
    NAME           MAJ:MIN RM   SIZE RO TYPE   MOUNTPOINT
    ...
    sdb              8:16   0     8G  0 disk
    +-sdb1           8:17   0     1G  0 part
      +-sdb1_crypt 253:0    0  1022M  0 crypt
    # blkid | grep sdb1
    /dev/sdb1: TYPE="crypto_LUKS" ...
    /dev/mapper/sdb1_crypt: TYPE="ext4" ...
    # ls -l /dev/mapper/sdb1_crypt /dev/dm-0
    brw-rw----. 1 root disk 253, 0 Sep 12 19:09 /dev/dm-0
    lrwxrwxrwx. 1 root root      7 Sep 12 19:09 /dev/mapper/sdb1_crypt -> ../dm-0
    # grep sdb1 /etc/fstab
    /dev/mapper/sdb1_crypt   /mnt/1   ext4   defaults    0 0

The mount point will be shown twice for the partition:
    /mnt/1, /mnt/1

This is because add_node_and_mountpoint() adds two entries for both the
symbolic and real block special names:
    map["/dev/mapper/sdb1_crypt"] = ["/mnt/1"]
    map["/dev/dm-0"]              = ["/mnt/1"]
This was needed for the old code which used string compare to match
block devices so that the mount point could be looked up by either name.
However since bug 767842 introduced major, minor number comparison it
became unnecessary.  As both names refer to the same device the mount
point gets added twice to the same entry.  Hence display of the double
mount.
    map[BlockSpecial{"/dev/mapper/sdb1_crypt", 253, 0}] =
                                                    ["/mnt/1", "/mnt/1"]

It is always going to be the case that the symbolic link and real block
special names have the same major, minor numbers.  That was the
requirement of the BlockSpecial class and the reason for using stat() to
lookup the numbers.  Therefore adding entries for both names will always
add duplicate entries.  Fix by stop using realpath() to lookup the real
name and adding the duplicate entry.

Introduced by:
    a800ca8b68
    Add BlockSpecial into mount_info and fstab_info (#767842)

Bug 771323 - GParted is showing duplicate mount points for unmounted
             encrypted file systems
2016-09-14 09:48:33 -06:00
Mike Fleetwood e6f4b1c305 Remove no longer needed includes from GParted_Core.cc
No longer needed since commit:
    504eb04ddc
    Roll back (remove) code to recognize /dev/mapper/* devices, because ...
2016-08-06 09:47:58 -06:00
Mike Fleetwood 63ec73dfda Split mount_info and fstab_info maps into separate Mount_Info module
The GParted_Core::mount_info and GParted_Core::fstab_info maps and the
methods that manipulate them are self-contained.  Therefore move them to
a separate Mount_Info module and reduce the size of the monster
GParted_Core slightly.
2016-08-06 09:47:58 -06:00
Mike Fleetwood a94be2da05 Switch to static class interface for FS_Info
The FS_Info module has a pseudo multi-object interface and used the
constructor to load the cache.  However all the data in the class is
static.  An FS_Info object doesn't contain any member variables, yet was
needed just to call the member functions.

Make all the member functions static removing the need to use any
FS_Info objects and provide an explicit load_cache() method.
2016-08-06 09:47:58 -06:00
Mike Fleetwood 5055c0b638 Simplify whole device matching in load_proc_partitions_Info_cache()
Following earlier commit "Pre-populate BlockSpecial cache while reading
/proc/partitions (#767842)" load_proc_partitions_info_cache() has
extracted just the name field from each line of /proc/partitions.
Therefore simplify the regular expressions matching each type of whole
disk device to just matching in the name field rather than matching in
the whole line.
2016-08-06 09:47:58 -06:00
Mike Fleetwood 912766c2e1 Switch to a static interface for Proc_Partitions_Info
The Proc_Partitions_Info has a pseudo multi-object interface and uses
the constructor to load the cache.  However all the data in the class is
static.  A Proc_Partitions_Info object doesn't contain any member
variables, yet was needed just to call the member functions.

Make all the member functions static removing the need to use any
Proc_Partitions_Info objects and provide and explicit load_cache()
method.
2016-08-06 09:47:58 -06:00
Mike Fleetwood 91e5a0960e Remove remaining use of retired vol_id
Vol_id has been retired and removed from all supported distributions.
See earlier commit "Remove use of retired vol_id from FS_Info module
(#767842)" for more details.  Therefore remove it's use from GParted
entirely.
2016-08-06 09:47:58 -06:00
Mike Fleetwood 571304d2c6 Pre-populate BlockSpecial cache while reading /proc/partitions (#767842)
GParted is already reading /proc/partitions to get whole disk device
names.  The file also contains the major, minor device number of every
partition.  Use this information to pre-populate the cache in the
BlockSpecial class.

    # cat /proc/partitions
    major minor  #blocks  name

       8        0   20971520 sda
       8        1     512000 sda1
       8        2   20458496 sda2
    ...
       9        3    1047552 md3
     259        2     262144 md3p1
     259        3     262144 md3p2
    ...
     253        0   18317312 dm-0
     253        1    2097152 dm-1
     253        2    8383872 dm-2
     253        3    1048576 dm-3

Note that for Device-Mapper names (dm-*) the kernel is not using the
canonical user space names (mapper/*).  There is no harm in
pre-populating the cache with these names and will help if tools report
them too.  It is just that for DMRaid, LVM and LUKS, GParted uses the
canonical /dev/mapper/* names so will still have to call stat() once for
each such name.

For plain disks (sd*) and Linux Software RAID arrays (md*) the kernel
name is the common user space name too, therefore matches what GParted
uses and pre-populating does avoid calling stat() at all for these
names.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 003d6eff94 Cache looked up major, minor numbers in BlockSpecial class (#767842)
Creation of every BlockSpecial object used to result in a stat() OS
call.  On one of my test VMs debugging with 4 disks and a few partitions
on each, GParted refresh generated 541 calls to stat() in the
BlockSpecial(name) constructor.  However there were only 45 unique
names.  So on average each name was stat()ed approximately 12 times.

Cache the major, minor number associated with each name after starting
with a cleared cache for each GParted refresh.  This reduces these
direct calls to stat() to just the 45 unique names.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood ce8fb1dd91 Use BlockSpecial in FS_Info module cache (#767842)
The FS_Info cache is loaded from "blkid" output and compares block
special names.  Therefore switch to using BlockSpecial objects so that
comparisons are performed by the major, minor device number instead.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 23da3ab9a8 Parse FS_Info cache into fields while loading (#767842)
FS_Info module caches the output from blkid as a single string and uses
regular expressions to find the line matching the requested block
special file name.  This is not compatible with using BlockSpecial
objects to represent block special files, and perform matching by major,
minor device number.  Therefore parse the blkid output into a vector of
structures containing the needed fields, ready for switching to
BlockSpecial objects in the following patch.

Interface to the module remains unchanged.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 8aa34f7baa Remove use of retired vol_id from FS_Info module (#767842)
Vol_id was removed from udev 142, released 2009-05-13, and udev switched
to using blkid instead [1].  All currently supported distributions use
later versions of udev (or systemd after the udev merge), except for
RedHat / CentOS 5 with udev 095.  However RedHat / CentOS 5 does provide
blkid and vol_id is found in udev specific /lib/udev directory not on
the PATH.  Therefore effectively vol_id is not available on any
supported distribution and blkid is always available.  Therefore remove
use of vol_id from the FS_Info module.  Less code to refactor and test
in following changes.

[1] delete vol_id and require util-linux-ng's blkid
    http://git.kernel.org/cgit/linux/hotplug/udev.git/commit/?id=f07996885dab45102492d7f16e7e2997e264c725

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 2b013e494c Use BlockSpecial in SWRaid_Info module cache (#767842)
The SWRaid_Info cache is loaded from "mdadm" command output and
/proc/mdstat file.  It contains the member name which is used to access
the cache, therefore switch to using BlockSpecial objects so that
comparison is performed using the major, minor device number.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 7cd574cac5 Use BlockSpecial in LUKS_Info module cache (#767842)
The LUKS_Info module cache is loaded from "dmsetup" command and compares
block special files, therefore switch to using BlockSpecial objects so
that comparisons are performed by major, minor device number.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood e4a8530b14 Overload is_dev_mounted() function (#767842)
Small optimisation which avoids constructing an extra BlockSpecial
object when determining if a btrfs member is mounted.  Rather than
extracting the name from the BlockSpecial object in
btrfs::get_mount_device() and re-constructing another BlockSpecial
object from that name in GParted_Core::is_dev_mounted(), pass the
BlockSpecial object directly.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood abee66484f Use BlockSpecial objects in btrfs member cache (#767842)
There are no known errors which affect the remaining caches in GParted.
However those caches which compare block special devices will be changed
to use BlockSpecial objects so comparison is by major, minor device
number rather than by name.

Change btrfs member cache loaded from "btrfs filesystem show" output to
use BlockSpecial objects.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood a800ca8b68 Add BlockSpecial into mount_info and fstab_info (#767842)
On some distributions having btrfs on top of LUKS encrypted partitions,
adding a second device and removing the first device used to mount the
file system causes GParted to no longer be able to report the file
system as busy or the mount points themselves.

For example, on CentOS 7, create a single btrfs file system and mount
it.  The provided /dev/mapper/sdb1_crypt name is reported, via
/proc/mounts, as the mounting device:
    # cryptsetup luksFormat --force-password /dev/sdb1
    # cryptsetup luksOpen /dev/sdb1 sdb1_crypt
    # mkfs.btrfs -L encrypted-btrfs /dev/mapper/sdb1_crypt
    # mount /dev/mapper/sdb1_crypt /mnt/1

    # ls -l /dev/mapper
    total 0
    lrwxrwxrwx. 1 root root       7 Jul  2 14:15 centos-root -> ../dm-1
    lrwxrwxrwx. 1 root root       7 Jul  2 14:15 centos-swap -> ../dm-0
    crw-------. 1 root root 10, 236 Jul  2 14:15 control
    lrwxrwxrwx. 1 root root       7 Jul  2 15:14 sdb1_crypt -> ../dm-2
    # fgrep btrfs /proc/mounts
    /dev/mapper/sdb1_crypt /mnt/1 btrfs rw,seclabel,relatime,space_cache 0 0

Add a second device to the btrfs file system:
    # cryptsetup luksFormat --force-password /dev/sdb2
    # cryptsetup luksOpen /dev/sdb2 sdb2_crypt
    # btrfs device add /dev/mapper/sdb2_crypt /mnt/1

    # ls -l /dev/mapper
    ...
    lrwxrwxrwx. 1 root root       7 Jul  2 15:12 sdb2_crypt -> ../dm-3
    # btrfs filesystem show /dev/mapper/sdb1_crypt
    Label: 'encrypted-btrfs'  uuid: 45d7b1ef-820c-4ef8-8abd-c70d928afb49
            Total devices 2 FS bytes used 32.00KiB
            devid    1 size 1022.00MiB used 12.00MiB path /dev/mapper/sdb1_crypt
            devid    2 size 1022.00MiB used 0.00B path /dev/mapper/sdb2_crypt

Remove the first mounting device from the btrfs file system.  Now the
non-canonical name /dev/dm-3 is reported, via /proc/mounts, as the
mounting device:
    # btrfs device delete /dev/mapper/sdb1_crypt /mnt/1

    # btrfs filesystem show /dev/mapper/sdb2_crypt
    Label: 'encrypted-btrfs'  uuid: 45d7b1ef-820c-4ef8-8abd-c70d928afb49
            Total devices 1 FS bytes used 96.00KiB
            devid    2 size 1022.00MiB used 144.00MiB path /dev/mapper/sdb2_crypt
    # fgrep btrfs /proc/mounts
    /dev/dm-3 /mnt/1 btrfs rw,seclabel,relatime,space_cache 0 0
    # ls -l /dev/dm-3
    brw-rw----. 1 root disk 253, 3 Jul  2 15:12 /dev/dm-3

GParted loads the mount_info mapping from /proc/mounts and with it the
/dev/dm-3 name.  When GParted is determining if the encrypted btrfs file
system is mounted or getting the mount points it is using the
/dev/mapper/sdb2_crypt name.  Therefore no information is found and the
file system is incorrectly reported as unmounted.

Fix by changing mount_info and fstab_info to use BlockSpecial objects
instead of strings so that matching is performed by major, minor device
numbers rather than by string compare.  Note that as BlockSpecial
objects are used as the key of std::map [1] mappings operator<() [2]
needs to be provided to order the key values.

[1] std::map
    http://www.cplusplus.com/reference/map/map/
[2] std::map::key_comp
    http://www.cplusplus.com/reference/map/map/key_comp/

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood ab2d4f5ee6 Create BlockSpecial class and use in LVM2_PV_Info (#767842)
In some cases creating an LVM2 Physical Volume on top of a DMRaid array
reports no usage information and this partition warning:
    Unable to read the contents of this file system!
    Because of this some operations may be unavailable.
    The cause might be a missing software package.
    The following list of software packages is required for lvm2
    pv file system support: lvm2.

For example on Ubuntu 14.04 LTS (with GParted built with
--enable-libparted-dmraid) create an LVM2 PV in a DMRaid array
partition.  GParted uses this command:
    # lvm pvcreate -M 2 /dev/mapper/isw_bacdehijbd_MyArray0p2

But LVM reports the PV having a different name:
    # lvm pvs
      PV                                                VG   Fmt  Attr PSize PFree
      /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2      lvm2 a--  1.00g 1.00g

This alternate name is loaded into the LVM2_PV_Info module cache.  Hence
when GParted queries partition /dev/mapper/isw_bacdehijbd_MyArray0p2 it
has no PV information against that name and reports unknown usage.

However they are actually the same block special device; major 252,
minor 2:
    # ls -l /dev/mapper/isw_bacdehijbd_MyArray0p2
    brw-rw---- 1 root disk 252, 2 Jul  2 11:09 /dev/mapper/isw_bacdehijbd_MyArray0p2

    # ls -l /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2
    lrwxrwxrwx 1 root root 10 Jul  2 11:09 /dev/disk/by-id/dm-name-isw_bacdehijbd_MyArray0p2 -> ../../dm-2
    # ls -l /dev/dm-2
    brw-rw---- 1 root disk 252, 2 Jul  2 11:09 /dev/dm-2

To determine if two names refer to the same block special device their
major, minor numbers need to be compared, instead of string comparing
their names.

Implement class BlockSpecial which encapsulates the name and major,
minor numbers for a block special device.  Also performs comparison as
needed.  See bug 767842 comments 4 and 5 for further investigation and
decision for choosing to implement a class.

Replace name strings in the LVM2_PV_Info module with BlockSpecial
objects performing correct block special device comparison.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 0f4df8dfd1 Remove now unused Proc_Partitions_Info::get_alternate_paths() (#767842)
Now Device and Partition objects only have a single path,
get_alternate_paths() is never called.  Remove the method and population
of the private alternate_paths_cache member that went with it.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 40f39bdbe2 Rename Partition::add_path() to set_path() (#767842)
To reflect that there is now only a single path in the Partition object
now.  Also get rid of the now unneeded optional clear_paths parameter
which was only relevant when there was a vector of paths.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 54652b0d4e Update calibrate_partition() for single partition path (#767842)
Now that Partition objects only have a single path, rather than a list
of paths, stop performing unnecessary actions in calibrate_partitions()
which added alternate paths reported from libparted.  Just left with
setting the partition path name correctly, when the path name doesn't
exist.  Happens when the path is set to "Copy of /dev/SRC" when the
partition was newly created by a copy-paste into unallocated space
earlier in the sequence of operations now being applied.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 214255eda3 Simplify Partition object to a single path (#767842)
Change from a vector of paths to a single path member in the Partition
object.  Remove add_paths() and get_paths() methods.  Keep add_path()
and get_path().

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 1dc8a0c628 Rename Device::add_path() to set_path() (#767842)
To reflect that there is only a single path in the Device object now.
Also get rid of the now unneeded optional parameter which was only
relevant when there was a vector of paths.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Mike Fleetwood 902afaa010 Simplify Device object to a single path (#767842)
Background

GParted stored a list of paths for Device and Partition objects.  It
sorted this list [1][2] and treated the first specially as that is what
get_path() returned and was used almost everywhere; with the file system
specific tools, looked up in various *_Info caches, etc.

[1] Device::add_path(), ::add_paths()
[2] Partition::add_path(), ::add_paths()

Mount point display [3] was the only bit of GParted which really worked
with the path list.  Busy file system detection [4] just used the path
provided by libparted, or for LUKS /dev/mapper/* names.  It checked that
single path against the mounted file systems found from /proc/mounts,
expanded with additional block device names when symlinks were
encountered.

[3] GParted_Core::set_mountpoints() -> set_mountpoints_helper()
[4] GParted_Core::set_device_partitions() -> is_busy()
    GParted_Core::set_device_one_partition() -> is_busy()
    GParted_Core::set_luks_partition() -> is_busy()

Having the first path, by sort order, treated specially by being used
everywhere and virtually ignoring the others was wrong, complicated to
remember and difficult code with.  As all the additional paths were
virtually unused and made no difference, remove them.  The "improved
detection of mountpoins, free space, etc.." benefit from commit [5]
doesn't seem to exist.  Therefore simplify to a single path for Device
and Partition objects.

[5] commit 6d8b169e73
    changed the way devices and partitions store their device paths.
    Instead of holding a 'realpath' and a symbolic path we store paths
    in a list.  This allows for improved detection of mountpoins, free
    space, etc..

This patch

Simplify the Device object from a vector of paths to a single path.
Remove add_paths() and get_paths() methods.  Keep add_path() and
get_path() for now.

Bug 767842 - File system usage missing when tools report alternate block
             device names
2016-08-06 09:47:58 -06:00
Natanael Copa 25209904a9 Fix sscanf modifier for long long (#768239)
POSIX says that %lld is the modifier for long long.
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fscanf.html

This fixes wrong sizes with musl libc.

Bug 768239 - sizes are wrong with musl libc due to use of non-standard
             sscanf "%Ld" modifier
2016-07-01 15:37:01 +01:00
Mike Fleetwood 8ac3a0e4ad Recognise GRUB2 core.img (#766989)
Recognise GRUB2 core.img boot code written to a partition without a file
system.  Such setups are possible/likely with GPT partitioned disks as
there is a specific partition type reserved for it [1][2]:
    21686148-6449-6E6F-744E-656564454649  (BIOS Boot partition)

[1] GUID Partition Table, Partition types
    https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs

[2] BIOS boot partition
    https://en.wikipedia.org/wiki/BIOS_boot_partition

Bug 766989 - zfsonline support - need file system name support for ZFS
             type codes
2016-06-15 12:45:05 -06:00
Mike Fleetwood 3daf73a01b Prevent assert failure from OperationDelete::get_partition_new() (#767233)
Composing these operations caused GParted to abort on an assert failure:
(1) Delete an existing partition,
(2) Create a new partition,
(3) Delete new partition.

This is the equivalent issue as fixed in the previous commit, except with
the delete operation rather than the check operation:
    Prevent assert failure from OperationCheck::get_partition_new() (#767233)

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================
    **
    ERROR:OperationDelete.cc:41:virtual GParted::Partition& GParted::OperationDelete::get_partition_new(): assertion failed: (false)
    Aborted (core dumped)

    # gdb ./gpartedbin core.19232 --batch --quiet --ex backtrace -ex quit
    [New Thread 19232]
    [New Thread 19234]
    [Thread debugging using libthread_db enabled]
    Core was generated by `./gpartedbin'.
    Program terminated with signal 6, Aborted.
    #0  0x000000361f2325e5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    64	  return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
    #0  0x000000361f2325e5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    #1  0x000000361f233dc5 in abort () at abort.c:92
    #2  0x0000003620a67324 in g_assertion_message (domain=<value optimized out>, file=<value optimized out>, line=<value optimized out>, func=0x50fcc0 "virtual GParted::Partition& GParted::OperationDelete::get_partition_new()", message=0x1b55f60 "assertion failed: (false)") at gtestutils.c:1358
    #3  0x0000003620a678f0 in g_assertion_message_expr (domain=0x0, file=0x50fa68 "OperationDelete.cc", line=41, func=0x50fcc0 "virtual GParted::Partition& GParted::OperationDelete::get_partition_new()", expr=<value optimized out>) at gtestutils.c:1369
    #4  0x000000000049aa0d in GParted::OperationDelete::get_partition_new (this=0x1b321b0) at OperationDelete.cc:41
    #5  0x00000000004c6700 in GParted::Win_GParted::activate_delete (this=0x7ffc91403670) at Win_GParted.cc:2068
    ...

As before the crash is happened in Win_GParted::activate_delete() as it
was going through the list of operations removing those which applied to
the never created partition.  It came across the delete operation of an
existing partition and called get_partition_new().  As partition_new was
not set or used by the delete operation this asserted false and crashed
GParted.

Unlike the check operation case, the delete operation doesn't have a
partition afterwards.  (As GParted represents unallocated space with
partition objects then the delete operation could be populated with a
new partition by constructing the correctly merged unallocated space
partition object, but that is what OperationDelete::apply_to_visual()
does and having a place holder doesn't seem like the right thing to do).
Instead exclude delete operations, on existing partitions, when looking
for operations applying to this not yet created partition as they are
mutually exclusive.

Bug 767233 - GParted core dump on assert failure in
             OperationDelete::get_partition_new()
2016-06-05 13:14:34 -06:00
Mike Fleetwood 1f2a50544d Prevent assert failure from OperationCheck::get_partition_new() (#767233)
Composing these operations caused GParted to abort on an assert failure:
(1) Check an existing partition,
(2) Create a new partition,
(3) Delete new partition.

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================
    **
    ERROR:OperationCheck.cc:40:virtual GParted::Partition& GParted::OperationCheck::get_partition_new(): assertion failed: (false)
    Aborted (core dumped)

    # gdb ./gpartedbin core.8876 --batch --quiet --ex backtrace -ex quit
    [New Thread 8876]
    [New Thread 8879]
    [Thread debugging using libthread_db enabled]
    Core was generated by `./gpartedbin'.
    Program terminated with signal 6, Aborted.
    #0  0x000000361f2325e5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    64	  return INLINE_SYSCALL (tgkill, 3, pid, selftid, sig);
    #0  0x000000361f2325e5 in raise (sig=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
    #1  0x000000361f233dc5 in abort () at abort.c:92
    #2  0x0000003620a67324 in g_assertion_message (domain=<value optimized out>, file=<value optimized out>, line=<value optimized out>, func=0x50f400 "virtual GParted::Partition& GParted::OperationCheck::get_partition_new()", message=0x1d37a00 "assertion failed: (false)") at gtestutils.c:1358
    #3  0x0000003620a678f0 in g_assertion_message_expr (domain=0x0, file=0x50f1a8 "OperationCheck.cc", line=40, func=0x50f400 "virtual GParted::Partition& GParted::OperationCheck::get_partition_new()", expr=<value optimized out>) at gtestutils.c:1369
    #4  0x0000000000498e21 in GParted::OperationCheck::get_partition_new (this=0x1d1bb30) at OperationCheck.cc:40
    #5  0x00000000004c66ec in GParted::Win_GParted::activate_delete (this=0x7fff031c3e30) at Win_GParted.cc:2068
    ...

When Win_GParted::activate_delete() was stepping through the operation
list removing operations (2 & 3 in the above recreation steps) which
related to the new partition never to be created it called
get_partition_new() on all operations in the list.  This included
calling get_partition_new() on the check operation (1 in the above
recreation steps).  As partition_new was not set or used by the check
operation get_partition_new() asserted false and crashed GParted.

Fix by populating the partition_new member in OperationCheck objects,
thus allowing get_partition_new() to be called on the object.  As a
check operation doesn't change any partition boundaries or file system
attributes, just duplicate the new partition from the original
partition.

Bug 767233 - GParted core dump on assert failure in
             OperationDelete::get_partition_new()
2016-06-05 13:14:34 -06:00
Mike Fleetwood cc7e412bc6 Only enable ext4 64bit feature when required (#766910)
E2fsprogs version 1.43 always creates 64bit ext4 file systems by default
[1][2] regardless of the partition size.  Previously it only enabled the
64bit feature when required on ext4 volumes 16 TiB and larger.  Also
note that RHEL / CentOS 7 always create 64bit ext4 file systems by
default from e2fsprogs 1.42.9 [3].

(At least some versions of) Grub 2 and syslinux boot loaders don't work
with 64bit ext4 file systems [4][5][6].  For maximum boot loader
compatibility make GParted implement what mke2fs previously did, only
setting the 64bit feature on volumes 16 TiB and larger and clearing it
otherwise.  Only applied to mkfs.ext4 version 1.42 and later.

[1] Release notes, E2fsprogs 1.43 (May 17, 2016)
    http://e2fsprogs.sourceforge.net/e2fsprogs-release.html#1.43

    "Mke2fs will now create file systems with the metadata_csum and
    64bit features enabled by default".

[2] http://git.kernel.org/cgit/fs/ext2/e2fsprogs.git/commit/?id=cd27af3ecb83e8fd1e3eaa14994284a1818c7c15
    mke2fs: enable the metadata_csum and 64bit features by default

[3] Comment 20 and 21 in Red Hat bug 1099237
    https://bugzilla.redhat.com/show_bug.cgi?id=1099237#c20

    "..., is rhel7 default behavior w.r.t. 64 bit really different from
    upstream ?

    Yes it is. This is what we have in RHEL7:
    Patch6: e2fsprogs-1.42.9-enable-64bit-feature-by-default.patch
    it fixed this bz: https://bugzilla.redhat.com/show_bug.cgi?id=982871
    and this is upstream proposal:
    http://www.spinics.net/lists/linux-ext4/msg42294.html"

[4] Grub 2 not working on Slackware with 64bit EXT4
    http://www.linuxquestions.org/questions/slackware-14/grub-mkconfig-error-in-slackware-current-64-bit-4175580544/

[5] Syslinux not working on Slackware with 64bit EXT4
    http://www.linuxquestions.org/questions/slackware-14/slackware64-current-5-20-2016-syslinux-booting-and-ext4-formatting-4175580324/

[6] Syslinux not working on RHEL 7 with 64bit EXT4
    Bug 1099237 - rhel7 ext4 defaults to 64 bit, which extlinux can't reliably read
    https://bugzilla.redhat.com/show_bug.cgi?id=1099237

Bug 766910 - Multiple boot loaders don't work on 64bit EXT4 file systems
2016-06-05 09:40:11 -06:00
Mike Fleetwood a13526bf41 Don't limit ext4 volume size when 64bit feature is available (#766910)
E2fsprogs 1.42 adds ext4 64bit feature [1] allowing volume sizes larger
than 16 TiB.  However only enable large volumes from e2fsprogs 1.42.9
when a large number of 64bit bugs were fixed [2].  (Also RHEL / CentOS 7
use e2fsprogs 1.42.9 and always enable 64bit feature by default).

[1] Release notes, E2fsprogs 1.42 (November 29, 2011)
    http://e2fsprogs.sourceforge.net/e2fsprogs-release.html#1.42

    "This release of e2fsprogs has support for file systems > 16TB.
    Online resize requires kernel support which will hopefully be in
    Linux version 3.2.  Offline support is not yet available for > 16TB
    file systems, but will be coming".

[2] Release notes, E2fsprogs 1.42.9 (December 28, 2013)
    http://e2fsprogs.sourceforge.net/e2fsprogs-release.html#1.42.9

    "Fixed a large number of bugs in resize2fs, e2fsck, debugfs, and
    libext2fs to correctly handle bigalloc and 64-bit file systems.
    There were many corner cases that had not been noticed in previous
    uses of these file systems, since they are not as common.  Some of
    the bugs could cause file system corruption or data loss, so users
    of 64-bit or bigalloc file systems are strongly urged to upgrade to
    e2fsprogs 1.42.9".

Bug 766910 - Multiple boot loaders don't work on 64bit EXT4 file systems
2016-06-05 09:40:11 -06:00
Mike Fleetwood af17808d08 Limit maximum ext2/3/4 volume size to just less than 16 TiB (#766910)
Maximum size of an ext2/3/4 volume is 2^32 - 1 blocks [1], ignoring ext4
with 64bit feature.  That is just under 16 TiB with a 4K block size.

    # truncate -s 16T /mnt/1/sparse.img
    # ls -l /mnt/1/sparse.img
    -rw-r--r--. 1 root root 17592186044416 May 28 19:33 /mnt/1/sparse.img
    # losetup /dev/loop0 /mnt/1/sparse.img
    # mkfs.ext3 /dev/loop0
    mke2fs 1.43 (17-May-2016)
    mkfs.ext3: Size of device (0x100000000 blocks) /dev/loop0 too big to be expressed
            in 32 bits using a blocksize of 4096.
    # losetup -d /dev/loop0
    #
    #
    # truncate -s $((16*1024**3-4))K sparse.img
    # ls -l /mnt/1/sparse.img
    -rw-r--r--. 1 root root 17592186040320 May 28 19:51 /mnt/1/sparse.img
    # losetup /dev/loop0 /mnt/1/sparse.img
    # mkfs.ext3 /dev/loop0
    mke2fs 1.43 (17-May-2016)
    Discarding device blocks: done
    Creating filesystem with 4294967295 4k blocks and 536870912 inodes
    Filesystem UUID: 9721d8d9-8711-499b-aae4-8ea4d9e16ca2
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
            4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
            102400000, 214990848, 512000000, 550731776, 644972544, 1934917632,
            2560000000, 3855122432

    Allocating group tables: done
    Writing inode tables: done
    Creating journal (32768 blocks): done
    Writing superblocks and filesystem accounting information: done

    # losetup -d /dev/loop0
    # rm /mnt/1/sparse.img

Actually limit the maximum volume size to 1 MiB less than 16 TiB for
coding reasons explained in the FIXME comment.

[1] Ext4 Disk Layout, Blocks
    https://ext4.wiki.kernel.org/index.php/Ext4_Disk_Layout#Blocks

Bug 766910 - Multiple boot loaders don't work on 64bit EXT4 file systems
2016-06-05 09:40:11 -06:00
Mike Fleetwood 3724421e30 Stop leaking PedGeometry object memory (#767009)
Calling libparted ped_geometry_new() creates a new PedGeometry object
from malloced memory, however the corresponding ped_geometry_destroy()
is never called to destroy the object and free the memory.

Perform a resize of a FAT file system when running GParted under
valgrind identifies several memory blocks leaked via ped_geometry_new()
from resize_move_filesystem_using_libparted().  One such example:

    # valgrind --track-origins=yes --leak-check=full ./gpartedbin
    ...
    ==32069== 32 bytes in 1 blocks are definitely lost in loss record 5,419 of 11,542
    ==32069==    at 0x4C29BFD: malloc (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==32069==    by 0x8ECD8C5: ped_malloc (libparted.c:231)
    ==32069==    by 0x8ED23C1: ped_geometry_new (geom.c:79)
    ==32069==    by 0x4764F3: GParted::GParted_Core::resize_move_filesystem_using_libparted(GParted::Partition const&, GParted::Partition const&, GParted::OperationDetail&) (GParted_Core.cc:2666)
    ==32069==    by 0x478007: GParted::GParted_Core::resize_filesystem(GParted::Partition const&, GParted::Partition const&, GParted::OperationDetail&, bool) (GParted_Core.cc:2990)
    ==32069==    by 0x478440: GParted::GParted_Core::maximize_filesystem(GParted::Partition const&, GParted::OperationDetail&) (GParted_Core.cc:3037)
    ==32069==    by 0x4769A0: GParted::GParted_Core::resize(GParted::Partition const&, GParted::Partition const&, GParted::OperationDetail&) (GParted_Core.cc:2746)
    ==32069==    by 0x47582B: GParted::GParted_Core::resize_move(GParted::Partition const&, GParted::Partition&, GParted::OperationDetail&) (GParted_Core.cc:2457)
    ==32069==    by 0x46DDB2: GParted::GParted_Core::apply_operation_to_disk(GParted::Operation*) (GParted_Core.cc:767)
    ...

There is also a leak of a PedGeometry object from
resize_move_partition().  Fix by calling ped_geometry_destroy() to
delete all the allocated PedGeometry objects and free the memory.

Bug 767009 - PedGeometry objects are memory leaked
2016-05-30 10:59:31 -06:00
Mike Fleetwood 29be7547cd Remove outdated comment fragment from activate_mount_partition()
Outdated by commit:
    6e97a63f49
    Always use blkid file system detection before libparted (#757781)
2016-05-20 09:25:48 -06:00
Mike Fleetwood ab923121de Replace whole path list after calibrate in apply_operation_to_disk() (#766349)
When replacing the list of paths for the other partition object involved
in either a Resize/Move or Format operation in apply_operation_to_disk()
should replace the whole list of partitions not just replace with the
first path.  Copy the whole path list is the correct action.  It makes
no material difference because secondary partition paths are only used
to discover mount points during refresh phase and not at the apply
phase, where only the primary path is used.

Bug 766349 - Resolve code ugliness with partition path getting set to
             "copy of /dev/SRC"
2016-05-20 09:25:35 -06:00
Mike Fleetwood b77fef0dd5 Stop relying on sort order when adding real paths in calibrate (#766349)
Quoting the relevant comments from GParted_Core::calibrate_partition():
    Re-add the real partition path from libparted.

    When creating a copy operation by pasting into unallocated space the
    list of paths for the partition object was set to
    ["Copy of /dev/SRC"] because the partition didn't yet exist before
    the operations were applied.  Additional operations on that new
    partition also got the list of paths set to ["Copy of /dev/SRC"].
    This re-adds the real path to the start of the list making it
    ["/dev/NEW", "Copy of /dev/SRC"].  This provides the real path for
    file system specific tools used during those additional operations
    such mkfs for the format operation or fsck and others for the
    resize/move operation.

    FIXME: Having this work just because "/dev/NEW" happens to sort
    before "Copy of /dev/SRC" is ugly!  Probably have a separate display
    path which can be changed at will without affecting the list of real
    paths for the partition.

Having a separate display path is overly complicated and unnecessary.
Just replace the list of paths with the real one reported by libparted
if it contained "Copy of /dev/SRC", determined by checking if the file
exists.  Otherwise continue to add the libparted name as an alternate
path.  Whole disk devices can never be named "Copy of /dev/SRC" because
they are not partitioned so never created or deleted by GParted, only
ever written to, hence don't need the extra exists test logic.

Bug 766349 - Resolve code ugliness with partition path getting set to
             "copy of /dev/SRC"
2016-05-20 09:20:02 -06:00
Mike Fleetwood 302cc8041e Stop overriding real path when pasting into existing partitions (#766349)
When composing a copy operation it always named the destination
partition as "copy of /dev/SRC".  For the case of pasting into
unallocated space creating a new partition this was the right thing to
do as the partition doesn't yet exist so the path is not yet known.
However for the case of pasting into an existing partition the path is
known and replacing it with "copy of /dev/SRC" is wrong.  No other
operation when operating on an existing partition changes it path.

Given a set of existing partitions, sdb1 to sdb4, compose a set of copy
operations as: copy sdb1 to sdb2, copy sdb2 to sdb3 and copy sdb3 to
sdb4.  The displayed partitions before being applied become:
    /dev/sdb1
    copy of /dev/sdb1
    copy of copy of /dev/sdb1
    copy of copy of copy of /dev/sdb1
And the pending operations are named:
    Copy /dev/sdb1 to /dev/sdb2
    Copy copy of /dev/sdb1 to /dev/sdb3
    Copy copy of copy of /dev/sdb1 to /sev/sdb4

This is perverse.  In the case of pasting into an existing partition
keep the real path name.  This keeps the partitions being displayed as:
    /dev/sdb1
    /dev/sdb2
    /dev/sdb3
    /dev/sdb4
And the pending operations named as:
    Copy /dev/sdb1 to /dev/sdb2
    Copy /dev/sdb2 to /dev/sdb3
    Copy /dev/sdb3 to /dev/sdb4
Much more understandable.

Also switch to an upper case "C" in "Copy of /dev/SRC" as the temporary
path name when pasting into unallocated space.  Finally update the
comment in calibrate_partition() to describe the remaining cases when
re-adding the path is still required.

Bug 766349 - Resolve code ugliness with partition path getting set to
             "copy of /dev/SRC"
2016-05-20 09:20:02 -06:00
Mike Fleetwood 94979a3805 Add symbolic constants SETTLE_DEVICE_*_MAX_WAIT_SECONDS
Make the code a little more self documenting by adding the symbolic
constants:
    SETTLE_DEVICE_APPLY_MAX_WAIT_SECONDS
    SETTLE_DEVICE_PROBE_MAX_WAIT_SECONDS
which highlight that settle_device() is called in two different
contexts, device probe and apply operations, with two different timeout
values.
2016-04-18 12:56:51 -06:00
Mike Fleetwood fd9013d5f6 Wait for udev to recreate /dev/PTN entries when calibrating (#762941)
File system specific commands sometimes fail reporting that the
partition specific /dev entry doesn't exist.  Example failing check
operation details:

    Check and repair file system (ext4) on dev/sdb4
      calibrate /dev/sdb4
        path: /dev/sdb4 (partition)
        start: 4196352
        end: 6293503
        size: 2097152 (1.00 GiB)
      check file system on /dev/sdb4 for errors and (if possible) fix them
        e2fsck -f -y -bv -C 0 /dev/sdb4
          e2fsck 1.42.9 (28-Dec-2013)
          e2fsck: No such file or directory while trying to open /dev/sdb4
          Possibly non-existent device?

This has been reproduced on CentOS 7.  Debugging shows that the
libparted calls used to re-read the partition details in
GParted_Core::calibrate_partition() leads to udev removing and re-adding
all the partition /dev entries for the disk.

    # udevadm monitor &
    # gpartedbin
    ...
     16.480662 +12.618659 calibrate_partition()          calling get_device("/dev/sdb", lp_device) ...
     16.483644 +0.002982 calibrate_partition()          get_device() returned
     16.483678 +0.000034 calibrate_partition()          calling get_disk(lp_device, lp_disk) ...
     16.618113 +0.134435 calibrate_partition()          get_disk() returned
    KERNEL[19275.707968] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
     16.618561 +0.000448 destroy_device_and_disk()      calling ped_disk_destroy(lp_disk) ...
     16.618584 +0.000023 destroy_device_and_disk()      ped_disk_destroy() returned
     16.618591 +0.000007 destroy_device_and_disk()      calling ped_device_destroy(lp_disk) ...
     16.618602 +0.000011 destroy_device_and_disk()      ped_device_destroy() returned
     16.618687 +0.000085 calibrate_partition()          return true
     16.618851 +0.000164 execute_command()              e2fsck -f -y -v -C 0 /dev/sdb4
    KERNEL[19275.708389] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[19275.708500] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    KERNEL[19275.708643] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    KERNEL[19275.768278] change   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb (block)
    KERNEL[19275.771171] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    KERNEL[19275.771360] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    KERNEL[19275.771542] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    KERNEL[19275.775858] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [19275.820153] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [19275.823152] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [19275.828275] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
     16.742735 +0.123884 execute_command()              exit status 8
    UDEV  [19275.841425] remove   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)
    UDEV  [19275.905478] change   /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb (block)
    UDEV  [19276.013580] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb3 (block)
    UDEV  [19276.034728] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb4 (block)
    UDEV  [19276.174840] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb1 (block)
    UDEV  [19276.237105] add      /devices/pci0000:00/0000:00:0d.0/ata4/host3/target3:0:0/3:0:0:0/block/sdb/sdb2 (block)

So exactly when GParted is running the external e2fsck command, udev is
in the middle of removing and re-adding all the /dev partition entries
for the disk.  Hence the above failure reporting that /dev/sdb4 didn't
exist.  This error depends on the timing between GParted running the
external file system specific command and udev removing and re-adding
the entries, so sometimes it works and sometimes it fails.

Further debugging showed that simply opening and closing the whole disk
device read-write triggers the same removing and re-adding of all the
partition /dev entries with udev >= 219.  Opening the whole disk device
read-write is what libparted has always done until this post
libparted 3.2 patch to make it open read-only when probing:

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=44d5ae0115c4ecfe3158748309e9912c5aede92d
    libparted: Use read only when probing devices on linux (#1245144)

To fix this simply wait for udev devices to settle in
calibrate_partitions().  The longest I have seen udev take to do this is
0.80 seconds in a VM.  Wait up to 10 seconds as is done in commit() ->
commit_to_os(), also called when applying operations.

On configurations which don't have this issue execution of udevadm
settle, which will return immediately, adds at most 0.1 seconds to the
time taken for the calibrate step.  This won't be noticed in the time
taken of the operation details so there is no point in trying to avoid
executing udevadm settle when not needed.

Bug 762941 - Operations sometimes failing with: No such file or
             directory
2016-04-18 12:56:51 -06:00
Mike Fleetwood e0a208576d Minor tidyup in load_proc_partitions_info_cache()
Minor issues:
1) In the while loop reading from /proc/partitions into variable line,
   just after the sscanf() call the variable was re-purposed to hold the
   device name making the code unnecessarily hard to follow.
2) Variable c_str was a fixed sized buffer holding the device name read
   from /proc/partitions.
3) Variable c_str name provides no meaning as to what data it holds.
4) Return value from all the Utils::regexp_label() calls is converted
   from Glib::ustring to std::string to be stored in device variable.

Resolve by using Utils::regexp_label() to extract the device name from
each line in /proc/partitions and store in the variable device, already
used for this purpose and now changed to type Glib::ustring.
2016-04-07 10:05:40 -06:00
Mike Fleetwood d04826cc27 Use realpath() safely (#764369)
realpath(3) manual page says:

    BUGS
        The POSIX.1-2001 standard version of this function is broken by
        design, since it is impossible to determine a suitable size for
        the output buffer, resolved_path.  According to POSIX.1-2001 a
        buffer of size PATH_MAX suffices, but PATH_MAX need not be a
        defined constant, and may have to be obtained using pathconf(3).
        And asking pathconf(3) does not really help, since, on the one
        hand POSIX warns that the result of pathconf(3) may be huge and
        unsuitable for mallocing memory, and on the other hand
        pathconf(3) may return -1 to signify that PATH_MAX is not
        bounded.  The resolved_path == NULL feature, not standardized in
        POSIX.1-2001, but standardized in POSIX.1-2008, allows this
        design problem to be avoided.

The resolved_path == NULL feature of realpath() has existed as a Glibc
extension since realpath() was first added to Glibc 1.90, released in
June 1996.  Therefore it can be used unconditionally.

    https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=fa0bc87c32d02cd81ec4d0ae00e0d943c683e6e1

Bug 764369 - Use realpath() safely
2016-04-07 10:05:40 -06:00
Mike Fleetwood a681f9f637 Replace 32-bit member variable "index" with wider local variables (#764658)
The previous commit (Fix crash reading NTFS usage when there is no
/dev/PTN entry) identified that the FileSystem member variable "index"
is too small on 64-bit machines.  Also this member variable stores no
FileSystem class information and was being used as a local variable.

Replace with local variables of the of the correct type, wide enough to
store the npos not found value.

Bug 764658 - GParted crashes when reading NTFS usage when there is no
             /dev/PTN entry
2016-04-07 09:56:00 -06:00
Mike Fleetwood 366152e449 Fix crash reading NTFS usage when there is no /dev/PTN entry (#764658)
On a 64-bit distribution, with an NTFS file system in a partition
without a /dev entry then GParted will crash when attempting to read
the file system usage.  Not having a /dev entry for the partition is
rare and only known to occur for the disk devices used within Fake RAID
(dmraid) arrays, and then only on Ubuntu 12.04 LTS.  Other/newer
distributions do create /dev entries for partitions found on disk
devices within Fake RAID arrays.

Create mirror Fake RAID array:
    # dmraid -f isw -C MyArray --type 1 --disk /dev/sdc,/dev/sdd
    # dmraid -ay

Create NTFS partition on the Fake RAID array.  On refresh GParted
crashes:
    # ./gpartedbin
    (gpartedbin:590): glibmm-ERROR **:
    unhandled exception (type std::exception) in signal handler:
    what: basic_string::assign

Without a /dev/sdc1 device entry the ntfsresize command reports this:
    # ntfsresize --info --force --no-progress-bar /dev/sdc1
    ntfsresize v2015.3.14 (libntfs-3g)
    ERROR(2): Failed to check '/dev/sdc1' mount state: No such file or directory
    Probably /etc/mtab is missing. It's too risky to continue. You might try
    an another Linux distro.

The problem code in ntfs::set_used_sectors():
    145         index = output.find( "Cluster size" );
    146         if ( index == output.npos ||
    147              sscanf( output.substr( index ).c_str(), "Cluster size       : %Ld", &S ) != 1 )
As "Cluster size" did not exist in the output find() returned the not
found token of string::npos [1], which in a 64-bit environment is
represented by 2^64-1 [2].  However it was saved in the variable index
of type unsigned integer, which is only a 32-bit integer, thus
truncating it to 2^32-1.  Therefore the comparison failed and sscanf()
tried to parse the output starting at offset 2^32-1 which resulted in
the crash.

Introduced by commit:
    324d99a172
    Record file system block size where known (#760709)

Fix by following the same pattern of the other comparisons in
ntfs::set_used_sectors() which checks if index is less than the output
length.

References:
[1] std::string::find
    http://www.cplusplus.com/reference/string/string/find/
[2] std::string::npos
    http://www.cplusplus.com/reference/string/string/npos/
(Note that Glib::ustring is derived from std::string in the Standard C++
library and provides a compatible interface).

Bug 764658 - GParted crashes when reading NTFS usage when there is no
             /dev/PTN entry
2016-04-07 09:56:00 -06:00
Mike Fleetwood 2bb78cf8dc Limit FAT32 maximum volume size to 2 TiB
GParted is allowing creation of a FAT32 formatted partition of any size.
However with a 512 byte sector size the maximum volume size of a FAT32
file system is reported to be 2 TiB.
* Wikipedia: File Allocation Table / FAT32
  https://en.wikipedia.org/wiki/File_Allocation_Table#FAT32
  "The boot sector uses a 32-bit field for the sector count, limiting
  the FAT32 volume size to 2 TB for a sector size of 512 bytes and 16 TB
  for a sector size of 4,096 bytes."
* Microsoft: Default cluster size for NTFS, FAT, and exFAT / Default
  cluster sizes for FAT32
  https://support.microsoft.com/en-us/kb/140365

Trying to create a FAT32 file system in a partition larger than 2 TiB
results in unallocated space being left after the file system.

Nuances:
[1] Larger sector sizes allow larger maximum volume sizes up to 16 TiB
    with 4096 byte sectors.
[2] mkdosfs/mkfs.fat has an -S SECTOR_SIZE option which allows changing
    the "logical" sector size of the file system allowing the maximum
    volume to be proportionally increased.
[3] mkfs.fat appears to have an signed overflow bug when the size of the
    partition is larger than maximum signed 32-bit integer of logical(?)
    sectors.  (2 TiB for a sector size of 512 bytes).  It reports the
    partition size as minus size and creates a 1 TiB file system.

GParted wants a single maximum file system size and the code is not
ready for a differing maximum file system size for different sector
sizes.

In fat16::create() could specify larger "logical" sector sizes to
mkfs.fat when the partition is larger than 2 TiB to allow maximum volume
size to be increased further.  However that will take a lot of cross
platform testing to ensure that all sorts of devices support "logical"
sector sizes other than 512 bytes on devices with a hardware sector size
of 512 bytes.  This is too much effort.

Therefore implement a single FAT32 maximum volume size of 2 TiB.

Bug 763896 - GParted not restricting creation of FAT32 volume to maximum
             size of 2 TiB
2016-03-19 10:38:36 -06:00
Mike Fleetwood 1358a5f4fe Use a single progress bar for the internal block copy operation (#762367)
As part of the internal block copy operation 5 initial ranges of blocks
are copied using different block sizes to determine the fastest.  Then
the remainder is copied using the fastest block size.  Each of these
copies reports progress independently, so during the benchmarking phase
the progress bar flashes 5 times as it goes from 0 to 100% in a fraction
of a second, before showing the progress of the remainder.

This looks bad, so report a single progress bar for all the ranges of
blocks copied in a single copy operation.

Already have variables done and length which track progress within each
copied range; and total_done which records amount copied in previous
ranges.  Just add total_length to allow overall progress to be reported.

Bug 762367 - Use a single progress bar for the whole of the internal
             copy operation
2016-02-23 10:41:20 -07:00
Mike Fleetwood 2f2280e3d5 Update internal block copy, total_done after last progress report (#762367)
Previously total_done was updated in copy_thread() after copying of the
blocks, but importantly before the last call to set_progress_info() to
update the progress bar.  Having total_done varying during the copy of a
single range of blocks, single call to copy_blocks::copy(), is an
impediment to being able to report a single progress bar across the
whole internal copy operation.

Move updating of total_done to copy() immediately after copy_thread()
completes.

Bug 762367 - Use a single progress bar for the whole of the internal
             copy operation
2016-02-23 10:41:19 -07:00
Mike Fleetwood 6d28a62077 Display progress of NTFS file system specific copy operation (#762366)
Copying of ntfs is performed using ntfsclone, which writes progress
indication to standard output like this:

    # ntfsclone -f /dev/sdb2 /dev/sdb1 2> /dev/null
    NTFS volume version: 3.1
    Cluster size       : 4096 bytes
    Current volume size: 21474832384 bytes (21475 MB)
    Current device size: 21474836480 bytes (21475 MB)
    Scanning volume ...
    100.00 percent completed
    Accounting clusters ...
    Space in use       : 1832 MB (8.5%)
    Cloning NTFS ...
    100.00 percent completed
    Syncing ...

Add ntfsclone progress tracker for ntfsclone command.  Deliberately
doesn't stop the progress bar.  See comment in ntfs::clone_progress()
for the explanation.

Bug 762366 - Add progress bar to NTFS file system specific copy method
2016-02-23 10:02:03 -07:00
Mike Fleetwood 822028b504 Always be explicit when emitting a signal by calling emit()
Mostly the code is explicit and calls the emit() method when emitting a
signal [1], like this:
    signal_name.emit();

However there are a few cases which use the function call operator on
the signal object [2], like this:
    signal_name();

The behaviour is identical [3] but it is preferred to be explicit that a
signal callback is being initiated, and it also makes them much easier
to search for too.

[1] List explicit emit() signal calls
        fgrep '.emit(' src/*.cc

[2] List function call operator emitted signals
        egrep "`sed -n '/sigc::signal/s/.*sigc::signal.*> *\([a-zA-Z_]*\).*/\1/p' include/*.h | tr '\n' '|' | sed 's/\(.*\).$/[^a-zA-Z_](\1)\\\(/'`" src/*.cc

[3] Quote from the libsigc++ Reference Manual, class sigc::signal
    https://developer.gnome.org/libsigc++/stable/classsigc_1_1signal7.html#ab37db0ecc788824d0baa3c301efc8dcd

        result_type sigc::signal<...>::operator()(...)

        Triggers the emission of the signal (see emit())
2016-02-12 09:09:57 -07:00
Mike Fleetwood 2a55c65876 Leave commands with progress bars shown in the applying dialog (#760709)
For non-progress tracked external commands the command being executed is
displayed in the Apply pending operations dialog, just below the top
pulsing progress bar.  However for progress tracked external commands
the description of the parent operation detail is displayed instead.

Example 1: non-progress tracked xfs check repair:

                                                               TreePath
    Check and repair file system (xfs) on /dev/sdc1            0
    + calibrate /dev/sdc1                                      0:0
    + check file system on /dev/sdc1 for errors and (if po...  0:1
      + xfs_repair -v /dev/sdc1                                0:1:0
        + [empty stdout]                                       0:1:0:0
        + [stderr]Phase 1 - find and verify superblock...      0:1:0:1

"xfs_repair -v /dev/sdc1" (TreePath 0:1:0) is shown because it is the
latest updated operation detail which is timed (set to status
executing).

Example 2: progress tracked ext4 copy using e2image:

                                                               TreePath
    Copy /dev/sdc2 to /dev/sdc3                                0
    + calibrate /dev/sdc2                                      0:0
    + check file system on /dev/sdc2 for errors and (if po...  0:1
    + set partition type on /dev/sdc3                          0:2
    + copy file system of /dev/sdc2 to /dev/sdc3               0:3
      + e2image -ra -p /dev/sdc2 /dev/sdc3                     0:3:0
        + [stdout]Scanning inodes...                           0:3:0:0
        + [stderr]e2image 1.42.9 (28-Dec-2013)...              0:3:0:1

"copy file system of /dev/sdc2 to /dev/sdc3" (TreePath 0:3) is shown
because that operation detail is also timed and it is being constantly
updated by the progress bar updates via it.

Change execute_command() to update the progress bar via the operation
detail it creates to hold the command being executed, instead of the
parent operation detail, to resolve the above.  Also replaces calling
operationdetail.get_last_child() throughout the method.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 075154b4e8 Simplify XFS copy specific code progress bar usage (#760709)
Remove starting and stopping the progress bar in xfs::copy().  The
progress bar will be automatically started in xfs::copy_progress()
callback when run_progressbar() is called; and automatically stopped in
FileSystem::execute_command() when it calls stop_progress() at the end.

Note that this will now not initialise the progress bar from zero
immediately that the XFS copy is started, but instead 0.5 seconds into
the copy when xfs::copy_progress() timed callback is called for the
first time.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 438b35aed9 Connect timed progress tracking callbacks inside execute_command() (#760709)
The timed progress tracking callback for execution of xfs copy follows
this pattern:

    sigc::connection c;
    ...
    c = Glib::signal_timeout().connect( ... sigc::mem_fun( *this, &xfs::copy_progress ) ..., 500 /*ms*/ );
    ... execute_command( ... );
    c.disconnect();

As with output progress tracking callbacks for ext2/3/4 and ntfs file
system specific commands, pass the callback slot and a flag into
execute_command() and connect the timed callback inside.  This
simplified the pattern to:

    ... execute_command( ...|EXEC_PROGRESS_TIMED,
                         static_cast<TimedSlot>( sigc::mem_fun( *this, &xfs::copy_progress ) ) );

NOTE:
The type of sigc::mem_fun() doesn't allow the compiler to choose between
the two overloaded variants of execute_command() with the fourth
parameter of either (full types without typedefs of StreamSlot and
TimedSlot respectively):
    sigc::slot<void, OperationDetail *> stream_progress_slot
    sigc::slot<bool, OperationDetail *> timed_progress_slot
Therefore have to cast the result of all callback slots to the relevant
type.  Hence:
    static_cast<StreamSlot>( sigc::mem_fun( *this, &{CLASS}::{NAME}_progress ) )
    static_cast<TimedSlot>( sigc::mem_fun( *this, &xfs::copy_progress ) )

References:
*   [sigc] Functor not resolving between overloaded methods with
    different slot types
    https://mail.gnome.org/archives/libsigc-list/2016-February/msg00000.html
*   Bug 306705 - Can't overload methods based on different slot<>
    parameters.
    https://bugzilla.gnome.org/show_bug.cgi?id=306705

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood e67bbe906f Remove the unnecessary signal_progress (#760709)
For the relevant stream from a file system specific command being
tracked, there were 2 callbacks attached: update_command_output() and
update_command_progress().  When called, update_command_progress() just
emitted signal_progress to call the file system specific progress
tracker callback.  Like this:

    signal_update.emit() -> update_command_output()
                         -> update_command_progress()
                                signal_progress.emit() -> {CLASS}::{NAME}_progress()

Instead just connect the file system specific progress tracker callback
directly to signal_update and bypass the unnecessary
update_command_progress() method and the signal_progress signal.  Like
this:

    signal_update.emit() -> update_command_output()
                         -> {CLASS}::{NAME}_progress()

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood c00927c23d Connect output progress tracking callbacks inside execute_command() (#760709)
All the output progress tracking callbacks for execution of ext2/3/4 and
ntfs file system specific commands followed this pattern:

    sigc::connection c = signal_progress.connect( sigc::mem_fun( *this, &ext2::..._progress ) );
    bool success = ! execute_command( ... );
    c.disconnect();
    return success;

Instead, pass the callback slot and a flag into execute_command() and
connect the callback inside.  This simplifies the pattern to:

    return ! execute_command( ...|EXEC_PROGRESS_STDOUT,
                              sigc::mem_fun( *this, &ext2::..._progress ) );

Note that as the progress tracking callbacks are only registered against
updates to the relevant stream from the tracked commands they won't be
called when the other stream is updated any more.

Also note that signal_progress is a member of the FileSystem class and
derived objects so lives as long as GParted is running, therefore the
progress tracking callbacks need explicitly disconnecting at the end of
execute_command().  However signal_update is a member of the PipeCapture
class of which the output and error local variables in execute_command()
are types.  Therefore there is no need to explicitly disconnect the
signal_update callbacks as they will be destructed along with the
callback slots when they go out of scope at the end of the
execute_command() method.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 27e30a570f Remove unused OperationDetail members (#760709)
Remove unused members: fraction and progress_text from the
OperationDetail class now that the ProgressBar class has superseded
their use.  This also allows removal of timer_global member from the
copy_blocks class.  Timer_global was only used to track the elapsed time
copying blocks and allow the remaining time to be estimated and written
into progress_text.  The ProgressBar class also does this itself
internally.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood b1313281bd Simplify use of the progress bar via OperationDetail (#760709)
Most of the file system specific command progress trackers followed this
pattern:

    void {CLASS}::{NAME}_progress( OperationDetail *operationdetail )
    {
            ProgressBar & progressbar = operationdetail->get_progressbar();
            // parse output for progress and target values
            if ( // have progress and target values )
            {
                    if ( ! progressbar.running() )
                            progressbar.start( target );
                    progressbar.update( progress );
                    operationdetail->signal_update( *operationdetail );
            }
            else if ( // found progress finished )
            {
                    if ( progressbar.running() )
                            progressbar.stop();
                    operationdetail->signal_update( *operationdetail );
            }
    }

That is a lot of repetition handling progress bar updates and
OperationDetail object update signalling.  Remove the need for direct
access to the single ProgressBar object and provide these two
OperationDetail methods instead:
    // Start and update in one
    run_progressbar( progress, target, optional text_mode );
    stop_progressbar();

Now the file system specific command progress trackers can become:

    void {CLASS}::{NAME}_progress( OperationDetail *operationdetail )
    {
            // parse output for progress and target values
            if ( // have progress and target values )
            {
                    operationdetail->run_progressbar( progress, target );
            }
            else if ( // found progress finished )
            {
                    operationdetail->stop_progressbar();
            }
    }

Make ProgressBar::get_progressbar() a private method to enforce use of
the new way to access the progress bar via the run_progress() and
stop_progressbar() methods.  Then make the Dialog_Progress a friend
class to OperationDetail so that the Apply pending operations dialog can
still access the single ProgressBar object for its querying needs.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 32622f4d57 Display progress of ext2/3/4 file system specific copy and move operations (#760709)
Using e2image to copy a file system looks like this.  (Intermediate
progress lines which are constantly overwritten are indicated with ">").
    # e2image -ra -p /dev/sdb4 /dev/sdb5
    e2image 1.42.13 (17-May-2015)
    Scanning inodes...
>   Copying 0 / 276510 blocks (0%)
>   Copying 8845 / 276510 blocks (3%)
>   Copying 48433 / 276510 blocks (18%)
>   Copying 77135 / 276510 blocks (28%)
>   Copying 111311 / 276510 blocks (40%)
>   Copying 137039 / 276510 blocks (50%)
>   Copying 166189 / 276510 blocks (60%) 00:00:03 remaining at 108.20 MB/s
>   Copying 190285 / 276510 blocks (69%) 00:00:03 remaining at 106.19 MB/s
>   Copying 209675 / 276510 blocks (76%) 00:00:02 remaining at 102.38 MB/s
>   Copying 238219 / 276510 blocks (86%) 00:00:01 remaining at 103.39 MB/s
>   Copying 256692 / 276510 blocks (93%) 00:00:00 remaining at 100.27 MB/s
    Copied 276510 / 276510 blocks (100%) in 00:00:10 at 108.01 MB/s

Note that the copying figures are reported in file system block size
units and the progress information is written to stderr, hence needing
these two previous commits:
    Record file system block size where known (#760709)
    Call any FS specific progress trackers for stderr updates too (#760709)

Add progress tracking function for e2image command.  Also tracks when
the text progress indicator has passed in the output so that the
progress bar can be stopped as well as started when needed.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 965d88d197 Call any FS specific progress trackers for stderr updates too (#760709)
So far the signal_progress callback slot was only emitted when standard
output from the file system specific command was updated.  This was okay
as all the commands until now wrote their progress information to
stdout.  However e2image writes its progress information to stderr,
therefore also emit signal_progress when stderr is updated too.

This does mean that the file system specific *_progress() tracking
callbacks will be called when either of the OperationDetail objects
containing stdout or stderr are updated.  Therefore the trackers may be
called when there is no update to the stream from which it is parsing
the progress information.  This is not a problem as the tracker will
just update the progress bar with the same information it already has.
Also it won't happen much as only e2image is known to write to both
streams, and then only one line to stdout and the updated progress
information to stderr.  This is just an observation and not an issue
which needs coding around.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 324d99a172 Record file system block size where known (#760709)
Record the file system block size in the Partition object.  Only
implemented for file systems when set_used_sectors() method has already
parsed the value or can easily parse the value from the existing
executed command(s).

Needed for ext2/3/4 copies and moves performed using e2image so that
they can be tracked in bytes by the ProgressBar class as e2image reports
progress in file system block size units.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 809a7e0954 Display progress of XFS file system specific copy operation (#760709)
XFS uses a file system specific method to copy the partition using
"xfsdump | xfsrestore".  Monitor progress by periodically querying the
destination file system usage and comparing to the source file system
usage.  Use 0.5 seconds as the polling interval to match that used by
the internal block copying algorithm.

NOTE:
The number of used blocks in the source and destination file system will
be very close but may not match exactly.  I have seen an XFS copy finish
with the following progress text:
    1.54 GiB of 1.50 GiB copied (-00:00:02 remaining)
Allow the progress bar to overrun like this as it is informing the user
that it actually copied a little more data and took a little longer than
expected.  Needs these two previous commits to correctly round and
format the negative time remaining:
    Fix rounding of negative numbers (#760709)
    Fix formatting of negative time values (#760709)

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood b0bd465098 Fix formatting of negative time values (#760709)
... to display a negative sign before the hours, minutes and seconds.
Before:
    Utils::format_time(-1)   = "00:00:0-1"
    Utils::format_time(-119) = "00:0-1:0-59"
After:
    Utils::format_time(-1)   = "-00:00:01"
    Utils::format_time(-119) = "-00:01:59"

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 7049a8bc44 Fix rounding of negative numbers (#760709)
Utils::round() was doing +0.5 then truncate.  Correct for positive
values.  Wrong for negative values.
E.G.
    Utils::round(-1.4)
        = trunc(-1.4 + 0.5)
        = trunc(-0.9)
        = 0
Round of -1.4 is definitely not 0.  Fix this for negative values by
subtracting 0.5 then truncating.

Reference:
    How can I convert a floating-point value to an integer in C?
    https://www.cs.tut.fi/~jkorpela/round.html

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood af0ed90d49 Fix ntfs resize progress tracker matching spurious text (#760709)
When the ntfs resize operation had almost completed, percentage complete
was >= 99.9%, the progress tracker was passing 0.04 (4%) to the progress
bar.  After reading the next chunk of output from the ntfsresize command
the last line contained this text:
    "  4)  set the bootable flag for the partit"

End of the ntfsresize command output for context:
    Relocating needed data ...
    100.00 percent completed
    Updating $BadClust file ...
    Updating $Bitmap file ...
    Updating Boot record ...
    Syncing device ...
    Successfully resized NTFS on device '/dev/sdd4'.
    You can go on to shrink the device for example with Linux fdisk.
    IMPORTANT: When recreating the partition, make sure that you
      1)  create it at the same disk sector (use sector as the unit!)
      2)  create it with the same partition type (usually 7, HPFS/NTFS)
      3)  do not make it smaller than the new NTFS filesystem size
      4)  set the bootable flag for the partition if it existed before
    Otherwise you won't be able to access NTFS or can't boot from the disk!
    If you make a mistake and don't have a partition table backup then you
    can recover the partition table by TestDisk or Parted's rescue mode.

This was occurring because *scanf() can't actually report failure to
match fixed text after conversion of the last variable.  See code
comment in ntfs::resize_progress() for more details.  Fix by using
.find() instead to match the required "percent completed" explicit text
of the progress information when it appears on the last line.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:57 -07:00
Mike Fleetwood 9f7a38e6b3 Update ntfs resize progress tracker to use the new ProgressBar (#760709)
Adapt the ntfs resize progress tracker to use the new ProgressBar class.
Also make it track when the text progress indicator has passed in the
output so that the progress bar can be stopped as well as started when
needed.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood 97f836869f Update ext2 fsck progress tracker to use the new ProgressBar (#760709)
Adapt the ext2 fsck progress tracker to use the new ProgressBar class.
Also make it track when the text progress bar has completely passed in
the output so that the progress bar can be stopped as well as started
when needed.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood ac949e3003 Update ext2 create progress tracker to use the new ProgressBar (#760709)
Adapt the ext2 create file system progress tracker to used the new
ProgressBar class.  Also make it track when the text progress indicator
completes so that the progress bar can be stopped as well as started
when needed.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood 608060f82d Update ext2 resize progress tracker to use the new ProgressBar (#760709)
Adapt the ext2 resize progress tracker to the new ProgressBar class.
Also update the progress function to track when text progress bars have
completely passed in the output so that the progress bar can be stopped
as well as started when needed.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood b0d9d2de7e Display progress from the single ProgressBar in the GUI (#760709)
Change the Applying pending operations dialog so that it takes it source
of progress from the single ProgressBar object, rather than the fraction
value in every OperationDetail object.  Also remove ProgressBar
debugging now that it is being used to drive the UI.

NOTE:
This temporarily causes the existing file system specific progress bars
to not be shown because they still update via the fraction member in
each OperationDetail object, rather than the new ProgressBar.  This will
be corrected in following commits.

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood c3669c3a96 Add a single ProgressBar for all OperationDetail objects (#760709)
1) Multiple progress bars

The OperationDetail class contains member fraction which is used to feed
data to the current operation progress bar shown in the Applying pending
operations dialog.  Dialog_Progress::on_signal_update() gets called for
every updated OperationDetail object and depending on whether fraction
is > 0.0 or not, switches between showing a growing or pulsing progress
bar.  This leads to the conclusion that every OperationDetail object
currently being updated is effectively driving the single on screen
progress bar with different data.

The Copy_Blocks code is careful to update text and faction in a single
OperationDetail object and everything is good.  The on screen progress
bar is switched into growing mode and then grows to 100%.

Since external command output is updated in real time [1] there are two
OperationDetail objects, one for stdout and one for stderr, which are
updated whenever data is read from the relevant stream.  Also now that
progress is interpreted from some external command output [2][3][4] a
separate OperationDetail object is getting updated with the progress
fraction.  (Actually the grandparent OperationDetail of the ones
receiving stdout and stderr updates as used by the file system specific
*_progress() methods).  In the normal case of an external command
which is reporting it's progress two OperationDetails are constantly
being updated together, the OperationDetail object tracking stdout and
it's grandparent receiving progress fraction updates.  This causes the
the code in Dialog_Progress::on_signal_update() to constantly switch
between growing and pulsing progress bar mode.  The only reason this
doesn't flash the progress bar is because the stdout OperationDetail
object is updated first and before the 100 ms timeout fires to pulse the
bar, it's grandparent is updated with the new fraction to keep growing
the bar instead.

2) Common code

The Copy_Blocks code currently tracks the progress of blocks copied
against target amount, which it has to do anyway.  That information is
then used to generate the text and fraction to update into the
OperationDetail object and drive the on screen progress bar.  This same
level of tracking is wanted for the XFS and ext2/3/4 file system
specific copy methods.

Conclusion and solution

Having multiple sources of progress bar data is a problem and makes it
clear that there must be only one source of progress data.  Also some
code can be shared for tracking the amount of blocks copied and
generating the display.

Therefore have a single ProgressBar object which is used everywhere.

This commit

It just creates a single ProgressBar object which is available via all
OperationDetail objects and Copy_Blocks is updated accordingly.  Note
that the ProgressBar still contains debugging and that the GUI progress
bar of the current operation is still driven via the fraction member in
any OperationDetail object.

Referenced commits:

[1] 52a2a9b00a
    Reduce threading (#685740)

[2] ae434579e1
    Display progress for e2fsck (#467925)
[3] baea186138
    Display progress for mke2fs (#467925)
[4] 57b028bb8e
    Display progress during resize (#467925)

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood 0ca8ed7369 Write a generic progress bar class (#760709)
Write a generic progress bar class.  Has the following features:
* Has separate progress and target numbers, rather than a single
  completion fraction, to enable the the next feature.
* Optionally generates text reporting the amount of data copied using
  the progress and target numbers like this:
      "1.00 MiB of 16.00 MiB copied"
* After running for 5 seconds, also add estimated remaining time.
  (Waits to allow the data copying rate to settle down a little before
  estimating the remaining time).  Looks like this:
      "1.00 MiB of 16.00 MiB copied (00:01:59) remaining)"

The ProgressBar class is not driving the visual progress bar yet.  It
has just been added into the internal block copy algorithm and generates
debug messages showing the progress bar is operating correctly.
Debugging looks like this:

    DEBUG: ProgressBar::start(target=2.0636e+09, text_mode=PROGRESSBAR_TEXT_COPY_BYTES)
    DEBUG: ProgressBar::update(progress=1.30023e+08) m_fraction=0.0630081 m_text="124.00 MiB of 1.92 GiB copied"
    DEBUG: ProgressBar::update(progress=2.67387e+08) m_fraction=0.129573 m_text="255.00 MiB of 1.92 GiB copied"
    DEBUG: ProgressBar::update(progress=4.0475e+08) m_fraction=0.196138 m_text="386.00 MiB of 1.92 GiB copied"
    ...
    DEBUG: ProgressBar::update(progress=1.13351e+09) m_fraction=0.549289 m_text="1.06 GiB of 1.92 GiB copied (00:00:04 remaining)"
    DEBUG: ProgressBar::update(progress=1.26249e+09) m_fraction=0.611789 m_text="1.18 GiB of 1.92 GiB copied (00:00:04 remaining)"
    DEBUG: ProgressBar::update(progress=1.39041e+09) m_fraction=0.67378 m_text="1.29 GiB of 1.92 GiB copied (00:00:03 remaining)"
    ...
    DEBUG: ProgressBar::update(progress=1.97552e+09) m_fraction=0.957317 m_text="1.84 GiB of 1.92 GiB copied (00:00:00 remaining)"
    DEBUG: ProgressBar::update(progress=2.0636e+09) m_fraction=1 m_text="1.92 GiB of 1.92 GiB copied"
    DEBUG: ProgressBar::stop()

Bug 760709 - Add progress bars to XFS and EXT2/3/4 file system specific
             copy methods
2016-02-12 09:09:56 -07:00
Mike Fleetwood ee6dcf5a96 Simplify code in Display_Info() by making use of Glib::build_path()
Simplify code in Dialog_Partition_Info::Dialog_Info() which was open
coding concatenating together a vector of strings with a new line
between each.  Replace with Glib::build_path(), as used elsewhere in
this method and other code.
2016-01-29 13:41:41 -07:00
Mike Fleetwood 40c2cdda0b Prevent incorrect no usage values warning for luks/unknown (#760080)
Create and open a LUKS mapping but don't create any file system within.

    # cryptsetup luksFormat /dev/sdb5
    # cryptsetup luksOpen /dev/sdb5 sdb5_crypt

GParted was incorrectly reporting this warning:

    Unable to read the contents of this file system!
    Because of this some operations may be unavailable.
    The cause might be a missing software package.
    The following list of software packages is required for luks file
    system support:  dmsetup.

This is even though the usage figures for the on disk LUKS encryption
are fully known.  See luks::set_used_sectors().

This was occurring because derived PartitionLUKS::set_usage_known()
was checking usage figures for the outer LUKS and the inner encrypted
file system, unknown in this case.  Correct when displaying figures in
the UI, but not correct in GParted_Core::set_used_sectors() when only
checking the outer LUKS usage figures were set correctly.  Fix this.

Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
Mike Fleetwood 974668104d Remove LUKS unsupported warning (#760080)
Bug 760080 - Implement read-only LUKS support
2016-01-29 13:41:41 -07:00
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 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
Curtis Gedak 0ec726111d Update copyright year 2016-01-18 09:28:01 -07:00
Mike Fleetwood bfb6a3800d Fix displaying partition names also as file system labels in some cases (#759972)
GParted was also displaying the GPT partition names as the file system
labels for some type of file systems.

Create 2 empty partitions on a GPT partitioned disk and name them like
this.  Then create a hfsplus file system with an empty label.  (Actually
single space character but blkid treats it as empty.  Can't use GParted
for this because when no label is specified mkfs.hfsplus sets the label
to "untitled").

    # sgdisk -p /dev/sdb
    /dev/sdb: 4194304 sectors, 2.0 GiB
    Logical sector size: 512 bytes
    ...
    Number  Start (sector)    End (sector)  Size       Code  Name
       1            2048          526335   256.0 MiB   8300  empty partition
       2          526336         1050623   256.0 MiB   8300  hfsplus partition
    # mkfs.hfsplus -v " " /dev/sdb2

Even though only the GPT partition names are set to "SOMETHING
partition", GParted will display them as the file system labels too.
Also in the Information dialog for the empty partition a file system
UUID is displayed as well even though none exists.

Blkid output looks like this:

    # blkid -V
    blkid from util-linux 2.27.1  (libblkid 2.27.0, 02-Nov-2015)
    # blkid | grep /dev/sdb
    /dev/sdb1: PARTLABEL="empty partition" PARTUUID="bf3d2085-65b7-4ae6-97da-5ff968ad7d2c"
    /dev/sdb2: UUID="2c893037-ff76-38f2-9158-2352ef5dc8de" TYPE="hfsplus" PARTLABEL="hfsplus partition" PARTUUID="457e9c2b-e4f2-4235-833b-28208592aaac"

With blkid from util-linux >= 2.22, released September 2012, it is
including additional PARTLABEL and PARTUUID name and value pairs for
GPT partitions [1].  The FS_Info module regular expressions used to
match the file system LABEL and UUID names also happen to match these
new PARTLABEL and PARTUUID names too.  Hence this bug when GParted has
to fall back to using the FS_Info module to read the file system label,
when there is no working file system specific method.  Effects: exfat,
f2fs, hfs, hfsplus, ufs, unknown.

Fix by requiring all the regular expressions used to search the blkid
output to also match the space character in front of the name.

[1] Util-linux 2.22 Release Notes
    https://git.kernel.org/cgit/utils/util-linux/util-linux.git/tree/Documentation/releases/v2.22-ReleaseNotes?h=v2.22

Bug 759972 - GParted displays partition names also as file system labels
             with new blkid for some file systems
2016-01-01 11:18:40 -07:00
Mike Fleetwood c249b7286d Fix temporary path name of new partitions (#759488)
In the UI new partitions were being named "unallocated" instead of
"New Partition #1", etc.  Also deleting a new partition not yet created
deletes all new partitions from the operation list because it matches
by name only and they were all named "unallocated".  Broken by this
recent commit:

    762cd1094a
    Return class member from Dialog_Partition_New::Get_New_Partition() (#757671)

Prior to this commit in the create new partition dialog code did these
relevant steps:
    Dialog_Partition_New::Get_New_Partition()
        Partition part_temp;
        ...
        part_temp.Set(..., "New Partition #%1", ...);

Create local Partition object using default constructor which calls
Partition::Reset() and clears the paths vector.  It then calls Set()
which appends the new name making the vector:
    paths = ["New Partition #%1"]

After the above commit the code did these steps:
    Dialog_Partition_New::Dialog_Partition_New(..., selected_partition, ...)
        set_data(..., selected_partition, ...);
            new_partition = selected_partition;

    Dialog_Partition_New::Get_New_Partition(...)
        new_partition.Set(..., "New Partition #%1", ...);

New_partition is copied from the selected unallocated partition in which
the new partition will be created.  So new_partition is now named
"unallocated".  Then the Set() call appends the new name making the
vector:
    paths = ["unallocated", "New Partition #%1"]

As get_path() returns the first name in the paths vector the path name
changed from "New Partition #%1" to "unallocated" causing this bug.

Fix by resetting the new_partition object to clear all vestiges of it
being a copy of the selected unallocated partition, Reset() call, before
then calling Set().  This then appends the new name to an empty vector
making it contain just the required new name:
    paths = ["New Partition #%1"]

Bug 759488 - Pending create partitions are all getting named
             "unallocated"
2015-12-18 09:35:28 -07:00
Phillip Susi ae434579e1 Display progress for e2fsck (#467925)
Parse output and update progress bar.

Bug 467925 - gparted: add progress bar during operation
2015-12-14 10:42:04 -07:00
Phillip Susi baea186138 Display progress for mke2fs (#467925)
Bug 467925 - gparted: add progress bar during operation
2015-12-14 10:42:04 -07:00
Phillip Susi 57b028bb8e Display progress during resize (#467925)
Capture and parse the progress reports of ntfsresize and resize2fs and
update the dialog progress bar.

Bug 467925 - gparted: add progress bar during operation
2015-12-14 10:42:04 -07:00
Curtis Gedak 5b0465e9a3 Make about dialog website link non-clickable (#758131)
To avoid security implications of invoking a web browser with root
privileges, make the about dialog website link into a non-clickable
label.

The set_website_label() method has been available since gtkmm 2.6.
https://developer.gnome.org/gtkmm/stable/classGtk_1_1AboutDialog.html#aa3d04115d068363be314414899703caa

Bug 758131 - Don't run GUI as root (Was: [wayland] gparted fails to
             start under wayland)
2015-11-20 17:49:19 +00:00
Mike Fleetwood 2c4df87a2c Return reference from Get_New_Partition() (#757671)
Return newly constructed partition object by reference rather than by
copy from the Copy, Resize/Move and New dialog classes.  This is another
case of stopping copying partition objects in preparation for using
polymorphic Partition objects.  In C++ polymorphism has to use pass by
pointer and reference and not pass by value, copying, to avoid object
slicing.

The returned reference to the partition is only valid until the dialog
object containing the new_partition member is destroyed.  This is okay
because in all three cases the returned referenced partition is copied
into a context with new lifetime expectations before the dialog object
is destroyed.

Case 1: GParted_Core::activate_paste()
    Referenced new_partition is copied in the OperationCopy constructor
    before the dialog object goes out of scope.

    Operation * operation = new OperationCopy( ...,
                                               dialog.Get_New_Partition( ... ),
                                               ... );

Case 2: GParted_Core::activate_new()
    Referenced new_partition is copied in the OperationCreate
    constructor before the dialog object goes out of scope.

    Operation * operation = new OperationCreate( ...,
                                                 dialog.Get_New_Partition( ... ) );

Case 3: GParted_Core::activate_resize()
    Temporary partition object is copied from the referenced
    new_partition before the dialog object goes out of scope.

    Partition part_temp = dialog.Get_New_Partition( ... );

Bug 757671 - Rework Dialog_Partition_New::Get_New_Partition() a bit
2015-11-11 10:12:18 -07:00
Mike Fleetwood 762cd1094a Return class member from Dialog_Partition_New::Get_New_Partition() (#757671)
Replace the use of local Partition variable with class member in
preparation for Dialog_Partition_New::Get_New_Partition() being changed
to return the new partition object by reference instead of by value.
    part_temp -> new_partition

Bug 757671 - Rework Dialog_Partition_New::Get_New_Partition() a bit
2015-11-11 10:12:18 -07:00
Mike Fleetwood c86b258475 Create unallocated space within new extended partition after aligning boundaries (#757671)
When creating a new extended partition, the unallocated space within it
was being created before adjusting the partition boundaries for
alignment reasons.  This must be wrong.  Move creation of the
unallocated space to after adjusting the partition boundaries for
alignment reasons.  First introduced by this commit:

    a30f991ca5
    Fix size reduced by 1 MiB when created after cylinder aligned partition

Also added a big FIXME comment explaining how further adjustments are
still made by snap_to_alignment() to the partition boundaries including
a test case where this too late adjustment causes overlapping boundaries
and apply to fail.

Bug 757671 - Rework Dialog_Partition_New::Get_New_Partition() a bit
2015-11-11 10:12:18 -07:00
Mike Fleetwood 451c2eac43 Rename parameter to selected_partition in Dialog_Partition_New methods (#757671)
This is just to make the parameter name in the Dialog_Partition_New
constructor and set_data() method match the name of the equivalent
parameter in the Dialog_Partition_Copy and Dialog_Partition_Resize_Move
classes.  (All three classes inherit from Dialog_Base_Partition and have
similar interfaces).

Bug 757671 - Rework Dialog_Partition_New::Get_New_Partition() a bit
2015-11-11 10:12:18 -07:00
Mike Fleetwood 6e97a63f49 Always use blkid file system detection before libparted (#757781)
Blkid recognises many more file system types and RAID member signatures
than libparted.  GParted already uses blkid detection instead of or
before libparted for whole disk devices [1] and for ext4 detection [2]
(only required with libparted < 1.9.0).  Also GParted could only use
blkid detection on non-512 byte sector devices [3] before libparted was
fixed in version 3.2 [4].  Blkid was documented as a mandatory
requirement from GParted 0.24.0 [5].

Util-linux package, of which blkid command is a part, is a core piece of
Linux software which is very actively maintained and used by lots of
other packages.  Parted package is much less active and has added
detection of fewer file systems and doesn't recognise any RAID members.

In cases of multiple signatures within a partition blkid and libparted
can report different results leading to confusion and issues for
GParted.  This was the primary reason for bug 688882 "Improve clearing
of file system signatures" and a number of other changes to GParted.
Also as the mount command links with libblkid it uses it's detection
when telling the kernel the type of a file system to be mounted.

There aren't any current issues with GParted's file system detection but
given the above argument, switch to using blkid before libparted for
file system detection.  Only falling back to libparted when blkid
doesn't report a result, notably for extended partitions.  Order of
information sources for detection is now:
 1) mdadm (for SWRaid members)
 2) blkid
 3) libparted
 4) GParted internal code

References:

[1] f8faee6377
    Avoid whole disk FAT being detected as MSDOS partition table (#743181)

[2] 533eb1bc03
    Added support for ext4 file systems

[3] 9e5e9f5627
    Enhance file system detection to use FS_Info method - blkid

[4] http://git.savannah.gnu.org/cgit/parted.git/commit/?id=80678bdd957cf49a9ccfc8b88ba3fb8b4c63fc12
    Fix filesystem detection on non 512 byte sectors

[5] 749a249571
    Document blkid command as a mandatory requirement (#753436)

Bug 757781 - Always use blkid file system detection before libparted
2015-11-10 09:44:36 -07:00
Mike Fleetwood 52183058ef Correct inclusion of Operation* headers in GParted_Core.cc
The GParted_Core code only interacts with derived Operation* objects
through the Operation base class interface and in one case the
OperationCopy class.  Therefore include Operation.h and OperationCopy.h
headers and no others.
2015-11-02 10:03:45 -07:00
Mike Fleetwood 32b5106aa1 Add clearing of ZFS labels
ZFS labels were not cleared by GParted when clearing old file system
signatures.

    # wget https://git.kernel.org/cgit/utils/util-linux/util-linux.git/plain/tests/ts/blkid/images-fs/zfs.img.bz2
    # bzip2 -dc zfs.img.bz2 > /dev/sdb1
    [In GParted format to cleared /dev/sdb1]
    # blkid /dev/sdb1
    /dev/sdb1: LABEL="tank" UUID="1782036546311300980" UUID_SUB="13179280127379850514" TYPE="zfs_member"

Update to also zero all 4 ZFS labels.

NOTE:
GParted now writes a little over 1 MiB when clearing old file system
signatures.  As this is performed in the main thread the UI is not able
to respond during this action.  Testing this on a range of USB flash
keys and hard drives found the slowest normal time to write this was
0.25 seconds, with an occasional outlier up to 2.8 seconds from a USB
flash key.  This is considered acceptable.
2015-11-02 10:03:45 -07:00
Mike Fleetwood eec78cd2b2 Stop over rounding up of signature zeroing lengths (#756829)
Following the previous commit "Add erasing of SWRaid metadata 0.90 and
1.0 super blocks (#756829)" signature zeroing specified to write 4 KiB
of zeros at position end - 64 KiB, aligned to 64 KiB.  Example operation
details from formatting a 1 GiB partition to cleared:

    Format /dev/sdb8 as cleared
    + calibrate /dev/sdb8
    + clear old file system signatures in /dev/sdb8
      + write 68.00 KiB of zeros as byte offset 0
      + wite 4.00 KiB of zeros at byte offset 67108864
      + wite 64.00 KiB of zeros at byte offset 1073676288
      + write 8.00 KiB of zeros at byte offset 1073733632
      + flush operating system cache of /dev/sdb

However it actually wrote 64 KiB.  This is because the rounding /
alignment was also applied to the zeroing length.  Before this commit
rounding / alignment was always less than or equal to the length so this
wasn't seen before.  Instead just apply device sector size rounding up
to the length.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood 743968ef68 Add clearing of SWRaid metadata 0.90 and 1.0 super blocks (#756829)
The super blocks for Linux Software RAID arrays using metadata types
0.90 and 1.0 are stored at the end of the partition and not currently
cleared by GParted.

Create a SWRaid array, stop it and format it to cleared using GParted.
The signature remains.

    # mdadm --create /dev/md1 --level=linear --raid-devices=1 --force --metadata=1.0 /dev/sdb1
    # mdadm --stop /dev/md1
    [In GParted format to cleared /dev/sdb1]
    # blkid /dev/sdb1
    /dev/sdb1: UUID="8ac947a7-063f-2266-5f2a-e5d178198139" UUID_SUB="49bd51d4-4c54-fb16-a45e-bd795f783f59" LABEL="rockover:1" TYPE="linux_raid_member"

As fixed in other cases before [1][2] it is necessary to clear all
signatures before formatting as a new file system to prevent recognition
issues.  For example now format the partition as a FAT32 file system.
Now there are two signatures and libparted reports one type and blkid
reports another.

    # mkdosfs -F 32 /dev/sdb1
    # blkid /dev/sdb1
    /dev/sdb1: UUID="8ac947a7-063f-2266-5f2a-e5d178198139" UUID_SUB="49bd51d4-4c54-fb16-a45e-bd795f783f59" LABEL="rockover:1" TYPE="linux_raid_member"
    # parted /dev/sdb print
    Model: ATA SAMSUNG SSD UM41 (scsi)
    Disk /dev/sdb: 8012MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start   End     Size    Type      File system  Flags
     1      1049kB  538MB   537MB   primary   fat32

(Deliberately avoided btrfs, ext2/3/4 and xfs as recent versions of
their mkfs tools clear other signatures first for the same reason).

[1] Bug 688882 - Improve clearing of file system signatures
[2] 3c75f3f5b1
    Use wipefs to clear old signatures before creating new file systems (#688882)

Update erase_filesystem_signatures() to also zero the necessary sectors
to clear SWRaid metadata 0.90 and 1.0 super blocks.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood a86f28bc32 Handle unusual case of missing mdadm but SWRaid arrays active (#756829)
If the installation is unusual / broken such that the mdadm command is
not found but there are active SWRaid arrays, provide a fallback with
some of the information.  In this case populate the cache with details
only available from /proc/mdstat.  Information will be limited to active
arrays only and their members.  No UUIDs or labels.  There will be no
information about inactive arrays and GParted will use it's normal
libparted and blkid identification for those devices and partitions.

As mdadm has gained the capability to manage Fake/BIOS RAID arrays they
also appear in /proc/mdstat when mdadm is used to start them.  Enhance
the parser of /proc/mdstat to only extract information for SWRaid arrays
with recognised metadata versions.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood bab1109d3d Ensure SWRaid_Info cache is loaded at least once (#756829)
Automatically load the cache of SWRaid information for the first time if
any of the querying methods are called before the first explicit
load_cache() call.  Means we can't accidentally use the class and
incorrectly find no SWRaid members when they do exist.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood f6c2f00df7 Populate member mount point with SWRaid array device (#756829)
Busy file systems are accessed via a mount point, LVM Physical Volumes
are activated via the Volume Group name and busy SWRaid members are
accessed via the array device, /dev entry.  Therefore choose to show the
array device in the mount point field for busy SWRaid members.

The kernel device name for an SWRaid array (without leading "/dev/") is
the same as used in /proc/mdstat and /proc/partitions.  Therefore the
array device (with leading "/dev/") displayed in GParted will match
between the mount point for busy SWRaid members and the array itself as
used in the device combo box.

    # cat /proc/mdstat
    Personalities : [raid1]
    md1 : active raid1 sda1[2] sdb1[3]
          524224 blocks super 1.0 [2/2] [UU]
    ...
    # cat /proc/partitions
    major minor  #blocks  name

       8        0   33554432 sda
       8        1     524288 sda1
    ...
       8       16   33554432 sdb
       8       17     524288 sdb1
    ...
       9        1     524224 md1
    ...

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood 7255c8af40 Use UUID and label of SWRaid arrays too (#756829)
In cases where blkid wrongly reports a file system instead of an SWRaid
member (sometimes confused by metadata 0.90/1.0 mirror array or old
version not recognising SWRaid members), the UUID and label are
obviously wrong too.  Therefore have to use the UUID and label returned
by the mdadm query command and never anything reported by blkid or any
file system specific command.

Example of blkid reporting the wrong type, UUID and label for /dev/sda1
and the correct values for /dev/sdb1:

    # blkid | egrep 'sd[ab]1'
    /dev/sda1: UUID="10ab5f7d-7d8a-4171-8b6a-5e973b402501" TYPE="ext4" LABEL="chimney-boot"
    /dev/sdb1: UUID="15224a42-c25b-bcd9-15db-60004e5fe53a" UUID_SUB="0a095e45-9360-1b17-0ad1-1fe369e22b98" LABEL="chimney:1" TYPE="linux_raid_member"

    # mdadm -E -s -v
    ARRAY /dev/md/1  level=raid1 metadata=1.0 num-devices=2 UUID=15224a42:c25bbcd9:15db6000:4e5fe53a name=chimney:1
       devices=/dev/sda1,/dev/sdb1
    ...
    ARRAY /dev/md127 level=raid1 num-devices=2 UUID=8dc7483c:d74ee0a8:b6a8dc3c:a57e43f8
       devices=/dev/sdb6,/dev/sda6
    ...

NOTES:
* In mdadm terminology the label is called the array name, hence name=
  parameter for array md/1 in the above output.
* Metadata 0.90 arrays don't support naming, hence the missing name=
  parameter for array md127 in the above output.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood 0ce9857380 Move busy detection of SWRaid members into the new module (#756829)
Add active attribute to the cache of SWRaid members.  Move parsing of
/proc/mdstat to discover busy SWRaid members into the cache loading
code.  New parsing code is a little different because it is finding all
members of active arrays rather than determining if a specific member is
active.

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood 5f02bcf463 Detect Linux SWRaid members by querying mdadm (#756829)
Detection of Linux SWRaid members currently fails in a number of cases:

1)  Arrays which use metadata type 0.90 or 1.0 store the super block at
    the end of the partition.  So file system signatures in at least
    linear and mirrored arrays occur at the same offsets in the
    underlying partitions.  As libparted only recognises file systems
    this is what is detected, rather than an SWRaid member.

    # mdadm -E -s -v
    ARRAY /dev/md/1  level=raid1 metadata=1.0 num-devices=2 UUID=15224a42:c25bbcd9:15db6000:4e5fe53a name=chimney:1
       devices=/dev/sda1,/dev/sdb1
    ...
    # wipefs /dev/sda1
    offset               type
    ----------------------------------------------------------------
    0x438                ext4   [filesystem]
                         LABEL: chimney-boot
                         UUID:  10ab5f7d-7d8a-4171-8b6a-5e973b402501

    0x1fffe000           linux_raid_member   [raid]
                         LABEL: chimney:1
                         UUID:  15224a42-c25b-bcd9-15db-60004e5fe53a

    # parted /dev/sda print
    Model: ATA VBOX HARDDISK (scsi)
    Disk /dev/sda: 34.4GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start   End     Size    Type      File system  Flags
     1      1049kB  538MB   537MB   primary   ext4         boot, raid
    ...

2)  Again with metadata type 0.90 or 1.0 arrays blkid may report the
    contained file system instead of an SWRaid member.  Have a single
    example of this configuration with a mirrored array containing the
    /boot file system.  Blkid reports one member as ext4 and the other as
    SWRaid!

    # blkid | egrep 'sd[ab]1'
    /dev/sda1: UUID="10ab5f7d-7d8a-4171-8b6a-5e973b402501" TYPE="ext4" LABEL="chimney-boot"
    /dev/sdb1: UUID="15224a42-c25b-bcd9-15db-60004e5fe53a" UUID_SUB="0a095e45-9360-1b17-0ad1-1fe369e22b98" LABEL="chimney:1" TYPE="linux_raid_member"

    Bypassing the blkid cache gets the correct result.

    # blkid -c /dev/null /dev/sda1
    /dev/sda1: UUID="15224a42-c25b-bcd9-15db-60004e5fe53a" UUID_SUB="d0460f90-d11a-e80a-ee1c-3d104dae7e5d" LABEL="chimney:1" TYPE="linux_raid_member"

    However this can't be used because if a user has a floppy configured
    in the BIOS but no floppy attached, GParted will wait for minutes as
    the kernel tries to access non-existent hardware on behalf of the
    blkid query.  See commit:
        18f863151c
        Fix long scan problem when BIOS floppy setting incorrect

3)  Old versions of blkid don't recognise SWRaid members at all so always
    report the file system when found.  Occurs with blkid v1.0 on
    RedHat / CentOS 5.

The only way I can see how to fix all these cases is to use the mdadm
command to query the configured arrays.  Then use this information for
first choice when detecting partition content, making the order: SWRaid
members, libparted, blkid and internal.

GParted shell wrapper already creates temporary blank udev rules to
prevent Linux Software RAID arrays being automatically started when
GParted refreshes its device information[1].  However an administrator
could manually stop or start arrays or change their configuration
between refreshes so GParted must load this information every refresh.
On my desktop with 4 internal hard drives and 3 testing Linux Software
RAID arrays, running mdadm adds between 0.20 and 0.30 seconds to the
device refresh time.

[1] a255abf343
    Prevent GParted starting stopped Linux Software RAID arrays (#709640)

Bug 756829 - SWRaid member detection enhancements
2015-11-02 10:03:45 -07:00
Mike Fleetwood 9d6ebb3dab Check usability of named device following a non-existent / invalid one (#756434)
When multiple devices are named on the command line and (after sorting
and removing duplicates) the device following a non-existent or invalid
one is not checked for usability [1].  In most situations this isn't
noticed as the device gets skipped at the "Searching ...  partitions"
step instead.  However as seen in bug 755495 and commit [2]
checking usability matters.

For example (on CentOS 6.5) a large sector disk device can be edited
when it follows a non-existent or invalid device named on the command
line:

    # modprobe scsi_debug dev_size_mb=128 sector_size=4096
    # fgrep scsi_debug /sys/block/*/device/model
    /sys/block/sdd/device/model:scsi_debug

    # ./gpartedbin /dev/does-not-exist /dev/sdd
    ======================
    libparted : 2.1
    ======================
    Could not stat device /dev/does-not-exist - No such file or directory.
    Device /dev/sdd has a logical sector size of 4096.  Not all parts of GNU Parted support this at the moment, and the working code is HIGHLY EXPERIMENTAL.

    /dev/sdd: unrecognised disk label

When erasing a device don't skip confirming the following device is
usable.

[1] Usable device as implemented by useable_device()
    Must not have a large sector size when GParted is built with an old
    version of libparted which doesn't support large sector sizes and
    must be able to read the first sector.

[2] 362b2db331
    Check disks named on the command line are safe to use too (#755495)

Bug 756434 - GParted dumps core when passing non-existent or invalid
             device on the command line
2015-10-15 09:39:49 -06:00
Mike Fleetwood bbf1a19cec Remove non-existent or invalid devices from those named (#756434)
A non-existent or invalid disk device named on the command line caused
two libparted dialogs to be displayed repeatedly on every refresh.  This
was because the device was only removed from the 'device_paths' vector
when it wasn't usable [1]; not when it didn't exist or was invalid, when
the libparted ped_device_get() call failed.  Fix this.

[1] Usable device as implemented by useable_device()
    Must not have a large sector size when GParted is built with an old
    version of libparted which doesn't support large sector sizes and
    must be able to read the first sector.

Bug 756434 - GParted dumps core when passing non-existent or invalid
             device on the command line
2015-10-15 09:39:19 -06:00
Mike Fleetwood 663807802f Prevent core dump with non-existent or invalid command line named device (#756434)
Naming a non-existent or invalid disk device on the command line causes
GParted to dump core.  Non-existent device looks like this:

    # ./gpartedbin /dev/does-not-exist
    ======================
    libparted : 2.4
    ======================
    Could not stat device /dev/does-not-exist - No such file or directory.
    Could not stat device /dev/does-not-exist - No such file or directory.
    Backtrace has 10 calls on stack:
      10: /lib64/libparted.so.0(ped_assert+0x31) [0x7fcfd10b3e61]
      9: /lib64/libparted.so.0(+0x3fdfc12a0c) [0x7fcfd10b4a0c]
      8: /home/mike/bin/gpartedbin-0.23.0-master-63-g23b5ba4() [0x455028]
      7: /home/mike/bin/gpartedbin-0.23.0-master-63-g23b5ba4() [0x455090]
      6: /home/mike/bin/gpartedbin-0.23.0-master-63-g23b5ba4() [0x4550d5]
      5: /home/mike/bin/gpartedbin-0.23.0-master-63-g23b5ba4() [0x46723f]
      4: /usr/lib64/libglibmm-2.4.so.1() [0x3ff5834a8d]
      3: /lib64/libglib-2.0.so.0() [0x3fe086a374]
      2: /lib64/libpthread.so.0() [0x3fdf407a51]
      1: /lib64/libc.so.6(clone+0x6d) [0x3fdf0e893d]
    Assertion (dev != NULL) at device.c:227 in function ped_device_open() failed.
    Aborted (core dumped)

And with an invalid device the output looks like this:

    # ./gpartedbin /dev/zero
    ======================
    libparted : 2.4
    ======================
    The device /dev/zero is so small that it cannot possibly store a file system or partition table.  Perhaps you selected the wrong device?
    Error fsyncing/closing /dev/zero: Invalid argument
    The device /dev/zero is so small that it cannot possibly store a file system or partition table.  Perhaps you selected the wrong device?
    Error fsyncing/closing /dev/zero: Invalid argument
    Backtrace has 10 calls on stack:
    ...
    [Same as above]

Bisected the cause to this commit from 2015-03-09 in GParted 0.22.0.  It
claimed to make no functional change.  That turned out not to be true.
    51ac4d5648
    Split get_device_and_disk() into two (#743181)

Fix by simply adding the missed if condition in get_device().

Bug 756434 - GParted dumps core when passing non-existent or invalid
             device on the command line
2015-10-15 09:38:49 -06:00
Mike Fleetwood 23b5ba4e4e Ensure DMRaid /dev entries are created before using named devices
For probed DMRaid devices (when not using libparted DMRaid support)
GParted waits up to 1 second for udev to have processed all events and
created the /dev entries after starting each DMRaid array.  This was
added by this commit from 2009-09-02:
    e7352a5000
    Ensure /dev file system device entries created before adding device

Do the same for devices named on the command line too.
2015-10-08 13:00:01 -06:00
Mike Fleetwood 3d3d98994b Sort command line named disk devices and remove duplicates (#755495)
Order named disk devices so that they appear in the combo box in the
same order which they would when probed.  Also remove duplicates so that
the same disk devices aren't scanned multiple times and appear
duplicated in the UI.

Try this; it used to take ages to load and looked weird:

    # gparted /dev/sda /dev/sdb /dev/sda /dev/sdb /dev/sda /dev/sdb

Bug 755495 - GParted allowing partitioning of large sector devices
             specified on the command line, when built with old
             libparted which doesn't support it
2015-10-08 13:00:01 -06:00
Mike Fleetwood 362b2db331 Check disks named on the command line are safe to use too (#755495)
When probing for disk devices GParted ensures that libparted is capable
of handling the sector size safely and that it is a real disk before it
is shown in the UI.  However when disk devices are named on the command
line none of these checks are performed.

Libparted versions before v2.2 can only safely handle a sector size of
512 bytes.  Therefore on old distributions with libparted < v2.2 GParted
allows unsafe editing of disk devices with larger sector sizes when they
are named on the command line.  Known to affect these distributions:
    RedHat/CentOS 5   (parted 1.8.1)
    RedHat/CentOS 6   (parted 2.1)

For example (on CentOS 6.5) large sector disk device is ignored when
probing:

    # modprobe scsi_debug dev_size_mb=128 sector_size=4096
    # fgrep scsi_debug /sys/block/*/device/model
    /sys/block/sdd/device/model:scsi_debug

    # gparted
    ======================
    libparted : 2.1
    ======================
    Device /dev/sdd has a logical sector size of 4096.  Not all parts of
    GNU Parted support this at the moment, and the working code is
    HIGHLY EXPERIMENTAL.

    Ignoring device /dev/sdd with logical sector size of 4096 bytes.
    GParted requires libparted version 2.2 or higher to support devices
    with sector sizes larger than 512 bytes.

However when the device is named it is not ignored and can be edited:

    # gparted /dev/sdd
    ======================
    libparted : 2.1
    ======================
    Device /dev/sdd has a logical sector size of 4096.  Not all parts of
    GNU Parted support this at the moment, and the working code is
    HIGHLY EXPERIMENTAL.

    /dev/sdd: unrecognised disk label

Apply the same validity checks to disk devices named on the command line
as to probed ones.

Bug 755495 - GParted allowing partitioning of large sector devices
             specified on the command line, when built with old
             libparted which doesn't support it
2015-10-08 13:00:01 -06:00
Mike Fleetwood 037020b116 Create new method GParted_Core::useable_device() (#755495)
Abstract code checking sector size and ensuring the first sector of a
candidate disk device can be read into new
GParted_Core::useable_device() method.

Bug 755495 - GParted allowing partitioning of large sector devices
             specified on the command line, when built with old
             libparted which doesn't support it
2015-10-08 13:00:01 -06:00
Mike Fleetwood 81b673ff5d Prevent 'index' may be used uninitialised warning in OperationDelete (#755214)
I missed another case of 'index' may be used uninitialised warning in
OperationDelete::apply_to_visual().  Indent a code block within an if
clause so that the compiler can confirm that the 'index' local variable
isn't used uninitialised.  Prevent this compiler warning:
    OperationDelete.cc: In member function 'virtual void GParted::OperationDelete::apply_to_visual(std::vector<GParted::Partition, std::allocator<GParted::Partition> >&)':
    OperationDelete.cc:34: warning: 'index' may be used uninitialized in this function

Bug 755214 - Refactor operation merging
2015-10-08 11:34:23 -06:00
Albert Young 584137b32b Remove prohibited characters from FAT16/32 labels (#755608)
GParted waits forever when attempting to set a FAT16/32 file system
label which contains prohibited characters [1][2].  This is because
mlabel asks a question and is waiting for input.  Force cancelling the
operation doesn't work either as GParted sends signal 2 (interrupt i.e.
[Ctrl-C]) but mtools commands specifically ignores this and a number of
other signals.  Have to kill mlabel with signal 9 (kill) to regain
control of GParted.

Mlabel command with prohibited characters in the label:

    # export MTOOLS_SKIP_CHECK=1
    # mlabel ::"MYLABEL/   " -i /dev/sdb10
    Long file name "MYLABEL/   " contains illegal character(s).
    a)utorename A)utorename-all r)ename R)ename-all
    s)kip S)kip-all q)uit (aArRsSq):

Remove prohibited characters from FAT16/32 file systems labels when
creating and labelling them.  Also upper case the label to meet label
requirements [1][2].  This silently corrects the label and the actual
label applied will be displayed when GParted refreshes after applying
the operation.

[1] Microsoft TechNet: Label
    https://technet.microsoft.com/en-us/library/bb490925.aspx

[2] Replicated in Wikikedia: label (command)
    https://en.wikipedia.org/wiki/Label_%28command%29

Bug 755608 - Labeling fat16/fat32 partitions hangs if certain characters
             included in label
2015-10-04 09:57:07 -06:00
Mike Fleetwood cbfb7e51f5 Replace Operation class members index and index_extended with local variables (#755214)
These member variables store no Operation class information and were
being used as local variables.  Replace with local variables.

Also indent a code block within an if clause so that the compiler can
confirm that the new local variable isn't used uninitialised.  Prevents
this compiler warning:
    OperationResizeMove.cc: In member function 'void GParted::OperationResizeMove::apply_normal_to_visual(std::vector<GParted::Partition, std::allocator<GParted::Partition> >&)':
    OperationResizeMove.cc:125: warning: 'index' may be used uninitialized in this function

Bug 755214 - Refactor operation merging
2015-09-28 11:02:07 -06:00
Mike Fleetwood a1ab21285b Remove unused index parameter from Add_Operation() (#755214)
Since this commit earlier in the patchset the second optional parameter
of method Win_GParted::Add_Operation() is no longer used.  Remove it.
    Replace open coded merge of resize/move into create operation
    (#755214)

Bug 755214 - Refactor operation merging
2015-09-28 11:02:07 -06:00
Mike Fleetwood 0e8f24b92b Fix visually re-applying copy new operation in create-copy-grow-first sequence (#755214)
This is the equivalent case fixed in the earlier commit, but now using
copy/paste to create the second new partition rather than plain new.
    Fix visually re-apply create operation in create-create-grow-first
    sequence (#755214)

Start with an existing partition as a copy source.  Then this sequence
of operations will cause the copy partition to disappear from the disk
graphic:
1) create new #1,
2) copy existing / paste into unallocated leaving space preceding,
3) resize new #1 larger.

There are two different types of copy operation.  The first is copy into
unallocated space creating a new partition which needs treating the same
as create new operation.  The second is copy into existing partition
which needs treating the same as the other operations which don't change
the boundaries of the partition.  Fix apply_to_visual() accordingly.

Bug 755214 - Refactor operation merging
2015-09-28 11:01:49 -06:00
Mike Fleetwood dc6ffc6a87 Move code visually re-applying create operation into parent class (#755214)
Move the code from OperationCreate::apply_to_visual() into new method
Operation::insert_new() in the parent class.  This is in preparation for
the following commit.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 27cbe36d0f Share duplicate code substituting partitions in multiple operations (#755214)
The apply_to_visual() method for the change UUID, format, label file
system and name partition operations duplicated identical code.  This
code was just substituting the partition in the disk graphic vector with
the new partition recorded in the operation, as none of these operations
change the partition boundaries.  Move this duplicate code into the
parent class in new method Operation::substitute_new().

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 9b497aae14 Fix visually re-applying create operation in create-create-grow-first sequence (#755214)
After previous commit "Replace open coded merge of resize/move into
create operation (#755214)" the second created partition would disappear
from the disk graphic in the following sequence: create new #1, create
new #2 leaving space preceding, resize #1 larger.  The create new #2
operation still existed and was shown in the operation list.  It was
just that it disappeared from the disk graphic.

Remember that when each operation is created it records the partition,
or the unallocated space, to which the operation is applied at the time
the operation is created in the partition_original member variable.  In
the above sequence the resize #1 larger operation was merged back into
the create new #1 operation.  When visually re-applying the create
new #1 operation to the disk graphic, it left a smaller unallocated
partition following it.  This was smaller than the unallocated partition
recorded in the create new #2 operation, hence it failed to visually
re-apply to the disk graphic.

The insight to fix this is that it doesn't matter what size the
unallocated space was when the create new operation was constructed.  It
only matters that the new partition to be created fits in the available
unallocated space currently in the disk graphic.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 27e5bbeece Add comment summarising current operation merging rules (#755214)
This information is already documented in the existing comments
associated with the calls to merge_operations() and assignments to the
mergetype variables.  The table just summaries the rules together in one
place.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 5793c5ac03 Replace open coded merge of resize/move into create operation (#755214)
For the case of resizing/moving a new, not yet created partition,
activate_resize() open coded the merge operation.  Again this code has
existed in GParted since before version 0.0.5 and the current code
history in Git.

Replace the necessary code so that an explicit merge_operations() call
is used instead; along with the other case of resizing/moving an
existing partition.

NOTES:

This commit changes the merge direction.  The old coded merged forward
by removing the old create operation and adding a new create operation
with the new size.  This was bad because with multiple pending create
operations, each merged resize operation reordered those create
operations.  Then when the operations were applied the partitions were
created and therefore numbered in a different order to that shown in
disk graphic.

The new code merges backwards by updating the initial create operation
with the new size.  This maintains the create operation order so that
when applied the partitions are numbered in the same order as shown in
the disk graphic.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 3d21f0f192 Replace open coded merge of format into create operation (#755214)
For the case of formatting a new, not yet created partition,
activate_format() open coded the merge operation.  This code has existed
in GParted since before version 0.0.5 and the current code history in
Git.

Replace the necessary code so that an explicit merge_operations() call
is used instead; along with the other case of formatting an existing
partition.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood cf5d8d928c Refactor merging rules into new merge_operations() (#755214)
Creation of the various operations involved various implicit rules about
how the different types of operations were merged in different cases.
This was open coded in each ::activate_*() method.  Abstract this into
new merge_operations() method and make the merging rules explicitly
specified.

NOTE:
The removal of operation type checking in the MERGE_LAST_WITH_ANY cases
is not a problem because all the Operation*::merge_operations() methods
ensure the operation types match as part of the merge attempt.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood d93d8abcc4 Rename function to merge_two_operations() and update validation (#755214)
Rename Win_GParted::Merge_Operations() to merge_two_operations().  To
reflect what it does and in preparation for further refactoring of the
code.

Be more strict on the validation of the first and second indexes.  The
first operation must also be before the second operation in the
operation[] vector.  (It is actually a programming bug if first and
second fail validation.  However so far g_assert() is only being used to
validate pointers, which if wrong would likely cause the program to
eventually crash when dereferenced later.  In this case a bug would
merely cause the incorrectly specified pair of operations to not be
merged).

Move validate_display_partition_ptr() declaration in the header file to
be in the same ordering as it's definition in the source file.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 7f4ffd28d5 Encapsulate operation merging inside the Operation* classes (#755214)
Win_GParted::Merge_Operations() method was modifying the internals of
Operation* objects; in particular the partition_new member variable.
This is breaking data hiding and encapsulation tenant of object oriented
programming.

Implement exactly the same operation merge semantics, but hide the
manipulation of the internals of the Operation* objects within the
Operation* classes themselves.

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 29a1f20688 Better comment what each Merge_Operations() call is achieving (#755214)
... before refactoring the code.

See the commit message from 2011-10-05 for details of what operations,
available at that time, on the same partition can be merged and in what
cases:
    b10349ae37
    Merge overlapping operations (#438573)

Bug 755214 - Refactor operation merging
2015-09-28 10:41:39 -06:00
Mike Fleetwood 26dc3dffa7 Localise checking for an extended partition into activate_new()
As previous commit, display_partitions is now a Win_GParted member
variable so checking for the existence of an extended partition can be
localised where it is used.

Remove index_extended member variable and localise the same checking in
activate_new().
2015-09-22 09:50:11 -06:00
Mike Fleetwood 3b5471516c Localise counting of primary partitions into max_amount_prim_reached()
Now that display_partitions is a Win_GParted member variable and
therefore available throughout the class, since commit [1], calculation
of primary_count can be localised in max_amount_prim_reached() where it
is used.

Implements a FIXME and removes primary_count as a member variable.

[1] 545b75d957
    Move vector of partition objects to a Win_GParted class member (#750168)
2015-09-22 09:50:11 -06:00
Mike Fleetwood 99e908f79b Refresh copy source when its a logical partition too (#754827)
Perform a copy, reformat source and paste sequence in GParted.  When the
source is a primary partition everything works as expected, with the
newly pasted partition reflecting the reformatted source.  However when
the source is a logical partition GParted thinks it is pasting the
original source, rather than the reformatted source.  The same is also
true for other file system manipulation operations: resize, file system
label and new UUID.  It is just that reformatting the source to a
different file system type is the most obvious in the UI and causes the
most significantly wrong actions to be performed.

For example start with an ext4 logical partition, select it for copy,
format it to xfs and paste into a new partition.  GParted thinks the
second operation will create a copy of an ext4 file system instead of
the xfs file system.  When applied the operation details are:

    Format /dev/sdd5 as xfs
    + calibrate /dev/sdd5
    + clear old file system signatures in /dev/sdd5
    + set partition type on /dev/sdd5
    + create new xfs file system
      + mkfs.xfs -f -L "" /dev/sdd5
    Copy /dev/sdd5 to /dev/sdd (start at 131.00 MiB)
    + calibrate /dev/sdd5
    + check file system on /dev/sdd5 for errors and (if possible) fix them
      + e2fsck -f -y -v -C 0 /dev/sdd5
          e2fsck: Subperblock invalid, trying backup blocks...
          Resize inode not valid.  Recreate? yes
          ...
          /dev/sdd5: ***** FILE SYSTEM WAS MODIFIED *****
    + create empty partition
    + set partition type on /dev/sdd6
        new partition type: ext4
    + copy file system of /dev/sdd5 to /dev/sdd6
        using internal algorithm
        ...

GParted formatted sdd5 to xfs, but then the copy step ran e2fsck and
managed to resurrect the ext4 file system and then performed a block
copy of it to partition sdd6.  The copy step should have ran xfs_repair
and used xfsdump | xfsrestore to copy the xfs file system.  Afterwards
sdd5 contains both xfs and ext4 signatures.

    # wipefs /dev/sdd5
    offset               type
    ----------------------------------------------------------------
    0x438                ext4   [filesystem]
                         UUID:  f0ed4247-76db-4d93-b3bc-c7da4a70f95e

    0x0                  xfs   [filesystem]
                         UUID:  1ac8e7c3-0311-4c64-8e4a-b715a23ea0bd

This has been broken at least as far back as GParted 0.1.0.

Fix by simply refreshing the copy source partition object when it is a
logical partition too, as well as when it is a primary partition.

Bug 754827 - Copy, reformat source and paste a logical partition thinks
             it's pasting the original file system
2015-09-22 09:50:11 -06:00
Mike Fleetwood 2b57229fc2 Implement shell style exit status decoding (#754684)
Command exit status is a 1 byte value between 0 and 255. [1][2]  However
at the Unix API level the value is encoded as documented in the
waitpid(2) manual page.  This is true for the Glib API too. [3]  This is
why, for example, the comment in ext2::check_repair() reported receiving
undocumented exit status 256.  It was actually receiving exit status 1
encoded as per the waitpid(2) method.

Add shell style exit status decoding [2] to execution of all external
commands.   Return value from Utils::execute_command() and
FileSystem::execute_command() functions are now:
    0 - 125 - Exit status from the command
    126     - Error executing the command
    127     - Command not found
    128+N   - Command terminated by signal N
    255     - Unexpected waitpid(2) condition
Also adjust checking of the returned statuses as necessary.

[1] Advanced Bash-Scripting Guide: Appendix D. Exit Codes With Special
    Meanings
    http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/exitcodes.html

[2] Quote from the bash(1) manual page:

        EXIT STATUS
            ... Exit statuses fall between 0 and 255, though as
            explained below, the shell may use values above 125
            specially.  ...

            ... When a command terminates on a fatal signal N, bash uses
            the value of 128+N as the exit status.

            If a command is not found, the child process created to
            execute it returns a status of 127.  If a command is found
            but is not executable, the return status is 126.

[3] Quote from the Glib Reference Manual, Spawning Processes section,
    for function g_spawn_check_exit_status():
    https://developer.gnome.org/glib/stable/glib-Spawning-Processes.html#g-spawn-check-exit-status

        The g_spawn_sync() and g_child_watch_add() family of APIs return
        an exit status for subprocesses encoded in a platform-specific
        way.  On Unix, this is guaranteed to be in the same format
        waitpid() returns, ...

Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
             functions
2015-09-21 10:11:19 -06:00
Mike Fleetwood a202b4569a Time and check commands setting fat16/32 labels and UUIDs (#754684)
Replace open coding of the creation of the operation details for the
mlabel command used to set the label and UUID with calls to
FileSystem::execute_command() which will do it all.  This also results
in the commands getting a time and check mark displayed in the operation
details.

Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
             functions
2015-09-21 10:11:19 -06:00
Mike Fleetwood 3eccd01f42 Time and check nearly all file system action commands (#754684)
There has been an undocumented rule that external commands displayed in
the operation details, as part of file system manipulations, only get a
time and check mark displayed when multiple commands are needed, and not
otherwise.  (GParted checks whether all commands are successful or not
regardless of whether a check mark is displayed in the operation details
or not).

EXCEPTION 1: btrfs resize

Since the following commit [1] from 2013-02-22, GParted stopped
displaying the timing for the btrfs resize command in the operation
details.  It being part of a multi-command sequence to perform the step.
This is because FileSystem::execute_command() since the commit can only
check the exit status for zero / non-zero while timing and checking the
command status but btrfs resize needs to consider some non-zero statuses
as successful.

[1] 52a2a9b00a
    Reduce threading (#685740)

EXCEPTION 2: ext2/3/4 move and copy using e2image

When use of e2image was added [2] the single command steps were timed
and check.

[2] 86111fe12a
    Use e2image to move/copy ext[234] file systems (#721516)

EXCEPTION 3: fat16/32 write label and UUID

Uses Utils::execute_command() rather than FileSystem::execute_command()
so can be separately changed.  See the following commit for resolution
of the final commands not yet timed and check mark displayed.

CHANGE:

Lets make a simpler rule of always displaying the time and a check mark
for all external commands displayed in the operation details.  However
this makes several of the other single command actions need special exit
status handling because zero success, non-zero failure is not correct
for every case.  Specifically affects resizing of reiserfs and check
repair of ext2/3/4, fat16/32, jfs and reiserfs.

After this change all external commands run as file system actions must
follow one of these two patterns of using the EXEC_CHECK_STATUS flag or
separately calling FileSystem::set_status() to register success or
failure of the command:
    exit_status = execute_command(cmd, od, EXEC_CHECK_STATUS...);
or:
    exit_status = execute_command(cmd, od, ...);
    bool success = (exit_status == 0 || exit_status == OTHER_SUCCESS_VALUE...);
    set_status(od, success );

Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
             functions
2015-09-21 10:11:19 -06:00
Mike Fleetwood 83ecae4918 Refactor flags in method FileSystem::execute_command() (#754684)
Change the two optional boolean parameters into a single optional flags
parameter which uses symbolically defined names.  Makes reading the
execute_command() calls much easier to understand.  (Implemented as bit
field using the same technique as used for Glib::SpawnFlags [1]).

This changes the calls thus:

  execute_command(cmd, od)              -> (cmd, od)
  execute_command(cmd, od, false)       -> (cmd, od, EXEC_NONE)  // [2]
  execute_command(cmd, od, true )       -> (cmd, od, EXEC_CHECK_STATUS)
  execute_command(cmd, od, false, true) -> (cmd, od, EXEC_CANCEL_SAFE)
  execute_command(cmd, od, true , true) ->
                          (cmd, od, EXEC_CHECK_STATUS|EXEC_CANCEL_SAFE)

[1] SpawnFlags bitwise operators in
    /usr/include/glibmm-2.4/glibmm/spawn.h.

[2] False and EXEC_NONE are the default values for the optional third
    parameter before and after this change respectively and both mean
    the same.  This is being used in btrfs::resize() and being kept for
    now despite it being the default.

Bug 754684 - Updates to FileSystem:: and Utils::execute_command()
             functions
2015-09-21 10:11:19 -06:00
Mike Fleetwood 66bb88abf7 Recognise NVME devices (#755022)
Add a pattern to recognise Non-Volatile Memory Express devices as valid
devices to work with.  Devices are named by the Linux kernel device
driver like /dev/nvme0n1 [1] with partitions named like /dev/nvme0n1p1
[2].

[1] linux 3.3 drivers/block/nvme.c nvme_alloc_ns()
    https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/drivers/block/nvme.c?id=v3.3#n1351

[2] Contents of /proc/partitions for a partitioned NVME device
    $ grep nvme /proc/partitions
     259        0  390711384 nvme0n1
     259        1        977 nvme0n1p1
     259        2   31250000 nvme0n1p2
     259        3  328209496 nvme0n1p3
     259        4   31249408 nvme0n1p4

Bug 755022 - gparted doesn't recognize nvme devices
2015-09-21 09:47:31 -06:00
Curtis Gedak dbc81f1537 Provide credit for patch by Wrolf Courtney (#754649)
Add recognition for work done by Wrolf Courtney to display LVM logical
volumes.

Bug 754649 - Display Logical Volumes in Volume Group of LVM2 Partition
2015-09-20 19:40:44 +01:00
Wrolf Courtney 9e950e89b4 Display list of Logical Volumes in the Partition Information dialog (754649)
Bug 754649 - Display Logical Volumes in Volume Group of LVM2 Partition
2015-09-15 20:12:12 +01:00
Mike Fleetwood ff3f51ac29 Update names of the ntfs and reiserfs specific packages (#753436)
The ntfs-3g package previously provided the FUSE based NTFS driver to
mount the file system and ntfsprogs provided the user space tools.  In
April 2011 the packages have merged [1] forming ntfs-3g_ntfsprogs.  Arch
Linux / Debian / Slackware / Ubuntu now just have an ntfs-3g package
with everything; where as CentOS / Fedora / openSUSE are sticking with
the original two package names.  Reverse the order of the needed
packages to:

    ntfs-3g / ntfsprogs

[1] Release: NTFS-3G + NTFSPROGS 2011.4.12
    http://www.tuxera.com/release-ntfs-3g-ntfsprogs-2011-4-12/

For reiserfs the upstream package is named reiserfsprogs.  Arch Linux /
Debian / Slackware / Ubuntu use this name; but CentOS / Fedora name it
reiserfs-utils and openSUSE names it just reiserfs.  Update the README
file with all 3 package names.

    reiserfsprogs / reiserfs-utils / reiserfs

In the File System Support dialog just use the first 2 package names as
we don't want to make the dialog too wide with all 3 names and hopefully
on openSUSE it is more obvious that the reiserfs package is needed to
support the reiserfs file system.

NOTE:
Again this slightly increases the width of the File System Support
dialog on my CentOS 6 desktop with default fonts, now from 676 to 707
pixels.  Again still well within the 800 wide target and still narrower
than the main window.

Bug 753436 - Update documentation of GParted software dependencies
2015-08-10 11:34:03 -06:00
Mike Fleetwood 4160ae5018 Update name of the btrfs file system specific package (#753436)
In Fedora bug report:
    Bug 1176108 - Warning shown on BTRFS partition because of missing btrfs-tools package
    https://bugzilla.redhat.com/show_bug.cgi?id=1176108#c0
The user said:
    However there is no btrfs-tools package in the standard Fedora repo.
    There is a btrfs-progs package, which is already installed.  It's
    unclear whether this is a real error or simply a mismatched package
    name.

The upstream software is named btrfs-progs.  Arch Linux / CentOS /
Fedora / Slackware use the upstream name.  Debian / Ubuntu name it
btrfs-tools and openSUSE calls it btrfsprogs (no dash).

Rename the needed software to:

    btrfs-progs / btrfs-tools

Upstream name first separated by slash from alternative names
distributions use.

NOTE:
This slightly increases the width of the File System Support dialog on
my CentOS 6 desktop with default fonts, from 655 to 676 pixels.  Still
well within the 800 wide target and still narrower that the main window.

Bug 753436 - Update documentation of GParted software dependencies
2015-08-10 11:31:31 -06:00
Mike Fleetwood 9f0b5ee633 Remove "Skip setting unsupported partition flag" message
This message would be displayed as part of the details for an operation
to create or format a partition as LVM2 PV on a disk using partition
table types dvh or pc98, which don't have an lvm flag.

 v Format /dev/sdb1 as lvm2 pv
   > calibrate /dev/sdb1
   > clear old file system signatures in /dev/sdb1
   > set partition type on /dev/sdb1
       Skip setting unsupported partition flag: lvm
   > create new lvm2 pv file system

Translators found this message difficult to translate, as reported in
bug 752901 - Add translator comment or fix string.  It is also only
standard practice to set the lvm flag on partitions containing LVM2 PVs,
and not required.  In hindsight reporting that an operation was skipped,
when the operation is not a necessity, is probably bad practice as it
could lead to uncertainty on behalf of the user and questions as to why
the flag isn't supported.  Just remove the message.
2015-08-08 09:55:42 -06:00
Mike Fleetwood 3276951ccd Minor update of colours for LVM2 PVs and ReFS
Correct the colour name comment for NILFS2.

Colour for LVM2 PV was a Medium Brown.  This is close to Face Skin Dark
from the GNOME colour palette.  Use this instead.

Make colour for ReFS a bit darker so it is more distinct from the colour
for NTFS.
2015-08-08 09:55:42 -06:00
Mike Fleetwood 71715a1c29 Add detection of ZFS (#752862)
Requires blkid from util-linux >= 2.15, released May 2009, for
detection of ZFS.

Bug 752862 - ZFS is not recognised
2015-08-08 09:55:42 -06:00
Curtis Gedak 0dc05fdf3b Add translation comments for code section setting partition flag (#752901)
Add translation comments to improve the clarity of some translatable
text strings regarding setting partition flags.

Bug 752901 - Add translator comment or fix string
2015-07-27 17:57:52 +01:00
Curtis Gedak c7c42f2cc5 Adjust pointers to prevent crash when resizing a logical partition (#752587)
Opening the Resize/Move dialog on a logical partition causes GParted to
crash.  This crash affects current GParted GIT HEAD, but does not affect
GParted 0.22.0.  Git bisect identifies that it was broken with the
following commit:
    Remove Set_Data() from the copy, resize/move and new dialog class APIs
    7a4a375ed6

The problem was trying to treat the reference display_partitions_ref
like a pointer, and in particular on line 1732 trying to make it refer
to the a different vector of partitions, .logicals sub-vector.

  1721  void Win_GParted::activate_resize()
  1722  {
  ...
  1726          std::vector<Partition> & display_partitions_ref = display_partitions;
  1727          if ( selected_partition_ptr->type == TYPE_LOGICAL )
  1728          {
  1729                  unsigned int ext = 0 ;
  1730                  while ( ext < display_partitions.size() && display_partitions[ext].type != TYPE_EXTENDED )
  1731                          ext++;
* 1732                  display_partitions_ref = display_partitions[ext].logicals;
  1733          }
  1734
  1735          Dialog_Partition_Resize_Move dialog( gparted_core.get_fs( selected_partition_ptr->filesystem ),
  1736                                               *selected_partition_ptr,
  1737                                               display_partitions_ref );

What was actually happening was that the .logicals sub-vector was being
copied, replacing the display_partitions vector and freeing the original
sub-vector.  This left selected_partition_ptr pointing to the original
memory where the selected partition use to exist in the .logicals
sub-vector.  At some point in the Dialog_Partition_Resize_Move class
*selected_partition_ptr was referenced, accessing the freed memory.
Crash soon followed.

Fix by using a pointer instead of a reference, which can be assigned to
point to a different object.

Bug 752587 - GParted crashing when opening Resize/Move dialog on
             logical partition
2015-07-22 17:22:08 +01:00
Curtis Gedak 1561d1ae7e Add libparted ped_file_system_resize thread to avoid blocking GUI (#737022)
Since GParted commit 52a2a9b "Reduce threading (#685740)", released in
GParted 0.15.0, application of operations occurs in the main thread
running the UI, therefore long running libparted actions such as
resizing a FAT16 or FAT32 file system hang the UI for as long as it take
to complete the operation.
https://git.gnome.org/browse/gparted/commit/?id=52a2a9b00a32996921ace055e71d0e09fb33c5fe

Though this problem exists for all libparted actions, it is particularly
noticeable when performing a large resize of fat16/fat32/hfs/hfs+ file
systems.

To address this significant cause of an unresponsive GUI, this
enhancement adds threading to the libparted ped_file_system_resize
function call.

Bug 737022 - UI hangs while running libparted operations such as
             FAT16/FAT32 resizing
2015-07-19 21:57:17 +01:00
Mike Fleetwood 038209a9cf Remember result of searching the PATH for udevadm and udevsettle cmds
GParted was also searching the PATH for the availability of the udevadm
and udevsettle commands for every device with a busy partition during a
refresh and for every applied operation effecting a partition table.  As
with hdparm previously this was wasteful.

Again, remember the result of searching the PATH at startup and refresh
when clicking on the [Rescan For Supported Actions] button in the File
System Support dialog.
2015-07-01 10:22:57 -06:00
Mike Fleetwood 54d0e3d056 Remember result of searching the PATH for the hdparm command (#751251)
Previously on every refresh for every device, GParted was searching the
PATH to discover if the hdparm command existed.  Stracing GParted showed
that calling Glib::find_program_in_path("hdparm") made the following OS
calls:
    access("/usr/lib64/qt-3.3/bin/hdparm", X_OK) = -1 ENOENT (No such file or directory)
    access("/usr/local/sbin/hdparm", X_OK) = -1 ENOENT (No such file or directory)
    access("/usr/local/bin/hdparm", X_OK) = -1 ENOENT (No such file or directory)
    access("/sbin/hdparm", X_OK) = 0
    getuid()                    = 0
    stat("/sbin/hdparm", {st_mode=S_IFREG|0755, st_size=137, ...}) = 0
    stat("/sbin/hdparm", {st_mode=S_IFREG|0755, st_size=137, ...}) = 0

The Linux VFS is very fast but repeatedly doing this is wasteful.
Remember the result of searching the PATH for the hdparm command at
startup and refresh this when the [Rescan For Supported Actions] button
is pressed in the File System Support dialog.  This is the same as
GParted already does for file system specific commands and their
capabilities.

Bug 751251 - Show serial number in device information
2015-07-01 10:22:57 -06:00
Mike Fleetwood 4fce7cd5ee Don't hang reading binary data from command output (#751251)
With a Kobo Touch eReader connected via USB, GParted would hang when
scanning the device with this error written to the terminal:

    $ sudo src/gpartedbin
    ======================
    libparted : 2.3
    ======================

    (gpartedbin:10261): glibmm-CRITICAL **:
    unhandled exception (type Glib::Error) in signal handler:
    domain: g_convert_error
    code  : 1
    what  : Invalid byte sequence in conversion input

The hdparm command was printing binary data as the serial number.
Fragment of the 'hdparm -I /dev/sdf' output:

    # hdparm -I /dev/sdf

    /dev/sdf:
    SG_IO: bad/missing sense data, sb[]:  70 00 05 00 00 00 00 0a 00 00 00 00 24 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

    ATAPI Optical card reader/writer, with non-removable media
            Model Number:       {BINARY_DATA}
            Serial Number:      {BINARY_DATA}
            Firmware Revision:  {BINARY_DATA}

GParted reads command output using the Glib::IOChannel class.  However
by default an IOChannel performs character set conversion on the data it
reads, so when it came across an invalid byte sequence in the binary
data the above exception was raised and the IOChannel::read() method
never returned.  Hence GParted became stuck reportedly scanning the
same device forever.  Code fragment:

src/PipeCapture.cc
    49  bool PipeCapture::OnReadable( Glib::IOCondition condition )
    50  {
    ...
    58          Glib::ustring str;
>>  59          Glib::IOStatus status = channel->read( str, 512 );
    60          if (status == Glib::IO_STATUS_NORMAL)
    61          {
    62                  for( Glib::ustring::iterator s = str.begin(); s != str.end(); s++ )

Quote from the IOChannel class reference:
    https://developer.gnome.org/glibmm/stable/classGlib_1_1IOChannel.html

    Note that IOChannels implement an automatic implicit character set
    conversion to the data stream, and usually will not pass by default
    binary data unchanged. To set the encoding of the channel, use
    e.g. set_encoding("ISO-8859-15"). To set the channel to no encoding,
    use set_encoding() without any arguments.

Fix by disabling the automatic character set conversion in the IOChannel
used to read output from executed commands.

Bug 751251 - Show serial number in device information
2015-07-01 10:22:57 -06:00
Mike Fleetwood 4b72ecd44e Display device serial numbers (#751251)
Run "hdparm -I /dev/DISK" to get the hard drive serial number of
every device which has one and display it in the Device Information.
The displayed value can either be the actual serial number, "none" or
blank.  "none" means the device doesn't have a hard drive serial number,
such as for Linux software RAID arrays, BIOS fake RAID arrays or USB
flash drives.  Blank means something went wrong getting the serial
number.  Either it couldn't be found in the hdparm output or the hdparm
command wasn't installed.

Example real hard drive:
    # hdparm -I /dev/sda
    ...
    ATA device, with non-removable media
            Model Number:       SAMSUNG HM500JI
            Serial Number:      S1WFJDSZ123732
    ...

Example Linux software RAID array:
    # hdparm -I /dev/md127

    /dev/md127:
     HDIO_DRIVE_CMD(identify) failed: Inappropriate ioctl for device

On my desktop with 4 internal hard drives 2 Linux software RAID arrays
on those hard drives, 2 USB flash drives and 1 USB hard drive attached,
running hdparm 9 times added 0.07 seconds to the device refresh time.

Bug 751251 - Show serial number in device information
2015-07-01 10:22:47 -06:00
Mike Fleetwood 8308ee6051 Support changing the UUID of a btrfs file system (#751337)
Btrfs-progs 4.1, released June 2015, includes support for changing the
UUID of a btrfs file system using the btrfstune command.  Check for
availability by looking for the -u option in the btrfstune help output.
Use btrfstune like this:

    # umount /dev/sdb1
    # btrfstune -f -u /dev/sdb1
    Current fsid: e7ad5dba-d721-4f99-990b-1ba2901c8ad2
    New fsid: 231563d9-e173-410d-b1da-d34c4319a423
    Set superblock flag CHANGING_FSID
    Change fsid in extents
    Change fsid on devices
    Clear superblock flag CHANGING_FSID
    Fsid change finished
    # echo $?
    0

Bug 751337 - btrfstune in btrfs-progs 4.1 supports changing the file
             system UUID
2015-06-28 10:57:58 -06:00
Mike Fleetwood d405bb2264 Provide comment for btrfs::clear_cache() call
Explain why the implementation uses a clear_cache() call, rather than a
straight load_cache() call.  This commit from 2014-02-17 implemented
incremental loading of the btrfs device cache:
    76e64f2905
    Detect busy status of multi-device btrfs file systems (#723842)
2015-06-13 10:56:31 -06:00
Mike Fleetwood 9be8d37600 Delay loading LVM2_PV_info cache until actually needed (#750582)
The lvm query commands were always run and the cache loaded even if
GParted, actually blkid, didn't identify any LVM2 PVs.  (GParted uses
libparted and blkid to identify partition content and the lvm commands
to provide the needed configuration details).

Now implement complete lazy initialization of the cache.  Never force
loading of the cache.  The cache is only loaded when the first value is
accessed from it.  When there are no LVM2 PVs, the cache is never
queried, so never loaded.  All the needed infrastructure for delayed
loading was previously added by this commit from 2011-12-11:
    ff8ad04120
    Lazy initialize the cache from querying LVM2 PVs (#160787)
Every public member function which access values from the cache already
calls initialize_if_required().  Just need to replace force loading of
the cache with a function which just clears the cache.

On my desktop, only when there are no LVM2 PVs, not loading the cache
and therefore not executing these external commands in
load_lvm2_pv_info_cache() saves 1.0 seconds of the 3.7 seconds it takes
to perform the a refresh in GParted:
    lvm vgscan
    lvm pvs ... -o pv_name,...
    lvm pvs ... -o vg_name,...

Bug 750582 - Refactor the LVM2_PV_Info module object interface and
             internal cache representation
2015-06-13 10:56:31 -06:00
Mike Fleetwood e6f7ea01f9 Parse LVM2_PV_Info cache into fields while loading (#750582)
GParted used to cache the results of the "lvm pvs" commands used to query
the state of the Logical Volume Manager as a series of lines of text.
Then every time a particular value was queried GParted would split all
the lines of text into fields until the required value was found.

Stop this repeat splitting of cached lines of text.  Instead parse the
lines of text into separate fields and store in structures of values of
the correct type in the cache.

Bug 750582 - Refactor the LVM2_PV_Info module object interface and
             internal cache representation
2015-06-13 10:56:15 -06:00
Mike Fleetwood 2c5e7b0d90 Stop needing any LVM2_PV_Info objects (#750582)
The LVM2_PV_Info cache had a pretend multi-object interface, yet all the
data is static.  An LVM2_PV_Info object doesn't contain any member
variables, yet was needed just to call the member functions.

Make all the member functions static removing the need to use any
LVM2_PV_Info objects.

Bug 750582 - Refactor the LVM2_PV_Info module object interface and
             internal cache representation
2015-06-13 10:56:12 -06:00
Mike Fleetwood 81f0b934bc Stop borrowing the constructor to load the LVM2_PV_Info cache (#750582)
An LVM2_PV_Info object contains no member variables as all the data is
static (exists once in the program and accessed by all objects).  The
constructor did nothing, except when passed true to load the cache.

Provide a separate load_cache() member function and remove the
constructors and destructor which do nothing.  The C++ compiler will
provide a default constructor and destructor, which don't do anything as
there are no member variables to initialise and finalise.

This makes the interface a little easier to understand.  Mostly a step
along the way of refactoring how the LVM2_PV_Info cache module works.

Bug 750582 - Refactor the LVM2_PV_Info module object interface and
             internal cache representation
2015-06-13 10:55:48 -06:00
Mike Fleetwood 7a4a375ed6 Remove Set_Data() from the copy, resize/move and new dialog class APIs
The copy, resize/move and new dialog classes (Dialog_Partition_Copy,
Dialog_Partition_Resize_Move and Dialog_Partition_New respectively) had
to be used like this:

    construct dialog object passing some parameters
    call Set_Data() to pass more parameters
    run() dialog
    call Get_New_Partition()

There is nothing in the classes which forces Set_Data() to be called,
but it must be called for the dialogs to work and prevent GParted from
crashing.

Make these class APIs safer by making it impossible to program
incorrectly in this regard.  Move all the additional parameters from
each Set_Data() method to each constructor.  The constructors just call
the now private set_data() methods.
2015-06-10 10:44:33 -06:00
Mike Fleetwood 32a5ace156 Rename Dialog_Base_Partition member to new_partition
The member variable was named selected_partition.  It is assigned from
Win_GParted::selected_partition_ptr (which is a pointer to a const
partition object so is never updated).  This gives connotations that it
won't be modified.

However it is updated freely as the new resultant partition object is
prepared before being returned from the dialog, most notable in the
Get_New_Partition() methods.

Therefore rename from selected_partition to new_partition.
2015-06-10 10:44:33 -06:00
Mike Fleetwood 8b96f8409f Stop copying selected_partition back on itself in the copy dialog (#750168)
The code goes like this:

    Dialog_Partition_Copy::Get_New_Partition()
        call Dialog_Base_Partition::Get_New_Partition()
            Update this->selected_partition with results from running
            the dialog.
            return this->selected_partition by value.
        Save value back to this->selected_partition.
        Update this->selected_partition some more.
        return this->selected_partition by value.

So there is an unnecessary copy of the partition object returned from
the base class Get_New_Partition() function back to the same variable in
the derived copy class Get_New_Partition() function.

Need to keep the base class Get_New_Partition() function as derived
class Dialog_Partition_Resize_Move uses that implementation as it
doesn't override it, and it's part of the interface.

Avoid this unnecessary copy by moving base class Get_New_Partition()
code into a new private function, called prepare_new_partition(), which
doesn't return anything.  Then have Get_New_Partition() in both classes
just return the required partition object.  Like this:

    Dialog_Base_Partition::Get_New_Partition()
        call prepare_new_partition()
        return this->selected_partition by value.

    Dialog_Partition_Copy::Get_New_Partition()
        call Dialog_Base_Partition::prepare_new_partition()
        Update this->selected_partition some more.
        return this->selected_partition by value.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:44:33 -06:00
Mike Fleetwood 90e3ed68fc Shallow copy Device object into Operation object (#750168)
When Operation objects are created they take a copy of the Device object
to which the operation is to be applied.  The Device object includes a
vector of all the contained Partition objects currently on the device,
so these get copied too.

These additional deep copied Partition objects in the Operation object
are never accessed.  Therefore don't copy the contained Partition
objects when copying the Device object into the Operation object.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:44:33 -06:00
Mike Fleetwood ece945685c Stop copying selected partition object in Manage Flags dialog (#750168)
When opening the Manage Flags dialog, creation of the dialog object was
creating a copy of the selected partition object.  If this was an
extended partition it also included recursively constructing the
contained logical partitions too.

Instead, replace the partition object in the DialogManageFlags class
with a reference to it.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:44:33 -06:00
Mike Fleetwood efaea94301 Stop copying selected partition object in Information dialog (#750168)
When opening the Partition Information dialog, creation of the dialog
object was creating a copy of the partition object to be displayed.  If
this was an extended partition it also included recursively constructing
the contained logical partitions too.

Instead, replace the partition object in the Dialog_Partition_Info class
with a reference to it.

NOTE:
In C++ a reference is really just a pointer under the hood.  As such,
dereferences of a pointer to an object in the context of needing a
reference to the object doesn't copy the object.  It merely initialises
the reference from the pointer.

Specifically, with this prototype:
    Dialog_Partition_Info( const Partition & partition );
and the dialog object being constructed in Win_GParted::activate_info():
    Dialog_Partition_Info dialog( *selected_partition_ptr );
the partition object is not copy constructed.  A reference (pointer) to
it is merely passed to the dialog constructor.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:43 -06:00
Mike Fleetwood 7f05037dbc Stop copying displayed partition objects in activate_resize() (#750168)
The first actions of Win_GParted::activate_resize() were to create a
copy of the vector of partitions for the currently displayed device and
visually apply any pending operations.  Exactly this has already been
done in Win_GParted::Refresh_Visual() with the result now available in
the member variable display_partitions.  Stop this unnecessary partition
object copying and processing by just using display_partitions member
variable instead.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:40 -06:00
Mike Fleetwood 1143917d34 Document how the GUI works and the lifetimes of important data (#750168)
Document how GParted displays partitions in the GUI and manages the
lifetime and ownership of that data.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:40 -06:00
Mike Fleetwood 6ae327c8f9 Assert selected_partition_ptr is valid before use (#750168)
Further ensure that a bug doesn't get introduced with the use of
selected_partition_ptr, by asserting that it points to a current
partition object in the vector of display partitions.

After deliberately breaking the code so that selected_partition_ptr
points to some other partition object, trying to display the Information
dialog causes this crash:

======================
libparted : 2.4
======================
**
ERROR:Win_GParted.cc:989:void GParted::Win_GParted::set_valid_operations(): assertion failed: (valid_display_partition_ptr( selected_partition_ptr ))
Aborted (core dumped)

At this point in the code:

    973  void Win_GParted::set_valid_operations()
    974  {
    ...
    986          // No partition selected ...
    987          if ( ! selected_partition_ptr )
    988                  return ;
>>  989          g_assert( valid_display_partition_ptr( selected_partition_ptr ) );  // Bug: Not pointing at a valid display partition object

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:40 -06:00
Mike Fleetwood 5e027d6989 Assert selected_partition_ptr is not NULL (#750168)
Add Glib g_assert() to ensure that a bug doesn't get introduced which
allows a partition callback to be called without a partition being
selected first.

After deliberately breaking the code so that selected_partition_ptr is
not set, trying to display the Information dialog causes this crash:

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================
    ERROR:Win_GParted.cc:1978:void GParted::Win_GParted::activate_info(): assertion failed: (selected_partition_ptr != NULL)
    Aborted (core dumped)

At this point in the code:

   1976  void Win_GParted::activate_info()
   1977  {
>> 1978          g_assert( selected_partition_ptr != NULL );  // Bug: Partition callback without a selected partition
   1979

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:17 -06:00
Mike Fleetwood da39e3cad3 Change selected partition into a pointer (#750168)
Now that TreeView_Details and DrawingAreaVisualDisk classes store and
pass pointers to partition objects in the Gtk signal callbacks, change
the selected partition into a pointer too.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:14 -06:00
Mike Fleetwood cc1448abd2 Store pointers to partition objects in DrawingAreaVisualDisk (#750168)
This stops copying of each displayed partition object into the
DrawingAreaVisualDisk class.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:14 -06:00
Mike Fleetwood acd5d7e580 Store pointers to partition objects in TreeView_Details (#750168)
This stops copying of each displayed partition object into the
TreeView_Details class.

It also stops copy constructing lots of partition objects when just
clicking on a partition in the disk graphic.  The disk graphic needs to
inform the main GUI and then the partition list which partition has been
selected.  The call sequence goes like:

    DrawingAreaVisualDisk::on_button_press_event(event)
      Win_GParted::on_partition_selected(partition_ptr, src_is_treeview)
        TreeView_Detail::set_selected(partition_ptr)
          TreeView_Detail::set_selected(rows, partition_ptr,
                                        inside_extended)

Relevant source and highlighted comparison line:

   140  bool TreeView_Detail::set_selected( Gtk::TreeModel::Children rows,
   141                                      const Partition * partition_ptr, bool inside_extended )
   142  {
   143          for ( unsigned int t = 0 ; t < rows .size() ; t++ )
   144          {
>> 145                  if ( static_cast<Partition>( rows[t][treeview_detail_columns.partition] ) == *partition_ptr )
   146                  {
   147                          if ( inside_extended )
   148                                  expand_all() ;
   149
   150                          set_cursor( static_cast<Gtk::TreePath>( rows[ t ] ) ) ;
   151                          return true ;
   152                  }
   153
   154                  if ( set_selected( rows[t].children(), partition_ptr, true ) )
   155                          return true ;
   156          }
   157
   158          return false ;
   159  }

Then in this function the partition selected in the disk graphic
(partition_ptr parameter) is compared in turn with each partition object
stored in the Gtk::TreeView model to find the matching one to mark it as
selected.  This mere act of accessing the partition object stored in a
row of the Gtk::TreeView model causes it to be copy constructed.  So
clicking on the 5th partition in the disk graphic will copy construct
the first 5 partition objects just to do a compare to find the matching
one.

This is because it is not possible to get a reference from a
Gtk:TreeViewProxy in gtkmm.  Merely accessing a value in a Gtk::TreeView
model takes a copy of that value.

    Subject: get a reference from a Gtk::TreeValueProxy
    http://comments.gmane.org/gmane.comp.gnome.gtkmm/2217
    http://marc.info/?t=104400417500001&r=1&w=4

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:14 -06:00
Mike Fleetwood c430acf52a Pass by pointer in the signal_partition_selected callbacks (#750168)
Change from passing a reference to the selected partition, to passing a
pointer to the selected partition in the signal_partition_selected
callbacks between the disk graphic, partition list and core GUI modules.

This is an enabler for the following patches.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:43:14 -06:00
Mike Fleetwood 545b75d957 Move vector of partition objects to a Win_GParted class member (#750168)
Win_GParted::Refresh_Visual() used a local variable containing a copy of
the vector of partitions in the current device to be displayed.  After
visually applying pending operations it loaded copies of each partition
object into the GUI widgets to display the disk graphic and partition
list, DrawingAreaVisualDisk and TreeView_Details classes respectively.
When a partition is selected in the UI, again a partition object is
copied.  Also several of the partition dialogs, including the
information dialog, take a copy of the partition object.  All these are
copies of the same set of partition objects, those currently being
displayed in the UI.

Move the vector of displayed partitions from a local variable in
Refresh_Visual() to a Win_GParted member variable.  This will allow for
the above cases to be changed to used pointers and references to the
same set of partition objects.

The valid lifetime of pointers to elements in this partition object
vector is from one refresh to the next, when the vector is cleared and
repopulated with a new set of partition objects.  This is exactly what
is needed as the GUI widgets are reloaded on each refresh, the selected
partition is reset and none of the partition dialog objects exist.
Dialog objects being created and destroyed on each use.

On the other hand some copies of partition objects currently being
displayed, still need to be made because they have lifetimes which need
to last longer than the next call to Refresh_Visual().  Specifically the
source of the copy partition and the partition objects copied into the
in the list of pending operations.

Bug 750168 - Reduce the amount of copying of partition objects
2015-06-10 10:42:36 -06:00
Mike Fleetwood 8bbb77f1f8 Remove cylinder size adjustments in the copy dialog (#749867)
BUF in the copy dialog class, Dialog_Partition_Copy, is use to adjust
limits in 2 cases:

1) Minimum size when copying an XFS file system

Minimum size was set to the used space + 2 * cylinder size (typically
plus ~16 MiB).  This commit from 2004-12-20 added it:
    a54b52ea33
    xfs copy now uses xfsdump and xfsrestore. icw some hacks in the other 2

Issues:
* This is increasing the minimum XFS file system size when copying it,
  which doesn't happen in the resize case for other file systems.
* It allows an XFS file system to be created which is smaller than the
  minimum size allowed by GParted.  Copying an empty XFS file system can
  create a new file system as small as 26 MiB.  This is smaller than the
  minimum GParted allows of 32 MiB because that is the minimum
  xfs_repair can handle.

Remove this addition when copying an XFS file system and enforce minimum
file system size.

2) Maximum size when copying a file system into empty space larger than
   it's maximum size

Maximum size was set to maximum file system size - cylinder size
(typically minus ~8 MiB).  Only applied to FAT16 which has a maximum
file system size set in and can be grown.  Added by this commit from
2004-12-15:
    10e8f3338d
    :get_fs now returns a const reference. in copy and resizedialog
    ...
    * in copy and resizedialog filesystems with MAX set now have a max size of MAX - one cylinder .

Issue:
* This is applying a lower maximum resize when copying the file system
  compared to that when creating the file system.
  NOTE:
  GParted currently allows all file systems to be resize to any size,
  regardless of the maximum file system size.  This is probably an
  oversight, but it does allow libparted to convert FAT16 to FAT32 file
  system when resizing.

Remove this lower maximum file system size when copying and resizing,
compared to creating.

Bug 749867 - Some limits are adjusted by arcane cylinder size amount
             when copying and resizing in a single operation
2015-05-28 12:53:41 -06:00
Mike Fleetwood b9262922a7 Remove last trace of cylinder size adjustments in the resize dialog (#749867)
This commit from 2010-05-20 removed use of cylinder size increase in the
minimum, and cylinder size decrease in the maximum file system sizes
from the resize/move dialog.
    e62a23b5b5
    Add partition alignment option to align to MiB (#617409)

This cylinder size limit adjustments were being performed using the
Dialog_Base_Partition::BUF member variable.  Now in the
Dialog_Partition_Resize_Move class it is never accessed, and only
unnecessarily set.  Move BUF from the common base class into the
Dialog_Partition_Copy class where it is still used.

Bug 749867 - Some limits are adjusted by arcane cylinder size amount
             when copying and resizing in a single operation
2015-05-28 12:44:51 -06:00
Mike Fleetwood 9ced6b051e Small simplification of Win_GParted code which calls get_custom_text()
Avoid long lines, long statements and repeated calls to
gparted_core.get_filesystem_object( selected_partition.filesystem ) by
storing the returned pointer in a local variable.

Needs the previous commit so that the the local variable can be a
pointer to a const FileSystem object instead of a pointer to a
(modifiable) FileSystem object.
2015-05-19 10:34:59 -06:00
Mike Fleetwood f6e4390aaf Add const qualifier to get_custom_text() member functions
The function never modifies any member variables so make it a const
member function.

(FileSystem::get_custom_text() is a virtual function so can't be made
static).
2015-05-19 10:34:59 -06:00
Mike Fleetwood d0580d5955 Rename two GParted_Core methods to detect_filesystem*()
Rename a couple of GParted_Core methods for consistency and to better
distinguish get_filesystem() from get_filesystems() which do completely
unrelated things.

  get_filesystem()                 -> detect_filesystem()
  recognise_filesystem_signature() -> detect_filesystem_internal()

Also make detect_filesystem() a static member method as it doesn't use
any member variables.  Requirement cascades to get_partition_path().
2015-05-07 08:01:43 -06:00
Mike Fleetwood df000a94a6 Tidy-up GParted_Core::init/fini_filesystems() function declarations
These member functions are only used within the GParted_Core class and
only operate on the static member variable FILESYSTEM_MAP.

Make both functions private and also make init_filesystems() static.
2015-05-07 08:01:43 -06:00
Mike Fleetwood 40820bada7 Fix memory leak of FileSystem objects in init_filesystems() (#749036)
The FileSystem objects stored in the FILESYSTEM_MAP are allocated once
using new in init_filesystems() but never deleted.

Valgrind output fragment:

    # valgrind --leak-check=full ./gparted
    ==29314== 353 (72 direct, 281 indirect) bytes in 1 blocks are definitely lost in loss record 6,287 of 6,905
    ==29314==    at 0x4A075FC: operator new(unsigned long) (vg_replace_malloc.c:298)
>>  ==29314==    by 0x46EDA5: GParted::GParted_Core::init_filesystems() (GParted_Core.cc:106)
    ==29314==    by 0x46EC5F: GParted::GParted_Core::GParted_Core() (GParted_Core.cc:96)
    ==29314==    by 0x4A74F4: GParted::Win_GParted::Win_GParted(std::vector<Glib::ustring, std::allocator<Glib::ustring> > const&) (Win_GParted.cc:51)
    ==29314==    by 0x4D600A: main (main.cc:56)
    ...
    ==29314== 161 (72 direct, 89 indirect) bytes in 1 blocks are definitely lost in loss record 6,119 of 6,905
    ==29314==    at 0x4A075FC: operator new(unsigned long) (vg_replace_malloc.c:298)
>>  ==29314==    by 0x46F50C: GParted::GParted_Core::init_filesystems() (GParted_Core.cc:124)
    ==29314==    by 0x46EC5F: GParted::GParted_Core::GParted_Core() (GParted_Core.cc:96)
    ==29314==    by 0x4A74F4: GParted::Win_GParted::Win_GParted(std::vector<Glib::ustring, std::allocator<Glib::ustring> > const&) (Win_GParted.cc:51)
    ==29314==    by 0x4D600A: main (main.cc:56)

GParted_Core.cc source:

   102  void GParted_Core::init_filesystems()
   103  {
   104          FILESYSTEM_MAP[ FS_UNKNOWN ]         = NULL ;
   105          FILESYSTEM_MAP[ FS_CLEARED ]         = NULL ;
>> 106          FILESYSTEM_MAP[ FS_BTRFS ]           = new btrfs() ;
   ...
>> 124          FILESYSTEM_MAP[ FS_XFS ]             = new xfs() ;
   125          FILESYSTEM_MAP[ FS_BITLOCKER ]       = NULL ;

Fix by deleting all FILESYSTEM_MAP pointers.  Note that delete on a NULL
pointer is defined by C++ as a safe do nothing operation.

    C++ FAQ / Do I need to check for null before delete p?
    https://isocpp.org/wiki/faq/freestore-mgmt#delete-handles-null

Fixing this reduces the valgrind reported definitely lost memory blocks
count from 25 down to 6.  19 FileSystem objects deleted and 19 memory
blocks no longer lost.

Bug 749036 - FileSystem objects are memory leaked in init_filesystems()
2015-05-07 08:01:43 -06:00
Mike Fleetwood cca8a55f51 Refactor operation cases in apply_operation_to_disk() (#746559)
Background:

GParted_Core::calibrate_partition() reloads the partition path name and
boundary to ensure they are correct before the operation is performed.
(See comments in calibrate_partition() for the reasons why this is
necessary).  This also displays details of the partition being modified
in the operation details to inform the user.

The operation object contains these relevant member objects:

  * partition_original
    Partition before the operation is applied.

  * partition_new
    Partition as it is intended to be after the operation has been
    applied.

  * partition_copied (for the copy operation only)
    Source partition being copied.

Issues:

GParted_Core::apply_operation_to_disk() was always calibrating partition
object partition_original, but for about half the operations
partition_original was not used and partition_new is used, so should be
calibrated instead.

Copy into an existing partition calibrated three partitions, the source,
destination before and destination after the operation was applied.
This doesn't really make sense in the operation details to the user.
They would expect to only see the source and destination partitions and
don't care about the distinction between the before and after
representation of the destination.

Minor issues:

The previous fix had to copy the correct partition path from the
calibrated partition_original object to the used partition_new object
for the format, label file system, name partition and change uuid
operations.

Calibrate was called for the create operation too, even though the
partition didn't yet exist.  It was a no-operation.

Fix:

Stop always calibrating the partition_original object and instead
calibrate the correct partition object in each operation case.  For the
copy into existing partition operation only calibrate the right two
partition objects as the user would expect.

Bug 746559 - Various operations fail when following paste into existing
             partition
2015-03-26 20:33:09 +00:00
Mike Fleetwood d9993c21ba Fix failing operations following paste into existing partition (#746559)
Format, label file system and new UUID operations would fail when
applied in a sequence to the destination partition following a previous
copy-paste operation.

Giving the copy of a file system a new label and a new UUID are the sort
of actions which should be performed when the disk containing the copy
remains attached to the same computer.  This really should work.

Fragment of the failing operation details for a copy and label operation
sequence:

    + Copy /dev/sdb1 to /dev/sdb2
      + calibrate /dev/sdb2
      + calibrate copy of /dev/sdb1
      + calibrate /dev/sdb1
      + check the file system on /dev/sdb1 for errors and (if possible fix them
      + copy file system of /dev/sdb1 to /dev/sdb2
    + Set file system label "small-dst" on copy of /dev/sdb1
      + calibrate copy of /dev/sdb1
          path: /dev/sdb2 (partition)
          ...
      + set file system label to "small-dst" on copy of /dev/sdb1
        + e2label copy of /dev/sdb1 "small-dst"
          Usage: e2label device [newlabel]

This is failing because the file system specific command is passed
"copy of /dev/sdb1" as the device name.  Code sequence:

 1) OperationCopy::OperationCopy() sets the real path name of the
    partition_new object to "copy of /dev/SRC" for display purposes.

 2) GParted_Core::apply_operation_to_disk() calls calibrate_partition()
    on partition_original object, restoring the real path name for
    object partition_original.

 3) apply_operation_to_disk() calls format(), label_filesystem() or
    change_uuid() on the partition_new object, which still has the real
    path name set to "copy of /dev/SRC".  File system specific commands
    fail with this as a path name.

Fix by copying the real path name from object partition_original to
partition_new, as is already done for the resize/move operation.  Also
apply this fix to the name partition operation, because it uses the
partition_new object and so that it displays the real path name in the
operation details.

Bug 746559 - Various operations fail when following paste into existing
             partition
2015-03-26 20:33:09 +00:00
Mike Fleetwood 8d005aff1a Name partitions as required when creating a new partition (#746214)
When the partition is named in the Create New Partition dialog, set the
partition name as part of the create partition operation.  Currently
this is only supported for GPTs.  See
Utils::get_max_partition_name_length() for details.

Bug 746214 - Partition naming enhancements
2015-03-25 10:02:43 -06:00
Mike Fleetwood 5fd7c92671 Add partition name to Create New Partition dialog (#746214)
Add a partition name entry box to the Create New Partition dialog.  The
entry box is greyed out (not sensitive) for partition table types which
don't support partition naming.  Currently only supported for GPTs.  See
Utils::get_max_partition_name_length() for details.

There was a slightly wider gap between the file system combobox row and
the label entry row when there were only three widgets on the right hand
side of the dialog.  This has been removed now that there are four
widgets so that they are all evenly spaced and they line up with the
four widgets on the left hand side.

So far the partition name can be entered and previewed, but isn't yet
applied to the disk.

Bug 746214 - Partition naming enhancements
2015-03-25 10:02:43 -06:00
Mike Fleetwood 6a9a06af0e Pass Device object when setting up Dialog_Partition_New (#746214)
Adding a partition name entry to the Create New Partition dialog will
need access to these two Device methods: partition_naming_supported()
and get_max_partition_length().  The Set_Data() function already takes
two parameters, only_unformatted and disktype, taken from Device member
variables.

Rather than add two more parameters to the Set_Data() function pass the
Device object instead, replacing the current only_unformatted and
disktype parameters.

Bug 746214 - Partition name enhancements
2015-03-25 10:02:43 -06:00
Mike Fleetwood 650f4c7f20 Remove redundant Gtk::Entry calls in Create New Partition dialog
This is a small tidy-up to remove Gtk::Entry method calls on the file
system label entry box in the Create New Partition dialog which serve no
purpose.

filesystem_label_entry.set_activates_default( true );
    It trying to make the Create New Partition dialog automatically
    close  when Enter is pressed with focus in the label entry box.
    However this doesn't work, presumably because the default widget for
    the dialog is not the Add button.  Remove.

filesystem_label_entry.set_text( partition.get_filesystem_label() );
    Initialises the text in the entry box with the file system label
    from the passed partition object.  The label is blank and the entry
    box defaults to blank.  Achieves nothing.  Remove.

filesystem_label_entry.select_region( 0, filesystem_label_entry.get_text_length() );
    Highlights the empty text in the entry box.  Achieves nothing.
    Remove.

NOTE:
The same set of Gtk::Entry method calls in Dialog_FileSystem_Label() and
Dialog_Partition_Name, which are editing the existing file system label
and partition name respectively, do work and have a useful effect so
shouldn't be removed.
2015-03-25 10:02:43 -06:00
Mike Fleetwood fc599270c2 Rename member object to filesystem_label_entry (#746214)
Rename Gtk::Entry object entry -> filesystem_label_entry in the
Dialog_Partition_New class.  This is in preparation for the introduction
of the partition name entry box in the Create New Partition dialog.

Bug 746214 - Partition name enhancements
2015-03-25 10:02:43 -06:00
Mike Fleetwood eae2dbaa82 Preserve partition name in preview of format operation (#746214)
Preview of the format operation cleared the partition name, yet when
applied, the partition name reappeared.  Fix the preview to reflect
reality.

Bug 746214 - Partition naming enhancements
2015-03-25 10:02:42 -06:00
Mike Fleetwood 9b2c95bcd1 Make support of naming for other partition table types possible (#746214)
Previously partition naming had only been implemented for gpt.  Make the
code ready to support naming of the other partition table types for
which libparted supports naming.  Specifically: amiga, dvh, mac and
pc98 in addition to gpt.  Document issues found with some of these
partition table types, which can relatively easily been worked around.

Leave support of naming for partition table types other than gpt
disabled, mostly just to reduce ongoing testing effort, at least until
there is any user demand for it.

Bug 746214 - Partition naming enhancements
2015-03-25 10:02:42 -06:00
Mike Fleetwood f804bc3244 Allow partition naming on busy partitions (#746214)
Allow partition names to be changed whether or not the partition is
busy, rather than only when not busy, because it doesn't effect the busy
file system or change the partition boundaries in any way.

Bug 746214 - Partition naming enhancements
2015-03-25 10:02:42 -06:00
Mike Fleetwood e9cc0b15a5 Only set lvm partition flag on tables which support it (#746204)
Attempting to create a new partition on a pc98 partition table fails
with the following libparted error:

    The flag 'lvm' is not available for pc98 disk labels.

This has been broken since LVM2 Physical Volume read-write support was
first added in this commit:

    c3ab62591b
    Add creation of LVM2 PVs (#670171)

Fix by only clearing and setting the lvm partition flag when the type of
the partition table supports it.  When creating a partition to contain
an LVM2 PV and the lvm flag is not support add the following message to
the operation results to explain that setting the lvm partition flag was
skipped and why:

    Skip setting unsupported partition flag: lvm

Bug 746204 - Creating partitions on pc98 table fails with lvm flag not
             available
2015-03-24 10:56:40 -06:00
Mike Fleetwood c882666e3a Refactor set_partition_type() setting lp_partition earlier (#746204)
Refactor GParted_Core::set_partition_type().

1) Set lp_partition variable earlier and use a single if lp_partition
   set condition, rather than in both if conditions for the normal file
   system case and the LVM2 Physical Volume case.

2) Stop calling Utils::get_filesystem_string() multiple times, instead
   save the result in a local variable.

Tidies the code a little and reorders it in preparation for the
following fix to only set the lvm partition flag when support, making
that code change simpler.

Bug 746204 - Creating partitions on pc98 table fails with lvm flag not
             available
2015-03-24 10:56:40 -06:00
Mike Fleetwood 4d83d3723d Remove unused device parameter from a few GParted_Core methods
resize_move() and move() stopped using the device parameter in this
commit from 2006-07-23:
    d663c3c277
    removed cylindersize buffering during resize from the filesystems. It is

create() stopped using the device parameter in this commit from 2006-03-19:
    ad9f2126e7
    fixed issues with copying (see also #335004) cleanups + added FIXME added

For reference most other operation methods had the device parameter
removed in this earlier commit from 2005-12-07:
    642f0a145b
    from now on each partition has a reference to it's device. make use of new
2015-03-09 11:04:13 -06:00
Mike Fleetwood 9c1a833a0d Fix failure to recognise whole disk file systems in certain cases (#743181)
When the following conditions were met GParted would fail to recognise a
newly created whole disk device file system, and instead show an unknown
file system filling the disk:

1) Disk was previously partitioned and contained at least one partition.

2) Using libparted version 2.0 to 3.0 inclusive.

Initial status:
    # blkid | fgrep sdc
    # fgrep sdc /proc/partitions
       8       32  976762584 sdc
       8       33  104857600 sdc1
    # parted /dev/sdc
    GNU Parted 2.4
    Using /dev/sdc
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) print
    Model: ATA ST1000LM024 HN-M (scsi)
    Disk /dev/sdc: 1000GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: msdos

    Number  Start   End    Size   Type     File system  Flags
     1      1049kB  107GB  107GB  primary

When creating the loop partition table libparted would not inform the
kernel to delete the old partitions.  /proc/partitions still contained
the details of the old partitions.
    (parted) mktable loop
    Warning: The existing disk label on /dev/sdc will be destroyed and
    all data on this disk will be lost. Do you want to continue?
    Yes/No? Yes
    (parted) print
    Model: ATA ST1000LM024 HN-M (scsi)
    Disk /dev/sdc: 1000GB
    Sector size (logical/physical): 512B/4096B
    Partition Table: loop

    Number  Start  End  Size  File system  Flags

    (parted) quit
    # fgrep sdc /proc/partitions
       8       32  976762584 sdc
       8       33  104857600 sdc1

Creation of the whole disk device file system goes unnoticed by blkid
because the kernel and therefore blkid's cache have stale partition
information.
    # mkfs.xfs -f /dev/sdc
    # blkid | fgrep sdc

NOTE:
On a Linux Software RAID array, as opposed to a hard disk, blkid does
notice creation of the whole disk device file system.  However the
kernel still has old partition details.

This was fixed in libparted 3.1 by commit:

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=f5c909c0cd50ed52a48dae6d35907dc08b137e88
    libparted: remove has_partitions check to allow loopback partitions

Fix by deleting old partitions before creating the loop table when
compiled with a broken version of libparted.  The GParted UI provides
no feedback while a new partition table is created, and with some
versions of GTK the UI become unresponsive too, so it is important to be
as fast as possible.  Evaluated three different methods, deleting 15 and
22 MSDOS partitions on a physical 5400 RPM hard drive using libparted
2.4:

M1) Delete and commit one partition at a time.
    Takes up to 24 seconds to delete 15 partitions.  With 22 partitions
    libparted always reports finding some of the partitions busy and
    unable to inform the kernel about the modifications.
    Too slow and doesn't work.

M2) Delete all partitions in one go and commit once.
    Takes up to 1.4 seconds to delete either 15 or 22 partitions.  Never
    removes partitions 17 and higher from the kernel.
    Doesn't work.

M3) Write GPT table (letting libparted delete any old partitions).
    Takes up to 0.8 seconds to delete either 15 or 22 partitions.
    Fast and works.

Use method 3 - write a GPT table thus using libparted code to inform the
kernel of the old partition deletions.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 4087cb2e2b Workaround older blkid not distinguishing between FAT16 and FAT32 (#743181)
Older versions of blkid don't correctly distinguish between FAT16 and
FAT32 file systems when overwriting one with the other.  This effects
GParted too with these file systems on whole disk devices where only
blkid is used to recognise the contents.  See previous fix for why only
blkid is used in this case:
    Avoid whole disk FAT being detected as MSDOS partition table
    (#743181)

Example:

    # blkid -v
    blkid from util-linux 2.20.1 (liblkid 2.20.0, 19-Oct-2011)
    # mkdosfs -F16 -I /dev/md1
    # blkid | fgrep md1
    /dev/md1: SEC_TYPE="msdos" UUID="7C23-95D9" TYPE="vfat"
    # mkdosfs -F32 -I /dev/md1
    # blkid | fgrep md1
    /dev/md1: SEC_TYPE="msdos" UUID="7F93-98F4" TYPE="vfat"

So blkid recognised the UUID changed but didn't remove the SEC_TYPE for
the FAT32 file system.  See FS_Info::get_fs_type() as it uses this to
distinguish between FAT16 and FAT32.  This is a caching update bug in
blkid, because telling blkid not to use the cache gets the right
results:

    # blkid -c /dev/null | fgrep md1
    /dev/md1: UUID="7F93-98F4" TYPE="vfat"

With testing determined that blkid from util-linux 2.23 and later are
not affected and earlier versions are affected.  Mostly recently known
affected distribution is Ubuntu 14.04 LTS with util-linux 2.20.1.

The straight forward fix would be to instruct blkid to not use its cache
with 'blkid -c /dev/null'.  But using blkid's cache is needed to prevent
blkid hanging for minutes when trying to access a non-existent floppy
drive when the BIOS is set incorrectly.  See commit:

    18f863151c
    Fix long scan problem when BIOS floppy setting incorrect

Instead, when using an older affected version of blkid and when blkid
cache reports a vfat file system, run blkid again bypassing the cache.
The device is known to exist and contain a vfat file system, just not
whether it is a FAT16 or FAT32 file system, so can't be a non-existent
floppy device and won't hang.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood f8faee6377 Avoid whole disk FAT being detected as MSDOS partition table (#743181)
Libparted 1.9.0 to 2.3 inclusive, recognises whole disk device FAT file
systems as MSDOS partition tables.  This causes GParted to do the same.

    # dd if=/dev/zero bs=1M of=/dev/md4
    # mkdosfs -F32 -v -I /dev/md4
    # blkid /dev/md4
    /dev/md4: UUID="53FE-31F2" TYPE="vfat"

    # parted /dev/md4
    GNU Parted 2.1
    Using /dev/md4
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) print
    Model: Unknown (unknown)
    Disk /dev/md4: 536MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start  End  Size  Type  File system  Flags

    (parted) quit

    # /tmp/parted24/bin/parted /dev/md4
    GNU Parted 2.4
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) print
    Model: Linux Software RAID Array (md)
    Disk /dev/md4: 536MB
    Sector size (logical/physical): 512B/512B
    Partition Table: loop

    Number  Start  End    Size   File system  Flags
     1      0.00B  536MB  536MB  fat32

    (parted) quit

This was fixed in libparted 2.4 by commit:

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=616a2a1659d89ff90f9834016a451da8722df509
    libparted: avoid regression when processing a whole-disk FAT partition

Make GParted immune to this bug by moving blkid performed whole disk
device file system detection before libparted partition detection.  Also
have to always erase old file system signatures on whole disk devices
when creating new partition tables to ensure that blkid doesn't detect
those old signatures before libparted has a chance to detect the new
partition table.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood e7ed209020 Erase file system signatures before creating a partition table (#743181)
When writing "loop" partition table over the top of some whole disk
device file system types GParted continued to show those whole disk
device file systems rather than the virtual unknown partition from the
"loop" partition table.

This affected btrfs, jfs, reiser4 and reiserfs.  It occurred because of
several factors:
1) Libparted only zeroed the first and last 9.5 KiB (assuming 512 byte
   sectors) of the device before writing a new partition table.  See
   ped_disk_clobber().
2) These file systems have their super blocks and therefore signatures
   after the first 9.5 KiB.
3) Whole disk device file system detection is performed using blkid
   before checking for a libparted "loop" partition table.  See
   GParted_Core::set_devices_thread().

Ref:
libparted 3.2: disk.c:ped_disk_clobber()
http://git.savannah.gnu.org/cgit/parted.git/tree/libparted/disk.c?id=v3.2#n302

Fix by always erasing any possible file system signatures on the device
before creating a new "loop" partition table.

NOTE:
This is typically taking up to 0.5 seconds in my testing on a 5400 RPM
hard drive, during which time the GParted UI is hung and the create
partition table dialog shows the apply button pressed but no other
progress indication.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood e9a5cf2843 Skip reading existing partition table before creating a new one (#743181)
Creating a new partition table was getting libparted to read any
existing partition table before creating a new partition table on the
device.  This is an unnecessary step, and if the device didn't already
contain a partition table also printed this error from libparted:

    /dev/sdb: unrecognised disk label

Since get_device_and_disk() has been split into two, just call
get_device() instead to just populate the PedDevice object representing
the disk device.  Removes a small unnecessary step.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 63f701033e Correct whole disk device file system format to cleared preview (#743181)
The preview of clearing a whole disk device file system was previewing
the same as formatting to all other file system types; as a cleared file
system spanning the whole disk device.  However when implemented this
removes all signatures on the disk so it actually becomes an unallocated
and unpartitioned device.  Make the preview match what happens in when
implemented.

GParted previously used mydevice.max_prims = -1 to represent an
unpartitioned device.  It is now represented as:

    mydevice.max_prims = 1
    mydevice.disktype  = _("unrecognized")
    mydevice.partitions[0].type         = TYPE_UNALLOCATED
    mydevice.partitions[0].whole_device = true
    mydevice.partitions[0].filesystem   = FS_UNALLOCATED

and the check for an unpartitioned device in Win_GParted.cc becomes:

    partitions[0].type == TYPE_UNALLOCATED && partitions[0].whole_device

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 8607717b7b Make "loop" table appear as unknown whole device file system (#743181)
Previously GParted displayed a device containing the parted "loop"
partition table signature "GNU Parted Loopback 0" and nothing else, as
an unrecognised device.

Now make GParted display this as a virtual whole disk device partition
with unknown contents, complete with the unable to detect a file system
warning.  This change then allows a whole disk device file system to be
created with the following two steps:
1) Create "loop" partition table on a device;
2) Format to required file system.

GParted represents a whole disk device file system as:

    mydevice.max_prims = 1
    mydevice.disktype  = "none"
    mydevice.partitions[0].type         = TYPE_PRIMARY
    mydevice.partitions[0].whole_device = true
    mydevice.partitions[0].filesystem   = FS_EXT4  (example)

Now represents just Parted's "loop" signature as:

    mydevice.max_prims = 1
    mydevice.disktype  = "loop"
    mydevice.partitions[0].type         = TYPE_PRIMARY
    mydevice.partitions[0].whole_device = true
    mydevice.partitions[0].filesystem   = FS_UNKNOWN

And as before, an unpartitioned device as:

    mydevice.max_prims = -1
    mydevice.disktype  = _("unrecognized")
    mydevice.partitions[0].type         = TYPE_UNALLOCATED
    mydevice.partitions[0].whole_device = true
    mydevice.partitions[0].filesystem   = FS_UNALLOCATED

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood c01106c54e Make resize of whole disk file systems work (#743181)
Only allow resizing, not moving of a whole disk device file system.
There is no actual partition to move and moving a file system away from
the start of a disk only makes it unrecognisable.

Also don't perform the partition resize step as there's no actual
partition to be resized.  Only the file system is being resized.
(Libparted actually allows the virtual partition spanning a whole disk
device to be resized, implementing it as a no-operation, but only for
recognised file systems.  For unrecognised file systems it fails with
"unrecognised disk label").

Note that the existing resize dialog was designed for resizing partition
boundaries, and their contained file systems, not for resizing file
systems within a fixed boundary.  The difference is noticeable when
there is unallocated space because the file system doesn't fill the
whole disk device.  The dialog starts resizing a virtual partition the
size of the whole disk device, not the actual size of the file system.
Leave addressing this for a possible future update.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 85a6c88eee Make copy into existing whole disk device file systems work (#743181)
First, copying into a whole disk device fails on the set partition type
step.  Fails with either libparted error "The flag 'lvm' is not
available for loop disk labels" or "unrecognised disk label" depending
whether libparted recognised the content and created a virtual partition
or not.  (This is with libparted 2.4).

Fix by just skipping setting the partition type on whole disk devices.

Second, if any file system specific tools are used during the copy, they
will fail because they are passed the device name as "copy of /dev/SRC"
instead of "/dev/DST".  Occurs when either the destination whole disk
device is not an identical size to the source so the file system check
and grow steps are added, or when file system specific tools are used to
copy the file system as with XFS or recent EXT2/3/4 tools.

Fix by re-adding the real partition path from libparted for whole disk
devices, as is already done for partitioned device names in
GParted_Core::calibrate_partition().

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood fb9653fd8e Force creation of reiserfs on whole disk devices (#743181)
Creation of reiserfs file system fails in GParted with the this error.

    # mkreiserfs -f --label "" /dev/sdb < /dev/null
    mkreiserfs 3.6.24

    /dev/sdb is entire device, not just one partition!
    Continue  (y/n):
    # echo $?
    1

Add second force flag, -f, to the mkreiserfs command to make it work.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Phillip Susi 70e17e9388 Force creation of ext2/3/4 and ntfs on whole disk devices (#683643)
Creation of ext2/3/4 and ntfs file systems fails in GParted on whole
disk devices with these errors.

    # mkfs.ext4 -L "" /dev/sdb < /dev/null
    mke2fs 1.42.9 (4-Feb-2014)
    /dev/sdb is entire device, not just one partition!
    Proceed anyway? (y,n)
    # echo $?
    1

    # mkntfs -Q -v -L "" /dev/sdc
    /dev/sdc is entire device, not just one partition.
    Refusing to make a filesystem here!
    # echo $?
    1

Add force flag, -F, to both mkfs commands to make them work.

Bug 683643 - Doesn't properly support partitionless drives.
2015-03-09 11:04:13 -06:00
Mike Fleetwood db8d964fba Make format work with whole disk devices (#743181)
Formatting a whole disk device fails on the set partition type step with
libparted error "unrecognised disk label".  This is because the previous
step just cleared the old file system signatures leaving libparted with
nothing to recognise.  Therefore libparted doesn't present a virtual
"loop" partition table.

As there is no partition table, there's no partition and no partition
type.  Just skip setting the partition type on whole disk devices.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood dc4e69136c Make clearing signatures work with whole disk devices (#743181)
This enables Format to Cleared operation to succeed on whole disk device
file systems even when libparted doesn't recognise the file system.

(Turns out that making calibrate work in the previous commit happened to
make Format to Cleared operation succeed, but only if libparted
recognised the file system on the whole disk device).

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 84eb35b7eb Report either partition or device in operational results (#743181)
In the operational results of the calibrate step include the type of the
path GParted is working with, either partition or whole disk device.  Do
the same for the create empty partition step too for consistency, even
though it only ever creates partitions.  Looks like:

    create empty partition
      path: /dev/sdb3 (partition)
      start: 2099200
      end: 4196351
      size: 2097152 (1.00 GiB)

    calibrate /dev/sdc
      path: /dev/sdc (device)
      start: 0
      end: 1953525167
      size: 1953525168 (931.51 GiB)

Makes it explicit to the users what GParted has detected.  Helps the
developers when looking at saved results to understand what decisions
were made and why specific steps were performed or not.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood c4229c99f8 Make calibrate work with whole disk devices (#743181)
This enables the Check, Label and New UUID operations to succeed on
whole disk device file systems even when libparted doesn't recognise the
file system.

This benefits reiser4 and lvm2 pv file systems with all versions of
libparted, current version is 3.2, and for nilfs2 with libparted < 2.4.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 51ac4d5648 Split get_device_and_disk() into two (#743181)
get_device_and_disk() basically calls libparted to get a PedDevice
object representing a disk device and a PedDisk object representing a
partition table.  Re-implement get_device_and_disk() using two separate
functions, get_device() and get_disk(), to get one of these objects
each.

No functionality changes with this commit.  It enables future commits to
incrementally add support for whole disk devices into GParted without
needing libparted to recognise the contents and create a virtual "loop"
partition table.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood b293a464cb Document workings of calibrate_partition() method
Document the reasons why GParted_Core::calibrate_partition() needs to
reload the device name and partition boundaries from libparted.
2015-03-09 11:04:13 -06:00
Mike Fleetwood 6c333f845c Enable operations on whole disk device virtual partitions (#743181)
Enable operations on whole disk devices containing any recognised file
system.

The new partition operation on an empty whole disk device continues to
display the "No partition table found on device /dev/DEVICE" information
dialog.

Specifically unsupported operations:

 *  Delete -

    Deletion of a partition only involves removal of the entry in the
    partition table leaving the file system intact on the disk.  However
    this doesn't work for a whole disk device file system.  Instead the
    file system signatures would have to be erased which is much more
    destructive and virtually impossible to undo.  Therefore don't
    implement whole disk device file system deletion.  Alternatives are
    to format the file system to cleared or create a partition table on
    the device.  Both of these imply overwriting the existing data and
    set the expectation that undo is not possible.

 *  Manage flags -

    There's no partition table, so there's no partition, so there's no
    flags.

Resize/Move operation is being supported so that a whole disk device
file system can be resized to handle devices which can be resize, such
as those from SANs or Linux Software RAID arrays.  The start of the file
system must remain fixed so move won't be allowed.

So far only simple operations work if they don't need libparted support
at all [1], or only need libparted support for the calibrate step AND
the file system on the whole disk device is recognised by libparted [2].
(Needs libparted to provide a "loop" partition, hence the recognition
requirement, so that the calibrate step can successfully read the
virtual "loop" partition table.  Doesn't matter whether it's an old
version of libparted and it gets the name of the device wrong as GParted
is already using the whole disk device name anyway).

[1] Operations not needing any libparted support:
    Mount on, Unmount, Swapon, Swapoff, Activate and Deactivate

[2] Operations only needing libparted support for the calibrate step:
    Check, Label, New UUID

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood 5098744f9a Add whole_device flag to the partition object (#743181)
Need to be able to take different actions in the GParted_Core partition
manipulation methods and in Win_GParted UI methods to deal with
libparted supported partitions or whole disk devices without a partition
table.  Add boolean whole_device to the partition object and set
appropriately to allow for this.

Bug 743181 - Add unpartitioned drive read-write support
2015-03-09 11:04:13 -06:00
Mike Fleetwood e33bb8d688 Display failing activate/deactivate commands in error dialogs
As was done with a failed mount operation, include the failed activate/
deactivate command in the error dialog.  Two example error dialogs now
look like this:

    (-) Could not deactivate swap

        # swapoff -v /dev/sdb7
        swapoff: /dev/sdb7: swapoff failed: Invalid argument

                                                    [  OK  ]

    (-) Could not unmount /dev/sdb6

        # umount -v "/mnt/6"
        umount: /mnt/6: not mounted

                           [  OK  ]
2015-03-09 10:50:40 -06:00
Mike Fleetwood 8d9c6f197d Second, try mounting file systems specifying the type (#742741)
On RHEL/CentOS 6, GParted fails to mount nilfs2 file system like this:

    # mkfs.nilfs2 /dev/sdb1
    # mount /dev/sdb1 /mnt/1
    mount: you must specify the filesystem type

This fails because mount internally uses libblkid to determine the file
system type when it is not specified on the command line.  However on
RHEL/CentOS 6 libblkid is too old to recognise nilfs2.

GParted used libparted recognition first and blkid second.  Mount only
uses libblkid.  When there are multiple signatures on a partition
GParted may report a different result to blkid alone.  Therefore fix by
first trying to mount the file system without specifying the type, as is
already done, and if that fails, trying specifying the file system type.
This allows GParted to mount nilfs2 file systems.

    # mount -t nilfs2 /dev/sdb1 /mnt/1
    # mount | fgrep sdb1
    /dev/sdb1 on /mnt/1 type nilfs2 (rw,gcpid=30946)

And for unsupported file systems the error dialog from the failed mount
command shows both commands like this:

    (-) Could not mount /dev/sdb3 on /mnt/3

        # mount -v /dev/sdb3 "/mnt/3"
        mount: unknown filesystem type 'reiser4'

        # mount -v -t reiser4 /dev/sdb3 "/mnt/3"
        mount: unknown filesystem type 'reiser4'

                                         [  OK  ]

Bug 742741 - Nilfs2 file system is unusable on RHEL/CentOS 6
2015-03-09 10:50:40 -06:00
Mike Fleetwood 0bdc1fef14 Add lookup for Linux kernel names of mountable file systems (#742741)
Function Utils::get_filesystem_kernel_name() returns the name of the
file system as needed for use in the mount command:
    mount -t TYPE DEVICE DIR

Needed because the kernel / mount name is 'hfsplus' where as libparted /
GParted, as reported by Utils::get_filesystem_string(), calls it 'hfs+'.
So far just added debugging when mounting a file system to test the
function works.

    # ./gartedbin
    ======================
    libparted : 2.1
    ======================
    DEBUG: (hfsplus) # mount -v /dev/sdb5 "/mnt/5"
    DEBUG: (nilfs2) # mount -v /dev/sdb1 "/mnt/1"

Bug 742741 - Nilfs2 file system is unusable on RHEL/CentOS 6
2015-03-09 10:50:40 -06:00
Mike Fleetwood 30086b2e80 Include failing mount command in the error dialog (#742741)
On RHEL/CentOS 6 GParted fails to mount nilfs2 file system.  Include the
failing file system mount command in the error dialog so the user knows
what command failed.  The error dialog now looks like:

    (-) Could not mount /dev/sdc1 on /mnt/1

        # mount -v /dev/sdc1 "/mnt/1"
        mount: you must specify the filesystem type

                                           [  OK  ]

Also stop telling the dialog that the secondary text contains pango
markup as the command line and error message certainly isn't pango
markup text.

Bug 742741 - Nilfs2 file system is unusable on RHEL/CentOS 6
2015-03-09 10:50:40 -06:00
Mike Fleetwood 53fb18096d Add GParted internal detection of nilfs2 (#742741)
GParted can create a nilfs2 file system on RHEL/CentOS 6, yet both
libparted and blkid are too old to recognise it when created.  Add
GParted internal detection of nilfs2 file systems.

Easy way to get the required signature needed to recognise nilfs2 is
using wipefs to report the signature bytes it erases, using a more up to
date distribution.  So using CentOS 7:

    # mkfs.nilfs2 /dev/sdb1
    # wipefs -a /dev/sdb1
    /dev/sdb1: 2 bytes were erased at offset 0x00000406 (nilfs2): 34 34

Also from util-linux source libblkid/src/superblocks/nilfs2.c
    #define NILFS_SB_MAGIC 0x3434

Bug 742741 - Nilfs2 file system is unusable on RHEL/CentOS 6
2015-03-09 10:50:39 -06:00
Mike Fleetwood f5c044142c Display libparted "loop" partition tables via "none" instead (#741430)
For libparted recognised file systems covering the whole disk device,
libparted reports the partition table as "loop".  With libparted 3.1 and
earlier, the partition device was reported wrongly as /dev/sdb1, instead
of /dev/sdb, by the ped_partition_get_path() call.  This causes GParted
to fail to report file system usage, label and UUID, and busy status.
Also multiple file system tool command errors from using the wrong
device are displayed.

    # mkfs.ext4 -L whole-ext4 /dev/sdb
    # ./gpartedbin

Example partition errors:

    e2label: No such file or directory while trying to open /dev/sdb1
    Couldn't find valid filesystem superblock.

    tune2fs 1.42.8 (20-Jun-2013)
    tune2fs: No such file or directory while trying to open /dev/sdb1
    Couldn't find valid filesystem superblock.

    dumpe2fs 1.42.8 (20-Jun-2013)
    dumpe2fs: No such file or directory while trying to open /dev/sdb1
    Couldn't find valid filesystem superblock.

    Unable to read the contents of this file system!
    Because of this some operations may be unavailable.
    This cause might be a missing software package.
    The following list of software packages is required for ext4 file
    system support: e2fsprogs v1.41+.

This also breaks most of the manipulation operations, again because it
is using the wrong device name.

Fix by ignoring libparted's "loop" partition table and just let it get
displayed via the "none" partition table mechanism.

Doing this renders the direction taken in bug 683643 to make GParted
work with libparted "loop" partition tables mute, as GParted no longer
works with such partition tables.  Instead they are replaced by "none"
partition tables within GParted.

    Bug 683643 - Doesn't properly support partitionless drives
    https://bugzilla.gnome.org/show_bug.cgi?id=683643

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood 474c5b10b3 Set activate/deactivate partition menu item name earlier (#741430)
So that, even though the menu item is greyed out, it is correctly named
Activate or Deactivate for an LVM2 Physical Volume on a whole disk
device, rather than the default Mount or Unmount.

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood dd049e8b4b Disable manipulation operations on "none" partition tables (#741430)
At this time we are providing read-only support for file systems on
whole disk devices.  Also all manipulation operations fail at the
calibrate step with libparted error "unrecognised disk label", and
manage flags is for real partitions only.

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood cbf8bd741d Display whole disk file systems via partition table "none" (#741430)
For file systems which libparted recognises, when found on the whole
disk device, it reports with partition table "loop" and a partition
covering the whole disk.  GParted duly displays this to the user.

For file systems which libparted doesn't recognise it reports
"unrecognised disk label".  As of the latest libparted 3.2, these file
system aren't recognised and can't currently be shown when on the whole
disk device:

    BitLocker, Crypt LUKS, exFAT, F2FS, LVM2 Physical Volume,
    Linux Software RAID, ReFS, Reiser 4

So only when libparted doesn't recognise a file system on the whole disk
device and GParted does, either via blkid or it's internal code, display
this with partition table "none".

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood ed827cf59a Create helper function set_partition_label_and_uuid() (#741430)
Move code which queries the file system label and UUID of a partition
into a separate helper function.

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood 005a842f25 Support querying whole disk devices for signatures (#741430)
Make GParted_Core::get_filesystem() capable of recognising file system
signatures on whole disk devices.  Pass NULL for lp_partition to make it
read signatures from the whole disk rather than a specific partition.
Add debugging to test it works.

    # fgrep sdb /proc/partitions
       8    16    8388608 sdb
    # lvm pvcreate /dev/sdb
     Writing physical volume data to disk "/dev/sdb"
     Physical volume "/dev/sdb" successfully created
    # ./gpartedbin
    ======================
    libparted : 1.8.1
    ======================
    Unable to open /dev/sdb - unrecognised disk label.
    DEBUG: /dev/sdb (lvm2 pv)

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood a9f08ddc7d Rename local variable to fsname in get_filesystem() (#741430)
Used 'fstype' as the name of local variable storing the FILESYSTEM
enumeration type.

'fs_type' was the name of the local variable storing the string with the
name of the file system type.  Rename it to 'fsname' to distinguish it
from the aforementioned variable.

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood cb645b16cf Refactor GParted internal file system signature detection (#741430)
Refactor GParted internal file system signature detection to remove code
duplication.  There were 5 separate copies of code to: allocate a
buffer, open, read and close the device, free the buffer and compare the
signature.

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood bd9c76f602 Reorder crypt-luks detection (#741430)
(1) Add recognition of LUKS signatures detected by blkid command, via
the FS_Info cache.

(2) Relocate internal GParted detection of LUKS signature to back after
libparted and blkid identification.  This reverses previous commit:

    7695593d21
    Improve crypt-luks detection (#578300)

Bug 578300 must have been caused by either: (1) reading stale data from
the whole disk device, or (2) formatting as LUKS over the top of ext3
leaving old signatures behind.  Case (1) has been resolved by commit:

    3bea067596
    Flush devices when scanning to prevent reading stale signatures
    (#723842)

Re-tested case (2) and find that no old signatures are left behind:

    # mkfs.ext3 /dev/sda8
    # wipefs /dev/sda8
    offset               type
    ----------------------------------------------------------------
    0x438                ext3   [filesystem]
                         UUID:  756dcd5f-73c5-4881-81ee-dc88e8b18f42
    # cryptsetup luksFormat /dev/sda8
    # wipefs /dev/sda8
    offset               type
    ----------------------------------------------------------------
    0x0                  crypto_LUKS   [crypto]
                         UUID:  de15ed1a-f461-46b7-803b-d0fd3b4bd680

Bug 741430 - GParted cannot recognise LVM signature on unpartitioned
             drive
2015-03-09 10:17:22 -06:00
Mike Fleetwood 70cc6187d5 Fix double quote error creating XFS file systems (#744108)
Trying to create an XFS file system causes the following error and
the create new xfs file system step to never complete.

    # ./gpartedbin
    ======================
    libparted : 2.4
    ======================

    (gpartedbin:15572): glibmm-CRITICAL **:
    unhandled exception (type Glib::Error) in signal handler:
    domain: g-shell-error-quark
    code  : 0
    what  : Text ended before matching quote was found for ". (The text was 'mkfs.xfs -f -L "mylabel" " /dev/sdb8')

Creation of XFS file systems was broken by the extra stray double quote
added to the command before the device name, by commit:

    63aeb150ac
    Rename member variables and methods in Partition class (#741424)

Bug 744108 - Creating an XFS file system never completes
2015-02-07 10:33:12 -07:00
Mike Fleetwood 9294a10fbd Correct dialog title displaying libparted exceptions (#743825)
Encountered this dialog:

    Libparted Bug Found!

    (-) Invalid argument during seek for read on /dev/md1

                          [ Retry ] [ Cancel ] [ Ignore ]

After investigation found that GParted was asking libparted to read from
sector -1.  Libparted was actually reporting an error but GParted was
incorrectly displaying this as a bug.  This was because setting the
dialog title was falling through from the error case to the bug case.
Fix this.

Also provide unique titles for all libparted exception types so that
they can all be distinguished.

 *  List of exceptions from libparted: enum _PedExceptionType
    http://www.gnu.org/software/parted/api/group__PedException.html

Bug 743825 - gparted displaying dialog: libparted bug found!
2015-02-03 09:45:55 -07:00
Curtis Gedak ed9d537227 Add Mike Fleetwood to menu_help_about 2015-02-01 13:14:05 -07:00
Curtis Gedak 44eaabc57c Provide credit for patch by Michael Zimmermann
Provide credit for patch to add support for GPT partition names.
Also indicate significant effort by Sinlu Bes while we tried different
ideas for how GPT partition name support should work.
2015-02-01 11:01:41 -07:00
Michael Zimmermann 1f5841b4ad Add support for GPT partition names (#741424)
Embedded devices (Android) use GPT partition names to identify
partitions, instead of file system labels.  Add support for viewing and
changing them.

As partition names are used to provide unique identification they are
never copied when copying the contents of one partition to another.

Note that GNU/Linux uses file system labels, UUIDs or device names for
identification during the boot process and afterwards so while partition
names can be used, they are optional and purely for user information.

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood b278782ef6 Remove redundant second if condition in Display_Info()
Remove second inner if condition performing the same check as the outer
if:
    partition.type != GParted::TYPE_UNALLOCATED
2015-02-01 10:08:23 -07:00
Mike Fleetwood e1dc89cd11 Rename class to Dialog_FileSystem_Label (#741424)
class Dialog_Partition_Label   -> Dialog_FileSystem_Label
file Dialog_Partition_Label.h  -> Dialog_FileSystem_Label.h
file Dialog_Partition_Label.cc -> Dialog_FileSystem_Label.cc

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood 04450c577c Rename class to OperationLabelFileSystem (#741424)
class OperationLabelPartition   -> OperationLabelFileSystem
file OperationLabelPartition.h  -> OperationLabelFileSystem.h
file OperationLabelPartition.cc -> OperationLabelFileSystem.cc

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood 3630b9c83b Rename methods in GParted_Core and Win_GParted classes (#741424)
class GParted_Core:
    label_partition() -> label_filesystem()

class Win_GParted:
    allow_label_partition()    -> allow_label_filesystem()
    activate_label_partition() -> activate_label_filesystem()

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood 63aeb150ac Rename member variables and methods in Partition class (#741424)
class Partition:
    have_label    -> have_filesystem_label
    label         -> filesystem_label
    label_known() -> filesystem_label_known()
    get_label()   -> get_filesystem_label()
    set_label()   -> set_filesystem_label()

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood d480800600 Rename enum to OPERATION_LABEL_FILESYSTEM (#741424)
This and the following few commits rename variables, methods, classes,
etc from *label_partition* to *label_filesystem* so that the code also
reflects that it is the label of the file system that is being modified
and to separate it from the name partition operation about to be added.

enum OPERATION_LABEL_PARTITION -> OPERATION_LABEL_FILESYSTEM

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood 332a2e7a79 Rename Partition Label to File System Label in the GUI (#741424)
Rename the Partition Label concept to File System Label throughout the
GUI.  This is to better reflect that it is the label of the file system
that is being modified and to separate it from the Partition Name
concept about to be added.

Bug 741424 - Add support for GPT partition names
2015-02-01 10:08:23 -07:00
Mike Fleetwood 8b45219a2d Make swap usage match figures reported by swapon -s (#742647)
GParted reports used figure of active swap space from 4 KiB upwards,
actually 1 page, where as 'swapon -s' reports figures from 0 upwards.
This is because GParted is counting the 1 page swap space overhead as
used space.

    # sfdisk -s /dev/sdb1
    262144
    # mkswap /dev/sdb1
    # swapon /dev/sdb1
    # swapon -s
    Filename                Type            Size    Used    Priority
    /dev/sdb1               partition       262140  0       -1

For this case GParted reports used as 4 KiB.
(ptn size - swap size = 262144 - 262140 = 4)

Instead make GParted report used figure without any overhead from 0
upwards, matching what 'swapon -s' reports.

Bug 742647 - Swap usage doesn't match figures reported by swapon -s
2015-01-28 11:38:53 -07:00
Curtis Gedak 570680826d Update copyright year 2015-01-26 09:48:36 -07:00
Phillip Susi 8a952cd4a9 Fix off by one sector error in GParted internal block copy (#742920)
GParted's internal block copy has an off by one sector bug when the
source is before the destination; and the copy is performed backwards
from high block to low block.  It is as though the source and
destination partitions were both one sector earlier on the disk.

In ASCII art it it looks like this:

Initial layout:      x<--SRC--><--DST-->
Actually wrote:               x<--SRC--
Should have written:           <--SRC-->

Affects moving partitions too.

This bug has existed since commit:

    bd9e16f22f
    Thread the internal copy algorithm (#685740)

Effectively the last sector of the partition is missed and one sector
before the start of the partition is corrupted.  Most of the time file
systems don't get around to using the very last sector so didn't notice.

Bug 742920 - GParted internal copy is overstepping partition boundaries
2015-01-16 11:31:14 +00:00
Mike Fleetwood be8cc9c084 Allow creation of reiser4 file systems on Linux 3.x (#742511)
With reiser4progs 1.0.7 and earlier mkfs specifically checks for Linux
kernel 2.5 or 2.6.  On Linux 3.x it fails with the following warning:

    # mkfs.reiser4 --yes --label "" /dev/sdb1
    Warn : Linux 3.13.0-39-generic is detected. Reiser4 does not support
    such a platform. Use -f to force over.
    # echo $?
    8

This is fixed in reiser4progs 1.0.8 and later.  From the reiser4progs
SourceForce README.txt:

    . Do not ask for confirmation to work under Linux-3.X;

Found on Ubuntu 12.04 LTS and Ubuntu 14.04 LTS both with reiser4progs
1.0.7 and a Linux 3.x kernel.

Fix by adding "--force" flag to the mkfs command line.

Bug 742511 - Creation of reiser4 file systems fails with unsupported
             warning
2015-01-07 08:44:26 -07:00
Mike Fleetwood 95ff4fbdc9 Refactor resize() to make it even easier to understand (#741211)
Split the resizing file system and resizing partition calls in
GParted_Core::resize() into separate grow and shrink code paths.

Note that this also changes the degenerative case of calling resize()
when the partition isn't changing size, for non-swap partitions, from a
file system check step to a successful no-op.  This doesn't matter as my
testing never found resize() to be called when the partition isn't
changing size.

Also correct spelling of local variable success.

Bug 741211 - Remove unnecessary duplicate actions when resizing a
             partition
2014-12-13 10:46:35 -07:00
Mike Fleetwood 99f770ddb0 Stop recreating linux-swap twice when shrinking a partition (#741211)
Shrinking swap partition operation performs these steps:

    Shrink /dev/sdb2 from 2.00 GiB to 1.00 GiB
    + calibrate /dev/sdb2
    + check file system on /dev/sdb2 for errors and (if possible) fix them
        checking is not available for this file system
    + shrink file system
      + create new linux-swap file system
        + mkswap -L "test-swap" -U "bd381eba-5df7-42e2-8e0e-411e9701c995" /dev/sdb2
    + shrink partition from 2.00 GiB to 1.00 GiB
      + create new linux-swap file system
        + mkswap -L "test-swap" -U "bd381eba-5df7-42e2-8e0e-411e9701c995" /dev/sdb2

Resizing a linux-swap partition was partially special cased in
GParted_Core::resize().  Make it fully special cased so that it just
does the following steps.  No more skipped file system checks or extra
resizing.

    1)  Resize partition,
    2)  Recreate linux-swap.

This existing call chain recreates the linux-swap:
    GParted_Core::resize_filesystem()
        linux_swap::resize()

A compound move and resize operation still performs unnecessary checks
and recreates of linux-swap, but less than before.

Bug 741211 - Remove unnecessary duplicate actions when resizing a
             partition
2014-12-11 11:10:09 -07:00
Mike Fleetwood b4acb14ff1 Remove second file system check when growing a partition (#741211)
Shrinking a partition performs a single file system check step, but
growing a partition still performs two file system checks.

    Grow /dev/sda8 from 1.00 GiB to 10.00 GiB
    + calibrate /dev/sda8
    + check file system on /dev/sda8 for errors and (if possible) fix them
    + grow partition from 1.00 GiB to 10.00 GiB
    + check file system on /dev/sda8 for errors and (if possible) fix them
    + grow file system to fill the partition

This is a leftover from the early days of GParted when resizing a
partition used cylinder buffering and performed three file system
checks.  See prior to commit:

    d663c3c277
    removed cylindersize buffering during resize from the filesystems.

Remove the second file system check when growing a partition.

Bug 741211 - Remove unnecessary duplicate actions when resizing a
             partition
2014-12-11 11:10:09 -07:00
Mike Fleetwood 6220a35dac Rework population of the create new partition file system list
Simplify how the list of file system types is populated in the Create
New Partition dialog.  Change from copying everything and removing
unwanted items to only copying required items.  Makes the code simpler
and therefore easier to understand.
2014-10-30 09:55:01 -06:00
Mike Fleetwood 3373ef07fa Recognise ReFS file system (#738471)
Only recognises ReFS file system.  No other actions are supported.
Requires blkid from util-linux >= 2.24.

Bug #738471 - ReFS file system is not recognised
2014-10-30 09:55:01 -06:00
Mike Fleetwood 88f67058e8 Use supported_filesystem() predicate (#738471)
Use GParted_Core::supported_filesystem() to remove the need to
explicitly list the growing number of recognised, but otherwise
unsupported, file system signatures in multiple places.

Bug #738471 - ReFS file system is not recognised
2014-10-30 09:55:01 -06:00
Mike Fleetwood 8b4b73a8f3 Add supported_filesystem() predicate method (#738471)
Helper to check whether a recognised file system type is supported by
GParted or not.  Supported means there is an implementation class and
will appear in the File System Support dialog.

Make supported_filesystem() a static member function so that it can be
called without a class object so that GParted_Core::GParted_Core()
initialiser isn't called multiple times.  This requires FILESYSTEM_MAP
to become a static member variable too.

Bug #738471 - ReFS file system is not recognised
2014-10-30 09:55:01 -06:00
Mike Fleetwood e3a1b93a6d Pass by value to get_filesystem_object()
get_filesystem_object() takes a constant reference to a FILESYSTEM, but
FILESYSTEM is just an enumeration.  So that's a pointer to a constant
int.  Just pass by value instead.
2014-10-30 09:55:01 -06:00
Mike Fleetwood f672f68863 Check for e4fsprogs commands for ext4 support on RHEL/CentOS 5.x (#738706)
RHEL / CentOS 5.6 and later officially support ext4 file system [1].
From RHEL / CentOS 5.3 ext4 file system was included as a technology
preview.  Ext4 file system tools are in a separate package e4fsprogs,
using uniquely named commands.  The standard e2fsprogs commands only
support ext2 and ext3 file systems.

    # mkfs.ext4 /dev/sdb3
    # tune2fs -l /dev/sdb3
    tune2fs 1.39 (29-May-2006)
    tune2fs: Filesystem has unsupported feature(s) while trying to open /dev/sdb3
    Couldn't find valid filesystem superblock.
    # echo $?
    1
    # tune4fs -l /dev/sdb3
    tune4fs 1.41.12 (17-May-2010)
    Filesystem volume name:   <none>
    Last mounted on:          <not available>
    Filesystem UUID:          ba4a9d58-7728-4b47-8a90-80e772615637
    Filesystem magic number:  0xEF53
    Filesystem revision #:    1 (dynamic)
    Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
    ...

For ext4 only, search for the e4fsprogs specific commands first and the
standard e2fsprogs commands second.

[1] RHEL 5.6 Release Notes, 5. Filesystems and Storage
https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/5.6_Release_Notes/ar01s05.html

Bug #738706 - GParted doesn't support ext4 on RHEL/CentOS 5.x
2014-10-27 09:32:36 -06:00
Mike Fleetwood 0fb8cce699 Reduce flashing redraw from automatic partition selection (#696149)
Automatic selection of the largest unallocated partition caused flashing
redraw of the partition graphic and partition list.  Both the partition
graphic and partition list were being drawn blank then redrawn fully
populated.  This only happened on some distributions including:
CentOS 5.10, 6.5, 7.0, Debian 6, Fedora 14, 20, Xubuntu 14.04 LTS.  Did
not happen on: Kubuntu 12.04 LTS.

This is a workaround, not a complete fix.  It moves automatic selection
of the largest unallocated partition to after processing of the GTK
Event loop in Refresh_Visual() which redraws the partition graphic and
partition list.  These visuals are now drawn only once, fully populated,
however this draws them without the selected partition.  The partition
selection is then drawn afterwards which causes the selection to flash
instead.  This is significant improvement to the whole partition graphic
and partition list flashing when redrawn.

Bug #696149 - Double refresh of display introduced with default
              unallocated space
2014-10-07 10:38:27 -06:00
Mike Fleetwood 52ee26f971 Prevent flashing redraw of the devices combobox (#696149)
The device combobox was getting drawn blank, then getting drawn again
with the selected device.  This was happening because at the start of
Win_GParted::refresh_combo_devices() the GTK model behind the combobox,
liststore_devices, was cleared, changing the active item, causing the
combobox to get redrawn empty.  After the GTK model had been repopulated
the active item was reset causing the comboxbox to get redrawn again,
now showing the selected device.  Call flow:

    Win_GParted::refresh_combo_devices()
        liststore_devices->clear()
            //Gtk::Combobox emits signal_change.  Registered callbacks
            //called.
                Win_GParted::combo_devices_changed()
                    Win_GParted::Refresh_Visual()
                        ...
        ...
        combo_devices.set_active(current_device);
            //Gtk::Combobox emits signal_change.  Registered callbacks
            //called.
                Win_GParted::combo_devices_changed()
                    Win_GParted::Refresh_Visual()
                        ...

This has always been the case, since the device combobox was first added
to GParted before version 0.1 by commit:

    3a4b43e0ad
    replaced deprecated OptionMenu with ComboBox ...

Fix by temporarily blocking the devices comboxbox from emitting
signal_changed while the GTK model behind the combobox is recreated.

However, since automatic selection of the largest free space was added
[1] in GParted 0.15.0, a more noticeable flashing redraw issue was
caused in which the partition graphic and partition list were both drawn
blank then redrawn fully populated.  Some distributions were not
affected by this at all, some only experienced a single flash and others
suffered from two or more flashing redraws.  Some affected
distributions: CentOS 5.10, 6.5, 7.0, Debian 6, Fedora 14, 19, 20,
Ubuntu 13.10, Xubuntu 14.04 LTS.  Did not occur on Kubuntu 12.04 LTS.

[1] 5b53c12f6e
    Select largest unallocated partition by default (#667365)

Bug #696149 - Double refresh of display introduced with default
              unallocated space
2014-10-07 10:38:27 -06:00
Mike Fleetwood e8533c39c2 Prevent automake warning about deprecated INCLUDES (#735742)
Automake 1.13 and later generates the following warning when building
GParted from git:

    $ ./autogen.sh
    ...
    checking for automake >= 1.9...
      testing automake-1.13... found 1.13.4
    ...
    Running automake-1.13...
    src/Makefile.am:1: warning: 'INCLUDES' is the old name for 'AM_CPPFLAGS' (or '*_CPPFLAGS')
    ...

INCLUDES name has been depreciated since automake 1.7.  GParted's
autogen.sh and configure.ac require at least automake 1.9.  Therefore
replace automake variable INCLUDES with AM_CPPFLAGS.

Bug #735742 - automake warns INCLUDES is deprecated
2014-08-31 11:19:57 -06:00
Mike Fleetwood 20158f0440 Tidyup Autoconf check for --disable-doc (#734076)
Use AC_ARG_ENABLE() and AS_HELP_STRING() macros to improve handling of
the --disable-doc option.

Change the names, and sence, of the Autoconf and Automake definitions
used to control building of the help documentation.  Reasons are:
(1) Switch from negative to positive statements which are more natural
    to comprehend.
(2) Insert HELP in the names from *_DOC to *_HELP_DOC as they control
    building of the documentation in the help subdirectory, not the
    manual page in the doc directory.

Autoconf: HAVE_DISABLE_DOC -> ENABLE_HELP_DOC
Automake: DISABLE_DOC      -> BUILD_HELP_DOC

Bug #734076 - Autodetect parted online partition resizing capability
2014-08-31 11:04:36 -06:00
Mike Fleetwood 3030118caf Pass link libraries via LIBS variable into Makefiles (#734718)
Currently -lparted is passed to the linker by being hard coded in the
gpartedbin_LDFLAGS variable in src/Makefile.am and -lparted-fs-resize
by conditionally being appended to the gpartedbin_LDFLAGS.

Add an Autoconf AC_CHECK_LIB check for the parted library using it's
default action-if-found to extend LIBS.  Update the existing
AC_CHECK_LIB check for the parted-fs-resize library so that the
overridden action-if-found also extends LIBS with -lparted-fs-resize.
These libraries are then assigned via the LIBS variable in the
generated Makefiles.

This matches how the other libraries, dl and uuid, were found in the
configure script and passed to the Makefiles.

Bug #734718 - Update Autoconf version specific libparted checks and
              defines to feature specific ones
2014-08-18 15:48:34 -06:00
Mike Fleetwood ed4ea6cf03 Rename HAVE_LIBPARTED_2_2_0_PLUS define into feature names (#734718)
Remove HAVE_LIBPARTED_2_2_0_PLUS definition

Rename version specific #define HAVE_LIBPARTED_2_2_0_PLUS into two
separate feature specific names set as required:

    Name                                  Set when?

    ENABLE_PT_REREAD_WORKAROUND           (libparted < 2.2)
    USE_LIBPARTED_LARGE_SECTOR_SUPPORT    (libparted >= 2.2)

Using feature specific #defines is the standard Autoconf way and makes
the resultant conditional code easier to understand.  Still have to
check the version of libparted though.

Bug #734718 - Update Autoconf version specific libparted checks and
              defines to feature specific ones
2014-08-18 15:48:34 -06:00
Mike Fleetwood 288c4dbf2e Remove little used HAVE_LIBPARTED_3_0_0_PLUS definition (#734718)
Remove HAVE_LIBPARTED_3_0_0_PLUS definition

The #define only controls whether fat16 and fat32 file systems are moved
by libparted (for versions <= 2.4) or the GParted internal algorithm
(for libparted versions >= 3.0).  See this commit for more details:
    0fda1d011d
    Enable new fs resize library available with parted-3.1 (#668281)

As nearly all other file system are moved using the GParted internal
algorithm, just always use it to move fat16 and fat32 file systems too.

Bug #734718 - Update Autoconf version specific libparted checks and
              defines to feature specific ones
2014-08-18 15:48:34 -06:00
Mike Fleetwood a4f761e290 Update parsing of btrfs filesystem show for multi-device membership (#733601)
This patch changes the reading of the btrfs multi-device membership to
resolve issue 1/2 by ignoring the exit status from the 'btrfs filesystem
show' command and relying on parsing the required information to
determine success or failure.

Bug #733601 - Btrfs: Warnings and missing label with btrfs-progs 3.12
              and 3.14
2014-07-28 10:06:00 -06:00
Mike Fleetwood 422829ebff Update parsing of btrfs filesystem show for file system usage (#733601)
Patch 3/4 - btrfs::read_label()

This patch changes the btrfs file system usage reading code to resolve
issue 1/2 by ignoring the exit status from the 'btrfs filesystem show'
command and relying on parsing the required information to determine
success or failure.

Bug #733601 - Btrfs: Warnings and missing label with btrfs-progs 3.12
              and 3.14
2014-07-28 10:06:00 -06:00
Mike Fleetwood eca732fb0c Update parsing of btrfs filesystem show for the label (#733601)
Issue 2/2 - GParted doesn't show label for mounted btrfs file systems

'btrfs filesystem show /dev/PTN' command is used to query details of a
btrfs file system including reading the file system label.  When the
file system is mounted the label is no longer enclosed in single quotes,
but only when using btrfs-progs v3.12.  This causes GParted to think the
label is blank when the file system is mounted and therefore no longer
display it.

File system label not enclosed in single quotes when mounted:

    # fgrep sdb1 /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,relatime,space_cache 0 0
    # btrfs filesystem show /dev/sdb1
    Label: test1-btrfs  uuid: 1f78fa38-2f85-41d3-9be6-ae0356ae9469
            Total devices 1 FS bytes used 192.00KiB
            devid    1 size 2.00GiB used 240.75MiB path /dev/sdb1

    Btrfs v3.12

File system label enclosed in single quotes when unmounted:

    # umount /dev/sdb1
    # btrfs filesystem show /dev/sdb1
    Label: 'test1-btrfs'  uuid: 1f78fa38-2f85-41d3-9be6-ae0356ae9469
            Total devices 1 FS bytes used 192.00KiB
            devid    1 size 2.00GiB used 240.75MiB path /dev/sdb1

    Btrfs v3.12

Removing the single quotes enclosing the label makes the output
identical to that from the older 'btrfs-show' command.

Fix by using a common parser to extract the label from both the
'btrfs filesystem show' and 'btrfs-show' commands which can read the
label with and without enclosing single quotes.

Patch 2/4 - btrfs::read_label()

This patch changes the btrfs file system label parsing code to resolve
issue 1/2 by ignoring the exit status from the 'btrfs filesystem show'
command and relying on parsing the required information to determine
success or failure.  Issue 2/2 is also resolved as described above.

Bug #733601 - Btrfs: Warnings and missing label with btrfs-progs 3.12
              and 3.14
2014-07-28 10:06:00 -06:00
Mike Fleetwood 82c6265fa5 Update parsing of btrfs filesystem show for the UUID (#733601)
Issue 1/2 - GParted shows warnings for mounted btrfs file systems

'btrfs filesystem show /dev/PTN' command is used to query details of a
btrfs file system.  When the file system is mounted the command reports
failed exit status 1, but only when using btrfs-progs v3.14 and v3.14.1.
This causes GParted to: (1) report warnings from the failed commands for
a mounted btrfs file system, (2) fail to determine file system usage
figures and (3) fail to display the mount point and busy indicator for
non-mounting devices in multi-device btrfs file systems.  The label is
also read using the secondary blkid method via the FS_Info cache.

Failed exit status 1 when the btrfs file system is mounted:

    # fgrep sdb1 /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,seclabel,relatime,space_cache 0 0
    # btrfs filesystem show /dev/sdb1
    Label: 'test1-btrfs'  uuid: 033e6b07-ee6a-4620-a585-8580a2b83275
            Total devices 1 FS bytes used 192.00KiB
            devid    1 size 2.00GiB used 240.75MiB path /dev/sdb1

    Btrfs v3.14.1
    # echo $?
    1

Successful exit status 0 when the btrfs file system is unmounted:

    # umount /dev/sdb1
    # btrfs filesystem show /dev/sdb1
    Label: 'test1-btrfs'  uuid: 033e6b07-ee6a-4620-a585-8580a2b83275
            Total devices 1 FS bytes used 192.00KiB
            devid    1 size 2.00GiB used 240.75MiB path /dev/sdb1

    Btrfs v3.14.1
    # echo $?
    0

Fix by ignoring the exit status of the 'btrfs filesystem show' command
and rely on parsing the required information to determine success or
failure.  The output from the older 'btrfs-show' command is almost
identical so the same code will parse it in all cases.

Patch 1/4 - btrfs::read_uuid()

This patch changes the secondary method used to read the btrfs UUID to
resolve issue 1/2 as described above.

Bug #733601 - Btrfs: Warnings and missing label with btrfs-progs 3.12
              and 3.14
2014-07-28 10:06:00 -06:00
Mike Fleetwood 3bea067596 Flush devices when scanning to prevent reading stale signatures (#723842)
While one partition is busy, reformat another partition from the command
line.  Afterwards parted/libparted still detects the original file
system and GParted shows errors from the file system specific tools
reporting the new file system doesn't exist.  Only limitation is that
the new new file system must be recognised by libparted (or by GParted's
fallback file system signature detection).

Case #1, File system reformatting:

    # parted /dev/sdb print
    Model: ATA SAMSUNG SSD UM41 (scsi)
    Disk /dev/sdb: 8012MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  2149MB  2147MB  primary  ext2
     2      2149MB  4296MB  2147MB  primary  ext2
    # mount | fgrep sdb
    /dev/sdb1 on /mnt/1 type ext2 (rw)

    # mkfs.xfs -f /dev/sdb2
    # blkid /dev/sdb2
    /dev/sdb2: UUID="c31823a2-b81b-46fa-8246-0a59695e4834" TYPE="xfs"
    # parted /dev/sdb print
    Model: ATA SAMSUNG SSD UM41 (scsi)
    Disk /dev/sdb: 8012MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start   End     Size    Type     File system  Flags
     1      1049kB  2149MB  2147MB  primary  ext2
     2      2149MB  4296MB  2147MB  primary  ext2
    # e2label /dev/sdb2
    e2label: Bad magic number in super-block while trying to open /dev/sdb2
    Couldn't find valid filesystem superblock.
    # dumpe2fs /dev/sdb2
    dumpe2fs 1.41.12 (17-May-2010)
    dumpe2fs: Bad magic number in super-block while trying to open /dev/sdb2
    Couldn't find valid filesystem superblock.

Case #2, Removing device from multi-device btrfs:

    # btrfs filesystem show /dev/sdb1
    Label: none  uuid: a05db434-efd5-4e8c-902f-05f89a88b610
            Total devices 2 FS bytes used 156.00KB
            devid    2 size 2.00GB used 512.00MB path /dev/sdb2
            devid    1 size 2.00GB used 240.75MB path /dev/sdb1

    # mount /dev/sdb1 /mnt/1
    # btrfs device delete /dev/sdb2
    # btrfs filesystem show /dev/sdb1
    Label: none  uuid: a05db434-efd5-4e8c-902f-05f89a88b610
            Total devices 1 FS bytes used 92.00KB
            devid    1 size 2.00GB used 714.25MB path /dev/sdb1
    # btrfs filesystem show /dev/sdb2

and GParted reports this error for partition /dev/sdb2:
    Unable to read the contents of this file system!
    Because of this some operations may be unavailable.
    The cause might be a missing software package.
    The following list of software packages is required for btrfs
    file system support:  btrfs-tools.

This is another case of libparted reading from the whole disk device
(/dev/sdb) yet the file system tools use the partition specific block
device (/dev/sdb2), and the Linux buffer cache not providing cache
coherency.  Previous scenario was fixed with:

    797f0b8eeb
    Flush device after wiping a file system (#688882)

This affects libparted 2.0 to 3.1 inclusive and is fixed by:

    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=fb99ba5ebd0dc34204fc9f1014131d5d494805bc
    Revert "linux-commit: do not unnecessarily open partition device nodes"

Fix by calling ped_device_sync() to guarantee cache coherency for each
device during scanning.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 20f52e2866 Display btrfs members in the Partition Information dialog (#723842)
Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 4b63e46a4e Fallback to reading mount command output instead of /etc/mtab (#723842)
With linux 3.5 and later, the device used to mount a btrfs file system
is updated in /proc/mounts when the previous mounting device is removed
from the file system.  Most recent distributions make /etc/mtab a
symbolic link to /proc/mounts.  However some still have /etc/mtab as a
plain file only updated by mount and umount, thus showing the old device
name which is no longer part of the file system.

On Ubuntu 13.10, which has /etc/mtab as a plain file managed by mount
and umount:

    # mkfs.btrfs /dev/sdb1
    # mount /dev/sdb1 /mnt/1
    # btrfs device add /dev/sdb2 /mnt/1
    # btrfs device delete /dev/sdb1 /mnt/1
    # sync
    # btrfs filesystem show /dev/sdb1
    # btrfs filesystem show /dev/sdb2
    Label: none  uuid: e47775a6-e5ad-4fb4-9ea4-1570aa5b4009
            Total devices 2 FS bytes used 28.00KB
            devid    2 size 2.00GB used 272.00MB path /dev/sdb2

    # fgrep btrfs /proc/mounts
    /dev/sdb2 /mnt/1 btrfs rw,relatime,space_cache 0 0
    # ls -l /etc/mtab
    -rw-r--r-- 1 root root 842 Apr 15 19:41 /etc/mtab
    # fgrep btrfs /etc/mtab
    /dev/sdb1 /mnt/1 btrfs rw 0 0

This causes GParted to report /dev/sdb1 as busy and mounted at /mnt/1
when it is no longer mounted.  This effects recent releases of Ubuntu,
13.04, 13.10 and 14.04.

Either /etc/mtab is a symlink and is identical to /proc/mounts or
/etc/mtab is a plain file with wrong information.  Fix by not reading
mounted file systems from /etc/mtab.

However old distributions only contain 'rootfs' and '/dev/root' device
names for the / (root) file system with '/dev/root' being a block device
rather than a symlink to the true device.  For example from CentOS 5.x:

    # fgrep ' / ' /proc/mounts
    rootfs / rootfs rw 0 0
    /dev/root / ext3 rw,data=ordered 0 0
    # ls -l /dev/root
    brw------- 1 root root 8, 3 Jun  4  2013 /dev/root

This prevents identification, and therefore busy detection, of the
device containing the / (root) file system.  Used to read /etc/mtab to
get the root file system device name.

    # fgrep ' / ' /etc/mtab
    /dev/sda3 / ext3 rw 0 0
    # ls -l /dev/sda3
    brw-r----- 1 root disk 8, 3 Jun  4  2013 /dev/sda3

As per commit:

    409096f739
    improved scanning for root mountpoint (/) ...

but, as discussed above, this contains an out of date device name after
the mounting device has been dynamically removed from a multi-device
btrfs, thus identifying the wrong device as busy.  Instead fall back to
reading mounted file systems from the output of the mount command, but
only when required.

    # mount | fgrep ' / '
    /dev/sda3 on / type ext3 (rw)

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood d47783eff8 Add fallback busy detection for btrfs file systems (#723842)
In a number of cases there may be no btrfs device cache entry.  Some of
the reasons why this can occur:
1)  Mounting device removed from btrfs on linux <= 3.4 so old mount
    point in /proc/mounts;
2)  btrfs and btrfs-show commands don't exist;
3)  btrfs or btrfs-show command returned non-zero exit status;
4)  get_cache_entry() failed to parse output from btrfs filesystem show
    or btrfs-show.

Without a valid btrfs device cache entry, busy detection for all member
devices fails.  Search the GParted internal mounted partitions map as
the fallback busy detection method.  This can only determine if the
mounting device is mounted or not, not any of the other members of a
multi-device btrfs file system.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 0e980a47a2 Pass devid when resizing btrfs file systems (#723842)
GParted doesn't specify the devid when resizing a btrfs file system, so
the kernel defaults to resizing devid 1.  On a multi-device btrfs this
may not be the same partition which GParted is resizing.  This will
result in file system truncation and corruption.  Shrinking the wrong
partition example:

    1)  Create a btrfs file system spanning 2 partitions:
            # mkfs.btrfs /dev/sdb1 /dev/sdb2
            # btrfs filesystem show /dev/sdb1
            Label: none  uuid: 41654265-9840-45c4-aca1-55989da358d6
                    Total devices 2 FS bytes used 112.00KiB
                    devid    1 size 2.00GiB used 437.50MiB path /dev/sdb1
                    devid    2 size 2.00GiB used 417.50MiB path /dev/sdb2

    2)  Resize /dev/sdb2 down to 1 GiB using GParted.  This command was
        run:
            btrfs filesystem resize 1048576K /tmp/gparted-ddyGRh
        which resized devid 1 (/dev/sdb1) to 1 GiB:
            # btrfs filesystem show /dev/sdb1
            Label: none  uuid: 41654265-9840-45c4-aca1-55989da358d6
                    Total devices 2 FS bytes used 256.00KiB
                    devid    1 size 1.00GiB used 437.50MiB path /dev/sdb1
                    devid    2 size 2.00GiB used 417.50MiB path /dev/sdb2
        but GParted instead resized /dev/sdb2 to 1 GiB:
            # sfdisk -s /dev/sdb1
            2097152
            # sfdisk -s /dev/sdb2
            1048576

Even on a single device btrfs devid 1 may no longer exist if the file
system has had the initial device removed from it.  Example:

    1)  Create a single btrfs file system, add a second device and
        remove the first:
            # mkfs.btrfs /dev/sdb1
            # mount /dev/sdb1 /mnt/1
            # btrfs device add /dev/sdb2 /mnt/1
            # btrfs device remove /dev/sdb1 /mnt/1
            # umount /mnt/1
            # btrfs filesystem show /dev/sdb2
            Label: none  uuid: 2cbf3ac3-1344-472a-a0c7-1476d23bdc9f
                    Total devices 1 FS bytes used 256.00KiB
                    devid    2 size 2.00GiB used 480.00MiB path /dev/sdb2

    2)  Again resize /dev/sdb2 down to 1 GiB using GParted.  This
        command was run:
            btrfs filesystem resize 1048576K /tmp/gparted-ddyGRh
        but it failed with:
            ERROR: unable to resize 'tmp/gparted-lEyGaY' - No such device
        A more informative error message was written to syslog:
            # tail -1 /var/log/messages
            Mar 12 14:15:01 localhost kernel: btrfs: resizer unable to find device 1

This is with Linux kernel 3.13.5 on Fedora 20, circa March 2014.

Fix by specifying the devid when resizing (part of) a btrfs file system.
Example command specifying devid 2:

    btrfs filesystem resize 2:1048576K /tmp/1

This will always work because it is the kernel which interprets the
devid colon size parameter and has always done so since btrfs was first
added to the kernel in version 2.6.32 [1].

Reference:
[1] linux v2.6.32 fs/btrfs/ioctl.c btrfs_ioctl_resize()
    https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/fs/btrfs/ioctl.c?id=v2.6.32#n578

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 287526681d Add devid to the cache of btrfs device information (#723842)
Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 1712809e01 Display usage for multi-device btrfs file systems (#723842)
Currently GParted fails to report the usage of a multi-device btrfs file
system if it is mounted or if the used space is larger than the size of
an individual member device.  When GParted does display usage figures it
also incorrectly reports the file system wide used figure against every
member device.

Mounted case:
    statvfs() provides an FS size which is larger than any individual
    device so is rejected.  See:
        GParted_Core::mounted_set_used_sectors()
            Utils::get_mounted_filesystem_usage()
            partition .set_sector_usage()

Unmounted case, FS used > device size:
    FS used figure is larger than any individual device so free space is
    calculated as a negative number and rejected.  See:
        btrfs::set_used_sectors()

Btrfs has a volume manager layer within the file system which allows it
to provide multiple levels of data redundancy, RAID levels, and use
multiple devices both of which can be changed while the file system is
mounted.  To achieve this btrfs has to allocate space at two different
level: (1) chunks of 256 MiB or more at the volume manager level; and
(2) extents at the file data level.
References:
*   Btrfs: Working with multiple devices
    https://lwn.net/Articles/577961/
*   Btrfs wiki: Glossary
    https://btrfs.wiki.kernel.org/index.php/Glossary

This makes the question of how much disk space is being used in an
individual device a complicated question to answer.  Further, the
current btrfs tools don't provide the required information.

Btrfs filesystem show only provides space usage information at the chunk
level per device.  At the file extent level only a single figure for the
whole file system is provided.  It also reports size of the data and
metadata being stored, not the larger figure of the amount of space
taken after redundancy is applied.  So it is impossible to answer the
question of how much disk space is being used in an individual device.
Example output:

    Label: none  uuid: 36eb51a2-2927-4c92-820f-b2f0b5cdae50
            Total devices 2 FS bytes used 156.00KB
            devid    2 size 2.00GB used 512.00MB path /dev/sdb2
            devid    1 size 2.00GB used 240.75MB path /dev/sdb1

Fix by guesstimating the per device used figure as the fraction of the
file system wide extent usage based on chunk usage per device.
Calculation:
    ptn fs used = total fs used * devid used / sum devid used

Positives:
1) Per device used figure will correctly be between zero and allocated
   chunk size.

Known inaccuracies:
[for single and multi-device btrfs file systems]
1) Btrfs filesystem show reports file system wide file extent usage
   without considering redundancy applied to that data.  (By default
   btrfs stores two copies of metadata and one copy of data).
2) At minimum size when all data has been consolidated there will be a
   few partly filled chunks of 256 MiB or more for data and metadata of
   each storage profile (RAID level).
[for multi-device btrfs file systems only]
3) Data may be far from evenly distributed between the chunks on
   multiple devices.
4) Extents can be and are relocated to other devices within the file
   system when shrinking a device.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood a086e115e5 Display mount points for multi-device btrfs file systems (#723842)
Linux can only show a single device name in /proc/mounts and /etc/mtab
for each mounted btrfs, even if it is a multi-device file system.  So
GParted only shows a mount point for one of the devices in the btrfs, no
matter how many devices are part of the file system.

    # mkfs.btrfs /dev/sdb1 /dev/sdb2
    # btrfs filesystem show /dev/sdb1
    Label: none  uuid: 36eb51a2-2927-4c92-820f-b2f0b5cdae50
            Total devices 2 FS bytes used 156.00KB
            devid    2 size 2.00GB used 512.00MB path /dev/sdb2
            devid    1 size 2.00GB used 240.75MB path /dev/sdb1
    # mount /dev/sdb1 /mnt/1
    # grep btrfs /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,seclabel,relatime,ssd,space_cache 0 0

GParted only shows the mount point for /dev/sdb1 as /mnt/1, but nothing
for /dev/sdb2.

Make GParted report the same mount point for all devices included in a
multi-device btrfs file system.

Add btrfs specific get_mount_device() method to report the mounting
device, if any, for the btrfs file system in the occupying the device in
question.  Uses the existing cache of btrfs file system device
membership.  Also extract common code from GParted_Core::
set_mountpoints() into set_mountpoints_helper().

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 76e64f2905 Detect busy status of multi-device btrfs file systems (#723842)
Busy detection of file systems works by checking if the device is
mounted (appears in the mount_info map).  For a multi-device btrfs file
system this will only report one of the devices as busy, not all of
them.

    # btrfs filesystem show /dev/sdb1
    Label: none  uuid: 36eb51a2-2927-4c92-820f-b2f0b5cdae50
            Total devices 2 FS bytes used 156.00KB
            devid    2 size 2.00GB used 512.00MB path /dev/sdb2
            devid    1 size 2.00GB used 240.75MB path /dev/sdb1
    # mount /dev/sdb1 /mnt/1
    # grep btrfs /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,seclabel,relatime,ssd,space_cache 0 0

GParted will only report /dev/sdb1 as busy, but not /dev/sdb2.

Add btrfs specific is_busy() method which reports the device as busy if
any of the devices in the btrfs file system are mounted.  This uses a
cache which maps device membership in all btrfs file systems.  The cache
is cleared on GParted refresh and incrementally populated as each btrfs
partition is checked for busy status.

WARNING:
Removal of the mounting device from a btrfs file system makes it
impossible to determine whether the file system is mounted or not for
linux <= 3.4.  This is because /proc/mounts continues to show the old
device which is no longer a member of the file system.

    # btrfs device delete /dev/sdb1 /mnt/1
    # sync
    # grep btrfs /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,seclabel,relatime,ssd,space_cache 0 0
    # btrfs filesystem show /dev/sdb1
    # btrfs filesystem show /dev/sdb2
    Label: none  uuid: 36eb51a2-2927-4c92-820f-b2f0b5cdae50
            Total devices 1 FS bytes used 28.00KB
            devid    2 size 2.00GB used 1.02GB path /dev/sdb2

Fixed in linux 3.5 by commit:
    Btrfs: implement ->show_devname
    https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=9c5085c147989d48dfe74194b48affc23f376650

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood a0c0533e3e Add is_dev_mounted() to expose core partition is mounted test (#723842)
Add static member function GParted_Core::is_dev_mounted() so that other
modules can determine if a particular partition contains a mounted file
system or not.

Make it a static member function so that it can be called without
needing the gparted_core object.  Extend to make the group of
manipulated variables (mount_info, fstab_info) and manipulating
functions (init_maps(), read_mountpoints_from_file(),
read_mountpoints_from_file_swaps(), get_all_mountpoints()) static too.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 49a2e19462 Restore busy detection of unknown mounted file systems (#723842)
Previous commit:
    Make partition busy detection method selectable per file system
    (#723842)
causes GParted to no longer detect an unknown mounted file system as
busy.

This is needed because there are many less popular file systems which
Linux can mount but GParted doesn't recognise, such as: Acorn ADFS,
AFFS, BeFS, BFS, etc.  Encountering an unrecognised mounted device can
also occur with btrfs on Linux 3.4 and earlier when the mounting device
is deleted from a multi-device btrfs file system.  Happens because
/proc/mounts continues to show the original mounting device even after
that device was deleted from the file system.

    # mkfs.btrfs /dev/sdb1
    # mount /dev/sdb1 /mnt/1
    # btrfs device add /dev/sdb2 /mnt/1
    # btrfs device delete /dev/sdb1 /mnt/1
    # btrfs filesystem sync /mnt/1
    # grep btrfs /proc/mounts
    /dev/sdb1 /mnt/1 btrfs rw,seclabel,relatime,ssd,space_cache 0 0
    # blkid /dev/sdb1
    # btrfs filesystem show /dev/sdb1
    # blkid /dev/sdb2
    /dev/sdb2: UUID="9c75647c-217a-4718-bcc7-f3ccd8cc7dc6" UUID_SUB="b5d43630-80d4-42ac-b406-185e97cd5bbe" TYPE="btrfs"
    # btrfs filesystem show /dev/sdb2
    Label: none  uuid: 9c75647c-217a-4718-bcc7-f3ccd8cc7dc6
            Total devices 1 FS bytes used 28.00KB
            devid    2 size 2.00GB used 1.02GB path /dev/sdb2

Reinstate the search for mounted partitions for busy detection even for
unrecognised file system types.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood b1dc9e69e3 Make partition busy detection method selectable per file system (#723842)
GParted's primary inbuilt busy detection method is "is the partition
mounted?".  A custom method is used for LVM2 PV because its not a
mounted file system.

Make busy detection selectable per file system type.

    .fs.busy = FS::NONE  (default)
        No busy detection.

    .fs.busy = FS::GPARTED
        Use internal GParted method which checks if the partition is
        mounted.

    .fs.busy = FS:EXTERNAL
        Call the file system type's member function is_busy().

LVM2 PV busy detection changes from a special case to just electing to
call the lvm2_pv::is_busy() method.  Linux Software RAID remains a
special case because it's only recognised, but not otherwise supported.

Bug #723842 - GParted resizes the wrong filesystem (does not pass the
              devid to btrfs filesystem resize)
2014-07-28 10:03:16 -06:00
Mike Fleetwood 0fcfd18061 Prevent cross thread write after free in _OnReadable() (#731752)
Fragment of debugging and valgrind output:
D: tid=2193 main()
...
D: tid=2202 GParted_Core::set_devices_thread()
...
D: tid=2202 Utils::execute_command(command="dumpe2fs -h /dev/sda1", output, error, use_C_locale=1)
D: tid=2202 this=0x13fef4a0 PipeCapture::PipeCapture()
D: tid=2202 this=0x13fef4f0 PipeCapture::PipeCapture()
D: tid=2202 this=0x13fef4a0 PipeCapture::connect_signal()
D:  sourceid=77
D: tid=2202 this=0x13fef4f0 PipeCapture::connect_signal()
D:  sourceid=78
D: tid=2193 data=0x13fef4a0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4a0 PipeCapture::OnReadable()
D:  signal_update.emit()
D:  return true
D: tid=2193 data=0x13fef4f0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4f0 PipeCapture::OnReadable()
D:  signal_update.emit()
D:  return true
D: tid=2193 data=0x13fef4a0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4a0 PipeCapture::OnReadable()
D:  signal_update.emit()
D:  return true
D: tid=2193 data=0x13fef4f0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4f0 PipeCapture::OnReadable()
D:  signal_eof.emit()
D:  return false
D:  (!rc)  &(pc->sourceid)=0x13fef518
D: tid=2193 data=0x13fef4a0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4a0 PipeCapture::OnReadable()
D:  signal_update.emit()
D:  return true
D: tid=2193 data=0x13fef4a0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4a0 PipeCapture::OnReadable()
D:  signal_update.emit()
D:  return true
D: tid=2193 data=0x13fef4a0 PipeCapture::_OnReadable()
D: tid=2193 this=0x13fef4a0 PipeCapture::OnReadable()
D:  signal_eof.emit()
D: tid=2202 this=0x13fef4f0 PipeCapture::~PipeCapture()
D:  sourceid=0
D: tid=2202 this=0x13fef4a0 PipeCapture::~PipeCapture()
D:  sourceid=77
D:  return false
D:  (!rc)  &(pc->sourceid)=0x13fef4c8
==2193== Thread 1:
==2193== Invalid write of size 4
==2193==    at 0x490580: GParted::PipeCapture::_OnReadable(_GIOChannel*, GIOCondition, void*) (PipeCapture.cc:56)
==2193==    by 0x38662492A5: g_main_context_dispatch (gmain.c:3066)
==2193==    by 0x3866249627: g_main_context_iterate.isra.24 (gmain.c:3713)
==2193==    by 0x3866249A39: g_main_loop_run (gmain.c:3907)
==2193==    by 0x3D7FD45C26: gtk_main (gtkmain.c:1257)
==2193==    by 0x469743: GParted::GParted_Core::set_devices(std::vector<GParted::Device, std::allocator<GParted::Device> >&) (GParted_Core.cc:155)
==2193==    by 0x4A78F1: GParted::Win_GParted::menu_gparted_refresh_devices() (Win_GParted.cc:1259)
==2193==    by 0x4A7886: GParted::Win_GParted::on_show() (Win_GParted.cc:1253)
==2193==    by 0x3D82B2009C: Gtk::Widget_Class::show_callback(_GtkWidget*) (widget.cc:3855)
==2193==    by 0x3867210297: g_closure_invoke (gclosure.c:777)
==2193==    by 0x3867221B86: signal_emit_unlocked_R (gsignal.c:3516)
==2193==    by 0x386722A0F1: g_signal_emit_valist (gsignal.c:3330)
==2193==  Address 0x13fef4c8 is not stack'd, malloc'd or (recently) free'd
==2193==

PipeCapture.cc (with debugging):
    46  gboolean PipeCapture::_OnReadable( GIOChannel *source,
    47                                     GIOCondition condition,
    48                                     gpointer data )
    49  {
    50          std::cout << "D: tid=" << (long int)syscall(SYS_gettid) << " data=" << data << " PipeCapture::_OnReadable()" << std::endl;
    51          PipeCapture *pc = static_cast<PipeCapture *>(data);
    52          gboolean rc = pc->OnReadable( Glib::IOCondition(condition) );
    53          if (!rc)
    54          {
    55                  std::cout << "D:  (!rc)  &(pc->sourceid)=" << &(pc->sourceid) << std::endl;
    56                  pc->sourceid = 0;
    57          }
    58          return rc;
    59  }

The use after free across threads only happens when an external program
is being executed from a thread other than the main() thread.  This is
because by default glib registered callbacks are run by the glib main
loop, which is only called from the main() thread with Gtk::Main::run().

Event sequence:
tid=2193                      tid=2202

main()
...
  GParted_Core::set_devices()
    Glib::Thread::create(... set_devices_thread ...)
    Gtk::Main::run()          GParted_Core::set_devices_thread()
                              ...
                                Utils::execute_command("dumpe2fs ... /dev/sda1" ...)
                                  Glib::spawn_async_with_pipes()
                                  PipeCapture outputcapture(out, output)
                                  outputcapture.connect_signal()
      //Glib main loop runs callback
      PipeCapture::_OnReadable()
        pc->OnReadable()
          //output read
          signal_update.emit()
          return true
      ...
      //Glib main loop runs callback
      PipeCapture::_OnReadable()
        pc->OnReadable()
          //eof reached
[1]       signal_eof.emit()
                                  return status.exit_status
[2]                               PipeCapture::~PipeCapture()
[3]       return false
[4]     pc->sourceid = 0

What is happening is that the PipeCapture destructor [2] is running in
the set_devices_thread() thread and freeing the object's memory as soon
as signal_eof.emit() [1] has been called.  Then signal_eof.emit()
returns back to OnReadable() which then returns false [3] back to the
_OnReadable() callback function which then assigns 0 to sourceid member
variable [4] in the already freed object, detected by valgrind as:
    Invalid write of size 4
       at ... GParted::PipeCapture::_OnReadable(...) (PipeCapture.cc:56)

This is happening because PipeCapture member variable sourceid is being
saved, in a different thread, just so the _OnReadable() callback can be
removed.  However a glib IOChannel callback, type GIOFunc(), returning
false will be automatically removed.

    GLib Reference Manual 2.26 / IO Channels
    https://developer.gnome.org/glib/2.26/glib-IO-Channels.html#GIOFunc

    GIOFunc()

    Returns : the function should return FALSE if the event source
              should be removed

Therefore fix by just not saving the event sourceid at all, and not
calling g_source_remove() to manually remove the callback, but instead
letting glib automatically remove the callback when it returns false.

Bug #731752 - Write after free cross thread race in
              PipeCapture::_OnReadable()
2014-07-06 10:22:40 -06:00
Phillip Susi 947cd02857 Change OperationDetail to not store complex objects in STL containers (#729139)
OperationDetail was storing its children in a std::vector.  This means they
can be moved around in memory arbitrarily, going through indeterminate
lifetimes.  This is generally a bad thing for any non trivial object and
in the case of OperationDetail, it created havoc with the way it maintains
pointers between parent/child objects for signal connections.  It will now
keep only pointers to children in a std::vector instead, so their lifetime
can be controlled, fixing various crashes.

Bug 729139 - Refactor OperationDetail to address random behavior
2014-05-18 10:07:45 -06:00
Phillip Susi 2e7b5d05a6 Prevent GSource double-destroy warning messages (#729800)
Returning false from the OnReadable callback causes the source to
be destroyed, but PipeCapture::~PipeCapture was destroying it a
second time.  Prevent this by zeroing out the sourceid.

Bug 729800 - Prevent GSource double-destroy warning messages
2014-05-18 09:53:31 -06:00
Mike Fleetwood 960ce2df56 Make LVM members selectable together in delete non-empty PV dialog
Place the LVM2 member names in a single string, separated by new lines,
so that they can be selected all together by the user.  This was not
previously possible when each member name was placed in a separate
widget.
2014-04-28 08:42:40 +01:00
Curtis Gedak 72aa552469 Set top vertical alignment for multi-line field and value label pairs
A LVM volume group can contain more than one member and comprise a
multi-line entry.  As such set the "Members" field header and value
label pair to top vertical alignment.
2014-04-28 08:42:40 +01:00
Curtis Gedak 6efa623401 Add optional yalign argument to Utils::mk_label() method
As part of the work on bug 652044 - uses deprecated APIs, selectable
vertical alignment was defaulted to ALIGN_CENTER for all labels.  The
relevant commits can be viewed in comment 26 of said bug report.
https://bugzilla.gnome.org/show_bug.cgi?id=652044#c26

For multi-line labels a vertical ALIGN_CENTER value is not
consistently aesthetically pleasing.  This becomes obvious when a
single-line heading label is paired with a multi-line value label.
To improve the aesthetics, a vertical alignment of ALIGN_TOP is
preferred.

Hence re-add the ability to optionally specify a vertical alignment for
labels.  If a yalign value is not specified a default vertical alignment
of ALIGN_CENTER is used.
2014-04-28 08:42:40 +01:00
Curtis Gedak e075ab006e Remove unused text_color argument from Utils::mk_label() method
None of the current GParted code uses the text_color argument for
Utils::mk_label().  Remove unused argument to simplify code.
2014-04-28 08:42:40 +01:00
Curtis Gedak 09711ae22d Remove partition information vgname from status field (#690542)
With the volume group name displayed immediately below the status field,
the vgname is no longer needed in the status field.

Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak c5f8220f60 Center the data below the partition information graphic (#690542)
When the partition information dialog is resized, the data below the
partition graphic would remain left aligned.  Improve the aesthetics by
horizontally centering the data in the window similar to the partition
graphic.

Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak c0529abf36 Place partition information LVM2 member names in string list (#690542)
Placing the LVM2 member names in a string list enables the member names
to be selected all at once by a user.  This was not possible with
separate label widgets for each LVM2 member name.

Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Mike Fleetwood 960bb2c6cd Remove shadow border from partition information scrollable region (#690542)
Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak ccaeb8dc51 Make the partition information dialog resizable (#690542)
Make the dialog resizable, add a vertical scrollbar to the information
and messages section, and set the initial height to ensure the dialog
fits entirely on an 800x600 screen.

A default height is required because some window managers, such as
fluxbox used in GParted Live, only permit resizing the height by using
the bottom corners of the dialog.  If the dialog is too large for the
screen then the user would not be able to resize it.

Note that two default initial heights are used in an effort to minimize
the amount of extra whitespace.

Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak b271e367ea Place partition information percentages in their own column (#690542)
On some desktop environments, such as Unity on Ubuntu 13.04, the
partition information dialog can display the used/unused/unallocated
field values overlapping with the "(##%)" percentage value.  The problem
is pronounced for values with 4 digits to the left of the decimal (for
example a 1024 MiB partition ).  To address this problem, these
percentages are placed in their own column.

Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak 898bc35198 Organize partition information into two field & value columns (#690542)
Organize the partition information field & value areas into two columns
to minimize the amount of vertical space required.

This is part of a series of changes to enable viewing all partition
information details on a minimum 800x600 display.

Part of Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak 92c771bc7c Add partition information section titles and indent fields (#690542)
This is part of a series of changes to enable viewing all partition
information details on a minimum 800x600 display.

Part of Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Curtis Gedak 7fc8aa49fe Organize partition information fields into logical sections (#690542)
This is part of a series of changes to enable viewing all partition
information details on a minimum 800x600 display.

Part of Bug 690542 - Partition Information Dialog Warning not readable
2014-04-28 08:42:40 +01:00
Mike Fleetwood 5f6656f267 Initialise file system objects only once
The code used to unnecessarily destroy and re-create the file system
objects on every scan for file system support tools.

Instead only create the file system objects once and just call each
object's get_filesystem_support() method on each rescan.
2014-04-23 10:25:56 -06:00
Mike Fleetwood 131098a797 Remove set_proper_filesystem() method
Prior to commit:

    1f3b11748e
    Remove GParted_Core::p_filesystem (#683149)

set_proper_filesystems() used to set GParted_Core::p_filesystem member
variable to one of the FileSystem objects, but that was just treating it
like a local variable.  After the commit local variables named
p_filesystem were used where required and set_proper_filesystem() became
a function which did nothing other than call get_filesystem_object().

Now remove set_proper_filesystem() altogether and use
get_filesystem_object() in its place.
2014-04-23 10:25:27 -06:00
Mike Fleetwood 438685577b Remove outdated comment about ped_partition_is_busy() for DMRaid devices
Comment is outdated after GParted stopped using ped_partition_is_busy()
for busy partition detection of normal and logical partitions since
commit:

    4202992063
    Fix false busy detection of unusual case with Linux Software RAID (#712533)
2014-04-23 09:54:20 -06:00
Curtis Gedak 7b1011237c Avoid using String::ucompose to build command lines
The String::ucompose method is for easy, i18n-friendly composition of
strings with Gtkmm.  From past experience, String::ucompose should not
be used to build command lines due to differences in how locales handle
number translation.  More specifically, not all locales use number
separators (spaces, commas or periods) and the decimal separator
(period or comma) in the same way.

The problem with using String::ucompose for command lines was
originally encountered and corrected in the following commit:

Fix attempt data rescue fail to open read-only view (#673246)
e494eca1f7
2014-03-05 22:12:10 +00:00
Phillip Susi 86111fe12a Use e2image to move/copy ext[234] file systems (#721516)
Use e2image features added in e2fsprogs 1.42.9 to move/copy
an ext[234] file system more efficiently by skipping unused blocks.
Fall back to the internal copy algorithm if e2image is not found
or does not support move/copy.

Bug #721516 - Use e2image to move/copy ext[234] filesystems
2014-03-05 22:12:10 +00:00
Curtis Gedak 203a345cfb Update copyright year 2014-02-08 12:23:55 +00:00
Phillip Susi 5f9a55fdcb Remove incorrect rounding in file system resize (#723543)
Work on bug 701075 inadvertently introduced a problem where ext2/3/4,
resierfs, and ntfs file systems were rounding the file system size up
to the nearest kiB.

The problem was discovered when a user resized a partition by moving
only the start boundary to the right thereby shrinking the partition.
In the situation where the resulting partition size was not an integer
kiB value (such as might occur on a 512 byte per sector device) the
file system size was rounded up to the nearest kiB.  This resulted in
a file system size being set one sector larger than the partition
size.

This problem was introduced with the following commit:

Shrink file systems to exact size (#701075)
3461413d28

The fix chosen for this problem involved removing the rounding logic.

Bug 723543 - Shrink ext2/3/4 results in attempt to set partition
             smaller than file system
2014-02-06 10:20:05 -07:00
Mike Fleetwood 9728479771 Recognise BitLocker encrypted partitions (#723232)
Only recognises partitions containing BitLocker Disk Encryption content.
No other actions are supported.

Bug #723232 - BitLocker Disk Encryption not recognised
2014-01-31 09:55:05 -07:00
Mike Fleetwood a70bfd32fe Restore compilation order of source files
Restore the order of the source files so that they are once again
compiled in order A-Z, a-z.  Order is obtained with:

    fgrep .cc src/Makefile.am | LANG=C sort
    fgrep .h include/Makefile.am | LANG=C sort
2014-01-27 10:54:48 -07:00
Mike Fleetwood 6f9b99f138 Make the File System Support dialog resizable (#342682)
Make the dialog resizable, add a vertical scrollbar and set the minimum
(and therefore initial) height to 500 pixels.  This is so that the
dialog entirely fits on an 800x600 screen, thus allowing the rescan
button to be pressed.

100 pixel difference is to account for the size of the top and bottom
GNOME 2 panels and two sets of title bars.  Two sets of title bars
because the window manager tries to place the top of dialog title bars
in line with the bottom of the main window title bar.

Bug #342682 - too much information in 'features' dialog
2014-01-27 10:54:48 -07:00
Mike Fleetwood cb8ce9caea Indent File System Support legend narrative (#342682)
Use padding to indent the legend narrative to better show hierarchy and
association with the "Legend" category as recommended in the:

    GNOME HIG 2.2.3 / Visual Design / Window Layout
    https://developer.gnome.org/hig-book/stable/design-window.html.en

Padding the legend narrative widget also deliberately increases the
spacing to the legend icons too.

Bug #342682 - too much information in 'features' dialog
2014-01-27 10:54:48 -07:00
Mike Fleetwood 59f684d8cd Always show the File System Support dialog legend (#342682)
Make the legend always shown, ready for when the dialog is resizable.

Change the widget containing the legend from an expander to a frame
widget.  Set the frame to be borderless using a bold label as
recommended in the GNOME Human Interface Guidelines 2.2.1 / Controls /
Frames and Separators.

    https://developer.gnome.org/hig-book/2.32/controls-frames.html.en

Bug #342682 - too much information in 'features' dialog
2014-01-27 10:54:48 -07:00
Mike Fleetwood dd9e2161d4 Enhance readability of the DialogFeatures constructor
The DialogFeatures::DialogFeatures() constructor used four levels of
nested code blocks and reused the same variable names at multiple
levels.  The variable hbox was used at three of the four levels.  This
was done by commit:

    91b971691d
    Bug #634090: Change some attributes to local variables

In my opinion this made the code hard to understand.  Give every local
variable a unique and meaningful name and remove the nested code blocks.
2014-01-27 10:54:48 -07:00
Daniel Mustieles 3861b9257b Replace obsolete FSF postal address in copyright notices (#721565)
This is part of parent bug:
    Bug #721455 - Obsolete info in license text on multiple modules

and GNOME Goal:
    https://wiki.gnome.org/Initiatives/GnomeGoals/Proposals

    * verify all source files to make sure they have a license and a
      copyright, and that both are up-to-date

Bug #721565 -  License text contains obsolete FSF postal address
2014-01-26 10:53:23 +00:00
Mike Fleetwood 0e253fa234 Prevent crash when creating new partition on loop label (#721988)
Write a loop partition table to a disk and then try to create a new
partition on that disk crashes GParted.

Analysis:

1) Loop partition table, actually just the signature / label
   "GNU Parted Loopback 0", is written to the disk.
2) create_partition() calls ped_disk_app_partition() to create the
   partition which, arguably erroneously, clears the loop signature.
3) In erase_filesystem_signatures(), get_device_and_disk() fails because
   there's no longer a loop signature for libparted to recognise.  This
   leaves lp_device, lp_disk and lp_partition = NULL, but incorrectly
   overall_success = true.
4) Overall_success = true allows the signature erasure loop to run and
   evaluate lp_device->sector_size, dereferencing a NULL pointer and
   crashing GParted.

Fix erase_filesystem_signatures() to correctly handle failure from
get_device_and_disk(), broken by this commit:

    246e05559d
    Clear btrfs file system super block mirror copies too (#705426)

Bug #721988 - Crash when creating new partition on disk with loop label
2014-01-13 08:06:09 +00:00
Mike Fleetwood 7b43d52720 Display partition table types in order (#711098)
In the Create Partition Table dialog display the entries in the combobox
in order.

Previously the default of MSDOS or GPT was moved to the first item in
the combobox.  Now the partition table types remain in order with just
either MSDOS or GPT being selected as as the default as required.

The partition table types are displayed in the order supplied by
libparted, which is alphabetic except with "loop" last.

Bug #711098 - Default partition table can not handle > 2 TiB disks
2014-01-06 10:45:00 +00:00
Phillip Susi 07bd72ba80 Default to GPT on disks >= 2 TiB (#711098)
MSDOS partition table is limited to addressing 2^32 sectors, limiting
disks using 512 byte sectors to 2 TiB in size.  Fdisk reports the
following warning on disks 2 TiB and larger.

    # truncate -s 2T /var/tmp/loop-2T
    # losetup /dev/loop0 /var/tmp/loop-2T
    # fdisk /dev/loop0

    WARNING: The size of this disk is 2.2 TB (2199023255552 bytes).
    DOS partition table format can not be used on drives for volumes
    larger than (2199023255040 bytes) for 512-byte sectors. Use parted(1) and GUID
    partition table format (GPT).

(Fdisk arguably reports this warning one sector too early.  Anyway for
safety and consistency GParted will use this limit too).  Continue to
use MSDOS as the default partition table type for disks smaller than 2
TiB and use GPT as the default for disks 2 TiB and larger.  This
maximises compatibility.

Also remove the advanced expander and always show the partition table
list box.

Bug #711098 - Default partition table can not handle > 2 TiB disks
2014-01-06 10:44:59 +00:00
Mike Fleetwood a4b82a9305 Successfully read kernel versions with only 2 components
When the kernel version as stated in /proc/version did not have at least
three numbers separated by periods, the version would fail to be read.
Sample /proc/version to demonstrate problem:

    Linux version 3.10-3-686-pae ...

The Linux kernel will always have a major number and a minor number
separated by a period.  There is likely also a patch version number too
that would be preceded by a period.  This enhancement will read 2 and 3
component Linux kernel versions.
2013-12-04 18:22:34 -07:00
Mike Fleetwood b3f0a0b1be Remove multi-line comment compiler warning from PipeCapture.cc
Remove this compiler warning:
    PipeCapture.cc:56:2: warning: multi-line comment [-Wcomment]
      //      /\                                          /\        /\
      ^

added by commit:
    1b54123580
    Enhance line discipline in PipeCapture::OnReadable() (#709276)
2013-11-27 20:21:10 +00:00
Curtis Gedak d842b8cdfb Show SWRAID active/not active status in information dialog (#712533)
Bug #712533 - Partitions not detected as busy inside Software RAID on
              some distros
2013-11-27 20:21:10 +00:00
Mike Fleetwood ad63ede421 Fix detection of busy status of extended partitions (#712533)
GParted doesn't report extended partitions as busy if it contains only
busy LVM2 PV or SWRAID logical partitions.  Libparted's
ped_partition_is_busy() only detects mounted file systems and swap space
as busy, not active LVM2 PVs or SWRAID members.  This is as of libparted
3.1 and earlier.

Fix by determining the busy status of an extended partition based solely
on the busy status of the logical partitions it contains.  This makes it
unnecessary to check for mounted DMRAID logical partitions or call
ped_partition_is_busy() for extended partitions.

Bug #712533 - Partitions not detected as busy inside Software RAID on
              some distros
2013-11-27 20:21:10 +00:00
Mike Fleetwood 4202992063 Fix false busy detection of unusual case with Linux Software RAID (#712533)
In this unusual setup of having one Linux Software RAID array
partitioned and a second array not partitioned, /dev/md1p1 partition in
the first array is falsely reported as busy when the file system filling
the second array /dev/md2 is mounted.

  Partition   RAID                File System        Mount mount
  ---------   -----------------   ----------------   -----------
  /dev/sdb1   SWRAID /dev/md1
                     /dev/md1p1   ext4,Label=test1   (Unmounted)
  /dev/sdb2   SWRAID /dev/md2     ext4,Label=test2   /mnt/2

This again affects Debian 6, Debian 7 and RHEL/CentOS 6.x and is as a
result of the this bug fixed in libparted 2.4:
    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=db52898c214310dab4ed84e6cb2f9ffc8c3aa502
    linux: also detect "in-use" dmraid and scsi-Nth (N>=16) partition

Fix by not using libparted's ped_partition_is_busy() to determine if a
normal or logical partition is busy.

Bug #712533 - Partitions not detected as busy inside Software RAID on
              some distros
2013-11-27 20:21:10 +00:00
Mike Fleetwood 4d7ebe0314 Fix busy detection of file systems inside Linux Software RAID (#712533)
On some distributions GParted doesn't detect when a file system is busy
and mounted when the partition is within a Linux Software RAID array.
Affected distributions include: Debian 6, Debian 7 and RHEL/CentOS 6.x.

This is because of a bug in libparted 2.3 and earlier.  It was fixed by
this change in libparted 2.4:
    http://git.savannah.gnu.org/cgit/parted.git/commit/?id=db52898c214310dab4ed84e6cb2f9ffc8c3aa502
    linux: also detect "in-use" dmraid and scsi-Nth (N>=16) partition

Ubuntu 13.10 with libparted 2.3 is not affected because it contains a
distribution specific patch to correct it.

Fix by using GParted's internal mounted partitions map, mount_info, to
determine if a partition is busy, in addition to using libparted's
ped_partition_is_busy().

Bug #712533 - Partitions not detected as busy inside Software RAID on
              some distros
2013-11-27 20:21:10 +00:00
Phillip Susi de2844d02d Add online resize support (#694622)
Many file systems are capable of growing while mounted, and a few can
even shrink.  This support must be explicitly enabled at configure time
with the --enable-online-resize flag and depends on a patched libparted.
Also requires kernel >= 3.6 for partition resizing, even if the
partition is in use (BLKPG_RESIZE_PARTITION).

Thanks to Mike Fleetwood for double check mark idea instead of a second
column to show the online grow/shrink.

Bug #694622 - Add support for online resize
2013-11-24 10:10:37 +00:00
Phillip Susi 73f2c689f7 Change default file system to ext4 (#711114)
In the Create New Partition dialog use ext4 as the default choice for
new file systems.  It has been the preferred file system of
distributions for many years.  Use ext3 and ext2 as second and third
choice defaults.  This handles RHEL/CentOS 5.x which doesn't support
ext4.

Bug #711114 - Change default fs to ext4
2013-10-31 21:16:14 +00:00
Mike Fleetwood 98cd693818 Make Partition Information dialog warnings selectable (#705596)
Allow all partition warning messages to be selectable and copied.  Merge
all the messages into a single Gtk::Label widget so that they can be
selected together.

Use a blank line between individual messages so that each message can be
distinguished. Therefore each message should be formatted as one or more
non-blank lines, with an optional trailing new line.  This is true of
GParted internal messages and probably all external messages and errors
from libparted and executed commands too.

Bug #705596 - Partition Information Dialog - let user copy warnings
2013-10-22 10:12:16 -06:00
Mike Fleetwood d2e1130ad2 Detect busy status of Linux Software RAID members (#709640)
Read the contents of /proc/mdstat file to determine if a device is a
member of of an active RAID array.

    $ cat /proc/mdstat
    Personalities : [raid1]
    md1 : active raid1 sda1[2] sdb1[3]
          524224 blocks super 1.0 [2/2] [UU]

    md2 : active raid1 sdb2[2] sda2[3](F)
          5238720 blocks super 1.1 [2/1] [U_]

    md3 : active raid1 sdb3[1]
          10477440 blocks super 1.1 [2/1] [_U]
          bitmap: 1/1 pages [4KB], 65536KB chunk

    md4 : inactive sda4[0](S)
          1048564 blocks super 1.2

    unused devices: <none>

There are 5 example Linux Software RAID arrays, md1 to md5.  All are
RAID1 mirrors with 2 members, in various states.

    Array  Members     Status
    md1    sda1, sdb2  Fully operational.
    md2    sda2, sdb2  Member sda2 marked as faulty.  (Device sda2 is
                       still in use).
    md3    sda3, sdb3  Member sda3 has been removed.  (Device sda3 is
                       not in use).
    md4    sda4, sdb4  Incremental start of member sda4 only.  (Neither
                       member devices is in use).
    md5    sda5, sdb5  Array stopped.  (Neither member device is in
                       use).

Also disable "Unmount" in the partition menu for active RAID array
members.

Bug #709640 - Linux Swap Suspend and Software RAID partitions not
              recognised
2013-10-19 10:21:03 -06:00
Mike Fleetwood fac3f5b865 Recognise Linux Swap Suspend and Software RAID partitions (#709640)
Recognise in kernel, Linux Swap Suspend partitions.  (When hibernated
the kernel write the RAM out to swap space and changes the magic string
from swap space to suspend).  Recognition required either
libparted >= 1.8.8.1 or blkid from util-linux >= 2.15 or before that
blkid from e2fsprogs >= 1.39.

Recognise Linux Software RAID partitions.  Recognition requires blkid
from util-linux >= 2.15.

Example:

    # blkid /dev/sda10 /dev/sda11
    /dev/sda10: ... TYPE="swsuspend"
    /dev/sda11: ... TYPE="linux_raid_member"

    # parted /dev/sda print
    Model: ATA SAMSUNG HM500JI (scsi)
    Disk /dev/sda: 500GB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos

    Number  Start   End     Size    Type      File system     Flags
    ...
    10      361GB   362GB   1074MB  logical   swsusp
    11      362GB   363GB   1074MB  logical                   raid

Bug #709640 - Linux Swap Suspend and Software RAID partitions not
              recognised
2013-10-19 10:05:54 -06:00