gparted/include/Utils.h

224 lines
7.4 KiB
C
Raw Normal View History

/* Copyright (C) 2004 Bart
* Copyright (C) 2008, 2009, 2010, 2011, 2012 Curtis Gedak
2004-10-06 09:32:40 -06:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
2004-10-06 09:32:40 -06:00
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
2004-10-06 09:32:40 -06:00
*/
/* UTILS
* Some stuff i need in a lot of places so i dropped in all together in one file.
*/
#ifndef GPARTED_UTILS_H
#define GPARTED_UTILS_H
2004-10-06 09:32:40 -06:00
#include "../include/i18n.h"
2004-10-06 09:32:40 -06:00
#include <gtkmm/label.h>
#include <glibmm/ustring.h>
#include <iostream>
#include <ctime>
#include <vector>
2004-10-06 09:32:40 -06:00
#define UUID_STRING_LENGTH 36
//Match RFC 4122 UUID strings. Exclude Nil UUID (all zeros) by excluding
// zero from the version field nibble.
#define RFC4122_NONE_NIL_UUID_REGEXP "[[:xdigit:]]{8}-[[:xdigit:]]{4}-[1-9a-fA-F][[:xdigit:]]{3}-[[:xdigit:]]{4}-[[:xdigit:]]{12}"
2004-10-06 09:32:40 -06:00
namespace GParted
{
typedef long long Sector;
typedef long long Byte_Value;
2004-10-06 09:32:40 -06:00
//Size units defined in bytes
const Byte_Value KIBIBYTE=1024;
const Byte_Value MEBIBYTE=(KIBIBYTE * KIBIBYTE);
const Byte_Value GIBIBYTE=(MEBIBYTE * KIBIBYTE);
const Byte_Value TEBIBYTE=(GIBIBYTE * KIBIBYTE);
const Byte_Value PEBIBYTE=(TEBIBYTE * KIBIBYTE);
const Byte_Value EXBIBYTE=(PEBIBYTE * KIBIBYTE);
const Glib::ustring UUID_RANDOM = _("(New UUID - will be randomly generated)") ;
const Glib::ustring UUID_RANDOM_NTFS_HALF = _("(Half new UUID - will be randomly generated)") ;
extern const Glib::ustring DEV_MAPPER_PATH;
enum FILESYSTEM
{
// Special partition types and functions
FS_UNALLOCATED = 0,
FS_UNKNOWN = 1,
FS_UNFORMATTED = 2,
FS_CLEARED = 3, //Clear existing file system signatures
FS_EXTENDED = 4,
// Supported file system types
FS_BTRFS = 5,
FS_EXFAT = 6, /* Also known as fat64 */
FS_EXT2 = 7,
FS_EXT3 = 8,
FS_EXT4 = 9,
FS_F2FS = 10,
FS_FAT16 = 11,
FS_FAT32 = 12,
FS_HFS = 13,
FS_HFSPLUS = 14,
FS_JFS = 15,
FS_LINUX_SWAP = 16,
FS_LUKS = 17,
FS_LVM2_PV = 18,
FS_NILFS2 = 19,
FS_NTFS = 20,
FS_REISER4 = 21,
FS_REISERFS = 22,
FS_UFS = 23,
FS_XFS = 24,
// Recognised signatures but otherwise unsupported file system types
FS_BITLOCKER = 25,
FS_GRUB2_CORE_IMG = 26,
FS_LINUX_SWRAID = 27,
FS_LINUX_SWSUSPEND = 28,
FS_REFS = 29,
FS_ZFS = 30,
// Partition space usage colours
FS_USED = 31,
FS_UNUSED = 32
} ;
2004-10-06 09:32:40 -06:00
enum SIZE_UNIT
{
UNIT_SECTOR = 0,
UNIT_BYTE = 1,
UNIT_KIB = 2,
UNIT_MIB = 3,
UNIT_GIB = 4,
UNIT_TIB = 5
} ;
enum CUSTOM_TEXT
{
CTEXT_NONE,
CTEXT_ACTIVATE_FILESYSTEM, // Activate text ('Mount', 'Swapon', VG 'Activate', ...)
CTEXT_DEACTIVATE_FILESYSTEM, // Deactivate text ('Unmount', 'Swapoff', VG 'Deactivate', ...)
CTEXT_CHANGE_UUID_WARNING, // Warning to print when changing UUIDs
CTEXT_RESIZE_DISALLOWED_WARNING // File system resizing currently disallowed reason
} ;
//struct to store file system information
2004-10-06 09:32:40 -06:00
struct FS
{
enum Support
{
NONE = 0,
GPARTED = 1,
LIBPARTED = 2,
EXTERNAL = 3
};
FILESYSTEM filesystem ;
Support busy ; //How to determine if partition/file system is busy
Support read ; //Can and how to read sector usage while inactive
Support read_label ;
Support write_label ;
Support read_uuid ;
Support write_uuid ;
Support create ;
Support create_with_label ; //Can and how to format file system with label
Support grow ;
Support shrink ;
Support move ; //startpoint and endpoint
Support check ; //some checktool available?
Support copy ;
Support remove ;
Support online_read ; //Can and how to read sector usage while active
Support online_grow ;
Support online_shrink ;
Byte_Value MIN ;
Byte_Value MAX ;
FS( FILESYSTEM fstype = FS_UNKNOWN ) : filesystem( fstype )
{
busy = read = read_label = write_label = read_uuid = write_uuid = create =
create_with_label = grow = shrink = move = check = copy = remove = online_read =
online_grow = online_shrink = NONE ;
MIN = MAX = 0 ;
}
} ;
2004-10-06 09:32:40 -06:00
class Utils
2004-10-06 09:32:40 -06:00
{
public:
2006-03-28 05:40:29 -07:00
static Sector round( double double_value ) ;
static Gtk::Label * mk_label( const Glib::ustring & text
, bool use_markup = true
, bool wrap = false
, bool selectable = false
, float yalign = 0.5 /* ALIGN_CENTER */
) ;
static Glib::ustring num_to_str( Sector number ) ;
2006-03-28 05:40:29 -07:00
static Glib::ustring get_color( FILESYSTEM filesystem ) ;
static Glib::RefPtr<Gdk::Pixbuf> get_color_as_pixbuf( FILESYSTEM filesystem, int width, int height ) ;
static int get_max_partition_name_length( Glib::ustring & tabletype );
static int get_filesystem_label_maxlength( FILESYSTEM filesystem ) ;
2006-03-28 05:40:29 -07:00
static Glib::ustring get_filesystem_string( FILESYSTEM filesystem ) ;
static const Glib::ustring get_filesystem_kernel_name( FILESYSTEM fstype );
static Glib::ustring get_filesystem_software( FILESYSTEM filesystem ) ;
static bool kernel_supports_fs( const Glib::ustring & fs ) ;
static bool kernel_version_at_least( int major_ver, int minor_ver, int patch_ver ) ;
static Glib::ustring format_size( Sector sectors, Byte_Value sector_size ) ;
static Glib::ustring format_time( std::time_t seconds ) ;
static double sector_to_unit( Sector sectors, Byte_Value sector_size, SIZE_UNIT size_unit ) ;
static int execute_command( const Glib::ustring & command ) ;
static int execute_command( const Glib::ustring & command,
Glib::ustring & output,
Glib::ustring & error,
bool use_C_locale = false ) ;
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-06 07:39:07 -06:00
static int get_failure_status( Glib::SpawnError & e );
static int decode_wait_status( int wait_status );
static Glib::ustring regexp_label( const Glib::ustring & text
, const Glib::ustring & pattern
) ;
static Glib::ustring trim( const Glib::ustring & src, const Glib::ustring & c = " \t\r\n" ) ;
static Glib::ustring last_line( const Glib::ustring & src );
static Glib::ustring get_lang() ;
static void tokenize( const Glib::ustring& str,
std::vector<Glib::ustring>& tokens,
const Glib::ustring& delimiters ) ;
static void split( const Glib::ustring& str,
std::vector<Glib::ustring>& result,
const Glib::ustring& delimiters ) ;
static int convert_to_int(const Glib::ustring & src);
static Glib::ustring generate_uuid(void);
Read file system size for mounted ext2/3/4 from superblock (#683255) A user had a 190 MiB partition containing an old ext2 file system. When unmounted it was reported as filling the partition, but when mounted it was reported as having 6% unallocated space. The file system's inode tables were approximately twice the size of those created by default with the current mkfs.ext2 defaults. To create an equivalent file system in a 190 MiB partition: mkfs.ext2 -N 97344 /dev/sda15 It turns out that for ext2, ext3 and ext4 file systems what was described as intrinsic unallocated space during the development of Bug #499202 is actually file system overhead. When mounted the ext2/3/4 kernel code reports the size of the file system after subtracting the overhead. Overhead is made up of superblock backups, group descriptors, allocation bitmaps and largest of all inode tables. E2fsprogs tools don't subtract this overhead when reporting the file system size. References: * The Second Extended File System, Internal Layout, by Dave Poirier http://www.nongnu.org/ext2-doc/ext2.html * Linux ext2_statfs() function http://lxr.linux.no/#linux+v3.5.3/fs/ext2/super.c#L1311 Call the file system specific method for reading the usage of ext2, ext3 and ext4 file systems while mounted. Make it read the file system size from the on disk superblock to avoid subtraction of overhead and use the statvfs() system call to return an up to date free space figure. Bug #683255 - ext2: statvfs differs from dumpe2fs (x MB unallocated space within the partition)
2012-09-12 16:51:49 -06:00
static int get_mounted_filesystem_usage( const Glib::ustring & mountpoint,
Byte_Value & fs_size, Byte_Value & fs_free,
Glib::ustring & error_message ) ;
static Byte_Value floor_size( Byte_Value value, Byte_Value rounding_size ) ;
static Byte_Value ceil_size( Byte_Value value, Byte_Value rounding_size ) ;
private:
static bool get_kernel_version( int & major_ver, int & minor_ver, int & patch_ver ) ;
};
2004-10-06 09:32:40 -06:00
}//GParted
#endif /* GPARTED_UTILS_H */