"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "src/FS_Info.cc" between
gparted-1.2.0.tar.gz and gparted-1.3.0.tar.gz

About: GParted is a graphical partition editor for creating, reorganizing, and deleting disk partitions (using GNU libparted).

FS_Info.cc  (gparted-1.2.0):FS_Info.cc  (gparted-1.3.0)
skipping to change at line 19 skipping to change at line 19
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>. * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/ */
#include "FS_Info.h" #include "FS_Info.h"
#include "BlockSpecial.h" #include "BlockSpecial.h"
#include "Proc_Partitions_Info.h"
#include "Utils.h" #include "Utils.h"
#include <iostream>
#include <glibmm/ustring.h> #include <glibmm/ustring.h>
#include <glibmm/miscutils.h> #include <glibmm/miscutils.h>
#include <glibmm/shell.h> #include <glibmm/shell.h>
#include <vector> #include <vector>
namespace GParted namespace GParted
{ {
//initialize static data elements //initialize static data elements
bool FS_Info::fs_info_cache_initialized = false ; bool FS_Info::fs_info_cache_initialized = false ;
bool FS_Info::blkid_found = false ; bool FS_Info::blkid_found = false ;
// Assume workaround is needed just in case determination fails and as // Assume workaround is needed just in case determination fails and as
// it only costs a fraction of a second to run blkid command again. // it only costs a fraction of a second to run blkid command again.
bool FS_Info::need_blkid_vfat_cache_update_workaround = true; bool FS_Info::need_blkid_vfat_cache_update_workaround = true;
// Vector of file system information. // Vector of file system information.
// E.g. // E.g.
// (Note BS(path) is a short hand for constructor BlockSpecial(path)). // (Note BS(path) is a short hand for constructor BlockSpecial(path)).
// //path , type , sec_type, uuid // //path , type , sec_type, uuid
, have_label, label , have_label, label
// [{BS("/dev/sda1") , "xfs" , "" , "f828ee8c-1e16- // [{BS("/dev/sda") , "" , "" , ""
4ca9-b234-e4949dcd4bd1" , false , "" }, , false , "" },
// {BS("/dev/sda2") , "LVM2_member", "" , "p31pR5-qPLm-YI // {BS("/dev/sda1"), "xfs" , "" , "f828ee8c-1e16-4ca9-b234-e494
Cz-O09i-sB4u-mAH2-GVSNWG", false , "" }, 9dcd4bd1" , false , "" },
// {BS("/dev/block/8:2") , "LVM2_member", "" , "p31pR5-qPLm-YI // {BS("/dev/sda2"), "LVM2_member", "" , "p31pR5-qPLm-YICz-O09i-sB4u-m
Cz-O09i-sB4u-mAH2-GVSNWG", false , "" }, AH2-GVSNWG", false , "" },
// {BS("/dev/mapper/centos-root"), "xfs" , "" , "a195605d-22c1- // {BS("/dev/sdb") , "" , "" , ""
422d-9213-1ed67f1eee46" , false , "" }, , false , "" },
// {BS("/dev/mapper/centos-swap"), "swap" , "" , "8d419cb6-c663- // {BS("/dev/sdb1"), "ext3" , "ext2" , "f218c3b8-237e-4fbe-92c5-7662
4db7-b91c-6bcef8418a4d" , false , "" }, 3bba4062" , true , "test-ext3" },
// {BS("/dev/sdb1") , "ext3" , "ext2" , "f218c3b8-237e- // {BS("/dev/sdb2"), "vfat" , "msdos" , "9F87-1061"
4fbe-92c5-76623bba4062" , true , "test-ext3" }, , true , "TEST-FAT16"},
// {BS("/dev/sdb2") , "vfat" , "msdos" , "9F87-1061" // {BS("/dev/sdb3"), "" , "" , ""
, true , "TEST-FAT16"}, , false , "" }
// {BS("/dev/sdb3") , "" , "" , ""
, false , "" }
// ] // ]
std::vector<FS_Entry> FS_Info::fs_info_cache; std::vector<FS_Entry> FS_Info::fs_info_cache;
void FS_Info::load_cache() void FS_Info::clear_cache()
{ {
set_commands_found(); set_commands_found();
load_fs_info_cache(); fs_info_cache.clear();
fs_info_cache_initialized = true; fs_info_cache_initialized = true;
} }
void FS_Info::load_cache_for_paths( const std::vector<Glib::ustring> &device_pat hs ) void FS_Info::load_cache_for_paths(const std::vector<Glib::ustring>& paths)
{ {
initialize_if_required(); if (not_initialised_then_error())
const BlockSpecial empty_bs = BlockSpecial(); return;
for ( unsigned int i = 0 ; i < device_paths.size() ; i ++ )
{ run_blkid_load_cache(paths);
const FS_Entry & fs_entry = get_cache_entry_by_path( device_paths
[i] );
if ( fs_entry.path == empty_bs )
{
// Run "blkid PATH" and load entry into cache for missing
entries.
load_fs_info_cache_extra_for_path( device_paths[i] );
}
}
} }
// Retrieve the file system type for the path // Retrieve the file system type for the path
Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path ) Glib::ustring FS_Info::get_fs_type( const Glib::ustring & path )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
const FS_Entry & fs_entry = get_cache_entry_by_path( path ); const FS_Entry & fs_entry = get_cache_entry_by_path( path );
Glib::ustring fs_type = fs_entry.type; Glib::ustring fs_type = fs_entry.type;
Glib::ustring fs_sec_type = fs_entry.sec_type; Glib::ustring fs_sec_type = fs_entry.sec_type;
// If vfat, decide whether fat16 or fat32 // If vfat, decide whether fat16 or fat32
if ( fs_type == "vfat" ) if ( fs_type == "vfat" )
{ {
if ( need_blkid_vfat_cache_update_workaround ) if ( need_blkid_vfat_cache_update_workaround )
{ {
// Blkid cache does not correctly add and remove SEC_TYPE when // Blkid cache does not correctly add and remove SEC_TYPE when
skipping to change at line 109 skipping to change at line 103
else else
fs_type = "fat32"; fs_type = "fat32";
} }
return fs_type; return fs_type;
} }
// Retrieve the label and set found indicator for the path // Retrieve the label and set found indicator for the path
Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found ) Glib::ustring FS_Info::get_label( const Glib::ustring & path, bool & found )
{ {
initialize_if_required(); if (not_initialised_then_error())
{
found = false;
return "";
}
BlockSpecial bs = BlockSpecial( path ); BlockSpecial bs = BlockSpecial( path );
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( bs == fs_info_cache[i].path ) if ( bs == fs_info_cache[i].path )
{ {
if ( fs_info_cache[i].have_label || fs_info_cache[i].type == "" ) if ( fs_info_cache[i].have_label || fs_info_cache[i].type == "" )
{ {
// Already have the label or this is a blank cach e entry // Already have the label or this is a blank cach e entry
// for a whole disk device containing a partition table, // for a whole disk device containing a partition table,
// so no label (as created by // so no label.
// load_fs_info_cache_extra_for_path()).
found = fs_info_cache[i].have_label; found = fs_info_cache[i].have_label;
return fs_info_cache[i].label; return fs_info_cache[i].label;
} }
// Run blkid to get the label for this one partition, upd ate the // Run blkid to get the label for this one partition, upd ate the
// cache and return the found label. // cache and return the found label.
found = run_blkid_update_cache_one_label( fs_info_cache[i ] ); found = run_blkid_update_cache_one_label( fs_info_cache[i ] );
return fs_info_cache[i].label; return fs_info_cache[i].label;
} }
found = false; found = false;
return ""; return "";
} }
// Retrieve the uuid given for the path // Retrieve the uuid given for the path
Glib::ustring FS_Info::get_uuid( const Glib::ustring & path ) Glib::ustring FS_Info::get_uuid( const Glib::ustring & path )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
const FS_Entry & fs_entry = get_cache_entry_by_path( path ); const FS_Entry & fs_entry = get_cache_entry_by_path( path );
return fs_entry.uuid; return fs_entry.uuid;
} }
// Retrieve the path given the uuid // Retrieve the path given the uuid
Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid ) Glib::ustring FS_Info::get_path_by_uuid( const Glib::ustring & uuid )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( uuid == fs_info_cache[i].uuid ) if ( uuid == fs_info_cache[i].uuid )
return fs_info_cache[i].path.m_name; return fs_info_cache[i].path.m_name;
return ""; return "";
} }
// Retrieve the path given the label // Retrieve the path given the label
Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label ) Glib::ustring FS_Info::get_path_by_label( const Glib::ustring & label )
{ {
initialize_if_required(); if (not_initialised_then_error())
return "";
update_fs_info_cache_all_labels(); update_fs_info_cache_all_labels();
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( label == fs_info_cache[i].label ) if ( label == fs_info_cache[i].label )
return fs_info_cache[i].path.m_name; return fs_info_cache[i].path.m_name;
return ""; return "";
} }
// Private methods // Private methods
void FS_Info::initialize_if_required() bool FS_Info::not_initialised_then_error()
{ {
if ( ! fs_info_cache_initialized ) if (! fs_info_cache_initialized)
{ std::cerr << "GParted Bug: FS_Info (blkid) cache not loaded befor
set_commands_found(); e use" << std::endl;
load_fs_info_cache(); return ! fs_info_cache_initialized;
fs_info_cache_initialized = true;
}
} }
void FS_Info::set_commands_found() void FS_Info::set_commands_found()
{ {
//Set status of commands found //Set status of commands found
blkid_found = (! Glib::find_program_in_path( "blkid" ) .empty() ) ; blkid_found = (! Glib::find_program_in_path( "blkid" ) .empty() ) ;
if ( blkid_found ) if ( blkid_found )
{ {
// Blkid from util-linux before 2.23 has a cache update bug which prevents // Blkid from util-linux before 2.23 has a cache update bug which prevents
// correct identification between FAT16 and FAT32 when overwritin g one // correct identification between FAT16 and FAT32 when overwritin g one
skipping to change at line 210 skipping to change at line 211
{ {
BlockSpecial bs = BlockSpecial( path ); BlockSpecial bs = BlockSpecial( path );
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
if ( bs == fs_info_cache[i].path ) if ( bs == fs_info_cache[i].path )
return fs_info_cache[i]; return fs_info_cache[i];
static FS_Entry not_found = {BlockSpecial(), "", "", "", false, ""}; static FS_Entry not_found = {BlockSpecial(), "", "", "", false, ""};
return not_found; return not_found;
} }
void FS_Info::load_fs_info_cache() void FS_Info::run_blkid_load_cache(const std::vector<Glib::ustring>& paths)
{
fs_info_cache.clear();
// Run "blkid" and load entries into the cache.
run_blkid_load_cache();
// (#771244) Ensure the cache has entries for all whole disk devices, eve
n if
// those entries are blank. Needed so that an ISO9660 image stored on a
whole
// disk device is detected before any embedded partitions within the imag
e.
const BlockSpecial empty_bs = BlockSpecial();
std::vector<Glib::ustring> all_devices = Proc_Partitions_Info::get_device
_paths();
for ( unsigned int i = 0 ; i < all_devices.size() ; i ++ )
{
const FS_Entry & fs_entry = get_cache_entry_by_path( all_devices[
i] );
if ( fs_entry.path == empty_bs )
{
// Run "blkid PATH" and load entry into cache for missing
entries.
load_fs_info_cache_extra_for_path( all_devices[i] );
}
}
}
void FS_Info::load_fs_info_cache_extra_for_path( const Glib::ustring & path )
{
bool entry_added = run_blkid_load_cache( path );
if ( ! entry_added )
{
// Ran "blkid PATH" but didn't find details suitable for loading
as a
// cache entry so add a blank entry for PATH name here.
FS_Entry fs_entry = {BlockSpecial( path ), "", "", "", false, ""}
;
fs_info_cache.push_back( fs_entry );
}
}
bool FS_Info::run_blkid_load_cache( const Glib::ustring & path )
{ {
// Parse blkid output line by line extracting mandatory field: path and o ptional // Parse blkid output line by line extracting mandatory field: path and o ptional
// fields: type, sec_type, uuid. Label is not extracted here because of blkid's // fields: type, sec_type, uuid. Label is not extracted here because of blkid's
// default non-reversible encoding of non printable ASCII bytes. // default non-reversible encoding of non printable ASCII bytes.
// Example command:
// blkid /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /d
ev/sdb3
// Example output: // Example output:
// /dev/sda: PTUUID="5012fb1f" PTTYPE="dos"
// /dev/sda1: UUID="f828ee8c-1e16-4ca9-b234-e4949dcd4bd1" TYPE="xfs" // /dev/sda1: UUID="f828ee8c-1e16-4ca9-b234-e4949dcd4bd1" TYPE="xfs"
// /dev/sda2: UUID="p31pR5-qPLm-YICz-O09i-sB4u-mAH2-GVSNWG" TYPE="LVM 2_member" // /dev/sda2: UUID="p31pR5-qPLm-YICz-O09i-sB4u-mAH2-GVSNWG" TYPE="LVM 2_member"
// /dev/block/8:2: UUID="p31pR5-qPLm-YICz-O09i-sB4u-mAH2-GVSNWG" TYPE // /dev/sdb: PTUUID="f57595e1-c0ae-40ee-be64-00851b2a9977" PTTYPE="gp
="LVM2_member" t"
// /dev/mapper/centos-root: UUID="a195605d-22c1-422d-9213-1ed67f1eee4
6" TYPE="xfs"
// /dev/mapper/centos-swap: UUID="8d419cb6-c663-4db7-b91c-6bcef8418a4
d" TYPE="swap"
// /dev/sdb1: LABEL="test-ext3" UUID="f218c3b8-237e-4fbe-92c5-76623bb a4062" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="71b3e059-30c5-492e-a526-9251dff7bbe b" // /dev/sdb1: LABEL="test-ext3" UUID="f218c3b8-237e-4fbe-92c5-76623bb a4062" SEC_TYPE="ext2" TYPE="ext3" PARTUUID="71b3e059-30c5-492e-a526-9251dff7bbe b"
// /dev/sdb2: SEC_TYPE="msdos" LABEL="TEST-FAT16" UUID="9F87-1061" TY PE="vfat" PARTUUID="9d07ad9a-d468-428f-9bfd-724f5efae4fb" // /dev/sdb2: SEC_TYPE="msdos" LABEL="TEST-FAT16" UUID="9F87-1061" TY PE="vfat" PARTUUID="9d07ad9a-d468-428f-9bfd-724f5efae4fb"
// /dev/sdb3: PARTUUID="bb8438e1-d9f1-45d3-9888-e990b598900d" // /dev/sdb3: PARTUUID="bb8438e1-d9f1-45d3-9888-e990b598900d"
if (! blkid_found)
return;
Glib::ustring cmd = "blkid"; Glib::ustring cmd = "blkid";
if ( path.size() ) for (unsigned int i = 0; i < paths.size(); i++)
cmd = cmd + " " + Glib::shell_quote( path ); cmd.append(" " + Glib::shell_quote(paths[i]));
Glib::ustring output; Glib::ustring output;
Glib::ustring error; Glib::ustring error;
bool loaded_entries = false; if (Utils::execute_command(cmd, output, error, true) != 0)
if ( blkid_found && return;
! Utils::execute_command( cmd, output, error, true ) )
std::vector<Glib::ustring> lines;
Utils::split(output, lines, "\n");
for (unsigned int i = 0; i < lines.size(); i++)
{ {
std::vector<Glib::ustring> lines; FS_Entry fs_entry = {BlockSpecial(), "", "", "", false, ""};
Utils::split( output, lines, "\n" ); Glib::ustring entry_path = Utils::regexp_label(lines[i], "^(.*):
for ( unsigned int i = 0 ; i < lines.size() ; i ++ ) ");
if (entry_path.length() > 0)
{ {
FS_Entry fs_entry = {BlockSpecial(), "", "", "", false, " fs_entry.path = BlockSpecial(entry_path);
"}; fs_entry.type = Utils::regexp_label(lines[i], " TYPE=
Glib::ustring entry_path = Utils::regexp_label( lines[i], \"([^\"]*)\"");
"^(.*): " ); fs_entry.sec_type = Utils::regexp_label(lines[i], " SEC_T
if ( entry_path.length() > 0 ) YPE=\"([^\"]*)\"");
{ fs_entry.uuid = Utils::regexp_label(lines[i], " UUID=
fs_entry.path = BlockSpecial( entry_path ); \"([^\"]*)\"");
fs_entry.type = Utils::regexp_label( lines[i], " fs_info_cache.push_back(fs_entry);
TYPE=\"([^\"]*)\"" );
fs_entry.sec_type = Utils::regexp_label( lines[i]
, " SEC_TYPE=\"([^\"]*)\"" );
fs_entry.uuid = Utils::regexp_label( lines[i], "
UUID=\"([^\"]*)\"" );
fs_info_cache.push_back( fs_entry );
loaded_entries = true;
}
} }
} }
return loaded_entries; return;
} }
void FS_Info::update_fs_info_cache_all_labels() void FS_Info::update_fs_info_cache_all_labels()
{ {
if ( ! blkid_found ) if ( ! blkid_found )
return; return;
// For all cache entries which are file systems but don't yet have a labe l load it // For all cache entries which are file systems but don't yet have a labe l load it
// now. // now.
for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ ) for ( unsigned int i = 0 ; i < fs_info_cache.size() ; i ++ )
 End of changes. 25 change blocks. 
123 lines changed or deleted 81 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)