Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Apr 24, 2024
1 parent 93c2001 commit a29a600
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 146 deletions.
2 changes: 1 addition & 1 deletion bee/sys/path_helper.h → bee/sys/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <bee/nonstd/expected.h>
#include <bee/nonstd/filesystem.h>

namespace bee::path_helper {
namespace bee::sys {
using path_expected = expected<fs::path, std::string>;
path_expected exe_path() noexcept;
path_expected dll_path() noexcept;
Expand Down
50 changes: 50 additions & 0 deletions bee/sys/path_bsd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <bee/error.h>
#include <bee/sys/path.h>
#include <unistd.h>

#if defined(__FreeBSD__)
# include <sys/param.h>
# include <sys/sysctl.h>
#elif defined(__OpenBSD__)
# include <sys/sysctl.h>
#endif

namespace bee::sys {
#if defined(__FreeBSD__)
path_expected exe_path() noexcept {
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
char exe[1024];
size_t length = 1024;
int error = sysctl(name, 4, exe, &length, NULL, 0);
if (error < 0 || length <= 1) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
return fs::path(exe, exe + length - 1);
}
#elif defined(__OpenBSD__)
path_expected exe_path() noexcept {
int name[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
size_t argc;
if (sysctl(name, 4, NULL, &argc, NULL, 0) < 0) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
const char** argv = (const char**)malloc(argc);
if (!argv) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
if (sysctl(name, 4, argv, &argc, NULL, 0) < 0) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
return fs::path(argv[0]);
}
#else
path_expected exe_path() noexcept {
std::error_code ec;
auto res = fs::read_symlink("/proc/self/exe", ec);
if (ec) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
return res;
}
#endif
}
140 changes: 0 additions & 140 deletions bee/sys/path_helper.cpp

This file was deleted.

14 changes: 14 additions & 0 deletions bee/sys/path_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <bee/error.h>
#include <bee/sys/path.h>
#include <unistd.h>

namespace bee::sys {
path_expected exe_path() noexcept {
std::error_code ec;
auto res = fs::read_symlink("/proc/self/exe", ec);
if (ec) {
return unexpected<std::string>(error::sys_errmsg("exe_path"));
}
return res;
}
}
19 changes: 19 additions & 0 deletions bee/sys/path_osx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <bee/sys/path.h>
#include <bee/utility/dynarray.h>
#include <mach-o/dyld.h>

namespace bee::sys {
path_expected exe_path() noexcept {
uint32_t path_len = 0;
_NSGetExecutablePath(0, &path_len);
if (path_len <= 1) {
return unexpected<std::string>("_NSGetExecutablePath failed.");
}
dynarray<char> buf(path_len);
int rv = _NSGetExecutablePath(buf.data(), &path_len);
if (rv != 0) {
return unexpected<std::string>("_NSGetExecutablePath failed.");
}
return fs::path(buf.data(), buf.data() + path_len - 1);
}
}
23 changes: 23 additions & 0 deletions bee/sys/path_posix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <bee/sys/path.h>
#include <dlfcn.h>

namespace bee::sys {
static path_expected dll_path(void* module_handle) noexcept {
::Dl_info dl_info;
dl_info.dli_fname = 0;
const int ret = ::dladdr(module_handle, &dl_info);
if (0 != ret && dl_info.dli_fname != NULL) {
std::error_code ec;
auto path = fs::absolute(dl_info.dli_fname, ec);
if (ec) {
return unexpected<std::string>(ec.message());
}
return path.lexically_normal();
}
return unexpected<std::string>("::dladdr failed.");
}

path_expected dll_path() noexcept {
return dll_path((void*)&exe_path);
}
}
40 changes: 40 additions & 0 deletions bee/sys/path_win.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <Windows.h>
#include <bee/error.h>
#include <bee/sys/path.h>
#include <bee/utility/dynarray.h>
#include <shlobj.h>

// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
extern "C" IMAGE_DOS_HEADER __ImageBase;

namespace bee::sys {
static path_expected dll_path(HMODULE module_handle) noexcept {
wchar_t buffer[MAX_PATH];
DWORD path_len = ::GetModuleFileNameW(module_handle, buffer, _countof(buffer));
if (path_len == 0) {
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW"));
}
if (path_len < _countof(buffer)) {
return fs::path(buffer, buffer + path_len);
}
for (DWORD buf_len = 0x200; buf_len <= 0x10000; buf_len <<= 1) {
dynarray<wchar_t> buf(buf_len);
path_len = ::GetModuleFileNameW(module_handle, buf.data(), buf_len);
if (path_len == 0) {
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW"));
}
if (path_len < _countof(buffer)) {
return fs::path(buf.data(), buf.data() + path_len);
}
}
return unexpected<std::string>("::GetModuleFileNameW return too long.");
}

path_expected exe_path() noexcept {
return dll_path(NULL);
}

path_expected dll_path() noexcept {
return dll_path(reinterpret_cast<HMODULE>(&__ImageBase));
}
}
6 changes: 3 additions & 3 deletions binding/lua_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <bee/nonstd/format.h>
#include <bee/nonstd/unreachable.h>
#include <bee/sys/file_handle.h>
#include <bee/sys/path_helper.h>
#include <bee/sys/path.h>

#include <chrono>
#include <utility>
Expand Down Expand Up @@ -932,7 +932,7 @@ namespace bee::lua_filesystem {
}

static int exe_path(lua_State* L) {
auto r = path_helper::exe_path();
auto r = sys::exe_path();
if (!r) {
return lua::push_error(L, r.error());
}
Expand All @@ -941,7 +941,7 @@ namespace bee::lua_filesystem {
}

static int dll_path(lua_State* L) {
auto r = path_helper::dll_path();
auto r = sys::dll_path();
if (!r) {
return lua::push_error(L, r.error());
}
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <bee/lua/module.h>
#include <bee/nonstd/filesystem.h>
#include <bee/nonstd/unreachable.h>
#include <bee/sys/path_helper.h>
#include <bee/sys/path.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -136,7 +136,7 @@ static int pushargs(lua_State *L) {
}

static fs::path getprogdir(lua_State *L) {
auto r = bee::path_helper::exe_path();
auto r = bee::sys::exe_path();
if (!r) {
luaL_error(L, "unable to get progdir: %s\n", r.error().c_str());
std::unreachable();
Expand Down

0 comments on commit a29a600

Please sign in to comment.