Skip to content

Commit

Permalink
Create new BlocksDevice class and move cache there
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev committed Feb 15, 2024
1 parent c60b2f7 commit e37f608
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 15 deletions.
7 changes: 5 additions & 2 deletions include/wfslib/blocks_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ class BlocksDevice {
BlocksDevice(const std::shared_ptr<Device>& device, const std::span<std::byte>& key);

virtual void WriteBlock(uint32_t block_number,
uint32_t size_in_blocks,
const std::span<std::byte>& data,
const std::span<std::byte>& hash,
uint32_t iv,
bool encrypt);
bool encrypt,
bool recalculate_hash);
virtual bool ReadBlock(uint32_t block_number,
uint32_t size_in_blocks,
const std::span<std::byte>& data,
const std::span<const std::byte>& hash,
uint32_t iv,
bool encrypt,
bool check_hash) const;
bool check_hash);

const Device* device() const { return device_.get(); }

Expand Down
6 changes: 5 additions & 1 deletion include/wfslib/file_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ class Wfs;

class FileDevice : public Device {
public:
FileDevice(const std::string& path, uint32_t log2_sector_size = 9 /* 512 */, bool read_only = true);
FileDevice(const std::string& path,
uint32_t log2_sector_size = 9 /* 512 */,
uint32_t sectors_count = 0,
bool read_only = true,
bool create = false);
void ReadSectors(const std::span<std::byte>& data, uint32_t sector_address, uint32_t sectors_count) override;
void WriteSectors(const std::span<std::byte>& data, uint32_t sector_address, uint32_t sectors_count) override;
uint32_t SectorsCount() const override { return sectors_count_; }
Expand Down
2 changes: 2 additions & 0 deletions include/wfslib/wfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ class Area;
class WfsItem;
class File;
class Directory;
class BlocksDevice;

class Wfs {
public:
Wfs(const std::shared_ptr<Device>& device, const std::span<std::byte>& key);
Wfs(const std::shared_ptr<BlocksDevice>& device);
~Wfs();

const std::shared_ptr<BlocksDevice>& GetDevice() { return device_; }
Expand Down
1 change: 1 addition & 0 deletions include/wfslib/wfs_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class WfsItem {
virtual bool IsDirectory() const;
virtual bool IsFile() const;
virtual bool IsLink() const;
virtual bool IsQuota() const;

protected:
AttributesBlock& attributes_data() { return attributes_; }
Expand Down
9 changes: 8 additions & 1 deletion src/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "utils.h"
#include "wfs.h"


Area::Area(const std::shared_ptr<BlocksDevice>& device,
const std::shared_ptr<Area>& root_area,
const std::shared_ptr<MetadataBlock>& block,
Expand Down Expand Up @@ -54,6 +53,14 @@ std::expected<std::shared_ptr<Directory>, WfsError> Area::GetRootDirectory() {
root_directory_attributes_);
}

std::expected<std::shared_ptr<Directory>, WfsError> Area::GetShadowDirectory1() {
return GetDirectory(as_const(this)->header()->shadow_directory_block_number_1.value(), ".shadow_dir_1", {});
}

std::expected<std::shared_ptr<Directory>, WfsError> Area::GetShadowDirectory2() {
return GetDirectory(as_const(this)->header()->shadow_directory_block_number_2.value(), ".shadow_dir_1", {});
}

std::expected<std::shared_ptr<Area>, WfsError> Area::GetArea(uint32_t block_number,
const std::string& root_directory_name,
const AttributesBlock& root_directory_attributes,
Expand Down
3 changes: 3 additions & 0 deletions src/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Area : public std::enable_shared_from_this<Area> {

std::expected<std::shared_ptr<Directory>, WfsError> GetRootDirectory();

std::expected<std::shared_ptr<Directory>, WfsError> GetShadowDirectory1();
std::expected<std::shared_ptr<Directory>, WfsError> GetShadowDirectory2();

std::expected<std::shared_ptr<Directory>, WfsError> GetDirectory(uint32_t block_number,
const std::string& name,
const AttributesBlock& attributes);
Expand Down
6 changes: 4 additions & 2 deletions src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ void Block::Resize(uint32_t data_size) {
}

bool Block::Fetch(bool check_hash) {
return device_->ReadBlock(block_number_, data_, as_const(this)->Hash(), iv_, encrypted_, check_hash);
return device_->ReadBlock(block_number_, 1 << (size_category_ - BlockSize::Basic), data_, as_const(this)->Hash(), iv_,
encrypted_, check_hash);
}

void Block::Flush() {
if (!dirty_)
return;
device_->WriteBlock(block_number_, data_, Hash(), iv_, encrypted_);
device_->WriteBlock(block_number_, 1 << (size_category_ - BlockSize::Basic), data_, Hash(), iv_, encrypted_,
/*recalculate_hash=*/true);
dirty_ = false;
}

Expand Down
10 changes: 7 additions & 3 deletions src/blocks_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,24 @@ BlocksDevice::BlocksDevice(const std::shared_ptr<Device>& device, const std::spa
: device_(device), device_encryption_(std::make_unique<DeviceEncryption>(device, key)) {}

void BlocksDevice::WriteBlock(uint32_t block_number,
uint32_t /*size_in_blocks*/,
const std::span<std::byte>& data,
const std::span<std::byte>& hash,
uint32_t iv,
bool encrypt) {
device_encryption_->CalculateHash(data, hash);
bool encrypt,
bool recalculate_hash) {
if (recalculate_hash)
device_encryption_->CalculateHash(data, hash);
device_encryption_->WriteBlock(ToDeviceSector(block_number), data, iv, encrypt);
}

bool BlocksDevice::ReadBlock(uint32_t block_number,
uint32_t /*size_in_blocks*/,
const std::span<std::byte>& data,
const std::span<const std::byte>& hash,
uint32_t iv,
bool encrypt,
bool check_hash) const {
bool check_hash) {
device_encryption_->ReadBlock(ToDeviceSector(block_number), data, iv, encrypt);
return !check_hash || device_encryption_->CheckHash(data, hash);
}
Expand Down
16 changes: 12 additions & 4 deletions src/file_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
#include <cassert>
#include <fstream>

FileDevice::FileDevice(const std::string& path, uint32_t log2_sector_size, bool read_only)
: file_(new std::fstream(path, std::ios::binary | (!read_only ? (std::ios::out | std::ios::in) : std::ios::in))),
FileDevice::FileDevice(const std::string& path,
uint32_t log2_sector_size,
uint32_t sectors_count,
bool read_only,
bool create)
: file_(new std::fstream(
path,
std::ios::binary | (!create ? std::ios::in : 0) | ((create || !read_only) ? std::ios::out : 0))),

Check failure on line 20 in src/file_device.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, gcc)

no matching function for call to ‘std::basic_fstream<char>::basic_fstream(const std::string&, int)’

Check failure on line 20 in src/file_device.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, gcc)

invalid conversion from ‘int’ to ‘std::ios_base::openmode’ [-fpermissive]
log2_sector_size_(log2_sector_size),
sectors_count_(sectors_count),
read_only_(read_only) {
if (file_->fail()) {
throw std::runtime_error("FileDevice: Failed to open file");
Expand All @@ -29,8 +36,9 @@ FileDevice::FileDevice(const std::string& path, uint32_t log2_sector_size, bool
sector size)");
}
sectors_count_ = static_cast<uint32_t>(file->tellg() >> log2_sector_size);*/
sectors_count_ = 0x10; // we will find the exact sectors count later with
// Wfs::DetectSectorsCount
if (sectors_count_ == 0)
sectors_count_ = 0x10; // we will find the exact sectors count later with
// Wfs::DetectSectorsCount
}

void FileDevice::ReadSectors(const std::span<std::byte>& data, uint32_t sector_address, uint32_t sectors_count) {
Expand Down
1 change: 1 addition & 0 deletions src/structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ struct Attributes {
bool IsDirectory() const { return !!(flags.value() & Flags::DIRECTORY); }
bool IsFile() const { return !IsDirectory(); }
bool IsLink() const { return !!(flags.value() & Flags::LINK); }
bool IsQuota() const { return !!(flags.value() & Flags::QUOTA); }

size_t DataOffset() const;

Expand Down
4 changes: 3 additions & 1 deletion src/wfs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#include "structs.h"

Wfs::Wfs(const std::shared_ptr<Device>& device, const std::span<std::byte>& key)
: device_(std::make_shared<BlocksDevice>(device, key)) {
: Wfs(std::make_shared<BlocksDevice>(device, key)) {}

Wfs::Wfs(const std::shared_ptr<BlocksDevice>& device) : device_(device) {
// Read first area
root_area_ = throw_if_error(Area::LoadRootArea(device_));
}
Expand Down
6 changes: 5 additions & 1 deletion src/wfs_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ bool WfsItem::IsFile() const {
}

bool WfsItem::IsLink() const {
return attributes_data().Attributes()->IsLink();
}

bool WfsItem::IsQuota() const {
auto attributes = attributes_data().Attributes();
return attributes->IsLink();
return attributes->IsDirectory() && attributes->IsQuota();
}

0 comments on commit e37f608

Please sign in to comment.