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

@ -37,17 +37,17 @@ static void slack_method_call (GDBusConnection *connection, const gchar *sender,
if (g_strcmp0(method_name, "SetTimezone") == 0) 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 else
{ {
g_dbus_method_invocation_return_error (invocation, g_dbus_method_invocation_return_error(invocation, G_IO_ERROR, G_IO_ERROR_FAILED,
G_IO_ERROR, "Set time zone: %s",
G_IO_ERROR_FAILED, error_code ? error_code.message().c_str() : "Zone info is not a regular file");
"Write operation failed");
} }
g_free (timezone); g_free (timezone);
} }
@ -181,23 +181,20 @@ namespace dlackware::timedate
{ {
void timedate1::list_timezones(const std::string& prefix, std::vector<Glib::ustring>& accumulator) 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_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() else if (zoneinfo_entry.is_regular_file() && !zoneinfo_entry.path().has_extension()
&& zoneinfo_entry != "leapseconds") && zoneinfo_entry.path().filename() != "leapseconds")
{ {
accumulator.push_back(prefix + zoneinfo_entry); accumulator.emplace_back(new_prefix);
} }
} }
} }
@ -221,39 +218,24 @@ namespace dlackware::timedate
} }
return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 }; return Glib::ustring{ zone_copied_from.get() + strlen(zoneinfo_database) + 1 };
} }
}
gboolean slack_set_timezone (gchar *zone) { bool timedate1::set_timezone(gchar *zone, std::error_code& ec)
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))
{ {
etc_localtime = g_file_new_for_path ("/etc/localtime"); ec.clear();
localtime_link = g_file_new_for_path ("/etc/localtime-copied-from"); auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone;
g_zone_file = g_file_new_for_path (zone_file);
if (!g_file_copy (g_zone_file, etc_localtime, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, NULL)) { if (!std::filesystem::is_regular_file(zone_file, ec) || ec)
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;
}
return false; 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) gboolean slack_set_time(gint64 seconds_since_epoch, gboolean relative)
{ {

View File

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