Skip to content

Commit

Permalink
Phase out StringRef part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
netheril96 committed Feb 27, 2024
1 parent 1c75aa1 commit 84d2332
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 92 deletions.
11 changes: 6 additions & 5 deletions sources/common_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const OSService& OSService::get_default()
return service;
}

std::string OSService::temp_name(StringRef prefix, StringRef suffix)
std::string OSService::temp_name(const std::string& prefix, const std::string& suffix)
{
byte random[16];
generate_random(random, array_length(random));
Expand All @@ -22,7 +22,7 @@ std::string OSService::temp_name(StringRef prefix, StringRef suffix)
return result;
}

void OSService::ensure_directory(StringRef path, unsigned mode) const
void OSService::ensure_directory(const std::string& path, unsigned mode) const
{
try
{
Expand All @@ -35,7 +35,7 @@ void OSService::ensure_directory(StringRef path, unsigned mode) const
}
}

bool OSService::remove_file_nothrow(StringRef path) const noexcept
bool OSService::remove_file_nothrow(const std::string& path) const noexcept
{
try
{
Expand All @@ -48,7 +48,7 @@ bool OSService::remove_file_nothrow(StringRef path) const noexcept
}
}

bool OSService::remove_directory_nothrow(StringRef path) const noexcept
bool OSService::remove_directory_nothrow(const std::string& path) const noexcept
{
try
{
Expand All @@ -61,7 +61,8 @@ bool OSService::remove_directory_nothrow(StringRef path) const noexcept
}
}

void OSService::recursive_traverse(StringRef dir, const recursive_traverse_callback& callback) const
void OSService::recursive_traverse(const std::string& dir,
const recursive_traverse_callback& callback) const
{
auto traverser = create_traverser(dir);
std::string name;
Expand Down
12 changes: 12 additions & 0 deletions sources/mystring.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>

#include <absl/strings/str_format.h>
#include <absl/strings/string_view.h>

typedef unsigned char byte;
Expand Down Expand Up @@ -66,6 +67,7 @@ class BasicStringRef
{
return {data(), size()};
}
operator std::basic_string<CharT>() const { return {data(), size()}; }
};

template <class CharT>
Expand Down Expand Up @@ -131,6 +133,16 @@ bool operator!=(BasicStringRef<CharT> a, const char* b)
typedef BasicStringRef<char> StringRef;
typedef BasicStringRef<wchar_t> WideStringRef;

inline absl::FormatConvertResult<absl::FormatConversionCharSet::kString>
AbslFormatConvert(const StringRef& p, const absl::FormatConversionSpec& spec, absl::FormatSink* s)
{
if (spec.conversion_char() == absl::FormatConversionChar::s)
{
absl::Format(s, "%s", absl::string_view(p));
}
return {true};
}

std::string hexify(const byte* data, size_t length);
void parse_hex(StringRef hex, byte* output, size_t len);

Expand Down
66 changes: 40 additions & 26 deletions sources/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,17 @@ typedef std::string native_string_type;
#endif

#ifdef WIN32
std::wstring widen_string(StringRef str);
std::string narrow_string(WideStringRef str);
std::wstring widen_string(const char* str, size_t size);
inline std::wstring widen_string(absl::string_view str)
{
return widen_string(str.data(), str.size());
}
std::string narrow_string(const wchar_t* str, size_t size);
inline std::string narrow_string(const std::wstring& str)
{
return narrow_string(str.data(), str.size());
}
inline std::string narrow_string(const wchar_t* str) { return narrow_string(str, wcslen(str)); }
[[noreturn]] void throw_windows_exception(const wchar_t* func_name);
void windows_init(void);
#endif
Expand All @@ -156,41 +165,46 @@ class OSService
std::string m_dir_name;

public:
static bool is_absolute(StringRef path);
static native_string_type concat_and_norm(StringRef base_dir, StringRef path);
native_string_type norm_path(StringRef path) const { return concat_and_norm(m_dir_name, path); }
static bool is_absolute(const std::string& path);
static native_string_type concat_and_norm(const std::string& base_dir, const std::string& path);
native_string_type norm_path(const std::string& path) const
{
return concat_and_norm(m_dir_name, path);
}

public:
OSService();
explicit OSService(StringRef path);
explicit OSService(const std::string& path);
~OSService();
std::shared_ptr<FileStream> open_file_stream(StringRef path, int flags, unsigned mode) const;
bool remove_file_nothrow(StringRef path) const noexcept;
bool remove_directory_nothrow(StringRef path) const noexcept;
void remove_file(StringRef path) const;
void remove_directory(StringRef path) const;

