Skip to content

Commit

Permalink
Remove all cmake detection
Browse files Browse the repository at this point in the history
Use C++'s own overload resolution to test for function existence
  • Loading branch information
netheril96 committed Feb 8, 2017
1 parent 8e20341 commit d207323
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 105 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif ()

include(FeatureDetection)

include_directories(${PROJECT_SOURCE_DIR}/sources)
set(EXTERNAL_DIR ${PROJECT_SOURCE_DIR}/external)
include_directories(${EXTERNAL_DIR})
Expand Down
47 changes: 0 additions & 47 deletions cmake/FeatureDetection.cmake

This file was deleted.

9 changes: 5 additions & 4 deletions sources/btree_dir.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ class BtreeNode
: m_parent_num(parent), m_num(num), m_dirty(false)
{
}
BtreeNode(BtreeNode&& other) noexcept : m_parent_num(other.m_parent_num),
m_num(other.m_num),
m_child_indices(std::move(other.m_child_indices)),
m_entries(std::move(other.m_entries))
BtreeNode(BtreeNode&& other) noexcept
: m_parent_num(other.m_parent_num)
, m_num(other.m_num)
, m_child_indices(std::move(other.m_child_indices))
, m_entries(std::move(other.m_entries))
{
std::swap(m_dirty, other.m_dirty);
other.m_num = INVALID_PAGE;
Expand Down
2 changes: 1 addition & 1 deletion sources/lite_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace lite

void* init(struct fuse_conn_info* fsinfo)
{
#ifdef CAN_SET_FUSE_CONN_INFO
#ifdef FUSE_CAP_BIG_WRITES
fsinfo->want |= FUSE_CAP_BIG_WRITES;
fsinfo->max_readahead = static_cast<unsigned>(-1);
fsinfo->max_write = static_cast<unsigned>(-1);
Expand Down
11 changes: 6 additions & 5 deletions sources/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,15 @@ extern Logger* global_logger;
#define GENERIC_LOG(log_level, ...) \
do \
{ \
using securefs::global_logger; \
if (global_logger && global_logger->get_level() <= log_level) \
{ \
global_logger->log(log_level, __VA_ARGS__); \
} \
} while (0)
#define TRACE_LOG(...) GENERIC_LOG(kLogTrace, __VA_ARGS__)
#define DEBUG_LOG(...) GENERIC_LOG(kLogDebug, __VA_ARGS__)
#define INFO_LOG(...) GENERIC_LOG(kLogInfo, __VA_ARGS__)
#define WARN_LOG(...) GENERIC_LOG(kLogWarning, __VA_ARGS__)
#define ERROR_LOG(...) GENERIC_LOG(kLogError, __VA_ARGS__)
#define TRACE_LOG(...) GENERIC_LOG(securefs::kLogTrace, __VA_ARGS__)
#define DEBUG_LOG(...) GENERIC_LOG(securefs::kLogDebug, __VA_ARGS__)
#define INFO_LOG(...) GENERIC_LOG(securefs::kLogInfo, __VA_ARGS__)
#define WARN_LOG(...) GENERIC_LOG(securefs::kLogWarning, __VA_ARGS__)
#define ERROR_LOG(...) GENERIC_LOG(securefs::kLogError, __VA_ARGS__)
}
2 changes: 1 addition & 1 deletion sources/operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ namespace operations

void* init(struct fuse_conn_info* fsinfo)
{
#ifdef CAN_SET_FUSE_CONN_INFO
#ifdef FUSE_CAP_BIG_WRITES
fsinfo->want |= FUSE_CAP_BIG_WRITES;
fsinfo->max_readahead = static_cast<unsigned>(-1);
fsinfo->max_write = static_cast<unsigned>(-1);
Expand Down
6 changes: 3 additions & 3 deletions sources/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ enum class Color
class OSService
{
private:
#ifdef HAS_AT_FUNCTIONS
int m_dir_fd;
#elif defined(WIN32)
#if defined(WIN32)
void* m_root_handle;
#else
int m_dir_fd;
#endif
std::string m_dir_name;

Expand Down
91 changes: 49 additions & 42 deletions sources/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,33 @@
#include <sys/xattr.h>
#endif

#ifndef AT_FDCWD
#define AT_FDCWD -2
#endif
// This provides definition for futimens when it is not already defined (e.g. on Apple)
// When it is defined, the template is selected later in overload resolution
template <class T>
static int futimens(T fd, const struct fuse_timespec ts[2])
{
int rc;
if (!ts)
rc = ::futimes(fd, nullptr);
else
{
struct timeval time_values[2];
for (size_t i = 0; i < 2; ++i)
{
time_values[i].tv_sec = ts[i].tv_sec;
time_values[i].tv_usec
= static_cast<decltype(time_values[i].tv_usec)>(ts[i].tv_nsec / 1000);
}
rc = ::futimes(fd, time_values);
}
return rc;
}

template <class T>
static int utimensat(int, const char*, const struct fuse_timespec*, T)
{
return 1;
}

namespace securefs
{
Expand Down Expand Up @@ -127,24 +151,7 @@ class UnixFileStream final : public FileStream

void utimens(const struct fuse_timespec ts[2]) override
{
int rc;
#ifdef HAS_FUTIMENS
rc = ::futimens(m_fd, ts);
#else
if (!ts)
rc = ::futimes(m_fd, nullptr);
else
{
struct timeval time_values[2];
for (size_t i = 0; i < 2; ++i)
{
time_values[i].tv_sec = ts[i].tv_sec;
time_values[i].tv_usec
= static_cast<decltype(time_values[i].tv_usec)>(ts[i].tv_nsec / 1000);
}
rc = ::futimes(m_fd, time_values);
}
#endif
int rc = ::futimens(m_fd, ts);
if (rc < 0)
THROW_POSIX_EXCEPTION(errno, "utimens");
}
Expand Down Expand Up @@ -240,7 +247,7 @@ class UnixDirectoryTraverser : public DirectoryTraverser

std::string OSService::norm_path(StringRef path) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
if (m_dir_fd < 0)
return path.to_string();
#endif
Expand All @@ -251,14 +258,14 @@ std::string OSService::norm_path(StringRef path) const

OSService::OSService()
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
m_dir_fd = AT_FDCWD;
#endif
}

OSService::~OSService()
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
if (m_dir_fd >= 0)
::close(m_dir_fd);
#endif
Expand All @@ -274,7 +281,7 @@ OSService::OSService(StringRef path)
m_dir_name.assign(buffer);
m_dir_name.push_back('/');

#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int dir_fd = ::open(path.c_str(), O_RDONLY);
if (dir_fd < 0)
THROW_POSIX_EXCEPTION(errno, "Opening directory " + path);
Expand All @@ -285,7 +292,7 @@ OSService::OSService(StringRef path)
std::shared_ptr<FileStream>
OSService::open_file_stream(StringRef path, int flags, unsigned mode) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int fd = ::openat(m_dir_fd, path.c_str(), flags, mode);
#else
int fd = ::open(norm_path(path).c_str(), flags, mode);
Expand All @@ -298,7 +305,7 @@ OSService::open_file_stream(StringRef path, int flags, unsigned mode) const

void OSService::remove_file(StringRef path) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int rc = ::unlinkat(m_dir_fd, path.c_str(), 0) == 0;
#else
int rc = ::unlink(norm_path(path).c_str()) == 0;
Expand All @@ -309,7 +316,7 @@ void OSService::remove_file(StringRef path) const

void OSService::remove_directory(StringRef path) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_REMOVEDIR
int rc = ::unlinkat(m_dir_fd, path.c_str(), AT_REMOVEDIR);
#else
int rc = ::rmdir(norm_path(path).c_str()) == 0;
Expand All @@ -328,7 +335,7 @@ void OSService::lock() const

void OSService::mkdir(StringRef path, unsigned mode) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int rc = ::mkdirat(m_dir_fd, path.c_str(), mode);
#else
int rc = ::mkdir(norm_path(path).c_str(), mode);
Expand All @@ -340,7 +347,7 @@ void OSService::mkdir(StringRef path, unsigned mode) const

void OSService::symlink(StringRef to, StringRef from) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int rc = ::symlinkat(to.c_str(), m_dir_fd, from.c_str());
#else
int rc = ::symlink(to.c_str(), norm_path(from).c_str());
Expand All @@ -352,7 +359,7 @@ void OSService::symlink(StringRef to, StringRef from) const

void OSService::link(StringRef source, StringRef dest) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int rc = ::linkat(m_dir_fd, source.c_str(), m_dir_fd, dest.c_str(), 0);
#else
int rc = ::link(norm_path(source).c_str(), norm_path(dest).c_str());
Expand All @@ -373,7 +380,7 @@ void OSService::statfs(struct fuse_statvfs* fs_info) const

void OSService::rename(StringRef a, StringRef b) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
int rc = ::renameat(m_dir_fd, a.c_str(), m_dir_fd, b.c_str());
#else
int rc = ::rename(norm_path(a).c_str(), norm_path(b).c_str());
Expand All @@ -385,7 +392,7 @@ void OSService::rename(StringRef a, StringRef b) const

bool OSService::stat(StringRef path, struct fuse_stat* stat) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_SYMLINK_NOFOLLOW
int rc = ::fstatat(m_dir_fd, path.c_str(), stat, AT_SYMLINK_NOFOLLOW);
#else
int rc = ::lstat(norm_path(path).c_str(), stat);
Expand All @@ -401,7 +408,7 @@ bool OSService::stat(StringRef path, struct fuse_stat* stat) const

void OSService::chmod(StringRef path, fuse_mode_t mode) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_SYMLINK_NOFOLLOW
int rc = ::fchmodat(m_dir_fd, path.c_str(), mode, AT_SYMLINK_NOFOLLOW);
#else
int rc = ::lchmod(norm_path(path).c_str(), mode);
Expand All @@ -413,7 +420,7 @@ void OSService::chmod(StringRef path, fuse_mode_t mode) const

ssize_t OSService::readlink(StringRef path, char* output, size_t size) const
{
#ifdef HAS_AT_FUNCTIONS
#ifdef AT_FDCWD
ssize_t rc = ::readlinkat(m_dir_fd, path.c_str(), output, size);
#else
ssize_t rc = ::readlink(norm_path(path).c_str(), output, size);
Expand All @@ -426,12 +433,13 @@ ssize_t OSService::readlink(StringRef path, char* output, size_t size) const

void OSService::utimens(StringRef path, const fuse_timespec* ts) const
{
#if defined(HAS_AT_FUNCTIONS) && defined(HAS_FUTIMENS)
int rc = ::utimensat(m_dir_fd, path.c_str(), ts, AT_SYMLINK_NOFOLLOW);
if (rc == 0)
return;
if (rc < 0)
THROW_POSIX_EXCEPTION(errno, "utimensat");
#else
int rc;

// When controls flows to here, the stub function has been called
if (!ts)
{
rc = ::lutimes(norm_path(path).c_str(), nullptr);
Expand All @@ -447,7 +455,6 @@ void OSService::utimens(StringRef path, const fuse_timespec* ts) const
}
if (rc < 0)
THROW_POSIX_EXCEPTION(errno, "lutimes");
#endif
}

std::unique_ptr<DirectoryTraverser> OSService::create_traverser(StringRef dir) const
Expand Down Expand Up @@ -487,7 +494,7 @@ int OSService::raise_fd_limit()

void OSService::get_current_time(fuse_timespec& current_time)
{
#ifdef HAS_CLOCK_GETTIME
#ifdef CLOCK_REALTIME
clock_gettime(CLOCK_REALTIME, &current_time);
#else
timeval tv;
Expand Down Expand Up @@ -529,7 +536,7 @@ void OSService::read_password_no_confirmation(const char* prompt,
CryptoPP::AlignedSecByteBlock* output)
{
byte buffer[4000];
DEFER(CryptoPP::SecureWipeBuffer(buffer, sizeof(buffer)));
DEFER(CryptoPP::SecureWipeBuffer(buffer, array_length(buffer)));
size_t bufsize = 0;

struct termios old_tios, new_tios;
Expand All @@ -551,7 +558,7 @@ void OSService::read_password_no_confirmation(const char* prompt,
int c = getchar();
if (c == '\r' || c == '\n' || c == EOF)
break;
if (bufsize < sizeof(buffer))
if (bufsize < array_length(buffer))
{
buffer[bufsize] = static_cast<byte>(c);
++bufsize;
Expand Down

0 comments on commit d207323

Please sign in to comment.