-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystemManager.cpp
121 lines (109 loc) · 2.73 KB
/
FileSystemManager.cpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <iostream>
#include "FileSystemManager.h"
#include "CommandProcessor.h"
#include "Utils.h"
namespace fs
{
enum Action { authenticate, registerUser, exit };
void FileSystemManager::run()
{
auto currentAppState = AppState::running;
while (currentAppState == AppState::running)
{
switch (getAction())
{
case authenticate:
{
if (const auto user = m_userManager.authenticate())
{
m_environments.emplace_back(user.value());
currentAppState = startFileSystem();
}
}
break;
case registerUser:
m_userManager.registerUser();
break;
case exit:
currentAppState = AppState::exiting;
break;
default:
std::clog << "Error FileSystemManager::run()\n\n";
}
}
}
FileSystemEnv* FileSystemManager::getMainEnv()
{
if (!m_environments.empty())
{
return &m_environments.front();
}
return nullptr;
}
FileSystemEnv* FileSystemManager::getEnv([[maybe_unused]] const std::string& user)
{
return nullptr; // TODO(pablo)
}
bool FileSystemManager::requestAuthentication(const std::string& user) const
{
return m_userManager.requestPassword(user);
}
AppState FileSystemManager::startFileSystem()
{
createEssentialFiles();
m_environments[0].loadUserEnv();
shell::CommandProcessor cp{ this };
const auto state = cp.run();
m_environments.clear();
return state;
}
[[nodiscard]] int FileSystemManager::getAction()
{
int choice{ -1 };
bool isValid{ false };
while (!isValid)
{
std::cout << "Please choose an option:\n"
<< "1 - Choose user\n"
<< "2 - Create new user\n"
<< "3 - Exit\n"
<< "> ";
choice = Utils::getNumber().value_or(-1) - 1;
isValid = true;
switch (choice)
{
case authenticate:
std::cout << "Choose user selected.\n\n";
break;
case registerUser:
std::cout << "Create user selected.\n\n";
break;
case exit:
std::cout << "Exit selected. Goodbye!\n\n";
break;
default:
isValid = false;
std::cout << "Invalid choice. Please try again.\n\n";
}
}
Utils::bufferSafetyCheck();
return choice;
}
void FileSystemManager::createEssentialFiles()
{
if (!Utils::checkIfDirectoryExists("./fs"))
{
if (!Utils::createDirectory("./fs"))
{
std::clog << "ERROR: failed to create fs directory\n\n";
}
}
if (!Utils::checkIfDirectoryExists("./fs/home"))
{
if (!Utils::createDirectory("./fs/home"))
{
std::clog << "ERROR: failed to create home directory\n\n";
}
}
}
} // namespace fs