mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-10 21:14:38 -04:00
Start implementation of the cat command
This commit is contained in:
parent
cc178d6486
commit
296b9e3a25
@ -66,6 +66,8 @@ void unmount();
|
|||||||
vector<file> ls();
|
vector<file> ls();
|
||||||
uint64_t free_size();
|
uint64_t free_size();
|
||||||
|
|
||||||
|
string read_file(const string& file);
|
||||||
|
|
||||||
const disk_descriptor* mounted_disk();
|
const disk_descriptor* mounted_disk();
|
||||||
const partition_descriptor* mounted_partition();
|
const partition_descriptor* mounted_partition();
|
||||||
|
|
||||||
|
@ -9,8 +9,9 @@ namespace fat32 {
|
|||||||
|
|
||||||
typedef const disks::disk_descriptor& dd;
|
typedef const disks::disk_descriptor& dd;
|
||||||
|
|
||||||
vector<disks::file> ls(dd disk, const disks::partition_descriptor& partition, const vector<string>& path);
|
|
||||||
uint64_t free_size(dd disk, const disks::partition_descriptor& partition);
|
uint64_t free_size(dd disk, const disks::partition_descriptor& partition);
|
||||||
|
vector<disks::file> ls(dd disk, const disks::partition_descriptor& partition, const vector<string>& path);
|
||||||
|
string read_file(dd disk, const disks::partition_descriptor& partition, const vector<string>& path, const string& file);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
62
kernel/include/pair.hpp
Normal file
62
kernel/include/pair.hpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
#ifndef PAIR_H
|
||||||
|
#define PAIR_H
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
class pair {
|
||||||
|
public:
|
||||||
|
typedef T1 first_type;
|
||||||
|
typedef T2 second_type;
|
||||||
|
|
||||||
|
first_type first;
|
||||||
|
second_type second;
|
||||||
|
|
||||||
|
//Constructor
|
||||||
|
|
||||||
|
constexpr pair(const first_type& a, const second_type& b) : first(a), second(b){
|
||||||
|
//Nothing to init
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U1, typename U2>
|
||||||
|
constexpr pair(U1&& x, U2&& y) : first(forward<U1>(x)), second(forward<U2>(y)){
|
||||||
|
//Nothing to init
|
||||||
|
}
|
||||||
|
|
||||||
|
//Copy constructors
|
||||||
|
|
||||||
|
constexpr pair(const pair&) = default;
|
||||||
|
|
||||||
|
template<typename U1, typename U2>
|
||||||
|
constexpr pair(const pair<U1, U2>& p) : first(p.first), second(p.second) {
|
||||||
|
//Nothing to init
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U1, typename U2>
|
||||||
|
pair& operator=(const pair<U1, U2>& p){
|
||||||
|
first = p.first;
|
||||||
|
second = p.second;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Move constructors
|
||||||
|
|
||||||
|
constexpr pair(pair&&) = default;
|
||||||
|
|
||||||
|
template<typename U1, typename U2>
|
||||||
|
constexpr pair(pair<U1, U2>&& p) : first(move(p.first)), second((p.second)) {
|
||||||
|
//Nothing to init
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename U1, typename U2>
|
||||||
|
pair& operator=(pair<U1, U2>&& p){
|
||||||
|
first = forward<U1>(p.first);
|
||||||
|
second = forward<U2>(p.second);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
inline constexpr pair<T1, T2> make_pair(T1&& x, T2&& y){
|
||||||
|
return pair<T1, T2>(forward<T1>(x), forward<T2>(y));
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -39,9 +39,14 @@ template< class T > struct remove_reference {typedef T type;};
|
|||||||
template< class T > struct remove_reference<T&> {typedef T type;};
|
template< class T > struct remove_reference<T&> {typedef T type;};
|
||||||
template< class T > struct remove_reference<T&&> {typedef T type;};
|
template< class T > struct remove_reference<T&&> {typedef T type;};
|
||||||
|
|
||||||
template< class T >
|
template<typename T>
|
||||||
constexpr typename remove_reference<T>::type&& move( T&& t ){
|
constexpr typename remove_reference<T>::type&& move(T&& t){
|
||||||
return static_cast<typename remove_reference<T>::type&&>(t);
|
return static_cast<typename remove_reference<T>::type&&>(t);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
constexpr T&& forward(typename remove_reference<T>::type& t ){
|
||||||
|
return static_cast<T&&>(t);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -228,6 +228,14 @@ uint64_t disks::free_size(){
|
|||||||
return fat32::free_size(*_mounted_disk, *_mounted_partition);
|
return fat32::free_size(*_mounted_disk, *_mounted_partition);
|
||||||
}
|
}
|
||||||
|
|
||||||
const vector<string>& disks::current_directory(){
|
vector<string>& disks::current_directory(){
|
||||||
return pwd;
|
return pwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string disks::read_file(const string& file){
|
||||||
|
if(!_mounted_disk || !_mounted_partition){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
return fat32::read_file(*_mounted_disk, *_mounted_partition, pwd, file);
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "types.hpp"
|
#include "types.hpp"
|
||||||
#include "console.hpp"
|
#include "console.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
|
#include "pair.hpp"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@ -198,9 +199,7 @@ bool filename_equals(char* name, const string& path){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
} //end of anonymous namespace
|
bool cache_disk_partition(fat32::dd disk, const disks::partition_descriptor& partition){
|
||||||
|
|
||||||
vector<disks::file> fat32::ls(dd disk, const disks::partition_descriptor& partition, const vector<string>& path){
|
|
||||||
if(cached_disk != disk.uuid || cached_partition != partition.uuid){
|
if(cached_disk != disk.uuid || cached_partition != partition.uuid){
|
||||||
partition_start = partition.start;
|
partition_start = partition.start;
|
||||||
|
|
||||||
@ -211,11 +210,11 @@ vector<disks::file> fat32::ls(dd disk, const disks::partition_descriptor& partit
|
|||||||
cached_partition = partition.uuid;
|
cached_partition = partition.uuid;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!fat_bs || !fat_is){
|
//Something may go wrong when reading the two base vectors
|
||||||
//Something went wrong when reading the two base vectors
|
return fat_bs && fat_is;
|
||||||
return {};
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
pair<bool, unique_heap_array<cluster_entry>> find_cluster(fat32::dd disk, const vector<string>& path){
|
||||||
auto cluster_addr = cluster_lba(fat_bs->root_directory_cluster_start);
|
auto cluster_addr = cluster_lba(fat_bs->root_directory_cluster_start);
|
||||||
|
|
||||||
unique_heap_array<cluster_entry> current_cluster(16 * fat_bs->sectors_per_cluster);
|
unique_heap_array<cluster_entry> current_cluster(16 * fat_bs->sectors_per_cluster);
|
||||||
@ -244,7 +243,7 @@ vector<disks::file> fat32::ls(dd disk, const disks::partition_descriptor& partit
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
return {};
|
return make_pair(false, unique_heap_array<cluster_entry>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -253,31 +252,47 @@ vector<disks::file> fat32::ls(dd disk, const disks::partition_descriptor& partit
|
|||||||
if(!found){
|
if(!found){
|
||||||
//TODO If not end_reached, read the next cluster
|
//TODO If not end_reached, read the next cluster
|
||||||
|
|
||||||
return {};
|
return make_pair(false, unique_heap_array<cluster_entry>());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return files(current_cluster);
|
return make_pair(true, move(current_cluster));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} //end of anonymous namespace
|
||||||
|
|
||||||
uint64_t fat32::free_size(dd disk, const disks::partition_descriptor& partition){
|
uint64_t fat32::free_size(dd disk, const disks::partition_descriptor& partition){
|
||||||
if(cached_disk != disk.uuid || cached_partition != partition.uuid){
|
if(!cache_disk_partition(disk, partition)){
|
||||||
partition_start = partition.start;
|
|
||||||
|
|
||||||
cache_bs(disk, partition);
|
|
||||||
cache_is(disk, partition);
|
|
||||||
|
|
||||||
cached_disk = disk.uuid;
|
|
||||||
cached_partition = partition.uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!fat_bs || !fat_is){
|
|
||||||
//Something went wrong when reading the two base vectors
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return fat_is->free_clusters * fat_bs->sectors_per_cluster * 512;
|
return fat_is->free_clusters * fat_bs->sectors_per_cluster * 512;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vector<disks::file> fat32::ls(dd disk, const disks::partition_descriptor& partition, const vector<string>& path){
|
||||||
|
if(!cache_disk_partition(disk, partition)){
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto cluster = find_cluster(disk, path);
|
||||||
|
|
||||||
|
if(cluster.first){
|
||||||
|
return files(cluster.second);
|
||||||
|
} else {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string fat32::read_file(dd disk, const disks::partition_descriptor& partition, const vector<string>& path, const string& file){
|
||||||
|
if(!cache_disk_partition(disk, partition)){
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
string content = "";
|
||||||
|
|
||||||
|
auto cluster = find_cluster(disk, path);
|
||||||
|
|
||||||
|
|
||||||
|
return move(content);
|
||||||
|
}
|
||||||
|
@ -53,13 +53,14 @@ void ls_command(const vector<string>& params);
|
|||||||
void cd_command(const vector<string>& params);
|
void cd_command(const vector<string>& params);
|
||||||
void pwd_command(const vector<string>& params);
|
void pwd_command(const vector<string>& params);
|
||||||
void free_command(const vector<string>& params);
|
void free_command(const vector<string>& params);
|
||||||
|
void cat_command(const vector<string>& params);
|
||||||
|
|
||||||
struct command_definition {
|
struct command_definition {
|
||||||
const char* name;
|
const char* name;
|
||||||
void (*function)(const vector<string>&);
|
void (*function)(const vector<string>&);
|
||||||
};
|
};
|
||||||
|
|
||||||
command_definition commands[18] = {
|
command_definition commands[19] = {
|
||||||
{"reboot", reboot_command},
|
{"reboot", reboot_command},
|
||||||
{"help", help_command},
|
{"help", help_command},
|
||||||
{"uptime", uptime_command},
|
{"uptime", uptime_command},
|
||||||
@ -78,6 +79,7 @@ command_definition commands[18] = {
|
|||||||
{"cd", cd_command},
|
{"cd", cd_command},
|
||||||
{"pwd", pwd_command},
|
{"pwd", pwd_command},
|
||||||
{"sysinfo", sysinfo_command},
|
{"sysinfo", sysinfo_command},
|
||||||
|
{"cat", cat_command},
|
||||||
};
|
};
|
||||||
|
|
||||||
string current_input;
|
string current_input;
|
||||||
@ -500,6 +502,14 @@ void cd_command(const vector<string>& params){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void cat_command(const vector<string>& params){
|
||||||
|
if(params.size() == 1){
|
||||||
|
k_print_line("No file provided");
|
||||||
|
} else {
|
||||||
|
auto content = disks::read_file(params[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} //end of anonymous namespace
|
} //end of anonymous namespace
|
||||||
|
|
||||||
void init_shell(){
|
void init_shell(){
|
||||||
|
Loading…
x
Reference in New Issue
Block a user