void rename(StringRef a, StringRef b) const;
std::shared_ptr<FileStream>
open_file_stream(const std::string& path, int flags, unsigned mode) const;
bool remove_file_nothrow(const std::string& path) const noexcept;
bool remove_directory_nothrow(const std::string& path) const noexcept;
void remove_file(const std::string& path) const;
void remove_directory(const std::string& path) const;

void rename(const std::string& a, const std::string& b) const;
void lock() const;
void ensure_directory(StringRef path, unsigned mode) const;
void mkdir(StringRef path, unsigned mode) const;
void ensure_directory(const std::string& path, unsigned mode) const;
void mkdir(const std::string& path, unsigned mode) const;
void statfs(struct fuse_statvfs*) const;
void utimens(StringRef path, const fuse_timespec ts[2]) const;
void utimens(const std::string& path, const fuse_timespec ts[2]) const;

// Returns false when the path does not exist; throw exceptions on other errors
// The ENOENT errors are too frequent so the API is redesigned
bool stat(StringRef path, struct fuse_stat* stat) const;
bool stat(const std::string& path, struct fuse_stat* stat) const;

void link(StringRef source, StringRef dest) const;
void chmod(StringRef path, fuse_mode_t mode) const;
void chown(StringRef path, fuse_uid_t uid, fuse_gid_t gid) const;
ssize_t readlink(StringRef path, char* output, size_t size) const;
void symlink(StringRef source, StringRef dest) const;
void link(const std::string& source, const std::string& dest) const;
void chmod(const std::string& path, fuse_mode_t mode) const;
void chown(const std::string& path, fuse_uid_t uid, fuse_gid_t gid) const;
ssize_t readlink(const std::string& path, char* output, size_t size) const;
void symlink(const std::string& source, const std::string& dest) const;

typedef std::function<void(StringRef, StringRef)> recursive_traverse_callback;
void recursive_traverse(StringRef dir, const recursive_traverse_callback& callback) const;
typedef std::function<void(const std::string&, const std::string&)> recursive_traverse_callback;
void recursive_traverse(const std::string& dir,
const recursive_traverse_callback& callback) const;

std::unique_ptr<DirectoryTraverser> create_traverser(StringRef dir) const;
std::unique_ptr<DirectoryTraverser> create_traverser(const std::string& dir) const;

#ifdef __APPLE__
// These APIs, unlike all others, report errors through negative error numbers as defined in
Expand All @@ -206,7 +220,7 @@ class OSService
static uint32_t getgid() noexcept;
static int64_t raise_fd_limit() noexcept;

static std::string temp_name(StringRef prefix, StringRef suffix);
static std::string temp_name(const std::string& prefix, const std::string& suffix);
static const OSService& get_default();
static void get_current_time(fuse_timespec& out);
static void get_current_time_in_tm(struct tm* tm, int* nanoseconds);
Expand Down
34 changes: 17 additions & 17 deletions sources/unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class UnixDirectoryTraverser : public DirectoryTraverser
DIR* m_dir;

public:
explicit UnixDirectoryTraverser(StringRef path)
explicit UnixDirectoryTraverser(const std::string& path)
{
m_dir = ::opendir(path.c_str());
if (!m_dir)
Expand Down Expand Up @@ -242,9 +242,9 @@ class UnixDirectoryTraverser : public DirectoryTraverser
void rewind() override { ::rewinddir(m_dir); }
};

bool OSService::is_absolute(StringRef path) { return path.size() > 0 && path[0] == '/'; }
bool OSService::is_absolute(const std::string& path) { return path.size() > 0 && path[0] == '/'; }

native_string_type OSService::concat_and_norm(StringRef base_dir, StringRef path)
native_string_type OSService::concat_and_norm(const std::string& base_dir, const std::string& path)
{
if (base_dir.empty() || is_absolute(path))
return path.to_string();
Expand All @@ -267,7 +267,7 @@ OSService::~OSService()
::close(m_dir_fd);
}

OSService::OSService(StringRef path)
OSService::OSService(const std::string& path)
{
char buffer[PATH_MAX + 1] = {0};
char* rc = ::realpath(path.c_str(), buffer);
Expand All @@ -284,7 +284,7 @@ OSService::OSService(StringRef path)
}

std::shared_ptr<FileStream>
OSService::open_file_stream(StringRef path, int flags, unsigned mode) const
OSService::open_file_stream(const std::string& path, int flags, unsigned mode) const
{
int fd = ::openat(m_dir_fd, path.c_str(), flags, mode);
if (fd < 0)
Expand All @@ -293,14 +293,14 @@ OSService::open_file_stream(StringRef path, int flags, unsigned mode) const
return std::make_shared<UnixFileStream>(fd);
}

void OSService::remove_file(StringRef path) const
void OSService::remove_file(const std::string& path) const
{
int rc = ::unlinkat(m_dir_fd, path.c_str(), 0);
if (rc < 0)
THROW_POSIX_EXCEPTION(errno, "unlinking " + norm_path(path));
}

