From 296b9e3a25c2f34d9f91936ef7cac661bd5fc502 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Thu, 5 Dec 2013 21:40:36 +0100 Subject: [PATCH] Start implementation of the cat command --- kernel/include/disks.hpp | 2 ++ kernel/include/fat32.hpp | 3 +- kernel/include/pair.hpp | 62 +++++++++++++++++++++++++++++++++++++++ kernel/include/utils.hpp | 9 ++++-- kernel/src/disks.cpp | 10 ++++++- kernel/src/fat32.cpp | 63 +++++++++++++++++++++++++--------------- kernel/src/shell.cpp | 12 +++++++- 7 files changed, 132 insertions(+), 29 deletions(-) create mode 100644 kernel/include/pair.hpp diff --git a/kernel/include/disks.hpp b/kernel/include/disks.hpp index 0e4aefe1..e10504e7 100644 --- a/kernel/include/disks.hpp +++ b/kernel/include/disks.hpp @@ -66,6 +66,8 @@ void unmount(); vector ls(); uint64_t free_size(); +string read_file(const string& file); + const disk_descriptor* mounted_disk(); const partition_descriptor* mounted_partition(); diff --git a/kernel/include/fat32.hpp b/kernel/include/fat32.hpp index 62264923..b07ea1e6 100644 --- a/kernel/include/fat32.hpp +++ b/kernel/include/fat32.hpp @@ -9,8 +9,9 @@ namespace fat32 { typedef const disks::disk_descriptor& dd; -vector ls(dd disk, const disks::partition_descriptor& partition, const vector& path); uint64_t free_size(dd disk, const disks::partition_descriptor& partition); +vector ls(dd disk, const disks::partition_descriptor& partition, const vector& path); +string read_file(dd disk, const disks::partition_descriptor& partition, const vector& path, const string& file); } diff --git a/kernel/include/pair.hpp b/kernel/include/pair.hpp new file mode 100644 index 00000000..39e386e2 --- /dev/null +++ b/kernel/include/pair.hpp @@ -0,0 +1,62 @@ +#ifndef PAIR_H +#define PAIR_H + +template +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 + constexpr pair(U1&& x, U2&& y) : first(forward(x)), second(forward(y)){ + //Nothing to init + } + + //Copy constructors + + constexpr pair(const pair&) = default; + + template + constexpr pair(const pair& p) : first(p.first), second(p.second) { + //Nothing to init + } + + template + pair& operator=(const pair& p){ + first = p.first; + second = p.second; + return *this; + } + + //Move constructors + + constexpr pair(pair&&) = default; + + template + constexpr pair(pair&& p) : first(move(p.first)), second((p.second)) { + //Nothing to init + } + + template + pair& operator=(pair&& p){ + first = forward(p.first); + second = forward(p.second); + return *this; + } +}; + +template +inline constexpr pair make_pair(T1&& x, T2&& y){ + return pair(forward(x), forward(y)); +} + +#endif diff --git a/kernel/include/utils.hpp b/kernel/include/utils.hpp index 285caad0..690d6564 100644 --- a/kernel/include/utils.hpp +++ b/kernel/include/utils.hpp @@ -39,9 +39,14 @@ template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference {typedef T type;}; template< class T > struct remove_reference {typedef T type;}; -template< class T > -constexpr typename remove_reference::type&& move( T&& t ){ +template +constexpr typename remove_reference::type&& move(T&& t){ return static_cast::type&&>(t); } +template +constexpr T&& forward(typename remove_reference::type& t ){ + return static_cast(t); +} + #endif diff --git a/kernel/src/disks.cpp b/kernel/src/disks.cpp index 2730b271..94ebd99e 100644 --- a/kernel/src/disks.cpp +++ b/kernel/src/disks.cpp @@ -228,6 +228,14 @@ uint64_t disks::free_size(){ return fat32::free_size(*_mounted_disk, *_mounted_partition); } -const vector& disks::current_directory(){ +vector& disks::current_directory(){ 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); +} diff --git a/kernel/src/fat32.cpp b/kernel/src/fat32.cpp index f179f7a2..1431b7d3 100644 --- a/kernel/src/fat32.cpp +++ b/kernel/src/fat32.cpp @@ -4,6 +4,7 @@ #include "types.hpp" #include "console.hpp" #include "utils.hpp" +#include "pair.hpp" namespace { @@ -198,9 +199,7 @@ bool filename_equals(char* name, const string& path){ return true; } -} //end of anonymous namespace - -vector fat32::ls(dd disk, const disks::partition_descriptor& partition, const vector& path){ +bool cache_disk_partition(fat32::dd disk, const disks::partition_descriptor& partition){ if(cached_disk != disk.uuid || cached_partition != partition.uuid){ partition_start = partition.start; @@ -211,11 +210,11 @@ vector fat32::ls(dd disk, const disks::partition_descriptor& partit cached_partition = partition.uuid; } - if(!fat_bs || !fat_is){ - //Something went wrong when reading the two base vectors - return {}; - } + //Something may go wrong when reading the two base vectors + return fat_bs && fat_is; +} +pair> find_cluster(fat32::dd disk, const vector& path){ auto cluster_addr = cluster_lba(fat_bs->root_directory_cluster_start); unique_heap_array current_cluster(16 * fat_bs->sectors_per_cluster); @@ -244,7 +243,7 @@ vector fat32::ls(dd disk, const disks::partition_descriptor& partit break; } else { - return {}; + return make_pair(false, unique_heap_array()); } } } @@ -253,31 +252,47 @@ vector fat32::ls(dd disk, const disks::partition_descriptor& partit if(!found){ //TODO If not end_reached, read the next cluster - return {}; + return make_pair(false, unique_heap_array()); } } - 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){ - if(cached_disk != disk.uuid || cached_partition != partition.uuid){ - 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 + if(!cache_disk_partition(disk, partition)){ return 0; } return fat_is->free_clusters * fat_bs->sectors_per_cluster * 512; } + +vector fat32::ls(dd disk, const disks::partition_descriptor& partition, const vector& 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& path, const string& file){ + if(!cache_disk_partition(disk, partition)){ + return ""; + } + + string content = ""; + + auto cluster = find_cluster(disk, path); + + + return move(content); +} diff --git a/kernel/src/shell.cpp b/kernel/src/shell.cpp index 0688dd0d..98d2adfc 100644 --- a/kernel/src/shell.cpp +++ b/kernel/src/shell.cpp @@ -53,13 +53,14 @@ void ls_command(const vector& params); void cd_command(const vector& params); void pwd_command(const vector& params); void free_command(const vector& params); +void cat_command(const vector& params); struct command_definition { const char* name; void (*function)(const vector&); }; -command_definition commands[18] = { +command_definition commands[19] = { {"reboot", reboot_command}, {"help", help_command}, {"uptime", uptime_command}, @@ -78,6 +79,7 @@ command_definition commands[18] = { {"cd", cd_command}, {"pwd", pwd_command}, {"sysinfo", sysinfo_command}, + {"cat", cat_command}, }; string current_input; @@ -500,6 +502,14 @@ void cd_command(const vector& params){ } } +void cat_command(const vector& params){ + if(params.size() == 1){ + k_print_line("No file provided"); + } else { + auto content = disks::read_file(params[1]); + } +} + } //end of anonymous namespace void init_shell(){