Prepare entries system call

This commit is contained in:
Baptiste Wicht 2014-02-27 18:06:03 +01:00
parent b818980b47
commit f1325c5762
4 changed files with 78 additions and 0 deletions

View File

@ -9,6 +9,7 @@
#define VFS_H
#include <stat_info.hpp>
#include <directory_entry.hpp>
//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);

View File

@ -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<directory_entry*>(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';
}
}

View File

@ -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 <types.hpp>
struct directory_entry {
size_t type;
size_t offset_next;
size_t length;
char name; //First char
};
#endif

View File

@ -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";
}