Skip to content

Commit

Permalink
Rename WfsItem to Entry (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
koolkdev authored Nov 19, 2024
1 parent 587568e commit 5fa95f3
Show file tree
Hide file tree
Showing 18 changed files with 149 additions and 150 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ add_library(${PROJECT_NAME}
src/directory_map.cpp
src/directory_parent_tree.cpp
src/directory.cpp
src/entry.cpp
src/eptree_iterator.cpp
src/eptree.cpp
src/errors.cpp
Expand All @@ -55,7 +56,6 @@ add_library(${PROJECT_NAME}
src/sub_block_allocator.cpp
src/transactions_area.cpp
src/wfs_device.cpp
src/wfs_item.cpp
)

if(BUILD_STATIC AND MSVC)
Expand Down
6 changes: 3 additions & 3 deletions include/wfslib/directory.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@

#include "directory_iterator.h"
#include "directory_map.h"
#include "entry.h"
#include "errors.h"
#include "wfs_item.h"

class Area;
class Block;
class File;

class Directory : public WfsItem, public std::enable_shared_from_this<Directory> {
class Directory : public Entry, public std::enable_shared_from_this<Directory> {
public:
using iterator = DirectoryIterator;

// TODO: Replace name with tree iterator?
Directory(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota, std::shared_ptr<Block> block);

std::expected<std::shared_ptr<WfsItem>, WfsError> GetObject(const std::string& name) const;
std::expected<std::shared_ptr<Entry>, WfsError> GetEntry(const std::string& name) const;
std::expected<std::shared_ptr<Directory>, WfsError> GetDirectory(const std::string& name) const;
std::expected<std::shared_ptr<File>, WfsError> GetFile(const std::string& name) const;

Expand Down
13 changes: 7 additions & 6 deletions include/wfslib/wfs_item.h → include/wfslib/entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <expected>
#include <memory>
#include <string>

Expand All @@ -17,20 +18,20 @@ class QuotaArea;

using AttributesRef = Block::DataRef<Attributes>;

class WfsItem {
class Entry {
public:
WfsItem(std::string name, AttributesRef block);
virtual ~WfsItem();
Entry(std::string name, AttributesRef block);
virtual ~Entry();

const std::string& name() const { return name_; }
bool is_directory() const { return !attributes()->is_link() && attributes()->is_directory(); }
bool is_file() const { return !attributes()->is_link() && !attributes()->is_directory(); }
bool is_link() const { return attributes()->is_link(); }
bool is_quota() const { return attributes()->is_directory() && attributes()->is_quota(); }

static std::expected<std::shared_ptr<WfsItem>, WfsError> Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref);
static std::expected<std::shared_ptr<Entry>, WfsError> Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref);

protected:
// TODO: Attributes copy as it can change?
Expand Down
2 changes: 1 addition & 1 deletion include/wfslib/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <expected>

enum WfsError {
kItemNotFound,
kEntryNotFound,
kNotDirectory,
kNotFile,
kBlockBadHash,
Expand Down
6 changes: 3 additions & 3 deletions include/wfslib/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <boost/iostreams/stream.hpp>
#include <memory>
#include <vector>
#include "wfs_item.h"
#include "entry.h"

class QuotaArea;

class File : public WfsItem, public std::enable_shared_from_this<File> {
class File : public Entry, public std::enable_shared_from_this<File> {
public:
class DataCategoryReader;
class DataCategory0Reader;
Expand All @@ -27,7 +27,7 @@ class File : public WfsItem, public std::enable_shared_from_this<File> {
class DataCategory4Reader;

File(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota)
: WfsItem(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}
: Entry(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}

uint32_t Size() const;
uint32_t SizeOnDisk() const;
Expand Down
6 changes: 3 additions & 3 deletions include/wfslib/link.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

#include <memory>

#include "wfs_item.h"
#include "entry.h"

class QuotaArea;

class Link : public WfsItem, public std::enable_shared_from_this<Link> {
class Link : public Entry, public std::enable_shared_from_this<Link> {
public:
Link(std::string name, AttributesRef attributes, std::shared_ptr<QuotaArea> quota)
: WfsItem(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}
: Entry(std::move(name), std::move(attributes)), quota_(std::move(quota)) {}

private:
// TODO: We may have cyclic reference here if we do cache in area.
Expand Down
4 changes: 2 additions & 2 deletions include/wfslib/wfs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BlocksDevice;
class Area;
class QuotaArea;
class TransactionsArea;
class WfsItem;
class Entry;
class Device;
class File;
class Directory;
Expand All @@ -35,7 +35,7 @@ class WfsDevice : public std::enable_shared_from_this<WfsDevice> {

BlocksDevice* device() { return device_.get(); }

std::shared_ptr<WfsItem> GetObject(const std::string& filename);
std::shared_ptr<Entry> GetEntry(const std::string& filename);
std::shared_ptr<File> GetFile(const std::string& filename);
std::shared_ptr<Directory> GetDirectory(const std::string& filename);

Expand Down
29 changes: 14 additions & 15 deletions src/directory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,43 @@ Directory::Directory(std::string name,
AttributesRef attributes,
std::shared_ptr<QuotaArea> quota,
std::shared_ptr<Block> block)
: WfsItem(std::move(name), std::move(attributes)),
: Entry(std::move(name), std::move(attributes)),
quota_(std::move(quota)),
block_(std::move(block)),
map_{quota_, block_} {}

std::expected<std::shared_ptr<WfsItem>, WfsError> Directory::GetObject(const std::string& name) const {
std::expected<std::shared_ptr<Entry>, WfsError> Directory::GetEntry(const std::string& name) const {
try {
// TODO: Case insensitive
auto it = find(name);
if (it.is_end()) {
return std::unexpected(WfsError::kItemNotFound);
return std::unexpected(WfsError::kEntryNotFound);
}
return (*it).item;
return (*it).entry;
} catch (const WfsException& e) {
return std::unexpected(e.error());
}
}

std::expected<std::shared_ptr<Directory>, WfsError> Directory::GetDirectory(const std::string& name) const {
auto obj = GetObject(name);
if (!obj.has_value())
return std::unexpected(obj.error());
if (!(*obj)->is_directory()) {
auto entry = GetEntry(name);
if (!entry.has_value())
return std::unexpected(entry.error());
if (!(*entry)->is_directory()) {
// Not a directory
return std::unexpected(kNotDirectory);
}
return std::dynamic_pointer_cast<Directory>(*obj);
return std::dynamic_pointer_cast<Directory>(*entry);
}

std::expected<std::shared_ptr<File>, WfsError> Directory::GetFile(const std::string& name) const {
auto obj = GetObject(name);
if (!obj.has_value())
return std::unexpected(obj.error());
if (!(*obj)->is_file()) {
auto entry = GetEntry(name);
if (!entry.has_value())
return std::unexpected(entry.error());
if (!(*entry)->is_file()) {
// Not a file
return std::unexpected(kNotFile);
}
return std::dynamic_pointer_cast<File>(*obj);
return std::dynamic_pointer_cast<File>(*entry);
}

Directory::iterator Directory::find(std::string key) const {
Expand Down
4 changes: 2 additions & 2 deletions src/directory_iterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

#include "directory_iterator.h"

#include "entry.h"
#include "quota_area.h"
#include "wfs_item.h"

DirectoryIterator::DirectoryIterator(DirectoryMapIterator base) : base_(base) {}

DirectoryIterator::reference DirectoryIterator::operator*() const {
auto val = *base_;
auto name = val.attributes.get()->GetCaseSensitiveName(val.name);
return {name, WfsItem::Load(base_.quota(), name, val.attributes)};
return {name, Entry::Load(base_.quota(), name, val.attributes)};
}

DirectoryIterator& DirectoryIterator::operator++() {
Expand Down
10 changes: 5 additions & 5 deletions src/directory_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@
#include "errors.h"

class QuotaArea;
class WfsItem;
class Entry;

struct DiretoryItem {
struct DiretoryEntry {
std::string name;
std::expected<std::shared_ptr<WfsItem>, WfsError> item;
std::expected<std::shared_ptr<Entry>, WfsError> entry;
};

class DirectoryIterator {
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = ptrdiff_t;

using value_type = DiretoryItem;
using ref_type = DiretoryItem;
using value_type = DiretoryEntry;
using ref_type = DiretoryEntry;

using reference = ref_type;

Expand Down
2 changes: 1 addition & 1 deletion src/directory_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool DirectoryMap::erase(std::string_view name) {
return false;
}
auto parents = it.parents();
// Free the item attributes first
// Free the entry attributes first
it.leaf().node.Free((*it.leaf().iterator).value(),
static_cast<uint16_t>(1 << (*it).attributes->entry_log2_size.value()));
it.leaf().node.erase(it.leaf().iterator);
Expand Down
6 changes: 3 additions & 3 deletions src/directory_map_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class QuotaArea;
struct Attributes;

struct DiretoryMapItem {
struct DiretoryMapEntry {
std::string name;
Block::DataRef<Attributes> attributes;
};
Expand All @@ -24,8 +24,8 @@ class DirectoryMapIterator {
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = ptrdiff_t;

using value_type = DiretoryMapItem;
using ref_type = DiretoryMapItem;
using value_type = DiretoryMapEntry;
using ref_type = DiretoryMapEntry;

using reference = ref_type;

Expand Down
13 changes: 6 additions & 7 deletions src/wfs_item.cpp → src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
* of the MIT license. See the LICENSE file for details.
*/

#include "wfs_item.h"
#include "entry.h"

#include "directory.h"
#include "file.h"
#include "link.h"
#include "quota_area.h"

WfsItem::WfsItem(std::string name, AttributesRef attributes)
: name_(std::move(name)), attributes_(std::move(attributes)) {}
Entry::Entry(std::string name, AttributesRef attributes) : name_(std::move(name)), attributes_(std::move(attributes)) {}

WfsItem::~WfsItem() = default;
Entry::~Entry() = default;

// static
std::expected<std::shared_ptr<WfsItem>, WfsError> WfsItem::Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref) {
std::expected<std::shared_ptr<Entry>, WfsError> Entry::Load(std::shared_ptr<QuotaArea> quota,
std::string name,
AttributesRef attributes_ref) {
auto* attributes = attributes_ref.get();
if (attributes->is_link()) {
// TODO, I think that the link info is in the attributes metadata
Expand Down
4 changes: 2 additions & 2 deletions src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

char const* WfsException::what() const noexcept {
switch (error_) {
case WfsError::kItemNotFound:
return "Item not found";
case WfsError::kEntryNotFound:
return "Entry not found";
case WfsError::kNotDirectory:
return "Not a directory";
case WfsError::kNotFile:
Expand Down
2 changes: 1 addition & 1 deletion src/quota_area.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include <expected>

#include "area.h"
#include "entry.h"
#include "errors.h"
#include "wfs_item.h"

class Directory;
class FreeBlocksAllocator;
Expand Down
18 changes: 9 additions & 9 deletions src/wfs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ WfsDevice::~WfsDevice() {
Flush();
}

std::shared_ptr<WfsItem> WfsDevice::GetObject(const std::string& filename) {
std::shared_ptr<Entry> WfsDevice::GetEntry(const std::string& filename) {
if (filename == "/")
return GetDirectory("/");
std::filesystem::path path(filename);
auto dir = GetDirectory(path.parent_path().string());
if (!dir)
return nullptr;
auto obj = dir->GetObject(path.filename().string());
if (!obj.has_value()) {
if (obj.error() == WfsError::kItemNotFound)
auto entry = dir->GetEntry(path.filename().string());
if (!entry.has_value()) {
if (entry.error() == WfsError::kEntryNotFound)
return nullptr;
else
throw WfsException(obj.error());
throw WfsException(entry.error());
}
return *obj;
return *entry;
}

std::shared_ptr<File> WfsDevice::GetFile(const std::string& filename) {
Expand All @@ -51,7 +51,7 @@ std::shared_ptr<File> WfsDevice::GetFile(const std::string& filename) {
return nullptr;
auto file = dir->GetFile(path.filename().string());
if (!file.has_value()) {
if (file.error() == WfsError::kItemNotFound)
if (file.error() == WfsError::kEntryNotFound)
return nullptr;
else
throw WfsException(file.error());
Expand All @@ -63,7 +63,7 @@ std::shared_ptr<Directory> WfsDevice::GetDirectory(const std::string& filename)
std::filesystem::path path(filename);
auto current_directory = GetRootDirectory();
if (!current_directory.has_value()) {
if (current_directory.error() == WfsError::kItemNotFound)
if (current_directory.error() == WfsError::kEntryNotFound)
return nullptr;
else
throw WfsException(current_directory.error());
Expand All @@ -75,7 +75,7 @@ std::shared_ptr<Directory> WfsDevice::GetDirectory(const std::string& filename)
continue;
current_directory = (*current_directory)->GetDirectory(part.string());
if (!current_directory.has_value()) {
if (current_directory.error() == WfsError::kItemNotFound)
if (current_directory.error() == WfsError::kEntryNotFound)
return nullptr;
else
throw WfsException(current_directory.error());
Expand Down
Loading

0 comments on commit 5fa95f3

Please sign in to comment.