LibSQL+Ladybird: Accept a list of paths for spawning SQLServer in Lagom

Use the new get_paths_for_helper_process method in Ladybird to query
Qt for the runtime path of the current executable as well as the build
directory paths.
This commit is contained in:
Andrew Kaster 2023-02-02 03:03:46 -07:00 committed by Andrew Kaster
parent 3e6d790cf0
commit ae9dc95b1f
Notes: sideshowbarker 2024-07-17 00:54:48 +09:00
4 changed files with 23 additions and 15 deletions

View File

@ -5,6 +5,7 @@
*/
#include "BrowserWindow.h"
#include "HelperProcess.h"
#include "Settings.h"
#include "Utilities.h"
#include "WebContentView.h"
@ -99,7 +100,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return app.exec();
}
auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client("./SQLServer/SQLServer"sv));
auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client(move(sql_server_paths)));
auto database = TRY(Browser::Database::create(move(sql_client)));
auto cookie_jar = TRY(Browser::CookieJar::create(*database));

View File

@ -6,6 +6,7 @@
*/
#include <AK/DeprecatedString.h>
#include <AK/String.h>
#include <LibSQL/SQLClient.h>
#if !defined(AK_OS_SERENITY)
@ -50,7 +51,7 @@ static ErrorOr<int> create_database_socket(DeprecatedString const& socket_path)
return socket_fd;
}
static ErrorOr<void> launch_server(DeprecatedString const& socket_path, DeprecatedString const& pid_path, StringView server_path)
static ErrorOr<void> launch_server(DeprecatedString const& socket_path, DeprecatedString const& pid_path, Vector<String> candidate_server_paths)
{
auto server_fd_or_error = create_database_socket(socket_path);
if (server_fd_or_error.is_error()) {
@ -77,15 +78,19 @@ static ErrorOr<void> launch_server(DeprecatedString const& socket_path, Deprecat
auto takeover_string = DeprecatedString::formatted("SQLServer:{}", server_fd);
TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
auto arguments = Array {
server_path,
"--pid-file"sv,
pid_path,
};
auto result = Core::System::exec(arguments[0], arguments, Core::System::SearchInPath::Yes);
ErrorOr<void> result;
for (auto const& server_path : candidate_server_paths) {
auto arguments = Array {
server_path.bytes_as_string_view(),
"--pid-file"sv,
pid_path,
};
auto result = Core::System::exec(arguments[0], arguments, Core::System::SearchInPath::Yes);
if (!result.is_error())
break;
}
if (result.is_error()) {
warnln("Could not launch {}: {}", server_path, result.error());
warnln("Could not launch any of {}: {}", candidate_server_paths, result.error());
TRY(Core::System::unlink(pid_path));
}
@ -132,14 +137,14 @@ static ErrorOr<bool> should_launch_server(DeprecatedString const& pid_path)
return false;
}
ErrorOr<NonnullRefPtr<SQLClient>> SQLClient::launch_server_and_create_client(StringView server_path)
ErrorOr<NonnullRefPtr<SQLClient>> SQLClient::launch_server_and_create_client(Vector<String> candidate_server_paths)
{
auto runtime_directory = TRY(Core::StandardPaths::runtime_directory());
auto socket_path = DeprecatedString::formatted("{}/SQLServer.socket", runtime_directory);
auto pid_path = DeprecatedString::formatted("{}/SQLServer.pid", runtime_directory);
if (TRY(should_launch_server(pid_path)))
TRY(launch_server(socket_path, pid_path, server_path));
TRY(launch_server(socket_path, pid_path, move(candidate_server_paths)));
auto socket = TRY(Core::Stream::LocalSocket::connect(move(socket_path)));
TRY(socket->set_blocking(true));

View File

@ -22,7 +22,7 @@ class SQLClient
public:
#if !defined(AK_OS_SERENITY)
static ErrorOr<NonnullRefPtr<SQLClient>> launch_server_and_create_client(StringView server_path);
static ErrorOr<NonnullRefPtr<SQLClient>> launch_server_and_create_client(Vector<String> candidate_server_paths);
#endif
virtual ~SQLClient() = default;

View File

@ -7,6 +7,7 @@
#include <AK/DeprecatedString.h>
#include <AK/Format.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
@ -361,8 +362,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
#if defined(AK_OS_SERENITY)
auto sql_client = TRY(SQL::SQLClient::try_create());
#else
VERIFY(sql_server_path != nullptr);
auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client(sql_server_path));
VERIFY(!sql_server_path.is_empty());
auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client({ TRY(String::from_utf8(sql_server_path)) }));
#endif
SQLRepl repl(loop, database_name, move(sql_client));