diff --git a/bee/subprocess/common.h b/bee/subprocess/common.h index 0f91fc23..cdee0b1a 100644 --- a/bee/subprocess/common.h +++ b/bee/subprocess/common.h @@ -1,7 +1,7 @@ #pragma once +#include #include -#include #include diff --git a/bee/utility/file_handle.cpp b/bee/sys/file_handle.cpp similarity index 91% rename from bee/utility/file_handle.cpp rename to bee/sys/file_handle.cpp index 19d4d186..debfbdae 100644 --- a/bee/utility/file_handle.cpp +++ b/bee/sys/file_handle.cpp @@ -1,24 +1,24 @@ -#include - -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 + +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; + } +} diff --git a/bee/utility/file_handle.h b/bee/sys/file_handle.h similarity index 96% rename from bee/utility/file_handle.h rename to bee/sys/file_handle.h index e0f30e95..cf309ad8 100644 --- a/bee/utility/file_handle.h +++ b/bee/sys/file_handle.h @@ -1,44 +1,44 @@ -#pragma once - -#include - -#include -#include - -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 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 + +#include +#include + +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 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; + }; +} diff --git a/bee/utility/file_handle_bsd.cpp b/bee/sys/file_handle_bsd.cpp similarity index 96% rename from bee/utility/file_handle_bsd.cpp rename to bee/sys/file_handle_bsd.cpp index 815354cf..7292870b 100644 --- a/bee/utility/file_handle_bsd.cpp +++ b/bee/sys/file_handle_bsd.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/bee/utility/file_handle_linux.cpp b/bee/sys/file_handle_linux.cpp similarity index 92% rename from bee/utility/file_handle_linux.cpp rename to bee/sys/file_handle_linux.cpp index 6e863944..96c225ff 100644 --- a/bee/utility/file_handle_linux.cpp +++ b/bee/sys/file_handle_linux.cpp @@ -1,36 +1,36 @@ -#include -#include -#include -#include -#include - -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 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 +#include +#include +#include +#include + +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 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; + } +} diff --git a/bee/utility/file_handle_osx.cpp b/bee/sys/file_handle_osx.cpp similarity index 91% rename from bee/utility/file_handle_osx.cpp rename to bee/sys/file_handle_osx.cpp index b3779c97..880e86f0 100644 --- a/bee/utility/file_handle_osx.cpp +++ b/bee/sys/file_handle_osx.cpp @@ -1,26 +1,26 @@ -#include -#include -#include - -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 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 +#include +#include + +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 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); + } +} diff --git a/bee/utility/file_handle_posix.cpp b/bee/sys/file_handle_posix.cpp similarity index 91% rename from bee/utility/file_handle_posix.cpp rename to bee/sys/file_handle_posix.cpp index ffb9a262..09f0474c 100644 --- a/bee/utility/file_handle_posix.cpp +++ b/bee/sys/file_handle_posix.cpp @@ -1,33 +1,33 @@ -#include -#include -#include - -#include - -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 +#include +#include + +#include + +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; + } + } +} diff --git a/bee/utility/file_handle_win.cpp b/bee/sys/file_handle_win.cpp similarity index 95% rename from bee/utility/file_handle_win.cpp rename to bee/sys/file_handle_win.cpp index d751c117..5c0f889e 100644 --- a/bee/utility/file_handle_win.cpp +++ b/bee/sys/file_handle_win.cpp @@ -1,88 +1,88 @@ -#include -#include -#include -#include -#include -#include - -namespace bee { - static FILE* handletofile(HANDLE h, int flags, const char* mode) noexcept { - const int fn = _open_osfhandle((intptr_t)h, flags); - if (fn == -1) { - return 0; - } - return _fdopen(fn, mode); - } - - FILE* file_handle::to_file(mode mode) const noexcept { - switch (mode) { - case mode::read: - return handletofile(h, _O_RDONLY | _O_BINARY, "rb"); - case mode::write: - return handletofile(h, _O_WRONLY | _O_BINARY, "wb"); - default: - std::unreachable(); - } - } - - file_handle file_handle::from_file(FILE* f) noexcept { - const int n = _fileno(f); - if (n < 0) { - return {}; - } - return { from_native((HANDLE)_get_osfhandle(n)) }; - } - - file_handle file_handle::dup(FILE* f) noexcept { - const file_handle h = from_file(f); - if (!h) { - return {}; - } - file_handle newh; - if (!::DuplicateHandle(::GetCurrentProcess(), h.value(), ::GetCurrentProcess(), &newh, 0, FALSE, DUPLICATE_SAME_ACCESS)) { - return {}; - } - return newh; - } - - file_handle file_handle::lock(const fs::path& filename) noexcept { - const HANDLE h = CreateFileW(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL); - return from_native(h); - } - - file_handle file_handle::open_link(const fs::path& filename) noexcept { - const HANDLE h = CreateFileW(filename.c_str(), 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); - return from_native(h); - } - - void file_handle::close() noexcept { - if (valid()) { - CloseHandle(h); - h = file_handle {}.h; - } - } - - std::optional file_handle::path() const { - if (!valid()) { - return std::nullopt; - } - const DWORD len = GetFinalPathNameByHandleW(h, NULL, 0, VOLUME_NAME_DOS); - if (len == 0) { - return std::nullopt; - } - dynarray path(static_cast(len)); - const DWORD len2 = GetFinalPathNameByHandleW(h, path.data(), len, VOLUME_NAME_DOS); - if (len2 == 0 || len2 >= len) { - return std::nullopt; - } - if (path[0] == L'\\' && path[1] == L'\\' && path[2] == L'?' && path[3] == L'\\') { - // Turn the \\?\UNC\ network path prefix into \\. - if (path[4] == L'U' && path[5] == L'N' && path[6] == L'C' && path[7] == L'\\') { - return fs::path(L"\\\\").concat(path.data() + 8, path.data() + len2); - } - // Remove the \\?\ prefix. - return fs::path { path.data() + 4, path.data() + len2 }; - } - return fs::path { path.data(), path.data() + len2 }; - } -} +#include +#include +#include +#include +#include +#include + +namespace bee { + static FILE* handletofile(HANDLE h, int flags, const char* mode) noexcept { + const int fn = _open_osfhandle((intptr_t)h, flags); + if (fn == -1) { + return 0; + } + return _fdopen(fn, mode); + } + + FILE* file_handle::to_file(mode mode) const noexcept { + switch (mode) { + case mode::read: + return handletofile(h, _O_RDONLY | _O_BINARY, "rb"); + case mode::write: + return handletofile(h, _O_WRONLY | _O_BINARY, "wb"); + default: + std::unreachable(); + } + } + + file_handle file_handle::from_file(FILE* f) noexcept { + const int n = _fileno(f); + if (n < 0) { + return {}; + } + return { from_native((HANDLE)_get_osfhandle(n)) }; + } + + file_handle file_handle::dup(FILE* f) noexcept { + const file_handle h = from_file(f); + if (!h) { + return {}; + } + file_handle newh; + if (!::DuplicateHandle(::GetCurrentProcess(), h.value(), ::GetCurrentProcess(), &newh, 0, FALSE, DUPLICATE_SAME_ACCESS)) { + return {}; + } + return newh; + } + + file_handle file_handle::lock(const fs::path& filename) noexcept { + const HANDLE h = CreateFileW(filename.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL); + return from_native(h); + } + + file_handle file_handle::open_link(const fs::path& filename) noexcept { + const HANDLE h = CreateFileW(filename.c_str(), 0, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL); + return from_native(h); + } + + void file_handle::close() noexcept { + if (valid()) { + CloseHandle(h); + h = file_handle {}.h; + } + } + + std::optional file_handle::path() const { + if (!valid()) { + return std::nullopt; + } + const DWORD len = GetFinalPathNameByHandleW(h, NULL, 0, VOLUME_NAME_DOS); + if (len == 0) { + return std::nullopt; + } + dynarray path(static_cast(len)); + const DWORD len2 = GetFinalPathNameByHandleW(h, path.data(), len, VOLUME_NAME_DOS); + if (len2 == 0 || len2 >= len) { + return std::nullopt; + } + if (path[0] == L'\\' && path[1] == L'\\' && path[2] == L'?' && path[3] == L'\\') { + // Turn the \\?\UNC\ network path prefix into \\. + if (path[4] == L'U' && path[5] == L'N' && path[6] == L'C' && path[7] == L'\\') { + return fs::path(L"\\\\").concat(path.data() + 8, path.data() + len2); + } + // Remove the \\?\ prefix. + return fs::path { path.data() + 4, path.data() + len2 }; + } + return fs::path { path.data(), path.data() + len2 }; + } +} diff --git a/bee/utility/path_helper.cpp b/bee/sys/path_helper.cpp similarity index 99% rename from bee/utility/path_helper.cpp rename to bee/sys/path_helper.cpp index 894d9e0a..003138e9 100644 --- a/bee/utility/path_helper.cpp +++ b/bee/sys/path_helper.cpp @@ -1,6 +1,6 @@ #include +#include #include -#include #if defined(_WIN32) diff --git a/bee/utility/path_helper.h b/bee/sys/path_helper.h similarity index 100% rename from bee/utility/path_helper.h rename to bee/sys/path_helper.h diff --git a/binding/lua_filesystem.cpp b/binding/lua_filesystem.cpp index 315b8793..28385bd4 100644 --- a/binding/lua_filesystem.cpp +++ b/binding/lua_filesystem.cpp @@ -8,8 +8,8 @@ #include #include #include -#include -#include +#include +#include #include #include diff --git a/bootstrap/main.cpp b/bootstrap/main.cpp index 8cc25b83..573e95d6 100644 --- a/bootstrap/main.cpp +++ b/bootstrap/main.cpp @@ -10,7 +10,7 @@ #include #include #include -#include +#include #include #include #include