aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOndrej Čerman <ondrej.cerman@gmail.com>2021-10-16 22:30:58 +0200
committerOndrej Čerman <ondrej.cerman@gmail.com>2021-10-17 22:26:13 +0200
commite941b2ada4ef3cad921d0fe87dcc75baf2cdcb73 (patch)
treeea9b43f9c8be4469cf7c10a73b62504e579c5990
parent7d647c8eafb9d408f8984c24774e2c1e3339cca0 (diff)
devices/storage - refactoring - all code that is extending udisks2 information has been moved to separate file.
-rw-r--r--CMakeLists.txt2
-rw-r--r--hardinfo/storage_util.c179
-rw-r--r--hardinfo/udisks2_util.c138
-rw-r--r--includes/storage_util.h17
-rw-r--r--includes/udisks2_util.h3
-rw-r--r--modules/devices.c2
-rw-r--r--modules/devices/storage.c17
7 files changed, 210 insertions, 148 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2499e7fe..5b2f098a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -304,6 +304,7 @@ add_executable(hardinfo
hardinfo/x_util.c
hardinfo/gpu_util.c
hardinfo/udisks2_util.c
+ hardinfo/storage_util.c
shell/callbacks.c
shell/iconcache.c
shell/menu.c
@@ -345,6 +346,7 @@ add_executable(hardinfo
hardinfo/x_util.c
hardinfo/gpu_util.c
hardinfo/udisks2_util.c
+ hardinfo/storage_util.c
shell/callbacks.c
shell/iconcache.c
shell/menu.c
diff --git a/hardinfo/storage_util.c b/hardinfo/storage_util.c
new file mode 100644
index 00000000..78802e4b
--- /dev/null
+++ b/hardinfo/storage_util.c
@@ -0,0 +1,179 @@
+#include "udisks2_util.h"
+#include "storage_util.h"
+#include "util_ids.h"
+#include "hardinfo.h"
+
+gchar *sdcard_ids_file = NULL;
+
+// moved from udisks2_util.h
+void find_sdcard_ids_file() {
+ if (sdcard_ids_file) return;
+ char *file_search_order[] = {
+ g_build_filename(g_get_user_config_dir(), "hardinfo", "sdcard.ids", NULL),
+ g_build_filename(params.path_data, "sdcard.ids", NULL),
+ NULL
+ };
+ int n;
+ for(n = 0; file_search_order[n]; n++) {
+ if (!sdcard_ids_file && !access(file_search_order[n], R_OK))
+ sdcard_ids_file = file_search_order[n];
+ else
+ g_free(file_search_order[n]);
+ }
+}
+
+// moved from udisks2_util.h
+void check_sdcard_vendor(u2driveext *e) {
+ if (!e || !e->d) return;
+ if (!e->d->media) return;
+ if (! (g_str_has_prefix(e->d->media, "flash_sd")
+ || g_str_has_prefix(e->d->media, "flash_mmc") )) return;
+ if (e->d->vendor && e->d->vendor[0]) return;
+ if (!e->d->block_dev) return;
+
+ if (!sdcard_ids_file)
+ find_sdcard_ids_file();
+
+ gchar *qpath = NULL;
+ ids_query_result result = {};
+
+ gchar *oemid_path = g_strdup_printf("/sys/block/%s/device/oemid", e->d->block_dev);
+ gchar *manfid_path = g_strdup_printf("/sys/block/%s/device/manfid", e->d->block_dev);
+ gchar *oemid = NULL, *manfid = NULL;
+ g_file_get_contents(oemid_path, &oemid, NULL, NULL);
+ g_file_get_contents(manfid_path, &manfid, NULL, NULL);
+
+ unsigned int id = strtol(oemid, NULL, 16);
+ char c2 = id & 0xff, c1 = (id >> 8) & 0xff;
+
+ qpath = g_strdup_printf("OEMID %02x%02x", (unsigned int)c1, (unsigned int)c2);
+ scan_ids_file(sdcard_ids_file, qpath, &result, -1);
+ g_free(oemid);
+ if (result.results[0])
+ oemid = g_strdup(result.results[0]);
+ else
+ oemid = g_strdup_printf("OEM %02x%02x \"%c%c\"",
+ (unsigned int)c1, (unsigned int)c2,
+ isprint(c1) ? c1 : '.', isprint(c2) ? c2 : '.');
+ g_free(qpath);
+
+ id = strtol(manfid, NULL, 16);
+ qpath = g_strdup_printf("MANFID %06x", id);
+ scan_ids_file(sdcard_ids_file, qpath, &result, -1);
+ g_free(manfid);
+ if (result.results[0])
+ manfid = g_strdup(result.results[0]);
+ else
+ manfid = g_strdup_printf("MANF %06x", id);
+ g_free(qpath);
+
+ vendor_list vl = NULL;
+ const Vendor *v = NULL;
+ v = vendor_match(oemid, NULL);
+ if (v) vl = vendor_list_append(vl, v);
+ v = vendor_match(manfid, NULL);
+ if (v) vl = vendor_list_append(vl, v);
+ vl = vendor_list_remove_duplicates_deep(vl);
+ e->vendors = vendor_list_concat(e->vendors, vl);
+
+ g_free(e->d->vendor);
+ if (g_strcmp0(oemid, manfid) == 0)
+ e->d->vendor = g_strdup(oemid);
+ else
+ e->d->vendor = g_strdup_printf("%s / %s", oemid, manfid);
+
+ g_free(oemid);
+ g_free(manfid);
+ g_free(oemid_path);
+ g_free(manfid_path);
+
+ if (e->d->revision && e->d->revision[0]) return;
+
+ /* bonus: revision */
+ gchar *fwrev_path = g_strdup_printf("/sys/block/%s/device/fwrev", e->d->block_dev);
+ gchar *hwrev_path = g_strdup_printf("/sys/block/%s/device/hwrev", e->d->block_dev);
+ gchar *fwrev = NULL, *hwrev = NULL;
+ g_file_get_contents(fwrev_path, &fwrev, NULL, NULL);
+ g_file_get_contents(hwrev_path, &hwrev, NULL, NULL);
+
+ unsigned int fw = fwrev ? strtol(fwrev, NULL, 16) : 0;
+ unsigned int hw = hwrev ? strtol(hwrev, NULL, 16) : 0;
+ g_free(e->d->revision);
+ e->d->revision = g_strdup_printf("%02x.%02x", hw, fw);
+
+ g_free(fwrev);
+ g_free(hwrev);
+ g_free(fwrev_path);
+ g_free(hwrev_path);
+
+}
+
+void set_nvme_controller_info(u2driveext *e){
+ gchar *path = g_strdup_printf("/sys/block/%s/device/device", e->d->block_dev);
+ gchar *systarget = g_file_read_link(path, NULL);
+ gchar *target = systarget ? g_filename_to_utf8(systarget, -1, NULL, NULL, NULL) : NULL;
+ gchar *pci_addy = target ? g_path_get_basename(target) : NULL;
+ e->nvme_controller = pci_addy ? pci_get_device_str(pci_addy) : NULL;
+ g_free(path);
+ g_free(systarget);
+ g_free(target);
+ g_free(pci_addy);
+ if (e->nvme_controller) {
+ e->vendors = vendor_list_append(e->vendors,
+ vendor_match(e->nvme_controller->vendor_id_str, NULL));
+ e->vendors = vendor_list_append(e->vendors,
+ vendor_match(e->nvme_controller->sub_vendor_id_str, NULL));
+ }
+}
+
+GSList* get_udisks2_drives_ext(void){
+ GSList *node, *list;
+ u2driveext* extdrive;
+
+ list = get_udisks2_all_drives_info();
+
+ for (node = list; node != NULL; node = node->next) {
+ extdrive = u2drive_ext((udiskd *)node->data);
+ node->data = extdrive;
+
+ if (!extdrive->d->vendor || !extdrive->d->vendor[0]) {
+ // sometimes vendors adds their name to the model field
+ const Vendor *v = vendor_match(extdrive->d->model, NULL);
+ if (v)
+ extdrive->d->vendor = g_strdup(v->name);
+ }
+
+ check_sdcard_vendor(extdrive);
+
+ extdrive->vendors = vendor_list_append(extdrive->vendors, vendor_match(extdrive->d->vendor, NULL));
+
+ // NVMe PCI device
+ if (strstr(extdrive->d->block_dev, "nvme")) {
+ set_nvme_controller_info(extdrive);
+ }
+
+ extdrive->vendors = gg_slist_remove_null(extdrive->vendors);
+ extdrive->vendors = vendor_list_remove_duplicates_deep(extdrive->vendors);
+
+ }
+ return list;
+}
+
+
+u2driveext* u2drive_ext(udiskd * udisks_drive_data) {
+ u2driveext* data = g_new0(u2driveext, 1);
+ data->d = udisks_drive_data;
+ return data;
+}
+
+void u2driveext_free(u2driveext *u) {
+ if (u) {
+ udiskd_free(u->d);
+ pcid_free(u->nvme_controller);
+ g_free(u);
+ }
+}
+
+void storage_shutdown(){
+ g_free(sdcard_ids_file);
+}
diff --git a/hardinfo/udisks2_util.c b/hardinfo/udisks2_util.c
index c45b8f8d..e86e59d3 100644
--- a/hardinfo/udisks2_util.c
+++ b/hardinfo/udisks2_util.c
@@ -1,7 +1,5 @@
#include <gio/gio.h>
#include "udisks2_util.h"
-#include "hardinfo.h"
-#include "util_ids.h"
#define UDISKS2_INTERFACE "org.freedesktop.UDisks2"
#define UDISKS2_MANAGER_INTERFACE "org.freedesktop.UDisks2.Manager"
@@ -19,109 +17,6 @@
GDBusConnection* udisks2_conn = NULL;
-gchar *sdcard_ids_file = NULL;
-
-void find_sdcard_ids_file() {
- if (sdcard_ids_file) return;
- char *file_search_order[] = {
- g_build_filename(g_get_user_config_dir(), "hardinfo", "sdcard.ids", NULL),
- g_build_filename(params.path_data, "sdcard.ids", NULL),
- NULL
- };
- int n;
- for(n = 0; file_search_order[n]; n++) {
- if (!sdcard_ids_file && !access(file_search_order[n], R_OK))
- sdcard_ids_file = file_search_order[n];
- else
- g_free(file_search_order[n]);
- }
-}
-
-void check_sdcard_vendor(udiskd *d) {
- if (!d) return;
- if (!d->media) return;
- if (! (g_str_has_prefix(d->media, "flash_sd")
- || g_str_has_prefix(d->media, "flash_mmc") )) return;
- if (d->vendor && d->vendor[0]) return;
- if (!d->block_dev) return;
-
- if (!sdcard_ids_file)
- find_sdcard_ids_file();
-
- gchar *qpath = NULL;
- ids_query_result result = {};
-
- gchar *oemid_path = g_strdup_printf("/sys/block/%s/device/oemid", d->block_dev);
- gchar *manfid_path = g_strdup_printf("/sys/block/%s/device/manfid", d->block_dev);
- gchar *oemid = NULL, *manfid = NULL;
- g_file_get_contents(oemid_path, &oemid, NULL, NULL);
- g_file_get_contents(manfid_path, &manfid, NULL, NULL);
-
- unsigned int id = strtol(oemid, NULL, 16);
- char c2 = id & 0xff, c1 = (id >> 8) & 0xff;
-
- qpath = g_strdup_printf("OEMID %02x%02x", (unsigned int)c1, (unsigned int)c2);
- scan_ids_file(sdcard_ids_file, qpath, &result, -1);
- g_free(oemid);
- if (result.results[0])
- oemid = g_strdup(result.results[0]);
- else
- oemid = g_strdup_printf("OEM %02x%02x \"%c%c\"",
- (unsigned int)c1, (unsigned int)c2,
- isprint(c1) ? c1 : '.', isprint(c2) ? c2 : '.');
- g_free(qpath);
-
- id = strtol(manfid, NULL, 16);
- qpath = g_strdup_printf("MANFID %06x", id);
- scan_ids_file(sdcard_ids_file, qpath, &result, -1);
- g_free(manfid);
- if (result.results[0])
- manfid = g_strdup(result.results[0]);
- else
- manfid = g_strdup_printf("MANF %06x", id);
- g_free(qpath);
-
- vendor_list vl = NULL;
- const Vendor *v = NULL;
- v = vendor_match(oemid, NULL);
- if (v) vl = vendor_list_append(vl, v);
- v = vendor_match(manfid, NULL);
- if (v) vl = vendor_list_append(vl, v);
- vl = vendor_list_remove_duplicates_deep(vl);
- d->vendors = vendor_list_concat(d->vendors, vl);
-
- g_free(d->vendor);
- if (g_strcmp0(oemid, manfid) == 0)
- d->vendor = g_strdup(oemid);
- else
- d->vendor = g_strdup_printf("%s / %s", oemid, manfid);
-
- g_free(oemid);
- g_free(manfid);
- g_free(oemid_path);
- g_free(manfid_path);
-
- if (d->revision && d->revision[0]) return;
-
- /* bonus: revision */
- gchar *fwrev_path = g_strdup_printf("/sys/block/%s/device/fwrev", d->block_dev);
- gchar *hwrev_path = g_strdup_printf("/sys/block/%s/device/hwrev", d->block_dev);
- gchar *fwrev = NULL, *hwrev = NULL;
- g_file_get_contents(fwrev_path, &fwrev, NULL, NULL);
- g_file_get_contents(hwrev_path, &hwrev, NULL, NULL);
-
- unsigned int fw = fwrev ? strtol(fwrev, NULL, 16) : 0;
- unsigned int hw = hwrev ? strtol(hwrev, NULL, 16) : 0;
- g_free(d->revision);
- d->revision = g_strdup_printf("%02x.%02x", hw, fw);
-
- g_free(fwrev);
- g_free(hwrev);
- g_free(fwrev_path);
- g_free(hwrev_path);
-
-}
-
GVariant* get_dbus_property(GDBusProxy* proxy, const gchar *interface,
const gchar *property) {
GVariant *result, *v = NULL;
@@ -375,7 +270,6 @@ void udiskd_free(udiskd *u) {
udisksa_free(u->smart_attributes);
g_free(u->media);
g_strfreev(u->media_compatibility);
- pcid_free(u->nvme_controller);
g_free(u);
}
}
@@ -708,37 +602,6 @@ gpointer get_udisks2_drive_info(const char *blockdev, GDBusProxy *block,
g_variant_unref(v);
}
- if (!u->vendor || !*u->vendor) {
- const Vendor *v = vendor_match(u->model, NULL);
- if (v)
- u->vendor = g_strdup(v->name);
- }
-
- check_sdcard_vendor(u);
-
- u->vendors = vendor_list_append(u->vendors, vendor_match(u->vendor, NULL));
-
- /* NVMe PCI device */
- if (strstr(u->block_dev, "nvme")) {
- gchar *path = g_strdup_printf("/sys/block/%s/device/device", u->block_dev);
- gchar *systarget = g_file_read_link(path, NULL);
- gchar *target = systarget ? g_filename_to_utf8(systarget, -1, NULL, NULL, NULL) : NULL;
- gchar *pci_addy = target ? g_path_get_basename(target) : NULL;
- u->nvme_controller = pci_addy ? pci_get_device_str(pci_addy) : NULL;
- g_free(path);
- g_free(systarget);
- g_free(target);
- g_free(pci_addy);
- if (u->nvme_controller) {
- u->vendors = vendor_list_append(u->vendors,
- vendor_match(u->nvme_controller->vendor_id_str, NULL));
- u->vendors = vendor_list_append(u->vendors,
- vendor_match(u->nvme_controller->sub_vendor_id_str, NULL));
- }
- }
- u->vendors = gg_slist_remove_null(u->vendors);
- u->vendors = vendor_list_remove_duplicates_deep(u->vendors);
-
return u;
}
@@ -761,5 +624,4 @@ void udisks2_shutdown(){
g_object_unref(udisks2_conn);
udisks2_conn = NULL;
}
- g_free(sdcard_ids_file);
}
diff --git a/includes/storage_util.h b/includes/storage_util.h
new file mode 100644
index 00000000..8dfed42f
--- /dev/null
+++ b/includes/storage_util.h
@@ -0,0 +1,17 @@
+#include "vendor.h"
+#include "pci_util.h"
+
+// udisks2 drive info extended
+typedef struct u2driveext {
+ udiskd *d;
+ pcid *nvme_controller;
+ vendor_list vendors;
+} u2driveext;
+
+
+GSList *get_udisks2_drives_ext();
+
+u2driveext* u2drive_ext(udiskd * udisks_drive_data);
+void u2driveext_free(u2driveext *u);
+
+void udisks2_shutdown();
diff --git a/includes/udisks2_util.h b/includes/udisks2_util.h
index a64ca3e2..0af221b9 100644
--- a/includes/udisks2_util.h
+++ b/includes/udisks2_util.h
@@ -1,5 +1,4 @@
#include "vendor.h"
-#include "pci_util.h"
typedef struct udiskp {
gchar *block;
@@ -55,8 +54,6 @@ typedef struct udiskd {
gint64 smart_bad_sectors;
gint32 smart_temperature;
udisksa *smart_attributes;
- pcid *nvme_controller;
- vendor_list vendors;
} udiskd;
typedef struct udiskt {
diff --git a/modules/devices.c b/modules/devices.c
index 7c8e5006..7a113e32 100644
--- a/modules/devices.c
+++ b/modules/devices.c
@@ -39,6 +39,7 @@
#include "devices.h"
#include "dt_util.h"
#include "udisks2_util.h"
+#include "pci_util.h"
gchar *callback_processors();
gchar *callback_gpu();
@@ -883,6 +884,7 @@ void hi_module_deinit(void)
{
moreinfo_del_with_prefix("DEV");
sensor_shutdown();
+ storage_shutdown();
udisks2_shutdown();
g_module_close(cups);
}
diff --git a/modules/devices/storage.c b/modules/devices/storage.c
index 4475e868..8bbb524d 100644
--- a/modules/devices/storage.c
+++ b/modules/devices/storage.c
@@ -1,7 +1,7 @@
/*
* HardInfo - Displays System Information
* Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@hardinfo.org>
- * modified by Ondrej Čerman (2019)
+ * modified by Ondrej Čerman (2019-2021)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -22,6 +22,7 @@
#include "hardinfo.h"
#include "devices.h"
#include "udisks2_util.h"
+#include "storage_util.h"
#define UNKIFNULL_AC(f) (f != NULL) ? f : _("(Unknown)");
@@ -79,6 +80,7 @@ gchar *nvme_pci_sections(pcid *p) {
gboolean __scan_udisks2_devices(void) {
GSList *node, *drives;
+ u2driveext *ext;
udiskd *disk;
udiskp *part;
udisksa *attrib;
@@ -196,9 +198,10 @@ gboolean __scan_udisks2_devices(void) {
moreinfo_del_with_prefix("DEV:UDISKS");
udisks2_storage_list = g_strdup(_("\n[UDisks2]\n"));
- drives = get_udisks2_all_drives_info();
+ drives = get_udisks2_drives_ext();
for (node = drives; node != NULL; node = node->next) {
- disk = (udiskd *)node->data;
+ ext = (u2driveext *)node->data;
+ disk = ext->d;
devid = g_strdup_printf("UDISKS%d", n++);
icon = NULL;
@@ -242,7 +245,7 @@ gboolean __scan_udisks2_devices(void) {
}
size = size_human_readable((gfloat) disk->size);
- ven_tag = vendor_list_ribbon(disk->vendors, params.fmt_opts);
+ ven_tag = vendor_list_ribbon(ext->vendors, params.fmt_opts);
udisks2_storage_list = h_strdup_cprintf("$%s$%s=%s|%s %s\n", udisks2_storage_list, devid, disk->block_dev, size, ven_tag ? ven_tag : "", disk->model);
storage_icons = h_strdup_cprintf("Icon$%s$%s=%s.png\n", storage_icons, devid, disk->model, icon);
@@ -301,8 +304,8 @@ gboolean __scan_udisks2_devices(void) {
if (disk->connection_bus && strlen(disk->connection_bus) > 0) {
moreinfo = h_strdup_cprintf(_("Connection bus=%s\n"), moreinfo, disk->connection_bus);
}
- if (disk->nvme_controller) {
- gchar *nvme = nvme_pci_sections(disk->nvme_controller);
+ if (ext->nvme_controller) {
+ gchar *nvme = nvme_pci_sections(ext->nvme_controller);
if (nvme)
moreinfo = h_strdup_cprintf("%s", moreinfo, nvme);
g_free(nvme);
@@ -428,7 +431,7 @@ gboolean __scan_udisks2_devices(void) {
moreinfo = NULL;
devid = NULL;
- udiskd_free(disk);
+ u2driveext_free(ext);
}
g_slist_free(drives);