Skip to content

Commit

Permalink
get rid of owner and group
Browse files Browse the repository at this point in the history
  • Loading branch information
lesomnus committed Jul 23, 2023
1 parent b7d400c commit 7b2a01d
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 150 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ int main(int argc, char* argv[]) {
### File Systems
- `vfs::make_os_fs` Proxy of `std::filesystem`.
- `vfs::make_vfs` Basic file system that is not thread-safe and does not consider permissions.
- 🏗️ `vfs::make_strict_vfs` Basic file system with permissions.
- `vfs::make_vfs` Basic file system.
- `vfs::make_mem_fs` Files are stored on the memory.
- `vfs::make_union_fs` Provides a single coherent file system over multiple file systems.
### Utilities
- `vfs::Fs::change_root` Changes the root directory.
- `vfs::Fs::mount` Mounts different file system.
- `vfs::Fs::copy` Copies a file between file systems.
- 🏗️ `vfs::with_user` Switches user.
- 🏗️ `vfs::with_mem_storage` Stores files in the memory.
## About Current Working Directory
Expand Down
24 changes: 0 additions & 24 deletions internal/vfs/impl/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ class File {
};
}

[[nodiscard]] virtual std::intmax_t owner() const = 0;

virtual void owner(std::intmax_t owner) = 0;

[[nodiscard]] virtual std::intmax_t group() const = 0;

virtual void group(std::intmax_t group) = 0;

[[nodiscard]] virtual std::filesystem::perms perms() const = 0;

virtual void perms(std::filesystem::perms prms, std::filesystem::perm_options opts) = 0;
Expand Down Expand Up @@ -234,22 +226,6 @@ class Directory: virtual public File {
// TODO: inherit from FileProxy?
class MountPoint: virtual public File {
public:
[[nodiscard]] std::intmax_t owner() const override {
return this->attachment()->owner();
}

void owner(std::intmax_t owner) override {
this->attachment()->owner(owner);
}

[[nodiscard]] std::intmax_t group() const override {
return this->attachment()->group();
}

void group(std::intmax_t group) override {
this->attachment()->group(group);
}

[[nodiscard]] std::filesystem::perms perms() const override {
return this->attachment()->perms();
}
Expand Down
24 changes: 0 additions & 24 deletions internal/vfs/impl/file_proxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,30 +62,6 @@ class FileProxy
}
}

[[nodiscard]] std::intmax_t owner() const override {
return this->target_->owner();
}

void owner(std::intmax_t owner) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->target_->owner(owner);
}
}

[[nodiscard]] std::intmax_t group() const override {
return this->target_->group();
}

void group(std::intmax_t group) override {
if constexpr(std::is_const_v<Storage>) {
throw std::filesystem::filesystem_error("", std::make_error_code(std::errc::read_only_file_system));
} else {
return this->target_->group(group);
}
}

