blob: e223508bd43909b72d938ea3f56f498221896c8e [file] [log] [blame]
Mario Limonciellod81ea2e2020-01-13 14:11:43 -06001/*
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
16gboolean
17fu_engine_update_motd (FuEngine *self, GError **error)
18{
19 guint upgrade_count = 0;
Richard Hughesdf89cd52020-06-26 20:25:18 +010020 g_autoptr(FuEngineRequest) request = fu_engine_request_new ();
Mario Limonciellod81ea2e2020-01-13 14:11:43 -060021 g_autoptr(GPtrArray) devices = NULL;
22 g_autoptr(GString) str = NULL;
23 g_autofree gchar *target = NULL;
24
Richard Hughesdf89cd52020-06-26 20:25:18 +010025 /* a subset of what fwupdmgr can do */
26 fu_engine_request_set_feature_flags (request,
27 FWUPD_FEATURE_FLAG_DETACH_ACTION |
28 FWUPD_FEATURE_FLAG_UPDATE_ACTION);
29
Mario Limonciellod81ea2e2020-01-13 14:11:43 -060030 /* get devices from daemon, we even want to know if it's nothing */
31 devices = fu_engine_get_devices (self, NULL);
32 if (devices != NULL) {
33 for (guint i = 0; i < devices->len; i++) {
34 FwupdDevice *dev = g_ptr_array_index (devices, i);
35 g_autoptr(GPtrArray) rels = NULL;
36
37 /* get the releases for this device */
38 rels = fu_engine_get_upgrades (self,
Richard Hughesdf89cd52020-06-26 20:25:18 +010039 request,
40 fwupd_device_get_id (dev),
41 NULL);
Mario Limonciellod81ea2e2020-01-13 14:11:43 -060042 if (rels == NULL)
43 continue;
44 upgrade_count++;
45 }
46 }
47
48 /* If running under systemd unit, use the directory as a base */
49 if (g_getenv ("RUNTIME_DIRECTORY") != NULL) {
50 target = g_build_filename (g_getenv ("RUNTIME_DIRECTORY"),
51 PACKAGE_NAME,
52 MOTD_FILE,
53 NULL);
54 /* otherwise use the cache directory */
55 } else {
56 g_autofree gchar *directory = fu_common_get_path (FU_PATH_KIND_CACHEDIR_PKG);
57 target = g_build_filename (directory, MOTD_DIR, MOTD_FILE, NULL);
58 }
59
60 /* create the directory and file, even if zero devices; we want an empty file then */
61 if (!fu_common_mkdir_parent (target, error))
62 return FALSE;
63
64 if (upgrade_count == 0) {
65 g_autoptr(GFile) file = g_file_new_for_path (target);
66 g_autoptr(GFileOutputStream) output = NULL;
67 output = g_file_replace (file, NULL, FALSE,
68 G_FILE_CREATE_NONE,
69 NULL, error);
70 return output != NULL;
71 }
72
Mario Limonciellod89bf612020-02-06 16:38:46 -060073 str = g_string_new ("\n");
Mario Limonciello6f6f50c2020-01-31 11:07:39 -060074 g_string_append_printf (str, ngettext ("%u device has a firmware upgrade available.",
75 "%u devices have a firmware upgrade available.",
76 upgrade_count),
77 upgrade_count);
Mario Limonciellod81ea2e2020-01-13 14:11:43 -060078 g_string_append_printf (str,
Mario Limonciellod89bf612020-02-06 16:38:46 -060079 "\n%s\n\n",
Mario Limonciellod81ea2e2020-01-13 14:11:43 -060080 _("Run `fwupdmgr get-upgrades` for more information."));
81 return g_file_set_contents (target, str->str, str->len, error);
82}
83