diff options
| author | Naohiro Aota <naohiro.aota@wdc.com> | 2021-08-30 14:52:55 +0900 |
|---|---|---|
| committer | Karel Zak <kzak@redhat.com> | 2021-08-31 10:23:47 +0200 |
| commit | abb6e8e67a8d14a13cff7582bad4f9c8f3507b77 (patch) | |
| tree | aa4e12f17a6758ea00f3ccd5cdfa871740404178 /misc-utils/lsblk.c | |
| parent | 28d6722d930566429f9f371c05ca7101b9af3b36 (diff) | |
| download | util-linux-abb6e8e67a8d14a13cff7582bad4f9c8f3507b77.tar.gz | |
lsblk: factor out function to read sysfs param as bytes
Factor out a new function device_read_bytes() to read a sysfs path as bytes
for a preparation for the next commit and to reduce the code duplication.
Reviewed-by: Damien Le Moal <damien.lemoal@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Diffstat (limited to 'misc-utils/lsblk.c')
| -rw-r--r-- | misc-utils/lsblk.c | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 100eba0779..9c41e70ada 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -708,6 +708,25 @@ static uint64_t device_get_discard_granularity(struct lsblk_device *dev) return dev->discard_granularity; } +static void device_read_bytes(struct lsblk_device *dev, char *path, char **str, + uint64_t *sortdata) +{ + uint64_t x; + + if (lsblk->bytes) { + ul_path_read_string(dev->sysfs, str, path); + if (sortdata) + str2u64(*str, sortdata); + return; + } + + if (ul_path_read_u64(dev->sysfs, &x, path) == 0) { + *str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); + if (sortdata) + *sortdata = x; + } +} + /* * Generates data (string) for column specified by column ID for specified device. If sortdata * is not NULL then returns number usable to sort the column if the data are available for the @@ -1033,18 +1052,7 @@ static char *device_get_data( } break; case COL_DMAX: - if (lsblk->bytes) { - ul_path_read_string(dev->sysfs, &str, "queue/discard_max_bytes"); - if (sortdata) - str2u64(str, sortdata); - } else { - uint64_t x; - if (ul_path_read_u64(dev->sysfs, &x, "queue/discard_max_bytes") == 0) { - str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); - if (sortdata) - *sortdata = x; - } - } + device_read_bytes(dev, "queue/discard_max_bytes", &str, sortdata); break; case COL_DZERO: if (device_get_discard_granularity(dev) > 0) @@ -1053,19 +1061,7 @@ static char *device_get_data( str = xstrdup("0"); break; case COL_WSAME: - if (lsblk->bytes) { - ul_path_read_string(dev->sysfs, &str, "queue/write_same_max_bytes"); - if (sortdata) - str2u64(str, sortdata); - } else { - uint64_t x; - - if (ul_path_read_u64(dev->sysfs, &x, "queue/write_same_max_bytes") == 0) { - str = size_to_human_string(SIZE_SUFFIX_1LETTER, x); - if (sortdata) - *sortdata = x; - } - } + device_read_bytes(dev, "queue/write_same_max_bytes", &str, sortdata); if (!str) str = xstrdup("0"); break; |
