VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Unix/FilesystemPath.cpp
blob: c7bfece9bf38b67c20bfb92632237797cb60fbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 Derived from source code of TrueCrypt 7.1a, which is
 Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
 by the TrueCrypt License 3.0.

 Modifications and additions to the original source code (contained in this file)
 and all other portions of this file are Copyright (c) 2013-2017 IDRIX
 and are governed by the Apache License 2.0 the full text of which is
 contained in the file License.txt included in VeraCrypt binary and source
 code distribution packages.
*/

#include "Platform/FilesystemPath.h"
#include "Platform/SystemException.h"
#include "Platform/StringConverter.h"
#include <stdio.h>
#include <sys/stat.h>

namespace VeraCrypt
{
	void FilesystemPath::Delete () const
	{
		throw_sys_sub_if (remove (string (*this).c_str()) == -1, Path);
	}

	UserId FilesystemPath::GetOwner () const
	{
		struct stat statData;
		throw_sys_if (stat (StringConverter::ToSingle (Path).c_str(), &statData) == -1);

		UserId owner;
		owner.SystemId = statData.st_uid;
		return owner;
	}

	FilesystemPathType::Enum FilesystemPath::GetType () const
	{
		// Strip trailing directory separator
		wstring path = Path;
		size_t pos = path.find_last_not_of (L'/');
		if (path.size() > 2 && pos != path.size() - 1)
			path = path.substr (0, pos + 1);

		struct stat statData;
		throw_sys_sub_if (stat (StringConverter::ToSingle (path).c_str(), &statData) != 0, Path);

		if (S_ISREG (statData.st_mode)) return FilesystemPathType::File;
		if (S_ISDIR (statData.st_mode)) return FilesystemPathType::Directory;
		if (S_ISCHR (statData.st_mode)) return FilesystemPathType::CharacterDevice;
		if (S_ISBLK (statData.st_mode)) return FilesystemPathType::BlockDevice;
		if (S_ISLNK (statData.st_mode)) return FilesystemPathType::SymbolickLink;

		return FilesystemPathType::Unknown;
	}

	FilesystemPath FilesystemPath::ToBaseName () const
	{
		wstring path = Path;
		size_t pos = path.find_last_of (L'/');

		if (pos == string::npos)
			return Path;

		return Path.substr (pos + 1);
	}

	FilesystemPath FilesystemPath::ToHostDriveOfPartition () const
	{
		DevicePath path;

#ifdef TC_LINUX

		path = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path));

#elif defined (TC_MACOSX)

		string pathStr = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path));
		path = pathStr.substr (0, pathStr.size() - 1);

#elif defined (TC_FREEBSD)

		string pathStr = StringConverter::ToSingle (Path);
		size_t p = pathStr.rfind ("s");
		if (p == string::npos)
			throw PartitionDeviceRequired (SRC_POS);
		path = pathStr.substr (0, p);

#elif defined (TC_SOLARIS)

		path = StringConverter::StripTrailingNumber (StringConverter::ToSingle (Path)) + "0";

#else
		throw NotImplemented (SRC_POS);
#endif
		if (!path.IsDevice())
			throw PartitionDeviceRequired (SRC_POS);

		return path;
	}
}
> c mov cx, 1 mov ah, 0xa int 0x10 } } void Print (const char *str) { char c; while (c = *str++) PrintChar (c); } void Print (uint32 number) { char str[12]; int pos = 0; while (number >= 10) { str[pos++] = (char) (number % 10) + '0'; number /= 10; } str[pos] = (char) (number % 10) + '0'; while (pos >= 0) PrintChar (str[pos--]); } void Print (const uint64 &number) { if (number.HighPart == 0) Print (number.LowPart); else PrintHex (number); } void PrintHex (byte b) { PrintChar (((b >> 4) >= 0xA ? 'A' - 0xA : '0') + (b >> 4)); PrintChar (((b & 0xF) >= 0xA ? 'A' - 0xA : '0') + (b & 0xF)); } void PrintHex (uint16 data) { PrintHex (byte (data >> 8)); PrintHex (byte (data)); } void PrintHex (uint32 data) { PrintHex (uint16 (data >> 16)); PrintHex (uint16 (data)); } void PrintHex (const uint64 &data) { PrintHex (data.HighPart); PrintHex (data.LowPart); } void PrintRepeatedChar (char c, int n) { while (n-- > 0) PrintChar (c); } void PrintEndl () { Print ("\r\n"); } void PrintEndl (int cnt) { while (cnt-- > 0) PrintEndl (); } void Beep () { PrintChar (7); } void InitVideoMode () { if (ScreenOutputDisabled) return; __asm { // Text mode 80x25 mov ax, 3 int 0x10 // Page 0 mov ax, 0x500 int 0x10 } } void ClearScreen () { if (ScreenOutputDisabled) return; __asm { // White text on black mov bh, 7 xor cx, cx mov dx, 0x184f mov ax, 0x600 int 0x10 // Cursor at 0,0 xor bh, bh xor dx, dx mov ah, 2 int 0x10 } } void PrintBackspace () { PrintChar (TC_BIOS_CHAR_BACKSPACE); PrintCharAtCursor (' '); } void PrintError (const char *message) { Print (TC_BOOT_STR_ERROR); Print (message); PrintEndl(); Beep(); } void PrintErrorNoEndl (const char *message) { Print (TC_BOOT_STR_ERROR); Print (message); Beep(); } byte GetShiftFlags () { byte flags; __asm { mov ah, 2 int 0x16 mov flags, al } return flags; } byte GetKeyboardChar () { return GetKeyboardChar (nullptr); } /* inline void Sleep () { __asm { mov al, 0 mov ah, 0x86 // Sleep for 250 milliseconds = 250 000 microseconds = 0x0003D090 mov cx, 0x0003 mov dx, 0xD090 int 0x15 } } */ byte GetKeyboardChar (byte *scanCode) { // Work around potential BIOS bugs (Windows boot manager polls the keystroke buffer) while (!IsKeyboardCharAvailable()) { // reduce CPU usage by halting CPU until the next external interrupt is fired __asm { hlt } } byte asciiCode; byte scan; __asm { mov ah, 0 int 0x16 mov asciiCode, al mov scan, ah } if (scanCode) *scanCode = scan; return asciiCode; } bool IsKeyboardCharAvailable () { bool available = false; __asm { mov ah, 1 int 0x16 jz not_avail mov available, true not_avail: } return available; } bool EscKeyPressed () { if (IsKeyboardCharAvailable ()) { byte keyScanCode; GetKeyboardChar (&keyScanCode); return keyScanCode == TC_BIOS_KEY_ESC; } return false; } void ClearBiosKeystrokeBuffer () { __asm { push es xor ax, ax mov es, ax mov di, 0x41e mov cx, 32 cld rep stosb // reset position pointers at 0x41A and 0x41C to the begining // of keyboard buffer to avoid revealing password/PIM length mov ax, 0x001e mov es:[0x41a], ax mov es:[0x41c], ax pop es } } bool IsPrintable (char c) { return c >= ' ' && c <= '~'; } bool IsDigit (char c) { return c >= '0' && c <= '9'; } int GetString (char *buffer, size_t bufferSize) { byte c; byte scanCode; size_t pos = 0; while (pos < bufferSize) { c = GetKeyboardChar (&scanCode); if (scanCode == TC_BIOS_KEY_ENTER) break; if (scanCode == TC_BIOS_KEY_ESC) return 0; buffer[pos++] = c; PrintChar (IsPrintable (c) ? c : ' '); } return pos; }