"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Unix/FilesystemPath.cpp" (10 Oct 2018, 3902 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "FilesystemPath.cpp" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.22_Source_vs_1.23_Source.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #include "Platform/FilesystemPath.h"
   14 #include "Platform/SystemException.h"
   15 #include "Platform/StringConverter.h"
   16 #include <stdio.h>
   17 #include <sys/stat.h>
   18 #if !defined(__FreeBSD__) && !defined(__APPLE__)
   19 #include <sys/sysmacros.h>
   20 #endif
   21 
   22 namespace VeraCrypt
   23 {
   24     void FilesystemPath::Delete () const
   25     {
   26         throw_sys_sub_if (remove (string (*this).c_str()) == -1, Path);
   27     }
   28 
   29     UserId FilesystemPath::GetOwner () const
   30     {
   31         struct stat statData;
   32         throw_sys_if (stat (StringConverter::ToSingle (Path).c_str(), &statData) == -1);
   33 
   34         UserId owner;
   35         owner.SystemId = statData.st_uid;
   36         return owner;
   37     }
   38 
   39     FilesystemPathType::Enum FilesystemPath::GetType () const
   40     {
   41         // Strip trailing directory separator
   42         wstring path = Path;
   43         size_t pos = path.find_last_not_of (L'/');
   44         if (path.size() > 2 && pos != path.size() - 1)
   45             path = path.substr (0, pos + 1);
   46 
   47         struct stat statData;
   48         throw_sys_sub_if (stat (StringConverter::ToSingle (path).c_str(), &statData) != 0, Path);
   49 
   50         if (S_ISREG (statData.st_mode)) return FilesystemPathType::File;
   51         if (S_ISDIR (statData.st_mode)) return FilesystemPathType::Directory;
   52         if (S_ISCHR (statData.st_mode)) return FilesystemPathType::CharacterDevice;
   53         if (S_ISBLK (statData.st_mode)) return FilesystemPathType::BlockDevice;
   54         if (S_ISLNK (statData.st_mode)) return FilesystemPathType::SymbolickLink;
   55 
   56         return FilesystemPathType::Unknown;
   57     }
   58 
   59     FilesystemPath FilesystemPath::ToBaseName () const
   60     {
   61         wstring path = Path;
   62         size_t pos = path.find_last_of (L'/');
   63 
   64         if (pos == string::npos)
   65             return Path;
   66 
   67         return Path.substr (pos + 1);
   68     }
   69 
   70     FilesystemPath FilesystemPath::ToHostDriveOfPartition () const
   71     {
   72         DevicePath path;
   73 
   74 #ifdef TC_LINUX
   75 
   76         path = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path));
   77 
   78         // If simply removing trailing number didn't produce a valid drive name, try to use sysfs to get the right one
   79         if (!path.IsDevice()) {
   80             struct stat st;
   81 
   82             if(stat (StringConverter::ToSingle (Path).c_str (), &st) == 0) {
   83                 const long maxPathLength = pathconf ("/", _PC_PATH_MAX);
   84 
   85                 if(maxPathLength != -1) {
   86                     string linkPathName ("/sys/dev/block/");
   87                     linkPathName += StringConverter::ToSingle (major (st.st_rdev)) + string (":") + StringConverter::ToSingle (minor (st.st_rdev));
   88 
   89                     vector<char> linkTargetPath(maxPathLength+1);
   90 
   91                     if(readlink(linkPathName.c_str (), linkTargetPath.data(), linkTargetPath.size()) != -1) {
   92                         const string targetPathStr (linkTargetPath.data());
   93                         const size_t lastSlashPos = targetPathStr.find_last_of ('/');
   94                         const size_t secondLastSlashPos = targetPathStr.find_last_of ('/', lastSlashPos-1);
   95                         path = string ("/dev/") + targetPathStr.substr (secondLastSlashPos+1, lastSlashPos-secondLastSlashPos-1);
   96                     }
   97                 }
   98             }
   99         }
  100 
  101 #elif defined (TC_MACOSX)
  102 
  103         string pathStr = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path));
  104         path = pathStr.substr (0, pathStr.size() - 1);
  105 
  106 #elif defined (TC_FREEBSD)
  107 
  108         string pathStr = StringConverter::ToSingle (Path);
  109         size_t p = pathStr.rfind ("s");
  110         if (p == string::npos)
  111             throw PartitionDeviceRequired (SRC_POS);
  112         path = pathStr.substr (0, p);
  113 
  114 #elif defined (TC_SOLARIS)
  115 
  116         path = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path)) + "0";
  117 
  118 #else
  119         throw NotImplemented (SRC_POS);
  120 #endif
  121         if (!path.IsDevice())
  122             throw PartitionDeviceRequired (SRC_POS);
  123 
  124         return path;
  125     }
  126 }