From 5e547b127f25b0d4f526fdb4126b95c4ba03c4b2 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Tue, 21 Sep 2021 01:36:47 +0200 Subject: Linux/macOS: Add CLI switch (--size=max) and UI option to give a file container all available free space. This commit also makes --size switch accept KiB/MiB/GiB/TiB prefixes and adds TiB choice in UI. --- src/Main/TextUserInterface.cpp | 102 +++++++++++++++++++++++++++++------------ 1 file changed, 72 insertions(+), 30 deletions(-) (limited to 'src/Main/TextUserInterface.cpp') diff --git a/src/Main/TextUserInterface.cpp b/src/Main/TextUserInterface.cpp index 2cacbb1b..7233b8b6 100644 --- a/src/Main/TextUserInterface.cpp +++ b/src/Main/TextUserInterface.cpp @@ -646,6 +646,29 @@ namespace VeraCrypt } else { + uint64 AvailableDiskSpace = 0; + wxLongLong diskSpace = 0; + if (wxGetDiskSpace (wxFileName (wstring (options->Path)).GetPath(), nullptr, &diskSpace)) + { + AvailableDiskSpace = (uint64) diskSpace.GetValue (); + if (maxVolumeSize > AvailableDiskSpace) + maxVolumeSize = AvailableDiskSpace; + } + + if (options->Size == (uint64) (-1)) + { + if (AvailableDiskSpace) + { + // caller requesting maximum size + // we use maxVolumeSize because it is guaranteed to be less of equal to AvailableDiskSpace + options->Size = maxVolumeSize; + } + else + { + throw_err (_("Failed to get available disk space on the selected target.")); + } + } + options->Quick = false; uint32 sectorSizeRem = options->Size % options->SectorSize; @@ -657,43 +680,62 @@ namespace VeraCrypt if (Preferences.NonInteractive) throw MissingArgument (SRC_POS); - wstring sizeStr = AskString (options->Type == VolumeType::Hidden ? _("\nEnter hidden volume size (sizeK/size[M]/sizeG): ") : _("\nEnter volume size (sizeK/size[M]/sizeG): ")); uint64 multiplier = 1024 * 1024; - - if (sizeStr.find (L"K") != string::npos) - { - multiplier = 1024; - sizeStr.resize (sizeStr.size() - 1); - } - else if (sizeStr.find (L"M") != string::npos) + wxString sizeStr = AskString (options->Type == VolumeType::Hidden ? _("\nEnter hidden volume size (sizeK/size[M]/sizeG/sizeT/max): ") : _("\nEnter volume size (sizeK/size[M]/sizeG.sizeT/max): ")); + if (sizeStr.CmpNoCase(wxT("max")) == 0) { - sizeStr.resize (sizeStr.size() - 1); - } - else if (sizeStr.find (L"G") != string::npos) - { - multiplier = 1024 * 1024 * 1024; - sizeStr.resize (sizeStr.size() - 1); + multiplier = 1; + if (AvailableDiskSpace) + { + // caller requesting maximum size + // we use maxVolumeSize because it is guaranteed to be less of equal to AvailableDiskSpace + options->Size = maxVolumeSize; + } + else + { + throw_err (_("Failed to get available disk space on the selected target.")); + } } - else if (sizeStr.find (L"T") != string::npos) + else { - multiplier = (uint64) 1024 * 1024 * 1024 * 1024; - sizeStr.resize (sizeStr.size() - 1); - } + multiplier = 1024 * 1024; + size_t index = sizeStr.find_first_not_of (wxT("0123456789")); + if (index == 0) + { + continue; + } + else if (index != (size_t) wxNOT_FOUND) + { + wxString sizeSuffix = sizeStr.Mid(index); + if (sizeSuffix.CmpNoCase(wxT("K")) == 0 || sizeSuffix.CmpNoCase(wxT("KiB")) == 0) + multiplier = BYTES_PER_KB; + else if (sizeSuffix.CmpNoCase(wxT("M")) == 0 || sizeSuffix.CmpNoCase(wxT("MiB")) == 0) + multiplier = BYTES_PER_MB; + else if (sizeSuffix.CmpNoCase(wxT("G")) == 0 || sizeSuffix.CmpNoCase(wxT("GiB")) == 0) + multiplier = BYTES_PER_GB; + else if (sizeSuffix.CmpNoCase(wxT("T")) == 0 || sizeSuffix.CmpNoCase(wxT("TiB")) == 0) + multiplier = BYTES_PER_TB; + else + continue; - try - { - options->Size = StringConverter::ToUInt64 (sizeStr); - options->Size *= multiplier; + sizeStr = sizeStr.Left (index); + } - sectorSizeRem = options->Size % options->SectorSize; - if (sectorSizeRem != 0) - options->Size += options->SectorSize - sectorSizeRem; - } - catch (...) - { - options->Size = 0; - continue; + try + { + options->Size = StringConverter::ToUInt64 (wstring(sizeStr)); + } + catch (...) + { + options->Size = 0; + continue; + } } + options->Size *= multiplier; + + sectorSizeRem = options->Size % options->SectorSize; + if (sectorSizeRem != 0) + options->Size += options->SectorSize - sectorSizeRem; if (options->Size < minVolumeSize) { -- cgit v1.2.3