mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-01 20:10:28 +00:00
LibCore: Automatically create config directories if necessary
If the .config directory (or its children, like lib) was deleted, ConfigFile would crash because it would try to open or create a file in a directory that didn't exist. Therefore, for user and library configs (but not system configs), ensure that the parent directories exist. This allows the user to delete the entire .config folder and all apps still work. (Except those which can't handle missing config. That's a separate issue though.) Fixes #13555 Note: Some changes to pledges and unveils are necessary for this to work. The only one who can recreate .config at the moment is ConfigServer, as others probably don't pledge the user home directory.
This commit is contained in:
parent
46b76f2f55
commit
0fd09b2381
Notes:
sideshowbarker
2024-07-17 12:07:24 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/0fd09b2381 Pull-request: https://github.com/SerenityOS/serenity/pull/13603 Issue: https://github.com/SerenityOS/serenity/issues/13555 Reviewed-by: https://github.com/BertalanD Reviewed-by: https://github.com/alichraghi ✅ Reviewed-by: https://github.com/awesomekling
4 changed files with 11 additions and 4 deletions
|
@ -6,23 +6,28 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/ConfigFile.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/StandardPaths.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <pwd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_lib(String const& lib_name, AllowWriting allow_altering)
|
||||
{
|
||||
String directory = StandardPaths::config_directory();
|
||||
auto path = String::formatted("{}/lib/{}.ini", directory, lib_name);
|
||||
String directory_name = String::formatted("{}/lib", StandardPaths::config_directory());
|
||||
auto directory = TRY(Directory::create(directory_name, Directory::CreateDirectories::Yes));
|
||||
auto path = String::formatted("{}/{}.ini", directory, lib_name);
|
||||
return ConfigFile::open(path, allow_altering);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<ConfigFile>> ConfigFile::open_for_app(String const& app_name, AllowWriting allow_altering)
|
||||
{
|
||||
String directory = StandardPaths::config_directory();
|
||||
auto directory = TRY(Directory::create(StandardPaths::config_directory(), Directory::CreateDirectories::Yes));
|
||||
auto path = String::formatted("{}/{}.ini", directory, app_name);
|
||||
return ConfigFile::open(path, allow_altering);
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/RefPtr.h>
|
||||
|
|
|
@ -14,6 +14,7 @@ ErrorOr<int> serenity_main(Main::Arguments)
|
|||
{
|
||||
TRY(Core::System::pledge("stdio accept rpath wpath cpath"));
|
||||
TRY(Core::System::unveil(Core::StandardPaths::config_directory(), "rwc"));
|
||||
TRY(Core::System::unveil(Core::StandardPaths::home_directory(), "rwc"));
|
||||
TRY(Core::System::unveil(nullptr, nullptr));
|
||||
|
||||
Core::EventLoop event_loop;
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
ErrorOr<int> serenity_main(Main::Arguments)
|
||||
{
|
||||
TRY(Core::System::pledge("stdio proc exec rpath"));
|
||||
TRY(Core::System::pledge("stdio proc exec rpath cpath"));
|
||||
auto keyboard_settings_config = TRY(Core::ConfigFile::open_for_app("KeyboardSettings"));
|
||||
|
||||
TRY(Core::System::unveil("/bin/keymap", "x"));
|
||||
|
|
Loading…
Reference in a new issue