VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Main/TextUserInterface.cpp
diff options
context:
space:
mode:
authorMounir IDRASSI <mounir.idrassi@idrix.fr>2021-09-21 01:36:47 +0200
committerMounir IDRASSI <mounir.idrassi@idrix.fr>2021-09-21 01:41:25 +0200
commit5e547b127f25b0d4f526fdb4126b95c4ba03c4b2 (patch)
treed44d9d200f8c8040bab0a6424e78b68596bc935b /src/Main/TextUserInterface.cpp
parent635213c8264c34e65b6711694e0b2cfd7fa6a078 (diff)
downloadVeraCrypt-5e547b127f25b0d4f526fdb4126b95c4ba03c4b2.tar.gz
VeraCrypt-5e547b127f25b0d4f526fdb4126b95c4ba03c4b2.zip
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.
Diffstat (limited to 'src/Main/TextUserInterface.cpp')
-rw-r--r--src/Main/TextUserInterface.cpp102
1 files changed, 72 insertions, 30 deletions
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)
{