Add an interface for sys operations

Create a mock sys interface to test file operations.

Change-Id: Iad23394c600d66ee5de8e21db4702f38492c3058
Signed-off-by: Kun Yi <kunyi@google.com>
diff --git a/sys.cpp b/sys.cpp
new file mode 100644
index 0000000..0a16dcb
--- /dev/null
+++ b/sys.cpp
@@ -0,0 +1,41 @@
+#include "sys.hpp"
+
+#include <fcntl.h>
+#include <unistd.h>
+
+namespace binstore
+{
+
+namespace internal
+{
+
+int SysImpl::open(const char* pathname, int flags) const
+{
+    return ::open(pathname, flags);
+}
+
+int SysImpl::close(int fd) const
+{
+    return ::close(fd);
+}
+
+off_t SysImpl::lseek(int fd, off_t offset, int whence) const
+{
+    return ::lseek(fd, offset, whence);
+}
+
+ssize_t SysImpl::read(int fd, void* buf, size_t count) const
+{
+    return ::read(fd, buf, count);
+}
+
+ssize_t SysImpl::write(int fd, const void* buf, size_t count) const
+{
+    return ::write(fd, buf, count);
+}
+
+SysImpl sys_impl;
+
+} // namespace internal
+
+} // namespace binstore
diff --git a/sys.hpp b/sys.hpp
new file mode 100644
index 0000000..230665b
--- /dev/null
+++ b/sys.hpp
@@ -0,0 +1,46 @@
+#pragma once
+
+#include <unistd.h>
+
+namespace binstore
+{
+
+namespace internal
+{
+
+/** @class Sys
+ *  @brief Overridable direct syscall interface
+ *
+ *  TODO: factor this out into a syscall class shared by all upstream projects
+ */
+class Sys
+{
+  public:
+    virtual ~Sys() = default;
+    virtual int open(const char* pathname, int flags) const = 0;
+    virtual int close(int fd) const = 0;
+    virtual off_t lseek(int fd, off_t offset, int whence) const = 0;
+    virtual ssize_t read(int fd, void* buf, size_t count) const = 0;
+    virtual ssize_t write(int fd, const void* buf, size_t count) const = 0;
+};
+
+/** @class SysImpl
+ *  @brief syscall concrete implementation
+ *  @details Passes through all calls to the normal linux syscalls
+ */
+class SysImpl : public Sys
+{
+  public:
+    int open(const char* pathname, int flags) const override;
+    int close(int fd) const override;
+    off_t lseek(int fd, off_t offset, int whence) const override;
+    ssize_t read(int fd, void* buf, size_t count) const override;
+    ssize_t write(int fd, const void* buf, size_t count) const override;
+};
+
+/** @brief Default instantiation of sys */
+extern SysImpl sys_impl;
+
+} // namespace internal
+
+} // namespace binstore
diff --git a/test/sys_mock.hpp b/test/sys_mock.hpp
new file mode 100644
index 0000000..0af7d7c
--- /dev/null
+++ b/test/sys_mock.hpp
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "sys.hpp"
+
+#include <gmock/gmock.h>
+
+namespace binstore
+{
+namespace internal
+{
+
+class SysMock : public internal::Sys
+{
+  public:
+    MOCK_CONST_METHOD2(open, int(const char*, int));
+    MOCK_CONST_METHOD1(close, int(int));
+    MOCK_CONST_METHOD3(lseek, off_t(int, off_t, int));
+    MOCK_CONST_METHOD3(read, ssize_t(int, void*, size_t));
+    MOCK_CONST_METHOD3(write, ssize_t(int, const void*, size_t));
+};
+
+} // namespace internal
+
+} // namespace binstore