-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
257 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
#include <bee/utility/file_handle.h> | ||
|
||
namespace bee { | ||
file_handle::file_handle() noexcept | ||
: h((value_type)-1) {} | ||
file_handle::operator bool() const noexcept { | ||
return valid(); | ||
} | ||
bool file_handle::valid() const noexcept { | ||
return *this != file_handle {}; | ||
} | ||
file_handle::value_type file_handle::value() const noexcept { | ||
return h; | ||
} | ||
file_handle::value_type* file_handle::operator&() noexcept { | ||
return &h; | ||
} | ||
bool file_handle::operator==(const file_handle& other) const noexcept { | ||
return h == other.h; | ||
} | ||
bool file_handle::operator!=(const file_handle& other) const noexcept { | ||
return h != other.h; | ||
} | ||
} | ||
#include <bee/sys/file_handle.h> | ||
|
||
namespace bee { | ||
file_handle::file_handle() noexcept | ||
: h((value_type)-1) {} | ||
file_handle::operator bool() const noexcept { | ||
return valid(); | ||
} | ||
bool file_handle::valid() const noexcept { | ||
return *this != file_handle {}; | ||
} | ||
file_handle::value_type file_handle::value() const noexcept { | ||
return h; | ||
} | ||
file_handle::value_type* file_handle::operator&() noexcept { | ||
return &h; | ||
} | ||
bool file_handle::operator==(const file_handle& other) const noexcept { | ||
return h == other.h; | ||
} | ||
bool file_handle::operator!=(const file_handle& other) const noexcept { | ||
return h != other.h; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,44 @@ | ||
#pragma once | ||
|
||
#include <bee/nonstd/filesystem.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace bee { | ||
class file_handle { | ||
public: | ||
#if defined(_WIN32) | ||
using value_type = void*; | ||
#else | ||
using value_type = int; | ||
#endif | ||
enum class mode { | ||
read, | ||
write, | ||
}; | ||
|
||
file_handle() noexcept; | ||
explicit operator bool() const noexcept; | ||
bool valid() const noexcept; | ||
value_type value() const noexcept; | ||
value_type* operator&() noexcept; | ||
bool operator==(const file_handle& other) const noexcept; | ||
bool operator!=(const file_handle& other) const noexcept; | ||
FILE* to_file(mode mode) const noexcept; | ||
std::optional<fs::path> path() const; | ||
void close() noexcept; | ||
static inline file_handle from_native(value_type v) noexcept { | ||
file_handle handle; | ||
handle.h = v; | ||
return handle; | ||
} | ||
static file_handle from_file(FILE* f) noexcept; | ||
static file_handle dup(FILE* f) noexcept; | ||
static file_handle lock(const fs::path& filename) noexcept; | ||
static file_handle open_link(const fs::path& filename) noexcept; | ||
|
||
private: | ||
value_type h; | ||
}; | ||
} | ||
#pragma once | ||
|
||
#include <bee/nonstd/filesystem.h> | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace bee { | ||
class file_handle { | ||
public: | ||
#if defined(_WIN32) | ||
using value_type = void*; | ||
#else | ||
using value_type = int; | ||
#endif | ||
enum class mode { | ||
read, | ||
write, | ||
}; | ||
|
||
file_handle() noexcept; | ||
explicit operator bool() const noexcept; | ||
bool valid() const noexcept; | ||
value_type value() const noexcept; | ||
value_type* operator&() noexcept; | ||
bool operator==(const file_handle& other) const noexcept; | ||
bool operator!=(const file_handle& other) const noexcept; | ||
FILE* to_file(mode mode) const noexcept; | ||
std::optional<fs::path> path() const; | ||
void close() noexcept; | ||
static inline file_handle from_native(value_type v) noexcept { | ||
file_handle handle; | ||
handle.h = v; | ||
return handle; | ||
} | ||
static file_handle from_file(FILE* f) noexcept; | ||
static file_handle dup(FILE* f) noexcept; | ||
static file_handle lock(const fs::path& filename) noexcept; | ||
static file_handle open_link(const fs::path& filename) noexcept; | ||
|
||
private: | ||
value_type h; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 36 additions & 36 deletions
72
bee/utility/file_handle_linux.cpp → bee/sys/file_handle_linux.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
#include <bee/nonstd/format.h> | ||
#include <bee/utility/file_handle.h> | ||
#include <fcntl.h> | ||
#include <sys/file.h> | ||
#include <unistd.h> | ||
|
||
namespace bee { | ||
file_handle file_handle::lock(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
if (fd == -1) { | ||
return {}; | ||
} | ||
if (::flock(fd, LOCK_EX | LOCK_NB) == -1) { | ||
::close(fd); | ||
return {}; | ||
} | ||
return from_native(fd); | ||
} | ||
|
||
file_handle file_handle::open_link(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_PATH | O_NOFOLLOW); | ||
return from_native(fd); | ||
} | ||
|
||
std::optional<fs::path> file_handle::path() const { | ||
if (!valid()) { | ||
return std::nullopt; | ||
} | ||
std::error_code ec; | ||
auto res = fs::read_symlink(std::format("/proc/self/fd/{}", h), ec); | ||
if (ec) { | ||
return std::nullopt; | ||
} | ||
return res; | ||
} | ||
} | ||
#include <bee/nonstd/format.h> | ||
#include <bee/sys/file_handle.h> | ||
#include <fcntl.h> | ||
#include <sys/file.h> | ||
#include <unistd.h> | ||
|
||
namespace bee { | ||
file_handle file_handle::lock(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
if (fd == -1) { | ||
return {}; | ||
} | ||
if (::flock(fd, LOCK_EX | LOCK_NB) == -1) { | ||
::close(fd); | ||
return {}; | ||
} | ||
return from_native(fd); | ||
} | ||
|
||
file_handle file_handle::open_link(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_PATH | O_NOFOLLOW); | ||
return from_native(fd); | ||
} | ||
|
||
std::optional<fs::path> file_handle::path() const { | ||
if (!valid()) { | ||
return std::nullopt; | ||
} | ||
std::error_code ec; | ||
auto res = fs::read_symlink(std::format("/proc/self/fd/{}", h), ec); | ||
if (ec) { | ||
return std::nullopt; | ||
} | ||
return res; | ||
} | ||
} |
52 changes: 26 additions & 26 deletions
52
bee/utility/file_handle_osx.cpp → bee/sys/file_handle_osx.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
#include <bee/utility/file_handle.h> | ||
#include <fcntl.h> | ||
#include <sys/file.h> | ||
|
||
namespace bee { | ||
file_handle file_handle::lock(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK | O_NONBLOCK, 0644); | ||
return from_native(fd); | ||
} | ||
|
||
file_handle file_handle::open_link(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_SYMLINK); | ||
return from_native(fd); | ||
} | ||
|
||
std::optional<fs::path> file_handle::path() const { | ||
if (!valid()) { | ||
return std::nullopt; | ||
} | ||
char path[PATH_MAX]; | ||
if (::fcntl(h, F_GETPATH, path) < 0) { | ||
return std::nullopt; | ||
} | ||
return fs::path(path); | ||
} | ||
} | ||
#include <bee/sys/file_handle.h> | ||
#include <fcntl.h> | ||
#include <sys/file.h> | ||
|
||
namespace bee { | ||
file_handle file_handle::lock(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_EXLOCK | O_NONBLOCK, 0644); | ||
return from_native(fd); | ||
} | ||
|
||
file_handle file_handle::open_link(const fs::path& filename) noexcept { | ||
int fd = ::open(filename.c_str(), O_SYMLINK); | ||
return from_native(fd); | ||
} | ||
|
||
std::optional<fs::path> file_handle::path() const { | ||
if (!valid()) { | ||
return std::nullopt; | ||
} | ||
char path[PATH_MAX]; | ||
if (::fcntl(h, F_GETPATH, path) < 0) { | ||
return std::nullopt; | ||
} | ||
return fs::path(path); | ||
} | ||
} |
66 changes: 33 additions & 33 deletions
66
bee/utility/file_handle_posix.cpp → bee/sys/file_handle_posix.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
#include <bee/nonstd/unreachable.h> | ||
#include <bee/utility/file_handle.h> | ||
#include <unistd.h> | ||
|
||
#include <cstdio> | ||
|
||
namespace bee { | ||
FILE* file_handle::to_file(mode mode) const noexcept { | ||
switch (mode) { | ||
case mode::read: | ||
return fdopen(h, "rb"); | ||
case mode::write: | ||
return fdopen(h, "wb"); | ||
default: | ||
std::unreachable(); | ||
} | ||
} | ||
|
||
file_handle file_handle::from_file(FILE* f) noexcept { | ||
return from_native(fileno(f)); | ||
} | ||
|
||
file_handle file_handle::dup(FILE* f) noexcept { | ||
return from_native(::dup(from_file(f).value())); | ||
} | ||
|
||
void file_handle::close() noexcept { | ||
if (valid()) { | ||
::close(h); | ||
h = file_handle {}.h; | ||
} | ||
} | ||
} | ||
#include <bee/nonstd/unreachable.h> | ||
#include <bee/sys/file_handle.h> | ||
#include <unistd.h> | ||
|
||
#include <cstdio> | ||
|
||
namespace bee { | ||
FILE* file_handle::to_file(mode mode) const noexcept { | ||
switch (mode) { | ||
case mode::read: | ||
return fdopen(h, "rb"); | ||
case mode::write: | ||
return fdopen(h, "wb"); | ||
default: | ||
std::unreachable(); | ||
} | ||
} | ||
|
||
file_handle file_handle::from_file(FILE* f) noexcept { | ||
return from_native(fileno(f)); | ||
} | ||
|
||
file_handle file_handle::dup(FILE* f) noexcept { | ||
return from_native(::dup(from_file(f).value())); | ||
} | ||
|
||
void file_handle::close() noexcept { | ||
if (valid()) { | ||
::close(h); | ||
h = file_handle {}.h; | ||
} | ||
} | ||
} |
Oops, something went wrong.