"Fossies" - the Fresh Open Source Software Archive

Member "src/Core/Unix/Solaris/CoreSolaris.cpp" (10 Oct 2018, 4505 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 "CoreSolaris.cpp" see the Fossies "Dox" file reference documentation.

    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 <stdio.h>
   14 #include <unistd.h>
   15 #include <sys/types.h>
   16 #include <sys/mount.h>
   17 #include <sys/mntent.h>
   18 #include <sys/mnttab.h>
   19 #include "CoreSolaris.h"
   20 #include "Core/Unix/CoreServiceProxy.h"
   21 
   22 namespace VeraCrypt
   23 {
   24     CoreSolaris::CoreSolaris ()
   25     {
   26     }
   27 
   28     CoreSolaris::~CoreSolaris ()
   29     {
   30     }
   31 
   32     DevicePath CoreSolaris::AttachFileToLoopDevice (const FilePath &filePath, bool readOnly) const
   33     {
   34         list <string> args;
   35         args.push_back ("-a");
   36         args.push_back (filePath);
   37 
   38         return StringConverter::Trim (Process::Execute ("lofiadm", args));
   39     }
   40 
   41     void CoreSolaris::DetachLoopDevice (const DevicePath &devicePath) const
   42     {
   43         list <string> args;
   44         args.push_back ("-d");
   45         args.push_back (devicePath);
   46 
   47         for (int t = 0; true; t++)
   48         {
   49             try
   50             {
   51                 Process::Execute ("lofiadm", args);
   52                 break;
   53             }
   54             catch (ExecutedProcessFailed&)
   55             {
   56                 if (t > 5)
   57                     throw;
   58                 Thread::Sleep (200);
   59             }
   60         }
   61     }
   62 
   63     HostDeviceList CoreSolaris::GetHostDevices (bool pathListOnly) const
   64     {
   65         HostDeviceList devices;
   66 
   67         foreach_ref (const FilePath &devPath, Directory::GetFilePaths ("/dev/rdsk", false))
   68         {
   69             string drivePath = devPath;
   70             if (drivePath.rfind ("p0") == drivePath.size() - 2)
   71             {
   72                 make_shared_auto (HostDevice, device);
   73                 device->Path = drivePath;
   74 
   75                 try
   76                 {
   77                     device->Size = GetDeviceSize (device->Path);
   78                 }
   79                 catch (...)
   80                 {
   81                     device->Size = 0;
   82                 }
   83 
   84                 if (device->Size == 0)
   85                     continue;
   86 
   87                 device->MountPoint = GetDeviceMountPoint (device->Path);
   88                 device->SystemNumber = 0;
   89 
   90                 devices.push_back (device);
   91 
   92                 for (int partNumber = 1; partNumber <= 32; partNumber++)
   93                 {
   94                     stringstream partPath;
   95                     partPath << drivePath.substr (0, drivePath.size() - 1) << partNumber;
   96 
   97                     if (FilesystemPath (partPath.str()).IsBlockDevice() || FilesystemPath (partPath.str()).IsCharacterDevice())
   98                     {
   99                         make_shared_auto (HostDevice, partition);
  100                         partition->Path = partPath.str();
  101 
  102                         try
  103                         {
  104                             partition->Size = GetDeviceSize (partition->Path);
  105                         }
  106                         catch (...)
  107                         {
  108                             partition->Size = 0;
  109                         }
  110 
  111                         if (partition->Size == 0)
  112                             continue;
  113 
  114                         partition->MountPoint = GetDeviceMountPoint (partition->Path);
  115                         partition->SystemNumber = 0;
  116 
  117                         device->Partitions.push_back (partition);
  118                     }
  119                 }
  120             }
  121         }
  122 
  123         return devices;
  124     }
  125 
  126     MountedFilesystemList CoreSolaris::GetMountedFilesystems (const DevicePath &devicePath, const DirectoryPath &mountPoint) const
  127     {
  128         MountedFilesystemList mountedFilesystems;
  129 
  130         FILE *mtab = fopen ("/etc/mnttab", "r");
  131         throw_sys_sub_if (!mtab, "/etc/mnttab");
  132         finally_do_arg (FILE *, mtab, { fclose (finally_arg); });
  133 
  134         int getmntentResult;
  135         struct mnttab entry;
  136         while ((getmntentResult = getmntent (mtab, &entry)) == 0)
  137         {
  138             make_shared_auto (MountedFilesystem, mf);
  139 
  140             if (entry.mnt_special)
  141                 mf->Device = DevicePath (entry.mnt_special);
  142             else
  143                 continue;
  144 
  145             if (entry.mnt_mountp)
  146                 mf->MountPoint = DirectoryPath (entry.mnt_mountp);
  147 
  148             if (entry.mnt_fstype)
  149                 mf->Type = entry.mnt_fstype;
  150 
  151             if ((devicePath.IsEmpty() || devicePath == mf->Device) && (mountPoint.IsEmpty() || mountPoint == mf->MountPoint))
  152                 mountedFilesystems.push_back (mf);
  153         }
  154 
  155         throw_sys_if (getmntentResult > 0);
  156 
  157         return mountedFilesystems;
  158     }
  159 
  160     void CoreSolaris::MountFilesystem (const DevicePath &devicePath, const DirectoryPath &mountPoint, const string &filesystemType, bool readOnly, const string &systemMountOptions) const
  161     {
  162         try
  163         {
  164             // Try to mount FAT by default as mount is unable to probe filesystem type on Solaris
  165             CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType.empty() ? "pcfs" : filesystemType, readOnly, systemMountOptions);
  166         }
  167         catch (ExecutedProcessFailed&)
  168         {
  169             if (!filesystemType.empty())
  170                 throw;
  171 
  172             CoreUnix::MountFilesystem (devicePath, mountPoint, filesystemType, readOnly, systemMountOptions);
  173         }
  174     }
  175 
  176     auto_ptr <CoreBase> Core (new CoreServiceProxy <CoreSolaris>);
  177     auto_ptr <CoreBase> CoreDirect (new CoreSolaris);
  178 }