Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ add_library(${PROJECT_NAME}
src/eptree.cpp
src/errors.cpp
src/file.cpp
src/file_storage.cpp
src/file_resizer.cpp
src/file_device.cpp
src/free_blocks_allocator.cpp
src/free_blocks_tree_iterator.cpp
Expand Down
26 changes: 17 additions & 9 deletions include/wfslib/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class QuotaArea;

class File : public Entry, public std::enable_shared_from_this<File> {
public:
class DataCategoryReader;
class DataCategory0Reader;
class RegularDataCategoryReader;
class DataCategory1Reader;
class DataCategory2Reader;
class DataCategory3Reader;
class DataCategory4Reader;
class FileStorage;
class InlineStorage;
class BlockStorage;
class SingleBlockStorage;
class LargeBlockStorage;
class ClusterBlockStorage;
class ExtendedClusterBlockStorage;

File(std::string name, MetadataRef metadata, std::shared_ptr<QuotaArea> quota)
: Entry(std::move(name), std::move(metadata)), quota_(std::move(quota)) {}
Expand All @@ -32,6 +32,10 @@ class File : public Entry, public std::enable_shared_from_this<File> {
uint32_t SizeOnDisk() const;
void Resize(size_t new_size);

void EnsureSize(size_t size);

void Truncate(size_t size);

class file_device {
public:
typedef char char_type;
Expand All @@ -45,18 +49,22 @@ class File : public Entry, public std::enable_shared_from_this<File> {

private:
size_t size() const;
void refresh_storage_if_needed();
std::shared_ptr<File> file_;
std::shared_ptr<DataCategoryReader> reader_;
std::shared_ptr<FileStorage> storage_;
boost::iostreams::stream_offset pos_;
uint8_t storage_type_;
};

typedef boost::iostreams::stream<file_device> stream;

private:
friend struct FileResizer;

std::shared_ptr<QuotaArea> quota() const { return quota_; }

// TODO: We may have cyclic reference here if we do cache in area.
std::shared_ptr<QuotaArea> quota_;

static std::shared_ptr<DataCategoryReader> CreateReader(std::shared_ptr<File> file);
static std::shared_ptr<FileStorage> CreateStorage(std::shared_ptr<File> file);
};
31 changes: 26 additions & 5 deletions src/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "block.h"

#include <algorithm>
#include "blocks_device.h"
#include "device.h"
#include "structs.h"
Expand Down Expand Up @@ -47,16 +48,28 @@ Block::~Block() {
}

void Block::Resize(uint32_t data_size) {
if (detached_) {
data_size_ = data_size;
if (data_.size() < data_size_)
data_.resize(data_size_, std::byte{0});
return;
}
assert(!device_->device()->IsReadOnly());
// Ensure that block data is aligned to device sectors
if (data_size_ == data_size)
const auto old_size = data_size_;
if (old_size == data_size)
return;

auto new_size = GetAlignedSize(data_size);
data_size_ = data_size;

// Ensure that block data is aligned to device sectors
auto new_size = GetAlignedSize(data_size_);
if (new_size != data_.size()) {
data_.resize(new_size, std::byte{0});
dirty_ = true;
} else if (data_size_ > old_size) {
auto* begin = data_.data() + old_size;
std::fill(begin, begin + (data_size_ - old_size), std::byte{0});
}
dirty_ = true;
}

void Block::Detach() {
Expand Down Expand Up @@ -87,10 +100,17 @@ void Block::Flush() {

uint32_t Block::GetAlignedSize(uint32_t size) const {
assert(size > 0 && size <= capacity());
if (detached_ || !device_) {
return size;
}
return static_cast<uint32_t>(div_ceil(size, device_->device()->SectorSize()) * device_->device()->SectorSize());
}

std::span<std::byte> Block::GetDataForWriting() {
if (detached_ || !device_) {
dirty_ = true;
return {data_.data(), data_.data() + size()};
}
assert(!device_->device()->IsReadOnly());
dirty_ = true;
return {data_.data(), data_.data() + size()};
Expand All @@ -111,8 +131,9 @@ std::expected<std::shared_ptr<Block>, WfsError> Block::LoadDataBlock(std::shared
assert(cached_block->physical_block_number() == physical_block_number);
assert(cached_block->block_size() == block_size);
assert(cached_block->block_type() == block_type);
assert(cached_block->size() == data_size);
assert(cached_block->encrypted() == encrypted);
if (cached_block->size() != data_size)
cached_block->Resize(data_size);
return cached_block;
}
auto block = std::make_shared<Block>(device, physical_block_number, block_size, block_type, data_size, iv,
Expand Down
1 change: 1 addition & 0 deletions src/directory_leaf_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ void DirectoryLeafTree::copy_value(DirectoryTree& new_tree,
assert(new_offset.has_value());
Block::RawDataRef<EntryMetadata> new_metadata{new_tree.block().get(), *new_offset};
std::memcpy(new_metadata.get_mutable(), metadata.get(), size);
// TODO: Also update current references
new_node.set_leaf(*new_offset);
}
Loading