summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2024-06-05 11:34:23 +0200
committerEugen Wissner <belka@caraus.de>2024-06-05 11:34:23 +0200
commit9d20748c5c49e11ec02379b4f35c4a55dabe8399 (patch)
tree65065258e7870580f5226275468f818e9c194ca8
parent9c95b1f8644943c52ed1b8361ddb1686d469ce2a (diff)
downloadslack-timedate-9d20748c5c49e11ec02379b4f35c4a55dabe8399.tar.gz
Fix linking a zone info to localtime
-rw-r--r--src/slack-timedate.cpp76
-rw-r--r--src/slack-timedate.h8
2 files changed, 33 insertions, 51 deletions
diff --git a/src/slack-timedate.cpp b/src/slack-timedate.cpp
index b054ade..45b8e50 100644
--- a/src/slack-timedate.cpp
+++ b/src/slack-timedate.cpp
@@ -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);
-
- 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;
- }
- return false;
+ ec.clear();
+ auto zone_file = std::filesystem::path("/usr/share/zoneinfo") / zone;
+
+ 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)
diff --git a/src/slack-timedate.h b/src/slack-timedate.h
index c80cbf2..2f8229f 100644
--- a/src/slack-timedate.h
+++ b/src/slack-timedate.h
@@ -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