Skip to content

Commit

Permalink
POSIX: Implement mounts, symlinks and tmp_fs
Browse files Browse the repository at this point in the history
  • Loading branch information
avdgrinten committed Dec 7, 2016
1 parent 1d663ec commit 6974940
Show file tree
Hide file tree
Showing 8 changed files with 522 additions and 225 deletions.
4 changes: 1 addition & 3 deletions posix/subsystem/dir.makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
$(call standard_dirs)

$c_OBJECTS := main.o vfs.o exec.o
$c_OBJECTS += extern_fs.o
$c_OBJECTS += tmp_fs.o extern_fs.o
$c_OBJECTS += posix.pb.o fs.pb.o
#device.o vfs.o process.o exec.o \
# dev_fs.o pts_fs.o sysfile_fs.o extern_fs.o
$c_OBJECT_PATHS := $(addprefix $($c_OBJDIR)/,$($c_OBJECTS))

all-$c: $($c_BINDIR)/posix-subsystem
Expand Down
34 changes: 24 additions & 10 deletions posix/subsystem/src/extern_fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace extern_fs {

namespace {

struct OpenFile {
struct OpenFile : FileData {
OpenFile(int fd)
: _file(helix::UniqueDescriptor(__mlibc_getPassthrough(fd))) { }

Expand Down Expand Up @@ -41,29 +41,43 @@ struct OpenFile {
protocols::fs::File _file;
};

struct RegularNode {
RegularNode(int fd)
struct Regular : RegularData {
Regular(int fd)
: _fd(fd) { }

COFIBER_ROUTINE(FutureMaybe<SharedFile>, open(SharedEntry entry), ([=] {
COFIBER_RETURN(SharedFile::create<OpenFile>(std::move(entry), _fd));
COFIBER_ROUTINE(FutureMaybe<SharedFile>, open(), ([=] {
COFIBER_RETURN(SharedFile{std::make_shared<OpenFile>(_fd)});
}))

private:
int _fd;
};

struct DirectoryNode {
COFIBER_ROUTINE(FutureMaybe<SharedNode>, resolveChild(std::string name), ([=] {
struct Directory : TreeData {
COFIBER_ROUTINE(FutureMaybe<SharedLink>, mkdir(std::string name), ([=] {
(void)name;
assert(!"mkdir is not implemented for extern_fs");
}))

COFIBER_ROUTINE(FutureMaybe<SharedLink>, symlink(std::string name, std::string link), ([=] {
(void)name;
(void)link;
assert(!"symlink is not implemented for extern_fs");
}))

COFIBER_ROUTINE(FutureMaybe<SharedLink>, getLink(std::string name), ([=] {
int fd = open(name.c_str(), O_RDONLY);
COFIBER_RETURN(SharedNode::createRegular<RegularNode>(fd));
auto node = SharedNode{std::make_shared<Regular>(fd)};
// TODO: do not use createRoot here!
COFIBER_RETURN(SharedLink::createRoot(std::move(node)));
}))
};

} // anonymous namespace

SharedNode createRootNode() {
return SharedNode::createDirectory<DirectoryNode>();
SharedLink createRoot() {
auto node = SharedNode{std::make_shared<Directory>()};
return SharedLink::createRoot(std::move(node));
}

} // namespace extern_fs
Expand Down
2 changes: 1 addition & 1 deletion posix/subsystem/src/extern_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace extern_fs {

SharedNode createRootNode();
SharedLink createRoot();

} // namespace extern_fs

Expand Down
135 changes: 135 additions & 0 deletions posix/subsystem/src/tmp_fs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@

#include <set>

#include <protocols/fs/client.hpp>
#include "common.hpp"
#include "tmp_fs.hpp"

namespace tmp_fs {

namespace {

/*struct OpenFile {
OpenFile(int fd)
: _file(helix::UniqueDescriptor(__mlibc_getPassthrough(fd))) { }
COFIBER_ROUTINE(FutureMaybe<off_t>, seek(off_t offset, VfsSeek whence), ([=] {
assert(whence == VfsSeek::absolute);
COFIBER_AWAIT _file.seekAbsolute(offset);
COFIBER_RETURN(offset);
}))
COFIBER_ROUTINE(FutureMaybe<size_t>, readSome(void *data, size_t max_length), ([=] {
size_t length = COFIBER_AWAIT _file.readSome(data, max_length);
COFIBER_RETURN(length);
}))
COFIBER_ROUTINE(FutureMaybe<helix::UniqueDescriptor>, accessMemory(), ([=] {
auto memory = COFIBER_AWAIT _file.accessMemory();
COFIBER_RETURN(std::move(memory));
}))
helix::BorrowedDescriptor getPassthroughLane() {
return _file.getLane();
}
private:
protocols::fs::File _file;
};
struct RegularNode {
RegularNode(int fd)
: _fd(fd) { }
COFIBER_ROUTINE(FutureMaybe<SharedFile>, open(SharedEntry entry), ([=] {
COFIBER_RETURN(SharedFile::create<OpenFile>(std::move(entry), _fd));
}))
private:
int _fd;
};*/

struct Symlink : SymlinkData {
Symlink(std::string link)
: _link(std::move(link)) { }

COFIBER_ROUTINE(FutureMaybe<std::string>, readSymlink(), ([=] {
COFIBER_RETURN(_link);
}))

private:
std::string _link;
};

struct Directory : TreeData {
COFIBER_ROUTINE(FutureMaybe<SharedLink>, getLink(std::string name), ([=] {
auto it = _entries.find(name);
assert(it != _entries.end());
COFIBER_RETURN(SharedLink{*it});
}))

COFIBER_ROUTINE(FutureMaybe<SharedLink>, mkdir(std::string name), ([=] {
assert(_entries.find(name) == _entries.end());
auto node = SharedNode{std::make_shared<Directory>()};
auto link = std::make_shared<Link>(SharedNode{}, std::move(name), std::move(node));
_entries.insert(link);
COFIBER_RETURN(SharedLink{link});
}))

COFIBER_ROUTINE(FutureMaybe<SharedLink>, symlink(std::string name, std::string path), ([=] {
assert(_entries.find(name) == _entries.end());
auto node = SharedNode{std::make_shared<Symlink>(std::move(path))};
auto link = std::make_shared<Link>(SharedNode{}, std::move(name), std::move(node));
_entries.insert(link);
COFIBER_RETURN(SharedLink{link});
}))

private:
struct Link : LinkData {
explicit Link(SharedNode owner, std::string name, SharedNode target)
: owner(std::move(owner)), name(std::move(name)), target(std::move(target)) { }

SharedNode getOwner() override {
return owner;
}

std::string getName() override {
return name;
}

SharedNode getTarget() override {
return target;
}

SharedNode owner;
std::string name;
SharedNode target;
};

struct Compare {
struct is_transparent { };

bool operator() (const std::shared_ptr<Link> &link, const std::string &name) const {
return link->name < name;
}
bool operator() (const std::string &name, const std::shared_ptr<Link> &link) const {
return name < link->name;
}

bool operator() (const std::shared_ptr<Link> &a, const std::shared_ptr<Link> &b) const {
return a->name < b->name;
}
};

std::set<std::shared_ptr<Link>, Compare> _entries;
};

} // anonymous namespace

SharedLink createRoot() {
auto node = SharedNode{std::make_shared<Directory>()};
return SharedLink::createRoot(std::move(node));
}

} // namespace tmp_fs

14 changes: 14 additions & 0 deletions posix/subsystem/src/tmp_fs.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

#ifndef POSIX_SUBSYSTEM_TMP_FS_HPP
#define POSIX_SUBSYSTEM_TMP_FS_HPP

#include "vfs.hpp"

namespace tmp_fs {

SharedLink createRoot();

} // namespace tmp_fs

#endif // POSIX_SUBSYSTEM_TMP_FS_HPP

0 comments on commit 6974940

Please sign in to comment.