mirror of
https://gitlab.freedesktop.org/upower/upower.git
synced 2026-04-05 07:50:36 +02:00
linux: Better error reporting from sysfs_get_double_with_error()
sysfs_get_double_with_error() used to return -1.0 for errors, but in some cases, we might want -1.0 to be a valid value, such as for negative discharge rates. Return FALSE on error instead.
This commit is contained in:
parent
041e70867f
commit
c4ca520f24
3 changed files with 19 additions and 10 deletions
|
|
@ -43,23 +43,31 @@
|
|||
|
||||
#include "sysfs-utils.h"
|
||||
|
||||
double
|
||||
sysfs_get_double_with_error (const char *dir, const char *attribute)
|
||||
gboolean
|
||||
sysfs_get_double_with_error (const char *dir,
|
||||
const char *attribute,
|
||||
double *value)
|
||||
{
|
||||
double result;
|
||||
char *contents;
|
||||
char *filename;
|
||||
gboolean ret = FALSE;
|
||||
double parsed;
|
||||
|
||||
g_return_val_if_fail (value != NULL, FALSE);
|
||||
|
||||
filename = g_build_filename (dir, attribute, NULL);
|
||||
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
|
||||
result = g_ascii_strtod (contents, NULL);
|
||||
parsed = g_ascii_strtod (contents, NULL);
|
||||
if (errno == 0)
|
||||
ret = TRUE;
|
||||
g_free (contents);
|
||||
} else {
|
||||
result = -1.0;
|
||||
}
|
||||
g_free (filename);
|
||||
|
||||
return result;
|
||||
if (ret)
|
||||
*value = parsed;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
double
|
||||
|
|
|
|||
|
|
@ -28,6 +28,8 @@ char *sysfs_get_string (const char *dir, const char *attribute);
|
|||
int sysfs_get_int (const char *dir, const char *attribute);
|
||||
gboolean sysfs_get_bool (const char *dir, const char *attribute);
|
||||
gboolean sysfs_file_exists (const char *dir, const char *attribute);
|
||||
double sysfs_get_double_with_error (const char *dir, const char *attribute);
|
||||
gboolean sysfs_get_double_with_error (const char *dir,
|
||||
const char *attribute,
|
||||
double *value);
|
||||
|
||||
#endif /* __SYSFS_UTILS_H__ */
|
||||
|
|
|
|||
|
|
@ -959,8 +959,7 @@ up_device_supply_refresh_device (UpDeviceSupply *supply,
|
|||
}
|
||||
|
||||
/* get a precise percentage */
|
||||
percentage = sysfs_get_double_with_error (native_path, "capacity");
|
||||
if (percentage < 0.0)
|
||||
if (!sysfs_get_double_with_error (native_path, "capacity", &percentage))
|
||||
percentage = sysfs_get_capacity_level (native_path, &level);
|
||||
|
||||
if (percentage < 0.0) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue