Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/envoy/api/io_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ template <typename ReturnValue> struct IoCallResult {
IoErrorPtr err_;
};

using IoCallBoolResult = IoCallResult<bool>;
using IoCallSizeResult = IoCallResult<ssize_t>;
using IoCallUint64Result = IoCallResult<uint64_t>;

inline Api::IoCallUint64Result ioCallUint64ResultNoError() {
Expand Down
4 changes: 4 additions & 0 deletions include/envoy/api/os_sys_calls.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#pragma once

#ifndef WIN32
#include <sys/ioctl.h>
#include <sys/mman.h> // for mode_t
#include <sys/socket.h> // for sockaddr
#include <sys/stat.h>
#include <sys/uio.h> // for iovec

#endif

#include <memory>
#include <string>

#include "envoy/api/os_sys_calls_common.h"
#include "envoy/common/pure.h"
#include "envoy/common/platform.h"

namespace Envoy {
namespace Api {
Expand Down
8 changes: 7 additions & 1 deletion include/envoy/common/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

// NOLINT(namespace-envoy)
#ifdef _MSC_VER
#include <malloc.h>
#include <stdint.h>

#define PACKED_STRUCT(definition, ...) \
__pragma(pack(push, 1)) definition, ##__VA_ARGS__; \
__pragma(pack(pop))

#ifdef _M_X64
using ssize_t = int64_t;
#else
#error Envoy is not supported on 32-bit Windows
#endif

#else
#define PACKED_STRUCT(definition, ...) definition, ##__VA_ARGS__ __attribute__((packed))

Expand Down
1 change: 1 addition & 0 deletions include/envoy/filesystem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ envoy_cc_library(
name = "filesystem_interface",
hdrs = ["filesystem.h"],
deps = [
"//include/envoy/api:io_error_interface",
"//include/envoy/api:os_sys_calls_interface",
"//include/envoy/event:dispatcher_interface",
],
Expand Down
25 changes: 7 additions & 18 deletions include/envoy/filesystem/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <memory>
#include <string>

#include "envoy/api/os_sys_calls.h"
#include "envoy/api/io_error.h"
#include "envoy/common/platform.h"
#include "envoy/common/pure.h"

#include "absl/strings/string_view.h"
Expand All @@ -25,37 +26,31 @@ class File {
*
* @return bool whether the open succeeded
*/
virtual Api::SysCallBoolResult open() PURE;
virtual Api::IoCallBoolResult open() PURE;

/**
* Write the buffer to the file. The file must be explicitly opened before writing.
*
* @return ssize_t number of bytes written, or -1 for failure
*/
virtual Api::SysCallSizeResult write(absl::string_view buffer) PURE;
virtual Api::IoCallSizeResult write(absl::string_view buffer) PURE;

/**
* Close the file.
*
* @return bool whether the close succeeded
*/
virtual Api::SysCallBoolResult close() PURE;
virtual Api::IoCallBoolResult close() PURE;

/**
* @return bool is the file open
*/
virtual bool isOpen() PURE;
virtual bool isOpen() const PURE;

/**
* @return string the file path
*/
virtual std::string path() PURE;

/**
* @return string a human-readable string describing the error code
* TODO(sesmith177) Use the IOError class after #5829 merges
*/
virtual std::string errorToString(int error) PURE;
virtual std::string path() const PURE;
};

using FilePtr = std::unique_ptr<File>;
Expand Down Expand Up @@ -97,12 +92,6 @@ class Instance {
*/
virtual std::string fileReadToEnd(const std::string& path) PURE;

/**
* @param path some filesystem path.
* @return SysCallStringResult containing the canonical path (see realpath(3)).
*/
virtual Api::SysCallStringResult canonicalPath(const std::string& path) PURE;

/**
* Determine if the path is on a list of paths Envoy will refuse to access. This
* is a basic sanity check for users, blacklisting some clearly bad paths. Paths
Expand Down
16 changes: 8 additions & 8 deletions source/common/access_log/access_log_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ AccessLogFileImpl::AccessLogFileImpl(Filesystem::FilePtr&& file, Event::Dispatch
}

void AccessLogFileImpl::open() {
const Api::SysCallBoolResult result = file_->open();
const Api::IoCallBoolResult result = file_->open();
if (!result.rc_) {
throw EnvoyException(fmt::format("unable to open file '{}': {}", file_->path(),
file_->errorToString(result.errno_)));
throw EnvoyException(
fmt::format("unable to open file '{}': {}", file_->path(), result.err_->getErrorDetails()));
}
}

Expand All @@ -68,9 +68,9 @@ AccessLogFileImpl::~AccessLogFileImpl() {
doWrite(flush_buffer_);
}

const Api::SysCallBoolResult result = file_->close();
const Api::IoCallBoolResult result = file_->close();
ASSERT(result.rc_, fmt::format("unable to close file '{}': {}", file_->path(),
file_->errorToString(result.errno_)));
result.err_->getErrorDetails()));
}
}

Expand All @@ -91,7 +91,7 @@ void AccessLogFileImpl::doWrite(Buffer::Instance& buffer) {
Thread::LockGuard lock(file_lock_);
for (const Buffer::RawSlice& slice : slices) {
absl::string_view data(static_cast<char*>(slice.mem_), slice.len_);
const Api::SysCallSizeResult result = file_->write(data);
const Api::IoCallSizeResult result = file_->write(data);
ASSERT(result.rc_ == static_cast<ssize_t>(slice.len_));
stats_.write_completed_.inc();
}
Expand Down Expand Up @@ -131,9 +131,9 @@ void AccessLogFileImpl::flushThreadFunc() {
try {
if (reopen_file_) {
reopen_file_ = false;
const Api::SysCallBoolResult result = file_->close();
const Api::IoCallBoolResult result = file_->close();
ASSERT(result.rc_, fmt::format("unable to close file '{}': {}", file_->path(),
file_->errorToString(result.errno_)));
result.err_->getErrorDetails()));
open();
}

Expand Down
1 change: 0 additions & 1 deletion source/common/api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ envoy_cc_library(
"//include/envoy/api:api_interface",
"//source/common/common:thread_lib",
"//source/common/event:dispatcher_lib",
"//source/common/filesystem:filesystem_lib",
],
)

Expand Down
5 changes: 3 additions & 2 deletions source/common/api/api_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
namespace Envoy {
namespace Api {

Impl::Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system)
: thread_factory_(thread_factory), time_system_(time_system) {}
Impl::Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system,
Filesystem::Instance& file_system)
: thread_factory_(thread_factory), time_system_(time_system), file_system_(file_system) {}

Event::DispatcherPtr Impl::allocateDispatcher() {
return std::make_unique<Event::DispatcherImpl>(*this, time_system_);
Expand Down
7 changes: 3 additions & 4 deletions source/common/api/api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include "envoy/filesystem/filesystem.h"
#include "envoy/thread/thread.h"

#include "common/filesystem/filesystem_impl.h"

namespace Envoy {
namespace Api {

Expand All @@ -18,7 +16,8 @@ namespace Api {
*/
class Impl : public Api {
public:
Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system);
Impl(Thread::ThreadFactory& thread_factory, Stats::Store&, Event::TimeSystem& time_system,
Filesystem::Instance& file_system);

// Api::Api
Event::DispatcherPtr allocateDispatcher() override;
Expand All @@ -29,8 +28,8 @@ class Impl : public Api {

private:
Thread::ThreadFactory& thread_factory_;
Filesystem::InstanceImpl file_system_;
Event::TimeSystem& time_system_;
Filesystem::Instance& file_system_;
};

} // namespace Api
Expand Down
30 changes: 28 additions & 2 deletions source/common/filesystem/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,36 @@ envoy_cc_posix_library(

envoy_cc_library(
name = "filesystem_lib",
srcs = ["filesystem_impl.cc"],
hdrs = ["filesystem_impl.h"],
deps = envoy_cc_platform_dep("filesystem_impl_lib"),
)

envoy_cc_posix_library(
name = "filesystem_impl_lib",
srcs = ["posix/filesystem_impl.cc"],
hdrs = ["posix/filesystem_impl.h"],
strip_include_prefix = "posix",
deps = [
":file_shared_lib",
],
)

envoy_cc_win32_library(
name = "filesystem_impl_lib",
srcs = ["win32/filesystem_impl.cc"],
hdrs = ["win32/filesystem_impl.h"],
strip_include_prefix = "win32",
deps = [
":file_shared_lib",
],
)

envoy_cc_library(
name = "file_shared_lib",
srcs = ["file_shared_impl.cc"],
hdrs = ["file_shared_impl.h"],
deps = [
"//include/envoy/filesystem:filesystem_interface",
"//source/common/common:assert_lib",
],
)

Expand Down
39 changes: 39 additions & 0 deletions source/common/filesystem/file_shared_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "common/filesystem/file_shared_impl.h"

#include <cstring>

namespace Envoy {
namespace Filesystem {

Api::IoError::IoErrorCode IoFileError::getErrorCode() const { return IoErrorCode::UnknownError; }

std::string IoFileError::getErrorDetails() const { return ::strerror(errno_); }

Api::IoCallBoolResult FileSharedImpl::open() {
if (isOpen()) {
return resultSuccess<bool>(true);
}

openFile();
return fd_ != -1 ? resultSuccess<bool>(true) : resultFailure<bool>(false, errno);
}

Api::IoCallSizeResult FileSharedImpl::write(absl::string_view buffer) {
const ssize_t rc = writeFile(buffer);
return rc != -1 ? resultSuccess<ssize_t>(rc) : resultFailure<ssize_t>(rc, errno);
};

Api::IoCallBoolResult FileSharedImpl::close() {
ASSERT(isOpen());

bool success = closeFile();
fd_ = -1;
return success ? resultSuccess<bool>(true) : resultFailure<bool>(false, errno);
}

bool FileSharedImpl::isOpen() const { return fd_ != -1; };

std::string FileSharedImpl::path() const { return path_; };

} // namespace Filesystem
} // namespace Envoy
61 changes: 61 additions & 0 deletions source/common/filesystem/file_shared_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <string>

#include "envoy/filesystem/filesystem.h"

#include "common/common/assert.h"

namespace Envoy {
namespace Filesystem {

class IoFileError : public Api::IoError {
public:
explicit IoFileError(int sys_errno) : errno_(sys_errno) {}

~IoFileError() override {}

Api::IoError::IoErrorCode getErrorCode() const override;
std::string getErrorDetails() const override;

private:
const int errno_;
};

using IoFileErrorPtr = std::unique_ptr<IoFileError, Api::IoErrorDeleterType>;

template <typename T> Api::IoCallResult<T> resultFailure(T result, int sys_errno) {
return {result, IoFileErrorPtr(new IoFileError(sys_errno), [](Api::IoError* err) {
ASSERT(err != nullptr);
delete err;
})};
}

template <typename T> Api::IoCallResult<T> resultSuccess(T result) {
return {result, IoFileErrorPtr(nullptr, [](Api::IoError*) { NOT_REACHED_GCOVR_EXCL_LINE; })};
}

class FileSharedImpl : public File {
public:
FileSharedImpl(const std::string& path) : fd_(-1), path_(path) {}

virtual ~FileSharedImpl() {}

// Filesystem::File
Api::IoCallBoolResult open() override;
Api::IoCallSizeResult write(absl::string_view buffer) override;
Api::IoCallBoolResult close() override;
bool isOpen() const override;
std::string path() const override;

protected:
virtual void openFile() PURE;
virtual ssize_t writeFile(absl::string_view buffer) PURE;
virtual bool closeFile() PURE;

int fd_;
const std::string path_;
};

} // namespace Filesystem
} // namespace Envoy
Loading