Fix linking a zone info to localtime
All checks were successful
Build / build (push) Successful in 19s

This commit is contained in:
Eugen Wissner 2024-06-05 11:34:23 +02:00
parent 9c95b1f864
commit 9d20748c5c
Signed by: belka
GPG Key ID: A27FDC1E8EE902C0
2 changed files with 30 additions and 48 deletions

View File

@ -36,18 +36,18 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
// Set time zone
if (g_strcmp0(method_name, "SetTimezone") == 0)
{
g_variant_get (parameters, "(&sb)", &timezone, &user_interaction);
g_variant_get(parameters, "(&sb)", &timezone, &user_interaction);
std::error_code error_code;
if (slack_set_timezone(timezone))
if (timedate1->set_timezone(timezone, error_code))
{
g_dbus_method_invocation_return_value (invocation, NULL);
g_dbus_method_invocation_return_value(invocation, NULL);
}
else
{
g_dbus_method_invocation_return_error (invocation,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"Write operation failed");
g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
"Set time zone: %s",
error_code ? error_code.message().c_str() : "Zone info is not a regular file");
}
g_free (timezone);
}
@ -181,23 +181,20 @@ namespace dlackware::timedate
{
void timedate1::list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator)
{
constexpr Glib::FileTest is_directory = Glib::FILE_TEST_IS_DIR;
constexpr Glib::FileTest is_regular = Glib::FILE_TEST_IS_REGULAR;
auto zoneinfo_path = std::filesystem::path(zoneinfo_database) / prefix;
auto zoneinfo_directory = Glib::Dir(zoneinfo_path);
for (auto zoneinfo_entry : zoneinfo_directory)
for (auto zoneinfo_entry : std::filesystem::directory_iterator(zoneinfo_path))
{
auto entry_path = zoneinfo_path / zoneinfo_entry;
auto new_prefix = prefix + zoneinfo_entry.path().filename().string();
if (Glib::file_test(entry_path, is_directory))
if (zoneinfo_entry.is_directory())
{
list_timezones(prefix + zoneinfo_entry + "/", accumulator);
list_timezones(new_prefix + "/", accumulator);
}
else if (Glib::file_test(entry_path, is_regular) && !entry_path.has_extension()
&& zoneinfo_entry != "leapseconds")
else if (zoneinfo_entry.is_regular_file() && !zoneinfo_entry.path().has_extension()
&& zoneinfo_entry.path().filename() != "leapseconds")
{
accumulator.push_back(prefix + zoneinfo_entry);
accumulator.emplace_back(new_prefix);
}
}
}
@ -221,38 +218,23 @@ namespace dlackware::timedate
}
return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 };
}
}
gboolean slack_set_timezone (gchar *zone) {
gchar *zone_file;
GFile *etc_localtime, *localtime_link, *g_zone_file;
zone_file = g_strconcat ("/usr/share/zoneinfo/", zone, NULL);
if (Glib::file_test(zone_file, Glib::FILE_TEST_IS_REGULAR))
bool timedate1::set_timezone(gchar *zone, std::error_code& ec)
{
etc_localtime = g_file_new_for_path ("/etc/localtime");
localtime_link = g_file_new_for_path ("/etc/localtime-copied-from");
g_zone_file = g_file_new_for_path (zone_file);
ec.clear();
auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone;
if (!g_file_copy (g_zone_file, etc_localtime, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL)) {
return FALSE;
}
if (!g_file_delete (localtime_link, NULL, NULL)) {
return FALSE;
}
if (!g_file_make_symbolic_link (localtime_link, zone_file, NULL, NULL)) {
return FALSE;
}
g_free (zone_file);
g_object_unref (etc_localtime);
g_object_unref (localtime_link);
g_object_unref (g_zone_file);
return true;
}
if (!std::filesystem::is_regular_file(zone_file, ec) || ec)
{
return false;
}
std::filesystem::path etc_localtime = "/etc/localtime";
std::filesystem::remove(etc_localtime);
std::filesystem::create_symlink(zone_file, etc_localtime, ec);
return !ec;
}
}
gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative)

View File

@ -73,13 +73,13 @@ namespace dlackware::timedate
// Returns the system time zone.
Glib::ustring timezone();
// Sets the system time zone to the one passed by the argument
// Returns true on success, false otherwise
bool set_timezone(gchar *, std::error_code& ec);
};
}
// Sets the system time zone to the one passed by the argument
// Returns true on success, false otherwise
gboolean slack_set_timezone (gchar *);
// Changes the date/time
// Takes the amount of seconds since UNIX epoche and
// Returns true on success, false otherwise