Skip to content

Commit

Permalink
refactoring ongoing
Browse files Browse the repository at this point in the history
  • Loading branch information
matgla committed Apr 26, 2024
1 parent 9320f65 commit b8d19ee
Show file tree
Hide file tree
Showing 13 changed files with 242 additions and 505 deletions.
10 changes: 5 additions & 5 deletions common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ set(include_dir ${CMAKE_CURRENT_SOURCE_DIR}/include/common)

target_sources(common
PUBLIC
${include_dir}/filesystem/filesystem_mount_points.hpp
${include_dir}/filesystem/file_descriptors.hpp
${include_dir}/filesystem/disk_parameters.hpp
${include_dir}/filesystem/filesystem.hpp
${include_dir}/units.hpp
PRIVATE
source/units.cpp
source/filesystem_mount_points.cpp
source/file_descriptors.cpp
PUBLIC
FILE_SET CXX_MODULES FILES
source/file_descriptors.cc
source/filesystem_mount_points.cc
)

use_cppfront(
TARGET common
MODULE_SOURCES
source/disk_parameters.cpp2
source/filesystem.cpp2
)

target_include_directories(common
Expand Down
53 changes: 0 additions & 53 deletions common/include/common/filesystem/filesystem_mount_points.hpp

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module;
/**
* file_descriptors.cpp
*
Expand All @@ -18,34 +19,44 @@
* <https://www.gnu.org/licenses/>.
*/

#include "common/filesystem/file_descriptors.hpp"
#include <bitset>
#include <cstdint>

namespace yasboot::fs
{
export module yasboot.filesystem.file_descriptors;

FileDescriptors &FileDescriptors::get()
{
export namespace yasboot::fs {
class FileDescriptors {
public:
static FileDescriptors &get();

int8_t allocate();
void release(int8_t fd);

private:
FileDescriptors();

std::bitset<8> file_descriptors_;
};
} // namespace yasboot::fs

namespace yasboot::fs {

FileDescriptors &FileDescriptors::get() {
static FileDescriptors instance;
return instance;
}

int8_t FileDescriptors::allocate()
{
for (int8_t i = 0; i < static_cast<int8_t>(file_descriptors_.size()); i++)
{
if (!file_descriptors_.test(i))
{
int8_t FileDescriptors::allocate() {
for (int8_t i = 0; i < static_cast<int8_t>(file_descriptors_.size()); i++) {
if (!file_descriptors_.test(i)) {
file_descriptors_.set(i);
return i;
}
}
return -1;
}

void FileDescriptors::release(int8_t fd)
{
file_descriptors_.reset(fd);
}
void FileDescriptors::release(int8_t fd) { file_descriptors_.reset(fd); }

FileDescriptors::FileDescriptors() = default;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module;
/**
* file_descriptors.hpp
* filesystem.cpp2
*
* Copyright (C) 2024 Mateusz Stadnik <[email protected]>
*
Expand All @@ -18,26 +19,17 @@
* <https://www.gnu.org/licenses/>.
*/

#pragma once
export module yasboot.filesystem;

#include <bitset>
#include <cstdint>
yasboot: namespace = {
fs: namespace = {

namespace yasboot::fs
{
export FileSystem: @interface type = {
mount: (inout this) -> std::expected<void, i32>;
open: (inout this, path: std::string_view, flags: i32) -> std::expected<i8, i8>;
has_fd: (this, fd: i32) -> bool;
read_file: (inout this, fd: i32, buffer: std::span<u8>) -> i32;
}

class FileDescriptors
{
public:
static FileDescriptors &get();

int8_t allocate();
void release(int8_t fd);

private:
FileDescriptors();

std::bitset<8> file_descriptors_;
};

} // namespace yasboot::fs
} // namespace fs
} // namespace yasboot
92 changes: 92 additions & 0 deletions common/source/filesystem_mount_points.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
module;
/**
* filesystem_mount_points.cpp
*
* Copyright (C) 2024 Mateusz Stadnik <[email protected]>
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version
* 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General
* Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/

#include <map>
#include <memory>
#include <string>
#include <string_view>
#include <utility>

export module yasboot.filesystem.filesystem_mount_points;

import yasboot.filesystem;

export namespace yasboot::fs {
class FileSystemMountPoints {
public:
using MountPoints = std::map<std::string, std::unique_ptr<FileSystem>>;

int register_mount_point(std::string_view path,
std::unique_ptr<FileSystem> filesystem);

std::pair<FileSystem *, std::string_view>
get_mount_point(std::string_view pathname);

const FileSystem *get_filesystem_for_fd(int fd) const;

static FileSystemMountPoints &get();

private:
FileSystemMountPoints() = default;
MountPoints mount_points_;
};

} // namespace yasboot::fs

namespace yasboot::fs {

int FileSystemMountPoints::register_mount_point(
std::string_view path, std::unique_ptr<FileSystem> filesystem) {
if (mount_points_.count(std::string(path))) {
return -1;
}

mount_points_.emplace(std::string(path), std::move(filesystem));
return 0;
}

FileSystemMountPoints &FileSystemMountPoints::get() {
static FileSystemMountPoints instance;
return instance;
}

std::pair<FileSystem *, std::string_view>
FileSystemMountPoints::get_mount_point(std::string_view pathname) {
std::pair<FileSystem *, std::string_view> result{};
for (const auto &[path, filesystem] : mount_points_) {
if (pathname.starts_with(path) && path.size() > result.second.size()) {
result = std::make_pair(filesystem.get(), pathname.substr(path.size()));
}
}

return result;
}

const FileSystem *FileSystemMountPoints::get_filesystem_for_fd(int fd) const {
for (const auto &[path, fs] : mount_points_) {
if (fs->has_fd(fd)) {
return fs.get();
}
}
return nullptr;
}

} // namespace yasboot::fs
71 changes: 0 additions & 71 deletions common/source/filesystem_mount_points.cpp

This file was deleted.

Loading

0 comments on commit b8d19ee

Please sign in to comment.