From 7773f9c3a8a369a2e92d95baa91f4c9af2ce158c Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Tue, 16 Aug 2016 21:40:55 +0200 Subject: [PATCH] Better use of path --- kernel/include/fs/fat32.hpp | 4 ++-- kernel/include/vfs/vfs.hpp | 8 +++++--- kernel/src/fs/fat32.cpp | 6 +++--- kernel/src/scheduler.cpp | 2 +- kernel/src/vfs/vfs.cpp | 34 +++++++++++++++------------------- 5 files changed, 26 insertions(+), 28 deletions(-) diff --git a/kernel/include/fs/fat32.hpp b/kernel/include/fs/fat32.hpp index a0ce2b81..84e07f26 100644 --- a/kernel/include/fs/fat32.hpp +++ b/kernel/include/fs/fat32.hpp @@ -24,13 +24,13 @@ typedef const disks::disk_descriptor& dd; struct fat32_file_system : vfs::file_system { private: path mount_point; - std::string device; + path device; fat_bs_t* fat_bs = nullptr; fat_is_t* fat_is = nullptr; public: - fat32_file_system(path mount_point, std::string device); + fat32_file_system(path mount_point, path device); ~fat32_file_system(); void init(); diff --git a/kernel/include/vfs/vfs.hpp b/kernel/include/vfs/vfs.hpp index 6cdc10af..dedf7b69 100644 --- a/kernel/include/vfs/vfs.hpp +++ b/kernel/include/vfs/vfs.hpp @@ -11,6 +11,8 @@ #include #include +#include "vfs/path.hpp" + namespace vfs { enum class partition_type { @@ -41,11 +43,11 @@ int64_t mount(partition_type type, size_t mp_fd, size_t dev_fd); int64_t mount(partition_type type, const char* mount_point, const char* device); //Used only inside kernel as a easy way to read a complete file -int64_t direct_read(const std::string& file, std::string& content); +int64_t direct_read(const path& file, std::string& content); //Use only inside the kernel for FS to access devices -int64_t direct_read(const char* file, char* buffer, size_t count, size_t offset = 0); -int64_t direct_write(const char* file, const char* buffer, size_t count, size_t offset = 0); +int64_t direct_read(const path& file, char* buffer, size_t count, size_t offset = 0); +int64_t direct_write(const path& file, const char* buffer, size_t count, size_t offset = 0); } //end of namespace vfs diff --git a/kernel/src/fs/fat32.cpp b/kernel/src/fs/fat32.cpp index 0ef4247c..05831a68 100644 --- a/kernel/src/fs/fat32.cpp +++ b/kernel/src/fs/fat32.cpp @@ -171,7 +171,7 @@ void init_file_entry(fat32::cluster_entry* entry_ptr, const char* name, uint32_t } //end of anonymous namespace -fat32::fat32_file_system::fat32_file_system(path mount_point, std::string device) : mount_point(mount_point), device(device) { +fat32::fat32_file_system::fat32_file_system(path mount_point, path device) : mount_point(mount_point), device(device) { //Nothing else to init } @@ -1364,11 +1364,11 @@ uint32_t fat32::fat32_file_system::find_free_cluster(){ } bool fat32::fat32_file_system::read_sectors(uint64_t start, uint8_t count, void* destination){ - auto result = vfs::direct_read(device.c_str(), reinterpret_cast(destination), count * 512, start * 512); + auto result = vfs::direct_read(device, reinterpret_cast(destination), count * 512, start * 512); return result > 0 && result == count * 512; } bool fat32::fat32_file_system::write_sectors(uint64_t start, uint8_t count, void* source){ - auto result = vfs::direct_write(device.c_str(), reinterpret_cast(source), count * 512, start * 512); + auto result = vfs::direct_write(device, reinterpret_cast(source), count * 512, start * 512); return result > 0 && result == count * 512; } diff --git a/kernel/src/scheduler.cpp b/kernel/src/scheduler.cpp index 88686990..e6087287 100644 --- a/kernel/src/scheduler.cpp +++ b/kernel/src/scheduler.cpp @@ -614,7 +614,7 @@ int64_t scheduler::exec(const std::string& file, const std::vector& logging::log(logging::log_level::TRACE, "scheduler:exec: read_file start\n"); std::string content; - auto result = vfs::direct_read(file, content); + auto result = vfs::direct_read(path(file), content); if(result < 0){ logging::logf(logging::log_level::DEBUG, "scheduler: direct_read error: %s\n", std::error_message(-result)); diff --git a/kernel/src/vfs/vfs.cpp b/kernel/src/vfs/vfs.cpp index 63ed557b..6dc70791 100644 --- a/kernel/src/vfs/vfs.cpp +++ b/kernel/src/vfs/vfs.cpp @@ -14,7 +14,6 @@ #include "vfs/vfs.hpp" #include "vfs/file_system.hpp" -#include "vfs/path.hpp" #include "fs/fat32.hpp" #include "fs/sysfs.hpp" @@ -32,13 +31,13 @@ namespace { struct mounted_fs { vfs::partition_type fs_type; - std::string device; + path device; path mount_point; vfs::file_system* file_system; mounted_fs() = default; - mounted_fs(vfs::partition_type type, std::string dev, path mp, vfs::file_system* fs) : + mounted_fs(vfs::partition_type type, path dev, path mp, vfs::file_system* fs) : fs_type(type), device(dev), mount_point(mp), file_system(fs) { // Nothing else to init @@ -134,7 +133,7 @@ path get_fs_path(const path& base_path, const mounted_fs& fs){ return path("/") / base_path.sub_path(fs.mount_point.size()); } -vfs::file_system* get_new_fs(vfs::partition_type type, const path& mount_point, const std::string& device){ +vfs::file_system* get_new_fs(vfs::partition_type type, const path& mount_point, const path& device){ switch(type){ case vfs::partition_type::FAT32: return new fat32::fat32_file_system(mount_point, device); @@ -201,14 +200,15 @@ int64_t vfs::mount(partition_type type, size_t mp_fd, size_t dev_fd){ int64_t vfs::mount(partition_type type, const char* mount_point, const char* device){ path mp_path(mount_point); + path dev_path(device); - auto fs = get_new_fs(type, mp_path, device); + auto fs = get_new_fs(type, mp_path, dev_path); if(!fs){ return -std::ERROR_INVALID_FILE_SYSTEM; } - mount_point_list.emplace_back(type, device, mp_path, fs); + mount_point_list.emplace_back(type, dev_path, mp_path, fs); return 0; } @@ -235,8 +235,6 @@ int64_t vfs::open(const char* file_path, size_t flags){ auto& fs = get_fs(base_path); auto fs_path = get_fs_path(base_path, fs); - logging::logf(logging::log_level::TRACE, "vfs: open %s:%s \n", base_path.string().c_str(), fs_path.string().c_str()); - //Special handling for opening the root if(fs_path.is_root()){ return scheduler::register_new_handle(base_path); @@ -379,8 +377,7 @@ int64_t vfs::read(size_t fd, char* buffer, size_t count, size_t offset){ return read; } -int64_t vfs::direct_read(const char* file, char* buffer, size_t count, size_t offset){ - auto base_path = get_path(file); +int64_t vfs::direct_read(const path& base_path, char* buffer, size_t count, size_t offset){ auto& fs = get_fs(base_path); auto fs_path = get_fs_path(base_path, fs); @@ -442,8 +439,7 @@ int64_t vfs::clear(size_t fd, size_t count, size_t offset){ return written; } -int64_t vfs::direct_write(const char* file, const char* buffer, size_t count, size_t offset){ - auto base_path = get_path(file); +int64_t vfs::direct_write(const path& base_path, const char* buffer, size_t count, size_t offset){ auto& fs = get_fs(base_path); auto fs_path = get_fs_path(base_path, fs); @@ -475,8 +471,7 @@ int64_t vfs::truncate(size_t fd, size_t size){ return result > 0 ? -result : 0; } -int64_t vfs::direct_read(const std::string& file_path, std::string& content){ - auto base_path = get_path(file_path.c_str()); +int64_t vfs::direct_read(const path& base_path, std::string& content){ auto& fs = get_fs(base_path); auto fs_path = get_fs_path(base_path, fs); @@ -559,7 +554,7 @@ int64_t vfs::mounts(char* buffer, size_t size){ size_t total_size = 0; for(auto& mp : mount_point_list){ - total_size += 4 * sizeof(size_t) + 3 + mp.device.size() + mp.mount_point.string().size() + partition_type_to_string(mp.fs_type).size(); + total_size += 4 * sizeof(size_t) + 3 + mp.device.string().size() + mp.mount_point.string().size() + partition_type_to_string(mp.fs_type).size(); } if(size < total_size){ @@ -576,11 +571,11 @@ int64_t vfs::mounts(char* buffer, size_t size){ auto fs_type = partition_type_to_string(mp.fs_type); entry->length_mp = mp.mount_point.string().size(); - entry->length_dev = mp.device.size(); + entry->length_dev = mp.device.string().size(); entry->length_type = fs_type.size(); if(i + 1 < mount_point_list.size()){ - entry->offset_next = 4 * sizeof(size_t) + 3 + mp.device.size() + mp.mount_point.string().size() + fs_type.size(); + entry->offset_next = 4 * sizeof(size_t) + 3 + mp.device.string().size() + mp.mount_point.string().size() + fs_type.size(); position += entry->offset_next; } else { entry->offset_next = 0; @@ -594,8 +589,9 @@ int64_t vfs::mounts(char* buffer, size_t size){ name_buffer[str_pos++] = mount_point[j]; } name_buffer[str_pos++] = '\0'; - for(size_t j = 0; j < mp.device.size(); ++j){ - name_buffer[str_pos++] = mp.device[j]; + auto device = mp.device.string(); + for(size_t j = 0; j < device.size(); ++j){ + name_buffer[str_pos++] = device[j]; } name_buffer[str_pos++] = '\0'; for(size_t j = 0; j < fs_type.size(); ++j){