diff options
Diffstat (limited to 'src/Platform/Unix/FilesystemPath.cpp')
-rw-r--r-- | src/Platform/Unix/FilesystemPath.cpp | 35 |
1 files changed, 32 insertions, 3 deletions
diff --git a/src/Platform/Unix/FilesystemPath.cpp b/src/Platform/Unix/FilesystemPath.cpp index f5d59f9f..1230c2aa 100644 --- a/src/Platform/Unix/FilesystemPath.cpp +++ b/src/Platform/Unix/FilesystemPath.cpp @@ -3,9 +3,9 @@ Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed by the TrueCrypt License 3.0. Modifications and additions to the original source code (contained in this file) - and all other portions of this file are Copyright (c) 2013-2016 IDRIX + and all other portions of this file are Copyright (c) 2013-2017 IDRIX and are governed by the Apache License 2.0 the full text of which is contained in the file License.txt included in VeraCrypt binary and source code distribution packages. */ @@ -14,8 +14,11 @@ #include "Platform/SystemException.h" #include "Platform/StringConverter.h" #include <stdio.h> #include <sys/stat.h> +#if !defined(__FreeBSD__) && !defined(__APPLE__) && !defined(__OpenBSD__) +#include <sys/sysmacros.h> +#endif namespace VeraCrypt { void FilesystemPath::Delete () const @@ -71,8 +74,31 @@ namespace VeraCrypt #ifdef TC_LINUX path = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path)); + // If simply removing trailing number didn't produce a valid drive name, try to use sysfs to get the right one + if (!path.IsDevice()) { + struct stat st; + + if(stat (StringConverter::ToSingle (Path).c_str (), &st) == 0) { + const long maxPathLength = pathconf ("/", _PC_PATH_MAX); + + if(maxPathLength != -1) { + string linkPathName ("/sys/dev/block/"); + linkPathName += StringConverter::ToSingle (major (st.st_rdev)) + string (":") + StringConverter::ToSingle (minor (st.st_rdev)); + + vector<char> linkTargetPath(maxPathLength+1); + + if(readlink(linkPathName.c_str (), linkTargetPath.data(), linkTargetPath.size()) != -1) { + const string targetPathStr (linkTargetPath.data()); + const size_t lastSlashPos = targetPathStr.find_last_of ('/'); + const size_t secondLastSlashPos = targetPathStr.find_last_of ('/', lastSlashPos-1); + path = string ("/dev/") + targetPathStr.substr (secondLastSlashPos+1, lastSlashPos-secondLastSlashPos-1); + } + } + } + } + #elif defined (TC_MACOSX) string pathStr = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path)); path = pathStr.substr (0, pathStr.size() - 1); @@ -80,10 +106,13 @@ namespace VeraCrypt #elif defined (TC_FREEBSD) string pathStr = StringConverter::ToSingle (Path); size_t p = pathStr.rfind ("s"); - if (p == string::npos) - throw PartitionDeviceRequired (SRC_POS); + if (p == string::npos) { + p = pathStr.rfind ("p"); + if (p == string::npos) + throw PartitionDeviceRequired (SRC_POS); + } path = pathStr.substr (0, p); #elif defined (TC_SOLARIS) |