[gparted] Display usage of encrypted file systems (#760080)



commit cb24aa4be1f67487f4e42b8a486e5902cf618c5b
Author: Mike Fleetwood <mike fleetwood googlemail com>
Date:   Mon Dec 28 13:47:12 2015 +0000

    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

 include/Partition.h     |   10 ++--
 include/PartitionLUKS.h |    5 ++
 src/PartitionLUKS.cc    |  104 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+), 5 deletions(-)
---
diff --git a/include/Partition.h b/include/Partition.h
index b571719..e1d79f2 100644
--- a/include/Partition.h
+++ b/include/Partition.h
@@ -82,11 +82,11 @@ public:
                  bool busy );
 
        void set_sector_usage( Sector sectors_fs_size, Sector sectors_fs_unused ) ;
-       bool sector_usage_known() const ;
-       Sector estimated_min_size() const ;
-       Sector get_sectors_used() const ;
-       Sector get_sectors_unused() const ;
-       Sector get_sectors_unallocated() const ;
+       virtual bool sector_usage_known() const;
+       virtual Sector estimated_min_size() const;
+       virtual Sector get_sectors_used() const;
+       virtual Sector get_sectors_unused() const;
+       virtual Sector get_sectors_unallocated() const;
        void get_usage_triple( int imax, int & i1, int & i2, int & i3 ) const ;
 
        void Set_Unallocated( const Glib::ustring & device_path,
diff --git a/include/PartitionLUKS.h b/include/PartitionLUKS.h
index 46c83bb..ec1b6fd 100644
--- a/include/PartitionLUKS.h
+++ b/include/PartitionLUKS.h
@@ -34,6 +34,11 @@ public:
        Partition & get_encrypted()              { return encrypted; };
        const Partition & get_encrypted() const  { return encrypted; };
 
+       virtual bool sector_usage_known() const;
+       virtual Sector estimated_min_size() const;
+       virtual Sector get_sectors_used() const;
+       virtual Sector get_sectors_unused() const;
+       virtual Sector get_sectors_unallocated() const;
        virtual Glib::ustring get_filesystem_label() const;
 
 private:
diff --git a/src/PartitionLUKS.cc b/src/PartitionLUKS.cc
index 878f119..71312db 100644
--- a/src/PartitionLUKS.cc
+++ b/src/PartitionLUKS.cc
@@ -37,6 +37,110 @@ PartitionLUKS * PartitionLUKS::clone() const
        return new PartitionLUKS( *this );
 }
 
+bool PartitionLUKS::sector_usage_known() const
+{
+       if ( busy )
+               // For an open dm-crypt mapping the usage of both the LUKS and encrypted
+               // file system must be known.
+               return Partition::sector_usage_known() && encrypted.sector_usage_known();
+       return Partition::sector_usage_known();
+}
+
+// An encrypted partition is laid out, and usage figures calculated like this:
+//
+//     Partition
+//     |<---------------------------------------->| <- this PartitionLUKS object
+//     LUKS   LUKS
+//     Header Mapping
+//     |<--->||<----------------------->|           <- encrypted Partition object member
+//     1111111111111111111111111111111111uuuuuuuuuu <- this->sectors_{used,unused,unallocated}
+//            Encrypted file system
+//            |<----------------->|
+//            111111111111110000000uuuuuu           <- encrypted.sectors_{used,unused,unallocated}
+//            ^                         ^
+//            |                         `------------- encrypted.sector_end
+//            `--------------------------------------- encrypted.sector_start (== size of LUKS header)
+//     1111111111111111111110000000uuuuuuuuuuuuuuuu <- Overall usage figures as used in the following
+//                                                     usage related methods.
+// Legend:
+// 1 - used sectors
+// 0 - unused sectors
+// u - unallocated sectors
+//
+// Calculations:
+//     total_used        = LUKS Header size + Encrypted file system used
+//                       = encrypted.sector_start + encrypted.sectors_used
+//     total_unallocated = LUKS unallocated + Encrypted file system unallocated
+//                       = this->sectors_unallocated + encrypted_unallocated
+//     total_unused      = LUKS unused + Encrypted file system unused
+//                       = 0 + encrypted.sectors_unused
+//
+//     By definition LUKS unused is 0 (See luks::set_used_sectors()).
+
+// Return estimated minimum size to which the partition can be resized.
+// See Partition::estimated_min_size() for unallocated threshold reasoning.
+Sector PartitionLUKS::estimated_min_size() const
+{
+       if ( busy )
+       {
+               // For an open dm-crypt mapping work with above described totals.
+               if ( sectors_used >= 0 && encrypted.sectors_used >= 0 )
+               {
+                       Sector total_used        = encrypted.sector_start + encrypted.sectors_used;
+                       Sector total_unallocated = sectors_unallocated + encrypted.sectors_unallocated;
+                       return total_used + std::min( total_unallocated, significant_threshold );
+               }
+               return -1;
+       }
+       return Partition::estimated_min_size();
+}
+
+// Return user displayable used sectors.
+// See Partition::get_sectors_used() for unallocated threshold reasoning.
+Sector PartitionLUKS::get_sectors_used() const
+{
+       if ( busy )
+       {
+               // For an open dm-crypt mapping work with above described totals.
+               if ( sectors_used >= 0 && encrypted.sectors_used >= 0 )
+               {
+                       Sector total_used        = encrypted.sector_start + encrypted.sectors_used;
+                       Sector total_unallocated = sectors_unallocated + encrypted.sectors_unallocated;
+                       if ( total_unallocated < significant_threshold )
+                               return total_used + total_unallocated;
+                       else
+                               return total_used;
+               }
+               return -1;
+       }
+       return Partition::get_sectors_used();
+}
+
+// Return user displayable unused sectors.
+// See above described totals.
+Sector PartitionLUKS::get_sectors_unused() const
+{
+       if ( busy )
+               return encrypted.get_sectors_unused();
+       return Partition::get_sectors_unused();
+}
+
+// Return user displayable unallocated sectors.
+// See Partition::get_sectors_unallocated() for unallocated threshold reasoning.
+Sector PartitionLUKS::get_sectors_unallocated() const
+{
+       if ( busy )
+       {
+               // For an open dm-crypt mapping work with above described totals.
+               Sector total_unallocated = sectors_unallocated + encrypted.sectors_unallocated;
+               if ( total_unallocated < significant_threshold )
+                       return 0;
+               else
+                       return total_unallocated;
+       }
+       return Partition::get_sectors_unallocated();
+}
+
 // Return the label of the encrypted file system within, or "" if no open mapping.
 Glib::ustring PartitionLUKS::get_filesystem_label() const
 {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]