diff --git a/kernel/include/vfs.hpp b/kernel/include/vfs.hpp index 23bcd826..fc184c01 100644 --- a/kernel/include/vfs.hpp +++ b/kernel/include/vfs.hpp @@ -9,6 +9,7 @@ #define VFS_H #include +#include //TODO Once userspace is done, integrate parts of disks.hpp here @@ -19,6 +20,7 @@ int64_t stat(size_t fd, stat_info& info); int64_t mkdir(const char* file); int64_t rm(const char* file); int64_t read(size_t fd, char* buffer, size_t max); +int64_t entries(size_t fd, char* buffer, size_t size); void close(size_t fd); diff --git a/kernel/src/vfs.cpp b/kernel/src/vfs.cpp index 25c257c5..15fc165c 100644 --- a/kernel/src/vfs.cpp +++ b/kernel/src/vfs.cpp @@ -229,3 +229,56 @@ int64_t vfs::read(size_t fd, char* buffer, size_t max){ return i; } + +int64_t entries(size_t fd, char* buffer, size_t size){ + if(!disks::mounted_partition() || !disks::mounted_disk()){ + return -std::ERROR_NOTHING_MOUNTED; + } + + if(!scheduler::has_handle(fd)){ + return -std::ERROR_INVALID_FILE_DESCRIPTOR; + } + + auto path = scheduler::get_handle(fd); + + if(path.empty()){ + return -std::ERROR_INVALID_FILE_PATH; + } + + //TODO file search should be done entirely by the file system + + auto files = fat32::ls(*disks::mounted_disk(), *disks::mounted_partition(), path); + + size_t total_size = 0; + + for(auto& f : files){ + total_size += sizeof(directory_entry) + f.file_name.size(); + } + + if(size < total_size){ + return -std::ERROR_BUFFER_SMALL; + } + + size_t position = 0; + + for(size_t i = 0; i < files.size(); ++i){ + auto& file = files[i]; + + auto entry = reinterpret_cast(buffer + position); + + entry->type = 0; //TODO Fill that + entry->length = file.file_name.size(); + + if(i + 1 < files.size()){ + entry->offset_next = file.file_name.size() + 1; + } else { + entry->offset_next = 0; + } + + char* name_buffer = &(entry->name); + for(size_t j = 0; j < file.file_name.size(); ++j){ + name_buffer[j] = file.file_name[j]; + } + name_buffer[file.file_name.size()] = '\0'; + } +} diff --git a/tlib/include/directory_entry.hpp b/tlib/include/directory_entry.hpp new file mode 100644 index 00000000..89cb8312 --- /dev/null +++ b/tlib/include/directory_entry.hpp @@ -0,0 +1,20 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#ifndef USER_DIRECTORY_ENTRY_HPP +#define USER_DIRECTORY_ENTRY_HPP + +#include + +struct directory_entry { + size_t type; + size_t offset_next; + size_t length; + char name; //First char +}; + +#endif diff --git a/tlib/include/errors.hpp b/tlib/include/errors.hpp index 6599efbc..c054be5c 100644 --- a/tlib/include/errors.hpp +++ b/tlib/include/errors.hpp @@ -23,6 +23,7 @@ constexpr const size_t ERROR_DIRECTORY = 6; constexpr const size_t ERROR_INVALID_FILE_DESCRIPTOR= 7; constexpr const size_t ERROR_FAILED= 8; constexpr const size_t ERROR_EXISTS= 9; +constexpr const size_t ERROR_BUFFER_SMALL= 10; inline const char* error_message(size_t error){ switch(error){ @@ -44,6 +45,8 @@ inline const char* error_message(size_t error){ return "Failed"; case ERROR_EXISTS: return "The file exists"; + case ERROR_BUFFER_SMALL: + return "The buffer is too small"; default: return "Unknonwn error"; }