28#include <jau/test/catch2_ext.hpp>
37static bool existsPath(
const std::string& libPath)
noexcept {
39 return path_stats.
exists();
41static bool existsLibBasename(
const std::string& libBasename,
const std::string& relDir, std::string& libPath)
noexcept {
62 const std::string libBasename =
"testlib";
68 std::cout <<
"Warning: library '" << libBasename <<
"' doesn't exist at: build '" << libPathBuild <<
"', nor at orig '" << libPathOrig <<
"'" << std::endl;
77 const std::string libDirOrig = libDir +
"/orig";
80 std::cout <<
"remove: " << libDirOrig << std::endl;
89 std::cout <<
"move: libs in '" << libDir <<
"' to '" << libDirOrig <<
"'" << std::endl;
92 std::cout <<
"- move: ignore entry depth[" << depth <<
"]" << element_stats.item().to_string() << std::endl;
96 std::string bname = element_stats.item().basename();
97 if( bname.starts_with(libName) ) {
98 const std::string p = libDirOrig +
"/" + bname;
99 std::cout <<
"- move: depth[" << depth <<
"]: '" << element_stats.path() <<
"' to '" << p <<
"'" << std::endl;
107 std::cout <<
"post move: " << path_stats2.
to_string() << std::endl;
119 const std::string libBasenameCopy =
"testlib2";
125 std::cout <<
"remove: " << libDirCopy << std::endl;
130 const std::string libPathCopy = libDirCopy +
"/" + libNameCopy;
131 REQUIRE(
true ==
jau::fs::copy(libPathOrigFile, libPathCopy, copts) );
133 std::string libPath2;
139 const std::string symbolName =
"jaulib_id_entryfunc";
141 std::cout <<
"- libBasename: " << libBasename << std::endl;
142 std::cout <<
"- libName: " << libName << std::endl;
147 std::cout <<
"- exe " << executable_path << std::endl << std::endl;
149 std::cout <<
"Warning: library '" << libName <<
"' doesn't exist at: '" << libPath <<
"'" << std::endl;
155 std::cout <<
"- Path: " << libPath << std::endl;
157 REQUIRE(
nullptr != libHandle );
160 std::cout <<
"- Symbol '" << symbolName <<
"': Handle = " <<
jau::to_hexstring(symHandle) << std::endl;
161 REQUIRE(
nullptr != symHandle );
164 if(
nullptr == nativePath ) {
165 std::cout <<
"- Native Path: null" << std::endl;
167 std::cout <<
"- Native Path: '" << std::string(nativePath) <<
"'" << std::endl;
169 REQUIRE(
nullptr != nativePath );
173 std::cout <<
"- Basename (path): " << bname1 << std::endl;
174 REQUIRE( libBasename == bname1 );
175 if(
nullptr != nativePath ) {
177 std::cout <<
"- Basename (native-path): " << bname2 << std::endl;
178 REQUIRE( libBasename == bname2 );
189 std::cout <<
"- lib_path_var_name: " << lib_path_var_name << std::endl;
190 std::cout <<
"- lib_path_var : " << lib_path_var << std::endl;
191 std::cout <<
"- lib_paths: count : " << lib_paths.size() << std::endl <<
" - path: ";
192 std::cout <<
jau::to_string(lib_paths,
"\n - path: ") << std::endl;
199 const std::string symbolName =
"jaulib_id_entryfunc";
201 std::cout <<
"- libBasename: " << libBasename << std::endl;
202 std::cout <<
"- libName: " << libName << std::endl;
206 const std::string libPathRel = libDirRel +
"/" + libName;
207 std::string libDirAbs, libPathAbs;
209 std::cout <<
"- cwd: " << cwd << std::endl;
210 std::cout <<
"- exe-rel " << executable_path << std::endl;
211 std::cout <<
"- exe-abs " << exe_path_abs << std::endl << std::endl;
214 REQUIRE(
true ==
fs.exists() );
215 libDirAbs =
fs.final_target()->path();
217 libPathAbs = libDirAbs +
"/" + libName;
220 std::cout <<
"Warning: library '" << libName <<
"' doesn't exist at: '" << libPathAbs <<
"'" << std::endl;
227 std::cout <<
"Sys-Path: '" << lib_path_var_name <<
"': Original" << std::endl;
229 std::cout <<
"- lib_path_var (1) : " << lib_path_var0 << std::endl;
230 std::cout <<
"- lib_paths: count : " << lib_paths1.size() << std::endl <<
" - path: '";
231 std::cout <<
jau::to_string(lib_paths1,
"'\n - path: '") <<
"'" << std::endl;
237 const bool searchSystemPath =
false;
238 const bool searchSystemPathFirst =
false;
239 const bool global =
false;
240 std::cout <<
"Check-1: Absolute Path: '" << libPathAbs <<
"', cwd2 '" << cwd2 <<
"'" << std::endl;
242 std::cout <<
"Check-1: " << nl.
toString() << std::endl;
243 REQUIRE(
true == nl.
isValid() );
244 REQUIRE(
true == nl.
isOpen() );
246 REQUIRE(
false == nl.
isOpen() );
247 REQUIRE(
true == nl.
isValid() );
255 const bool searchSystemPath =
false;
256 const bool searchSystemPathFirst =
false;
257 const bool global =
false;
258 std::cout <<
"Check-2: Relative Path to cwd: libPathRel '" << libPathRel <<
"', cwd2 '" << cwd2 <<
"'" << std::endl;
260 std::cout <<
"Check-2: " << nl.
toString() << std::endl;
261 REQUIRE(
true == nl.
isValid() );
262 REQUIRE(
true == nl.
isOpen() );
264 REQUIRE(
false == nl.
isOpen() );
265 REQUIRE(
true == nl.
isValid() );
272 std::cout <<
"Sys-Path: '" << lib_path_var_name <<
"': Variant 1: With libDirAbs" << std::endl;
273 std::string lib_path_var2;
274 if( lib_path_var0.size() > 0 ) {
277 lib_path_var2 = libDirAbs;
279 ::setenv(lib_path_var_name.c_str(), lib_path_var2.c_str(), 1 );
280 std::cout <<
"- lib_path_var set 2: " << lib_path_var2 << std::endl;
284 std::cout <<
"- lib_path_var get 2: " << lib_path_var << std::endl;
285 std::cout <<
"- lib_paths: count : " << lib_paths.size() << std::endl <<
" - path: '";
286 std::cout <<
jau::to_string(lib_paths,
"'\n - path: '") <<
"'" << std::endl;
291 const bool searchSystemPath =
true;
292 const bool searchSystemPathFirst =
true;
293 const bool global =
false;
296 std::cout <<
"Check-10: libBasename in sys: '" << libBasename <<
"', cwd2 '" << cwd2 <<
"'" << std::endl;
298 std::cout <<
"Check-10: " << nl.
toString() << std::endl;
299 REQUIRE(
true == nl.
isValid() );
300 REQUIRE(
true == nl.
isOpen() );
302 REQUIRE(
false == nl.
isOpen() );
303 REQUIRE(
true == nl.
isValid() );
307 std::cout <<
"Check-11: libName in sys: '" << libName <<
"', cwd2 '" << cwd2 <<
"'" << std::endl;
309 std::cout <<
"Check-11: " << nl.
toString() << std::endl;
310 REQUIRE(
true == nl.
isValid() );
311 REQUIRE(
true == nl.
isOpen() );
313 REQUIRE(
false == nl.
isOpen() );
314 REQUIRE(
true == nl.
isValid() );
320 std::cout <<
"Sys-Path: '" << lib_path_var_name <<
"': Variant 2: With test_exe path" << std::endl;
321 std::string lib_path_var2;
322 if( lib_path_var0.size() > 0 ) {
325 lib_path_var2 = exe_dir;
327 ::setenv(lib_path_var_name.c_str(), lib_path_var2.c_str(), 1 );
328 std::cout <<
"- lib_path_var set 3: " << lib_path_var2 << std::endl;
332 std::cout <<
"- lib_path_var get 3: " << lib_path_var << std::endl;
333 std::cout <<
"- lib_paths: count : " << lib_paths.size() << std::endl <<
" - path: '";
334 std::cout <<
jau::to_string(lib_paths,
"'\n - path: '") <<
"'" << std::endl;
340 const bool searchSystemPath =
true;
341 const bool searchSystemPathFirst =
true;
342 const bool global =
true;
343 std::cout <<
"Check-12: Relative Path to cwd: " << libPathRel << std::endl;
345 std::cout <<
"Check-12: " << nl.
toString() << std::endl;
346 REQUIRE(
true == nl.
isValid() );
347 REQUIRE(
true == nl.
isOpen() );
349 REQUIRE(
false == nl.
isOpen() );
350 REQUIRE(
true == nl.
isValid() );
356TEST_CASE(
"Test10",
"[orig][NativeLibrary][dll][os]" ) {
361TEST_CASE(
"Test11",
"[copy][NativeLibrary][dll][os]" ) {
static std::string getProperty(const std::string &name) noexcept
Returns the value of the environment's variable 'name'.
Platform agnostic representation of POSIX ::lstat() and ::stat() for a given pathname.
constexpr bool exists() const noexcept
Returns true if entity does not exist, exclusive bit.
const file_stats * final_target(size_t *link_count=nullptr) const noexcept
Returns the final target element, either a pointer to this instance if not a symbolic-link or the fin...
std::string to_string() const noexcept
Returns a comprehensive string representation of this element.
std::string path() const noexcept
Returns the unix path representation.
Low level secure dynamic linker access.
static std::string getBaseName(const std::string &filename, const bool isBasename=false, const bool caseInsensitive=jau::os::is_windows()) noexcept
Returns the library basename, i.e.
static DynamicLinker & get() noexcept
Returns static singleton instance of DynamicLinker.
symhandle_t lookupSymbol(libhandle_t handle, const std::string &symbolName) noexcept
void * symhandle_t
symbol handle within a library
static constexpr_cxx20 std::string getEnvLibPathVarName() noexcept
Returns the environment library path variable name, e.g.
void * libhandle_t
library handle
const char * lookupLibraryPathname(libhandle_t handle, const std::string &symbolName) noexcept
static constexpr_cxx20 std::string getCanonicalName(const std::string &basename, const bool checkIsCanonical=true) noexcept
Returns canonical library name for this system from given library-basename, e.g.
libhandle_t openLibraryLocal(const std::string &pathname) noexcept
Opens the named library, restricting access to this process.
static std::vector< std::string > getSystemEnvLibraryPaths()
Returns a list of system paths, from the getSystemEnvLibraryPathVarname() variable.
Runtime libary dynamic library (RTLD) access.
bool isValid() const noexcept
Returns true if this instance is valid, i.e.
void close() noexcept
Closes this native library.
std::string toString() const noexcept
bool isOpen() const noexcept
Returns true if this instance isValid() and not close()'ed, otherwise false.
static NativeLibrary open(const std::string &libName, const bool searchSystemPath, const bool searchSystemPathFirst, const bool global) noexcept
Opens the given native library, assuming it has the same base name on all platforms.
std::string to_string(const endian_t v) noexcept
Return std::string representation of the given endian.
constexpr bool is_set(const E mask, const E bits) noexcept
bool mkdir(const std::string &path, const fmode_t mode=jau::fs::fmode_t::def_dir_prot, const bool verbose=false) noexcept
Create directory.
bool copy(const std::string &source_path, const std::string &dest_path, const copy_options copts=copy_options::none) noexcept
Copy the given source_path to dest_path using copy_options.
std::string get_cwd() noexcept
Return the current working directory or empty on failure.
bool visit(const std::string &path, const traverse_options topts, const path_visitor &visitor, std::vector< int > *dirfds=nullptr) noexcept
Visit element(s) of a given path, see traverse_options for detailed settings.
std::string absolute(const std::string_view &relpath) noexcept
Returns the absolute path of given relpath if existing, otherwise an empty string.
std::string dirname(const std::string_view &path) noexcept
Return stripped last component from given path separated by /, excluding the trailing separator /.
copy_options
Filesystem copy options used to copy() path elements.
traverse_options
Filesystem traverse options used to visit() path elements.
bool rename(const std::string &oldpath, const std::string &newpath) noexcept
Rename oldpath to newpath using POSIX rename(), with the following combinations.
bool remove(const std::string &path, const traverse_options topts=traverse_options::none) noexcept
Remove the given path.
bool chdir(const std::string &path) noexcept
Change working directory.
traverse_event
Filesystem traverse event used to call path_visitor for path elements from visit().
jau::function< bool(traverse_event, const file_stats &, size_t)> path_visitor
path_visitor jau::FunctionDef definition
@ def_dir_prot
Default directory protection bit: Safe default: POSIX S_IRWXU | S_IRGRP | S_IXGRP or rwx_usr | read_g...
@ verbose
Enable verbosity mode, show error messages on stderr.
@ overwrite
Overwrite existing destination files.
@ preserve_all
Preserve uid and gid if allowed and access- and modification-timestamps, i.e.
@ verbose
Enable verbosity mode, potentially used by a path_visitor implementation like remove().
@ dir_check_entry
Call path_visitor at directory entry, allowing path_visitor to skip traversal of this directory if re...
@ recursive
Traverse through directories, i.e.
@ none
No value, neither file, symlink nor dir_entry or dir_exit.
@ file
Visiting a file, may be in conjunction with symlink, i.e.
@ dir_check_entry
Visiting a directory on entry, see traverse_options::dir_check_entry.
@ symlink
Visiting a symbolic-link, either to a file or a non-existing entity.
constexpr_cxx20 std::string path_separator() noexcept
Returns the OS's path separator as a string, e.g.
std::string to_hexstring(value_type const &v, const bool skipLeading0x=false) noexcept
Produce a lower-case hexadecimal string representation with leading 0x in MSB of the given pointer.
Author: Sven Gothel sgothel@jausoft.com Copyright Gothel Software e.K.
Author: Sven Gothel sgothel@jausoft.com Copyright (c) 2022 Gothel Software e.K.
static bool existsLibBasename(const std::string &libBasename, const std::string &relDir, std::string &libPath) noexcept
void test10NativeLibrary(const std::string &libBasename, const std::string &libDirRel)
TEST_CASE("Test00", "[dll][os]")
void test01DynamikLinkerAbs(const std::string &libBasename, const std::string &relDir)
static bool existsPath(const std::string &libPath) noexcept