"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/FileCommon.cpp" (10 Oct 2018, 2060 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 "FileCommon.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 "File.h"
   14 #ifdef TC_UNIX
   15 #include <sys/types.h>
   16 #include <sys/stat.h>
   17 #include <unistd.h>
   18 #include <utime.h>
   19 #endif
   20 
   21 namespace VeraCrypt
   22 {
   23     File::~File ()
   24     {
   25         try
   26         {
   27             if (FileIsOpen)
   28                 Close();
   29         }
   30         catch (...) { }
   31     }
   32 
   33     void File::Copy (const FilePath &sourcePath, const FilePath &destinationPath, bool preserveTimestamps)
   34     {
   35         File source;
   36         source.Open (sourcePath);
   37 
   38         File destination;
   39         destination.Open (destinationPath, CreateWrite);
   40 
   41         SecureBuffer buffer (OptimalReadSize);
   42         uint64 len;
   43 
   44         while ((len = source.Read (buffer)) > 0)
   45         {
   46             destination.Write (buffer, static_cast <size_t> (len));
   47         }
   48 
   49         if (preserveTimestamps)
   50         {
   51             destination.Flush();
   52 #ifndef TC_WINDOWS
   53             struct stat statData;
   54             throw_sys_sub_if (stat (string (sourcePath).c_str(), &statData) == -1, wstring (sourcePath));
   55 
   56             struct utimbuf u;
   57             u.actime = statData.st_atime;
   58             u.modtime = statData.st_mtime;
   59             throw_sys_sub_if (utime (string (destinationPath).c_str(), &u) == -1, wstring (destinationPath));
   60 #endif
   61         }
   62     }
   63 
   64     FilePath File::GetPath () const
   65     {
   66         if_debug (ValidateState());
   67         return Path;
   68     }
   69 
   70     void File::ReadCompleteBuffer (const BufferPtr &buffer) const
   71     {
   72         size_t dataLeft = buffer.Size();
   73         size_t offset = 0;
   74 
   75         while (dataLeft > 0)
   76         {
   77             size_t dataRead = static_cast <size_t> (Read (buffer.GetRange (offset, dataLeft)));
   78             if (dataRead == 0)
   79                 throw InsufficientData (SRC_POS);
   80 
   81             dataLeft -= dataRead;
   82             offset += dataRead;
   83         }
   84     }
   85 
   86     void File::ValidateState () const
   87     {
   88         if (!FileIsOpen)
   89             throw NotInitialized (SRC_POS);
   90     }
   91 }