Skip to content

Commit

Permalink
Merge OsHandle into FsNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
kentonv committed Apr 27, 2017
1 parent e3c4dd6 commit 6d4c05d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
2 changes: 1 addition & 1 deletion c++/src/kj/filesystem-disk.c++
Expand Up @@ -1398,7 +1398,7 @@ protected:
#define FSNODE_METHODS(classname) \
Maybe<int> getFd() override { return DiskHandle::getFd(); } \
\
Own<OsHandle> cloneOsHandle() override { \
Own<FsNode> cloneFsNode() override { \
return heap<classname>(DiskHandle::clone()); \
} \
\
Expand Down
6 changes: 3 additions & 3 deletions c++/src/kj/filesystem.c++
Expand Up @@ -704,7 +704,7 @@ class InMemoryFile final: public File, public Refcounted {
public:
InMemoryFile(Clock& clock): clock(clock), lastModified(clock.now()) {}

Own<OsHandle> cloneOsHandle() override {
Own<FsNode> cloneFsNode() override {
return addRef(*this);
}

Expand Down Expand Up @@ -892,7 +892,7 @@ public:
InMemoryDirectory(Clock& clock)
: clock(clock), lastModified(clock.now()) {}

Own<OsHandle> cloneOsHandle() override {
Own<FsNode> cloneFsNode() override {
return addRef(*this);
}

Expand Down Expand Up @@ -1555,7 +1555,7 @@ class AppendableFileImpl final: public AppendableFile {
public:
AppendableFileImpl(Own<File>&& fileParam): file(kj::mv(fileParam)) {}

Own<OsHandle> cloneOsHandle() override {
Own<FsNode> cloneFsNode() override {
return heap<AppendableFileImpl>(file->clone());
}

Expand Down
37 changes: 15 additions & 22 deletions c++/src/kj/filesystem.h
Expand Up @@ -241,9 +241,11 @@ class PathPtr {
// L1). You can't do asynchronous RAM access so why asynchronous filesystem? The only way to
// parallelize these is using threads.

class OsHandle {
class FsNode {
// Base class for filesystem node types.

public:
Own<OsHandle> clone();
Own<FsNode> clone();
// Creates a new object of exactly the same type as this one, pointing at exactly the same
// external object.
//
Expand All @@ -252,19 +254,6 @@ class OsHandle {
virtual Maybe<int> getFd() = 0;
// Get the underlying file descriptor, if any. Returns nullptr if this object actually isn't
// wrapping a file descriptor.
//
// TODO(now): Do we really want everything inheriting OsHandle or should people dynamic_cast to
// it? What about people without RTTI?

protected:
virtual Own<OsHandle> cloneOsHandle() = 0;
// Implements clone(). Required to return an object with exactly the same type as this one.
// Hence, every subclass must implement this.
};

class FsNode: public OsHandle {
public:
Own<FsNode> clone();

enum class Type {
FILE,
Expand Down Expand Up @@ -310,6 +299,11 @@ class FsNode: public OsHandle {
// into the filesystem (*after* syncing the data), so than incomplete data is never visible to
// other processes. (In practice this works by writing into a temporary file and then rename()ing
// it.)

protected:
virtual Own<FsNode> cloneFsNode() = 0;
// Implements clone(). Required to return an object with exactly the same type as this one.
// Hence, every subclass must implement this.
};

class ReadableFile: public FsNode {
Expand Down Expand Up @@ -888,17 +882,16 @@ inline PathPtr PathPtr::slice(size_t start, size_t end) const {
return PathPtr(parts.slice(start, end));
}

inline Own<OsHandle> OsHandle::clone() { return cloneOsHandle(); }
inline Own<FsNode> FsNode::clone() { return cloneOsHandle().downcast<FsNode>(); }
inline Own<ReadableFile> ReadableFile::clone() { return cloneOsHandle().downcast<ReadableFile>(); }
inline Own<FsNode> FsNode::clone() { return cloneFsNode().downcast<FsNode>(); }
inline Own<ReadableFile> ReadableFile::clone() { return cloneFsNode().downcast<ReadableFile>(); }
inline Own<AppendableFile> AppendableFile::clone() {
return cloneOsHandle().downcast<AppendableFile>();
return cloneFsNode().downcast<AppendableFile>();
}
inline Own<File> File::clone() { return cloneOsHandle().downcast<File>(); }
inline Own<File> File::clone() { return cloneFsNode().downcast<File>(); }
inline Own<ReadableDirectory> ReadableDirectory::clone() {
return cloneOsHandle().downcast<ReadableDirectory>();
return cloneFsNode().downcast<ReadableDirectory>();
}
inline Own<Directory> Directory::clone() { return cloneOsHandle().downcast<Directory>(); }
inline Own<Directory> Directory::clone() { return cloneFsNode().downcast<Directory>(); }

inline void Directory::transfer(
PathPtr toPath, WriteMode toMode, PathPtr fromPath, TransferMode mode) {
Expand Down

0 comments on commit 6d4c05d

Please sign in to comment.