summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/Sources/FileManager.cxx
blob: aa8fca2a404e7192d4b4fe7d0904a0a81e14f2dc (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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* -------------------------------------------

	Copyright ZKA Technologies.

------------------------------------------- */

#include <KernelKit/FileManager.hxx>
#include <NewKit/Utils.hxx>

/// BUGS: 0
//! @brief File manager for Kernel.

namespace Kernel
{
	STATIC FilesystemManagerInterface* kMounted = nullptr;

	/// @brief FilesystemManager getter.
	/// @return The mounted filesystem.
	_Output FilesystemManagerInterface* FilesystemManagerInterface::GetMounted()
	{
		return kMounted;
	}

	/// @brief Unmount filesystem.
	/// @return The unmounted filesystem.
	_Output FilesystemManagerInterface* FilesystemManagerInterface::Unmount()
	{
		if (kMounted)
		{
			auto mount = kMounted;
			kMounted   = nullptr;

			return mount;
		}

		return nullptr;
	}

	/// @brief Mount filesystem.
	/// @param mount_ptr The filesystem to mount.
	/// @return if it succeeded true, otherwise false.
	bool FilesystemManagerInterface::Mount(_Input FilesystemManagerInterface* mount_ptr)
	{
		if (mount_ptr != nullptr)
		{
			kMounted = mount_ptr;
			return true;
		}

		return false;
	}

#ifdef __FSKIT_USE_NEWFS__
	/// @brief Opens a new file.
	/// @param path
	/// @param r
	/// @return
	_Output NodePtr NewFilesystemManager::Open(_Input const Char* path, _Input const Char* r)
	{
		if (!path || *path == 0)
			return nullptr;

		if (!r || *r == 0)
			return nullptr;

		auto catalog = fImpl->GetCatalog(path);

		if (catalog->Kind != kNewFSCatalogKindFile)
		{
			fImpl->CloseCatalog(catalog);
			return nullptr;
		}

		return node_cast(catalog);
	}

	/// @brief Writes to a catalog's fork.
	/// @param node the node ptr.
	/// @param data the data.
	/// @param flags the size.
	/// @return
	Void NewFilesystemManager::Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size)
	{
		if (!node)
			return;
		if (!size)
			return;

		constexpr auto cDataForkName = kNewFSDataFork;
		this->Write(cDataForkName, node, data, flags, size);
	}

	/// @brief Read from filesystem fork.
	/// @param node the catalog node.
	/// @param flags the flags with it.
	/// @param sz the size to read.
	/// @return
	_Output VoidPtr NewFilesystemManager::Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT size)
	{
		if (!node)
			return nullptr;
		if (!size)
			return nullptr;

		constexpr auto cDataForkName = kNewFSDataFork;
		return this->Read(cDataForkName, node, flags, size);
	}

	Void NewFilesystemManager::Write(_Input const Char* name,
									 _Input NodePtr		node,
									 _Input VoidPtr		data,
									 _Input Int32		flags,
									 _Input SizeT		size)
	{
		if (!size ||
			size > kNewFSForkSize)
			return;

		if (!data)
			return;

		NEWOS_UNUSED(flags);

		if ((reinterpret_cast<NFS_CATALOG_STRUCT*>(node))->Kind == kNewFSCatalogKindFile)
			fImpl->WriteCatalog(reinterpret_cast<NFS_CATALOG_STRUCT*>(node), data, size,
								name);
	}

	_Output VoidPtr NewFilesystemManager::Read(_Input const Char* name,
											   _Input NodePtr	  node,
											   _Input Int32		  flags,
											   _Input SizeT		  sz)
	{
		if (sz > kNewFSForkSize)
			return nullptr;

		if (!sz)
			return nullptr;

		NEWOS_UNUSED(flags);

		if ((reinterpret_cast<NFS_CATALOG_STRUCT*>(node))->Kind == kNewFSCatalogKindFile)
			return fImpl->ReadCatalog(reinterpret_cast<NFS_CATALOG_STRUCT*>(node), sz,
									  name);

		return nullptr;
	}

	/// @brief Seek from Catalog.
	/// @param node
	/// @param off
	/// @retval true always returns false, this is unimplemented.
	/// @retval false always returns this, it is unimplemented.

	_Output Bool NewFilesystemManager::Seek(NodePtr node, SizeT off)
	{
		if (!node || off == 0)
			return false;

		return fImpl->Seek(reinterpret_cast<NFS_CATALOG_STRUCT*>(node), off);
	}

	/// @brief Tell where the catalog is.
	/// @param node
	/// @retval true always returns false, this is unimplemented.
	/// @retval false always returns this, it is unimplemented.

	_Output SizeT NewFilesystemManager::Tell(NodePtr node)
	{
		if (!node)
			return kNPos;

		return fImpl->Tell(reinterpret_cast<NFS_CATALOG_STRUCT*>(node));
	}

	/// @brief Rewinds the catalog.
	/// @param node
	/// @retval true always returns false, this is unimplemented.
	/// @retval false always returns this, it is unimplemented.

	_Output Bool NewFilesystemManager::Rewind(NodePtr node)
	{
		if (!node)
			return false;

		return this->Seek(node, 0);
	}

	/// @brief Returns the filesystem parser.
	/// @return the Filesystem parser class.
	_Output NewFSParser* NewFilesystemManager::GetParser() noexcept
	{
		return fImpl;
	}
#endif // __FSKIT_USE_NEWFS__
} // namespace Kernel