-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemEnv.cpp
More file actions
142 lines (126 loc) · 3.43 KB
/
FileSystemEnv.cpp
File metadata and controls
142 lines (126 loc) · 3.43 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <fstream>
#include <iostream>
#include <queue>
#include <sstream>
#include "FileSystemEnv.h"
#include "File.h"
#include "PathResolver.h"
#include "Utils.h"
namespace fs
{
const char* const FileSystemEnv::usersHomePath{ "fs/home/" };
const User* FileSystemEnv::getUser() const
{
return m_user;
}
FileSystemEnv::FileSystemEnv(const User* user) : m_user{ user }, m_rootPath{ usersHomePath + m_user->getUsername() }
{
}
FileSystemEnv::~FileSystemEnv()
{
saveUserEnv();
}
FileSystemEnv::FileSystemEnv(FileSystemEnv&& other) noexcept
{
m_root = std::move(other.m_root);
m_user = other.m_user;
other.m_user = nullptr;
}
FileSystemEnv& FileSystemEnv::operator=(FileSystemEnv&& other) noexcept
{
m_root = std::move(other.m_root);
m_user = other.m_user;
other.m_user = nullptr;
return *this;
}
void FileSystemEnv::loadUserEnv()
{
if (!Utils::checkIfDirectoryExists(m_rootPath))
{
if (!Utils::createDirectory(m_rootPath))
{
std::clog << "ERROR: failed to create user directory\n\n";
}
m_root = std::make_unique<Directory>(m_user, m_user->getUsername(), "");
}
else
{
std::ifstream file{ m_rootPath / ".config_file_tree" };
std::string line{};
std::getline(file, line);
std::istringstream rootData{ line };
m_root = std::make_unique<Directory>(rootData);
while (std::getline(file, line))
{
std::istringstream iss{ line };
std::unique_ptr<SystemObject> sysObj;
if (line[0] == '0' + static_cast<int>(SystemObjectType::directory))
{
sysObj = std::make_unique<Directory>(iss);
}
else
{
sysObj = std::make_unique<File>(iss);
}
if (auto* parent = searchSystemObject(sysObj->getParentPath()); parent->getType() == SystemObjectType::directory)
{
if (!dynamic_cast<Directory*>(parent)->addChildren(std::move(sysObj)))
{
std::clog << "ERROR: failed to add children to directory\n\n";
}
}
else
{
std::clog << "ERROR: trying to add an object to a non-directory-object\n\n";
}
}
}
}
void FileSystemEnv::saveUserEnv() const
{
std::ofstream file{ m_rootPath / ".config_file_tree" };
std::queue<const Directory*> directories;
directories.push(m_root.get());
file << *m_root;
while (!directories.empty())
{
const auto* dir = directories.front();
directories.pop();
for (const auto& [key, val] : dir->getChildren())
{
file << '\n';
file << *val;
if (val->getType() == SystemObjectType::directory)
{
directories.push(dynamic_cast<const Directory*>(val.get()));
}
}
}
}
SystemObject* FileSystemEnv::searchSystemObject(const std::string& path)
{
const PathResolver pr{ path };
Directory* currDir = m_root.get();
for (const auto& dirName : pr.getParentPath())
{
if (auto* sysObj = currDir->searchChildren(dirName);
sysObj && sysObj->getType() == SystemObjectType::directory)
{
currDir = dynamic_cast<Directory*>(sysObj);
}
else
{
return nullptr;
}
}
if (path.empty())
{
return m_root.get();
}
return currDir->searchChildren(pr.getObjectName());
}
Directory* FileSystemEnv::getRoot()
{
return m_root.get();
}
} // namespace fs