Skip to content

Commit

Permalink
整理代码
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 24, 2024
1 parent 6c45c44 commit 93c2001
Show file tree
Hide file tree
Showing 12 changed files with 257 additions and 257 deletions.
2 changes: 1 addition & 1 deletion bee/subprocess/common.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <bee/sys/file_handle.h>
#include <bee/utility/dynarray.h>
#include <bee/utility/file_handle.h>

#include <utility>

Expand Down
48 changes: 24 additions & 24 deletions bee/utility/file_handle.cpp → bee/sys/file_handle.cpp
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;
}
}
88 changes: 44 additions & 44 deletions bee/utility/file_handle.h → bee/sys/file_handle.h
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;
};
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <bee/nonstd/format.h>
#include <bee/utility/file_handle.h>
#include <bee/sys/file_handle.h>
#include <sys/file.h>
#include <unistd.h>

Expand Down
72 changes: 36 additions & 36 deletions bee/utility/file_handle_linux.cpp → bee/sys/file_handle_linux.cpp
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 bee/utility/file_handle_osx.cpp → bee/sys/file_handle_osx.cpp
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 bee/utility/file_handle_posix.cpp → bee/sys/file_handle_posix.cpp
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;
}
}
}
Loading

0 comments on commit 93c2001

Please sign in to comment.