From 14511b19ea6abfb845340066aa0a97b5b045fd2f Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 12 Aug 2010 20:41:28 +0200 Subject: [PATCH] Make $fs_used_perc and $fs_bar report used space correctly they reported fs->size - fs->avail, which is not correct if fs has super-user reserved blocks. note that now $fs_used_perc and $fs_free_perc need not add up to 100%, but that is consistent with what $fs_used and $fs_free do. --- src/fs.cc | 48 +++++++++++++++--------------------------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/src/fs.cc b/src/fs.cc index 7fdddcd8..197b552f 100644 --- a/src/fs.cc +++ b/src/fs.cc @@ -216,31 +216,30 @@ void init_fs_bar(struct text_object *obj, const char *arg) obj->data.opaque = prepare_fs_stat(arg); } -static double do_fs_barval(struct text_object *obj, int be_free_bar) +static double get_fs_perc(struct text_object *obj, bool get_free) { - double val = 1.0; - struct fs_stat *fs = (struct fs_stat *)obj->data.opaque; + struct fs_stat *fs = static_cast(obj->data.opaque); + double ret = 0.0; - if (!fs) - return 0; + if(fs && fs->size) { + if(get_free) + ret = fs->avail; + else + ret = fs->size - fs->free; + ret /= fs->size; + } - if (fs->size) - val = (double)fs->avail / fs->size; - - if (!be_free_bar) - val = 1.0 - val; - - return val; + return ret; } double fs_barval(struct text_object *obj) { - return do_fs_barval(obj, 0); + return get_fs_perc(obj, false); } double fs_free_barval(struct text_object *obj) { - return do_fs_barval(obj, 1); + return get_fs_perc(obj, true); } void init_fs(struct text_object *obj, const char *arg) @@ -248,31 +247,14 @@ void init_fs(struct text_object *obj, const char *arg) obj->data.opaque = prepare_fs_stat(arg ? arg : "/"); } -static uint8_t fs_percentage(struct text_object *obj, int be_free) -{ - struct fs_stat *fs = (struct fs_stat *)obj->data.opaque; - int val = 100; - - if (!fs) - return 0; - - if (fs->size) - val = fs->avail * 100 / fs->size; - - if (!be_free) - val = 100 - val; - - return val; -} - uint8_t fs_free_percentage(struct text_object *obj) { - return fs_percentage(obj, 1); + return get_fs_perc(obj, true) * 100; } uint8_t fs_used_percentage(struct text_object *obj) { - return fs_percentage(obj, 0); + return get_fs_perc(obj, false) * 100; } #define HUMAN_PRINT_FS_GENERATOR(name, expr) \