// Copyright 2014 Renato Tegon Forti, Antony Polukhin.
-// Copyright 2015-2016 Antony Polukhin.
+// Copyright 2015-2019 Antony Polukhin.
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
#ifndef BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
#define BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
-#include <boost/config.hpp>
+#include <boost/dll/config.hpp>
#include <boost/dll/shared_library_load_mode.hpp>
#include <boost/dll/detail/posix/path_from_handle.hpp>
#include <boost/dll/detail/posix/program_location_impl.hpp>
#include <boost/move/utility.hpp>
#include <boost/swap.hpp>
-#include <boost/filesystem/path.hpp>
-#include <boost/filesystem/operations.hpp>
#include <boost/predef/os.h>
#include <dlfcn.h>
~shared_library_impl() BOOST_NOEXCEPT {
unload();
}
-
+
shared_library_impl(BOOST_RV_REF(shared_library_impl) sl) BOOST_NOEXCEPT
: handle_(sl.handle_)
{
return *this;
}
- void load(boost::filesystem::path sl, load_mode::type mode, boost::system::error_code &ec) {
+
+ static boost::dll::fs::path decorate(const boost::dll::fs::path & sl) {
+ boost::dll::fs::path actual_path = (
+ std::strncmp(sl.filename().string().c_str(), "lib", 3)
+ ? boost::dll::fs::path((sl.has_parent_path() ? sl.parent_path() / L"lib" : L"lib").native() + sl.filename().native())
+ : sl
+ );
+ actual_path += suffix();
+ return actual_path;
+ }
+
+ void load(boost::dll::fs::path sl, load_mode::type mode, boost::dll::fs::error_code &ec) {
typedef int native_mode_t;
unload();
// Do not allow opening NULL paths. User must use program_location() instead
if (sl.empty()) {
boost::dll::detail::reset_dlerror();
- ec = boost::system::error_code(
- boost::system::errc::bad_file_descriptor,
- boost::system::generic_category()
+ ec = boost::dll::fs::make_error_code(
+ boost::dll::fs::errc::bad_file_descriptor
);
return;
}
#else
if (!sl.is_absolute() && !(mode & load_mode::search_system_folders)) {
- boost::system::error_code current_path_ec;
- boost::filesystem::path prog_loc = boost::filesystem::current_path(current_path_ec);
+ boost::dll::fs::error_code current_path_ec;
+ boost::dll::fs::path prog_loc = boost::dll::fs::current_path(current_path_ec);
if (!current_path_ec) {
prog_loc /= sl;
sl.swap(prog_loc);
if (!!(mode & load_mode::append_decorations)) {
mode &= ~load_mode::append_decorations;
- boost::filesystem::path actual_path = (
- std::strncmp(sl.filename().string().c_str(), "lib", 3)
- ? (sl.has_parent_path() ? sl.parent_path() / L"lib" : L"lib").native() + sl.filename().native()
- : sl
- );
- actual_path += suffix();
-
+ boost::dll::fs::path actual_path = decorate(sl);
handle_ = dlopen(actual_path.c_str(), static_cast<native_mode_t>(mode));
if (handle_) {
boost::dll::detail::reset_dlerror();
return;
}
+ boost::dll::fs::error_code prog_loc_err;
+ boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
+ if (boost::dll::fs::exists(actual_path) && !boost::dll::fs::equivalent(sl, loc, prog_loc_err)) {
+ // decorated path exists : current error is not a bad file descriptor and we are not trying to load the executable itself
+ ec = boost::dll::fs::make_error_code(
+ boost::dll::fs::errc::executable_format_error
+ );
+ return;
+ }
}
// Opening by exactly specified path
return;
}
- ec = boost::system::error_code(
- boost::system::errc::bad_file_descriptor,
- boost::system::generic_category()
+ ec = boost::dll::fs::make_error_code(
+ boost::dll::fs::errc::bad_file_descriptor
);
// Maybe user wanted to load the executable itself? Checking...
// We assume that usually user wants to load a dynamic library not the executable itself, that's why
// we try this only after traditional load fails.
- boost::system::error_code prog_loc_err;
- boost::filesystem::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
- if (!prog_loc_err && boost::filesystem::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
- // As is known the function dlopen() loads the dynamic library file
- // named by the null-terminated string filename and returns an opaque
- // "handle" for the dynamic library. If filename is NULL, then the
+ boost::dll::fs::error_code prog_loc_err;
+ boost::dll::fs::path loc = boost::dll::detail::program_location_impl(prog_loc_err);
+ if (!prog_loc_err && boost::dll::fs::equivalent(sl, loc, prog_loc_err) && !prog_loc_err) {
+ // As is known the function dlopen() loads the dynamic library file
+ // named by the null-terminated string filename and returns an opaque
+ // "handle" for the dynamic library. If filename is NULL, then the
// returned handle is for the main program.
ec.clear();
boost::dll::detail::reset_dlerror();
handle_ = dlopen(NULL, static_cast<native_mode_t>(mode));
if (!handle_) {
- ec = boost::system::error_code(
- boost::system::errc::bad_file_descriptor,
- boost::system::generic_category()
+ ec = boost::dll::fs::make_error_code(
+ boost::dll::fs::errc::bad_file_descriptor
);
}
}
boost::swap(handle_, rhs.handle_);
}
- boost::filesystem::path full_module_path(boost::system::error_code &ec) const {
+ boost::dll::fs::path full_module_path(boost::dll::fs::error_code &ec) const {
return boost::dll::detail::path_from_handle(handle_, ec);
}
- static boost::filesystem::path suffix() {
+ static boost::dll::fs::path suffix() {
// https://sourceforge.net/p/predef/wiki/OperatingSystems/
#if BOOST_OS_MACOS || BOOST_OS_IOS
return ".dylib";
#endif
}
- void* symbol_addr(const char* sb, boost::system::error_code &ec) const BOOST_NOEXCEPT {
+ void* symbol_addr(const char* sb, boost::dll::fs::error_code &ec) const BOOST_NOEXCEPT {
// dlsym - obtain the address of a symbol from a dlopen object
void* const symbol = dlsym(handle_, sb);
if (symbol == NULL) {
- ec = boost::system::error_code(
- boost::system::errc::invalid_seek,
- boost::system::generic_category()
+ ec = boost::dll::fs::make_error_code(
+ boost::dll::fs::errc::invalid_seek
);
}
}}} // boost::dll::detail
#endif // BOOST_DLL_SHARED_LIBRARY_IMPL_HPP
-