From 594e997a0dad7bdc0974d713969a4482f34d615b Mon Sep 17 00:00:00 2001 From: deepend Date: Fri, 4 Dec 2026 18:26:41 -0600 Subject: [PATCH] fix Linux storage reporting --- plugins/sysinfo/meson.build | 2 +- plugins/sysinfo/unix/modern-backend.c | 31 --- plugins/sysinfo/unix/storage.c | 294 ++++++++++++++++++++++++++ plugins/sysinfo/unix/storage.h | 11 + 4 files changed, 306 insertions(+), 32 deletions(-) create mode 100644 plugins/sysinfo/unix/storage.c create mode 100644 plugins/sysinfo/unix/storage.h diff --git a/plugins/sysinfo/meson.build b/plugins/sysinfo/meson.build index f64be5e2..d8cb7da3 100644 --- a/plugins/sysinfo/meson.build +++ b/plugins/sysinfo/meson.build @@ -24,7 +24,7 @@ if system == 'linux' endif sysinfo_includes += 'unix' - sysinfo_sources += 'unix/modern-backend.c' + sysinfo_sources += ['unix/modern-backend.c', 'unix/storage.c'] elif system == 'freebsd' or system == 'gnu' or system.startswith('gnu/') sysinfo_includes += ['shared', 'unix'] diff --git a/plugins/sysinfo/unix/modern-backend.c b/plugins/sysinfo/unix/modern-backend.c index 6240a259..8d074ddf 100644 --- a/plugins/sysinfo/unix/modern-backend.c +++ b/plugins/sysinfo/unix/modern-backend.c @@ -789,37 +789,6 @@ sysinfo_backend_get_memory (void) return memory; } -char * -sysinfo_backend_get_disk (void) -{ - const char *path; - struct statvfs fs; - guint64 block_size; - guint64 total; - guint64 available; - char *usage; - - /* Flatpak's home itself can be tmpfs; its data directory is persistent - * storage on the host filesystem that actually holds the app's data. */ - path = g_file_test ("/.flatpak-info", G_FILE_TEST_IS_REGULAR) - ? g_get_user_data_dir () : g_get_home_dir (); - if (!path || path[0] == '\0') - { - path = "/"; - } - - if (statvfs (path, &fs) != 0) - { - return NULL; - } - - block_size = fs.f_frsize != 0 ? (guint64) fs.f_frsize : (guint64) fs.f_bsize; - total = (guint64) fs.f_blocks * block_size; - available = (guint64) fs.f_bavail * block_size; - usage = format_usage (total, available); - return usage; -} - char * sysinfo_backend_get_uptime (void) { diff --git a/plugins/sysinfo/unix/storage.c b/plugins/sysinfo/unix/storage.c new file mode 100644 index 00000000..086be1fc --- /dev/null +++ b/plugins/sysinfo/unix/storage.c @@ -0,0 +1,294 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Report mounted local filesystem capacity without probing network mounts. */ +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(__has_include) +# if __has_include() +# include +# endif +#endif + +#include +#include "../format.h" +#include "../sysinfo-backend.h" +#include "storage.h" + +typedef struct +{ + guint64 total; + guint64 available; +} StorageUsage; + +static char * +decode_mount_field (const char *field) +{ + GString *value = g_string_new (NULL); + const char *p; + + for (p = field; *p; p++) + { + /* mountinfo escapes whitespace and backslashes as octal. */ + if (*p == '\\' && p[1] && p[2] && p[3] && + p[1] >= '0' && p[1] <= '3' && + p[2] >= '0' && p[2] <= '7' && p[3] >= '0' && p[3] <= '7') + { + guint code = (p[1] - '0') * 64 + (p[2] - '0') * 8 + p[3] - '0'; + if (code == 0) + { + g_string_free (value, TRUE); + return NULL; + } + g_string_append_c (value, (char) code); + p += 3; + } + else + g_string_append_c (value, *p); + } + return g_string_free (value, FALSE); +} + +gboolean +sysinfo_linux_mountinfo_parse (const char *line, char **device, + char **mountpoint, char **filesystem, char **source) +{ + char **fields; + guint i, n; + gboolean valid = FALSE; + + *device = *mountpoint = *filesystem = *source = NULL; + fields = g_strsplit (line, " ", -1); + n = g_strv_length (fields); + for (i = 6; i + 3 < n; i++) + { + if (strcmp (fields[i], "-") != 0) + continue; + if (!strchr (fields[2], ':') || fields[4][0] != '/' || !fields[i + 1][0]) + break; + *mountpoint = decode_mount_field (fields[4]); + *source = decode_mount_field (fields[i + 2]); + if (*mountpoint && *source) + { + *device = g_strdup (fields[2]); + *filesystem = g_strdup (fields[i + 1]); + valid = TRUE; + } + else + { + g_clear_pointer (mountpoint, g_free); + g_clear_pointer (source, g_free); + } + break; + } + g_strfreev (fields); + return valid; +} + +static gboolean +local_filesystem (const char *filesystem) +{ + /* A conservative list avoids statvfs on NFS, SMB, autofs and remote FUSE. + * fuseblk is accepted only when a real block device is verified below. */ + static const char *types[] = { + "ext2", "ext3", "ext4", "xfs", "btrfs", "bcachefs", "f2fs", + "jfs", "reiserfs", "nilfs2", "vfat", "msdos", "exfat", "ntfs", + "ntfs3", "fuseblk", "hfs", "hfsplus", "ufs", "minix", "zfs", NULL + }; + guint i; + for (i = 0; types[i]; i++) + if (strcmp (types[i], filesystem) == 0) + return TRUE; + return FALSE; +} + +static gboolean +read_usage (const char *path, StorageUsage *usage) +{ + struct statvfs fs; + guint64 block_size; + + if (statvfs (path, &fs) != 0) + return FALSE; + block_size = fs.f_frsize ? (guint64) fs.f_frsize : (guint64) fs.f_bsize; + if (!block_size || !fs.f_blocks || (guint64) fs.f_blocks > G_MAXUINT64 / block_size) + return FALSE; + usage->total = (guint64) fs.f_blocks * block_size; + usage->available = MIN ((guint64) fs.f_bavail, (guint64) fs.f_blocks) * block_size; + return TRUE; +} + +static char * +filesystem_key (const char *device, const char *mountpoint, + const char *filesystem, const char *source) +{ + struct stat st; + char *id, *sys_path, *target, *name; + gboolean block_source = FALSE; + + if (strcmp (filesystem, "zfs") == 0) + { + /* Datasets share their pool; don't add its capacity repeatedly. */ + const char *slash = strchr (source, '/'); + return source[0] && source[0] != '/' ? + g_strdup_printf ("zfs:%.*s", (int) (slash ? (gsize) (slash - source) : strlen (source)), source) : NULL; + } + + /* A bind mount shares the device number. Btrfs subvolumes can have + * separate synthetic device numbers, so prefer the source block device. */ + if (g_str_has_prefix (source, "/dev/") && stat (source, &st) == 0 && S_ISBLK (st.st_mode)) + { + id = g_strdup_printf ("%u:%u", (unsigned int) major (st.st_rdev), + (unsigned int) minor (st.st_rdev)); + block_source = TRUE; + } + else + id = g_strdup (device); + + sys_path = g_build_filename ("/sys/dev/block", id, NULL); + target = g_file_read_link (sys_path, NULL); + g_free (sys_path); + if (!target && !block_source) + { + g_free (id); + return NULL; + } + name = g_path_get_basename (target ? target : source); + g_free (target); + if (g_str_has_prefix (name, "loop") || g_str_has_prefix (name, "ram") || + g_str_has_prefix (name, "zram") || g_str_has_prefix (name, "fd") || + g_str_has_prefix (name, "sr")) + { + g_free (name); + g_free (id); + return NULL; + } + g_free (name); + +#ifdef BTRFS_IOC_FS_INFO + if (strcmp (filesystem, "btrfs") == 0) + { + /* The filesystem UUID is shared by all subvolumes and members of + * a multi-device filesystem (unlike st_dev or statfs.f_fsid). */ + int fd = open (mountpoint, O_RDONLY); + if (fd >= 0) + { + struct btrfs_ioctl_fs_info_args info = {0}; + if (ioctl (fd, BTRFS_IOC_FS_INFO, &info) == 0) + { + GString *uuid = g_string_new ("btrfs:"); + guint i; + for (i = 0; i < sizeof (info.fsid); i++) + g_string_append_printf (uuid, "%02x", (unsigned int) info.fsid[i]); + g_free (id); + id = g_string_free (uuid, FALSE); + } + close (fd); + } + } +#else + (void) mountpoint; +#endif + return id; +} + +static char * +format_scoped_usage (const StorageUsage *usage, const char *scope) +{ + char *formatted = sysinfo_format_usage (usage->total, usage->available); + char *result; + if (!formatted) + return NULL; + result = g_strdup_printf ("%s (%s)", formatted, scope); + g_free (formatted); + return result; +} + +char * +sysinfo_backend_get_disk (void) +{ + const gboolean flatpak = g_file_test ("/.flatpak-info", G_FILE_TEST_IS_REGULAR); + const char *path = flatpak ? g_get_user_data_dir () : g_get_home_dir (); + StorageUsage aggregate = {0, 0}; + char *mountinfo = NULL; + + /* Flatpak sees a sandbox mount namespace, not all host disks. */ + if (!flatpak && g_file_get_contents ("/proc/self/mountinfo", &mountinfo, NULL, NULL)) + { + GHashTable *volumes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + char **lines = g_strsplit (mountinfo, "\n", -1); + guint i; + GHashTableIter iter; + gpointer value; + + for (i = 0; lines[i]; i++) + { + char *device, *mountpoint, *filesystem, *source; + char *key = NULL; + StorageUsage usage; + struct stat st; + char *actual_device = NULL; + + if (!sysinfo_linux_mountinfo_parse (lines[i], &device, &mountpoint, &filesystem, &source)) + continue; + /* A covered mount must not be measured as the filesystem on top of it. */ + if (local_filesystem (filesystem) && stat (mountpoint, &st) == 0) + { + actual_device = g_strdup_printf ("%u:%u", (unsigned int) major (st.st_dev), + (unsigned int) minor (st.st_dev)); + if (strcmp (device, actual_device) == 0) + key = filesystem_key (device, mountpoint, filesystem, source); + } + if (key && read_usage (mountpoint, &usage)) + { + StorageUsage *previous = g_hash_table_lookup (volumes, key); + /* Quotas may expose different sizes on the same filesystem. + * Keep one observation rather than adding shared capacity. */ + if (!previous || usage.total > previous->total || + (usage.total == previous->total && usage.available > previous->available)) + { + StorageUsage *copy = g_new (StorageUsage, 1); + *copy = usage; + g_hash_table_replace (volumes, key, copy); + key = NULL; + } + } + g_free (actual_device); + g_free (key); + g_free (device); + g_free (mountpoint); + g_free (filesystem); + g_free (source); + } + g_hash_table_iter_init (&iter, volumes); + while (g_hash_table_iter_next (&iter, NULL, &value)) + { + const StorageUsage *usage = value; + if (usage->total > G_MAXUINT64 - aggregate.total) + { + aggregate.total = 0; + break; + } + aggregate.total += usage->total; + aggregate.available += usage->available; + } + g_hash_table_unref (volumes); + g_strfreev (lines); + g_free (mountinfo); + if (aggregate.total) + return format_scoped_usage (&aggregate, "mounted local filesystems"); + } + + /* Containers and unusual filesystems may not expose block identities. + * Do not describe this limited view as the whole machine's capacity. */ + if (!path || !path[0]) + path = "/"; + if (!read_usage (path, &aggregate)) + return NULL; + return format_scoped_usage (&aggregate, flatpak ? "app data filesystem" : "home filesystem"); +} diff --git a/plugins/sysinfo/unix/storage.h b/plugins/sysinfo/unix/storage.h new file mode 100644 index 00000000..14e3ea9a --- /dev/null +++ b/plugins/sysinfo/unix/storage.h @@ -0,0 +1,11 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef SYSINFO_LINUX_STORAGE_H +#define SYSINFO_LINUX_STORAGE_H + +#include + +/* The returned strings are owned by the caller. */ +gboolean sysinfo_linux_mountinfo_parse (const char *line, char **device, + char **mountpoint, char **filesystem, char **source); + +#endif