Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 Mario Limonciello <mario.limonciello@dell.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: LGPL-2.1+ |
| 5 | */ |
| 6 | |
| 7 | #define G_LOG_DOMAIN "FuEngine" |
| 8 | |
| 9 | #include "config.h" |
| 10 | |
| 11 | #include <glib/gi18n.h> |
| 12 | |
| 13 | #include "fu-engine.h" |
| 14 | #include "fu-engine-helper.h" |
| 15 | |
| 16 | gboolean |
| 17 | fu_engine_update_motd (FuEngine *self, GError **error) |
| 18 | { |
| 19 | guint upgrade_count = 0; |
| 20 | g_autoptr(GPtrArray) devices = NULL; |
| 21 | g_autoptr(GString) str = NULL; |
| 22 | g_autofree gchar *target = NULL; |
| 23 | |
| 24 | /* get devices from daemon, we even want to know if it's nothing */ |
| 25 | devices = fu_engine_get_devices (self, NULL); |
| 26 | if (devices != NULL) { |
| 27 | for (guint i = 0; i < devices->len; i++) { |
| 28 | FwupdDevice *dev = g_ptr_array_index (devices, i); |
| 29 | g_autoptr(GPtrArray) rels = NULL; |
| 30 | |
| 31 | /* get the releases for this device */ |
| 32 | rels = fu_engine_get_upgrades (self, |
| 33 | fwupd_device_get_id (dev), |
| 34 | NULL); |
| 35 | if (rels == NULL) |
| 36 | continue; |
| 37 | upgrade_count++; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /* If running under systemd unit, use the directory as a base */ |
| 42 | if (g_getenv ("RUNTIME_DIRECTORY") != NULL) { |
| 43 | target = g_build_filename (g_getenv ("RUNTIME_DIRECTORY"), |
| 44 | PACKAGE_NAME, |
| 45 | MOTD_FILE, |
| 46 | NULL); |
| 47 | /* otherwise use the cache directory */ |
| 48 | } else { |
| 49 | g_autofree gchar *directory = fu_common_get_path (FU_PATH_KIND_CACHEDIR_PKG); |
| 50 | target = g_build_filename (directory, MOTD_DIR, MOTD_FILE, NULL); |
| 51 | } |
| 52 | |
| 53 | /* create the directory and file, even if zero devices; we want an empty file then */ |
| 54 | if (!fu_common_mkdir_parent (target, error)) |
| 55 | return FALSE; |
| 56 | |
| 57 | if (upgrade_count == 0) { |
| 58 | g_autoptr(GFile) file = g_file_new_for_path (target); |
| 59 | g_autoptr(GFileOutputStream) output = NULL; |
| 60 | output = g_file_replace (file, NULL, FALSE, |
| 61 | G_FILE_CREATE_NONE, |
| 62 | NULL, error); |
| 63 | return output != NULL; |
| 64 | } |
| 65 | |
| 66 | str = g_string_new (""); |
Mario Limonciello | 6f6f50c | 2020-01-31 11:07:39 -0600 | [diff] [blame^] | 67 | g_string_append_printf (str, ngettext ("%u device has a firmware upgrade available.", |
| 68 | "%u devices have a firmware upgrade available.", |
| 69 | upgrade_count), |
| 70 | upgrade_count); |
Mario Limonciello | d81ea2e | 2020-01-13 14:11:43 -0600 | [diff] [blame] | 71 | g_string_append_printf (str, |
| 72 | "\n%s\n", |
| 73 | _("Run `fwupdmgr get-upgrades` for more information.")); |
| 74 | return g_file_set_contents (target, str->str, str->len, error); |
| 75 | } |
| 76 | |