|
|
1.1 root 1: /*
2: Copyright (c) 2008 TrueCrypt Foundation. All rights reserved.
3:
4: Governed by the TrueCrypt License 2.4 the full text of which is contained
5: in the file License.txt included in TrueCrypt binary and source code
6: distribution packages.
7: */
8:
9: # define FSEEK _fseeki64
10: # define FTELL _ftelli64
11:
12: #include <stdio.h>
13: #include "File.h"
14:
15: namespace TrueCrypt
16: {
17:
18: void File::Close ()
19: {
20: if_debug (ValidateState ());
21:
22: if (!SharedHandle)
23: fclose (FileHandle);
24: FileIsOpen = false;
25: }
26:
27: void File::Delete ()
28: {
29: Close();
30: Path.Delete();
31: }
32:
33: uint32 File::GetDeviceSectorSize () const
34: {
35: return 512;
36: }
37:
38: void File::Flush () const
39: {
40: if_debug (ValidateState ());
41: throw_sys_sub_if (fflush (FileHandle) != 0, wstring (Path));
42: }
43:
44: uint64 File::Length () const
45: {
46: uint64 current = FTELL (FileHandle);
47: SeekEnd (0);
48: uint64 length = FTELL (FileHandle);
49: SeekAt (current);
50: return length;
51: }
52:
53: void File::Open (const FilePath &path, FileOpenMode mode, FileShareMode shareMode, FileOpenFlags flags)
54: {
55: mFileOpenFlags = flags;
56:
57: const wchar_t *fMode;
58:
59: switch (mode)
60: {
61: case CreateReadWrite:
62: fMode = L"w+b";
63: break;
64:
65: case CreateWrite:
66: fMode = L"wb";
67: break;
68:
69: case OpenRead:
70: fMode = L"rb";
71: break;
72:
73: case OpenWrite:
74: case OpenReadWrite:
75: fMode = L"r+b";
76: break;
77:
78: default:
79: throw ParameterIncorrect (SRC_POS);
80: }
81:
82: FileHandle = _wfopen (wstring (path).c_str(), fMode);
83: throw_sys_sub_if (FileHandle == nullptr, wstring (path));
84:
85: Path = path;
86: FileIsOpen = true;
87: }
88:
89: uint64 File::Read (const BufferPtr &buffer) const
90: {
91: if_debug (ValidateState ());
92:
93: uint64 bytesRead = fread (buffer, 1, buffer.Size(), FileHandle);
94: throw_sys_sub_if (ferror (FileHandle) != 0, wstring (Path));
95:
96: return bytesRead;
97: }
98:
99: uint64 File::ReadAt (const BufferPtr &buffer, uint64 position) const
100: {
101: if_debug (ValidateState ());
102:
103: SeekAt (position);
104: return Read (buffer);
105: }
106:
107: void File::SeekAt (uint64 position) const
108: {
109: if_debug (ValidateState());
110: throw_sys_sub_if (FSEEK (FileHandle, position, SEEK_SET) != 0, wstring (Path));
111: }
112:
113: void File::SeekEnd (int offset) const
114: {
115: if_debug (ValidateState ());
116: throw_sys_sub_if (FSEEK (FileHandle, offset, SEEK_END) != 0, wstring (Path));
117: }
118:
119: void File::Write (const ConstBufferPtr &buffer) const
120: {
121: if_debug (ValidateState ());
122: uint64 bytesWritten = fwrite (buffer, 1, buffer.Size(), FileHandle);
123: throw_sys_sub_if (bytesWritten != buffer.Size() || ferror (FileHandle) != 0, wstring (Path));
124: }
125:
126: void File::WriteAt (const ConstBufferPtr &buffer, uint64 position) const
127: {
128: if_debug (ValidateState ());
129: SeekAt (position);
130: Write (buffer);
131: }
132: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.