|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
1.1.1.4 ! root 4: Governed by the TrueCrypt License 2.7 the full text of which is contained
1.1 root 5: in the file License.txt included in TrueCrypt binary and source code
6: distribution 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: {
1.1.1.3 root 21: try
22: {
23: if (FileIsOpen)
24: Close();
25: }
26: catch (...) { }
1.1 root 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 archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.