Max OS 0.3
Loading...
Searching...
No Matches
vfsresource.cpp
Go to the documentation of this file.
1
10
11using namespace MaxOS;
12using namespace MaxOS::filesystem;
13using namespace MaxOS::processes;
14using namespace MaxOS::common;
15using namespace syscore::filesystem;
16
24FileResource::FileResource(string const& name, size_t flags, processes::resource_type_t type)
25: Resource(name, flags, type),
26 file(nullptr) // Initialised by the registry
27{
28
29}
30
31FileResource::~FileResource() = default;
32
41int FileResource::read(void* buffer, size_t size, size_t flags) {
42
43 // File not found
44 if(!file)
45 return -1;
46
47 // Handle the operation
48 switch ((FileFlags)flags) {
49
50 case FileFlags::DEFAULT:{
51
52 buffer_t file_buffer(buffer, size);
53 file->read(&file_buffer, size);
54 break;
55 }
56
57 case FileFlags::READ_SIZE:{
58 return file->size();
59 }
60
61 case FileFlags::READ_OFFSET:{
62
63 return file->position();
64 }
65
66 default:
67 return -1;
68 }
69 return size;
70}
71
80int FileResource::write(void const* buffer, size_t size, size_t flags) {
81
82 // File not found
83 if(!file)
84 return -1;
85
86 // Handle the operation
87 switch ((FileFlags)flags) {
88 case FileFlags::DEFAULT:{
89
90 buffer_t file_buffer((void*)buffer, size);
91 file->write(&file_buffer, size);
92 break;
93 }
94
95 case FileFlags::WRITE_SEEK_SET:
96 file->seek(SeekType::SET, size);
97 break;
98
99 case FileFlags::WRITE_SEEK_CUR:
100 file->seek(SeekType::CURRENT, size);
101 break;
102
103 case FileFlags::WRITE_SEEK_END:
104 file->seek(SeekType::END, size);
105 break;
106
107 case FileFlags::WRITE_NAME:{
108
109 // Open the parent
110 Resource* parent = GlobalResourceRegistry::get_registry(resource_type_t::FILESYSTEM)->get_resource(Path::parent_directory(name()));
111 auto parent_directory = ((DirectoryResource*)parent)->directory;
112
113 // Rename
114 string new_name = string((uint8_t*)buffer, size);
115 parent_directory->rename_file(file, new_name);
116 break;
117 }
118
119 default:
120 return -1;
121
122 }
123
124 return size;
125}
126
135: Resource(name, flags, type),
136 directory(nullptr)
137{
138
139}
140
141DirectoryResource::~DirectoryResource() = default;
142
149void DirectoryResource::write_entries(void const* buffer, size_t size) const {
150
151 size_t amount_written = 0;
152
153 entry_information_t* entry = nullptr;
154 size_t entry_capacity = 0;
155
156 auto write_single_entry = [&](const string& name, size_t entry_size, bool is_file) {
157
158 // Make sure there is enough space
159 size_t required_size = sizeof(entry_information_t) + name.length() + 1;
161 delete[] (uint8_t*)entry;
164 }
165
166 // Create the entry
167 entry->is_file = is_file;
168 entry->size = entry_size;
169 entry->entry_length = required_size;
170 memcpy(entry->name, name.c_str(), name.length());
171 entry->name[name.length()] = '\0';
172
173 // Not enough space
174 if (amount_written + entry->entry_length > size)
175 return false;
176
177 // Copy the entry
178 memcpy((uint8_t*)buffer + amount_written, entry, entry->entry_length);
179 amount_written += entry->entry_length;
180 return true;
181 };
182
183 // Write files
184 for (const auto& file : directory->files()) {
185 if (!write_single_entry(file->name(), file->size(), true))
186 break;
187 }
188
189 // Write directories
190 for (const auto& dir : directory->subdirectories()) {
191 if (!write_single_entry(dir->name(), dir->size(), false))
192 break;
193 }
194
195 delete[] (uint8_t*)entry;
196}
197
203size_t DirectoryResource::entries_size() const {
204
205 size_t size = 0;
206
207 // Files
208 for (const auto& file : directory->files()) {
209 size_t entry_size = sizeof(entry_information_t) + file->name().length() + 1;
210 size += entry_size;
211 }
212
213 // Subdirectories
214 for (const auto& dir : directory->subdirectories()) {
215 size_t entry_size = sizeof(entry_information_t) + dir->name().length() + 1;
216 size += entry_size;
217 }
218
219 return size;
220}
221
230int DirectoryResource::read(void* buffer, size_t size, size_t flags) {
231
232 // Directory not found
233 if(!directory)
234 return -1;
235
236 switch ((DirectoryFlags)flags){
237 case DirectoryFlags::READ_ENTRIES:{
238 write_entries(buffer, size);
239 break;
240 }
241
242 case DirectoryFlags::READ_ENTRIES_SIZE:{
243 return entries_size();
244 }
245
246 default:
247 return -1;
248 }
249
250 return size;
251
252}
253
262int DirectoryResource::write(void const* buffer, size_t size, size_t flags) {
263
264 // Directory not found
265 if(!directory)
266 return -1;
267
268 auto registry = GlobalResourceRegistry::get_registry(resource_type_t::FILESYSTEM);
269
270 switch ((DirectoryFlags)flags){
271
272 case DirectoryFlags::WRITE_NAME : {
273
274 // Open the parent
276 auto parent_directory = ((DirectoryResource*)parent)->directory;
277
278 // Rename
279 string new_name = string((uint8_t*)buffer, size);
280 parent_directory->rename_subdirectory(directory, new_name);
281 break;
282 }
283
284 case DirectoryFlags::WRITE_NEW_FILE:{
285
286 string new_name = string((uint8_t*)buffer, size);
288 break;
289 }
290
291
292 case DirectoryFlags::WRITE_NEW_DIR:{
293
294 string new_name = string((uint8_t*)buffer, size);
296 break;
297 }
298
299 case DirectoryFlags::WRITE_REMOVE_FILE:{
300
301 string new_name = string((uint8_t*)buffer, size);
303 break;
304 }
305 case DirectoryFlags::WRITE_REMOVE_DIR:{
306
307 string new_name = string((uint8_t*)buffer, size);
309 break;
310 }
311
312 default:
313 return -1;
314 }
315
316 return size;
317}
318
330
331VFSResourceRegistry::~VFSResourceRegistry() = default;
332
340Resource* VFSResourceRegistry::open_as_resource(const string& name, Directory* directory) {
341
342 // Doesnt exist
343 if(!directory)
344 return nullptr;
345
346 // Create the resource
347 auto resource = new DirectoryResource(name, 0, resource_type_t::FILESYSTEM);
348 resource->directory = directory;
349
351 return resource;
352}
353
361Resource* VFSResourceRegistry::open_as_resource(string const& name, File* file) {
362
363 // Doesnt exist
364 if(!file)
365 return nullptr;
366
367 // Create the resource
368 auto resource = new FileResource(name, 0, resource_type_t::FILESYSTEM);
369 resource->file = file;
370
372 return resource;
373}
374
375
377
378 string path = Path::absolute_path(name);
380
381 // Resource already opened
383 if(resource != nullptr)
384 return resource;
385
386 // Open the resource
387 bool is_file = Path::is_file(path);
388 if(is_file)
389 return open_as_resource(path, m_vfs->open_file(path));
390 else
391 return open_as_resource(path, m_vfs->open_directory(path));
392}
393
394Resource* VFSResourceRegistry::create_resource(string const& name, size_t flags) {
395
396 string path = Path::absolute_path(name);
398
399 // Resource already opened
401 if(resource != nullptr)
402 return resource;
403
404 // Open the resource
405 bool is_file = Path::is_file(path);
406 if(is_file)
407 return open_as_resource(path, m_vfs->create_file(path));
408 else
409 return open_as_resource(path, m_vfs->create_directory(path));
410
411}
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
A wrapper for a Directory which exposes Directory operations as Resource operations.
Definition vfsresource.h:49
DirectoryResource(const string &name, size_t flags, processes::resource_type_t type)
Construct a new Directory Resource object.
int read(void *buffer, size_t size, size_t flags) final
read from a directory resource
int write(const void *buffer, size_t size, size_t flags) final
write to a directory resource
Directory * directory
The directory that this resource handles & exposes.
Definition vfsresource.h:61
Handles a group of files (directory)
Definition filesystem.h:57
virtual Directory * create_subdirectory(const string &name)
Create a directory in the directory.
virtual void remove_subdirectory(const string &name)
Try to remove a directory in the directory.
virtual void remove_file(const string &name)
Delete a file in the directory.
virtual File * create_file(const string &name)
Create a file in the directory.
A wrapper for a File which exposes File operations as Resource operations.
Definition vfsresource.h:32
int write(const void *buffer, size_t size, size_t flags) final
write to a file resource
FileResource(const string &name, size_t flags, processes::resource_type_t type)
Construct a new File Resource object.
File * file
The file that this resource handles & exposes.
Definition vfsresource.h:38
int read(void *buffer, size_t size, size_t flags) final
read from a file resource
Handles file operations and information.
Definition filesystem.h:31
virtual void read(common::buffer_t *data, size_t size)
read data from the file
size_t size() const
Get the size of the file.
void seek(SeekType seek_type, size_t offset)
Seek to a position in the file.
uint32_t position() const
Get where the file is currently at (amount read/write/seeked)
virtual void write(common::buffer_t *data, size_t size)
write data to the file
static string parent_directory(const string &path)
Get the parent directory of the path.
Definition path.cpp:148
static bool is_file(const string &path)
Check if a path is a file.
Definition path.cpp:42
static string join_path(const string &base, const string &extended)
Joins two paths together.
Definition path.cpp:208
static string absolute_path(const string &path)
Calculates path with out any "../" or "./".
Definition path.cpp:170
processes::Resource * get_resource(const string &name) final
Gets a resource from the registry by name.
processes::Resource * create_resource(const string &name, size_t flags) final
Try to create a new resource with the specified name, will fail if the name is in use.
VFSResourceRegistry(VirtualFileSystem *vfs)
Construct a new VFS Resource Registry object.
Combines all the filesystems across the partitions on each disk into a single filesystems and exposes...
Definition vfs.h:25
Directory * open_directory(const string &path)
Try to open a directory on the virtual file system and read it's contents.
Definition vfs.cpp:245
File * open_file(const string &path, size_t offset=0)
Try to open a file on the virtual file system with a given offset.
Definition vfs.cpp:459
Directory * create_directory(string path)
Attempts to open the parent directory and creates the sub directory at the end of the path.
Definition vfs.cpp:293
File * create_file(const string &path)
Attempts to open the parent directory and create the file at the end of the path.
Definition vfs.cpp:425
Manages the creation, retention and destruction of resources of a certain type. Should be subclassed ...
Definition resource.h:54
virtual Resource * get_resource(const string &name)
Gets a resource from the registry by name.
Definition resource.cpp:128
virtual bool register_resource(Resource *resource)
Registers a resource in the registry.
Definition resource.cpp:146
static BaseResourceRegistry * get_registry(resource_type_t type)
Gets a registry of a type.
Definition resource.cpp:217
static Process * current_process()
Gets the process on the currently executing core.
Represents a generic resource that can be opened, closed, read from and written to.
Definition resource.h:29
string name()
Gets the name of this resource.
Definition resource.cpp:79
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
Definition memoryIO.cpp:165
::syscore::ResourceType resource_type_t
Alias to make the libsyscore ResourceType accessible here.
Definition resource.h:22
class MaxOS::String string
Typedef for String.
Defines VFSResource classes for wrapping File and Directory objects as Resources in the Virtual File ...