mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:47:43 +00:00
LibCore+LibFileSystem: Move resolve_executable_from_environment to Core
This commit is contained in:
parent
00bd443d1c
commit
26efdf9d4f
3 changed files with 39 additions and 26 deletions
|
@ -1655,4 +1655,39 @@ ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// This constant is copied from LibFileSystem. We cannot use or even include it directly,
|
||||||
|
// because that would cause a dependency of LibCore on LibFileSystem, effectively rendering
|
||||||
|
// the distinction between these libraries moot.
|
||||||
|
static constexpr StringView INTERNAL_DEFAULT_PATH_SV = "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv;
|
||||||
|
|
||||||
|
ErrorOr<String> resolve_executable_from_environment(StringView filename, int flags)
|
||||||
|
{
|
||||||
|
if (filename.is_empty())
|
||||||
|
return Error::from_errno(ENOENT);
|
||||||
|
|
||||||
|
// Paths that aren't just a file name generally count as already resolved.
|
||||||
|
if (filename.contains('/')) {
|
||||||
|
TRY(Core::System::access(filename, X_OK, flags));
|
||||||
|
return TRY(String::from_utf8(filename));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto const* path_str = ::getenv("PATH");
|
||||||
|
StringView path;
|
||||||
|
if (path_str)
|
||||||
|
path = { path_str, strlen(path_str) };
|
||||||
|
if (path.is_empty())
|
||||||
|
path = INTERNAL_DEFAULT_PATH_SV;
|
||||||
|
|
||||||
|
auto directories = path.split_view(':');
|
||||||
|
|
||||||
|
for (auto directory : directories) {
|
||||||
|
auto file = TRY(String::formatted("{}/{}", directory, filename));
|
||||||
|
|
||||||
|
if (!Core::System::access(file, X_OK, flags).is_error())
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Error::from_errno(ENOENT);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -267,4 +267,6 @@ ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servnam
|
||||||
ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length);
|
ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
ErrorOr<String> resolve_executable_from_environment(StringView filename, int flags = 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -361,32 +361,8 @@ ErrorOr<void> link_file(StringView destination_path, StringView source_path)
|
||||||
|
|
||||||
ErrorOr<String> resolve_executable_from_environment(StringView filename, int flags)
|
ErrorOr<String> resolve_executable_from_environment(StringView filename, int flags)
|
||||||
{
|
{
|
||||||
if (filename.is_empty())
|
// FIXME: Callers should Call Core::System::resolve_executable_from_environment instead of FileSystem::resolve_executable_from_environment.
|
||||||
return Error::from_errno(ENOENT);
|
return Core::System::resolve_executable_from_environment(filename, flags);
|
||||||
|
|
||||||
// Paths that aren't just a file name generally count as already resolved.
|
|
||||||
if (filename.contains('/')) {
|
|
||||||
TRY(Core::System::access(filename, X_OK, flags));
|
|
||||||
return TRY(String::from_utf8(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
auto const* path_str = ::getenv("PATH");
|
|
||||||
StringView path;
|
|
||||||
if (path_str)
|
|
||||||
path = { path_str, strlen(path_str) };
|
|
||||||
if (path.is_empty())
|
|
||||||
path = DEFAULT_PATH_SV;
|
|
||||||
|
|
||||||
auto directories = path.split_view(':');
|
|
||||||
|
|
||||||
for (auto directory : directories) {
|
|
||||||
auto file = TRY(String::formatted("{}/{}", directory, filename));
|
|
||||||
|
|
||||||
if (!Core::System::access(file, X_OK, flags).is_error())
|
|
||||||
return file;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Error::from_errno(ENOENT);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool looks_like_shared_library(StringView path)
|
bool looks_like_shared_library(StringView path)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue