Update README.md
[nemesis.git] / Platform / FileCommon.cpp
1 /*
2  Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.
3
4  Governed by the TrueCrypt License 3.0 the full text of which is contained in
5  the file License.txt included in TrueCrypt binary and source code distribution
6  packages.
7 */
8
9 #include "File.h"
10 #ifdef TC_UNIX
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <utime.h>
15 #endif
16
17 namespace TrueCrypt
18 {
19         File::~File ()
20         {
21                 try
22                 {
23                         if (FileIsOpen)
24                                 Close();
25                 }
26                 catch (...) { }
27         }
28
29         void File::Copy (const FilePath &sourcePath, const FilePath &destinationPath, bool preserveTimestamps)
30         {
31                 File source;
32                 source.Open (sourcePath);
33
34                 File destination;
35                 destination.Open (destinationPath, CreateWrite);
36
37                 SecureBuffer buffer (OptimalReadSize);
38                 uint64 len;
39
40                 while ((len = source.Read (buffer)) > 0)
41                 {
42                         destination.Write (buffer, static_cast <size_t> (len));
43                 }
44
45                 if (preserveTimestamps)
46                 {
47                         destination.Flush();
48 #ifndef TC_WINDOWS
49                         struct stat statData;
50                         throw_sys_sub_if (stat (string (sourcePath).c_str(), &statData) == -1, wstring (sourcePath));
51
52                         struct utimbuf u;
53                         u.actime = statData.st_atime;
54                         u.modtime = statData.st_mtime;
55                         throw_sys_sub_if (utime (string (destinationPath).c_str(), &u) == -1, wstring (destinationPath));
56 #endif
57                 }
58         }
59         
60         FilePath File::GetPath () const
61         {
62                 if_debug (ValidateState());
63                 return Path;
64         }
65
66         void File::ReadCompleteBuffer (const BufferPtr &buffer) const
67         {
68                 size_t dataLeft = buffer.Size();
69                 size_t offset = 0;
70
71                 while (dataLeft > 0)
72                 {
73                         size_t dataRead = static_cast <size_t> (Read (buffer.GetRange (offset, dataLeft)));
74                         if (dataRead == 0)
75                                 throw InsufficientData (SRC_POS);
76
77                         dataLeft -= dataRead;
78                         offset += dataRead;
79                 }
80         }
81         
82         void File::ValidateState () const
83         {
84                 if (!FileIsOpen)
85                         throw NotInitialized (SRC_POS);
86         }
87 }
This page took 0.031299 seconds and 3 git commands to generate. Download a nemesis OSX (sierra+high sierra, tested/working) binary, with fuse-ext3 via e2fsprogs, at this link. application and installer are signed by screwjack, llc. must install fuse with macFUSE layer first.