void OSService::remove_directory(StringRef path) const
void OSService::remove_directory(const std::string& path) const
{
int rc = ::unlinkat(m_dir_fd, path.c_str(), AT_REMOVEDIR);
if (rc < 0)
Expand All @@ -319,23 +319,23 @@ void OSService::lock() const
absl::StrFormat("Fail to obtain exclusive lock on %s", m_dir_name));
}

void OSService::mkdir(StringRef path, unsigned mode) const
void OSService::mkdir(const std::string& path, unsigned mode) const
{
int rc = ::mkdirat(m_dir_fd, path.c_str(), mode);
if (rc < 0)
THROW_POSIX_EXCEPTION(errno,
absl::StrFormat("Fail to create directory %s", norm_path(path)));
}

void OSService::symlink(StringRef to, StringRef from) const
void OSService::symlink(const std::string& to, const std::string& from) const
{
int rc = ::symlinkat(to.c_str(), m_dir_fd, from.c_str());
if (rc < 0)
THROW_POSIX_EXCEPTION(errno,
absl::StrFormat("symlink to=%s and from=%s", to, norm_path(from)));
}

void OSService::link(StringRef source, StringRef dest) const
void OSService::link(const std::string& source, const std::string& dest) const
{
int rc = ::linkat(m_dir_fd, source.c_str(), m_dir_fd, dest.c_str(), 0);
if (rc < 0)
Expand All @@ -351,15 +351,15 @@ void OSService::statfs(struct fuse_statvfs* fs_info) const
THROW_POSIX_EXCEPTION(errno, "statvfs");
}

void OSService::rename(StringRef a, StringRef b) const
void OSService::rename(const std::string& a, const std::string& b) const
{
int rc = ::renameat(m_dir_fd, a.c_str(), m_dir_fd, b.c_str());
if (rc < 0)
THROW_POSIX_EXCEPTION(
errno, absl::StrFormat("Renaming from %s to %s", norm_path(a), norm_path(b)));
}

bool OSService::stat(StringRef path, struct fuse_stat* stat) const
bool OSService::stat(const std::string& path, struct fuse_stat* stat) const
{
int rc = ::fstatat(m_dir_fd, path.c_str(), stat, AT_SYMLINK_NOFOLLOW);
if (rc < 0)
Expand All @@ -371,7 +371,7 @@ bool OSService::stat(StringRef path, struct fuse_stat* stat) const
return true;
}

void OSService::chmod(StringRef path, fuse_mode_t mode) const
void OSService::chmod(const std::string& path, fuse_mode_t mode) const
{
int rc = ::fchmodat(m_dir_fd, path.c_str(), mode, AT_SYMLINK_NOFOLLOW);
if (rc < 0 && errno == ENOTSUP)
Expand All @@ -381,7 +381,7 @@ void OSService::chmod(StringRef path, fuse_mode_t mode) const
absl::StrFormat("chmod %s with mode=0%o", norm_path(path), mode));
}

void OSService::chown(StringRef path, uid_t uid, gid_t gid) const
void OSService::chown(const std::string& path, uid_t uid, gid_t gid) const
{
int rc = ::fchownat(m_dir_fd, path.c_str(), uid, gid, AT_SYMLINK_NOFOLLOW);
if (rc < 0 && errno == ENOTSUP)
Expand All @@ -391,7 +391,7 @@ void OSService::chown(StringRef path, uid_t uid, gid_t gid) const
errno, absl::StrFormat("chown %s with uid=%d and gid=%d", norm_path(path), uid, gid));
}

ssize_t OSService::readlink(StringRef path, char* output, size_t size) const
ssize_t OSService::readlink(const std::string& path, char* output, size_t size) const
{
ssize_t rc = ::readlinkat(m_dir_fd, path.c_str(), output, size);
if (rc < 0)
Expand All @@ -400,14 +400,14 @@ ssize_t OSService::readlink(StringRef path, char* output, size_t size) const
return rc;
}

void OSService::utimens(StringRef path, const fuse_timespec* ts) const
void OSService::utimens(const std::string& path, const fuse_timespec* ts) const
{
int rc = ::utimensat(m_dir_fd, path.c_str(), ts, AT_SYMLINK_NOFOLLOW);
if (rc < 0)
THROW_POSIX_EXCEPTION(errno, "utimensat");
}

std::unique_ptr<DirectoryTraverser> OSService::create_traverser(StringRef dir) const
std::unique_ptr<DirectoryTraverser> OSService::create_traverser(const std::string& dir) const
{
return securefs::make_unique<UnixDirectoryTraverser>(norm_path(dir));
}
Expand Down
Loading

0 comments on commit 84d2332

Please sign in to comment.