[[nodiscard]] std::filesystem::perms perms() const override {
return this->target_->perms();
}
Expand Down
15 changes: 9 additions & 6 deletions internal/vfs/impl/mem_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class MemRegularFile
, public RegularFile
, public std::enable_shared_from_this<MemRegularFile> {
public:
MemRegularFile(
std::intmax_t owner,
std::intmax_t group,
std::filesystem::perms perms = DefaultPerms);
MemRegularFile(std::filesystem::perms perms);

MemRegularFile()
: MemRegularFile(RegularFile::DefaultPerms) { }

MemRegularFile(MemRegularFile const& other);
MemRegularFile(MemRegularFile&& other) = default;
Expand Down Expand Up @@ -52,8 +52,11 @@ class MemRegularFile
class MemDirectory
: public VDirectory {
public:
MemDirectory(std::intmax_t owner, std::intmax_t group, std::filesystem::perms perms = DefaultPerms)
: VDirectory(owner, group, perms) { }
MemDirectory(std::filesystem::perms perms)
: VDirectory(perms) { }

MemDirectory()
: VDirectory(DefaultPerms) { }

MemDirectory(MemDirectory const& other) = default;
MemDirectory(MemDirectory&& other) = default;
Expand Down
18 changes: 0 additions & 18 deletions internal/vfs/impl/os_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,6 @@ class OsFile: virtual public File {
OsFile(OsFile const& other) = default;
OsFile(OsFile&& other) = default;

[[nodiscard]] std::intmax_t owner() const override {
// TODO:
return 0;
}

void owner(std::intmax_t owner) override {
// TODO:
}

[[nodiscard]] std::intmax_t group() const override {
// TODO:
return 0;
}

void group(std::intmax_t group) override {
// TODO:
}

[[nodiscard]] std::filesystem::perms perms() const override {
return std::filesystem::status(this->path_).permissions();
}
Expand Down
57 changes: 7 additions & 50 deletions internal/vfs/impl/vfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,12 @@ namespace impl {

class VFile: virtual public File {
public:
VFile(
std::intmax_t owner,
std::intmax_t group,
std::filesystem::perms perms)
: owner_(owner)
, group_(group)
, perms_(perms) { }
VFile(std::filesystem::perms perms)
: perms_(perms) { }

VFile(VFile const& other) = default;
VFile(VFile&& other) = default;

[[nodiscard]] std::intmax_t owner() const override {
return this->owner_;
}

void owner(std::intmax_t owner) override {
this->owner_ = owner;
}

[[nodiscard]] std::intmax_t group() const override {
return this->group_;
}

void group(std::intmax_t group) override {
this->group_ = group;
}

[[nodiscard]] std::filesystem::perms perms() const override {
return this->perms_;
}
Expand All @@ -65,41 +44,19 @@ class VFile: virtual public File {
VFile& operator=(VFile&& other) = default;

protected:
std::intmax_t owner_;
std::intmax_t group_;
std::filesystem::perms perms_;

std::filesystem::perms perms_;
std::filesystem::file_time_type last_write_time_;
};

class VRegularFile
: public VFile
, public TempRegularFile {
public:
VRegularFile(
std::intmax_t owner,
std::intmax_t group,
std::filesystem::perms perms = DefaultPerms);
VRegularFile(std::filesystem::perms perms = DefaultPerms);

VRegularFile(VRegularFile const& other) = delete;
VRegularFile(VRegularFile&& other) = default;

[[nodiscard]] std::intmax_t owner() const override {
return VFile::owner();
}

void owner(std::intmax_t owner) override {
VFile::owner(owner);
}

[[nodiscard]] std::intmax_t group() const override {
return VFile::group();
}

void group(std::intmax_t group) override {
VFile::group(group);
}

[[nodiscard]] std::filesystem::perms perms() const override {
return VFile::perms();
}
Expand All @@ -126,7 +83,7 @@ class VSymlink
, public Symlink {
public:
VSymlink(std::filesystem::path target)
: VFile(0, 0, std::filesystem::perms::all)
: VFile(std::filesystem::perms::all)
, target_(std::move(target)) { }

[[nodiscard]] std::filesystem::path target() const override {
Expand All @@ -141,8 +98,8 @@ class VDirectory
: public VFile
, public Directory {
public:
VDirectory(std::intmax_t owner, std::intmax_t group, std::filesystem::perms perms = DefaultPerms)
: VFile(owner, group, perms) { }
VDirectory(std::filesystem::perms perms = DefaultPerms)
: VFile(perms) { }

VDirectory(VDirectory const& other) = default;
VDirectory(VDirectory&& other) = default;
Expand Down
2 changes: 1 addition & 1 deletion src/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ std::filesystem::path Entry::path() const {
}

std::shared_ptr<DirectoryEntry> DirectoryEntry::make_root() {
auto d = std::make_shared<VDirectory>(0, 0);
auto d = std::make_shared<VDirectory>();
return std::make_shared<DirectoryEntry>("/", nullptr, std::move(d));
}

Expand Down
2 changes: 0 additions & 2 deletions src/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ namespace impl {

void RegularFile::copy_from(RegularFile const& other) {
*this->open_write() << other.open_read()->rdbuf();
this->owner(other.owner());
this->group(other.group());
this->perms(other.perms(), fs::perm_options::replace);
}

Expand Down
11 changes: 4 additions & 7 deletions src/mem_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ namespace fs = std::filesystem;
namespace vfs {
namespace impl {

MemRegularFile::MemRegularFile(
std::intmax_t owner,
std::intmax_t group,
fs::perms perms)
: VFile(owner, group, perms)
MemRegularFile::MemRegularFile(fs::perms perms)
: VFile(perms)
, data_(std::make_shared<std::string>()) { }

MemRegularFile::MemRegularFile(MemRegularFile const& other)
Expand Down Expand Up @@ -102,12 +99,12 @@ MemRegularFile& MemRegularFile::operator=(MemRegularFile const& other) {
}

std::pair<std::shared_ptr<RegularFile>, bool> MemDirectory::emplace_regular_file(std::string const& name) {
auto [it, ok] = this->files_.emplace(name, std::make_shared<MemRegularFile>(0, 0));
auto [it, ok] = this->files_.emplace(name, std::make_shared<MemRegularFile>());
return std::make_pair(std::dynamic_pointer_cast<RegularFile>(it->second), ok);
}

std::pair<std::shared_ptr<Directory>, bool> MemDirectory::emplace_directory(std::string const& name) {
auto [it, ok] = this->files_.emplace(name, std::make_shared<MemDirectory>(0, 0));
auto [it, ok] = this->files_.emplace(name, std::make_shared<MemDirectory>());
return std::make_pair(std::dynamic_pointer_cast<Directory>(it->second), ok);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mem_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace fs = std::filesystem;
namespace vfs {

std::shared_ptr<Fs> make_mem_fs(fs::path const& temp_dir) {
auto d = std::make_shared<impl::DirectoryEntry>("/", nullptr, std::make_shared<impl::MemDirectory>(0, 0));
auto d = std::make_shared<impl::DirectoryEntry>("/", nullptr, std::make_shared<impl::MemDirectory>());
return std::make_shared<impl::Vfs>(std::move(d), temp_dir);
}

Expand Down
11 changes: 4 additions & 7 deletions src/vfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ void VFile::perms(fs::perms prms, fs::perm_options opts) {
}
}

VRegularFile::VRegularFile(
std::intmax_t owner,
std::intmax_t group,
fs::perms perms)
: VFile(owner, group, perms) { }
VRegularFile::VRegularFile(fs::perms perms)
: VFile(perms) { }

std::shared_ptr<File> VDirectory::next(std::string const& name) const {
auto it = this->files_.find(name);
Expand Down Expand Up @@ -104,12 +101,12 @@ std::uintmax_t VDirectory::clear() {
}

std::pair<std::shared_ptr<RegularFile>, bool> VDirectory::emplace_regular_file(std::string const& name) {
auto [it, ok] = this->files_.emplace(name, std::make_shared<VRegularFile>(0, 0));
auto [it, ok] = this->files_.emplace(name, std::make_shared<VRegularFile>());
return std::make_pair(std::dynamic_pointer_cast<RegularFile>(it->second), ok);
}

std::pair<std::shared_ptr<Directory>, bool> VDirectory::emplace_directory(std::string const& name) {
auto [it, ok] = this->files_.emplace(name, std::make_shared<VDirectory>(0, 0));
auto [it, ok] = this->files_.emplace(name, std::make_shared<VDirectory>());
return std::make_pair(std::dynamic_pointer_cast<Directory>(it->second), ok);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/mem_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class TestMemFile: public testing::suites::TestFileFixture {
public:
std::shared_ptr<vfs::impl::Directory> make() {
return std::make_shared<vfs::impl::MemDirectory>(0, 0);
return std::make_shared<vfs::impl::MemDirectory>();
}
};

Expand Down
8 changes: 4 additions & 4 deletions tests/src/union_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace fs = std::filesystem;
class TestUnionFile: public testing::suites::TestFileFixture {
public:
std::shared_ptr<vfs::impl::Directory> make() {
auto upper = std::make_shared<vfs::impl::MemDirectory>(0, 0);
auto lower = std::make_shared<vfs::impl::MemDirectory>(0, 0);
auto upper = std::make_shared<vfs::impl::MemDirectory>();
auto lower = std::make_shared<vfs::impl::MemDirectory>();

return std::make_shared<vfs::impl::UnionDirectory>(std::move(upper), std::move(lower));
}
Expand All @@ -24,8 +24,8 @@ class TestUnionFile: public testing::suites::TestFileFixture {
METHOD_AS_TEST_CASE(testing::suites::TestFile<TestUnionFile>::test, "UnionFile");

TEST_CASE("UnionDirectory") {
auto upper = std::make_shared<vfs::impl::MemDirectory>(0, 0);
auto lower = std::make_shared<vfs::impl::MemDirectory>(0, 0);
auto upper = std::make_shared<vfs::impl::MemDirectory>();
auto lower = std::make_shared<vfs::impl::MemDirectory>();

// /
// + foo
Expand Down
2 changes: 1 addition & 1 deletion tests/src/vfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class TestVFile: public testing::suites::TestFileFixture {
public:
std::shared_ptr<vfs::impl::Directory> make() {
return std::make_shared<vfs::impl::VDirectory>(0, 0);
return std::make_shared<vfs::impl::VDirectory>();
}
};

Expand Down

0 comments on commit 7b2a01d

Please sign in to comment.